Browse Source

Trivial changes

Chia-Che Tsai 8 years ago
parent
commit
cfa620e24a
43 changed files with 370 additions and 121 deletions
  1. 38 6
      LibOS/glibc-2.19.patch
  2. 6 0
      LibOS/glibc-2.19/syscalldb.c
  3. 6 0
      LibOS/glibc-2.19/syscalldb.h
  4. 1 1
      LibOS/shim/include/glibc-version.h
  5. 1 1
      LibOS/shim/include/shim_fs.h
  6. 8 1
      LibOS/shim/include/shim_handle.h
  7. 19 5
      LibOS/shim/include/shim_internal.h
  8. 2 0
      LibOS/shim/include/shim_table.h
  9. 13 0
      LibOS/shim/include/shim_thread.h
  10. 0 0
      LibOS/shim/src/.packed/shim.sha384
  11. BIN
      LibOS/shim/src/.packed/shim.tar.gz
  12. 33 8
      LibOS/shim/src/bookkeep/shim_vma.c
  13. 17 2
      LibOS/shim/src/fs/dev/fs.c
  14. 1 2
      LibOS/shim/src/fs/pipe/fs.c
  15. 0 1
      LibOS/shim/src/fs/shim_namei.c
  16. 1 3
      LibOS/shim/src/fs/socket/fs.c
  17. 2 1
      LibOS/shim/src/ipc/shim_ipc_helper.c
  18. 1 1
      LibOS/shim/src/shim.map
  19. 2 1
      LibOS/shim/src/shim_checkpoint.c
  20. 56 5
      LibOS/shim/src/shim_init.c
  21. 2 0
      LibOS/shim/src/shim_malloc.c
  22. 2 2
      LibOS/shim/src/shim_syscalls.c
  23. 1 1
      LibOS/shim/src/sys/shim_brk.c
  24. 1 8
      LibOS/shim/src/sys/shim_clone.c
  25. 0 9
      LibOS/shim/src/sys/shim_futex.c
  26. 26 9
      LibOS/shim/src/sys/shim_mmap.c
  27. 33 23
      LibOS/shim/src/sys/shim_poll.c
  28. 13 0
      LibOS/shim/src/sys/shim_time.c
  29. 3 6
      LibOS/shim/test/apps/openjdk/java.manifest.template
  30. 1 1
      LibOS/shim/test/native/.packed/test.sha384
  31. BIN
      LibOS/shim/test/native/.packed/test.tar.gz
  32. 18 0
      LibOS/shim/test/native/malloc.c
  33. 1 1
      Pal/src/Makefile.Host
  34. 49 3
      Pal/src/db_rtld.c
  35. 0 11
      Pal/src/dl-machine-x86_64.h
  36. 2 0
      Pal/src/do-rel.h
  37. 1 2
      Pal/src/host/Linux/clone-x86_64.S
  38. 1 1
      Pal/src/host/Linux/db_memory.c
  39. 1 3
      Pal/src/host/Linux/db_sockets.c
  40. 2 0
      Pal/src/pal.h
  41. 2 2
      Pal/src/pal_defs.h
  42. 1 0
      Pal/src/pal_internal.h
  43. 3 1
      Pal/src/slab.c

+ 38 - 6
LibOS/glibc-2.19.patch

@@ -316,7 +316,7 @@ index 4c58fc9..0ae2fa8 100644
  dl-routines += dl-cache
  endif
 diff --git a/elf/Versions b/elf/Versions
-index 2383992..03fbd38 100644
+index 2383992..1cdf63a 100644
 --- a/elf/Versions
 +++ b/elf/Versions
 @@ -24,14 +24,15 @@ libc {
@@ -324,7 +324,7 @@ index 2383992..03fbd38 100644
      __libc_dlclose; __libc_dlopen_mode; __libc_dlsym;
    }
 +  SHIM {
-+    syscalldb;
++    syscalldb; glibc_option;
 +  }
  }
  
@@ -348,7 +348,7 @@ index 2383992..03fbd38 100644
      __pointer_chk_guard;
    }
 +  SHIM {
-+    syscalldb; glibc_vers_2_17; register_library;
++    syscalldb; glibc_vers_2_17; glibc_option; register_library;
 +  }
  }
 diff --git a/elf/circleload1.c b/elf/circleload1.c
@@ -699,6 +699,38 @@ index eef2bfd..a21d2ac 100644
  
  #define OUT \
    for (map = MAPS; map != NULL; map = map->l_next)			      \
+diff --git a/malloc/arena.c b/malloc/arena.c
+index 5088a25..d12cf1e 100644
+--- a/malloc/arena.c
++++ b/malloc/arena.c
+@@ -21,6 +21,27 @@
+ 
+ /* Compile-time constants.  */
+ 
++#ifndef HEAP_MAX_SIZE
++# ifdef DEFAULT_MMAP_THRESHOLD_MAX
++#  define DEFAULT_HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX)
++# else
++#  define DEFAULT_HEAP_MAX_SIZE (1024 * 1024) /* must be a power of two */
++# endif
++# include <sysdep.h>
++static long int heap_max_size = 0;
++# define HEAP_MAX_SIZE								\
++	({									\
++		if (!heap_max_size) {						\
++			long int size = glibc_option("heap_size");		\
++			if (size > 0)						\
++				heap_max_size = size >> 1;			\
++			else							\
++				heap_max_size = DEFAULT_HEAP_MAX_SIZE;		\
++		}								\
++		heap_max_size;							\
++	})
++#endif
++
+ #define HEAP_MIN_SIZE (32 * 1024)
+ #ifndef HEAP_MAX_SIZE
+ # ifdef DEFAULT_MMAP_THRESHOLD_MAX
 diff --git a/nptl/Makefile b/nptl/Makefile
 index 57cc8c6..81f1bf4 100644
 --- a/nptl/Makefile
@@ -713,7 +745,7 @@ index 57cc8c6..81f1bf4 100644
  extra-libs := libpthread
  extra-libs-others := $(extra-libs)
 diff --git a/nptl/Versions b/nptl/Versions
