From 8b0a236e66c2ba99a085d2ca79db9e979856e0cd Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Stelmach?= Date: Mon, 28 Nov 2016 14:09:19 +0100 Subject: [PATCH] tests: add a simple test test1-crash crashes and dumps a core. test1-sleep enters an infinite sleep enabling ptrace-based stack examination. test1-ill triggers the "illegal instruction" exception (SIGILL). Change-Id: I995f5ce8817e49ec57bac6cac8e7f458807d03f0 --- CMakeLists.txt | 2 ++ tests/CMakeLists.txt | 11 +++++++++ tests/test1.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test1.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bd6432..7dc9668 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,3 +15,5 @@ ADD_SUBDIRECTORY(src/crash-stack) ADD_SUBDIRECTORY(src/dump_systemstate) ADD_SUBDIRECTORY(src/log_dump) +ADD_SUBDIRECTORY(tests) + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..043bdcc --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,11 @@ +# tests +set(CMAKE_C_FLAGS "-g -O0 -fno-omit-frame-pointer") + +if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv7l") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -marm") +endif () + +add_executable(test1-crash test1.c) +add_executable(test1-sleep test1.c) +add_executable(test1-ill test1.c) + diff --git a/tests/test1.c b/tests/test1.c new file mode 100644 index 0000000..016a857 --- /dev/null +++ b/tests/test1.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int do_sleep=0; +int do_ill=0; + +#if defined(__arm__) +char ill_buf[] = { 0xf0, 0x00, 0x00, 0xf0 }; +#elif defined(__aarch64__) +char ill_buf[] = { 0x22, 0x00, 0x00, 0xf9 }; +#elif defined(__i386__) || defined(__x86_64__) +char ill_buf[] = { 0x06, 0x00, 0x00, 0x00 }; +#else +#error "Unsupported architecture" +#endif + +int main(int ac, char *av[]); + +void(*baz)(void); + +int bar(int a, int b){ + int *c=(int*)0; + if (do_ill) + baz(); + if (do_sleep) + sleep(-1); + return a + b + *c; +} + +int foo(int c){ + int a=1; + int b=2; + return bar(a+c,b+c); +} + +int *memory; + +int allocate(size_t amount) { + size_t i; + memory = malloc(amount); + + if (memory == NULL) + exit(1); + + for (i=0; i < amount/sizeof(size_t); i++) { + memory[i]=i; + } + return 0; +} + +int main(int ac, char* av[]) +{ + if (strcmp(basename(av[0]), "test1-sleep") == 0) + do_sleep = 1; + if (strcmp(basename(av[0]), "test1-ill") == 0) + do_ill = 1; + baz = mmap(0, sizeof(ill_buf), PROT_READ|PROT_WRITE|PROT_EXEC, + MAP_PRIVATE|MAP_ANON, -1, 0); + memcpy(baz, ill_buf, sizeof(ill_buf)); + allocate(256*1024*1024); + return foo(2); +} -- 2.7.4