Reactos
at master 61 lines 1.2 kB view raw
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <windows.h> 5 6#include <pseh/pseh.h> 7 8int test(int x) 9{ 10 return x+1; 11} 12 13void execute(char* message, int(*func)(int)) 14{ 15 ULONG status = 0; 16 ULONG result; 17 18 printf("%s ... ", message); 19 20 _SEH_TRY 21 { 22 result = func(1); 23 } 24 _SEH_HANDLE 25 { 26 status = _SEH_GetExceptionCode(); 27 } 28 _SEH_END; 29 if (status == 0) 30 { 31 printf("OK.\n"); 32 } 33 else 34 { 35 printf("Error, status=%lx.\n", status); 36 } 37} 38 39char data[100]; 40 41int main(void) 42{ 43 unsigned char stack[100]; 44 void* heap; 45 ULONG protection; 46 47 printf("NoExecute\n"); 48 49 execute("Executing within the code segment", test); 50 memcpy(data, test, 100); 51 execute("Executing within the data segment", (int(*)(int))data); 52 memcpy(stack, test, 100); 53 execute("Executing on stack segment", (int(*)(int))stack); 54 heap = VirtualAlloc(NULL, 100, MEM_COMMIT, PAGE_READWRITE); 55 memcpy(heap, test, 100); 56 execute("Executing on the heap with protection PAGE_READWRITE", (int(*)(int))heap); 57 VirtualProtect(heap, 100, PAGE_EXECUTE, &protection); 58 execute("Executing on the heap with protection PAGE_EXECUTE", (int(*)(int))heap); 59 60 return 0; 61}