Ruben Van Boxem
2012-Jan-07 20:23 UTC
[LLVMdev] [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test
---------- Forwarded message ---------- From: Ruben Van Boxem <vanboxem.ruben at gmail.com> Date: 2012/1/7 Subject: Re: [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test To: Anton Korobeynikov <anton at korobeynikov.info> 2012/1/7 Anton Korobeynikov <anton at korobeynikov.info>> Hello Ruben > > > Please apply at your leisure! Thanks! > Will you please fix the code to use the same coding style as > everything else around? >I see I missed some curly braces. I also modified spacing a tiny bit. If you mean anything else, I cannot see what's out of style. Thanks for the prompt response, Ruben -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120107/be5d1002/attachment.html> -------------- next part -------------- Index: test/Unit/clear_cache_test.c ==================================================================--- test/Unit/clear_cache_test.c (revision 147731) +++ test/Unit/clear_cache_test.c (working copy) @@ -11,11 +11,20 @@ #include <stdio.h> #include <string.h> #include <stdint.h> +#if defined(_WIN32) +#include <windows.h> +void __clear_cache(void* start, void* end) +{ + if( !FlushInstructionCache(GetCurrentProcess(), start, end-start) ) + exit(1); +} +#else #include <sys/mman.h> +extern void __clear_cache(void* start, void* end); +#endif -extern void __clear_cache(void* start, void* end); typedef int (*pfunc)(void); @@ -38,7 +47,15 @@ // 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 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 Index: test/Unit/enable_execute_stack_test.c ==================================================================--- test/Unit/enable_execute_stack_test.c (revision 147731) +++ test/Unit/enable_execute_stack_test.c (working copy) @@ -11,12 +11,27 @@ #include <stdio.h> #include <string.h> #include <stdint.h> +#if defined(_WIN32) +#include <windows.h> +void __clear_cache(void* start, void* end) +{ + if( !FlushInstructionCache(GetCurrentProcess(), start, end-start) ) + exit(1); +} +void __enable_execute_stack(void *addr) +{ + MEMORY_BASIC_INFORMATION b; + + if ( !VirtualQuery(addr, &b, sizeof(b)) ) + exit(1); + if( !VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect) ) + exit(1); +} +#else #include <sys/mman.h> - - - extern void __clear_cache(void* start, void* end); extern void __enable_execute_stack(void* addr); +#endif typedef int (*pfunc)(void); Index: lib/int_endianness.h ==================================================================--- lib/int_endianness.h (revision 147731) +++ lib/int_endianness.h (working copy) @@ -80,6 +80,13 @@ #endif /* GNU/Linux */ +#if defined(_WIN32) + +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 + +#endif /* Windows */ + /* . */ #if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN)
Ruben Van Boxem
2012-Jan-07 22:24 UTC
[LLVMdev] [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test
2012/1/7 Anton Korobeynikov <anton at korobeynikov.info>> Hi Ruben, > > > I see I missed some curly braces. I also modified spacing a tiny bit. > Doesn't seem so. E.g. you have: > > + if ( !VirtualQuery(addr, &b, sizeof(b)) ) > + exit(1); > + if( !VirtualProtect(b.BaseAddress, b.RegionSize, > PAGE_EXECUTE_READWRITE, &b.Protect) ) > Add space after "if". Do not put spaces after "(" and before ")". Same > for other places in the file. >OK, I followed the rules you set forth here. But the files followed different ones, hence my confusion. I modified the rest of the file to follow these spacing guidelines (code which was wrongly formatted to begin with). Thanks for having patience :), Ruben> > -- > With best regards, Anton Korobeynikov > Faculty of Mathematics and Mechanics, Saint Petersburg State University >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120107/e7977fcc/attachment.html> -------------- next part -------------- Index: test/Unit/clear_cache_test.c ==================================================================--- test/Unit/clear_cache_test.c (revision 147738) +++ test/Unit/clear_cache_test.c (working copy) @@ -11,11 +11,20 @@ #include <stdio.h> #include <string.h> #include <stdint.h> +#if defined(_WIN32) +#include <windows.h> +void __clear_cache(void* start, void* end) +{ + if (!FlushInstructionCache(GetCurrentProcess(), start, end-start)) + exit(1); +} +#else #include <sys/mman.h> +extern void __clear_cache(void* start, void* end); +#endif -extern void __clear_cache(void* start, void* end); typedef int (*pfunc)(void); @@ -38,21 +47,29 @@ // 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 ) +#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 memcpy(execution_buffer, (void *)(uintptr_t)&func1, 128); __clear_cache(execution_buffer, &execution_buffer[128]); pfunc f1 = (pfunc)(uintptr_t)execution_buffer; - if ( (*f1)() != 1 ) + if ((*f1)() != 1) return 1; // verify you can overwrite a function with another memcpy(execution_buffer, (void *)(uintptr_t)&func2, 128); __clear_cache(execution_buffer, &execution_buffer[128]); pfunc f2 = (pfunc)(uintptr_t)execution_buffer; - if ( (*f2)() != 2 ) + if ((*f2)() != 2) return 1; return 0; Index: test/Unit/enable_execute_stack_test.c ==================================================================--- test/Unit/enable_execute_stack_test.c (revision 147738) +++ test/Unit/enable_execute_stack_test.c (working copy) @@ -11,12 +11,27 @@ #include <stdio.h> #include <string.h> #include <stdint.h> +#if defined(_WIN32) +#include <windows.h> +void __clear_cache(void* start, void* end) +{ + if (!FlushInstructionCache(GetCurrentProcess(), start, end-start)) + exit(1); +} +void __enable_execute_stack(void *addr) +{ + MEMORY_BASIC_INFORMATION b; + + if (!VirtualQuery(addr, &b, sizeof(b))) + exit(1); + if (!VirtualProtect(b.BaseAddress, b.RegionSize, PAGE_EXECUTE_READWRITE, &b.Protect)) + exit(1); +} +#else #include <sys/mman.h> - - - extern void __clear_cache(void* start, void* end); extern void __enable_execute_stack(void* addr); +#endif typedef int (*pfunc)(void); @@ -43,14 +58,14 @@ memcpy(execution_buffer, (void *)(uintptr_t)&func1, 128); __clear_cache(execution_buffer, &execution_buffer[128]); pfunc f1 = (pfunc)(uintptr_t)execution_buffer; - if ( (*f1)() != 1 ) + if ((*f1)() != 1) return 1; // verify you can overwrite a function with another memcpy(execution_buffer, (void *)(uintptr_t)&func2, 128); __clear_cache(execution_buffer, &execution_buffer[128]); pfunc f2 = (pfunc)(uintptr_t)execution_buffer; - if ( (*f2)() != 2 ) + if ((*f2)() != 2) return 1; return 0; Index: lib/int_endianness.h ==================================================================--- lib/int_endianness.h (revision 147738) +++ lib/int_endianness.h (working copy) @@ -80,6 +80,13 @@ #endif /* GNU/Linux */ +#if defined(_WIN32) + +#define _YUGA_LITTLE_ENDIAN 1 +#define _YUGA_BIG_ENDIAN 0 + +#endif /* Windows */ + /* . */ #if !defined(_YUGA_LITTLE_ENDIAN) || !defined(_YUGA_BIG_ENDIAN)
Seemingly Similar Threads
- [LLVMdev] [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test
- [LLVMdev] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test
- [LLVMdev] [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test
- [LLVMdev] compiler-rt on Windows
- [LLVMdev] [llvm-commits] [PATCH][Compiler-rt] Windows implementation of mmap functionality in clear_cache_test and enable_execute_stack_test