[PATCH] D67929: builtins test: Move clear_cache_test.c from a mprotect()ed global to a mmap()ed variable

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 13:05:55 PDT 2019


thakis created this revision.
thakis added a reviewer: delcypher.
Herald added a project: LLVM.

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.


https://reviews.llvm.org/D67929

Files:
  compiler-rt/test/builtins/Unit/clear_cache_test.c


Index: compiler-rt/test/builtins/Unit/clear_cache_test.c
===================================================================
--- compiler-rt/test/builtins/Unit/clear_cache_test.c
+++ compiler-rt/test/builtins/Unit/clear_cache_test.c
@@ -51,24 +51,29 @@
 #endif
 }
 
-unsigned char execution_buffer[128] __attribute__((aligned(8)));
-
 int main()
 {
+#if !defined(_WIN32)
+    int *execution_buffer = mmap(0, 128, PROT_READ | PROT_WRITE | PROT_EXEC,
+                                 MAP_ANON | MAP_PRIVATE, 0, 0);
+    if (execution_buffer == MAP_FAILED)
+      return 1;
+#else
+    HANDLE mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
+                                       PAGE_EXECUTE_READWRITE, 0, 128, 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
+
     // 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))
-#else
-    if (mprotect(start, end-start, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
-#endif
-        return 1;
 
     // verify you can copy and execute a function
     pfunc f1 = (pfunc)memcpy_f(execution_buffer, func1, 128);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67929.221394.patch
Type: text/x-patch
Size: 1679 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190923/e5d993b2/attachment.bin>


More information about the llvm-commits mailing list