summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2019-09-25 11:57:51 +0000
committerNico Weber <nicolasweber@gmx.de>2019-09-25 11:57:51 +0000
commite01a28f8cd0672b26a2cf80ad7ebafe953063293 (patch)
tree26594a576804dc76493c662f06a492bb0e2044c1
parent1db063ce1e1c7e244f429fafc0c55df631bd12f5 (diff)
downloadcompiler-rt-e01a28f8cd0672b26a2cf80ad7ebafe953063293.tar.gz
builtins test: Move clear_cache_test.c from a mprotect()ed global to a mmap()ed variable
ld64 in the macOS 10.15 SDK gives __DATA a maxprot of 3, meaning it can't be made executable at runtime by default. Change clear_cache_test.c to use mmap()ed data that's mapped as writable and executable from the beginning, instead of trying to mprotect()ing a __DATA variable as executable. This fixes the test on macOS with the 10.15 SDK. PR43407. Differential Revision: https://reviews.llvm.org/D67929 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@372849 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--test/builtins/Unit/clear_cache_test.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/test/builtins/Unit/clear_cache_test.c b/test/builtins/Unit/clear_cache_test.c
index e85cc3027..4220ba651 100644
--- a/test/builtins/Unit/clear_cache_test.c
+++ b/test/builtins/Unit/clear_cache_test.c
@@ -13,25 +13,16 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
+
#if defined(_WIN32)
#include <windows.h>
-static uintptr_t get_page_size() {
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- return si.dwPageSize;
-}
#else
#include <unistd.h>
#include <sys/mman.h>
-
-static uintptr_t get_page_size() {
- return sysconf(_SC_PAGE_SIZE);
-}
#endif
extern void __clear_cache(void* start, void* end);
-
typedef int (*pfunc)(void);
// Make these static to avoid ILT jumps for incremental linking on Windows.
@@ -51,34 +42,36 @@ memcpy_f(void *dst, const void *src, size_t n) {
#endif
}
-unsigned char execution_buffer[128] __attribute__((aligned(8)));
-
int main()
{
- // make executable the page containing execution_buffer
- uintptr_t page_size = get_page_size();
- char* start = (char*)((uintptr_t)execution_buffer & (-page_size));
- char* end = (char*)((uintptr_t)(&execution_buffer[128+page_size]) & (-page_size));
-#if defined(_WIN32)
- DWORD dummy_oldProt;
- MEMORY_BASIC_INFORMATION b;
- if (!VirtualQuery(start, &b, sizeof(b)))
- return 1;
- if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect))
+ const int kSize = 128;
+#if !defined(_WIN32)
+ uint8_t *execution_buffer = mmap(0, kSize,
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_ANON | MAP_PRIVATE, 0, 0);
+ if (execution_buffer == MAP_FAILED)
+ return 1;
#else
- if (mprotect(start, end-start, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
-#endif
+ HANDLE mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
+ PAGE_EXECUTE_READWRITE, 0, kSize, NULL);
+ if (mapping == NULL)
return 1;
+ uint8_t* execution_buffer = MapViewOfFile(
+ mapping, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0);
+ if (execution_buffer == NULL)
+ return 1;
+#endif
+
// verify you can copy and execute a function
- pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);
- __clear_cache(execution_buffer, &execution_buffer[128]);
+ pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, kSize);
+ __clear_cache(execution_buffer, execution_buffer + kSize);
if ((*f1)() != 1)
return 1;
// verify you can overwrite a function with another
- pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, 128);
- __clear_cache(execution_buffer, &execution_buffer[128]);
+ pfunc f2 = (pfunc)memcpy_f(execution_buffer, func2, kSize);
+ __clear_cache(execution_buffer, execution_buffer + kSize);
if ((*f2)() != 2)
return 1;