Added support for more functions. 
Formatting changes.

llvm-svn: 123730
diff --git a/safecode/include/safecode/Runtime/BBRuntime.h b/safecode/include/safecode/Runtime/BBRuntime.h
index 2fdd76f..0083b39 100755
--- a/safecode/include/safecode/Runtime/BBRuntime.h
+++ b/safecode/include/safecode/Runtime/BBRuntime.h
@@ -158,7 +158,7 @@
   void * bb_exactcheck2_debug (const char *base, const char *result, unsigned size,
                             TAG, SRC_INFO);
 
-  void bb___sc_dbg_funccheck (unsigned num, void *f, void *g, ...);
+  void __sc_bb_funccheck (unsigned num, void *f, void *g, ...);
   void * bb_pchk_getActualValue (PPOOL, void * src);
 
   // Change memory protections to detect dangling pointers
diff --git a/safecode/runtime/BBRuntime/BaggyBoundsCheck.cpp b/safecode/runtime/BBRuntime/BaggyBoundsCheck.cpp
index 489cb20..8803240 100644
--- a/safecode/runtime/BBRuntime/BaggyBoundsCheck.cpp
+++ b/safecode/runtime/BBRuntime/BaggyBoundsCheck.cpp
@@ -40,10 +40,6 @@
 #include <signal.h>
 #include <ucontext.h>
 #include <sys/mman.h>
-#if 0
-#include <sys/ucontext.h>
-#endif
-
 #include <pthread.h>
 
 #define TAG unsigned tag
@@ -69,18 +65,14 @@
 FILE * ReportLog;
 
 // Configuration for C code; flags that we should stop on the first error
-//unsigned StopOnError = 0;
 unsigned StopOnError;
 
 // signal handler
 static void bus_error_handler(int, siginfo_t *, void *);
 
-
-
-
 unsigned SLOT_SIZE = 4;
 unsigned WORD_SIZE = 64;
-unsigned char * __baggybounds_size_table1_begin1; 
+unsigned char * __baggybounds_size_table_begin; 
 
 
 //===----------------------------------------------------------------------===//
@@ -89,9 +81,19 @@
 //
 //===----------------------------------------------------------------------===//
 
+void *
+__sc_bb_poolinit(DebugPoolTy *Pool, unsigned NodeSize, unsigned) {
+  return Pool;
+}
+
+void 
+__sc_bb_pooldestroy(DebugPoolTy *Pool) {
+  return;
+}
+
 
 //
-// Function: baggy_init_runtime
+// Function: pool_init_runtime
 //
 // Description: 
 //   This function is called to initialize the entire SAFECode run-time. It 
@@ -104,10 +106,6 @@
 //  Terminate  - Set to non-zero to have SAFECode terminate when an error 
 //               occurs. 
 //
-void *
-__sc_bb_poolinit(DebugPoolTy *Pool, unsigned NodeSize, unsigned) {
-  return Pool;
-}
 
 void 
 pool_init_runtime(unsigned Dangling, unsigned RewriteOOB, unsigned Terminate) {
@@ -133,11 +131,6 @@
   madvise (Addr, invalidsize, MADV_FREE);
   InvalidLower = (uintptr_t) Addr;
   InvalidUpper = (uintptr_t) Addr + invalidsize;
-  /*if (logregs) {
-    fprintf (stderr, "OOB Area: %p - %p\n", (void *) InvalidLower,
-                                            (void *) InvalidUpper);
-    fflush (stderr);
-  }*/
 #endif
   
   //
@@ -169,9 +162,9 @@
     fflush (stderr);
   }
   // Initialize the baggy bounds table
-  __baggybounds_size_table1_begin1 =(unsigned char*) mmap(0, ((size_t)(1024*1024*1024)*(size_t)(64*1024)), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_NORESERVE, -1, 0);
+  __baggybounds_size_table_begin =(unsigned char*) mmap(0, ((size_t)(1024*1024*1024)*(size_t)(64*1024)), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_NORESERVE, -1, 0);
 
