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