about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2024-12-03 11:43:38 +0100
committerptitSeb <sebastien.chev@gmail.com>2024-12-03 11:43:46 +0100
commit794f2104bcee9a2aff192804d9c07b2163e79a51 (patch)
tree6d96d0493dc84d3ad10ffc6b598fce976b46a903 /tests
parent036af9f55fa5b8542872c14b801bce5512c31583 (diff)
downloadbox64-794f2104bcee9a2aff192804d9c07b2163e79a51.tar.gz
box64-794f2104bcee9a2aff192804d9c07b2163e79a51.zip
Improved signal handling ([BOX32] Too)
Diffstat (limited to 'tests')
-rw-r--r--tests/ref21.txt48
-rwxr-xr-xtests/test21bin23696 -> 23624 bytes
-rw-r--r--tests/test21.c195
3 files changed, 227 insertions, 16 deletions
diff --git a/tests/ref21.txt b/tests/ref21.txt
index 8fc221a0..14aada64 100644
--- a/tests/ref21.txt
+++ b/tests/ref21.txt
@@ -1,6 +1,50 @@
 sig = 11
 got bad_ptr
 sig = 5
-si_addr: 0, si_code: 128, si_errno: 0, RIP offset: 1, TRAPERR=0 TRAPNO=3
+si_addr: 0, si_code: 128, si_errno: 0, RIP offset: 1, TRAPERR=0x0 TRAPNO=3
 sig = 5
-si_addr: 0, si_code: 128, si_errno: 0, RIP offset: 2, TRAPERR=0 TRAPNO=3
+si_addr: 0, si_code: 128, si_errno: 0, RIP offset: 2, TRAPERR=0x0 TRAPNO=3
+sig = 11
+si_addr: 0, si_code: 128, si_errno: 0, RIP offset: 0, TRAPERR=0x16a TRAPNO=13
+from non-existant memory
+sig = 11
+si_addr: ffffffffdeadbeef, si_code: 1, si_errno: 0, TRAPERR=0x7 TRAPNO=14
+segfault, good
+sig = 11
+si_addr: ffffffffdeadbeef, si_code: 1, si_errno: 0, TRAPERR=0x5 TRAPNO=14
+segfault, good
+sig = 11
+si_addr: ffffffffdeadbeef, si_code: 1, si_errno: 0, TRAPERR=0x15 TRAPNO=14
+segfault, good
+from NULL memory
+sig = 11
+si_addr: 0, si_code: 1, si_errno: 0, TRAPERR=0x6 TRAPNO=14
+segfault, good
+sig = 11
+si_addr: 0, si_code: 1, si_errno: 0, TRAPERR=0x4 TRAPNO=14
+segfault, good
+sig = 11
+si_addr: 0, si_code: 1, si_errno: 0, TRAPERR=0x14 TRAPNO=14
+segfault, good
+from existant memory
+exec_p prot = 0
+sig = 11
+si_addr: exec_p+0, si_code: 2, si_errno: 0, TRAPERR=0x6 TRAPNO=14
+segfault, good
+sig = 11
+si_addr: exec_p+0, si_code: 2, si_errno: 0, TRAPERR=0x4 TRAPNO=14
+segfault, good
+exec_p prot = PROT_READ
+sig = 11
+si_addr: exec_p+0, si_code: 2, si_errno: 0, TRAPERR=0x7 TRAPNO=14
+segfault, good
+exec_p prot = PROT_READ|PROT_WRITE
+sig = 11
+si_addr: exec_p+0, si_code: 2, si_errno: 0, RIP offset: 0, TRAPERR=0x15 TRAPNO=14
+Cannot run, good
+exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC
+exec_p prot = PROT_READ|PROT_WRITE
+sig = 11
+si_addr: exec_p+0, si_code: 2, si_errno: 0, RIP offset: 0, TRAPERR=0x15 TRAPNO=14
+Cannot run, good!
+exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC
diff --git a/tests/test21 b/tests/test21
index 934ebcb7..7696b453 100755
--- a/tests/test21
+++ b/tests/test21
Binary files differdiff --git a/tests/test21.c b/tests/test21.c
index 09bae72a..74865023 100644
--- a/tests/test21.c
+++ b/tests/test21.c
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdint.h>
 
 static jmp_buf context_buf;
 
