From a323269a098a3c08a82521a296bbc43670563b81 Mon Sep 17 00:00:00 2001
From: jdh8d <jdh8d@git.zephyr-software.com>
Date: Sun, 19 Apr 2015 23:33:23 +0000
Subject: [PATCH] updated zipr callbacks to work for x86-64

---
 configure             | 23 +++++++-------
 configure.in          | 19 ++++++------
 configure_for_rss     |  2 +-
 libc/include/stdint.h | 12 ++++++--
 libc/src/itoa.c       | 11 ++++---
 rss/rss_callbacks.c   | 70 ++++++++++++++++++++++++++++++++++++++++---
 6 files changed, 103 insertions(+), 34 deletions(-)

diff --git a/configure b/configure
index aa46b1e..adf1197 100755
--- a/configure
+++ b/configure
@@ -590,9 +590,9 @@ ASFLAGS
 AS
 LD
 DIRS
+OPTIMIZE
 LTLIBOBJS
 LIBOBJS
-OPTIMIZE
 OBJEXT
 EXEEXT
 ac_ct_CC
@@ -2553,22 +2553,12 @@ if test "${enable_inferfn+set}" = set; then :
   enableval=$enable_inferfn;
 fi
 
-#AC_ARG_ENABLE([example],              	[  --enable-debugging                enable -g when compiling])
 # Check whether --enable-libc was given.
 if test "${enable_libc+set}" = set; then :
   enableval=$enable_libc;
 fi
 
 
-OPTIMIZE="-O3"
-if test "$enable_debugging" = yes; then
-        OPTIMIZE="-g  -DDEBUG"
-fi
-
-OPTIMIZE=$OPTIMIZE
-
-
-
 enable_dir()
 {
 
@@ -4901,10 +4891,17 @@ fi
 	fi
 }
 
+OPTIMIZE="-O3"
+if test "$enable_debugging" = yes; then
+        OPTIMIZE="-g  -DDEBUG"
+fi
+
+
+
 LIB=$ZIPR_CALLBACKS/lib/callbacks.a
 AR=ar
 CC=gcc
-CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-stack-protector"
+CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-stack-protector -fno-builtin"
 LD=gcc
 LDFLAGS="-r -nostdlib -fPIC -fPIE"
 AS=nasm
@@ -7254,6 +7251,8 @@ $as_echo "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
 fi
 
 
+OPTIMIZE=$OPTIMIZE
+
 
 
 
diff --git a/configure.in b/configure.in
index 63faf14..b591c24 100644
--- a/configure.in
+++ b/configure.in
@@ -7,17 +7,8 @@ AC_PROG_CC
 AC_ARG_ENABLE([debugging],              [  --enable-debugging                enable -g when compiling])
 AC_ARG_ENABLE([cgc],              	[  --enable-cgc                enable -DCGC when compiling for CGC binaries])
 AC_ARG_ENABLE([inferfn],              	[  --enable-inferfn                enable -DCGC when compiling for CGC binaries])
-#AC_ARG_ENABLE([example],              	[  --enable-debugging                enable -g when compiling])
 AC_ARG_ENABLE([libc],              	[  --enable-libc                enable $1 for building ])
 
-OPTIMIZE="-O3"
-if test "$enable_debugging" = yes; then
-        OPTIMIZE="-g  -DDEBUG"
-fi
-
-AC_SUBST(OPTIMIZE, $OPTIMIZE)
-
-
 enable_dir()
 {
 
@@ -37,10 +28,17 @@ enable_dir()
 	fi
 }
 
+OPTIMIZE="-O3"
+if test "$enable_debugging" = yes; then
+        OPTIMIZE="-g  -DDEBUG"
+fi
+
+
+
 LIB=$ZIPR_CALLBACKS/lib/callbacks.a
 AR=ar
 CC=gcc
-CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-stack-protector"
+CFLAGS="$OPTIMIZE -nostdinc -fPIE -fPIC -I$ZIPR_CALLBACKS/libc/include  -fno-stack-protector -fno-builtin"
 LD=gcc
 LDFLAGS="-r -nostdlib -fPIC -fPIE"
 AS=nasm
@@ -73,6 +71,7 @@ enable_dir rss
 
 AC_OUTPUT(Makefile)
 
+AC_SUBST(OPTIMIZE, $OPTIMIZE)
 AC_SUBST(DIRS)
 AC_SUBST(CC)
 AC_SUBST(CFLAGS)
diff --git a/configure_for_rss b/configure_for_rss
index 159ad5b..c85966d 100755
--- a/configure_for_rss
+++ b/configure_for_rss
@@ -1,4 +1,4 @@
 #!/bin/bash
 
 # pickup the CGC syscalls
