remotemono
IPCVector.h
1 /*
2  Copyright 2020 David "Alemarius Nexus" Lerch
3 
4  This file is part of RemoteMono.
5 
6  RemoteMono is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  RemoteMono is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with RemoteMono. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include "config.h"
23 
24 #include <stdint.h>
25 #include <BlackBone/Patterns/PatternSearch.h>
26 #include <BlackBone/Process/Process.h>
27 #include <BlackBone/Process/RPC/RemoteFunction.hpp>
28 
29 
30 
31 namespace remotemono
32 {
33 
34 
35 // NOTE: We need these classes to specify the calling convention explicitly. blackbone::RemoteFunction gets it from
36 // the function signature, but for x64 locals, function pointer types that differ only in the calling convention (e.g.
37 // the __fastcall prefix) are indistinguishable (all aliases for Microsoft's x64 calling convention), so BlackBone will
38 // always detect them as __cdecl, even when defined as __fastcall. This is a problem with x86 remotes, where these
39 // calling conventions still make a difference.
40 template<typename Fn>
42 
43 template<typename R, typename... Args>
44 class RemoteFunctionFastcall<R ( __fastcall* )(Args...)> : public blackbone::RemoteFunctionBase<blackbone::cc_fastcall, R, Args...>
45 {
46 public:
47  using RemoteFunctionBase::RemoteFunctionBase;
48 
49  RemoteFunctionFastcall( blackbone::Process& proc, R( __fastcall* ptr )(Args...), blackbone::ThreadPtr boundThread = nullptr )
50  : blackbone::RemoteFunctionBase( proc, reinterpret_cast<ptr_t>(ptr), boundThread )
51  {
52  }
53 };
54 
55 
56 
65 template <typename ElemT, typename IntPtrT>
66 class IPCVector
67 {
68 public:
69  typedef IntPtrT VectorPtr;
70  typedef IntPtrT DataPtr;
71 
72  typedef VectorPtr (__fastcall *VECTOR_NEW)(uint32_t cap);
73  typedef void (__fastcall *VECTOR_FREE)(VectorPtr v);
74  typedef void (__fastcall *VECTOR_ADD)(VectorPtr v, ElemT elem);
75  typedef void (__fastcall *VECTOR_CLEAR)(VectorPtr v);
76  typedef uint32_t (__fastcall *VECTOR_LENGTH)(VectorPtr v);
77  typedef uint32_t (__fastcall *VECTOR_CAPACITY)(VectorPtr v);
78  typedef DataPtr (__fastcall *VECTOR_DATA)(VectorPtr v);
79 
80  typedef void (__fastcall *VECTOR_GROW)(VectorPtr v, uint32_t);
81 
82  struct VectorAPI
83  {
84  blackbone::ptr_t vectorNew;
85  blackbone::ptr_t vectorFree;
86  blackbone::ptr_t vectorAdd;
87  blackbone::ptr_t vectorClear;
88  blackbone::ptr_t vectorLength;
89  blackbone::ptr_t vectorCapacity;
90  blackbone::ptr_t vectorData;
91 
92  blackbone::ptr_t vectorGrow;
93  };
95  {
96  VECTOR_NEW vectorNew;
97  VECTOR_FREE vectorFree;
98  VECTOR_ADD vectorAdd;
99  VECTOR_CLEAR vectorClear;
100  VECTOR_LENGTH vectorLength;
101  VECTOR_CAPACITY vectorCapacity;
102  VECTOR_DATA vectorData;
103 
104  VECTOR_GROW vectorGrow;
105  };
107  {
115 
117  };
118 
119 private:
120  struct Vector
121  {
122  DataPtr data;
123  uint32_t len;
124  uint32_t cap;
125  };
126 
127 public:
128  IPCVector();
129  virtual ~IPCVector();
130 
131  void inject(blackbone::Process* process);
132  void uninject();
133 
134  VectorAPI& getAPI() { return api; }
135 
136  VectorPtr vectorNew(uint32_t cap = 16);
137  void vectorFree(VectorPtr v);
138  void vectorAdd(VectorPtr v, ElemT elem);
139  void vectorClear(VectorPtr v);
140  uint32_t vectorLength(VectorPtr v);
141  uint32_t vectorCapacity(VectorPtr v);
142  DataPtr vectorData(VectorPtr v);
143 
144  void vectorGrow(VectorPtr v, uint32_t cap);
145 
146  VectorPtr create(const std::vector<ElemT>& data);
147  void read(VectorPtr v, std::vector<ElemT>& out);
148 
149 private:
150  blackbone::Process* process;
151  bool injected;
152  VectorAPI api;
153  VectorLocalAPI localApi;
154  VectorRemoteAPI* remAPI;
155  blackbone::MemBlock remoteCode;
156  void* code;
157 };
158 
159 
160 }
161 
162 
163 
164 
165 #include "IPCVector_Impl.h"
remotemono::IPCVector::VectorLocalAPI
Definition: IPCVector.h:95
remotemono::RemoteFunctionFastcall
Definition: IPCVector.h:41
remotemono::IPCVector::VectorRemoteAPI
Definition: IPCVector.h:107
remotemono::IPCVector
Definition: IPCVector.h:67
remotemono::IPCVector::VectorAPI
Definition: IPCVector.h:83