@@ -33,12 +34,20 @@ typedef void(*vFv_t)(void);
 #define X_ERR		19
 static void segv_action(int sig, siginfo_t* info, ucontext_t* ucntx)
 {
+	if(!exec_p) {
+		segv_handler(sig);
+		return;
+	}
 	printf("sig = %d\n", sig);
-	printf("si_addr: %zx, si_code: %d, si_errno: %d, RIP offset: %zd, TRAPERR=%d TRAPNO=%d\n", 
-		info->si_addr,
-		info->si_code,
-		info->si_errno,
-		((intptr_t)ucntx->uc_mcontext.gregs[X_IP])-((intptr_t)exec_p),
+	uintptr_t rip = (intptr_t)ucntx->uc_mcontext.gregs[X_IP];
+	if(info->si_addr>=exec_p && info->si_addr<(exec_p+10))
+		printf("si_addr: exec_p+%zx, ", (uintptr_t)info->si_addr-(uintptr_t)exec_p);
+	else
+		printf("si_addr: %zx, ", info->si_addr);
+	printf("si_code: %d, si_errno: %d, ", info->si_code, info->si_errno);
+	if(rip>=((intptr_t)exec_p) && rip<((intptr_t)exec_p+5))
+		printf("RIP offset: %zd, ", rip-((intptr_t)exec_p));
+	printf("TRAPERR=0x%x TRAPNO=%d\n", 
 		ucntx->uc_mcontext.gregs[X_ERR],
 		ucntx->uc_mcontext.gregs[X_TRAPNO]
 	);
@@ -48,6 +57,7 @@ static void segv_action(int sig, siginfo_t* info, ucontext_t* ucntx)
 static unsigned char buff_cc[] = { 0xcc, 0xc3 };
 static unsigned char buff_cd03[] = { 0xcd, 0x03, 0xc3 };
 static unsigned char buff_cd2d[] = { 0xcd, 0x2d, 0xc3 };
+static uint8_t buff_simplef[] = { 0xb8, 1, 0, 0, 0, 0xc3 };
 void test_cc()
 {
 	memcpy(exec_p, buff_cc, sizeof(buff_cc));
@@ -60,24 +70,178 @@ void test_cc()
 		vFv_t f = (vFv_t)exec_p;
 		f();
 	}
-	/*memcpy(exec_p, buff_cd2d, sizeof(buff_cd2d));
+	memcpy(exec_p, buff_cd2d, sizeof(buff_cd2d));
 	if(!setjmp(context_buf)) {
 		vFv_t f = (vFv_t)exec_p;
 		f();
-	}*/
+	}
 }
 