-./configure --enable-rss --enable-libc --prefix=$ZIPR_INSTALL
+./configure --enable-rss --enable-libc --prefix=$ZIPR_INSTALL $*
diff --git a/libc/include/stdint.h b/libc/include/stdint.h
index 9b7f618..d7f0f9d 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -21,8 +21,14 @@
 #ifndef stdint_h
 #define stdint_h
 
-typedef signed long int ssize_t;
-typedef unsigned long int size_t;
-typedef unsigned long int uintptr_t;
+#ifdef LINUX64
+	typedef signed long long int ssize_t;
+	typedef unsigned long long int size_t;
+	typedef unsigned long long int uintptr_t;
+#else
+	typedef signed long int ssize_t;
+	typedef unsigned long int size_t;
+	typedef unsigned long int uintptr_t;
+#endif
 
 #endif
diff --git a/libc/src/itoa.c b/libc/src/itoa.c
index 6f32665..ee69424 100644
--- a/libc/src/itoa.c
+++ b/libc/src/itoa.c
@@ -1,14 +1,17 @@
 
 #include <strlen.h>
+#include <stdint.h>
 
-void itox(unsigned int i, char *s)
+/* convert integer i into string of hex digits at string s. */
+void itox(uintptr_t i, char *s)
 {
     unsigned char n;
- 
-    s += 8;
+    
+    /* 4-byte integers print 8 chars, 8-byte integers print 16 bytes */
+    s += 2*sizeof(void*);
     *s = '\0';
  
-    for (n = 8; n != 0; --n) {
+    for (n = 2*sizeof(void*); n != 0; --n) {
         *--s = "0123456789ABCDEF"[i & 0x0F];
         i >>= 4;
     }
diff --git a/rss/rss_callbacks.c b/rss/rss_callbacks.c
index 07770bd..9476630 100644
--- a/rss/rss_callbacks.c
+++ b/rss/rss_callbacks.c
@@ -19,15 +19,77 @@
  */
 
 #include <stdint.h>
+#include <strlen.h>
 
-void zipr_push_stack(uintptr_t ret)
+
+#define MAX_ENTRIES 4096
+
+uintptr_t ret_shadow_stack[MAX_ENTRIES];
+int rss_head_pointer=0;
+
+static void print_stack(char* s)
 {
-	char str[]="zipr_push_callback\n";
+#ifdef DEBUG
+	write(1,s,strlen(s));
+	s=": rss_head_pointer=";
+	write(1,s,strlen(s));
+	char rss_head_str[2*sizeof(void*)+1];
+	itox(rss_head_pointer,rss_head_str);
+	write(1,rss_head_str,strlen(rss_head_str));
+	write(1,"\n",1);
+	int i;
+	for(i=0;i<rss_head_pointer;i++)
+	{
+		char i_str[2*sizeof(void*)+1];
+		char entry_str[2*sizeof(void*)+1];
+		itox(i,i_str);
+		itox(ret_shadow_stack[i],entry_str);
+		write(1,i_str,strlen(i_str));
+		write(1,",",1);
+		write(1,entry_str,strlen(entry_str));
+		write(1,"\n",1);
+		
+	}
+#endif
+}
+
+void zipr_push_stack(uintptr_t app_ret_addr, uintptr_t stack_ret_addr)
+{
+#ifdef DEBUG
+	char appret[2*sizeof(void*)+1];
+	char stackret[2*sizeof(void*)+1];
+	itox(app_ret_addr,appret);
+	itox(stack_ret_addr,stackret);
+	char str[]="zipr_push_callback, appret,stackret=";
 	write(1,str,sizeof(str));
+	write(1,appret,sizeof(appret));
+	write(1,",",1);
+	write(1,stackret,sizeof(stackret));
+	write(1,"\n",1);
+
+	print_stack("push");
+#endif
+	ret_shadow_stack[rss_head_pointer++]=stack_ret_addr;
 }
 
-void zipr_pop_stack(uintptr_t ret)
+void zipr_pop_stack(uintptr_t app_ret_addr, uintptr_t stack_ret_addr)
 {
-	char str[]="zipr_pop_callback\n";
+#ifdef DEBUG
+	char appret[2*sizeof(void*)+1];
+	char stackret[2*sizeof(void*)+1];
+	itox(app_ret_addr,appret);
+	itox(stack_ret_addr,stackret);
+	char str[]="zipr_pop_callback, appret,stackret=";
 	write(1,str,sizeof(str));
+	write(1,appret,sizeof(appret));
+	write(1,",",1);
+	write(1,stackret,sizeof(stackret));
+	write(1,"\n",1);
+	print_stack("pop");
+#endif
+
+	if(ret_shadow_stack[--rss_head_pointer]!=stack_ret_addr)
+		*(int*)(0)=0;
+	
+
 }
-- 
GitLab