clear_cache_test.c revision 7e8904f4b2b6b084ea2b4203d62554b42c068296
1b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//===-- clear_cache_test.c - Test clear_cache -----------------------------===//
2b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
3b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//                     The LLVM Compiler Infrastructure
4b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
59ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
69ad441ffec97db647fee3725b3424284fb913e14Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//
8b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar//===----------------------------------------------------------------------===//
9b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
10b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
11b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include <stdio.h>
12b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include <string.h>
13b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include <stdint.h>
147e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#if defined(_WIN32)
157e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#include <windows.h>
167e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikovvoid __clear_cache(void* start, void* end)
177e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov{
187e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if (!FlushInstructionCache(GetCurrentProcess(), start, end-start))
197e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov        exit(1);
207e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov}
217e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#else
22b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar#include <sys/mman.h>
237e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikovextern void __clear_cache(void* start, void* end);
247e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#endif
25b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
26b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
27b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
28b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
29b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbartypedef int (*pfunc)(void);
30b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
31b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint func1()
32b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
33b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return 1;
34b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
35b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
36b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint func2()
37b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
38b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return 2;
39b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
40b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
41b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
42b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
43b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarunsigned char execution_buffer[128];
44b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
45b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbarint main()
46b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar{
47b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    // make executable the page containing execution_buffer
48b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    char* start = (char*)((uintptr_t)execution_buffer & (-4095));
49b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    char* end = (char*)((uintptr_t)(&execution_buffer[128+4096]) & (-4095));
507e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#if defined(_WIN32)
517e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    DWORD dummy_oldProt;
527e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    MEMORY_BASIC_INFORMATION b;
537e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if (!VirtualQuery(start, &b, sizeof(b)))
547e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov        return 1;
557e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect))
567e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#else
577e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if (mprotect(start, end-start, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
587e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov#endif
59b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
60b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
61b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    // verify you can copy and execute a function
62a07fa29cead05031869b7488f4e845584e57b628Shantonu Sen    memcpy(execution_buffer, (void *)(uintptr_t)&func1, 128);
63b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    __clear_cache(execution_buffer, &execution_buffer[128]);
64a07fa29cead05031869b7488f4e845584e57b628Shantonu Sen    pfunc f1 = (pfunc)(uintptr_t)execution_buffer;
657e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if ((*f1)() != 1)
66b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
67b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
68b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    // verify you can overwrite a function with another
69a07fa29cead05031869b7488f4e845584e57b628Shantonu Sen    memcpy(execution_buffer, (void *)(uintptr_t)&func2, 128);
70b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    __clear_cache(execution_buffer, &execution_buffer[128]);
71a07fa29cead05031869b7488f4e845584e57b628Shantonu Sen    pfunc f2 = (pfunc)(uintptr_t)execution_buffer;
727e8904f4b2b6b084ea2b4203d62554b42c068296Anton Korobeynikov    if ((*f2)() != 2)
73b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar        return 1;
74b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar
75b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar    return 0;
76b3a6901e66f55b35aa9e01bcb24134e6a65ea004Daniel Dunbar}
77