luau/CodeGen/src/CodeBlockUnwind.cpp

121 lines
3.7 KiB
C++
Raw Normal View History

2022-09-15 23:13:58 +01:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/CodeBlockUnwind.h"
2022-10-21 18:33:43 +01:00
#include "Luau/CodeAllocator.h"
2022-09-15 23:13:58 +01:00
#include "Luau/UnwindBuilder.h"
#include <string.h>
#if defined(_WIN32) && defined(_M_X64)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
2023-05-12 13:15:01 +01:00
#elif defined(__linux__) || defined(__APPLE__)
2022-09-15 23:13:58 +01:00
// Defined in unwind.h which may not be easily discoverable on various platforms
extern "C" void __register_frame(const void*);
extern "C" void __deregister_frame(const void*);
2023-05-05 20:57:12 +01:00
extern "C" void __unw_add_dynamic_fde() __attribute__((weak));
2022-09-15 23:13:58 +01:00
#endif
2023-05-12 13:15:01 +01:00
#if defined(__APPLE__) && defined(__aarch64__)
#include <sys/sysctl.h>
#endif
2023-05-05 20:57:12 +01:00
namespace Luau
{
namespace CodeGen
{
2023-05-12 13:15:01 +01:00
#if defined(__linux__) || defined(__APPLE__)
2022-09-15 23:13:58 +01:00
static void visitFdeEntries(char* pos, void (*cb)(const void*))
{
2023-05-05 20:57:12 +01:00
// When using glibc++ unwinder, we need to call __register_frame/__deregister_frame on the entire .eh_frame data
// When using libc++ unwinder (libunwind), each FDE has to be handled separately
// libc++ unwinder is the macOS unwinder, but on Linux the unwinder depends on the library the executable is linked with
// __unw_add_dynamic_fde is specific to libc++ unwinder, as such we determine the library based on its existence
if (__unw_add_dynamic_fde == nullptr)
return cb(pos);
2022-09-15 23:13:58 +01:00
for (;;)
{
unsigned partLength;
memcpy(&partLength, pos, sizeof(partLength));
if (partLength == 0) // Zero-length section signals completion
break;
unsigned partId;
memcpy(&partId, pos + 4, sizeof(partId));
if (partId != 0) // Skip CIE part
cb(pos); // CIE is found using an offset in FDE
pos += partLength + 4;
}
}
#endif
2022-10-07 00:55:58 +01:00
void* createBlockUnwindInfo(void* context, uint8_t* block, size_t blockSize, size_t& beginOffset)
2022-09-15 23:13:58 +01:00
{
UnwindBuilder* unwind = (UnwindBuilder*)context;
// All unwinding related data is placed together at the start of the block
2023-04-14 13:05:27 +01:00
size_t unwindSize = unwind->getSize();
2022-10-21 18:33:43 +01:00
unwindSize = (unwindSize + (kCodeAlignment - 1)) & ~(kCodeAlignment - 1); // Match code allocator alignment
2022-09-15 23:13:58 +01:00
LUAU_ASSERT(blockSize >= unwindSize);
2023-04-14 13:05:27 +01:00
char* unwindData = (char*)block;
unwind->finalize(unwindData, unwindSize, block, blockSize);
2022-09-15 23:13:58 +01:00
2023-04-14 13:05:27 +01:00
#if defined(_WIN32) && defined(_M_X64)
if (!RtlAddFunctionTable((RUNTIME_FUNCTION*)block, uint32_t(unwind->getFunctionCount()), uintptr_t(block)))
2022-09-15 23:13:58 +01:00
{
2023-05-25 21:46:51 +01:00
LUAU_ASSERT(!"Failed to allocate function table");
2022-09-15 23:13:58 +01:00
return nullptr;
}
2023-05-12 13:15:01 +01:00
#elif defined(__linux__) || defined(__APPLE__)
2023-05-05 20:57:12 +01:00
visitFdeEntries(unwindData, __register_frame);
2022-09-15 23:13:58 +01:00
#endif
2022-10-07 00:55:58 +01:00
beginOffset = unwindSize + unwind->getBeginOffset();
2022-09-15 23:13:58 +01:00
return block;
}
void destroyBlockUnwindInfo(void* context, void* unwindData)
{
#if defined(_WIN32) && defined(_M_X64)
2023-04-14 13:05:27 +01:00
if (!RtlDeleteFunctionTable((RUNTIME_FUNCTION*)unwindData))
2023-05-25 21:46:51 +01:00
LUAU_ASSERT(!"Failed to deallocate function table");
2023-05-12 13:15:01 +01:00
#elif defined(__linux__) || defined(__APPLE__)
2023-05-05 20:57:12 +01:00
visitFdeEntries((char*)unwindData, __deregister_frame);
2022-09-15 23:13:58 +01:00
#endif
}
2023-05-12 13:15:01 +01:00
bool isUnwindSupported()
{
#if defined(_WIN32) && defined(_M_X64)
return true;
#elif defined(__APPLE__) && defined(__aarch64__)
char ver[256];
size_t verLength = sizeof(ver);
// libunwind on macOS 12 and earlier (which maps to osrelease 21) assumes JIT frames use pointer authentication without a way to override that
return sysctlbyname("kern.osrelease", ver, &verLength, NULL, 0) == 0 && atoi(ver) >= 22;
#elif defined(__linux__) || defined(__APPLE__)
return true;
#else
return false;
#endif
}
2022-09-15 23:13:58 +01:00
} // namespace CodeGen
} // namespace Luau