-index bb11277..4480543 100644
+index bb11277..354149a 100644
 --- a/nptl/Versions
 +++ b/nptl/Versions
 @@ -31,6 +31,9 @@ libc {
@@ -721,7 +753,7 @@ index bb11277..4480543 100644
      __libc_dl_error_tsd;
    }
 +  SHIM {
-+    syscalldb;
++    syscalldb; glibc_option;
 +  }
  }
  
@@ -732,7 +764,7 @@ index bb11277..4480543 100644
    }
 +
 +  SHIM {
-+    syscalldb;
++    syscalldb; glibc_option;
 +  }
  }
 diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c

+ 6 - 0
LibOS/glibc-2.19/syscalldb.c

@@ -1,5 +1,6 @@
 #include "syscalldb.h"
 #include <stdarg.h>
+#include <errno.h>
 
 int register_library (const char * name, unsigned long load_address)
 	__attribute__((weak));
@@ -8,3 +9,8 @@ int register_library (const char * name, unsigned long load_address)
 {
 	return 0;
 }
+
+long int glibc_option (const char * opt)
+{
+	return -EINVAL;
+}

+ 6 - 0
LibOS/glibc-2.19/syscalldb.h

@@ -10,6 +10,12 @@ asm (
 ".weak syscalldb\r\n"
 ".type syscalldb, @function\r\n");
 
+long int glibc_option (const char * opt);
+
+asm (
+".weak glibc_option\r\n"
+".type glibc_option, @function\r\n");
+
 #endif /* Assembler */
 
 #endif /* _SYSCALLDB_H */

+ 1 - 1
LibOS/shim/include/glibc-version.h

@@ -1,6 +1,6 @@
 /* update the file whenever changes made to glibc.
    pick whatever random value. */
 
-#define GLIBC_VERSION_2_17      0x0e9d893a
+#define GLIBC_VERSION_2_17      0xd893a451
 
 int register_library (const char * name, unsigned long load_address);

+ 1 - 1
LibOS/shim/include/shim_fs.h

@@ -71,7 +71,7 @@ struct shim_fs_ops {
 
     int (*truncate) (struct shim_handle * hdl, int len);
 
-    /* stat: get status of the file */
+    /* hstat: get status of the file */
     int (*hstat) (struct shim_handle * hdl, struct stat * buf);
 
     /* setflags: set flags of the file */

+ 8 - 1
LibOS/shim/include/shim_handle.h

@@ -98,24 +98,31 @@ struct shim_file_handle {
 #define FILE_DENTRY_DATA(dent)  ((struct shim_file_data *) (dent)->data)
 
 struct shim_dev_ops {
+    /* open: provide a filename relative to the mount point and flags,
+       modify the shim handle */
     int (*open) (struct shim_handle * hdl, const char * name, int flags);
 
+    /* close: clean up the file state inside the handle */
     int (*close) (struct shim_handle * hdl);
 
+    /* read: the content from the file opened as handle */
     int (*read) (struct shim_handle * hdl, void * buf, size_t count);
 
+    /* write: the content from the file opened as handle */
     int (*write) (struct shim_handle * hdl, const void * buf, size_t count);
 
+    /* flush: flush out user buffer */
     int (*flush) (struct shim_handle * hdl);
 
+    /* seek: the content from the file opened as handle */
     int (*seek) (struct shim_handle * hdl, off_t offset, int wence);
 
     int (*truncate) (struct shim_handle * hdl, int len);
 
     int (*mode) (const char * name, mode_t * mode);
 
+    /* stat, hstat: get status of the file */
     int (*stat) (const char * name, struct stat * buf);
-
     int (*hstat) (struct shim_handle * hdl, struct stat * buf);
 };
 

+ 19 - 5
LibOS/shim/include/shim_internal.h

@@ -140,16 +140,15 @@ int shim_terminate (void);
 extern bool in_gdb;
 static inline void do_pause (void);
 
+#define BREAK_GDB() do { asm volatile ("int $3"); } while (0)
+
 #if USE_PAUSE == 1
 # define pause()                                                            \
     do {                                                                    \
-        if (in_gdb)                                                         \
-            asm volatile("int $3");                                         \
-        else                                                                \
-            do_pause();                                                     \
+        if (in_gdb) BREAK_GDB(); else do_pause();                           \
     } while (0)
 #else
-# define pause() do { if (in_gdb) asm volatile ("int $3"); } while (0)
+# define pause() do { if (in_gdb) BREAK_GDB(); } while (0)
 #endif
 
 #define bug()                                                               \
@@ -175,6 +174,9 @@ static inline void do_pause (void);
 # define assert(test) do {} while (0)
 #endif
 
+#define DEBUG_HERE() \
+    do { debug("%s (" __FILE__ ":%d)\n", __func__, __LINE__); } while (0)
+
 /* definition for syscall table */
 void handle_signal (bool delayed_only);
 long convert_pal_errno (long err);
@@ -199,10 +201,13 @@ long convert_pal_errno (long err);
 # define END_SYSCALL_PROFILE(name)      do {} while (0)
 #endif
 
+void check_stack_hook (void);
+
 #define BEGIN_SHIM(name, args ...)                          \
     SHIM_ARG_TYPE __shim_##name (args) {                    \
         SHIM_ARG_TYPE ret = 0;                              \
         /* handle_signal(true); */                          \
+        /* check_stack_hook(); */                           \
         BEGIN_SYSCALL_PROFILE();
 
 #define END_SHIM(name)                                      \
@@ -719,6 +724,8 @@ extern void * __code_address, * __code_address_end;
 
 int shim_clean (void);
 
+unsigned long parse_int (const char * str);
+
 extern void * initial_stack;
 extern const char ** initial_envp;
 
@@ -742,6 +749,13 @@ extern const char ** initial_envp;
         _stack;                                                     \
     })
 
+#define current_stack()                                             \
+    ({                                                              \
+        void * _rsp;                                                \
+        asm volatile ("movq %%rsp, %0" : "=r"(_rsp) :: "memory");   \
+        _rsp;                                                       \
+    })
+
 void get_brk_region (void ** start, void ** end, void ** current);
 
 int init_randgen (void);

