[vm] Fix unwinding records on Windows

This change fixes the following 2 bugs related to unwinding records
on Windows:

1) When cross-compiling from another OS to Windows, unwinding records
   were not added to the end of the code section. Later, when loading
   AOT snapshot, arbitrary bytes at the end of the code section were
   used as the unwinding data, which could result in the errors
   returned from Windows API calls.

2) When code section is mapped, its size was rounded up to the page
   size; when looking for unwinding record, size of the unwinding
   record was subtracted from the rounded size. This is not correct
   as unwinding record is placed right at the end of code section,
   so code section size should be used before rounding.

Also, magic value is added to the unwinding record in order to
verify that it is preserved and correctly found.

TEST=Manually tested repro from b/320642692
TEST=ffi/ffi_induce_a_crash_test

Fixes b/320642692
Fixes https://github.com/dart-lang/sdk/issues/54206

Change-Id: Id0c6413cd1b759da9e9f25f7617eef55f33b04a2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346940
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2024-01-18 01:09:49 +00:00 committed by Commit Queue
parent 4a7dd72bcf
commit 5230995e3a
7 changed files with 79 additions and 31 deletions

View file

@ -471,8 +471,8 @@ bool LoadedElf::LoadSegments() {
// present on the page.
if (map_type == File::kReadExecute) {
void* ptable = nullptr;
UnwindingRecordsPlatform::RegisterExecutableMemory(
memory->address(), memory->size(), &ptable);
UnwindingRecordsPlatform::RegisterExecutableMemory(memory->address(),
length, &ptable);
dynamic_runtime_function_tables_.Add(ptable);
}
#endif

View file

@ -5,10 +5,19 @@
#include "platform/unwinding_records.h"
#include "platform/globals.h"
#if !defined(DART_HOST_OS_WINDOWS) || \
namespace dart {
#if !defined(DART_TARGET_OS_WINDOWS) || \
(!defined(TARGET_ARCH_X64) && !defined(TARGET_ARCH_ARM64))
namespace dart {
intptr_t UnwindingRecordsPlatform::SizeInBytes() {
return 0;
}
#endif // !defined(DART_TARGET_OS_WINDOWS) ...
#if !defined(DART_HOST_OS_WINDOWS) || \
(!defined(TARGET_ARCH_X64) && !defined(TARGET_ARCH_ARM64))
void UnwindingRecordsPlatform::Init() {}
void UnwindingRecordsPlatform::Cleanup() {}
@ -17,10 +26,7 @@ void UnwindingRecordsPlatform::RegisterExecutableMemory(
intptr_t size,
void** pp_dynamic_table) {}
void UnwindingRecordsPlatform::UnregisterDynamicTable(void* p_dynamic_table) {}
intptr_t UnwindingRecordsPlatform::SizeInBytes() {
return 0;
}
#endif // !defined(DART_HOST_OS_WINDOWS) ...
} // namespace dart
#endif // !defined(DART_HOST_OS_WINDOWS) || !defined(TARGET_ARCH_X64)

View file

@ -26,13 +26,14 @@ class UnwindingRecordsPlatform : public AllStatic {
static void* GetDeleteGrowableFunctionTableFunc();
};
#if defined(DART_HOST_OS_WINDOWS) && defined(TARGET_ARCH_X64)
#if defined(DART_TARGET_OS_WINDOWS) && defined(TARGET_ARCH_X64)
#pragma pack(push, 1)
//
// Refer to https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64
//
typedef unsigned char UBYTE;
typedef uint16_t USHORT;
typedef union _UNWIND_CODE {
struct {
UBYTE CodeOffset;
@ -52,12 +53,25 @@ typedef struct _UNWIND_INFO {
UNWIND_CODE UnwindCode[2];
} UNWIND_INFO, *PUNWIND_INFO;
#if !defined(DART_HOST_OS_WINDOWS)
typedef uint32_t ULONG;
typedef struct _RUNTIME_FUNCTION {
ULONG BeginAddress;
ULONG EndAddress;
ULONG UnwindData;
} RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;
#endif
static constexpr int kPushRbpInstructionLength = 1;
static const int kMovRbpRspInstructionLength = 3;
static constexpr int kRbpPrefixLength =
kPushRbpInstructionLength + kMovRbpRspInstructionLength;
static constexpr int kRBP = 5;
#ifndef UNW_FLAG_NHANDLER
#define UNW_FLAG_NHANDLER 0
#endif
struct GeneratedCodeUnwindInfo {
UNWIND_INFO unwind_info;
@ -77,8 +91,11 @@ struct GeneratedCodeUnwindInfo {
}
};
static constexpr uint32_t kUnwindingRecordMagic = 0xAABBCCDD;
struct CodeRangeUnwindingRecord {
void* dynamic_table;
uint32_t magic;
uint32_t runtime_function_count;
GeneratedCodeUnwindInfo unwind_info;
intptr_t exception_handler;
@ -87,7 +104,7 @@ struct CodeRangeUnwindingRecord {
#pragma pack(pop)
#elif defined(DART_HOST_OS_WINDOWS) && defined(TARGET_ARCH_ARM64)
#elif defined(DART_TARGET_OS_WINDOWS) && defined(TARGET_ARCH_ARM64)
#pragma pack(push, 1)
@ -187,9 +204,11 @@ struct UnwindData {
};
static const uint32_t kDefaultRuntimeFunctionCount = 1;
static constexpr uint32_t kUnwindingRecordMagic = 0xAABBCCEE;
struct CodeRangeUnwindingRecord {
void* dynamic_table;
uint32_t magic;
uint32_t runtime_function_count;
UnwindData<> unwind_info;
uint32_t exception_handler;
@ -208,7 +227,7 @@ struct CodeRangeUnwindingRecord {
#pragma pack(pop)
#endif // defined(DART_HOST_OS_WINDOWS) && defined(TARGET_ARCH_X64)
#endif // defined(DART_TARGET_OS_WINDOWS) && defined(TARGET_ARCH_X64)
} // namespace dart

View file

@ -7,16 +7,10 @@
#include "platform/assert.h"
#include "platform/globals.h"
#if defined(DART_HOST_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
namespace dart {
static HMODULE ntdll_module;
static decltype(&::RtlAddGrowableFunctionTable)
add_growable_function_table_func_ = nullptr;
static decltype(&::RtlDeleteGrowableFunctionTable)
delete_growable_function_table_func_ = nullptr;
#if defined(DART_TARGET_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
#if defined(TARGET_ARCH_X64)
const intptr_t kReservedUnwindingRecordsSizeBytes = 64;
@ -28,6 +22,17 @@ intptr_t UnwindingRecordsPlatform::SizeInBytes() {
return kReservedUnwindingRecordsSizeBytes;
}
#endif // defined(DART_TARGET_OS_WINDOWS)
#if defined(DART_HOST_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
static HMODULE ntdll_module;
static decltype(&::RtlAddGrowableFunctionTable)
add_growable_function_table_func_ = nullptr;
static decltype(&::RtlDeleteGrowableFunctionTable)
delete_growable_function_table_func_ = nullptr;
void* UnwindingRecordsPlatform::GetAddGrowableFunctionTableFunc() {
return add_growable_function_table_func_;
}
@ -68,6 +73,7 @@ void UnwindingRecordsPlatform::RegisterExecutableMemory(
uint8_t* record_ptr = static_cast<uint8_t*>(start) + unwinding_record_offset;
CodeRangeUnwindingRecord* record =
reinterpret_cast<CodeRangeUnwindingRecord*>(record_ptr);
RELEASE_ASSERT(record->magic == kUnwindingRecordMagic);
uword start_num = reinterpret_cast<intptr_t>(start);
uword end_num = start_num + size;
DWORD status = func(pp_dynamic_table,
@ -87,6 +93,6 @@ void UnwindingRecordsPlatform::UnregisterDynamicTable(void* p_dynamic_table) {
func(p_dynamic_table);
}
} // namespace dart
#endif // defined(DART_HOST_OS_WINDOWS)
} // namespace dart

View file

@ -5,18 +5,25 @@
#include "vm/unwinding_records.h"
#include "vm/globals.h"
#if !defined(DART_HOST_OS_WINDOWS) || \
(!defined(TARGET_ARCH_X64) && !defined(TARGET_ARCH_ARM64))
namespace dart {
#if !defined(DART_TARGET_OS_WINDOWS) || \
(!defined(TARGET_ARCH_X64) && !defined(TARGET_ARCH_ARM64))
const void* UnwindingRecords::GenerateRecordsInto(intptr_t offset,
uint8_t* target_buffer) {
return nullptr;
}
#endif
#if !defined(DART_HOST_OS_WINDOWS) || \
(!defined(TARGET_ARCH_X64) && !defined(TARGET_ARCH_ARM64))
void UnwindingRecords::RegisterExecutablePage(Page* page) {}
void UnwindingRecords::UnregisterExecutablePage(Page* page) {}
#endif
} // namespace dart
#endif // !defined(DART_HOST_OS_WINDOWS) || !defined(TARGET_ARCH_X64)

View file

@ -8,11 +8,11 @@
#include "platform/unwinding_records.h"
#if defined(DART_HOST_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
namespace dart {
#if defined(DART_TARGET_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
static void InitUnwindingRecord(intptr_t offset,
CodeRangeUnwindingRecord* record,
size_t code_size_in_bytes) {
@ -90,6 +90,7 @@ static void InitUnwindingRecord(intptr_t offset,
#else
#error What architecture?
#endif
record->magic = kUnwindingRecordMagic;
}
const void* UnwindingRecords::GenerateRecordsInto(intptr_t offset,
@ -100,6 +101,11 @@ const void* UnwindingRecords::GenerateRecordsInto(intptr_t offset,
return target_buffer;
}
#endif // defined(DART_TARGET_OS_WINDOWS)
#if defined(DART_HOST_OS_WINDOWS) && \
(defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64))
// Special exception-unwinding records are put at the end of executable
// page on Windows for 64-bit applications.
void UnwindingRecords::RegisterExecutablePage(Page* page) {
@ -118,6 +124,7 @@ void UnwindingRecords::RegisterExecutablePage(Page* page) {
new (reinterpret_cast<uint8_t*>(page->memory_->start()) +
unwinding_record_offset) CodeRangeUnwindingRecord();
InitUnwindingRecord(unwinding_record_offset, record, page->memory_->size());
RELEASE_ASSERT(record->magic == kUnwindingRecordMagic);
DWORD status = function(
/*DynamicTable=*/&record->dynamic_table,
/*FunctionTable=*/record->runtime_function,
@ -141,9 +148,10 @@ void UnwindingRecords::UnregisterExecutablePage(Page* page) {
reinterpret_cast<CodeRangeUnwindingRecord*>(
reinterpret_cast<uint8_t*>(page->memory_->start()) +
unwinding_record_offset);
RELEASE_ASSERT(record->magic == kUnwindingRecordMagic);
function(record->dynamic_table);
}
} // namespace dart
#endif // defined(DART_HOST_OS_WINDOWS)
} // namespace dart

View file

@ -5,6 +5,8 @@
// Test for unhandled exception treatment on Windows.
//
// SharedObjects=ffi_test_functions
// VMOptions=
// VMOptions=--force_load_elf_from_memory
import 'dart:ffi';
import 'dart:io';