2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-02-23 05:21:06 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
2019-02-27 18:39:32 +08:00
|
|
|
#include <cstdint>
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
static const int ptrGarbageContent[16] = {
|
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
0x0131, 0x133, 0xA, 0xEF};
|
|
|
|
static const auto ptrGarbage = (void *)ptrGarbageContent;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline T ptrOffset(T ptrBefore, size_t offset) {
|
|
|
|
auto addrBefore = (uintptr_t)ptrBefore;
|
|
|
|
auto addrAfter = addrBefore + offset;
|
|
|
|
return (T)addrAfter;
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:24:24 +08:00
|
|
|
template <>
|
|
|
|
inline uint64_t ptrOffset(uint64_t ptrBefore, size_t offset) {
|
|
|
|
return ptrBefore + offset;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
template <typename TA, typename TB>
|
|
|
|
inline size_t ptrDiff(TA ptrAfter, TB ptrBefore) {
|
|
|
|
auto addrBefore = (uintptr_t)ptrBefore;
|
|
|
|
auto addrAfter = (uintptr_t)ptrAfter;
|
|
|
|
return addrAfter - addrBefore;
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:24:24 +08:00
|
|
|
template <typename T>
|
|
|
|
inline uint64_t ptrDiff(uint64_t ptrAfter, T ptrBefore) {
|
|
|
|
return ptrAfter - ptrBefore;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
template <typename IntegerAddressType>
|
|
|
|
inline void *addrToPtr(IntegerAddressType addr) {
|
|
|
|
uintptr_t correctBitnessAddress = static_cast<uintptr_t>(addr);
|
|
|
|
void *ptrReturn = reinterpret_cast<void *>(correctBitnessAddress);
|
|
|
|
return ptrReturn;
|
|
|
|
}
|
|
|
|
|
2019-10-28 02:48:26 +08:00
|
|
|
struct PatchStoreOperation {
|
|
|
|
template <typename T>
|
|
|
|
void operator()(T *memory, T value) {
|
|
|
|
*memory = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void patchWithRequiredSize(void *memoryToBePatched, uint32_t patchSize, uint64_t patchValue) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (patchSize == sizeof(uint64_t)) {
|
2019-10-28 02:48:26 +08:00
|
|
|
uint64_t *curbeAddress = reinterpret_cast<uint64_t *>(memoryToBePatched);
|
2020-10-07 21:32:03 +08:00
|
|
|
PatchStoreOperation{}(curbeAddress, patchValue);
|
2017-12-21 07:45:38 +08:00
|
|
|
} else {
|
2019-10-28 02:48:26 +08:00
|
|
|
uint32_t *curbeAddress = reinterpret_cast<uint32_t *>(memoryToBePatched);
|
2020-10-07 21:32:03 +08:00
|
|
|
PatchStoreOperation{}(curbeAddress, static_cast<uint32_t>(patchValue));
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 15:42:00 +08:00
|
|
|
|
2019-12-03 17:21:33 +08:00
|
|
|
inline uint64_t castToUint64(const void *address) {
|
2019-12-03 19:42:47 +08:00
|
|
|
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(const_cast<void *>(address)));
|
2019-12-03 17:21:33 +08:00
|
|
|
}
|