Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 1 | //===-- clear_cache_test.c - Test clear_cache -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 9ad441f | 2010-11-16 22:13:33 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <stdint.h> |
Anton Korobeynikov | 7e8904f | 2012-01-12 21:13:48 +0000 | [diff] [blame] | 14 | #if defined(_WIN32) |
| 15 | #include <windows.h> |
| 16 | void __clear_cache(void* start, void* end) |
| 17 | { |
| 18 | if (!FlushInstructionCache(GetCurrentProcess(), start, end-start)) |
| 19 | exit(1); |
| 20 | } |
| 21 | #else |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 22 | #include <sys/mman.h> |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 23 | extern void __clear_cache(void* start, void* end); |
Anton Korobeynikov | 7e8904f | 2012-01-12 21:13:48 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | |
| 27 | |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 28 | |
| 29 | typedef int (*pfunc)(void); |
| 30 | |
| 31 | int func1() |
| 32 | { |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | int func2() |
| 37 | { |
| 38 | return 2; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | unsigned char execution_buffer[128]; |
| 44 | |
| 45 | int main() |
| 46 | { |
| 47 | // make executable the page containing execution_buffer |
| 48 | char* start = (char*)((uintptr_t)execution_buffer & (-4095)); |
| 49 | char* end = (char*)((uintptr_t)(&execution_buffer[128+4096]) & (-4095)); |
Anton Korobeynikov | 7e8904f | 2012-01-12 21:13:48 +0000 | [diff] [blame] | 50 | #if defined(_WIN32) |
| 51 | DWORD dummy_oldProt; |
| 52 | MEMORY_BASIC_INFORMATION b; |
| 53 | if (!VirtualQuery(start, &b, sizeof(b))) |
| 54 | return 1; |
| 55 | if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect)) |
| 56 | #else |
| 57 | if (mprotect(start, end-start, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) |
| 58 | #endif |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 59 | return 1; |
| 60 | |
| 61 | // verify you can copy and execute a function |
Shantonu Sen | a07fa29 | 2009-10-28 15:54:04 +0000 | [diff] [blame] | 62 | memcpy(execution_buffer, (void *)(uintptr_t)&func1, 128); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 63 | __clear_cache(execution_buffer, &execution_buffer[128]); |
Shantonu Sen | a07fa29 | 2009-10-28 15:54:04 +0000 | [diff] [blame] | 64 | pfunc f1 = (pfunc)(uintptr_t)execution_buffer; |
Anton Korobeynikov | 7e8904f | 2012-01-12 21:13:48 +0000 | [diff] [blame] | 65 | if ((*f1)() != 1) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | |
| 68 | // verify you can overwrite a function with another |
Shantonu Sen | a07fa29 | 2009-10-28 15:54:04 +0000 | [diff] [blame] | 69 | memcpy(execution_buffer, (void *)(uintptr_t)&func2, 128); |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 70 | __clear_cache(execution_buffer, &execution_buffer[128]); |
Shantonu Sen | a07fa29 | 2009-10-28 15:54:04 +0000 | [diff] [blame] | 71 | pfunc f2 = (pfunc)(uintptr_t)execution_buffer; |
Anton Korobeynikov | 7e8904f | 2012-01-12 21:13:48 +0000 | [diff] [blame] | 72 | if ((*f2)() != 2) |
Daniel Dunbar | b3a6901 | 2009-06-26 16:47:03 +0000 | [diff] [blame] | 73 | return 1; |
| 74 | |
| 75 | return 0; |
| 76 | } |