From 6375fca5beaa40b4ca9ba94eab6d387bdcca090d Mon Sep 17 00:00:00 2001 From: Hartmut Birr Date: Sun, 26 Sep 2004 17:00:15 +0000 Subject: [PATCH] - Added a little sample which tests the noexecute protection. svn path=/trunk/; revision=11077 --- reactos/apps/tests/noexecute/makefile | 32 ++++++++++++ reactos/apps/tests/noexecute/noexecute.c | 65 ++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 reactos/apps/tests/noexecute/makefile create mode 100644 reactos/apps/tests/noexecute/noexecute.c diff --git a/reactos/apps/tests/noexecute/makefile b/reactos/apps/tests/noexecute/makefile new file mode 100644 index 00000000000..fe2bacb3f63 --- /dev/null +++ b/reactos/apps/tests/noexecute/makefile @@ -0,0 +1,32 @@ +# +# $Id: makefile,v 1.1 2004/09/26 17:00:15 hbirr Exp $ +# + +PATH_TO_TOP = ../../.. + +TARGET_NORC = yes + +TARGET_TYPE = program + +TARGET_APPTYPE = console + +TARGET_NAME = noexecute + +TARGET_SDKLIBS = pseh.a + +TARGET_PCH = + +TARGET_OBJECTS = \ + noexecute.o + +TARGET_CFLAGS = -Wall -Werror -D__USE_W32API + +DEP_OBJECTS = $(TARGET_OBJECTS) + +include $(PATH_TO_TOP)/rules.mak + +include $(TOOLS_PATH)/helper.mk + +include $(TOOLS_PATH)/depend.mk + +# EOF diff --git a/reactos/apps/tests/noexecute/noexecute.c b/reactos/apps/tests/noexecute/noexecute.c new file mode 100644 index 00000000000..ed9b81b1ea6 --- /dev/null +++ b/reactos/apps/tests/noexecute/noexecute.c @@ -0,0 +1,65 @@ +/* + * $Id: noexecute.c,v 1.1 2004/09/26 17:00:15 hbirr Exp $ + */ + +#include +#include +#include +#include + +#include + +int test(int x) +{ + return x+1; +} + +void execute(char* message, int(*func)(int)) +{ + ULONG status = 0; + ULONG result; + + printf("%s ... ", message); + + _SEH_TRY + { + result = func(1); + } + _SEH_HANDLE + { + status = _SEH_GetExceptionCode(); + } + _SEH_END; + if (status == 0) + { + printf("OK.\n"); + } + else + { + printf("Error, status=%lx.\n", status); + } +} + +char data[100]; + +int main(void) +{ + unsigned char stack[100]; + void* heap; + ULONG protection; + + printf("NoExecute\n"); + + execute("Executing within the code segment", test); + memcpy(data, test, 100); + execute("Executing within the data segment", (int(*)(int))data); + memcpy(stack, test, 100); + execute("Executing on stack segment", (int(*)(int))stack); + heap = VirtualAlloc(NULL, 100, MEM_COMMIT, PAGE_READWRITE); + memcpy(heap, test, 100); + execute("Executing on the heap with protection PAGE_READWRITE", (int(*)(int))heap); + VirtualProtect(heap, 100, PAGE_EXECUTE, &protection); + execute("Executing on the heap with protection PAGE_EXECUTE", (int(*)(int))heap); + + return 0; +}