From 2d2628df0001d2b643811dc69717c4afe70c4e31 Mon Sep 17 00:00:00 2001
From: David Phillips <david@sighup.nz>
Date: Sun, 11 Aug 2019 17:22:20 +1200
Subject: Fix and add tests for NULL deref cases found by scan-build

---
 test/asm/run-asm.sh                                | 59 ++++++++++++++++++++++
 test/asm/should-fail/001-segfault-li-comma-end.asm |  2 +
 test/asm/should-fail/002-segfault-b-short.asm      |  2 +
 test/asm/should-fail/003-segfault-j-short.asm      |  2 +
 test/asm/should-fail/004-segfault-imm-short.asm    |  2 +
 5 files changed, 67 insertions(+)
 create mode 100755 test/asm/run-asm.sh
 create mode 100644 test/asm/should-fail/001-segfault-li-comma-end.asm
 create mode 100644 test/asm/should-fail/002-segfault-b-short.asm
 create mode 100644 test/asm/should-fail/003-segfault-j-short.asm
 create mode 100644 test/asm/should-fail/004-segfault-imm-short.asm

(limited to 'test/asm')

diff --git a/test/asm/run-asm.sh b/test/asm/run-asm.sh
new file mode 100755
index 0000000..7574abb
--- /dev/null
+++ b/test/asm/run-asm.sh
@@ -0,0 +1,59 @@
+#!/bin/bash -e
+
+#
+# Script for running all of the automated which will go from source to binary.
+#
+
+fail() {
+	echo -e '[\e[1;31mFAIL\e[0m] '"$1:" "$2"
+	has_failure=1
+}
+
+pass() {
+	echo -e '[\e[1;32mPASS\e[0m] '"$1"
+}
+
+clean() {
+	echo "Removing work dir $WORK"
+	rm -r "$WORK"
+}
+
+if [ "$1" == "noclean" ]; then
+	NO_CLEAN=1
+else
+	NO_CLEAN=0
+fi
+WORK=$(mktemp -d)
+pushd $(dirname "$0") >/dev/null
+source ../valgrind.sh
+export ASM="$PWD/../../assembler"
+export DISASM="$PWD/../../disassembler"
+has_failure=0
+
+for first_stage_asm in should-fail/*.asm ; do
+	t=$(basename "$first_stage_asm")
+	first_stage_bin="$WORK/${t}-first_stage.bin"
+	log="$WORK/${t}.log"
+
+	# Assemble test code
+	set +e
+	$VALGRIND $VALGRIND_OPTS "$ASM" "$first_stage_asm" "$first_stage_bin" 2>"$log"
+	xc="$?"
+	set -e
+	if (( xc > 0 && xc < 128 )); then
+			pass "$t" "assembly xfailed"
+	elif (( xc == 0 )); then
+		fail "$t" "assembly didn't fail as expected"
+	else
+		fail "$t" "assembler was sent signal $(( xc - 128 ))"
+	fi
+done
+popd >/dev/null
+
+if [[ "$failure" != "0" && "$NO_CLEAN" == "1"  ]] ; then
+	echo "Warning: Leaving work dir $WORK in place. Please remove this yourself"
+else
+	clean
+fi
+
+exit "$has_failure"
diff --git a/test/asm/should-fail/001-segfault-li-comma-end.asm b/test/asm/should-fail/001-segfault-li-comma-end.asm
new file mode 100644
index 0000000..bde586c
--- /dev/null
+++ b/test/asm/should-fail/001-segfault-li-comma-end.asm
@@ -0,0 +1,2 @@
+; Test for bug previously found with scan-build. Important: no CR/LF at EOF
+ldi $1, 
\ No newline at end of file
diff --git a/test/asm/should-fail/002-segfault-b-short.asm b/test/asm/should-fail/002-segfault-b-short.asm
new file mode 100644
index 0000000..898a3b0
--- /dev/null
+++ b/test/asm/should-fail/002-segfault-b-short.asm
@@ -0,0 +1,2 @@
+; Test for bug previously found with scan-build. Important: no CR/LF at EOF
+bnz
\ No newline at end of file
diff --git a/test/asm/should-fail/003-segfault-j-short.asm b/test/asm/should-fail/003-segfault-j-short.asm
new file mode 100644
index 0000000..0b1486b
--- /dev/null
+++ b/test/asm/should-fail/003-segfault-j-short.asm
@@ -0,0 +1,2 @@
+; Test for bug previously found with scan-build. Important: no CR/LF at EOF
+jmp
\ No newline at end of file
diff --git a/test/asm/should-fail/004-segfault-imm-short.asm b/test/asm/should-fail/004-segfault-imm-short.asm
new file mode 100644
index 0000000..42e8c4b
--- /dev/null
+++ b/test/asm/should-fail/004-segfault-imm-short.asm
@@ -0,0 +1,2 @@
+; Test for bug previously found with scan-build. Important: no CR/LF at EOF
+addi $0, $0,
\ No newline at end of file
-- 
cgit v1.1