21 #ifndef HIPPOMOCKS_REPLACE_H 22 #define HIPPOMOCKS_REPLACE_H 24 # if defined(_M_IX86) || defined(__i386__) || defined(i386) || defined(_X86_) || defined(__THW_INTEL) || defined(__x86_64__) || defined(_M_X64) 26 # elif defined(arm) || defined(__arm__) || defined(ARM) || defined(_ARM_) || defined(__aarch64__) 30 # if defined(__x86_64__) || defined(_M_X64) 31 # define CMOCK_FUNC_PLATFORMIS64BIT 35 # if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64)) 36 # define _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT 37 # elif defined(__linux__) && defined(__GNUC__) 38 # define _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT 39 # elif defined(__APPLE__) 40 # define _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT 42 # elif defined(SOME_ARM) && defined(__GNUC__) 43 # define _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT 46 extern "C" void __clear_cache(
char *beg,
char *
end);
49 # ifdef _HIPPOMOCKS__ENABLE_CFUNC_MOCKING_SUPPORT 55 extern "C" __declspec(dllimport)
int WINCALL VirtualProtect(
void *func,
unsigned long long byteCount,
unsigned long flags,
unsigned long *oldFlags);
57 extern "C" __declspec(dllimport)
int WINCALL VirtualProtect(
void *func,
unsigned long byteCount,
unsigned long flags,
unsigned long *oldFlags);
60 # ifndef PAGE_EXECUTE_READWRITE 61 # define PAGE_EXECUTE_READWRITE 0x40 64 # ifndef NO_HIPPOMOCKS_NAMESPACE 72 Unprotect(
void *location,
size_t byteCount)
74 , byteCount(byteCount)
76 VirtualProtect(origFunc, byteCount, PAGE_EXECUTE_READWRITE, &oldprotect);
80 unsigned long dontcare;
81 VirtualProtect(origFunc, byteCount, oldprotect, &dontcare);
86 unsigned long oldprotect;
89 # include <sys/mman.h> 92 # ifndef NO_HIPPOMOCKS_NAMESPACE 101 Unprotect(
void *location,
size_t count)
102 : origFunc((intptr_t)location & (~0xFFF))
103 , byteCount(count + ((intptr_t)location - origFunc))
105 mprotect((
void *)origFunc, this->byteCount, PROT_READ | PROT_WRITE | PROT_EXEC);
109 mprotect((
void *)origFunc, byteCount, PROT_READ | PROT_EXEC);
117 typedef unsigned int e9ptrsize_t;
119 template <
typename T,
typename U>
122 union { T t; U u; } un;
133 template <
typename T>
134 Replace(T funcptr, T replacement)
135 : origFunc(horrible_cast<void *>(funcptr))
137 Unprotect _allow_write(origFunc,
sizeof(backupData));
138 memcpy(backupData, origFunc,
sizeof(backupData));
140 # ifdef CMOCK_FUNC_PLATFORMIS64BIT 141 if (llabs((
long long)origFunc - (
long long)replacement) < 0x80000000LL)
144 *(
unsigned char *)origFunc = 0xE9;
145 *(e9ptrsize_t *)(horrible_cast<intptr_t>(origFunc) + 1) = (e9ptrsize_t)(horrible_cast<intptr_t>(replacement) - horrible_cast<intptr_t>(origFunc) -
sizeof(e9ptrsize_t) - 1);
146 # ifdef CMOCK_FUNC_PLATFORMIS64BIT 150 unsigned char *func = (
unsigned char *)origFunc;
157 *(
long long *)(horrible_cast<intptr_t>(origFunc) + 6) = (
long long)(horrible_cast<intptr_t>(replacement));
160 # elif defined(SOME_ARM) 161 unsigned int *rawptr = (
unsigned int *)((intptr_t)(origFunc) & (~3));
162 if ((intptr_t)origFunc & 1)
164 rawptr[0] = 0x6800A001;
165 rawptr[1] = 0x46874687;
166 rawptr[2] = (intptr_t)replacement;
170 rawptr[0] = 0xE59FF000;
171 rawptr[1] = (intptr_t)replacement;
172 rawptr[2] = (intptr_t)replacement;
174 __clear_cache((
char *)rawptr, (
char *)rawptr + 16);
179 Unprotect _allow_write(origFunc,
sizeof(backupData));
180 memcpy(origFunc, backupData,
sizeof(backupData));
182 unsigned int *rawptr = (
unsigned int *)((intptr_t)(origFunc) & (~3));
183 __clear_cache((
char *)rawptr, (
char *)rawptr + 16);
189 # ifndef NO_HIPPOMOCKS_NAMESPACE
auto end(reversed_container< T > container) -> decltype(container.container.rend())