2017-12-21 00:45:38 +01:00
|
|
|
/*
|
2023-04-20 13:41:56 +00:00
|
|
|
* Copyright (C) 2018-2023 Intel Corporation
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
2018-09-18 09:11:08 +02:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 00:45:38 +01:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2023-04-20 13:41:56 +00:00
|
|
|
#include "shared/source/helpers/debug_helpers.h"
|
|
|
|
|
|
2017-12-21 00:45:38 +01:00
|
|
|
#include <cstddef>
|
2019-02-27 11:39:32 +01:00
|
|
|
#include <cstdint>
|
2023-09-19 11:40:49 +00:00
|
|
|
#include <limits>
|
2017-12-21 00:45:38 +01:00
|
|
|
|
2022-08-26 12:39:52 +00:00
|
|
|
inline const int ptrGarbageContent[16] = {
|
2017-12-21 00:45:38 +01:00
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
|
0x0131, 0x133, 0xA, 0xEF,
|
|
|
|
|
0x0131, 0x133, 0xA, 0xEF};
|
2022-08-26 12:39:52 +00:00
|
|
|
inline const auto ptrGarbage = (void *)ptrGarbageContent;
|
2017-12-21 00:45:38 +01:00
|
|
|
|
|
|
|
|
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 12:24:24 +01:00
|
|
|
template <>
|
|
|
|
|
inline uint64_t ptrOffset(uint64_t ptrBefore, size_t offset) {
|
|
|
|
|
return ptrBefore + offset;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01: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 12:24:24 +01:00
|
|
|
template <typename T>
|
|
|
|
|
inline uint64_t ptrDiff(uint64_t ptrAfter, T ptrBefore) {
|
|
|
|
|
return ptrAfter - ptrBefore;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 00:45:38 +01: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-27 19:48:26 +01: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 00:45:38 +01:00
|
|
|
if (patchSize == sizeof(uint64_t)) {
|
2019-10-27 19:48:26 +01:00
|
|
|
uint64_t *curbeAddress = reinterpret_cast<uint64_t *>(memoryToBePatched);
|
2020-10-07 15:32:03 +02:00
|
|
|
PatchStoreOperation{}(curbeAddress, patchValue);
|
2023-04-20 13:41:56 +00:00
|
|
|
} else if (patchSize == sizeof(uint32_t)) {
|
2019-10-27 19:48:26 +01:00
|
|
|
uint32_t *curbeAddress = reinterpret_cast<uint32_t *>(memoryToBePatched);
|
2020-10-07 15:32:03 +02:00
|
|
|
PatchStoreOperation{}(curbeAddress, static_cast<uint32_t>(patchValue));
|
2023-04-20 13:41:56 +00:00
|
|
|
} else {
|
|
|
|
|
UNRECOVERABLE_IF(patchSize != 0);
|
2017-12-21 00:45:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 09:42:00 +02:00
|
|
|
|
2019-12-03 10:21:33 +01:00
|
|
|
inline uint64_t castToUint64(const void *address) {
|
2019-12-03 12:42:47 +01:00
|
|
|
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(const_cast<void *>(address)));
|
2019-12-03 10:21:33 +01:00
|
|
|
}
|
2023-09-19 11:40:49 +00:00
|
|
|
|
|
|
|
|
inline uint32_t getLowPart(uint64_t value) {
|
|
|
|
|
return static_cast<uint32_t>(value & std::numeric_limits<uint32_t>::max());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline uint32_t getHighPart(uint64_t value) {
|
|
|
|
|
return static_cast<uint32_t>(value >> 32);
|
|
|
|
|
}
|