+ 2 - 0
LibOS/shim/include/shim_table.h

@@ -443,6 +443,8 @@ int shim_do_epoll_ctl (int epfd, int op, int fd,
                        struct __kernel_epoll_event * event);
 int shim_do_clock_gettime (clockid_t which_clock,
                            struct timespec * tp);
+int shim_do_clock_getres (clockid_t which_clock,
+                          struct timespec * tp);
 int shim_do_exit_group (int error_code);
 int shim_do_tgkill (int tgid, int pid, int sig);
 int shim_do_openat (int dfd, const char * filename, int flags, int mode);

+ 13 - 0
LibOS/shim/include/shim_thread.h

@@ -285,6 +285,19 @@ void set_handle_map (struct shim_thread * thread,
 int thread_exit (struct shim_thread * self, bool send_ipc);
 int try_process_exit (int error_code);
 
+/* thread cloning helpers */
+struct clone_args {
+    PAL_HANDLE create_event;
+    PAL_HANDLE initialize_event;
+    struct shim_thread * parent, * thread;
+    void * stack;
+    void * return_pc;
+};
+
+int clone_implementation_wrapper(struct clone_args * arg);
+
+int clean_held_locks (struct shim_thread * self);
+
 void * allocate_stack (size_t size, size_t protect_size, bool user);
 int populate_user_stack (void * stack, size_t stack_size,
                          int nauxv, elf_auxv_t ** auxpp,

File diff suppressed because it is too large
+ 0 - 0
LibOS/shim/src/.packed/shim.sha384


BIN
LibOS/shim/src/.packed/shim.tar.gz


+ 33 - 8
LibOS/shim/src/bookkeep/shim_vma.c

@@ -169,7 +169,7 @@ void put_vma (struct shim_vma * vma)
 
 static void __remove_vma (struct shim_vma * vma)
 {
-    list_del_init(&vma->list);
+    list_del(&vma->list);
     put_vma(vma);
 }
 
@@ -230,6 +230,8 @@ static int __bkeep_mmap (void * addr, int length,
     struct shim_vma * tmp = __lookup_supervma(addr, length, &prev);
     int ret = 0;
 
+    debug("bkeep_mmap: %p - %p\n", addr, addr + length);
+
     if (file)
         get_handle(file);
 
@@ -315,7 +317,7 @@ static int __bkeep_mmap (void * addr, int length,
     tmp->file = file;
     tmp->offset = offset;
     __set_comment(tmp, comment);
-
+    assert(!prev || prev == tmp || prev->addr + prev->length <= tmp->addr);
     return 0;
 
 err:
@@ -349,6 +351,8 @@ static int __bkeep_munmap (void * addr, int length, const int * flags)
 {
     struct shim_vma * tmp, * n;
 
+    debug("bkeep_unmmap: %p - %p\n", addr, addr + length);
+
     list_for_each_entry_safe(tmp, n, &vma_list, list) {
         if (test_vma_equal (tmp, addr, length)) {
             if (!check_vma_flags(tmp, flags))
@@ -434,6 +438,8 @@ static int __bkeep_mprotect (void * addr, int length, int prot,
     struct shim_vma * tmp = __lookup_vma(addr, length);
     int ret;
 
+    debug("bkeep_mprotect: %p - %p\n", addr, addr + length);
+
     if (tmp) {
         /* exact match */
         if (!check_vma_flags(tmp, flags))
@@ -622,26 +628,43 @@ static void __set_heap_top (void * bottom, void * top)
 
 void * get_unmapped_vma (int length, int flags)
 {
-    struct shim_vma * new = get_new_vma(), * tmp = NULL;
+    struct shim_vma * new = get_new_vma(), * prev = NULL;
     if (!new)
         return NULL;
 
     lock(vma_list_lock);
 
+    if (heap_top - heap_bottom < length) {
+        unlock(vma_list_lock);
+        put_vma(new);
+        return NULL;
+    }
+
     do {
         new->addr   = heap_top - length;
         new->length = length;
         new->flags  = flags|VMA_UNMAPPED;
 
-        list_for_each_entry_reverse(tmp, &vma_list, list) {
-            if (new->addr >= tmp->addr + tmp->length)
+        list_for_each_entry_reverse(prev, &vma_list, list) {
+            if (new->addr >= prev->addr + prev->length)
                 break;
 
             if (new->addr < heap_bottom)
                 break;
 
-            if (new->addr > tmp->addr - length)
-                new->addr = tmp->addr - length;
+            if (prev->addr - heap_bottom < length) {
+                unlock(vma_list_lock);
+                put_vma(new);
+                return NULL;
+            }
+
+            if (new->addr > prev->addr - length)
+                new->addr = prev->addr - length;
+        }
+
+        if (&prev->list == &vma_list) {
+            prev = NULL;
+            break;
         }
 
         if (new->addr < heap_bottom) {
@@ -656,8 +679,10 @@ void * get_unmapped_vma (int length, int flags)
         }
     } while (!new->addr);
 
+    assert(!prev || prev->addr + prev->length <= new->addr);
     get_vma(new);
-    list_add(&new->list, tmp ? &tmp->list : &vma_list);
+    list_add(&new->list, prev ? &prev->list : &vma_list);
+    debug("get unmapped: %p-%p\n", new->addr, new->addr + new->length);
     unlock(vma_list_lock);
     return new->addr;
 }

+ 17 - 2
LibOS/shim/src/fs/dev/fs.c

@@ -110,7 +110,7 @@ static int dev_null_truncate (struct shim_handle * hdl, int size)
 
 static int dev_random_mode (const char * name, mode_t * mode)
 {
-    *mode = 0444|S_IFCHR;
+    *mode = 0666|S_IFCHR;
     return 0;
 }
 
@@ -132,7 +132,7 @@ static int dev_urandom_read (struct shim_handle * hdl, void * buf,
 
 static int dev_random_stat (const char * name, struct stat * stat)
 {
-    stat->st_mode = 0444|S_IFCHR;
+    stat->st_mode = 0666|S_IFCHR;
     stat->st_uid = 0;
     stat->st_gid = 0;
     stat->st_size = 0;
@@ -408,6 +408,20 @@ static int dev_hstat (struct shim_handle * hdl, struct stat * buf)
     return hdl->info.dev.dev_ops.hstat(hdl, buf);
 }
 
+static int dev_poll (struct shim_handle * hdl, int poll_type)
+{
+    if (poll_type == FS_POLL_SZ)
+        return 0;
+
+    int ret = 0;
+    if ((poll_type & FS_POLL_RD) && hdl->info.dev.dev_ops.read)
+        ret |= FS_POLL_RD;
+    if ((poll_type & FS_POLL_WR) && hdl->info.dev.dev_ops.write)
+        ret |= FS_POLL_WR;
+
+    return ret;
+}
+
 static int dev_follow_link (struct shim_dentry * dent, struct shim_qstr * link)
 {
     const char * name = qstrgetstr(&dent->rel_path);
@@ -433,6 +447,7 @@ struct shim_fs_ops dev_fs_ops = {
         .write          = &dev_write,
         .seek           = &dev_seek,
         .hstat          = &dev_hstat,
+        .poll           = &dev_poll,
         .truncate       = &dev_truncate,
     };
 

+ 1 - 2
LibOS/shim/src/fs/pipe/fs.c

@@ -105,7 +105,7 @@ static int pipe_checkout (struct shim_handle * hdl)
 
 static int pipe_poll (struct shim_handle * hdl, int poll_type)
 {
-    int ret = -EAGAIN;
+    int ret = 0;
 
     lock(hdl->lock);
 
@@ -126,7 +126,6 @@ static int pipe_poll (struct shim_handle * hdl, int poll_type)
     }
 
     ret = 0;
-
     if (attr.disconnected)
         ret |= FS_POLL_ER;
     if ((poll_type & FS_POLL_RD) && attr.readable)

+ 0 - 1
LibOS/shim/src/fs/shim_namei.c

@@ -414,7 +414,6 @@ static int link_path_walk (const char * name, struct lookup * look)
                     dent->state |= DENTRY_NEGATIVE;
                     err = 0;
                 } else {
-                    debug("lookup failure\n");
                     goto out;
                 }
             }

+ 1 - 3
LibOS/shim/src/fs/socket/fs.c

@@ -170,9 +170,8 @@ static int socket_checkout (struct shim_handle * hdl)
 
 static int socket_poll (struct shim_handle * hdl, int poll_type)
 {
-    int ret = -EAGAIN;
-
     struct shim_sock_handle * sock = &hdl->info.sock;
+    int ret = 0;
 
     lock(hdl->lock);
 
@@ -234,7 +233,6 @@ static int socket_poll (struct shim_handle * hdl, int poll_type)
     }
 
     ret = 0;
-
     if (attr.disconnected)
         ret |= FS_POLL_ER;
     if ((poll_type & FS_POLL_RD) && attr.readable)

+ 2 - 1
LibOS/shim/src/ipc/shim_ipc_helper.c

@@ -119,7 +119,8 @@ int init_ipc_ports (void)
     if ((ret = init_ipc_port(cur_process.self, NULL, IPC_PORT_SERVER)) < 0)
         return ret;
 
-    if ((ret = init_ipc_port(cur_process.parent, PAL_CB(parent_process),
+    if (PAL_CB(parent_process) &&
+        (ret = init_ipc_port(cur_process.parent, PAL_CB(parent_process),
                              IPC_PORT_DIRPRT|IPC_PORT_LISTEN)) < 0)
         return ret;
 

+ 1 - 1
LibOS/shim/src/shim.map

@@ -1,6 +1,6 @@
 SHIM {
     global:
         syscalldb; register_library;
-        glibc_vers_*;
+        glibc_vers_*; glibc_option;
     local: *;
 };

+ 2 - 1
LibOS/shim/src/shim_checkpoint.c

@@ -873,7 +873,8 @@ int do_migrate_process (int (*migrate) (struct shim_cp_store *,
 #endif
     BEGIN_PROFILE_INTERVAL();
 
-    PAL_HANDLE proc = DkProcessCreate(exec ? qstrgetstr(&exec->uri) : NULL,
+    PAL_HANDLE proc = DkProcessCreate(exec ? qstrgetstr(&exec->uri) :
+                                      pal_control.executable,
                                       0, argv);
 
     if (!proc) {

+ 56 - 5
LibOS/shim/src/shim_init.c

@@ -103,6 +103,58 @@ long convert_pal_errno (long err)
            pal_errno_to_unix_errno[err] : 0;
 }
 
+unsigned long parse_int (const char * str)
+{
+    unsigned long num = 0;
+    int radix = 10;
+    char c;
+
+    if (str[0] == '0') {
+        str++;
+        radix = 8;
+        if (str[0] == 'x') {
+            str++;
+            radix = 16;
+        }
+    }
+
+    while ((c = *(str++))) {
+        int val;
+        if (c >= 'A' && c <= 'F')
+            val = c - 'A' + 10;
+        else if (c >= 'a' && c <= 'f')
+            val = c - 'a' + 10;
+        else if (c >= '0' && c <= '9')
+            val = c - '0';
+        else
+            break;
+        if (val >= radix)
+            break;
+        num = num * radix + val;
+    }
+
+    if (c == 'G' || c == 'g')
+        num *= 1024 * 1024 * 1024;
+    else if (c == 'M' || c == 'm')
+        num *= 1024 * 1024;
+    else if (c == 'K' || c == 'k')
+        num *= 1024;
+
+    return num;
+}
+
+long int * glibc_option (const char * opt)
+{
+    char cfg[CONFIG_MAX];
+
+    if (!memcmp(opt, "heap_size", 9)) {
+        return get_config(root_config, "glibc.heap_size", cfg, CONFIG_MAX) < 0 ?
+            -ENOENT : parse_int(cfg);
+    }
+
+    return -EINVAL;
+}
+
 void * migrated_memory_start;
 void * migrated_memory_end;
 void * migrated_shim_addr;
@@ -209,7 +261,7 @@ void * allocate_stack (size_t size, size_t protect_size, bool user)
 
     if (user) {
         if (bkeep_mmap(stack, size, PROT_READ|PROT_WRITE,
-                       STACK_FLAGS, NULL, 0, "stack") < 0)
+                       STACK_FLAGS, NULL, 0, "[stack]") < 0)
             return NULL;
 
         if (protect_size &&
@@ -219,7 +271,6 @@ void * allocate_stack (size_t size, size_t protect_size, bool user)
     }
 
     debug("allocated stack at %p (size = %d)\n", stack, size);
-
     return stack;
 }
 
@@ -296,7 +347,7 @@ int init_stack (const char ** argv, const char ** envp, const char *** argpp,
             char stack_cfg[CONFIG_MAX];
             if (get_config(root_config, "sys.stack.size", stack_cfg,
                            CONFIG_MAX) > 0)
-                sys_stack_size = ALIGN_UP(atoi(stack_cfg));
+                sys_stack_size = ALIGN_UP(parse_int(stack_cfg));
         }
     }
 
@@ -627,6 +678,7 @@ int shim_init (int argc, void * args, void ** return_stack)
     RUN_INIT(init_randgen);
     RUN_INIT(init_heap);
     RUN_INIT(init_slab);
+    RUN_INIT(read_environs, envp);
     RUN_INIT(init_str_mgr);
     RUN_INIT(init_internal_map);
     RUN_INIT(init_vma);
@@ -678,7 +730,6 @@ restore:
     RUN_INIT(init_mount);
     RUN_INIT(init_async);
     RUN_INIT(init_stack, argv, envp, &argp, nauxv, &auxp);
-    RUN_INIT(read_environs, envp);
     RUN_INIT(init_loader);
     RUN_INIT(init_ipc_helper);
     RUN_INIT(init_signal);
@@ -947,7 +998,7 @@ void check_stack_hook (void)
             sys_printf("*** stack is almost drained (RSP = %p, stack = %p-%p) ***\n",
                        rsp, cur_thread->stack, cur_thread->stack_top);
     } else {
-        sys_printf("*** context dismateched with thread stack (RSP = %p, stack = %p-%p) ***\n",
+        sys_printf("*** context dismatched with thread stack (RSP = %p, stack = %p-%p) ***\n",
                    rsp, cur_thread->stack, cur_thread->stack_top);
     }
 }

+ 2 - 0
LibOS/shim/src/shim_malloc.c

@@ -32,6 +32,8 @@
 #include <pal.h>
 #include <pal_debug.h>
 
+#include <asm/mman.h>
+
 static LOCKTYPE slab_mgr_lock;
 
 #define system_lock()       lock(slab_mgr_lock)

+ 2 - 2
LibOS/shim/src/shim_syscalls.c

@@ -899,8 +899,8 @@ SHIM_SYSCALL_PASSTHROUGH (clock_settime, 2, int, clockid_t, which_clock,
 DEFINE_SHIM_SYSCALL (clock_gettime, 2, shim_do_clock_gettime, int,
                      clockid_t, which_clock, struct timespec *, tp)
 
-SHIM_SYSCALL_PASSTHROUGH (clock_getres, 2, int, clockid_t, which_clock,
-                          struct timespec *, tp)
+DEFINE_SHIM_SYSCALL (clock_getres, 2, shim_do_clock_getres, int,
+                     clockid_t, which_clock, struct timespec *, tp)
 
 SHIM_SYSCALL_PASSTHROUGH (clock_nanosleep, 4, int, clockid_t, which_clock,
                           int, flags, const struct timespec *, rqtp,

+ 1 - 1
LibOS/shim/src/sys/shim_brk.c

@@ -68,7 +68,7 @@ int init_brk_region (void)
         char brk_cfg[CONFIG_MAX];
         if (root_config &&
             get_config(root_config, "sys.brk.size", brk_cfg, CONFIG_MAX) > 0)
-            brk_max_size = atoi(brk_cfg);
+            brk_max_size = parse_int(brk_cfg);
         if (!brk_max_size)
             brk_max_size = DEFAULT_BRK_MAX_SIZE;
     }

+ 1 - 8
LibOS/shim/src/sys/shim_clone.c

@@ -41,14 +41,6 @@
 #include <linux/sched.h>
 #include <asm/prctl.h>
 
-struct clone_args {
-    PAL_HANDLE create_event;
-    PAL_HANDLE initialize_event;
-    struct shim_thread * thread, * parent;
-    void * stack;
-    void * return_pc;
-};
-
 /* from **sysdeps/unix/sysv/linux/x86_64/clone.S:
    The userland implementation is:
    int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
@@ -127,6 +119,7 @@ int clone_implementation_wrapper(struct clone_args * arg)
     my_thread->stack_top = vma->addr + vma->length;
     my_thread->stack_red = my_thread->stack = vma->addr;
     snprintf(vma->comment, VMA_COMMENT_LEN, "stack:%d", my_thread->tid);
+    put_vma(vma);
 
     /* Don't signal the initialize event until we are actually init-ed */ 
     DkEventSet(pcargs->initialize_event);

+ 0 - 9
LibOS/shim/src/sys/shim_futex.c

@@ -159,15 +159,6 @@ int shim_do_futex (unsigned int * uaddr, int op, int val, void * utime,
                 hdl2->type = TYPE_FUTEX;
                 futex2 = &hdl2->info.futex;
                 futex2->uaddr = uaddr2;
-#if 0
-                futex2->event = DkSynchronizationEventCreate(0);
-                if (futex2->event == NULL) {
-                    ret = -PAL_ERRNO;
-                    unlock(futex_list_lock);
-                    put_handle(hdl2);
-                    goto out;
-                }
-#endif
                 get_handle(hdl2);
                 INIT_LIST_HEAD(&futex2->waiters);
                 INIT_LIST_HEAD(&futex2->list);

+ 26 - 9
LibOS/shim/src/sys/shim_mmap.c

@@ -43,12 +43,17 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
 {
     struct shim_handle * hdl = NULL;
     long ret = -ENOMEM;
+    bool reserved = false;
 
     assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
 
     int pal_alloc_type = ((flags & MAP_32BIT) ? PAL_ALLOC_32BIT : 0);
 
-    addr = addr ? : get_unmapped_vma(ALIGN_UP(length), flags);
+    if (!addr) {
+        addr = get_unmapped_vma(ALIGN_UP(length), flags);
+        if (addr)
+            reserved = true;
+    }
 
     void * mapped = ALIGN_DOWN((void *) addr);
     void * mapped_end = ALIGN_UP((void *) addr + length);
@@ -60,27 +65,34 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
         addr = (void *) DkVirtualMemoryAlloc(addr, length, pal_alloc_type,
                                              PAL_PROT(prot, 0));
 
-        if (!addr)
-            return (void *) -PAL_ERRNO;
+        if (!addr) {
+            ret = (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED) ? -EPERM : -PAL_ERRNO;
+            goto free_reserved;
+        }
 
         ADD_PROFILE_OCCURENCE(mmap, length);
     } else {
-        if (fd < 0)
-            return (void *) -EINVAL;
+        if (fd < 0) {
+            ret = -EINVAL;
+            goto free_reserved;
+        }
 
         hdl = get_fd_handle(fd, NULL, NULL);
-        if (!hdl)
-            return (void *) -EBADF;
+        if (!hdl) {
+            ret = -EBADF;
+            goto free_reserved;
+        }
 
         if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
             put_handle(hdl);
-            return (void *) -ENODEV;
+            ret = -ENODEV;
+            goto free_reserved;
         }
 
         if ((ret = hdl->fs->fs_ops->mmap(hdl, &addr, length, prot,
                                          flags, offset)) < 0) {
             put_handle(hdl);
-            return (void *) ret;
+            goto free_reserved;
         }
     }
 
@@ -95,6 +107,11 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
     if (hdl)
         put_handle(hdl);
     return addr;
+
+free_reserved:
+    if (reserved)
+        bkeep_munmap((void *) mapped, mapped_end - mapped, &flags);
+    return (void *) ret;
 }
 
 int shim_do_mprotect (void * addr, size_t len, int prot)

+ 33 - 23
LibOS/shim/src/sys/shim_poll.c

@@ -214,23 +214,23 @@ no_op:
 
             if (need_poll) {
                 int polled = hdl->fs->fs_ops->poll(hdl, need_poll);
-                if (polled != -EAGAIN) {
-                    if (polled & FS_POLL_ER) {
-                        debug("fd %d known to have error\n", p->fd);
-                        p->flags |= KNOWN_R|KNOWN_W|RET_E;
-                    }
-
-                    if (do_r && (polled & FS_POLL_RD)) {
-                        debug("fd %d known to be readable\n", p->fd);
-                        p->flags |= KNOWN_R|RET_R;
-                        do_r = false;
-                    }
-
-                    if (do_w && (polled & FS_POLL_WR)) {
-                        debug("fd %d known to be writeable\n", p->fd);
-                        p->flags |= KNOWN_W|RET_W;
-                        do_w = false;
-                    }
+
+                if (polled & FS_POLL_ER) {
+                    debug("fd %d known to have error\n", p->fd);
+                    p->flags |= KNOWN_R|KNOWN_W|RET_E;
+                    do_r = do_w = false;
+                }
+
+                if ((polled & FS_POLL_RD)) {
+                    debug("fd %d known to be readable\n", p->fd);
+                    p->flags |= KNOWN_R|RET_R;
+                    do_r = false;
+                }
+
+                if (polled & FS_POLL_WR) {
+                    debug("fd %d known to be writeable\n", p->fd);
+                    p->flags |= KNOWN_W|RET_W;
+                    do_w = false;
                 }
             }
 
@@ -244,7 +244,7 @@ no_op:
 
         if (!(to_poll->flags & (POLL_R|POLL_W))) {
             if (!hdl->pal_handle) {
-                p->flags |= (KNOWN_R|KNOWN_W|RET_E);
+                p->flags |= KNOWN_R|KNOWN_W|RET_E;
                 do_r = do_w = false;
                 goto done_finding;
             }
@@ -280,7 +280,10 @@ done_finding:
     pals = __try_alloca(cur, sizeof(PAL_HANDLE) * npals);
     npals = 0;
 
-    for (n = &polling, p = polling ; p ; n = &p->next, p = p->next) {
+    n = &polling;
+    for (p = polling ; p ; p = p->next) {
+        assert(p->handle);
+
         if (!(p->flags & (POLL_R|POLL_W))) {
             *n = p->next;
             put_handle(p->handle);
@@ -289,6 +292,7 @@ done_finding:
         }
 
         pals[npals++] = p->handle->pal_handle;
+        n = &p->next;
     }
 
     SAVE_PROFILE_INTERVAL(do_poll_second_loop);
@@ -309,26 +313,31 @@ done_finding:
         if (!DkStreamAttributesQuerybyHandle(polled, &attr))
             break;
 
-        for (n = &polling, p = polling ; p ; n = &p->next, p = p->next)
+        n = &polling;
+        for (p = polling ; p ; p = p->next) {
             if (p->handle->pal_handle == polled)
                 break;
+            n = &p->next;
+        }
 
         if (!p)
             break;
 
         debug("handle %s is polled\n", qstrgetstr(&p->handle->uri));
 
+        p->flags |= KNOWN_R|KNOWN_W;
+
         if (attr.disconnected) {
             debug("handle is polled to be disconnected\n");
-            p->flags |= (KNOWN_R|KNOWN_W|RET_E);
+            p->flags |= RET_E;
         }
         if (attr.readable) {
             debug("handle is polled to be readable\n");
-            p->flags |= (KNOWN_R|RET_R);
+            p->flags |= RET_R;
         }
         if (attr.writeable) {
             debug("handle is polled to be writeable\n");
-            p->flags |= (KNOWN_W|RET_W);
+            p->flags |= RET_W;
         }
 
         for (q = p->children ; q ; q = q->next)
@@ -524,6 +533,7 @@ int shim_do_select (int nfds, fd_set * readfds, fd_set * writefds,
         bool do_w = (writefds && __FD_ISSET(fd, writefds));
         if (!do_r && !do_w)
             continue;
+        debug("poll fd %d %s%s\n", fd, do_r ? "R" : "", do_w ? "W" : "");
         polls[npolls].fd = fd;
         polls[npolls].flags = (do_r ? DO_R : 0)|(do_w ? DO_W : 0);
         npolls++;

+ 13 - 0
LibOS/shim/src/sys/shim_time.c

@@ -81,3 +81,16 @@ int shim_do_clock_gettime (clockid_t which_clock,
     tp->tv_nsec = (time % 1000000) * 1000;
     return 0;
 }
+
+int shim_do_clock_getres (clockid_t which_clock,
+                          struct timespec * tp)
+{
+    /* all clock are the same */
+
+    if (!tp)
+        return -EINVAL;
+
+    tp->tv_sec  = 0;
+    tp->tv_nsec = 1000;
+    return 0;
+}

+ 3 - 6
LibOS/shim/test/apps/openjdk/java.manifest.template

@@ -26,9 +26,6 @@ fs.mount.other.usr.type = chroot
 fs.mount.other.usr.path = /usr
 fs.mount.other.usr.uri = file:/usr
 
-fs.mount.other.nsswitch.type = chroot
-fs.mount.other.nsswitch.path = /etc/nsswitch.conf
-fs.mount.other.nsswitch.uri = file:/etc/nsswitch.conf
-
-sys.brk.size = 16777216  # 16M
-sys.stack.size = 262144  # 256K
+sys.stack.size = 256K
+sys.brk.size = 16M
+glibc.heap_size = 16M

+ 1 - 1
LibOS/shim/test/native/.packed/test.sha384

@@ -1 +1 @@
-alarm brk clone condvar.pthread cpuinfo dir divzero dup epoll epoll_socket errno exec exec_fork exec_victim file fork fork_bomb fork_exec fs futextest.pthread get_time.m helloworld helloworld.pthread kill meminfo msg_create msg_create.libos msg_send msg_send.libos multiproc multisleep pid_alloc pid_kill pie pipe pipe_latency proc readdir rename sandbox_create.libos script sem sleep socketpair sqrt.m static sync.pthread system tcp time udp unix vfork vfork_exec 4a227e88da4be6c68bf9653cd7ac662862f2c9734a19224ed982e161d6f46a69d512d9436312dd09e2b4a85a345d3ab6
+alarm brk clone condvar.pthread cpuinfo dir divzero dup epoll epoll_socket errno exec exec_fork exec_victim file fork fork_bomb fork_exec fs futextest.pthread get_time.m helloworld helloworld.pthread kill malloc meminfo msg_create msg_create.libos msg_send msg_send.libos multiproc multisleep pid_alloc pid_kill pie pipe pipe_latency proc readdir rename sandbox_create.libos script sem sleep socketpair sqrt.m static sync.pthread system tcp time udp unix vfork vfork_exec 9a626f937e9e046299e96c5c730fea876c426178db3fdbd4028aef89846fcdea1628dd074cd89b3b651e2cbe77517ad0

BIN
LibOS/shim/test/native/.packed/test.tar.gz


+ 18 - 0
LibOS/shim/test/native/malloc.c

@@ -0,0 +1,18 @@
+/* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
+/* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+int main(int argc, char ** argv)
+{
+    for (int i = 0 ; i < 100000 ; i++) {
+        malloc(16);
+        malloc(32);
+        malloc(64);
+        malloc(128);
+        malloc(256);
+        malloc(512);
+    }
+    return 0;
+}

+ 1 - 1
Pal/src/Makefile.Host

@@ -15,4 +15,4 @@ $(error Unsupported platform: $(OS))
 endif
 endif
 
-OS_MACRO := $(shell echo $(OS) | tr '[:lower:]' '[:upper:]')
+OS_MACRO := $(shell echo $(OS) | tr '[:lower:]' '[:upper:]' | tr '-' '_')

+ 49 - 3
Pal/src/db_rtld.c

@@ -519,6 +519,51 @@ int load_elf_object (const char * uri, enum object_type type)
     return ret;
 }
 
+int add_elf_object(void * addr, PAL_HANDLE handle, int type)
+{
+    struct link_map * map = new_elf_object(_DkStreamRealpath(handle), type);
+    const ElfW(Ehdr) * header = (void *) addr;
+    int ret;
+
+    map->l_entry = header->e_entry;
+    map->l_phdr  = (void *) header->e_phoff;
+    map->l_phnum = header->e_phnum;
+
+    const ElfW(Phdr) * ph, * phdr;
+    phdr = __alloca(header->e_phnum * sizeof(ElfW(Phdr)));
+
+    if ((ret = _DkStreamRead(handle, header->e_phoff,
+                             header->e_phnum * sizeof(ElfW(Phdr)),
+                             (void *) phdr, NULL, 0)) < 0)
+        return ret;
+
+    for (ph = phdr; ph < &phdr[map->l_phnum]; ++ph)
+        switch (ph->p_type) {
+            case PT_DYNAMIC:
+                map->l_ld = (void *) ph->p_vaddr;
+                map->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
+                break;
+        }
+
+    map->l_real_ld = map->l_ld;
+    map->l_ld = remalloc(map->l_ld, sizeof(ElfW(Dyn)) * map->l_ldnum);
+
+    elf_get_dynamic_info(map->l_ld, map->l_info, 0);
+
+    setup_elf_hash(map);
+
+    //ELF_DYNAMIC_SCAN(map->l_info, 0);
+
+    struct link_map * prev = loaded_maps;
+    while (prev->l_next)
+        prev = prev->l_next;
+    map->l_prev = prev;
+    map->l_next = NULL;
+    prev->l_next = map;
+
+    return 0;
+}
+
 static int relocate_elf_object (struct link_map *l);
 
 #if CACHE_LOADED_BINARIES == 1
@@ -528,7 +573,7 @@ struct cached_elf_object {
     PAL_NUM             pal_token;
     void *              pal_addr;
     struct link_map     map;
-    char                map_name[PATH_MAX];
+    char                map_name[80];
 
     struct cached_loadcmd {
         void * mapstart;
@@ -604,7 +649,7 @@ void cache_elf_object (PAL_HANDLE handle, struct link_map * map)
                     (void *) obj->map.l_info[i] - (unsigned long) map->l_ld;
     }
     obj->map.l_name = NULL;
-    memcpy(obj->map_name, map->l_name, PATH_MAX);
+    memcpy(obj->map_name, map->l_name, sizeof(obj->map_name));
     obj->nloadcmds  = 0;
 
     const ElfW(Ehdr) * header = (void *) map->l_map_start;
@@ -1253,7 +1298,8 @@ void * stack_before_call __attribute__((unused)) = NULL;
 
 void start_execution (const char * first_argv, int argc, const char ** argv,
                       const char ** envp)
-{    /* First we will try to run all the preloaded libraries which come with
+{
+    /* First we will try to run all the preloaded libraries which come with
        entry points */
     if (exec_map) {
         __pal_control.executable_range.start = (void *) exec_map->l_map_start;

+ 0 - 11
Pal/src/dl-machine-x86_64.h

@@ -30,7 +30,6 @@
 
 #define ELF_MACHINE_NAME "x86_64"
 
-#include <sys/param.h>
 #include <sysdeps/generic/ldsodefs.h>
 #include "pal_internal.h"
 
@@ -143,16 +142,6 @@ elf_machine_rela (struct link_map *l, Elf64_Rela *reloc, Elf64_Sym *sym,
             *(Elf64_Addr *) reloc_addr = value;
             break;
 
-#if 0
-        case R_X86_64_COPY:
-            debug_reloc(R_X86_64_COPY);
-            int sym_size = sym ? sym->st_size : 0;
-            int ref_size = refsym ? refsym->st_size : 0;
-            memcpy (reloc_addr_arg, (void *) value, MIN (sym_size,
-                    ref_size));
-            break;
-#endif
-
         case R_X86_64_IRELATIVE:
             debug_reloc(R_X86_64_IRELATIVE);
             value = sym_map + reloc->r_addend;

+ 2 - 0
Pal/src/do-rel.h

@@ -41,6 +41,8 @@
                               (void *) (l->l_addr + relative->r_offset))
 #endif
 
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+
 static void __attribute__((unused))
 elf_dynamic_do_rel (struct link_map *l, ElfW(Addr) reladdr, int relsize)
 {

+ 1 - 2
Pal/src/host/Linux/clone-x86_64.S

@@ -111,8 +111,7 @@ L(thread_start):
 	/* movl $SYS_ify(exit), %eax */
 	/* syscall */
 	/* Instead of syscall exit, let's call _DkThreadExit */
-	movq _DkThreadExit@GOTPCREL(%rip), %rax
-	call *%rax
+	call _DkThreadExit(%rip)
 	cfi_endproc
 
 	cfi_startproc

+ 1 - 1
Pal/src/host/Linux/db_memory.c

@@ -37,7 +37,7 @@
 
 bool _DkCheckMemoryMappable (const void * addr, int size)
 {
-    return (addr <= DATA_END && addr + size >= TEXT_START);
+    return (addr < DATA_END && addr + size > TEXT_START);
 }
 
 int _DkVirtualMemoryAlloc (void ** paddr, int size, int alloc_type,

+ 1 - 3
Pal/src/host/Linux/db_sockets.c

@@ -1225,9 +1225,7 @@ PAL_HANDLE _DkBroadcastStreamOpen (void)
     if (!pal_sec.mcast_port) {
         unsigned short mcast_port;
         _DkFastRandomBitsRead(&mcast_port, sizeof(unsigned short));
-        if (mcast_port < 1024)
-            mcast_port += 1024;
-        pal_sec.mcast_port = mcast_port > 1024 ? mcast_port : mcast_port + 1204;
+        pal_sec.mcast_port = mcast_port > 1024 ? mcast_port : mcast_port + 1024;
     }
 
     struct sockaddr_in addr;

+ 2 - 0
Pal/src/pal.h

@@ -483,4 +483,6 @@ DkPhysicalMemoryMap (PAL_HANDLE channel, PAL_NUM entries, PAL_PTR * addrs,
 
 PAL_NUM DkMemoryAvailableQuota (void);
 
+PAL_BOL
+DkCpuIdRetrieve (PAL_NUM level, PAL_NUM values[4]);
 #endif /* PAL_H */

+ 2 - 2
Pal/src/pal_defs.h

@@ -12,9 +12,9 @@
 #endif
 
 /* statically allocate slab manager */
-#define STATIC_SLAB             1
+#define STATIC_SLAB              1
 
 /* maximum length of URIs */
-#define URI_MAX                 256
+#define URI_MAX                  256
 
 #endif /* PAL_DEFS_H */

+ 1 - 0
Pal/src/pal_internal.h

@@ -461,6 +461,7 @@ int _DkPhysicalMemoryCommit (PAL_HANDLE channel, int entries,
                              PAL_PTR * addrs, PAL_NUM * sizes, int flags);
 int _DkPhysicalMemoryMap (PAL_HANDLE channel, int entries,
                           PAL_PTR * addrs, PAL_NUM * sizes, PAL_FLG * prots);
+int _DkCpuIdRetrieve (unsigned int leaf, unsigned int values[4]);
 unsigned long _DkHandleCompatibilityException (unsigned long syscallno,
                                                unsigned long args[6]);
 

+ 3 - 1
Pal/src/slab.c

@@ -37,11 +37,13 @@ static PAL_LOCK slab_mgr_lock = LOCK_INIT;
 #define system_unlock() _DkInternalUnlock(&slab_mgr_lock)
 
 #if STATIC_SLAB == 1
-# define POOL_SIZE 64 * 1024 /* 4MB by default */
+# warning "Using static slab"
+# define POOL_SIZE 4 * 1024 * 1024 /* 4MB by default */
 static char mem_pool[POOL_SIZE];
 static char *bump = mem_pool;
 static char *mem_pool_end = &mem_pool[POOL_SIZE];
 #else
+# warning "Using dynamic slab"
 # define PAGE_SIZE (slab_alignment)
 #endif
 

Some files were not shown because too many files changed in this diff