-  if(__baggybounds_size_table1_begin1 == MAP_FAILED) 
+  if(__baggybounds_size_table_begin == MAP_FAILED) 
   {
     fprintf (stderr, "Baggy Bounds Table initialization failed!");
     fflush (stderr);
@@ -181,7 +174,6 @@
   return;
 }
 
-
 void
 __internal_register(void *allocaptr, unsigned NumBytes) {
   uintptr_t Source = (uintptr_t)allocaptr;
@@ -191,33 +183,27 @@
     size++;
   }
   size = (size < SLOT_SIZE) ? SLOT_SIZE : size;
-  //printf("registering %p for size %u\n", allocaptr, NumBytes);
   uintptr_t Source1 = Source & ~((1<<size)-1);
   if(Source1 != Source) {
     printf("%p, %p, %u Not aligned\n", (void*)Source, (void*)Source1, NumBytes);
     assert(false);
   }
   Source = Source & ~((1<<size)-1);
-  //printf("registering %p for size %u\n", (void*)Source, NumBytes);
   unsigned long index = Source >> SLOT_SIZE;
   unsigned range = 1 << (size - SLOT_SIZE);
 
-  memset(__baggybounds_size_table1_begin1 + index, size, range);
+  memset(__baggybounds_size_table_begin + index, size, range);
   return;
 }
 
 //
-// Function: poolargvregister()
+// Function: sc_bb_poolargvregister()
 //
 // Description:
 //  Register all of the argv strings in the external object pool.
 //
 void * 
 __sc_bb_poolargvregister(int argc, char **argv) {
-    /*if (logregs) {
-      fprintf (stderr, "poolargvregister:  \n"); 
-      fflush (stderr);
-    }*/
   char ** argv_temp = (char **)__sc_bb_src_poolalloc(NULL,(sizeof(char*)*(argc+1)),0,"main\n", 0);
   for (int index=0; index < argc; ++index) {
     char *argv_index_temp = (char *)__sc_bb_src_poolalloc(NULL, (strlen(argv[index])+ 1)*sizeof(char),0,"main\n", 0);
@@ -225,14 +211,9 @@
     
     __internal_register(argv_index_temp, (strlen (argv[index]) + 1)*sizeof(char));
     argv_temp[index]= argv_index_temp;
-    /*if (logregs) {
-      fprintf (stderr, "poolargvregister: %p %u: %s %p \n", argv_index_temp,
-               (unsigned)strlen(argv[index]), argv_index_temp, argv_temp[index]);
-      fflush (stderr);
-    }*/
     
   }
-  argv_temp[argc]=NULL;
+  argv_temp[argc] = NULL;
 
   //
   // Register the actual argv array as well.  Note that the transform can
@@ -247,7 +228,7 @@
 }
 
 //
-// Function: __baggy_dbg_src_poolregister()
+// Function: __sc_bb_src_poolregister()
 //
 // Description:
 //  This function is externally visible and is called by code to register
@@ -263,18 +244,12 @@
   // Print debug information about what object the caller is trying to
   // register.
   //
-  /*if (logregs) {
-    fprintf (ReportLog, "poolreg_debug(%d):  %p-%p: %d %d %s %d\n", tag,
-             (void*)allocaptr,
-             ((char*)(allocaptr)) + NumBytes - 1, NumBytes, tag, SourceFilep, lineno);
-    fflush (ReportLog);
-  }*/
   __internal_register(allocaptr, NumBytes);
   return;
 }
 
 //
-// Function: __baggy_dbg_src_poolregister_stack()
+// Function: __sc_bb_src_poolregister_stack()
 //
 // Description:
 //  This function is externally visible and is called by code to register
@@ -286,25 +261,19 @@
 				unsigned NumBytes, TAG,
 				const char* SourceFilep, 
 				unsigned lineno) {
-  /*if (logregs) {
-    fprintf (ReportLog, "poolreg_debug_stack(%d):  %p-%p: %d %d %s %d\n", tag,
-              allocaptr,
-             ((char*)(allocaptr)) + NumBytes - 1, NumBytes, tag, SourceFilep, lineno);
-    fflush (ReportLog);
-  }*/
   __internal_register(allocaptr, NumBytes);
   return;
 }
 
 //
