2022-09-08 23:14:25 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/CodeAllocator.h"
|
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
#include "Luau/CodeGenCommon.h"
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2022-09-15 23:38:17 +01:00
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
2022-09-08 23:14:25 +01:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2022-09-15 23:38:17 +01:00
|
|
|
#endif
|
|
|
|
#ifndef NOMINMAX
|
2022-09-08 23:14:25 +01:00
|
|
|
#define NOMINMAX
|
2022-09-15 23:38:17 +01:00
|
|
|
#endif
|
2023-10-03 14:59:44 +01:00
|
|
|
#include <windows.h>
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
const size_t kPageSize = 4096;
|
|
|
|
#else
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-11-18 19:47:21 +00:00
|
|
|
#if defined(__FreeBSD__) && !(_POSIX_C_SOURCE >= 200112L)
|
|
|
|
const size_t kPageSize = getpagesize();
|
|
|
|
#else
|
2022-09-08 23:14:25 +01:00
|
|
|
const size_t kPageSize = sysconf(_SC_PAGESIZE);
|
|
|
|
#endif
|
2022-11-18 19:47:21 +00:00
|
|
|
#endif
|
2022-09-08 23:14:25 +01:00
|
|
|
|
2024-03-09 00:47:53 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
extern "C" void sys_icache_invalidate(void* start, size_t len);
|
|
|
|
#endif
|
|
|
|
|
2022-09-08 23:14:25 +01:00
|
|
|
static size_t alignToPageSize(size_t size)
|
|
|
|
{
|
|
|
|
return (size + kPageSize - 1) & ~(kPageSize - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2023-08-11 15:42:37 +01:00
|
|
|
static uint8_t* allocatePagesImpl(size_t size)
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2023-08-11 15:42:37 +01:00
|
|
|
|
|
|
|
return (uint8_t*)VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:42:37 +01:00
|
|
|
static void freePagesImpl(uint8_t* mem, size_t size)
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2023-08-11 15:42:37 +01:00
|
|
|
|
2022-09-08 23:14:25 +01:00
|
|
|
if (VirtualFree(mem, 0, MEM_RELEASE) == 0)
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(!"failed to deallocate block memory");
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void makePagesExecutable(uint8_t* mem, size_t size)
|
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT((uintptr_t(mem) & (kPageSize - 1)) == 0);
|
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
DWORD oldProtect;
|
|
|
|
if (VirtualProtect(mem, size, PAGE_EXECUTE_READ, &oldProtect) == 0)
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(!"Failed to change page protection");
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flushInstructionCache(uint8_t* mem, size_t size)
|
|
|
|
{
|
2023-06-16 18:35:18 +01:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM)
|
2022-09-08 23:14:25 +01:00
|
|
|
if (FlushInstructionCache(GetCurrentProcess(), mem, size) == 0)
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(!"Failed to flush instruction cache");
|
2023-06-16 18:35:18 +01:00
|
|
|
#endif
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-08-11 15:42:37 +01:00
|
|
|
static uint8_t* allocatePagesImpl(size_t size)
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2023-08-11 15:42:37 +01:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2023-09-01 18:58:27 +01:00
|
|
|
void* result = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_JIT, -1, 0);
|
2023-08-11 15:42:37 +01:00
|
|
|
#else
|
2023-09-01 18:58:27 +01:00
|
|
|
void* result = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
2023-08-11 15:42:37 +01:00
|
|
|
#endif
|
2023-09-01 18:58:27 +01:00
|
|
|
|
|
|
|
return (result == MAP_FAILED) ? nullptr : static_cast<uint8_t*>(result);
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 15:42:37 +01:00
|
|
|
static void freePagesImpl(uint8_t* mem, size_t size)
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2023-08-11 15:42:37 +01:00
|
|
|
|
|
|
|
if (munmap(mem, size) != 0)
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(!"Failed to deallocate block memory");
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void makePagesExecutable(uint8_t* mem, size_t size)
|
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT((uintptr_t(mem) & (kPageSize - 1)) == 0);
|
|
|
|
CODEGEN_ASSERT(size == alignToPageSize(size));
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
if (mprotect(mem, size, PROT_READ | PROT_EXEC) != 0)
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(!"Failed to change page protection");
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flushInstructionCache(uint8_t* mem, size_t size)
|
|
|
|
{
|
2024-03-09 00:47:53 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
sys_icache_invalidate(mem, size);
|
|
|
|
#else
|
2022-09-08 23:14:25 +01:00
|
|
|
__builtin___clear_cache((char*)mem, (char*)mem + size);
|
2024-03-09 00:47:53 +00:00
|
|
|
#endif
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
|
|
|
CodeAllocator::CodeAllocator(size_t blockSize, size_t maxTotalSize)
|
2023-08-11 15:42:37 +01:00
|
|
|
: CodeAllocator(blockSize, maxTotalSize, nullptr, nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeAllocator::CodeAllocator(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext)
|
|
|
|
: blockSize{blockSize}
|
|
|
|
, maxTotalSize{maxTotalSize}
|
|
|
|
, allocationCallback{allocationCallback}
|
|
|
|
, allocationCallbackContext{allocationCallbackContext}
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(blockSize > kMaxReservedDataSize);
|
|
|
|
CODEGEN_ASSERT(maxTotalSize >= blockSize);
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeAllocator::~CodeAllocator()
|
|
|
|
{
|
|
|
|
if (destroyBlockUnwindInfo)
|
|
|
|
{
|
|
|
|
for (void* unwindInfo : unwindInfos)
|
|
|
|
destroyBlockUnwindInfo(context, unwindInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t* block : blocks)
|
|
|
|
freePages(block, blockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeAllocator::allocate(
|
2024-08-02 15:30:04 +01:00
|
|
|
const uint8_t* data,
|
|
|
|
size_t dataSize,
|
|
|
|
const uint8_t* code,
|
|
|
|
size_t codeSize,
|
|
|
|
uint8_t*& result,
|
|
|
|
size_t& resultSize,
|
|
|
|
uint8_t*& resultCodeStart
|
|
|
|
)
|
2022-09-08 23:14:25 +01:00
|
|
|
{
|
2022-10-21 18:54:01 +01:00
|
|
|
// 'Round up' to preserve code alignment
|
|
|
|
size_t alignedDataSize = (dataSize + (kCodeAlignment - 1)) & ~(kCodeAlignment - 1);
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
size_t totalSize = alignedDataSize + codeSize;
|
|
|
|
|
|
|
|
// Function has to fit into a single block with unwinding information
|
2022-10-07 01:23:29 +01:00
|
|
|
if (totalSize > blockSize - kMaxReservedDataSize)
|
2022-09-08 23:14:25 +01:00
|
|
|
return false;
|
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
size_t startOffset = 0;
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
// We might need a new block
|
|
|
|
if (totalSize > size_t(blockEnd - blockPos))
|
|
|
|
{
|
2022-10-07 01:23:29 +01:00
|
|
|
if (!allocateNewBlock(startOffset))
|
2022-09-08 23:14:25 +01:00
|
|
|
return false;
|
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(totalSize <= size_t(blockEnd - blockPos));
|
2022-09-08 23:14:25 +01:00
|
|
|
}
|
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT((uintptr_t(blockPos) & (kPageSize - 1)) == 0); // Allocation starts on page boundary
|
2022-09-08 23:14:25 +01:00
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
size_t dataOffset = startOffset + alignedDataSize - dataSize;
|
|
|
|
size_t codeOffset = startOffset + alignedDataSize;
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
if (dataSize)
|
|
|
|
memcpy(blockPos + dataOffset, data, dataSize);
|
|
|
|
if (codeSize)
|
|
|
|
memcpy(blockPos + codeOffset, code, codeSize);
|
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
size_t pageAlignedSize = alignToPageSize(startOffset + totalSize);
|
2022-09-08 23:14:25 +01:00
|
|
|
|
2022-09-15 23:38:17 +01:00
|
|
|
makePagesExecutable(blockPos, pageAlignedSize);
|
2022-09-08 23:14:25 +01:00
|
|
|
flushInstructionCache(blockPos + codeOffset, codeSize);
|
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
result = blockPos + startOffset;
|
2022-09-08 23:14:25 +01:00
|
|
|
resultSize = totalSize;
|
|
|
|
resultCodeStart = blockPos + codeOffset;
|
|
|
|
|
2022-09-15 23:38:17 +01:00
|
|
|
// Ensure that future allocations from the block start from a page boundary.
|
|
|
|
// This is important since we use W^X, and writing to the previous page would require briefly removing
|
|
|
|
// executable bit from it, which may result in access violations if that code is being executed concurrently.
|
|
|
|
if (pageAlignedSize <= size_t(blockEnd - blockPos))
|
|
|
|
{
|
|
|
|
blockPos += pageAlignedSize;
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT((uintptr_t(blockPos) & (kPageSize - 1)) == 0);
|
|
|
|
CODEGEN_ASSERT(blockPos <= blockEnd);
|
2022-09-15 23:38:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Future allocations will need to allocate fresh blocks
|
|
|
|
blockPos = blockEnd;
|
|
|
|
}
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodeAllocator::allocateNewBlock(size_t& unwindInfoSize)
|
|
|
|
{
|
|
|
|
// Stop allocating once we reach a global limit
|
|
|
|
if ((blocks.size() + 1) * blockSize > maxTotalSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint8_t* block = allocatePages(blockSize);
|
|
|
|
|
|
|
|
if (!block)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
blockPos = block;
|
|
|
|
blockEnd = block + blockSize;
|
|
|
|
|
|
|
|
blocks.push_back(block);
|
|
|
|
|
|
|
|
if (createBlockUnwindInfo)
|
|
|
|
{
|
|
|
|
void* unwindInfo = createBlockUnwindInfo(context, block, blockSize, unwindInfoSize);
|
|
|
|
|
2022-10-21 18:54:01 +01:00
|
|
|
// 'Round up' to preserve alignment of the following data and code
|
|
|
|
unwindInfoSize = (unwindInfoSize + (kCodeAlignment - 1)) & ~(kCodeAlignment - 1);
|
2022-09-08 23:14:25 +01:00
|
|
|
|
2024-02-16 02:04:39 +00:00
|
|
|
CODEGEN_ASSERT(unwindInfoSize <= kMaxReservedDataSize);
|
2022-09-08 23:14:25 +01:00
|
|
|
|
|
|
|
if (!unwindInfo)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unwindInfos.push_back(unwindInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:42:37 +01:00
|
|
|
uint8_t* CodeAllocator::allocatePages(size_t size) const
|
|
|
|
{
|
|
|
|
const size_t pageAlignedSize = alignToPageSize(size);
|
|
|
|
|
|
|
|
uint8_t* const mem = allocatePagesImpl(pageAlignedSize);
|
|
|
|
if (mem == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (allocationCallback)
|
|
|
|
allocationCallback(allocationCallbackContext, nullptr, 0, mem, pageAlignedSize);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeAllocator::freePages(uint8_t* mem, size_t size) const
|
|
|
|
{
|
|
|
|
const size_t pageAlignedSize = alignToPageSize(size);
|
|
|
|
|
|
|
|
if (allocationCallback)
|
|
|
|
allocationCallback(allocationCallbackContext, mem, pageAlignedSize, nullptr, 0);
|
|
|
|
|
|
|
|
freePagesImpl(mem, pageAlignedSize);
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:14:25 +01:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|