From 433dbb0f40b63c9532e0d25f0616c176b5893024 Mon Sep 17 00:00:00 2001
From: an7s <an7s@git.zephyr-software.com>
Date: Fri, 2 Sep 2011 18:08:09 +0000
Subject: [PATCH] Multiply examples

---
 .gitattributes                  |  2 ++
 examples/integerbug.c           | 54 ++++++++++++++++++++++++++++++---
 examples/mul/generate_cprogs.sh | 53 ++++++++++++++++++++++++++++++++
 examples/mul/mul.ctmpl          | 21 +++++++++++++
 4 files changed, 125 insertions(+), 5 deletions(-)
 create mode 100755 examples/mul/generate_cprogs.sh
 create mode 100644 examples/mul/mul.ctmpl

diff --git a/.gitattributes b/.gitattributes
index 2ac542c52..6cc5923c9 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -145,6 +145,8 @@ examples/Makefile -text
 examples/dumbledore.c -text
 examples/dumbledore_cmd.c -text
 examples/integerbug.c -text
+examples/mul/generate_cprogs.sh -text
+examples/mul/mul.ctmpl -text
 examples/overflow1.c -text
 examples/test1.c -text
 libIRDB/Makefile -text
diff --git a/examples/integerbug.c b/examples/integerbug.c
index 7b3c4ab48..43a270481 100644
--- a/examples/integerbug.c
+++ b/examples/integerbug.c
@@ -1,5 +1,7 @@
-#define MAX_INT 2147483647
-#define MAX_UINT 4294967295
+#include <stdio.h>
+
+#define INT_MAX 2147483647
+#define UINT_MAX 4294967295
 
 int int_fussy_overflow(int x, int y)
 {
@@ -58,6 +60,23 @@ char* integer_underflow(unsigned len, char *src)
   return comm;
 }
 
+#define BUFF_SIZE 10
+
+// if len is < 0, this will bypass the check 
+// which will result in an overflow of buf[] 
+char* sign_error_buffer_overflow(int len, char *src)
+{
+  printf("sign_error_buffer_overflow(): %d\n", len);
+  char buf[BUFF_SIZE];
+  if (len < BUFF_SIZE) {
+    printf("Copying %u bytes into buffer of size %u\n", len, BUFF_SIZE);
+    return memcpy(buf, src, len);
+  }
+  else {
+    return NULL;
+  }
+}
+
 char* signed_error(int size)
 {
   printf("signed_error(): %d\n", size);
@@ -87,6 +106,24 @@ char* trunc_error(unsigned size, int numElements)
   return malloc(len * numElements);
 }
 
+short sign_extend_char_short(char c)
+{
+  short s;
+  return s = c;
+}
+
+short sign_extend_char_long(char c)
+{
+  long l;
+  return l = c;
+}
+
+short sign_extend_short_long(short s)
+{
+  long l;
+  return l = s;
+}
+
 int main(int argc, char **argv)
 {
   int selector = 0;
@@ -109,9 +146,10 @@ int main(int argc, char **argv)
       bufptr = integer_underflow(10, buf);
       result = signed_error_bypass_check(10);
       bufptr = trunc_error(10, 10);
+      signed_overflow(2, 3);
       break;
 
-    // bad inputs here
+    // "bad" inputs here
     case 1:
       bufptr = integer_overflow_into_malloc_2(2000000000, 4);
       break;
@@ -128,10 +166,16 @@ int main(int argc, char **argv)
       bufptr = trunc_error(65000, 10);
       break;
     case 6:
-      int_fussy_overflow(MAX_INT,MAX_INT);
+      int_fussy_overflow(INT_MAX,INT_MAX);
       break;
     case 7:
-      signed_overflow(MAX_INT, MAX_INT);
+      signed_overflow(INT_MAX, INT_MAX);
+      break;
+    case 8:
+      signed_overflow(INT_MAX, INT_MAX);
+      break;
+    case 9:
+      sign_error_buffer_overflow(-1, buf);
       break;
   }
 }
diff --git a/examples/mul/generate_cprogs.sh b/examples/mul/generate_cprogs.sh
new file mode 100755
index 000000000..a40985010
--- /dev/null
+++ b/examples/mul/generate_cprogs.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+#types="char short int long unsigned+char unsigned+short unsigned+int unsigned+long";
+types="int char"
+
+create_prog()
+{
+	progname=$1
+	my_type1=$2
+	my_type2=$3
+
+	# substitute space for +
+        real_type1=`echo $my_type1 | sed "s/+/ /g"`
+        real_type2=`echo $my_type2 | sed "s/+/ /g"`
+
+	# substitute _ for + to get a valid C function name
+        name_type1=`echo $my_type1 | sed "s/+/_/g"`
+        name_type2=`echo $my_type2 | sed "s/+/_/g"`
+        function_name="test_${name_type1}_${name_type2}"
+
+        case $my_type1 in
+	"char") format_specifier="%c" ;;
+	"short") format_specifier="%hd" ;;
+	"int") format_specifier="%d" ;;
+	"long") format_specifier="%ld" ;;
+	"unsigned+char") format_specifier="%uc" ;;
+	"unsigned+short") format_specifier="%hu" ;;
+	"unsigned+int") format_specifier="%u" ;;
+	"unsigned+long") format_specifier="%ul" ;;
+	esac
+
+        # create the program.
+        cat mul.ctmpl                       | \
+                sed "s/#FUNCTION_NAME#/$function_name/g" | \
+                sed "s/#FORMAT_SPECIFIER#/$format_specifier/g"     | \
+                sed "s/#TYPE1#/$real_type1/g"     | \
+                sed "s/#TYPE2#/$real_type2/g"     \
+                        > $progname.c
+
+	gcc -w $progname.c -o $progname.orig.exe
+	
+	$PEASOUP_HOME/tools/ps_analyze.sh --step ilr=off --step p1transform=off $progname.orig.exe $progname.protected.exe
+}
+
+for type1 in $types
+do
+	for type2 in $types
+	do
+		progname_c=mul.$type1.$type2
+		# actually create the .c program
+		create_prog $progname_c "$type1" "$type2" static_lib
+	done
+done
diff --git a/examples/mul/mul.ctmpl b/examples/mul/mul.ctmpl
new file mode 100644
index 000000000..f73522e05
--- /dev/null
+++ b/examples/mul/mul.ctmpl
@@ -0,0 +1,21 @@
+#TYPE1# #FUNCTION_NAME#(#TYPE1# x, #TYPE2# y)
+{
+  #TYPE1# result = x * y;
+  printf("result = #FORMAT_SPECIFIER#\n", result);
+  return result;
+}
+
+main()
+{
+  #TYPE1# x = 127;
+  #TYPE2# y = 0xFFFFFFFF;
+  #FUNCTION_NAME#(x, y);
+
+  x = 0x0FFFFFFF;
+  y = 0x0FFFFFFF;
+  #FUNCTION_NAME#(x, y);
+
+  x = 0x0000007F;
+  y = 0x0000007F;
+  #FUNCTION_NAME#(x, y);
+}
-- 
GitLab