-// Function: __baggy_dbg_poolregister_stack()
+// Function: __sc_bb_poolregister_stack()
 //
 // Description:
 //  This function is externally visible and is called by code to register
 //  a stack allocation without debug information.
 //
 void 
-__ssc_bb_poolregister_stack (DebugPoolTy *Pool,
+__sc_bb_poolregister_stack (DebugPoolTy *Pool,
 				void * allocaptr, 
 				unsigned NumBytes) {
   __sc_bb_src_poolregister_stack(Pool, allocaptr, NumBytes, 0, "<unknown>", 0);
@@ -312,7 +281,7 @@
 }
 
 //
-// Function: __baggy_dbg_src_poolregister_global()
+// Function: __sc_bb_src_poolregister_global()
 //
 // Description:
 //  This function is externally visible and is called by code to register
@@ -322,12 +291,13 @@
 __sc_bb_poolregister_global (DebugPoolTy *Pool,
 				 void *allocaptr, 
 				 unsigned NumBytes) {
-  __sc_bb_src_poolregister_global_debug(Pool, allocaptr, NumBytes, 0 , "<unknown>", 0);
+  __sc_bb_src_poolregister_global_debug(Pool, 
+                                        allocaptr, NumBytes, 0 , "<unknown>", 0);
   return;
 }
 
 //
-// Function: __baggy_dbg_src_poolregister_global_debug()
+// Function: __sc_bb_src_poolregister_global_debug()
 //
 // Description:
 //  This function is externally visible and is called by code to register
@@ -343,7 +313,7 @@
 }
 
 //
-// Function: poolregister()
+// Function: __sc_bb_poolregister()
 //
 // Description:
 //  Register the memory starting at the specified pointer of the specified size
@@ -368,13 +338,9 @@
 				TAG,
 				const char* SourceFilep,
 				unsigned lineno) {
-  /*if (logregs) {
-    fprintf (stderr, "pool_unregister: Start: %p: %s %d\n", allocaptr, SourceFilep, lineno);
-    fflush (stderr);
-  }*/
   uintptr_t Source = (uintptr_t)allocaptr;
   unsigned  e;
-  e = __baggybounds_size_table1_begin1[Source >> SLOT_SIZE];
+  e = __baggybounds_size_table_begin[Source >> SLOT_SIZE];
   if(e == 0 ) {
     return;
   }
@@ -383,7 +349,7 @@
   unsigned long index = base >> SLOT_SIZE;
   unsigned int slots = 1<<(e - SLOT_SIZE);
   
-  memset(__baggybounds_size_table1_begin1 + index, 0, slots);
+  memset(__baggybounds_size_table_begin + index, 0, slots);
 }
 
 void
@@ -397,14 +363,10 @@
 				TAG,
 				const char* SourceFilep,
 				unsigned lineno) {
-  /*if (logregs) {
-    fprintf (stderr, "pool_unregister: Start: %p: %s %d\n", allocaptr, SourceFilep, lineno);
-    fflush (stderr);
-  }*/
   uintptr_t Source = (uintptr_t)allocaptr;
   
   unsigned  e;
-  e = __baggybounds_size_table1_begin1[Source >> SLOT_SIZE];
+  e = __baggybounds_size_table_begin[Source >> SLOT_SIZE];
   if(e == 0 ) {
     return;
   }
@@ -412,7 +374,7 @@
   uintptr_t base = Source & ~(size -1);
   unsigned long index = base >> SLOT_SIZE;
   unsigned int slots = 1<<(e - SLOT_SIZE);
-  memset(__baggybounds_size_table1_begin1 + index, 0, slots);
+  memset(__baggybounds_size_table_begin + index, 0, slots);
 }
 
 //
@@ -431,18 +393,11 @@
   while((unsigned)(1<<size) < NumBytes) {
     size++;
   }
