Skip to content

Commit

Permalink
NativeAOT runtime PAL cleanup (#91831)
Browse files Browse the repository at this point in the history
- Enable printing of assert messages on Unix and delete NO_UI_ASSERT define
- Delete MessageBox-based assert dialog. We have dropped it on CoreCLR some time ago too
- Simplify implementation of PalVirtualAlloc to just what is needed
- Add missing implementation of PalVirtualFree
- Delete a few other unused methods
  • Loading branch information
jkotas authored Sep 11, 2023
1 parent c84ccfa commit e21a975
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 218 deletions.
1 change: 0 additions & 1 deletion src/coreclr/nativeaot/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ else()
if(NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS)
add_definitions(-DFEATURE_READONLY_GS_COOKIE)
endif()
add_definitions(-DNO_UI_ASSERT)
include(unix/configure.cmake)
include(${CLR_SRC_NATIVE_DIR}/minipal/configure.cmake)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/DebugHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct DotNetRuntimeDebugHeader DotNetRuntimeDebugHeader = {};
{ \
s_GlobalEntries[currentGlobalPos] = { #Name, Name }; \
++currentGlobalPos; \
ASSERT(currentGlobalPos <= GlobalEntriesArraySize) \
ASSERT(currentGlobalPos <= GlobalEntriesArraySize); \
} while(0) \

extern "C" void PopulateDebugHeaders()
Expand Down
22 changes: 2 additions & 20 deletions src/coreclr/nativeaot/Runtime/PalRedhawk.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,6 @@ typedef enum _EXCEPTION_DISPOSITION {

#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)

#define DLL_PROCESS_ATTACH 1
#define DLL_THREAD_ATTACH 2
#define DLL_THREAD_DETACH 3
#define DLL_PROCESS_DETACH 0

#define INFINITE 0xFFFFFFFF

#define DUPLICATE_CLOSE_SOURCE 0x00000001
Expand All @@ -533,19 +528,6 @@ typedef enum _EXCEPTION_DISPOSITION {
#define PAGE_GUARD 0x100
#define PAGE_NOCACHE 0x200
#define PAGE_WRITECOMBINE 0x400
#define MEM_COMMIT 0x1000
#define MEM_RESERVE 0x2000
#define MEM_DECOMMIT 0x4000
#define MEM_RELEASE 0x8000
#define MEM_FREE 0x10000
#define MEM_PRIVATE 0x20000
#define MEM_MAPPED 0x40000
#define MEM_RESET 0x80000
#define MEM_TOP_DOWN 0x100000
#define MEM_WRITE_WATCH 0x200000
#define MEM_PHYSICAL 0x400000
#define MEM_LARGE_PAGES 0x20000000
#define MEM_4MB_PAGES 0x80000000

#define WAIT_OBJECT_0 0
#define WAIT_TIMEOUT 258
Expand Down Expand Up @@ -710,8 +692,8 @@ inline uint8_t * PalNtCurrentTeb()
EXTERN_C void * __cdecl _alloca(size_t);
#pragma intrinsic(_alloca)

REDHAWK_PALIMPORT _Ret_maybenull_ _Post_writable_byte_size_(size) void* REDHAWK_PALAPI PalVirtualAlloc(_In_opt_ void* pAddress, uintptr_t size, uint32_t allocationType, uint32_t protect);
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalVirtualFree(_In_ void* pAddress, uintptr_t size, uint32_t freeType);
REDHAWK_PALIMPORT _Ret_maybenull_ _Post_writable_byte_size_(size) void* REDHAWK_PALAPI PalVirtualAlloc(uintptr_t size, uint32_t protect);
REDHAWK_PALIMPORT void REDHAWK_PALAPI PalVirtualFree(_In_ void* pAddress, uintptr_t size);
REDHAWK_PALIMPORT UInt32_BOOL REDHAWK_PALAPI PalVirtualProtect(_In_ void* pAddress, uintptr_t size, uint32_t protect);
REDHAWK_PALIMPORT void PalFlushInstructionCache(_In_ void* pAddress, size_t size);
REDHAWK_PALIMPORT void REDHAWK_PALAPI PalSleep(uint32_t milliseconds);
Expand Down
24 changes: 0 additions & 24 deletions src/coreclr/nativeaot/Runtime/PalRedhawkFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ inline UInt32_BOOL PalCloseHandle(HANDLE arg1)
return CloseHandle(arg1);
}

extern "C" UInt32_BOOL __stdcall CreateDirectoryW(LPCWSTR, LPSECURITY_ATTRIBUTES);
inline UInt32_BOOL PalCreateDirectoryW(LPCWSTR arg1, LPSECURITY_ATTRIBUTES arg2)
{
return CreateDirectoryW(arg1, arg2);
}

extern "C" void __stdcall DeleteCriticalSection(CRITICAL_SECTION *);
inline void PalDeleteCriticalSection(CRITICAL_SECTION * arg1)
{
Expand Down Expand Up @@ -77,12 +71,6 @@ inline uint32_t PalGetEnvironmentVariable(_In_opt_ LPCSTR lpName, _Out_writes_to
}
#endif

extern "C" void * __stdcall GetProcAddress(HANDLE, const char *);
inline void * PalGetProcAddress(HANDLE arg1, const char * arg2)
{
return GetProcAddress(arg1, arg2);
}

extern "C" UInt32_BOOL __stdcall InitializeCriticalSectionEx(CRITICAL_SECTION *, uint32_t, uint32_t);
inline UInt32_BOOL PalInitializeCriticalSectionEx(CRITICAL_SECTION * arg1, uint32_t arg2, uint32_t arg3)
{
Expand All @@ -101,18 +89,6 @@ inline void PalLeaveCriticalSection(CRITICAL_SECTION * arg1)
LeaveCriticalSection(arg1);
}

extern "C" HANDLE __stdcall LoadLibraryExW(const WCHAR *, HANDLE, uint32_t);
inline HANDLE PalLoadLibraryExW(const WCHAR * arg1, HANDLE arg2, uint32_t arg3)
{
return LoadLibraryExW(arg1, arg2, arg3);
}

extern "C" void __stdcall RaiseException(uint32_t, uint32_t, uint32_t, const uint32_t *);
inline void PalRaiseException(uint32_t arg1, uint32_t arg2, uint32_t arg3, const uint32_t * arg4)
{
RaiseException(arg1, arg2, arg3, arg4);
}

extern "C" UInt32_BOOL __stdcall ResetEvent(HANDLE);
inline UInt32_BOOL PalResetEvent(HANDLE arg1)
{
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/nativeaot/Runtime/RhConfigValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
// very few configuration values are exposed in this manner.
//

// By default, print assert to console and break in the debugger, if attached. Set to 0 for a pop-up dialog on assert.
DEBUG_CONFIG_VALUE_WITH_DEFAULT(BreakOnAssert, 1)

RETAIL_CONFIG_VALUE(StressLogLevel)
RETAIL_CONFIG_VALUE(TotalStressLogSize)
RETAIL_CONFIG_VALUE(gcServer)
Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/nativeaot/Runtime/ThunksMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
{
#ifdef WIN32

void * pNewMapping = PalVirtualAlloc(NULL, THUNKS_MAP_SIZE * 2, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
void * pNewMapping = PalVirtualAlloc(THUNKS_MAP_SIZE * 2, PAGE_READWRITE);
if (pNewMapping == NULL)
return NULL;

Expand All @@ -108,7 +108,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
// reduce it to RW for the data section. For the stubs section we need to increase to RWX to generate the stubs
// instructions. After this we go back to RX for the stubs section before the stubs are used and should not be
// changed anymore.
void * pNewMapping = PalVirtualAlloc(NULL, THUNKS_MAP_SIZE * 2, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READ);
void * pNewMapping = PalVirtualAlloc(THUNKS_MAP_SIZE * 2, PAGE_EXECUTE_READ);
if (pNewMapping == NULL)
return NULL;

Expand All @@ -118,7 +118,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
if (!PalVirtualProtect(pDataSection, THUNKS_MAP_SIZE, PAGE_READWRITE) ||
!PalVirtualProtect(pThunksSection, THUNKS_MAP_SIZE, PAGE_EXECUTE_READWRITE))
{
PalVirtualFree(pNewMapping, 0, MEM_RELEASE);
PalVirtualFree(pNewMapping, THUNKS_MAP_SIZE * 2);
return NULL;
}

Expand Down Expand Up @@ -244,7 +244,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
#else
if (!PalVirtualProtect(pThunksSection, THUNKS_MAP_SIZE, PAGE_EXECUTE_READ))
{
PalVirtualFree(pNewMapping, 0, MEM_RELEASE);
PalVirtualFree(pNewMapping, THUNKS_MAP_SIZE * 2);
return NULL;
}
#endif
Expand Down Expand Up @@ -288,7 +288,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()
{
int thunkDataSize = thunkDataMappingSize * thunkDataMappingCount;

g_pThunkStubData = (uintptr_t)PalVirtualAlloc(NULL, thunkDataSize, MEM_RESERVE, PAGE_READWRITE);
g_pThunkStubData = (uintptr_t)VirtualAlloc(NULL, thunkDataSize, MEM_RESERVE, PAGE_READWRITE);

if (g_pThunkStubData == NULL)
{
Expand All @@ -298,7 +298,7 @@ EXTERN_C NATIVEAOT_API void* __cdecl RhAllocateThunksMapping()

void* pThunkDataBlock = (int8_t*)g_pThunkStubData + nextThunkDataMapping * thunkDataMappingSize;

if (PalVirtualAlloc(pThunkDataBlock, thunkDataMappingSize, MEM_COMMIT, PAGE_READWRITE) == NULL)
if (VirtualAlloc(pThunkDataBlock, thunkDataMappingSize, MEM_COMMIT, PAGE_READWRITE) == NULL)
{
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/nativeaot/Runtime/allocheap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ AllocHeap::~AllocHeap()
{
BlockListElem *pCur = m_blockList.PopHead();
if (pCur->GetStart() != m_pbInitialMem || m_fShouldFreeInitialMem)
PalVirtualFree(pCur->GetStart(), pCur->GetLength(), MEM_RELEASE);
PalVirtualFree(pCur->GetStart(), pCur->GetLength());
delete pCur;
}
}
Expand Down Expand Up @@ -279,15 +279,15 @@ bool AllocHeap::_AllocNewBlock(uintptr_t cbMem)
cbMem = ALIGN_UP(cbMem, OS_PAGE_SIZE);

uint8_t * pbMem = reinterpret_cast<uint8_t*>
(PalVirtualAlloc(NULL, cbMem, MEM_COMMIT, m_roProtectType));
(PalVirtualAlloc(cbMem, m_roProtectType));

if (pbMem == NULL)
return false;

BlockListElem *pBlockListElem = new (nothrow) BlockListElem(pbMem, cbMem);
if (pBlockListElem == NULL)
{
PalVirtualFree(pbMem, 0, MEM_RELEASE);
PalVirtualFree(pbMem, cbMem);
return false;
}

Expand Down
11 changes: 9 additions & 2 deletions src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ ep_rt_aot_system_timestamp_get (void)
return static_cast<int64_t>(((static_cast<uint64_t>(value.dwHighDateTime)) << 32) | static_cast<uint64_t>(value.dwLowDateTime));
}

int32_t
ep_rt_aot_get_os_page_size (void)
{
STATIC_CONTRACT_NOTHROW;
return (int32_t)OS_PAGE_SIZE;
}

ep_rt_file_handle_t
ep_rt_aot_file_open_write (const ep_char8_t *path)
{
Expand Down Expand Up @@ -502,7 +509,7 @@ uint8_t *
ep_rt_aot_valloc0 (size_t buffer_size)
{
STATIC_CONTRACT_NOTHROW;
return reinterpret_cast<uint8_t *>(PalVirtualAlloc (NULL, buffer_size, MEM_COMMIT, PAGE_READWRITE));
return reinterpret_cast<uint8_t *>(PalVirtualAlloc (buffer_size, PAGE_READWRITE));
}

void
Expand All @@ -513,7 +520,7 @@ ep_rt_aot_vfree (
STATIC_CONTRACT_NOTHROW;

if (buffer)
PalVirtualFree (buffer, 0, MEM_RELEASE);
PalVirtualFree (buffer, buffer_size);
}

void
Expand Down
6 changes: 5 additions & 1 deletion src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,8 +929,12 @@ int32_t
ep_rt_system_get_alloc_granularity (void)
{
STATIC_CONTRACT_NOTHROW;
// return static_cast<int32_t>(g_SystemInfo.dwAllocationGranularity);
#ifdef TARGET_WINDOWS
return 0x10000;
#else
extern int32_t ep_rt_aot_get_os_page_size (void);
return ep_rt_aot_get_os_page_size();
#endif
}

static
Expand Down
101 changes: 21 additions & 80 deletions src/coreclr/nativeaot/Runtime/rhassert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,32 @@
#include "PalRedhawk.h"
#include "rhassert.h"


#include "RhConfig.h"

#ifdef _DEBUG

#define MB_ABORTRETRYIGNORE 0x00000002L
#define IDABORT 3
#define IDRETRY 4
#define IDIGNORE 5

void Assert(const char * expr, const char * file, uint32_t line_num, const char * message)
{
#ifndef DACCESS_COMPILE
#ifdef NO_UI_ASSERT
printf(
"--------------------------------------------------\n"
"Debug Assertion Violation\n\n"
"%s%s%s"
"Expression: '%s'\n\n"
"File: %s, Line: %u\n"
"--------------------------------------------------\n",
message ? ("Message: ") : (""),
message ? (message) : (""),
message ? ("\n\n") : (""),
expr, file, line_num);

// Flush standard output before failing fast to make sure the assertion failure message
// is retained when tests are being run with redirected stdout.
fflush(stdout);

// If there's no debugger attached, we just FailFast
if (!PalIsDebuggerPresent())
PalRaiseFailFastException(NULL, NULL, FAIL_FAST_GENERATE_EXCEPTION_ADDRESS);

// If there is a debugger attached, we break and then allow continuation.
PalDebugBreak();
#else
if (g_pRhConfig->GetBreakOnAssert())
{
printf(
"--------------------------------------------------\n"
"Debug Assertion Violation\n\n"
"%s%s%s"
"Expression: '%s'\n\n"
"File: %s, Line: %u\n"
"--------------------------------------------------\n",
message ? ("Message: ") : (""),
message ? (message) : (""),
message ? ("\n\n") : (""),
expr, file, line_num);

// Flush standard output before failing fast to make sure the assertion failure message
// is retained when tests are being run with redirected stdout.
fflush(stdout);

// If there's no debugger attached, we just FailFast
if (!PalIsDebuggerPresent())
PalRaiseFailFastException(NULL, NULL, FAIL_FAST_GENERATE_EXCEPTION_ADDRESS);

// If there is a debugger attached, we break and then allow continuation.
PalDebugBreak();
return;
}

char buffer[4096];

sprintf_s(buffer, ARRAY_SIZE(buffer),
"--------------------------------------------------\n"
"Debug Assertion Violation\n\n"
"%s%s%s"
"Expression: '%s'\n\n"
"File: %s, Line: %u\n"
"--------------------------------------------------\n"
"Abort: Exit Immediately\n"
"Retry: DebugBreak()\n"
"Ignore: Keep Going\n"
"--------------------------------------------------\n",
message ? ("Message: ") : (""),
message ? (message) : (""),
message ? ("\n\n") : (""),
expr, file, line_num);

HANDLE hMod = PalLoadLibraryExW(L"user32.dll", NULL, 0);
int32_t (* pfn)(HANDLE, char *, const char *, uint32_t) =
(int32_t (*)(HANDLE, char *, const char *, uint32_t))PalGetProcAddress(hMod, "MessageBoxA");

int32_t result = pfn(NULL, buffer, "Redhawk Assert", MB_ABORTRETRYIGNORE);

switch (result)
{
case IDABORT:
PalTerminateCurrentProcess(42);
break;
case IDRETRY:
PalDebugBreak();
break;
case IDIGNORE:
break;
}
#endif
#else
UNREFERENCED_PARAMETER(expr);
UNREFERENCED_PARAMETER(file);
UNREFERENCED_PARAMETER(line_num);
UNREFERENCED_PARAMETER(message);
#endif //!DACCESS_COMPILE
}

#endif // _DEBUG
Loading

0 comments on commit e21a975

Please sign in to comment.