2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-09-18 15:11:08 +08:00
|
|
|
* Copyright (C) 2017-2018 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 <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IntegerAddressType>
|
|
|
|
inline void *addrToPtr(IntegerAddressType addr) {
|
|
|
|
uintptr_t correctBitnessAddress = static_cast<uintptr_t>(addr);
|
|
|
|
void *ptrReturn = reinterpret_cast<void *>(correctBitnessAddress);
|
|
|
|
return ptrReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void patchWithRequiredSize(void *memoryToBePatched, uint32_t patchSize, uintptr_t patchValue) {
|
|
|
|
if (patchSize == sizeof(uint64_t)) {
|
|
|
|
uint64_t *curbeAddress = (uint64_t *)memoryToBePatched;
|
|
|
|
*curbeAddress = patchValue;
|
|
|
|
} else {
|
|
|
|
uint32_t *curbeAddress = (uint32_t *)memoryToBePatched;
|
|
|
|
*curbeAddress = (uint32_t)patchValue;
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 15:42:00 +08:00
|
|
|
|
|
|
|
inline uint64_t castToUint64(void *address) {
|
|
|
|
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(address));
|
|
|
|
}
|