- // unsigned char size = (unsigned char)ceil(log(NumBytes)/log(2)); 
   if(size < SLOT_SIZE) size = SLOT_SIZE;
-  unsigned int alloc = 1<< size;
+  unsigned int alloc = 1 << size;
   void *p;
   posix_memalign(&p, alloc, alloc);
 
-  /*if (logregs) {
-    fprintf (ReportLog, "poolalloc_debug(%d): %p-%p: %d %d %s %d\n", tag,
-              (void*)p,
-             ((char*)(p)) + NumBytes - 1, NumBytes, tag, SourceFilep, lineno);
-    fflush (ReportLog);
-  }*/
   return p;
 }
 
@@ -453,13 +408,12 @@
   while((unsigned)(1<<size) < (NumBytes*Number)) {
     size++;
   }
-  //unsigned char size = (unsigned char)ceil(log(NumBytes*Number)/log(2)); 
   if(size < SLOT_SIZE) size = SLOT_SIZE;
   unsigned int alloc = 1<< size;
   void *p;
   posix_memalign(&p, alloc, alloc);
   __sc_bb_src_poolregister(Pool, p, (Number*NumBytes), tag, SourceFilep, lineno);
-  if(p) {
+  if (p){
     bzero(p, Number*NumBytes);
   }
   return p;
@@ -489,7 +443,7 @@
   __sc_bb_poolregister(Pool, New, NumBytes);
 
   uintptr_t Source = (uintptr_t)Node; 
-  unsigned  char e = __baggybounds_size_table1_begin1[Source >> SLOT_SIZE];
+  unsigned  char e = __baggybounds_size_table_begin[Source >> SLOT_SIZE];
   unsigned int size = 1 << e;
   memcpy(New, Node, size);
 
@@ -497,6 +451,7 @@
   __sc_bb_poolfree(Pool, Node);
   return New;
 }
+
 void *
 __sc_bb_poolalloc(DebugPoolTy *Pool,
 		      unsigned NumBytes) {
@@ -504,7 +459,7 @@
 }
 
 //
-// Function: poolfree_debug()
+// Function: __sc_bb_src_poolfree()
 //
 // Description:
 //  This function is identical to poolfree() except that it relays source-level
@@ -515,10 +470,6 @@
 		          void *Node,TAG,
 			  const char* SourceFile, 
 			  unsigned lineno) {
-  /*if (logregs) {
-    fprintf(stderr, "pool_unregister:1387: Node=%p\n", Node);
-    fflush (stderr);
-  }*/
   free(Node);
 }	
 
