electric-fence: builds and boots
authorRonald G. Minnich <rminnich@gmail.com>
Wed, 31 May 2017 17:00:14 +0000 (10:00 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 7 Jun 2017 15:38:14 +0000 (11:38 -0400)
tstheap fails:

Unhandled user trap from early SCP

in uthread_yield, some other dumb mistake I'm making.

Change-Id: Ib1428dcb2a8c1984352e2adc4c8dea050d497e48
Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
Makefile
tests/Makefile
tests/electric-fence/Makefrag [new file with mode: 0644]
tests/electric-fence/tstheap.c [new file with mode: 0644]
user/electric-fence/Makefile
user/electric-fence/efence.c
user/electric-fence/page.c
user/electric-fence/tstheap.c [deleted file]

index 0bd5a83..f7c7861 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -601,7 +601,7 @@ ndblib: iplib
 # Higher-level libraries.  Built before tests/, but after apps-install.
 # TODO: would like to move perfmon here, since it's not meant to be low-level.
 # But the apps-install has perf, which depends on user/perfmon.
-user-extra-dirs = vmm
+user-extra-dirs = vmm electric-fence
 $(user-extra-dirs): $(user-base-dirs)
 
 user-dirs = $(user-base-dirs) $(user-extra-dirs)
index b8c4095..8fc77b7 100644 (file)
@@ -27,6 +27,7 @@ TESTS_EXECS_CPP  = $(patsubst $(TESTS_DIR)/%.cc, \
 include $(TESTS_DIR)/openmp/Makefrag
 include $(TESTS_DIR)/vmm/Makefrag
 include $(TESTS_DIR)/dune/Makefrag
+include $(TESTS_DIR)/electric-fence/Makefrag
 
 TESTS_EXECS = $(TESTS_EXECS_C) $(TESTS_EXECS_CPP)
 
diff --git a/tests/electric-fence/Makefrag b/tests/electric-fence/Makefrag
new file mode 100644 (file)
index 0000000..532f8e4
--- /dev/null
@@ -0,0 +1,25 @@
+ELECTRIC-FENCE_TESTS_DIR = $(TESTS_DIR)/electric-fence
+
+ELECTRIC-FENCE_TESTS_CFLAGS += $(CFLAGS_TESTS) -Wunused -Werror -D__NO_STRING_INLINES
+
+ALL_ELECTRIC-FENCE_TEST_FILES := $(wildcard $(ELECTRIC-FENCE_TESTS_DIR)/*.c)
+
+ELECTRIC-FENCE_TESTS_LDLIBS := $(TESTS_LDLIBS) -lelf -lelectric-fence
+
+ELECTRIC-FENCE_TESTS_SRCS := $(ALL_ELECTRIC-FENCE_TEST_FILES)
+
+ELECTRIC-FENCE_TESTS_LDDEPENDS := $(ELECTRIC-FENCE_TESTS_DIR)/%.c
+
+TESTS_EXECS_C  += $(patsubst $(ELECTRIC-FENCE_TESTS_DIR)/%.c, \
+                      $(OBJDIR)/$(ELECTRIC-FENCE_TESTS_DIR)/%, \
+                      $(ELECTRIC-FENCE_TESTS_SRCS))
+
+STATIC := $(findstring static,$(ELECTRIC-FENCE_TESTS_CFLAGS))
+$(OBJDIR)/$(ELECTRIC-FENCE_TESTS_DIR)/%: $(ELECTRIC-FENCE_TESTS_LDDEPENDS)
+       @echo + cc [ELECTRIC-FENCE_TESTS] $<
+       @mkdir -p $(@D)
+       $(Q)$(CC) $(ELECTRIC-FENCE_TESTS_CFLAGS) -o $@ $< $(ELECTRIC-FENCE_TESTS_LDLIBS)
+       @if [ "$(STATIC)" != "static" ]; then \
+               $(OBJDUMP) -S $@ > $@.asm; \
+               $(NM) -n $@ > $@.sym; \
+       fi
diff --git a/tests/electric-fence/tstheap.c b/tests/electric-fence/tstheap.c
new file mode 100644 (file)
index 0000000..3daf189
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <limits.h>
+#include "efence.h"
+
+/*
+ * This is a simple program to exercise the allocator. It allocates and frees
+ * memory in a pseudo-random fashion. It should run silently, using up time
+ * and resources on your system until you stop it or until it has gone
+ * through TEST_DURATION (or the argument) iterations of the loop.
+ */
+
+extern C_LINKAGE double drand48(void); /* For pre-ANSI C systems */
+
+#define        POOL_SIZE       1024
+#define        LARGEST_BUFFER  30000
+#define        TEST_DURATION   1000000
+
+void * pool[POOL_SIZE];
+
+#ifdef FAKE_DRAND48
+/*
+ * Add -DFAKE_DRAND48 to your compile flags if your system doesn't
+ * provide drand48().
+ */
+
+#ifndef        ULONG_MAX
+#define        ULONG_MAX       ~(1L)
+#endif
+
+double
+drand48(void)
+{
+       return (random() / (double)ULONG_MAX);
+}
+#endif
+
+int
+main(int argc, char * * argv)
+{
+       int     count = 0;
+       int     duration = TEST_DURATION;
+
+       if ( argc >= 2 )
+               duration = atoi(argv[1]);
+
+       for ( ; count < duration; count++ ) {
+               void * *        element = &pool[(int)(drand48() * POOL_SIZE)];
+               size_t          size = (size_t)(drand48() * (LARGEST_BUFFER + 1));
+
+               if ( *element ) {
+                       free( *element );
+                       *element = 0;
+               }
+               else if ( size > 0 ) {
+                       *element = malloc(size);
+               }
+       }
+
+       // now let's be dumb.
+       char *c = malloc(4096);
+       c[4097] = 0;
+       return 0;
+}
index 0ca89b2..684c989 100644 (file)
@@ -1,3 +1,4 @@
-LIBNAME = eflib
+LIBNAME = electric-fence
 INCDIR = .
+CFLAGS_USER += -D__NO_STRING_INLINES
 include ../Makefrag-user-lib
index 0a0d506..2930400 100644 (file)
@@ -193,18 +193,18 @@ static size_t             bytesPerPage = 0;
  /*
  * mutex to enable multithreaded operation
  */
-static pthread_mutex_t mutex ;
+static uth_mutex_t mutex;
 static pid_t mutexpid=0;
 static int locknr=0;
 
 
 static void lock() {
-    if (pthread_mutex_trylock(&mutex)) {
+    if (uth_mutex_trylock(&mutex)) {
        if (mutexpid==getpid()) {
            locknr++;
            return;
        } else {
-           pthread_mutex_lock(&mutex);
+           uth_mutex_lock(&mutex);
        }
     } 
     mutexpid=getpid();
@@ -215,7 +215,7 @@ static void unlock() {
     locknr--;
     if (!locknr) {
        mutexpid=0;
-       pthread_mutex_unlock(&mutex);
+       uth_mutex_unlock(&mutex);
     }
 }
 
@@ -814,7 +814,7 @@ malloc(size_t size)
         void  *allocation;   
  
         if ( allocationList == 0 ) {
-                pthread_mutex_init(&mutex, NULL); 
+               uth_mutex_init(&mutex);
                 initialize();   /* This sets EF_ALIGNMENT */
         }       
         lock();
index 925c44c..bc925e7 100644 (file)
@@ -7,55 +7,14 @@
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <stdio.h>
-#include <errno.h>
 #include <string.h>
-
-/*
- * Lots of systems are missing the definition of PROT_NONE.
- */
-#ifndef        PROT_NONE
-#define        PROT_NONE       0
-#endif
-
-/*
- * 386 BSD has MAP_ANON instead of MAP_ANONYMOUS.
- */
-#if ( !defined(MAP_ANONYMOUS) && defined(MAP_ANON) )
-#define        MAP_ANONYMOUS   MAP_ANON
-#endif
-
-/*
- * For some reason, I can't find mprotect() in any of the headers on
- * IRIX or SunOS 4.1.2
- */
-/* extern C_LINKAGE int mprotect(void * addr, size_t len, int prot); */
+#include <parlib/uthread.h>
 
 static caddr_t startAddr = (caddr_t) 0;
 
-#if ( !defined(sgi) && !defined(_AIX) && __DARWIN_C_LEVEL < __DARWIN_C_FULL )
-extern int     sys_nerr;
-/*extern char *        sys_errlist[];*/
-#endif
-
-static const char *
-stringErrorReport(void)
-{
-#if ( defined(sgi) )
-       return strerror(oserror());
-#elif ( defined(_AIX) )
-       return strerror(errno);
-#else
-       if ( errno > 0 && errno < sys_nerr )
-               return sys_errlist[errno];
-       else
-               return "Unknown error.\n";
-#endif
-}
-
 /*
  * Create memory.
  */
-#if defined(MAP_ANONYMOUS)
 void *
 Page_Create(size_t size)
 {
@@ -79,7 +38,6 @@ Page_Create(size_t size)
        ,-1
        ,0);
 
-#ifndef        __hpux
        /*
         * Set the "address hint" for the next mmap() so that it will abut
         * the mapping we just created.
@@ -91,59 +49,17 @@ Page_Create(size_t size)
         * of those systems that were running 9.01 have been upgraded.
         */
        startAddr = allocation + size;
-#endif
-
-       if ( allocation == (caddr_t)-1 )
-               EF_Exit("mmap() failed: %s", stringErrorReport());
-
-       return (void *)allocation;
-}
-#else
-void *
-Page_Create(size_t size)
-{
-       static int      devZeroFd = -1;
-       caddr_t         allocation;
-
-       if ( devZeroFd == -1 ) {
-               devZeroFd = open("/dev/zero", O_RDWR);
-               if ( devZeroFd < 0 )
-                       EF_Exit(
-                        "open() on /dev/zero failed: %s"
-                       ,stringErrorReport());
-       }
-
-       /*
-        * In this version, "startAddr" is a _hint_, not a demand.
-        * When the memory I map here is contiguous with other
-        * mappings, the allocator can coalesce the memory from two
-        * or more mappings into one large contiguous chunk, and thus
-        * might be able to find a fit that would not otherwise have
-        * been possible. I could _force_ it to be contiguous by using
-        * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
-        * generated by other software, etc.
-        */
-       allocation = (caddr_t) mmap(
-        startAddr
-       ,size
-       ,PROT_READ|PROT_WRITE
-       ,MAP_PRIVATE
-       ,devZeroFd
-       ,0);
-
-       startAddr = allocation + size;
 
        if ( allocation == (caddr_t)-1 )
-               EF_Exit("mmap() failed: %s", stringErrorReport());
+               EF_Exit("mmap() failed: %r");
 
        return (void *)allocation;
 }
-#endif
 
 static void
 mprotectFailed(void)
 {
-       EF_Exit("mprotect() failed: %s", stringErrorReport());
+       EF_Exit("mprotect() failed: %r");
 }
 
 void
@@ -166,23 +82,9 @@ Page_Delete(void * address, size_t size)
        Page_DenyAccess(address, size);
 }
 
-#if defined(_SC_PAGESIZE)
-size_t
-Page_Size(void)
-{
-       return (size_t)sysconf(_SC_PAGESIZE);
-}
-#elif defined(_SC_PAGE_SIZE)
-size_t
-Page_Size(void)
-{
-       return (size_t)sysconf(_SC_PAGE_SIZE);
-}
-#else
-/* extern int  getpagesize(); */
+
 size_t
 Page_Size(void)
 {
-       return getpagesize();
+       return PGSIZE;
 }
-#endif
diff --git a/user/electric-fence/tstheap.c b/user/electric-fence/tstheap.c
deleted file mode 100644 (file)
index c712fed..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <math.h>
-#include <limits.h>
-#include "efence.h"
-
-/*
- * This is a simple program to exercise the allocator. It allocates and frees
- * memory in a pseudo-random fashion. It should run silently, using up time
- * and resources on your system until you stop it or until it has gone
- * through TEST_DURATION (or the argument) iterations of the loop.
- */
-
-extern C_LINKAGE double drand48(void); /* For pre-ANSI C systems */
-
-#define        POOL_SIZE       1024
-#define        LARGEST_BUFFER  30000
-#define        TEST_DURATION   1000000
-
-void * pool[POOL_SIZE];
-
-#ifdef FAKE_DRAND48
-/*
- * Add -DFAKE_DRAND48 to your compile flags if your system doesn't
- * provide drand48().
- */
-
-#ifndef        ULONG_MAX
-#define        ULONG_MAX       ~(1L)
-#endif
-
-double
-drand48(void)
-{
-       return (random() / (double)ULONG_MAX);
-}
-#endif
-
-int
-main(int argc, char * * argv)
-{
-       int     count = 0;
-       int     duration = TEST_DURATION;
-
-       if ( argc >= 2 )
-               duration = atoi(argv[1]);
-
-       for ( ; count < duration; count++ ) {
-               void * *        element = &pool[(int)(drand48() * POOL_SIZE)];
-               size_t          size = (size_t)(drand48() * (LARGEST_BUFFER + 1));
-
-               if ( *element ) {
-                       free( *element );
-                       *element = 0;
-               }
-               else if ( size > 0 ) {
-                       *element = malloc(size);
-               }
-       }
-       return 0;
-}