From 3ffbe347642e1c9b9c30c496b48fa6fb506393bf Mon Sep 17 00:00:00 2001
From: an7s <an7s@git.zephyr-software.com>
Date: Fri, 14 Oct 2016 20:14:16 +0000
Subject: [PATCH] Add libc tests

Former-commit-id: a7a01cb1b0b00a1a5f44c5827fad319fe04f295d
---
 .gitattributes                          |  2 +
 tools/selective_cfi/tests/libc_driver.c |  6 ++
 tools/selective_cfi/tests/test_foo.sh   |  2 +-
 tools/selective_cfi/tests/test_libc.sh  | 84 +++++++++++++++++++++++++
 4 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 tools/selective_cfi/tests/libc_driver.c
 create mode 100755 tools/selective_cfi/tests/test_libc.sh

diff --git a/.gitattributes b/.gitattributes
index 970ce8d8a..04d099ff2 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1158,6 +1158,7 @@ tools/selective_cfi/scfi_instr.hpp -text
 tools/selective_cfi/tests/dude.c -text
 tools/selective_cfi/tests/fib.c -text
 tools/selective_cfi/tests/foo.c -text
+tools/selective_cfi/tests/libc_driver.c -text
 tools/selective_cfi/tests/libdude.c -text
 tools/selective_cfi/tests/libfib.c -text
 tools/selective_cfi/tests/libfib2.c -text
@@ -1166,6 +1167,7 @@ tools/selective_cfi/tests/pow.c -text
 tools/selective_cfi/tests/test_dude.sh -text
 tools/selective_cfi/tests/test_fib.sh -text
 tools/selective_cfi/tests/test_foo.sh -text
+tools/selective_cfi/tests/test_libc.sh -text
 tools/selective_cfi/tests/test_pow.sh -text
 tools/selective_cfi/zest_cfi_runtime/SConscript -text
 tools/selective_cfi/zest_cfi_runtime/SConscript32 -text
diff --git a/tools/selective_cfi/tests/libc_driver.c b/tools/selective_cfi/tests/libc_driver.c
new file mode 100644
index 000000000..38ebfd514
--- /dev/null
+++ b/tools/selective_cfi/tests/libc_driver.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+main()
+{
+	printf("hello world\n");
+}
diff --git a/tools/selective_cfi/tests/test_foo.sh b/tools/selective_cfi/tests/test_foo.sh
index e92f3eb45..dfa8706c1 100755
--- a/tools/selective_cfi/tests/test_foo.sh
+++ b/tools/selective_cfi/tests/test_foo.sh
@@ -72,7 +72,7 @@ main()
 	test foo.exe libfoo.so.cfi		# shared lib only
 	test foo.exe.cfi libfoo.so.cfi		# both protected
 	report
-	clean
+#	clean
 }
 
 passes=0 
diff --git a/tools/selective_cfi/tests/test_libc.sh b/tools/selective_cfi/tests/test_libc.sh
new file mode 100755
index 000000000..6217f8c8e
--- /dev/null
+++ b/tools/selective_cfi/tests/test_libc.sh
@@ -0,0 +1,84 @@
+#!/bin/bash 
+
+do_cfi()
+{
+	$PS $1 $2 --backend zipr --step move_globals=on --step selective_cfi=on --step-option selective_cfi:--multimodule --step-option move_globals:--cfi  --step-option fix_calls:--fix-all --step-option zipr:"--add-sections false"
+	if [ ! $? -eq 0 ]; then
+		echo "do_cfi(): failed to protect"
+	fi
+}
+
+do_coloring_cfi()
+{
+	$PS $1 $2 --backend zipr --step move_globals=on --step selective_cfi=on --step-option selective_cfi:--multimodule --step-option move_globals:--cfi  --step-option fix_calls:--fix-all --step-option selective_cfi:--color  --step-option zipr:"--add-sections false"
+	if [ ! $? -eq 0 ]; then
+		echo "do_coloring_cfi(): failed to protect"
+	fi
+}
+
+
+get_correct()
+{
+	cp libfoo.so.orig libfoo.so
+	./foo.exe > correct
+}
+
+test()
+{
+	
+	cp $2 libfoo.so  
+	./$1 > out 
+
+	cmp out correct
+	if [ $? = 1 ]; then
+		fails=$(expr $fails + 1 )
+		echo test failed
+	else
+		passes=$(expr $passes + 1 )
+		echo test passed.
+	fi
+}
+
+
+build()
+{
+	gcc -o libc_driver.exe libc_driver.c -w 
+
+	libcpath=$(ldd libc_driver.exe|grep libc.so.6|sed "s/.*=>//"|sed "s/(.*//")
+	echo libc=$libcpath
+	cp $libcpath libc.so.6.orig
+}
+
+
+protect()
+{
+	do_cfi libc.so.6.orig libc-2.19.so.cfi
+}
+
+clean()
+{
+	rm out
+	rm correct
+	rm -Rf foo.exe peasoup_exe* libfoo.so libfoo.so.orig libfoo.so.cfi foo.cfi foo.exe.cfi
+}
+
+report ()
+{
+	total=$(expr $passes + $fails)
+	echo "Passes:  $passes / $total"
+	echo "Fails :  $fails / $total"
+}
+
+main()
+{
+	build
+	protect
+#	get_correct
+#	report
+#	clean
+}
+
+passes=0 
+fails=0
+
+main $*
-- 
GitLab