tests: add a simple test 39/103539/3
authorŁukasz Stelmach <l.stelmach@samsung.com>
Mon, 28 Nov 2016 13:09:19 +0000 (14:09 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 8 Dec 2016 15:53:31 +0000 (16:53 +0100)
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
tests/CMakeLists.txt [new file with mode: 0644]
tests/test1.c [new file with mode: 0644]

index 7bd6432..7dc9668 100644 (file)
@@ -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 (file)
index 0000000..043bdcc
--- /dev/null
@@ -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 (file)
index 0000000..016a857
--- /dev/null
@@ -0,0 +1,68 @@
+#include <libgen.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+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);
+}