/* * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include "shared/source/helpers/debug_helpers.h" #include #include inline const int ptrGarbageContent[16] = { 0x0131, 0x133, 0xA, 0xEF, 0x0131, 0x133, 0xA, 0xEF, 0x0131, 0x133, 0xA, 0xEF, 0x0131, 0x133, 0xA, 0xEF}; inline const auto ptrGarbage = (void *)ptrGarbageContent; template inline T ptrOffset(T ptrBefore, size_t offset) { auto addrBefore = (uintptr_t)ptrBefore; auto addrAfter = addrBefore + offset; return (T)addrAfter; } template <> inline uint64_t ptrOffset(uint64_t ptrBefore, size_t offset) { return ptrBefore + offset; } template inline size_t ptrDiff(TA ptrAfter, TB ptrBefore) { auto addrBefore = (uintptr_t)ptrBefore; auto addrAfter = (uintptr_t)ptrAfter; return addrAfter - addrBefore; } template inline uint64_t ptrDiff(uint64_t ptrAfter, T ptrBefore) { return ptrAfter - ptrBefore; } template inline void *addrToPtr(IntegerAddressType addr) { uintptr_t correctBitnessAddress = static_cast(addr); void *ptrReturn = reinterpret_cast(correctBitnessAddress); return ptrReturn; } struct PatchStoreOperation { template void operator()(T *memory, T value) { *memory = value; } }; inline void patchWithRequiredSize(void *memoryToBePatched, uint32_t patchSize, uint64_t patchValue) { if (patchSize == sizeof(uint64_t)) { uint64_t *curbeAddress = reinterpret_cast(memoryToBePatched); PatchStoreOperation{}(curbeAddress, patchValue); } else if (patchSize == sizeof(uint32_t)) { uint32_t *curbeAddress = reinterpret_cast(memoryToBePatched); PatchStoreOperation{}(curbeAddress, static_cast(patchValue)); } else { UNRECOVERABLE_IF(patchSize != 0); } } inline uint64_t castToUint64(const void *address) { return static_cast(reinterpret_cast(const_cast(address))); }