@@ -572,11 +523,7 @@
 //
 static void
 bus_error_handler (int sig, siginfo_t * info, void * context) {
-  /*if (logregs) {
-    fprintf (stderr, "SAFECode: Fault!\n");
-    fflush (stderr);
-  }*/
-
+  
   //
   // Disable the signal handler for now.  If this function does something
   // wrong, we want the bus error to terminate the program.
diff --git a/safecode/runtime/BBRuntime/RuntimeChecks.cpp b/safecode/runtime/BBRuntime/RuntimeChecks.cpp
index 282ab5b..7824f25 100755
--- a/safecode/runtime/BBRuntime/RuntimeChecks.cpp
+++ b/safecode/runtime/BBRuntime/RuntimeChecks.cpp
@@ -39,7 +39,7 @@
 
 
 extern FILE * ReportLog;
-extern unsigned char* __baggybounds_size_table1_begin1; 
+extern unsigned char* __baggybounds_size_table_begin; 
 extern unsigned SLOT_SIZE;
 extern unsigned WORD_SIZE;
 extern const unsigned int  logregs;
@@ -61,20 +61,14 @@
   //
   // Check if it is an OOB pointer
   //
-  //void *ObjStart, *ObjEnd;
   uintptr_t val =1 ;
   unsigned char e;
 
-  e = __baggybounds_size_table1_begin1[Source >> SLOT_SIZE];
+  e = __baggybounds_size_table_begin[Source >> SLOT_SIZE];
   val = (Source^Dest)>>e;
   if(val) {
   
-  //if (isRewritePtr ((void*)Source)) {// If it is OOB ptr, report a violation  
     if(Source & 0xffff800000000000) {
-    /*    void * RealSrc = baggy_pchk_getActualValue(NULL, (void*)Source);
-        Dest = ((intptr_t) RealSrc + (Dest -  Source));
-        getOOBObject ((void*)Source, ObjStart, ObjEnd);
-        Source = (uintptr_t)(ObjStart);*/
 
       Source = Source & 0x7fffffffffff;
       if(Source & 0x8) {
@@ -87,7 +81,7 @@
   //
   // Look for the bounds in the table
   //
-    e = __baggybounds_size_table1_begin1[Source >> SLOT_SIZE];
+    e = __baggybounds_size_table_begin[Source >> SLOT_SIZE];
     if(e == 0) {
       return (void*)Dest;
     }
@@ -99,7 +93,7 @@
 
     if(val) {
   //    return rewrite_ptr(NULL, (void*)Source, (void*)Source, (void*)Source, "<unknown", 0); 
-        return (void*)(Dest | 0xffff800000000000);
+        Dest = (Dest | 0xffff800000000000);
     }
   }
   return (void*)Dest;
@@ -122,7 +116,6 @@
   // Check if is an OOB pointer
   //
     if((uintptr_t)Node& 0xffff800000000000) {
-  //if (isRewritePtr (Node) ){// If it is OOB ptr, report a violation  
   
     DebugViolationInfo v;
     v.type = ViolationInfo::FAULT_LOAD_STORE,
@@ -185,17 +178,7 @@
 
 void
 bb_poolcheckui (DebugPoolTy *Pool, void *Node, TAG) {
-  /*if (logregs) {
-      fprintf (stderr, "PoolCheck :   %p \n",  Node );
-      fflush (stderr);
-  }*/
-  //if (isRewritePtr (Node)) {// If it is OOB ptr, report a violation 
     if((uintptr_t)Node& 0xffff800000000000) {
-    /*if (logregs) {
-      fprintf (stderr, "PoolCheck : Violation(A):  %p \n",  Node );
-      fflush (stderr);
-    }
-    pintf("%p\n", Node);*/
     assert(false);
   }
   return;
@@ -217,10 +200,6 @@
 void * __attribute__((noinline))
 bb_boundscheck_debug (DebugPoolTy * Pool, void * Source, void * Dest, TAG, const char * SourceFile, unsigned lineno) {
 
- /* if (logregs) {
-    fprintf (stderr, "boundscheck_debug(%d): %d: %p - %p\n", tag, lineno, Source, Dest);
-    fflush (stderr);
-  }*/
   return _barebone_boundscheck((uintptr_t)Source, (uintptr_t)Dest);
 }
 
@@ -245,10 +224,6 @@
                      void * Dest, TAG,
                      const char * SourceFile,
                      unsigned int lineno) {
-  /*if (logregs) {
-    fprintf (stderr, "boundscheck_debug(%d): %s %d: %p - %p\n", tag, SourceFile, lineno, Source, Dest);
-    fflush (stderr);
-  }*/
   return  _barebone_boundscheck((uintptr_t)Source, (uintptr_t)Dest);
 }
 
@@ -316,3 +291,35 @@
   
   return (void*)Source;
 }
+
+//
+// Function: funccheck()
+//
+// Description:
+//  Determine whether the specified function pointer is one of the functions
+//  in the given list.
+//
+// Inputs:
+//  num - The number of function targets in the DSNode.
+//  f   - The function pointer that we are testing.
+//  g   - The first function given in the DSNode.
+//
+void
+__sc_bb_funccheck (unsigned num, void *f, void *g, ...) {
+  va_list ap;
+  unsigned i = 0;
+
+  // Test against the first function in the list
+  if (f == g) return;
+  i++;
+  va_start(ap, g);
+  for ( ; i != num; ++i) {
+    void *h = va_arg(ap, void *);
+    if (f == h) {
+      return;
+    }
+  }
+  abort();
+}
+
+