summaryrefslogtreecommitdiff
path: root/test/Unit/clear_cache_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/Unit/clear_cache_test.c')
-rw-r--r--test/Unit/clear_cache_test.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/Unit/clear_cache_test.c b/test/Unit/clear_cache_test.c
new file mode 100644
index 00000000..b9b17351
--- /dev/null
+++ b/test/Unit/clear_cache_test.c
@@ -0,0 +1,59 @@
+//===-- clear_cache_test.c - Test clear_cache -----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <sys/mman.h>
+
+
+
+extern void __clear_cache(void* start, void* end);
+
+typedef int (*pfunc)(void);
+
+int func1()
+{
+ return 1;
+}
+
+int func2()
+{
+ return 2;
+}
+
+
+
+unsigned char execution_buffer[128];
+
+int main()
+{
+ // make executable the page containing execution_buffer
+ char* start = (char*)((uintptr_t)execution_buffer & (-4095));
+ char* end = (char*)((uintptr_t)(&execution_buffer[128+4096]) & (-4095));
+ if ( mprotect(start, end-start, PROT_READ|PROT_WRITE|PROT_EXEC) != 0 )
+ return 1;
+
+ // verify you can copy and execute a function
+ memcpy(execution_buffer, &func1, 128);
+ __clear_cache(execution_buffer, &execution_buffer[128]);
+ pfunc f1 = (pfunc)execution_buffer;
+ if ( (*f1)() != 1 )
+ return 1;
+
+ // verify you can overwrite a function with another
+ memcpy(execution_buffer, &func2, 128);
+ __clear_cache(execution_buffer, &execution_buffer[128]);
+ pfunc f2 = (pfunc)execution_buffer;
+ if ( (*f2)() != 2 )
+ return 1;
+
+ return 0;
+}