-int main()
+void test_segfault()
 {
-	if(signal(SIGSEGV, segv_handler) == SIG_ERR) {
-		printf("signal: Err = %d\n", errno);
-		return -1;
+	printf("from non-existant memory\n");
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		int *bad_ptr = (int*)0xffffffffdeadbeef;
+		*(uint8_t*)bad_ptr = 0xc3;
+	} else {
+		printf("segfault, good\n");
 	}
-	//printf("handler = %p\n", segv_handler);
-	test();
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		int *bad_ptr = (int*)0xffffffffdeadbeef;
+		if(*(uint8_t*)bad_ptr == 0xc3)
+			printf("should not be readable or writeable!\n");
+		else
+			printf("should not be readable!\n");
+		printf("aborting test\n");
+		return;
+	} else {
+		printf("segfault, good\n");
+	}
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		void* bad_ptr = (int*)0xffffffffdeadbeef;
+		void(*f)() = bad_ptr;
+		f();
+		printf("should not work!!! aboting test\n");
+		return;
+	} else {
+		printf("segfault, good\n");
+	}
+	printf("from NULL memory\n");
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		int *bad_ptr = (int*)NULL;
+		*(uint8_t*)bad_ptr = 0xc3;
+	} else {
+		printf("segfault, good\n");
+	}
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		int *bad_ptr = (int*)NULL;
+		if(*(uint8_t*)bad_ptr == 0xc3)
+			printf("should not be readable or writeable!\n");
+		else
+			printf("should not be readable!\n");
+		printf("aborting test\n");
+		return;
+	} else {
+		printf("segfault, good\n");
+	}
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		void* bad_ptr = (int*)NULL;
+		void(*f)() = bad_ptr;
+		f();
+		printf("should not work!!! aboting test\n");
+		return;
+	} else {
+		printf("segfault, good\n");
+	}
+	printf("from existant memory\n");
+	printf("exec_p prot = 0\n");
+	mprotect(exec_p, 65536, 0);
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		*(uint8_t*)exec_p = 0xc3;
+	} else {
+		printf("segfault, good\n");
+	}
+	// reading for exising protected memory
+	if(!setjmp(context_buf)) {
+		if(*(uint8_t*)exec_p == 0xc3)
+			printf("Error, this value should not be 0xc3\n");
+	} else {
+		printf("segfault, good\n");
+	}
+	printf("exec_p prot = PROT_READ\n");
+	mprotect(exec_p, 65536, PROT_READ);
+	// writing to existing protected memory
+	if(!setjmp(context_buf)) {
+		*(uint8_t*)exec_p = 0xc3;
+	} else {
+		printf("segfault, good\n");
+	}
+	// reading should work
+	if(!setjmp(context_buf)) {
+		if(*(uint8_t*)exec_p == 0xc3)
+			printf("Error, this value should not be 0xc3\n");
+	} else {
+		printf("segfault, not good....\n");
+	}
+	// reading should work
+	if(!setjmp(context_buf)) {
+		if(*(uint8_t*)exec_p == 0xc3)
+			printf("Error, this value should not be 0xc3\n");
+	} else {
+		printf("segfault, good\n");
+	}
+	printf("exec_p prot = PROT_READ|PROT_WRITE\n");
+	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE);
+	// writing should
+	if(!setjmp(context_buf)) {
+		*(uint8_t*)exec_p = 0xc3;
+	} else {
+		printf("segfault, not good, aborting test\n");
+		return;
+	}
+	// reading should work
+	if(!setjmp(context_buf)) {
+		if(*(uint8_t*)exec_p != 0xc3) {
+			printf("Error, this value should be 0xc3, aborting test\n");
+			return;
+		}
+	} else {
+		printf("segfault, not good....\n");
+	}
+	// should not be able to run
+	if(!setjmp(context_buf)) {
+		vFv_t f = (vFv_t)exec_p;
+		f();
+	} else {
+		printf("Cannot run, good\n");
+	}
+	printf("exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC\n");
+	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE|PROT_EXEC);
+	if(!setjmp(context_buf)) {
+		vFv_t f = (vFv_t)exec_p;
+		f();
+	} else {
+		printf("Cannot run, not good!\n");
+	}
+	printf("exec_p prot = PROT_READ|PROT_WRITE\n");
+	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE);
+	memcpy(exec_p, buff_simplef, sizeof(buff_simplef));
+	if(!setjmp(context_buf)) {
+		int(*f)() = exec_p;
+		if(f()!=1) {
+			printf("function return should be 1\n");
+		}
+	} else {
+		printf("Cannot run, good!\n");
+	}
+	printf("exec_p prot = PROT_READ|PROT_WRITE|PROT_EXEC\n");
+	mprotect(exec_p, 65536, PROT_READ|PROT_WRITE|PROT_EXEC);
+	if(!setjmp(context_buf)) {
+		int(*f)() = exec_p;
+		if(f()!=1) {
+			printf("function return should be 1\n");
+		}
+		((uint8_t*)exec_p)[1] = 2;
+		if(f()!=2) {
+			printf("function return should be 2\n");
+		}
+	} else {
+		printf("Cannot run, not good, aborting test!\n");
+	}
+}
+
+int main()
+{
     struct sigaction action = {0};
     action.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
-    action.sa_sigaction = segv_action;
+    action.sa_sigaction = (void*)segv_action;
     if(sigaction(SIGSEGV, &action, NULL)) {
 		printf("sigaction: Err = %d\n", errno);
 		return -2;
@@ -86,11 +250,14 @@ int main()
 		printf("sigaction 2: Err = %d\n", errno);
 		return -2;
 	}
+	exec_p = NULL;
+	test();
 	exec_p = mmap(NULL, 65536, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
 	if(exec_p==MAP_FAILED) {
 		printf("mmap: Err = %d\n", errno);
 		return -3;
 	}
 	test_cc();
+	test_segfault();
 	return 0;
 }