Browse Source

Reformat repository to our clang-format rules

Michał Kowalczyk 4 years ago
parent
commit
de42ebabe1
100 changed files with 6699 additions and 7681 deletions
  1. 1 0
      .clang-format
  2. 1 1
      LibOS/shim/include/glibc-version.h
  3. 184 196
      LibOS/shim/include/shim_handle.h
  4. 106 124
      LibOS/shim/include/shim_ipc.h
  5. 83 92
      LibOS/shim/include/shim_ipc_ns.h
  6. 163 124
      LibOS/shim/include/shim_profile.h
  7. 0 1
      LibOS/shim/include/shim_signal.h
  8. 103 111
      LibOS/shim/include/shim_sysv.h
  9. 783 848
      LibOS/shim/include/shim_table.h
  10. 0 1
      LibOS/shim/include/shim_thread.h
  11. 18 19
      LibOS/shim/include/shim_unistd.h
  12. 2 2
      LibOS/shim/include/shim_unistd_defs.h
  13. 66 87
      LibOS/shim/include/shim_utils.h
  14. 48 60
      LibOS/shim/include/shim_vma.h
  15. 157 227
      LibOS/shim/src/bookkeep/shim_handle.c
  16. 31 37
      LibOS/shim/src/elf/dl-machine-x86_64.h
  17. 27 34
      LibOS/shim/src/elf/do-rel.h
  18. 10 12
      LibOS/shim/src/elf/ldsodefs.h
  19. 105 116
      LibOS/shim/src/elf/rel.h
  20. 239 282
      LibOS/shim/src/elf/shim_rtld.c
  21. 142 176
      LibOS/shim/src/fs/dev/fs.c
  22. 53 62
      LibOS/shim/src/fs/pipe/fs.c
  23. 140 142
      LibOS/shim/src/fs/proc/fs.c
  24. 103 77
      LibOS/shim/src/fs/proc/info.c
  25. 98 95
      LibOS/shim/src/fs/proc/ipc-thread.c
  26. 214 217
      LibOS/shim/src/fs/proc/thread.c
  27. 57 75
      LibOS/shim/src/fs/socket/fs.c
  28. 46 58
      LibOS/shim/src/fs/str/fs.c
  29. 69 69
      LibOS/shim/src/ipc/shim_ipc.c
  30. 47 47
      LibOS/shim/src/ipc/shim_ipc_child.c
  31. 197 241
      LibOS/shim/src/ipc/shim_ipc_nsimpl.h
  32. 226 306
      LibOS/shim/src/ipc/shim_ipc_sysv.c
  33. 39 49
      LibOS/shim/src/shim_debug.c
  34. 89 98
      LibOS/shim/src/shim_malloc.c
  35. 4 6
      LibOS/shim/src/shim_object.c
  36. 431 432
      LibOS/shim/src/shim_parser.c
  37. 321 429
      LibOS/shim/src/shim_syscalls.c
  38. 320 320
      LibOS/shim/src/shim_table.c
  39. 31 37
      LibOS/shim/src/sys/shim_alarm.c
  40. 9 14
      LibOS/shim/src/sys/shim_benchmark.c
  41. 17 22
      LibOS/shim/src/sys/shim_dup.c
  42. 115 136
      LibOS/shim/src/sys/shim_epoll.c
  43. 15 24
      LibOS/shim/src/sys/shim_fcntl.c
  44. 138 118
      LibOS/shim/src/sys/shim_futex.c
  45. 17 22
      LibOS/shim/src/sys/shim_getcwd.c
  46. 48 66
      LibOS/shim/src/sys/shim_getpid.c
  47. 21 26
      LibOS/shim/src/sys/shim_getrlimit.c
  48. 111 123
      LibOS/shim/src/sys/shim_ioctl.c
  49. 61 73
      LibOS/shim/src/sys/shim_migrate.c
  50. 42 51
      LibOS/shim/src/sys/shim_mmap.c
  51. 206 266
      LibOS/shim/src/sys/shim_msgget.c
  52. 54 64
      LibOS/shim/src/sys/shim_pipe.c
  53. 9 13
      LibOS/shim/src/sys/shim_sched.c
  54. 200 249
      LibOS/shim/src/sys/shim_semget.c
  55. 205 277
      LibOS/shim/src/sys/shim_socket.c
  56. 9 18
      LibOS/shim/src/sys/shim_time.c
  57. 10 14
      LibOS/shim/src/sys/shim_uname.c
  58. 16 21
      LibOS/shim/src/sys/shim_vfork.c
  59. 20 25
      LibOS/shim/src/sys/shim_wait.c
  60. 20 28
      LibOS/shim/src/sys/shim_wrappers.c
  61. 119 126
      LibOS/shim/src/utils/md5.c
  62. 32 52
      LibOS/shim/src/utils/printf.c
  63. 8 12
      LibOS/shim/src/utils/strobjs.c
  64. 10 15
      LibOS/shim/src/vdso/vdso.c
  65. 20 23
      LibOS/shim/test/benchmark/fork_latency.c
  66. 14 19
      LibOS/shim/test/benchmark/rpc_latency.libos.c
  67. 18 22
      LibOS/shim/test/benchmark/rpc_latency2.libos.c
  68. 31 35
      LibOS/shim/test/benchmark/sig_latency.c
  69. 7 7
      LibOS/shim/test/benchmark/start.c
  70. 25 26
      LibOS/shim/test/benchmark/test_start.m.c
  71. 4 6
      LibOS/shim/test/inline/fork.c
  72. 2 3
      LibOS/shim/test/inline/helloworld.c
  73. 4 6
      LibOS/shim/test/inline/vfork.c
  74. 5 7
      LibOS/shim/test/native/alarm.c
  75. 2 3
      LibOS/shim/test/native/brk.c
  76. 17 21
      LibOS/shim/test/native/clone.c
  77. 36 39
      LibOS/shim/test/native/condvar.pthread.c
  78. 7 7
      LibOS/shim/test/native/cpuinfo.c
  79. 5 6
      LibOS/shim/test/native/dir.c
  80. 5 7
      LibOS/shim/test/native/divzero.c
  81. 2 3
      LibOS/shim/test/native/dup.c
  82. 12 12
      LibOS/shim/test/native/epoll.c
  83. 39 49
      LibOS/shim/test/native/epoll_socket.c
  84. 2 2
      LibOS/shim/test/native/errno.c
  85. 2 2
      LibOS/shim/test/native/exec_fork.c
  86. 3 4
      LibOS/shim/test/native/file.c
  87. 3 4
      LibOS/shim/test/native/fork.c
  88. 21 25
      LibOS/shim/test/native/fork_bomb.c
  89. 5 6
      LibOS/shim/test/native/fork_exec.c
  90. 3 4
      LibOS/shim/test/native/fs.c
  91. 10 14
      LibOS/shim/test/native/futextest.pthread.c
  92. 25 26
      LibOS/shim/test/native/get_time.m.c
  93. 1 2
      LibOS/shim/test/native/helloworld.c
  94. 4 6
      LibOS/shim/test/native/helloworld.pthread.c
  95. 7 9
      LibOS/shim/test/native/kill.c
  96. 3 4
      LibOS/shim/test/native/malloc.c
  97. 7 7
      LibOS/shim/test/native/meminfo.c
  98. 31 37
      LibOS/shim/test/native/msg_create.c
  99. 34 39
      LibOS/shim/test/native/msg_create.libos.c
  100. 17 24
      LibOS/shim/test/native/msg_send.c

+ 1 - 0
.clang-format

@@ -1,5 +1,6 @@
 BasedOnStyle: Google
 AlignConsecutiveAssignments: true
+AlignConsecutiveMacros: true
 AllowShortFunctionsOnASingleLine: Empty
 AllowShortIfStatementsOnASingleLine: false
 ColumnLimit:     100

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

@@ -3,4 +3,4 @@
 
 #define GLIBC_VERSION 20170114
 
-int register_library (const char * name, unsigned long load_address);
+int register_library(const char* name, unsigned long load_address);

+ 184 - 196
LibOS/shim/include/shim_handle.h

@@ -23,22 +23,19 @@
 #ifndef _SHIM_HANDLE_H_
 #define _SHIM_HANDLE_H_
 
-#include <stdalign.h>
-
 #include <asm/fcntl.h>
 #include <asm/resource.h>
-#include <linux/shm.h>
+#include <atomic.h>  // TODO: migrate to stdatomic.h
 #include <linux/in.h>
 #include <linux/in6.h>
+#include <linux/shm.h>
 #include <linux/un.h>
-
-#include <shim_types.h>
+#include <list.h>
+#include <pal.h>
 #include <shim_defs.h>
 #include <shim_sysv.h>
-
-#include <atomic.h> // TODO: migrate to stdatomic.h
-#include <pal.h>
-#include <list.h>
+#include <shim_types.h>
+#include <stdalign.h>
 
 /* start definition of shim handle */
 enum shim_handle_type {
@@ -68,93 +65,93 @@ enum shim_file_type {
 };
 
 struct shim_file_data {
-    struct shim_lock    lock;
-    struct atomic_int   version;
-    bool                queried;
+    struct shim_lock lock;
+    struct atomic_int version;
+    bool queried;
     enum shim_file_type type;
-    mode_t     mode;
-    struct atomic_int   size;
-    struct shim_qstr    host_uri;
-    unsigned long       atime;
-    unsigned long       mtime;
-    unsigned long       ctime;
-    unsigned long       nlink;
+    mode_t mode;
+    struct atomic_int size;
+    struct shim_qstr host_uri;
+    unsigned long atime;
+    unsigned long mtime;
+    unsigned long ctime;
+    unsigned long nlink;
 };
 
 struct shim_file_handle {
-    unsigned int        version;
-    struct shim_file_data * data;
+    unsigned int version;
+    struct shim_file_data* data;
 
     enum shim_file_type type;
-    off_t               size;
-    off_t               marker;
+    off_t size;
+    off_t marker;
 
     enum { FILEBUF_MAP, FILEBUF_NONE } buf_type;
-    size_t              mapsize;
-    off_t               mapoffset;
-    void *              mapbuf;
+    size_t mapsize;
+    off_t mapoffset;
+    void* mapbuf;
 };
 
-#define FILE_HANDLE_DATA(hdl)   ((hdl)->info.file.data)
-#define FILE_DENTRY_DATA(dent)  ((struct shim_file_data*)(dent)->data)
+#define FILE_HANDLE_DATA(hdl)  ((hdl)->info.file.data)
+#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);
+    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);
+    int (*close)(struct shim_handle* hdl);
 
     /* read: the content from the file opened as handle */
-    ssize_t (*read) (struct shim_handle * hdl, void * buf, size_t count);
+    ssize_t (*read)(struct shim_handle* hdl, void* buf, size_t count);
 
     /* write: the content from the file opened as handle */
-    ssize_t (*write) (struct shim_handle * hdl, const void * buf, size_t count);
+    ssize_t (*write)(struct shim_handle* hdl, const void* buf, size_t count);
 
     /* flush: flush out user buffer */
-    int (*flush) (struct shim_handle * hdl);
+    int (*flush)(struct shim_handle* hdl);
 
     /* seek: the content from the file opened as handle */
-    off_t (*seek) (struct shim_handle * hdl, off_t offset, int wence);
+    off_t (*seek)(struct shim_handle* hdl, off_t offset, int wence);
 
-    int (*truncate) (struct shim_handle * hdl, uint64_t len);
+    int (*truncate)(struct shim_handle* hdl, uint64_t len);
 
-    int (*mode) (const char * name, mode_t * mode);
+    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);
+    int (*stat)(const char* name, struct stat* buf);
+    int (*hstat)(struct shim_handle* hdl, struct stat* buf);
 };
 
 struct shim_dev_handle {
-    struct shim_dev_ops     dev_ops;
+    struct shim_dev_ops dev_ops;
 };
 
 struct shim_pipe_handle {
 #if USE_SIMPLE_PIPE == 1
-    struct shim_handle *    pair;
+    struct shim_handle* pair;
 #else
-    IDTYPE                  pipeid;
+    IDTYPE pipeid;
 #endif
 };
 
-#define SOCK_STREAM     1
-#define SOCK_DGRAM      2
-#define SOCK_NONBLOCK   04000
-#define SOCK_CLOEXEC    02000000
+#define SOCK_STREAM   1
+#define SOCK_DGRAM    2
+#define SOCK_NONBLOCK 04000
+#define SOCK_CLOEXEC  02000000
 
-#define SOL_TCP         6
+#define SOL_TCP 6
 
-#define PF_LOCAL        1
-#define PF_UNIX         PF_LOCAL
-#define PF_FILE         PF_LOCAL
-#define PF_INET         2
-#define PF_INET6        10
+#define PF_LOCAL 1
+#define PF_UNIX  PF_LOCAL
+#define PF_FILE  PF_LOCAL
+#define PF_INET  2
+#define PF_INET6 10
 
-#define AF_UNIX         PF_UNIX
-#define AF_INET         PF_INET
-#define AF_INET6        PF_INET6
+#define AF_UNIX  PF_UNIX
+#define AF_INET  PF_INET
+#define AF_INET6 PF_INET6
 
 enum shim_sock_state {
     SOCK_CREATED,
@@ -171,10 +168,10 @@ struct shim_unix_data {
 };
 
 struct shim_sock_handle {
-    int     domain;
-    int     sock_type;
-    int     protocol;
-    int     error;
+    int domain;
+    int sock_type;
+    int protocol;
+    int error;
 
     enum shim_sock_state sock_state;
 
@@ -182,87 +179,85 @@ struct shim_sock_handle {
         // INET addr
         struct {
             struct addr_inet {
-                unsigned short      port;
-                unsigned short      ext_port;
+                unsigned short port;
+                unsigned short ext_port;
                 union {
-                    struct in_addr  v4;
+                    struct in_addr v4;
                     struct in6_addr v6;
                 } addr;
             } bind, conn;
         } in;
         // UNIX addr
         struct addr_unix {
-            struct shim_dentry * dentry;
-            unsigned int         pipeid;
-            struct shim_unix_data * data;
+            struct shim_dentry* dentry;
+            unsigned int pipeid;
+            struct shim_unix_data* data;
         } un;
     } addr;
 
     struct shim_sock_option {
-        struct shim_sock_option * next;
-        int     level;
-        int     optname;
-        int     optlen;
-        char    optval[];
+        struct shim_sock_option* next;
+        int level;
+        int optname;
+        int optlen;
+        char optval[];
     } * pending_options;
 };
 
 struct shim_dirent {
-    struct shim_dirent * next;
-    unsigned long        ino;          /* Inode number */
-    unsigned char        type;
-    char                 name[];       /* File name (null-terminated) */
+    struct shim_dirent* next;
+    unsigned long ino; /* Inode number */
+    unsigned char type;
+    char name[]; /* File name (null-terminated) */
 };
 
-#define SHIM_DIRENT_SIZE offsetof(struct shim_dirent, name)
+#define SHIM_DIRENT_SIZE      offsetof(struct shim_dirent, name)
 #define SHIM_DIRENT_ALIGNMENT alignof(struct shim_dirent)
 /* Size of struct shim_dirent instance together with alignment,
  * which might be different depending on the length of the name field */
-#define SHIM_DIRENT_ALIGNED_SIZE(len) ( \
-                (SHIM_DIRENT_SIZE \
-                    + (len) \
-                    + SHIM_DIRENT_ALIGNMENT - 1) \
-            / SHIM_DIRENT_ALIGNMENT * SHIM_DIRENT_ALIGNMENT)
+#define SHIM_DIRENT_ALIGNED_SIZE(len)                                                 \
+    ((SHIM_DIRENT_SIZE + (len) + SHIM_DIRENT_ALIGNMENT - 1) / SHIM_DIRENT_ALIGNMENT * \
+     SHIM_DIRENT_ALIGNMENT)
 
 struct shim_dir_handle {
     int offset;
-    struct shim_dentry * dotdot;
-    struct shim_dentry * dot;
-    struct shim_dentry ** buf;
-    struct shim_dentry ** ptr;
+    struct shim_dentry* dotdot;
+    struct shim_dentry* dot;
+    struct shim_dentry** buf;
+    struct shim_dentry** ptr;
 };
 
 struct shim_shm_handle {
     /* XXX: need to implement */
-    void * __reserved;
+    void* __reserved;
 };
 
 struct msg_type;
 struct msg_item;
 struct msg_client;
 
-#define MAX_SYSV_CLIENTS        32
+#define MAX_SYSV_CLIENTS 32
 
 DEFINE_LIST(shim_msg_handle);
 struct shim_msg_handle {
-    unsigned long       msqkey;         /* msg queue key from user */
-    IDTYPE              msqid;          /* msg queue identifier */
-    bool                owned;          /* owned by current process */
-    struct shim_ipc_info * owner;
-    LEASETYPE           lease;
-    int                 perm;           /* access permissions */
-    bool                deleted;        /* marking the queue deleted */
-    int                 nmsgs;          /* number of msgs */
-    int                 currentsize;    /* current size in bytes */
-    struct msg_qobj *   queue;
-    int                 queuesize;
-    int                 queueused;
-    struct msg_qobj *   freed;
-    PAL_HANDLE          event;          /* event for waiting */
-    int                 ntypes;
-    int                 maxtypes;
-    struct msg_type *   types;
-    struct sysv_score   scores[MAX_SYSV_CLIENTS];
+    unsigned long msqkey; /* msg queue key from user */
+    IDTYPE msqid;         /* msg queue identifier */
+    bool owned;           /* owned by current process */
+    struct shim_ipc_info* owner;
+    LEASETYPE lease;
+    int perm;        /* access permissions */
+    bool deleted;    /* marking the queue deleted */
+    int nmsgs;       /* number of msgs */
+    int currentsize; /* current size in bytes */
+    struct msg_qobj* queue;
+    int queuesize;
+    int queueused;
+    struct msg_qobj* freed;
+    PAL_HANDLE event; /* event for waiting */
+    int ntypes;
+    int maxtypes;
+    struct msg_type* types;
+    struct sysv_score scores[MAX_SYSV_CLIENTS];
     LIST_TYPE(shim_msg_handle) list;
     LIST_TYPE(shim_msg_handle) key_hlist;
     LIST_TYPE(shim_msg_handle) qid_hlist;
@@ -272,18 +267,18 @@ struct sem_objs;
 
 DEFINE_LIST(shim_sem_handle);
 struct shim_sem_handle {
-    unsigned long       semkey;
-    IDTYPE              semid;
-    bool                owned;
-    struct shim_ipc_info * owner;
-    LEASETYPE           lease;
-    int                 perm;
-    bool                deleted;
-    PAL_HANDLE          event;
-    int                 nsems;
-    struct sem_obj *    sems;
-    int                 nreqs;
-    struct sysv_score   scores[MAX_SYSV_CLIENTS];
+    unsigned long semkey;
+    IDTYPE semid;
+    bool owned;
+    struct shim_ipc_info* owner;
+    LEASETYPE lease;
+    int perm;
+    bool deleted;
+    PAL_HANDLE event;
+    int nsems;
+    struct sem_obj* sems;
+    int nreqs;
+    struct sysv_score scores[MAX_SYSV_CLIENTS];
     LISTP_TYPE(sem_ops) migrated;
     LIST_TYPE(shim_sem_handle) list;
     LIST_TYPE(shim_sem_handle) key_hlist;
@@ -293,42 +288,42 @@ DEFINE_LIST(futex_waiter);
 DEFINE_LISTP(futex_waiter);
 DEFINE_LIST(shim_futex_handle);
 struct shim_futex_handle {
-    int *   uaddr;
+    int* uaddr;
     LISTP_TYPE(futex_waiter) waiters;
-    struct shim_vma *   vma;
+    struct shim_vma* vma;
     LIST_TYPE(shim_futex_handle) list;
 };
 
 struct shim_str_data {
     REFTYPE ref_count;
-    char * str;
+    char* str;
     off_t len;
     size_t buf_size;
     bool dirty;
-    int (*update) (struct shim_handle * hdl);
-    int (*modify) (struct shim_handle * hdl);
+    int (*update)(struct shim_handle* hdl);
+    int (*modify)(struct shim_handle* hdl);
 };
 
 struct shim_str_handle {
-    struct shim_str_data * data;       /* inode is stored in dentry, too.
-                                          store pointer here for efficiency */
-    char * ptr;
+    struct shim_str_data* data; /* inode is stored in dentry, too.
+                                   store pointer here for efficiency */
+    char* ptr;
 };
 
 DEFINE_LIST(shim_epoll_fd);
 DEFINE_LISTP(shim_epoll_fd);
 struct shim_epoll_handle {
-    int                 maxfds;
-    int                 nfds;
+    int maxfds;
+    int nfds;
     LISTP_TYPE(shim_epoll_fd) fds; /* this list contains all the
                                     * shim_epoll_fd objects in correspondence
                                     * with the registered handles. */
-    FDTYPE *            pal_fds;
-    PAL_HANDLE *        pal_handles;
-    int                 npals;
-    int                 nread;
-    int                 nwaiters;
-    AEVENTTYPE          event;
+    FDTYPE* pal_fds;
+    PAL_HANDLE* pal_handles;
+    int npals;
+    int nread;
+    int nwaiters;
+    AEVENTTYPE event;
 };
 
 struct shim_mount;
@@ -338,106 +333,99 @@ struct shim_dentry;
 /* The epolls list links to the back field of the shim_epoll_fd structure
  */
 struct shim_handle {
-    enum shim_handle_type   type;
+    enum shim_handle_type type;
 
-    REFTYPE             ref_count;
+    REFTYPE ref_count;
 
-    char                    fs_type[8];
-    struct shim_mount *     fs;
-    struct shim_qstr        path;
-    struct shim_dentry *    dentry;
+    char fs_type[8];
+    struct shim_mount* fs;
+    struct shim_qstr path;
+    struct shim_dentry* dentry;
 
     /* If this handle is registered for any epoll handle, this list contains
      * a shim_epoll_fd object in correspondence with the epoll handle. */
     LISTP_TYPE(shim_epoll_fd) epolls;
 
-    struct shim_qstr        uri;    /* URI representing this handle, it is not
-                                     * necessary to be set. */
+    struct shim_qstr uri; /* URI representing this handle, it is not
+                           * necessary to be set. */
 
-    PAL_HANDLE              pal_handle;
+    PAL_HANDLE pal_handle;
 
     union {
-        struct shim_file_handle   file;
-        struct shim_dev_handle    dev;
-        struct shim_pipe_handle   pipe;
-        struct shim_sock_handle   sock;
-        struct shim_shm_handle    shm;
-        struct shim_msg_handle    msg;
-        struct shim_sem_handle    sem;
-        struct shim_futex_handle  futex;
-        struct shim_str_handle    str;
-        struct shim_epoll_handle  epoll;
+        struct shim_file_handle file;
+        struct shim_dev_handle dev;
+        struct shim_pipe_handle pipe;
+        struct shim_sock_handle sock;
+        struct shim_shm_handle shm;
+        struct shim_msg_handle msg;
+        struct shim_sem_handle sem;
+        struct shim_futex_handle futex;
+        struct shim_str_handle str;
+        struct shim_epoll_handle epoll;
     } info;
 
-    struct shim_dir_handle    dir_info;
+    struct shim_dir_handle dir_info;
 
-    int                 flags;
-    int                 acc_mode;
-    IDTYPE              owner;
-    struct shim_lock    lock;
+    int flags;
+    int acc_mode;
+    IDTYPE owner;
+    struct shim_lock lock;
 };
 
 /* allocating / manage handle */
-struct shim_handle * get_new_handle (void);
-void flush_handle (struct shim_handle * hdl);
-void get_handle (struct shim_handle * hdl);
-void put_handle (struct shim_handle * hdl);
+struct shim_handle* get_new_handle(void);
+void flush_handle(struct shim_handle* hdl);
+void get_handle(struct shim_handle* hdl);
+void put_handle(struct shim_handle* hdl);
 
 /* file descriptor table */
 struct shim_fd_handle {
-    FDTYPE      vfd;        /* virtual file descriptor */
-    int         flags;      /* file descriptor flags, only FD_CLOEXEC */
+    FDTYPE vfd; /* virtual file descriptor */
+    int flags;  /* file descriptor flags, only FD_CLOEXEC */
 
-    struct shim_handle * handle;
+    struct shim_handle* handle;
 };
 
 struct shim_handle_map {
     /* the top of created file descriptors */
-    FDTYPE      fd_size;
-    FDTYPE      fd_top;
+    FDTYPE fd_size;
+    FDTYPE fd_top;
 
     /* refrence count and lock */
-    REFTYPE          ref_count;
+    REFTYPE ref_count;
     struct shim_lock lock;
 
     /* An array of file descriptor belong to this mapping */
-    struct shim_fd_handle ** map;
+    struct shim_fd_handle** map;
 };
 
 /* allocating file descriptors */
-#define FD_NULL ((FDTYPE)-1)
+#define FD_NULL                     ((FDTYPE)-1)
 #define HANDLE_ALLOCATED(fd_handle) ((fd_handle) && (fd_handle)->vfd != FD_NULL)
 
-struct shim_handle * __get_fd_handle (FDTYPE fd, int * flags,
-                                      struct shim_handle_map * map);
-struct shim_handle * get_fd_handle (FDTYPE fd, int * flags,
-                                    struct shim_handle_map * map);
-int set_new_fd_handle (struct shim_handle * hdl, int flags,
-                       struct shim_handle_map * map);
-int set_new_fd_handle_by_fd (FDTYPE fd, struct shim_handle * hdl,
-                             int flags, struct shim_handle_map * map);
-struct shim_handle *
-__detach_fd_handle (struct shim_fd_handle * fd, int * flags,
-                    struct shim_handle_map * map);
-struct shim_handle * detach_fd_handle (FDTYPE fd, int * flags,
-                                       struct shim_handle_map * map);
+struct shim_handle* __get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
+struct shim_handle* get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
+int set_new_fd_handle(struct shim_handle* hdl, int flags, struct shim_handle_map* map);
+int set_new_fd_handle_by_fd(FDTYPE fd, struct shim_handle* hdl, int flags,
+                            struct shim_handle_map* map);
+struct shim_handle* __detach_fd_handle(struct shim_fd_handle* fd, int* flags,
+                                       struct shim_handle_map* map);
+struct shim_handle* detach_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map);
 
 /* manage handle mapping */
-int dup_handle_map (struct shim_handle_map ** new_map,
-                    struct shim_handle_map * old_map);
-int flush_handle_map (struct shim_handle_map * map);
-void get_handle_map (struct shim_handle_map * map);
-void put_handle_map (struct shim_handle_map * map);
-int walk_handle_map (int (*callback) (struct shim_fd_handle *,
-                                      struct shim_handle_map *),
-                     struct shim_handle_map * map);
-
-int init_handle (void);
-int init_important_handles (void);
-
-off_t get_file_size (struct shim_handle * file);
-
-int do_handle_read (struct shim_handle * hdl, void * buf, int count);
-int do_handle_write (struct shim_handle * hdl, const void * buf, int count);
+int dup_handle_map(struct shim_handle_map** new_map, struct shim_handle_map* old_map);
+int flush_handle_map(struct shim_handle_map* map);
+void get_handle_map(struct shim_handle_map* map);
+void put_handle_map(struct shim_handle_map* map);
+int walk_handle_map(int (*callback)(struct shim_fd_handle*, struct shim_handle_map*),
+                    struct shim_handle_map* map);
+
+int init_handle(void);
+int init_important_handles(void);
+
+off_t get_file_size(struct shim_handle* file);
+
+int do_handle_read(struct shim_handle* hdl, void* buf, int count);
+int do_handle_write(struct shim_handle* hdl, const void* buf, int count);
 
 #endif /* _SHIM_HANDLE_H_ */

+ 106 - 124
LibOS/shim/include/shim_ipc.h

@@ -23,48 +23,48 @@
 #ifndef _SHIM_IPC_H_
 #define _SHIM_IPC_H_
 
-#include <shim_types.h>
+#include <list.h>
+#include <pal.h>
 #include <shim_defs.h>
 #include <shim_handle.h>
-#include <shim_thread.h>
 #include <shim_sysv.h>
-
-#include <pal.h>
-#include <list.h>
+#include <shim_thread.h>
+#include <shim_types.h>
 
 DEFINE_LIST(shim_ipc_info);
 struct shim_ipc_info {
-    IDTYPE                  vmid;
-    struct shim_ipc_port *  port;
-    PAL_HANDLE              pal_handle;
-    struct shim_qstr        uri;
+    IDTYPE vmid;
+    struct shim_ipc_port* port;
+    PAL_HANDLE pal_handle;
+    struct shim_qstr uri;
     LIST_TYPE(shim_ipc_info) hlist;
-    REFTYPE                 ref_count;
+    REFTYPE ref_count;
 };
 
 enum { PID_NS, SYSV_NS, TOTAL_NS };
 
 struct shim_process {
-    IDTYPE              vmid;
-    struct shim_lock    lock;
-    int                 exit_code;
-    struct shim_ipc_info * self, * parent;
-    struct shim_ipc_info * ns[TOTAL_NS];
+    IDTYPE vmid;
+    struct shim_lock lock;
+    int exit_code;
+    struct shim_ipc_info* self;
+    struct shim_ipc_info* parent;
+    struct shim_ipc_info* ns[TOTAL_NS];
 };
 
 extern struct shim_process cur_process;
 
-#define IPC_MSG_MINIMAL_SIZE        48
+#define IPC_MSG_MINIMAL_SIZE 48
 
 struct shim_ipc_msg {
-    unsigned char       code;
-    size_t              size;
-    IDTYPE              src, dst;
-    unsigned long       seq;
+    unsigned char code;
+    size_t size;
+    IDTYPE src, dst;
+    unsigned long seq;
 #ifdef PROFILE
-    unsigned long       time;
+    unsigned long time;
 #endif
-    char                msg[];
+    char msg[];
 } __attribute__((packed));
 
 struct shim_ipc_port;
@@ -72,40 +72,38 @@ struct shim_thread;
 
 DEFINE_LIST(shim_ipc_msg_duplex);
 struct shim_ipc_msg_duplex {
-    struct shim_thread *    thread;
+    struct shim_thread* thread;
     LIST_TYPE(shim_ipc_msg_duplex) list;
-    int                     retval;
-    void *                  private;
-    struct shim_ipc_msg     msg;
+    int retval;
+    void* private;
+    struct shim_ipc_msg msg;
 };
 
-typedef void (*port_fini) (struct shim_ipc_port *, IDTYPE vmid,
-                           unsigned int exitcode);
+typedef void (*port_fini)(struct shim_ipc_port*, IDTYPE vmid, unsigned int exitcode);
 
-#define MAX_IPC_PORT_FINI_CB        3
+#define MAX_IPC_PORT_FINI_CB 3
 
 DEFINE_LIST(shim_ipc_port);
 DEFINE_LISTP(shim_ipc_msg_duplex);
 struct shim_ipc_port {
-    PAL_HANDLE          pal_handle;
+    PAL_HANDLE pal_handle;
 
-    REFTYPE             ref_count;
+    REFTYPE ref_count;
     LIST_TYPE(shim_ipc_port) list;
     LISTP_TYPE(shim_ipc_msg_duplex) msgs;
-    struct shim_lock    msgs_lock;
+    struct shim_lock msgs_lock;
 
-    port_fini           fini[MAX_IPC_PORT_FINI_CB];
+    port_fini fini[MAX_IPC_PORT_FINI_CB];
 
-    IDTYPE              type;
-    IDTYPE              vmid;
+    IDTYPE type;
+    IDTYPE vmid;
 };
 
-#define IPC_CALLBACK_ARGS   \
-    struct shim_ipc_msg * msg, struct shim_ipc_port * port
+#define IPC_CALLBACK_ARGS struct shim_ipc_msg* msg, struct shim_ipc_port* port
 
 /* if callback return RESPONSE_CALLBACK, send a response even if the callback
    succeed. */
-#define RESPONSE_CALLBACK   1
+#define RESPONSE_CALLBACK 1
 
 typedef int (*ipc_callback)(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
 
@@ -131,7 +129,7 @@ int ipc_checkpoint_send(const char* cpdir, IDTYPE cpsession);
 int ipc_checkpoint_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
 
 /* Message code from child to parent */
-#define IPC_CLD_BASE       IPC_BASE_BOUND
+#define IPC_CLD_BASE IPC_BASE_BOUND
 enum {
     IPC_CLD_EXIT = IPC_CLD_BASE,
 #ifdef PROFILE
@@ -154,7 +152,7 @@ int ipc_cld_exit_send(IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned i
 int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port);
 
 #ifdef PROFILE
-# include <shim_profile.h>
+#include <shim_profile.h>
 
 struct shim_ipc_cld_profile {
     unsigned long time;
@@ -167,7 +165,7 @@ int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* por
 #endif
 
 /* Message code to namespace manager */
-#define IPC_PID_BASE       IPC_CLD_BOUND
+#define IPC_PID_BASE IPC_CLD_BOUND
 
 #define NS     pid
 #define NS_CAP PID
@@ -208,10 +206,9 @@ struct shim_ipc_pid_getstatus {
     IDTYPE pids[];
 } __attribute__((packed));
 
-int ipc_pid_getstatus_send (struct shim_ipc_port * port, IDTYPE dest,
-                            int npids, IDTYPE * pids,
-                            struct pid_status ** status);
-int ipc_pid_getstatus_callback (IPC_CALLBACK_ARGS);
+int ipc_pid_getstatus_send(struct shim_ipc_port* port, IDTYPE dest, int npids, IDTYPE* pids,
+                           struct pid_status** status);
+int ipc_pid_getstatus_callback(IPC_CALLBACK_ARGS);
 
 /* PID_RETSTATUS: return status of pid(s) */
 struct shim_ipc_pid_retstatus {
@@ -219,10 +216,9 @@ struct shim_ipc_pid_retstatus {
     struct pid_status status[];
 } __attribute__((packed));
 
-int ipc_pid_retstatus_send (struct shim_ipc_port * port, IDTYPE dest,
-                            int nstatus, struct pid_status * status,
-                            unsigned long seq);
-int ipc_pid_retstatus_callback (IPC_CALLBACK_ARGS);
+int ipc_pid_retstatus_send(struct shim_ipc_port* port, IDTYPE dest, int nstatus,
+                           struct pid_status* status, unsigned long seq);
+int ipc_pid_retstatus_callback(IPC_CALLBACK_ARGS);
 
 /* PID_GETMETA: get metadata of certain pid */
 enum pid_meta_code {
@@ -237,9 +233,8 @@ struct shim_ipc_pid_getmeta {
     enum pid_meta_code code;
 } __attribute__((packed));
 
-int ipc_pid_getmeta_send (IDTYPE pid, enum pid_meta_code code,
-                          void ** data);
-int ipc_pid_getmeta_callback (IPC_CALLBACK_ARGS);
+int ipc_pid_getmeta_send(IDTYPE pid, enum pid_meta_code code, void** data);
+int ipc_pid_getmeta_callback(IPC_CALLBACK_ARGS);
 
 /* PID_RETMETA: return metadata of certain pid */
 struct shim_ipc_pid_retmeta {
@@ -249,11 +244,10 @@ struct shim_ipc_pid_retmeta {
     char data[];
 } __attribute__((packed));
 
-int ipc_pid_retmeta_send (struct shim_ipc_port * port, IDTYPE dest,
-                          IDTYPE pid, enum pid_meta_code code,
-                          const void * data, int datasize,
-                          unsigned long seq);
-int ipc_pid_retmeta_callback (IPC_CALLBACK_ARGS);
+int ipc_pid_retmeta_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE pid,
+                         enum pid_meta_code code, const void* data, int datasize,
+                         unsigned long seq);
+int ipc_pid_retmeta_callback(IPC_CALLBACK_ARGS);
 
 /* PID_NOP: send junk message (for benchmarking) */
 struct shim_ipc_pid_nop {
@@ -261,8 +255,7 @@ struct shim_ipc_pid_nop {
     char payload[];
 } __attribute__((packed));
 
-int ipc_pid_nop_send (struct shim_ipc_port * port, IDTYPE dest, int count,
-                      const void * buf, int len);
+int ipc_pid_nop_send(struct shim_ipc_port* port, IDTYPE dest, int count, const void* buf, int len);
 int ipc_pid_nop_callback(IPC_CALLBACK_ARGS);
 
 /* PID_SENDRPC: send arbitary message (for benchmarking) */
@@ -272,15 +265,14 @@ struct shim_ipc_pid_sendrpc {
     char payload[];
 } __attribute__((packed));
 
-int ipc_pid_sendrpc_send (IDTYPE pid, IDTYPE sender, const void * buf,
-                          int len);
-int ipc_pid_sendrpc_callback (IPC_CALLBACK_ARGS);
+int ipc_pid_sendrpc_send(IDTYPE pid, IDTYPE sender, const void* buf, int len);
+int ipc_pid_sendrpc_callback(IPC_CALLBACK_ARGS);
 
-#define IPC_SYSV_BASE      IPC_PID_BOUND
+#define IPC_SYSV_BASE IPC_PID_BOUND
 
 struct sysv_key {
-    unsigned long   key;
-    enum sysv_type  type;
+    unsigned long key;
+    enum sysv_type type;
 };
 
 #define NS     sysv
@@ -312,9 +304,9 @@ struct shim_ipc_sysv_delres {
     enum sysv_type type;
 } __attribute__((packed));
 
-int ipc_sysv_delres_send (struct shim_ipc_port * port, IDTYPE dest,
-                          IDTYPE resid, enum sysv_type type);
-int ipc_sysv_delres_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_delres_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE resid,
+                         enum sysv_type type);
+int ipc_sysv_delres_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_MOVRES */
 struct shim_ipc_sysv_movres {
@@ -325,10 +317,9 @@ struct shim_ipc_sysv_movres {
     char uri[1];
 } __attribute__((packed));
 
-int ipc_sysv_movres_send (struct sysv_client * client, IDTYPE owner,
-                          const char * uri, LEASETYPE lease, IDTYPE resid,
-                          enum sysv_type type);
-int ipc_sysv_movres_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_movres_send(struct sysv_client* client, IDTYPE owner, const char* uri, LEASETYPE lease,
+                         IDTYPE resid, enum sysv_type type);
+int ipc_sysv_movres_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_MSGSND */
 struct shim_ipc_sysv_msgsnd {
@@ -337,10 +328,9 @@ struct shim_ipc_sysv_msgsnd {
     char msg[];
 } __attribute__((packed));
 
-int ipc_sysv_msgsnd_send (struct shim_ipc_port * port, IDTYPE dest,
-                          IDTYPE msgid, long msgtype,
-                          const void * buf, size_t size, unsigned long seq);
-int ipc_sysv_msgsnd_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_msgsnd_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE msgid, long msgtype,
+                         const void* buf, size_t size, unsigned long seq);
+int ipc_sysv_msgsnd_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_MSGRCV */
 struct shim_ipc_sysv_msgrcv {
@@ -350,9 +340,8 @@ struct shim_ipc_sysv_msgrcv {
     int flags;
 } __attribute__((packed));
 
-int ipc_sysv_msgrcv_send (IDTYPE msgid, long msgtype, int flags, void * buf,
-                          size_t size);
-int ipc_sysv_msgrcv_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_msgrcv_send(IDTYPE msgid, long msgtype, int flags, void* buf, size_t size);
+int ipc_sysv_msgrcv_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_MSGMOV */
 struct shim_ipc_sysv_msgmov {
@@ -362,10 +351,9 @@ struct shim_ipc_sysv_msgmov {
     struct sysv_score scores[];
 } __attribute__((packed));
 
-int ipc_sysv_msgmov_send (struct shim_ipc_port * port, IDTYPE dest,
-                          IDTYPE msgid, LEASETYPE lease,
-                          struct sysv_score * scores, int nscores);
-int ipc_sysv_msgmov_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_msgmov_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE msgid, LEASETYPE lease,
+                         struct sysv_score* scores, int nscores);
+int ipc_sysv_msgmov_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_SEMOP */
 struct shim_ipc_sysv_semop {
@@ -377,9 +365,9 @@ struct shim_ipc_sysv_semop {
 
 #define IPC_SEM_NOTIMEOUT ((unsigned long)-1)
 
-int ipc_sysv_semop_send (IDTYPE semid, struct sembuf * sops, int nsops,
-                         unsigned long timeout, unsigned long * seq);
-int ipc_sysv_semop_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semop_send(IDTYPE semid, struct sembuf* sops, int nsops, unsigned long timeout,
+                        unsigned long* seq);
+int ipc_sysv_semop_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_SEMCTL */
 struct shim_ipc_sysv_semctl {
@@ -390,9 +378,8 @@ struct shim_ipc_sysv_semctl {
     unsigned char vals[];
 } __attribute__((packed));
 
-int ipc_sysv_semctl_send (IDTYPE semid, int semnum, int cmd, void * vals,
-                          size_t valsize);
-int ipc_sysv_semctl_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semctl_send(IDTYPE semid, int semnum, int cmd, void* vals, size_t valsize);
+int ipc_sysv_semctl_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_SEMRET */
 struct shim_ipc_sysv_semret {
@@ -400,9 +387,9 @@ struct shim_ipc_sysv_semret {
     unsigned char vals[];
 } __attribute__((packed));
 
-int ipc_sysv_semret_send (struct shim_ipc_port * port, IDTYPE dest,
-                          void * vals, size_t valsize, unsigned long seq);
-int ipc_sysv_semret_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semret_send(struct shim_ipc_port* port, IDTYPE dest, void* vals, size_t valsize,
+                         unsigned long seq);
+int ipc_sysv_semret_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_SEMMOV */
 struct shim_ipc_sysv_semmov {
@@ -412,12 +399,10 @@ struct shim_ipc_sysv_semmov {
     struct sem_backup sems[];
 } __attribute__((packed));
 
-int ipc_sysv_semmov_send (struct shim_ipc_port * port, IDTYPE dest,
-                          IDTYPE semid, LEASETYPE lease,
-                          struct sem_backup * sems, int nsems,
-                          struct sem_client_backup * srcs, int nsrcs,
-                          struct sysv_score * scores, int nscores);
-int ipc_sysv_semmov_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semmov_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE semid, LEASETYPE lease,
+                         struct sem_backup* sems, int nsems, struct sem_client_backup* srcs,
+                         int nsrcs, struct sysv_score* scores, int nscores);
+int ipc_sysv_semmov_callback(IPC_CALLBACK_ARGS);
 
 #ifdef USE_SHARED_SEMAPHORE
 /* SYSV_SEMQUERY */
@@ -425,8 +410,8 @@ struct shim_ipc_sysv_semquery {
     IDTYPE semid;
 } __attribute__((packed));
 
-int ipc_sysv_semquery_send (IDTYPE semid, int * nsems, PAL_NUM ** host_sem_ids);
-int ipc_sysv_semquery_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semquery_send(IDTYPE semid, int* nsems, PAL_NUM** host_sem_ids);
+int ipc_sysv_semquery_callback(IPC_CALLBACK_ARGS);
 
 /* SYSV_SEMREPLY */
 struct shim_ipc_sysv_semreply {
@@ -435,13 +420,12 @@ struct shim_ipc_sysv_semreply {
     PAL_NUM host_sem_ids[];
 } __attribute__((packed));
 
-int ipc_sysv_semreply_send (struct shim_ipc_port * port, IDTYPE dest,
-                            IDTYPE semid, int nsems, PAL_NUM * host_sem_ids,
-                            unsigned long seq);
-int ipc_sysv_semreply_callback (IPC_CALLBACK_ARGS);
+int ipc_sysv_semreply_send(struct shim_ipc_port* port, IDTYPE dest, IDTYPE semid, int nsems,
+                           PAL_NUM* host_sem_ids, unsigned long seq);
+int ipc_sysv_semreply_callback(IPC_CALLBACK_ARGS);
 #endif
 
-#define IPC_CODE_NUM     IPC_SYSV_BOUND
+#define IPC_CODE_NUM IPC_SYSV_BOUND
 
 /* functions and routines */
 int init_ipc(void);
@@ -454,30 +438,28 @@ struct shim_ipc_info* create_ipc_info_cur_process(bool is_self_ipc_info);
 int get_ipc_info_cur_process(struct shim_ipc_info** pinfo);
 
 enum {
-    LISTEN,     /* listening */
-    SERVER,     /* connect as a server */
-    KEEPALIVE,  /* keep the connetion alive */
-    DIRCLD,     /* direct child */
-    DIRPRT,     /* direct parent */
-    NS_PORT_CONSTS(PID)
-    NS_PORT_CONSTS(SYSV)
+    LISTEN,    /* listening */
+    SERVER,    /* connect as a server */
+    KEEPALIVE, /* keep the connetion alive */
+    DIRCLD,    /* direct child */
+    DIRPRT,    /* direct parent */
+    NS_PORT_CONSTS(PID) NS_PORT_CONSTS(SYSV)
 };
 
 enum {
-    IPC_PORT_LISTEN = 1<<LISTEN,
-    IPC_PORT_SERVER = 1<<SERVER,
-    IPC_PORT_KEEPALIVE = 1<<KEEPALIVE,
-    IPC_PORT_DIRCLD = 1<<DIRCLD,
-    IPC_PORT_DIRPRT = 1<<DIRPRT,
-    NS_PORT_TYPES(PID)
-    NS_PORT_TYPES(SYSV)
+    IPC_PORT_LISTEN    = 1 << LISTEN,
+    IPC_PORT_SERVER    = 1 << SERVER,
+    IPC_PORT_KEEPALIVE = 1 << KEEPALIVE,
+    IPC_PORT_DIRCLD    = 1 << DIRCLD,
+    IPC_PORT_DIRPRT    = 1 << DIRPRT,
+    NS_PORT_TYPES(PID) NS_PORT_TYPES(SYSV)
 };
 
 /* general-purpose routines */
-void add_ipc_port_by_id(IDTYPE vmid, PAL_HANDLE hdl, IDTYPE type,
-                        port_fini fini, struct shim_ipc_port** portptr);
-void add_ipc_port(struct shim_ipc_port * port, IDTYPE vmid, IDTYPE type, port_fini fini);
-void del_ipc_port_fini(struct shim_ipc_port * port, unsigned int exitcode);
+void add_ipc_port_by_id(IDTYPE vmid, PAL_HANDLE hdl, IDTYPE type, port_fini fini,
+                        struct shim_ipc_port** portptr);
+void add_ipc_port(struct shim_ipc_port* port, IDTYPE vmid, IDTYPE type, port_fini fini);
+void del_ipc_port_fini(struct shim_ipc_port* port, unsigned int exitcode);
 struct shim_ipc_port* lookup_ipc_port(IDTYPE vmid, IDTYPE type);
 void get_ipc_port(struct shim_ipc_port* port);
 void put_ipc_port(struct shim_ipc_port* port);
@@ -499,7 +481,7 @@ static_always_inline size_t get_ipc_msg_size(size_t payload) {
 static_always_inline size_t get_ipc_msg_duplex_size(size_t payload) {
     assert(sizeof(struct shim_ipc_msg_duplex) >= sizeof(struct shim_ipc_msg));
     return get_ipc_msg_size(payload) +
-        (sizeof(struct shim_ipc_msg_duplex) - sizeof(struct shim_ipc_msg));
+           (sizeof(struct shim_ipc_msg_duplex) - sizeof(struct shim_ipc_msg));
 }
 
 void init_ipc_msg(struct shim_ipc_msg* msg, int code, size_t size, IDTYPE dest);

+ 83 - 92
LibOS/shim/include/shim_ipc_ns.h

@@ -23,46 +23,34 @@
 #ifndef __SHIM_IPC_NS_H__
 #define __SHIM_IPC_NS_H__
 
-#include <shim_types.h>
 #include <shim_internal.h>
+#include <shim_types.h>
+
+#define IPC_NS_CALLBACKS(ns)                                                                \
+    /* FINDNS */ &ipc_##ns##_findns_callback, /* TELLNS   */ &ipc_##ns##_tellns_callback,   \
+    /* LEASE  */ &ipc_##ns##_lease_callback,  /* OFFER    */ &ipc_##ns##_offer_callback,    \
+    /* RENEW  */ &ipc_##ns##_renew_callback,  /* SUBLEASE */ &ipc_##ns##_sublease_callback, \
+    /* QUERY  */ &ipc_##ns##_query_callback,  /* QUERYALL */ &ipc_##ns##_queryall_callback, \
+    /* ANSWER */ &ipc_##ns##_answer_callback,
+
+#define IPC_NS_KEY_CALLBACKS(ns) \
+    /* FINDKEY */ &ipc_##ns##_findkey_callback, /* TELLKEY */ &ipc_##ns##_tellkey_callback,
+
+#define NS_PORT_CONSTS(n) n##CLT, n##LDR, n##CON, n##OWN,
 
-#define IPC_NS_CALLBACKS(ns)                        \
-    /* FINDNS   */ &ipc_##ns##_findns_callback,     \
-    /* TELLNS   */ &ipc_##ns##_tellns_callback,     \
-    /* LEASE    */ &ipc_##ns##_lease_callback,      \
-    /* OFFER    */ &ipc_##ns##_offer_callback,      \
-    /* RENEW    */ &ipc_##ns##_renew_callback,      \
-    /* SUBLEASE */ &ipc_##ns##_sublease_callback,   \
-    /* QUERY    */ &ipc_##ns##_query_callback,      \
-    /* QUERYALL */ &ipc_##ns##_queryall_callback,   \
-    /* ANSWER   */ &ipc_##ns##_answer_callback,
-
-
-#define IPC_NS_KEY_CALLBACKS(ns)                    \
-    /* FINDKEY */  &ipc_##ns##_findkey_callback,    \
-    /* TELLKEY */  &ipc_##ns##_tellkey_callback,
-
-#define NS_PORT_CONSTS(n)   \
-    n##CLT,                 \
-    n##LDR,                 \
-    n##CON,                 \
-    n##OWN,
-
-#define NS_PORT_TYPES(n)            \
-    IPC_PORT_##n##CLT = 1<<n##CLT,  \
-    IPC_PORT_##n##LDR = 1<<n##LDR,  \
-    IPC_PORT_##n##CON = 1<<n##CON,  \
-    IPC_PORT_##n##OWN = 1<<n##OWN,
+#define NS_PORT_TYPES(n)                                              \
+    IPC_PORT_##n##CLT = 1 << n##CLT, IPC_PORT_##n##LDR = 1 << n##LDR, \
+    IPC_PORT_##n##CON = 1 << n##CON, IPC_PORT_##n##OWN = 1 << n##OWN,
 
 struct ipc_ns_offered {
-    IDTYPE       base, size;
-    LEASETYPE    lease;
+    IDTYPE base, size;
+    LEASETYPE lease;
     unsigned int owner_offset;
 } __attribute__((packed));
 
 struct ipc_ns_client {
-    IDTYPE       vmid;
-    char         uri[1];
+    IDTYPE vmid;
+    char uri[1];
 } __attribute__((packed));
 
 #endif /* __SHIM_IPC_NS_H__ */
@@ -72,30 +60,26 @@ struct ipc_ns_client {
 #define NS_CODE(t)     CONCAT3(IPC, NS_CAP, t)
 #define NS_MSG_TYPE(t) struct CONCAT3(shim_ipc, NS, t)
 
-int CONCAT3(add, NS, range) (IDTYPE base, IDTYPE owner,
-                             const char * uri, LEASETYPE lease);
+int CONCAT3(add, NS, range)(IDTYPE base, IDTYPE owner, const char* uri, LEASETYPE lease);
 
-int CONCAT3(del, NS, range) (IDTYPE idx);
+int CONCAT3(del, NS, range)(IDTYPE idx);
 
-int CONCAT3(add, NS, subrange) (IDTYPE idx, IDTYPE owner,
-                                const char * uri, LEASETYPE * lease);
+int CONCAT3(add, NS, subrange)(IDTYPE idx, IDTYPE owner, const char* uri, LEASETYPE* lease);
 
-int CONCAT3(del, NS, subrange) (IDTYPE idx);
+int CONCAT3(del, NS, subrange)(IDTYPE idx);
 
-int CONCAT3(alloc, NS, range) (IDTYPE owner, const char * uri,
-                               IDTYPE * base, LEASETYPE * lease);
+int CONCAT3(alloc, NS, range)(IDTYPE owner, const char* uri, IDTYPE* base, LEASETYPE* lease);
 
 struct CONCAT2(NS, range) {
-    IDTYPE              base, size;
-    IDTYPE              owner;
-    struct shim_qstr    uri;
-    LEASETYPE           lease;
-    struct shim_ipc_port * port;
+    IDTYPE base, size;
+    IDTYPE owner;
+    struct shim_qstr uri;
+    LEASETYPE lease;
+    struct shim_ipc_port* port;
 };
 
-int CONCAT3(get, NS, range) (IDTYPE idx,
-                             struct CONCAT2(NS, range) * range,
-                             struct shim_ipc_info ** pinfo);
+int CONCAT3(get, NS, range)(IDTYPE idx, struct CONCAT2(NS, range)* range,
+                            struct shim_ipc_info** pinfo);
 enum {
     NS_CODE(FINDNS) = CONCAT3(IPC, NS_CAP, BASE),
     NS_CODE(TELLNS),
@@ -114,110 +98,117 @@ enum {
 };
 
 /* FINDNS: find the channel of the namespace leader */
-int NS_SEND(findns) (bool block);
-int NS_CALLBACK(findns) (IPC_CALLBACK_ARGS);
+int NS_SEND(findns)(bool block);
+int NS_CALLBACK(findns)(IPC_CALLBACK_ARGS);
 
 /* TELLNS: tell the channel of namespace leader */
 NS_MSG_TYPE(tellns) {
     IDTYPE vmid;
     char uri[1];
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(tellns) (struct shim_ipc_port * port, IDTYPE dest,
-                     struct shim_ipc_info * leader, unsigned long seq);
-int NS_CALLBACK(tellns) (IPC_CALLBACK_ARGS);
+int NS_SEND(tellns)(struct shim_ipc_port* port, IDTYPE dest, struct shim_ipc_info* leader,
+                    unsigned long seq);
+int NS_CALLBACK(tellns)(IPC_CALLBACK_ARGS);
 
 /* LEASE: lease a range of name */
 NS_MSG_TYPE(lease) {
     char uri[1];
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(lease) (LEASETYPE * lease);
-int NS_CALLBACK(lease) (IPC_CALLBACK_ARGS);
+int NS_SEND(lease)(LEASETYPE* lease);
+int NS_CALLBACK(lease)(IPC_CALLBACK_ARGS);
 
 /* OFFER: offer a range of name */
 NS_MSG_TYPE(offer) {
     IDTYPE base, size;
     LEASETYPE lease;
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(offer) (struct shim_ipc_port * port, IDTYPE dest, IDTYPE base,
-                    IDTYPE size, LEASETYPE lease, unsigned long seq);
-int NS_CALLBACK(offer) (IPC_CALLBACK_ARGS);
+int NS_SEND(offer)(struct shim_ipc_port* port, IDTYPE dest, IDTYPE base, IDTYPE size,
+                   LEASETYPE lease, unsigned long seq);
+int NS_CALLBACK(offer)(IPC_CALLBACK_ARGS);
 
 /* RENEW: renew lease of a range of name */
 NS_MSG_TYPE(renew) {
     IDTYPE base, size;
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(renew) (IDTYPE base, IDTYPE size);
-int NS_CALLBACK(renew) (IPC_CALLBACK_ARGS);
+int NS_SEND(renew)(IDTYPE base, IDTYPE size);
+int NS_CALLBACK(renew)(IPC_CALLBACK_ARGS);
 
 /* SUBLEASE: lease a range of names */
 NS_MSG_TYPE(sublease) {
     IDTYPE tenant;
     IDTYPE idx;
     char uri[1];
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(sublease) (IDTYPE tenant, IDTYPE idx, const char * uri,
-                       LEASETYPE * lease);
-int NS_CALLBACK(sublease) (IPC_CALLBACK_ARGS);
+int NS_SEND(sublease)(IDTYPE tenant, IDTYPE idx, const char* uri, LEASETYPE* lease);
+int NS_CALLBACK(sublease)(IPC_CALLBACK_ARGS);
 
 /* QUERY: query the channel of certain name */
 NS_MSG_TYPE(query) {
     IDTYPE idx;
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(query) (IDTYPE idx);
-int NS_CALLBACK(query) (IPC_CALLBACK_ARGS);
+int NS_SEND(query)(IDTYPE idx);
+int NS_CALLBACK(query)(IPC_CALLBACK_ARGS);
 
 /* QUERY: query the channel of all names */
-int NS_SEND(queryall) (void);
-int NS_CALLBACK(queryall) (IPC_CALLBACK_ARGS);
+int NS_SEND(queryall)(void);
+int NS_CALLBACK(queryall)(IPC_CALLBACK_ARGS);
 
 /* ANSWER: answer the channel of certain names */
 NS_MSG_TYPE(answer) {
     int nanswers;
     struct ipc_ns_offered answers[];
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(answer) (struct shim_ipc_port * port, IDTYPE dest,
-                     int nanswers, struct ipc_ns_offered * answers,
-                     int nowners, struct ipc_ns_client ** ownerdata,
-                     int * ownerdatasz, unsigned long seq);
-int NS_CALLBACK(answer) (IPC_CALLBACK_ARGS);
+int NS_SEND(answer)(struct shim_ipc_port* port, IDTYPE dest, int nanswers,
+                    struct ipc_ns_offered* answers, int nowners, struct ipc_ns_client** ownerdata,
+                    int* ownerdatasz, unsigned long seq);
+int NS_CALLBACK(answer)(IPC_CALLBACK_ARGS);
 
 #ifdef NS_KEY
 
-int CONCAT2(NS, add_key) (NS_KEY * key, IDTYPE id);
-int CONCAT2(NS, get_key) (NS_KEY * key, bool delete);
+int CONCAT2(NS, add_key)(NS_KEY* key, IDTYPE id);
+int CONCAT2(NS, get_key)(NS_KEY* key, bool delete);
 
 /* FINDKEY */
 NS_MSG_TYPE(findkey) {
     NS_KEY key;
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(findkey) (NS_KEY * key);
-int NS_CALLBACK(findkey) (IPC_CALLBACK_ARGS);
+int NS_SEND(findkey)(NS_KEY* key);
+int NS_CALLBACK(findkey)(IPC_CALLBACK_ARGS);
 
 /* TELLKEY */
 NS_MSG_TYPE(tellkey) {
     NS_KEY key;
     IDTYPE id;
-} __attribute__((packed));
+}
+__attribute__((packed));
 
-int NS_SEND(tellkey) (struct shim_ipc_port * port, IDTYPE dest, NS_KEY * key,
-                      IDTYPE id, unsigned long seq);
-int NS_CALLBACK(tellkey) (IPC_CALLBACK_ARGS);
+int NS_SEND(tellkey)(struct shim_ipc_port* port, IDTYPE dest, NS_KEY* key, IDTYPE id,
+                     unsigned long seq);
+int NS_CALLBACK(tellkey)(IPC_CALLBACK_ARGS);
 
-# undef NS_KEY
+#undef NS_KEY
 #endif
 
-IDTYPE CONCAT2(allocate, NS) (IDTYPE min, IDTYPE max);
-void CONCAT2(release,  NS) (IDTYPE idx);
+IDTYPE CONCAT2(allocate, NS)(IDTYPE min, IDTYPE max);
+void CONCAT2(release, NS)(IDTYPE idx);
 
-int CONCAT3(prepare, NS, leader) (void);
+int CONCAT3(prepare, NS, leader)(void);
 
 #undef NS_SEND
 #undef NS_CALLBACK

+ 163 - 124
LibOS/shim/include/shim_profile.h

@@ -29,10 +29,10 @@
 #include <atomic.h>
 
 struct shim_profile {
-    const char * name;
+    const char* name;
     enum { CATEGORY, OCCURENCE, INTERVAL } type;
     bool disabled;
-    struct shim_profile * root;
+    struct shim_profile* root;
     union {
         struct {
             struct atomic_int count;
@@ -60,163 +60,202 @@ struct profile_val {
 extern struct shim_profile __profile;
 extern struct shim_profile __profile_end;
 
-#define N_PROFILE \
-    (((void *) &__profile_end - (void *) &__profile) / sizeof(struct shim_profile))
+#define N_PROFILE (((void*)&__profile_end - (void*)&__profile) / sizeof(struct shim_profile))
 
 #define PROFILES (&__profile)
 
-#define DEFINE_PROFILE_CATEGORY(prof, rprof)                \
-    _DEFINE_PROFILE_CATEGORY(prof, rprof)
-#define _DEFINE_PROFILE_CATEGORY(prof, rprof)               \
-    extern struct shim_profile profile_##rprof;             \
-    struct shim_profile profile_##prof                      \
-        __attribute__((section(".profile"))) = {            \
-        .name = #prof,                                      \
-        .root = &profile_##rprof,                           \
-        .type = CATEGORY,                                   \
+#define DEFINE_PROFILE_CATEGORY(prof, rprof) _DEFINE_PROFILE_CATEGORY(prof, rprof)
+#define _DEFINE_PROFILE_CATEGORY(prof, rprof)                                   \
+    extern struct shim_profile profile_##rprof;                                 \
+    struct shim_profile profile_##prof __attribute__((section(".profile"))) = { \
+        .name = #prof,                                                          \
+        .root = &profile_##rprof,                                               \
+        .type = CATEGORY,                                                       \
     };
 
-#define DEFINE_PROFILE_CATEGORY_DISABLED(prof, rprof)       \
-    _DEFINE_PROFILE_CATEGORY(prof, rprof)
-#define _DEFINE_PROFILE_CATEGORY_DISABLED(prof, rprof)      \
-    extern struct shim_profile profile_##rprof;             \
-    struct shim_profile profile_##prof                      \
-        __attribute__((section(".profile"))) = {            \
-        .name = #prof,                                      \
-        .disabled = true,                                   \
-        .root = &profile_##rprof,                           \
-        .type = CATEGORY,                                   \
+#define DEFINE_PROFILE_CATEGORY_DISABLED(prof, rprof) _DEFINE_PROFILE_CATEGORY(prof, rprof)
+#define _DEFINE_PROFILE_CATEGORY_DISABLED(prof, rprof)                          \
+    extern struct shim_profile profile_##rprof;                                 \
+    struct shim_profile profile_##prof __attribute__((section(".profile"))) = { \
+        .name     = #prof,                                                      \
+        .disabled = true,                                                       \
+        .root     = &profile_##rprof,                                           \
+        .type     = CATEGORY,                                                   \
     };
 
-
-#define DEFINE_PROFILE_OCCURENCE(prof, rprof)               \
-    _DEFINE_PROFILE_OCCURENCE(prof, rprof)
-#define _DEFINE_PROFILE_OCCURENCE(prof, rprof)              \
-    extern struct shim_profile profile_##rprof;             \
-    struct shim_profile profile_##prof                      \
-        __attribute__((section(".profile"))) = {            \
-        .name = #prof,                                      \
-        .root = &profile_##rprof,                           \
-        .type = OCCURENCE,                                  \
+#define DEFINE_PROFILE_OCCURENCE(prof, rprof) _DEFINE_PROFILE_OCCURENCE(prof, rprof)
+#define _DEFINE_PROFILE_OCCURENCE(prof, rprof)                                  \
+    extern struct shim_profile profile_##rprof;                                 \
+    struct shim_profile profile_##prof __attribute__((section(".profile"))) = { \
+        .name = #prof,                                                          \
+        .root = &profile_##rprof,                                               \
+        .type = OCCURENCE,                                                      \
     };
 
-#define DEFINE_PROFILE_INTERVAL(prof, rprof)                \
-    _DEFINE_PROFILE_INTERVAL(prof, rprof)
-#define _DEFINE_PROFILE_INTERVAL(prof, rprof)               \
-    extern struct shim_profile profile_##rprof;             \
-    struct shim_profile profile_##prof                      \
-        __attribute__((section(".profile"))) = {            \
-        .name = #prof,                                      \
-        .root = &profile_##rprof,                           \
-        .type = INTERVAL,                                   \
+#define DEFINE_PROFILE_INTERVAL(prof, rprof) _DEFINE_PROFILE_INTERVAL(prof, rprof)
+#define _DEFINE_PROFILE_INTERVAL(prof, rprof)                                   \
+    extern struct shim_profile profile_##rprof;                                 \
+    struct shim_profile profile_##prof __attribute__((section(".profile"))) = { \
+        .name = #prof,                                                          \
+        .root = &profile_##rprof,                                               \
+        .type = INTERVAL,                                                       \
     };
 
 #define profile_ profile_root
 
 #define INC_PROFILE_OCCURENCE(prof) _INC_PROFILE_OCCURENCE(prof)
-#define _INC_PROFILE_OCCURENCE(prof)                        \
-    ({                                                      \
-        extern struct shim_profile profile_##prof;          \
-        profile_##prof.disabled ? 0 :                       \
-        atomic_inc_return(&profile_##prof.val.occurence.count); \
+#define _INC_PROFILE_OCCURENCE(prof)                                                          \
+    ({                                                                                        \
+        extern struct shim_profile profile_##prof;                                            \
+        profile_##prof.disabled ? 0 : atomic_inc_return(&profile_##prof.val.occurence.count); \
     })
 
 #define ADD_PROFILE_OCCURENCE(prof, num) _ADD_PROFILE_OCCURENCE(prof, num)
-#define _ADD_PROFILE_OCCURENCE(prof, num)                   \
-    ({                                                      \
-        extern struct shim_profile profile_##prof;          \
-        profile_##prof.disabled ? 0 :                       \
-        atomic_add_return(num, &profile_##prof.val.occurence.count); \
+#define _ADD_PROFILE_OCCURENCE(prof, num)                                                          \
+    ({                                                                                             \
+        extern struct shim_profile profile_##prof;                                                 \
+        profile_##prof.disabled ? 0 : atomic_add_return(num, &profile_##prof.val.occurence.count); \
     })
 
-#define BEGIN_PROFILE_INTERVAL()                            \
-    unsigned long _interval;                                \
-    do { _interval = DkSystemTimeQuery(); } while (0)
+#define BEGIN_PROFILE_INTERVAL()         \
+    unsigned long _interval;             \
+    do {                                 \
+        _interval = DkSystemTimeQuery(); \
+    } while (0)
 
-#define BEGIN_PROFILE_INTERVAL_SET(val)                     \
-    unsigned long _interval;                                \
-    do { _interval = (val); } while (0)
+#define BEGIN_PROFILE_INTERVAL_SET(val) \
+    unsigned long _interval;            \
+    do {                                \
+        _interval = (val);              \
+    } while (0)
 
-#define SET_PROFILE_INTERVAL(val)                           \
-    do { _interval = (val); } while (0)
+#define SET_PROFILE_INTERVAL(val) \
+    do {                          \
+        _interval = (val);        \
+    } while (0)
 
 #define GET_PROFILE_INTERVAL() DkSystemTimeQuery()
 
-#define UPDATE_PROFILE_INTERVAL()                           \
-    ({                                                      \
-        unsigned long _c = DkSystemTimeQuery();             \
-        unsigned long _t = _c - _interval;                  \
-        _interval = _c;                                     \
-        _t;                                                 \
-     })
+#define UPDATE_PROFILE_INTERVAL()               \
+    ({                                          \
+        unsigned long _c = DkSystemTimeQuery(); \
+        unsigned long _t = _c - _interval;      \
+        _interval        = _c;                  \
+        _t;                                     \
+    })
 
 #define ASSIGN_PROFILE_INTERVAL(prof) _ASSIGN_PROFILE_INTERVAL(prof)
-#define _ASSIGN_PROFILE_INTERVAL(prof)                      \
-    extern struct shim_profile profile_##prof;              \
-    struct shim_profile *_profile = &profile_##prof;
-
-#define SAVE_PROFILE_INTERVAL_ASSIGNED()                    \
-     ({                                                     \
-        _profile->disabled ? 0 : ({                         \
-        unsigned long _t = UPDATE_PROFILE_INTERVAL();       \
-        atomic_inc(&_profile->val.interval.count);          \
-        atomic_add(_t, &_profile->val.interval.time);       \
-        _t; });                                             \
-     })
+#define _ASSIGN_PROFILE_INTERVAL(prof)         \
+    extern struct shim_profile profile_##prof; \
+    struct shim_profile* _profile = &profile_##prof;
+
+#define SAVE_PROFILE_INTERVAL_ASSIGNED()                  \
+    ({                                                    \
+        _profile->disabled ? 0 : ({                       \
+            unsigned long _t = UPDATE_PROFILE_INTERVAL(); \
+            atomic_inc(&_profile->val.interval.count);    \
+            atomic_add(_t, &_profile->val.interval.time); \
+            _t;                                           \
+        });                                               \
+    })
 
 #define SAVE_PROFILE_INTERVAL(prof) _SAVE_PROFILE_INTERVAL(prof)
-#define _SAVE_PROFILE_INTERVAL(prof)                        \
-     ({                                                     \
-        extern struct shim_profile profile_##prof;          \
-        profile_##prof.disabled ? 0 : ({                    \
-        unsigned long _t = UPDATE_PROFILE_INTERVAL();       \
-        atomic_inc(&profile_##prof.val.interval.count);     \
-        atomic_add(_t, &profile_##prof.val.interval.time);  \
-        _t; });                                             \
-     })
-
-#define SAVE_PROFILE_INTERVAL_SINCE(prof, since)            \
-        _SAVE_PROFILE_INTERVAL_SINCE(prof, since)
-#define _SAVE_PROFILE_INTERVAL_SINCE(prof, since)           \
-     ({                                                     \
-        extern struct shim_profile profile_##prof;          \
-        profile_##prof.disabled ? 0 : ({                    \
-        unsigned long _c = DkSystemTimeQuery();             \
-        unsigned long _t = _c - (since);                    \
-        atomic_inc(&profile_##prof.val.interval.count);     \
-        atomic_add(_t, &profile_##prof.val.interval.time);  \
-        _t; });                                             \
-     })
-
-#define SAVE_PROFILE_INTERVAL_SET(prof, begin, end)         \
-        _SAVE_PROFILE_INTERVAL_SET(prof, begin, end)
-#define _SAVE_PROFILE_INTERVAL_SET(prof, begin, end)        \
-     ({                                                     \
-        extern struct shim_profile profile_##prof;          \
-        profile_##prof.disabled ? 0 : ({                    \
-        unsigned long _t = (end) - (begin);                 \
-        atomic_inc(&profile_##prof.val.interval.count);     \
-        atomic_add(_t, &profile_##prof.val.interval.time);  \
-        _t; });                                             \
-     })
+#define _SAVE_PROFILE_INTERVAL(prof)                           \
+    ({                                                         \
+        extern struct shim_profile profile_##prof;             \
+        profile_##prof.disabled ? 0 : ({                       \
+            unsigned long _t = UPDATE_PROFILE_INTERVAL();      \
+            atomic_inc(&profile_##prof.val.interval.count);    \
+            atomic_add(_t, &profile_##prof.val.interval.time); \
+            _t;                                                \
+        });                                                    \
+    })
+
+#define SAVE_PROFILE_INTERVAL_SINCE(prof, since) _SAVE_PROFILE_INTERVAL_SINCE(prof, since)
+#define _SAVE_PROFILE_INTERVAL_SINCE(prof, since)              \
+    ({                                                         \
+        extern struct shim_profile profile_##prof;             \
+        profile_##prof.disabled ? 0 : ({                       \
+            unsigned long _c = DkSystemTimeQuery();            \
+            unsigned long _t = _c - (since);                   \
+            atomic_inc(&profile_##prof.val.interval.count);    \
+            atomic_add(_t, &profile_##prof.val.interval.time); \
+            _t;                                                \
+        });                                                    \
+    })
+
+#define SAVE_PROFILE_INTERVAL_SET(prof, begin, end) _SAVE_PROFILE_INTERVAL_SET(prof, begin, end)
+#define _SAVE_PROFILE_INTERVAL_SET(prof, begin, end)           \
+    ({                                                         \
+        extern struct shim_profile profile_##prof;             \
+        profile_##prof.disabled ? 0 : ({                       \
+            unsigned long _t = (end) - (begin);                \
+            atomic_inc(&profile_##prof.val.interval.count);    \
+            atomic_add(_t, &profile_##prof.val.interval.time); \
+            _t;                                                \
+        });                                                    \
+    })
 
 #else
 
 #define DEFINE_PROFILE_CATEGORY(prof, rprof)
 #define DEFINE_PROFILE_OCCURENCE(prof, rprof)
 #define DEFINE_PROFILE_INTERVAL(prof, rprof)
-#define INC_PROFILE_OCCURENCE(prof) ({ do {} while (0); 0; })
-#define ADD_PROFILE_OCCURENCE(prof, val) ({ do {} while (0); 0; })
-#define BEGIN_PROFILE_INTERVAL() do {} while (0)
-#define BEGIN_PROFILE_INTERVAL_SET(val) do {} while (0)
-#define SET_PROFILE_INTERVAL(val) do {} while (0)
+#define INC_PROFILE_OCCURENCE(prof) \
+    ({                              \
+        do {                        \
+        } while (0);                \
+        0;                          \
+    })
+#define ADD_PROFILE_OCCURENCE(prof, val) \
+    ({                                   \
+        do {                             \
+        } while (0);                     \
+        0;                               \
+    })
+#define BEGIN_PROFILE_INTERVAL() \
+    do {                         \
+    } while (0)
+#define BEGIN_PROFILE_INTERVAL_SET(val) \
+    do {                                \
+    } while (0)
+#define SET_PROFILE_INTERVAL(val) \
+    do {                          \
+    } while (0)
 #define GET_PROFILE_INTERVAL() (0)
-#define UPDATE_PROFILE_INTERVAL() ({ do {} while (0); 0; })
-#define ASSIGN_PROFILE_INTERVAL(prof) do {} while (0)
-#define SAVE_PROFILE_INTERVAL_ASSIGNED() ({ do {} while (0); 0; })
-#define SAVE_PROFILE_INTERVAL(prof) ({ do {} while (0); 0; })
-#define SAVE_PROFILE_INTERVAL_SINCE(prof, time) ({ do {} while (0); 0; })
-#define SAVE_PROFILE_INTERVAL_SET(prof, begin, end) ({ do {} while (0); 0; })
+#define UPDATE_PROFILE_INTERVAL() \
+    ({                            \
+        do {                      \
+        } while (0);              \
+        0;                        \
+    })
+#define ASSIGN_PROFILE_INTERVAL(prof) \
+    do {                              \
+    } while (0)
+#define SAVE_PROFILE_INTERVAL_ASSIGNED() \
+    ({                                   \
+        do {                             \
+        } while (0);                     \
+        0;                               \
+    })
+#define SAVE_PROFILE_INTERVAL(prof) \
+    ({                              \
+        do {                        \
+        } while (0);                \
+        0;                          \
+    })
+#define SAVE_PROFILE_INTERVAL_SINCE(prof, time) \
+    ({                                          \
+        do {                                    \
+        } while (0);                            \
+        0;                                      \
+    })
+#define SAVE_PROFILE_INTERVAL_SET(prof, begin, end) \
+    ({                                              \
+        do {                                        \
+        } while (0);                                \
+        0;                                          \
+    })
 
 #endif
 

+ 0 - 1
LibOS/shim/include/shim_signal.h

@@ -3,7 +3,6 @@
 
 #include <shim_types.h>
 #include <shim_defs.h>
-#include <shim_thread.h>
 
 struct shim_signal_handle {
     /* sigaction */

+ 103 - 111
LibOS/shim/include/shim_sysv.h

@@ -21,151 +21,147 @@
  * functionality.
  */
 
-
 #ifndef __SHIM_SYSV_H__
 #define __SHIM_SYSV_H__
 
-#include <shim_types.h>
 #include <shim_handle.h>
+#include <shim_types.h>
 
 enum sysv_type { SYSV_NONE, SYSV_MSGQ, SYSV_SEM, SYSV_SHM };
 
-#define SYSV_TYPE_STR(type)     \
-        ((type) == SYSV_MSGQ ? "MSGQ" :   \
-        ((type) == SYSV_SEM  ? "SEM"  :   \
-        ((type) == SYSV_SHM  ? "SHM"  :   \
-         "")))
+#define SYSV_TYPE_STR(type)       \
+    ((type) == SYSV_MSGQ ? "MSGQ" \
+                         : ((type) == SYSV_SEM ? "SEM" : ((type) == SYSV_SHM ? "SHM" : "")))
 
-#define VALID_SYSV_TYPE(type)   \
-        ((type) == SYSV_MSGQ || (type) == SYSV_SEM || (type) == SYSV_SHM)
+#define VALID_SYSV_TYPE(type) ((type) == SYSV_MSGQ || (type) == SYSV_SEM || (type) == SYSV_SHM)
 
 struct sysv_score {
-    IDTYPE                  vmid;
-    unsigned long           score;
+    IDTYPE vmid;
+    unsigned long score;
 };
 
 struct sysv_client {
-    struct shim_ipc_port *  port;
-    IDTYPE                  vmid;
-    unsigned                seq;
+    struct shim_ipc_port* port;
+    IDTYPE vmid;
+    unsigned seq;
 };
 
 struct shim_handle;
 
 struct sysv_balance_policy {
-    unsigned int    score_decay;
-    unsigned int    score_max;
-    unsigned int    balance_threshold;
-    int (*migrate) (struct shim_handle * hdl, struct sysv_client * client);
+    unsigned int score_decay;
+    unsigned int score_max;
+    unsigned int balance_threshold;
+    int (*migrate)(struct shim_handle* hdl, struct sysv_client* client);
 };
 
-int __balance_sysv_score (struct sysv_balance_policy * policy,
-                          struct shim_handle * hdl,
-                          struct sysv_score * scores, int nscores,
-                          struct sysv_client * src, long score);
+int __balance_sysv_score(struct sysv_balance_policy* policy, struct shim_handle* hdl,
+                         struct sysv_score* scores, int nscores, struct sysv_client* src,
+                         long score);
 
 #define MSG_NOERROR 010000
 
 #include <list.h>
 
 struct __kernel_msgbuf {
-    long mtype;     /* type of message */
-    char mtext[];   /* message text */
+    long mtype;   /* type of message */
+    char mtext[]; /* message text */
 };
 
-#define MSG_QOBJ_SIZE   64
+#define MSG_QOBJ_SIZE 64
 
 struct msg_qobj {
-    void * next;
-    char data[MSG_QOBJ_SIZE - sizeof(void *)];
+    void* next;
+    char data[MSG_QOBJ_SIZE - sizeof(void*)];
 } __attribute__((packed));
 
 struct msg_item {
-    void * next;
+    void* next;
     unsigned short size;
     char data[];
 } __attribute__((packed));
 
-#define MSG_ITEM_DATA_SIZE(size)                                        \
-    ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_item) ? (size) :        \
-     MSG_QOBJ_SIZE - sizeof(struct msg_item))
+#define MSG_ITEM_DATA_SIZE(size)                               \
+    ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_item) ? (size) \
+                                                      : MSG_QOBJ_SIZE - sizeof(struct msg_item))
 
 struct msg_ext_item {
-    void * next;
+    void* next;
     char data[];
 } __attribute__((packed));
 
-#define MSG_EXT_ITEM_DATA_SIZE(size)                                    \
-    ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_ext_item) ? (size) :    \
-     MSG_QOBJ_SIZE - sizeof(struct msg_ext_item))
+#define MSG_EXT_ITEM_DATA_SIZE(size)                      \
+    ((size) < MSG_QOBJ_SIZE - sizeof(struct msg_ext_item) \
+         ? (size)                                         \
+         : MSG_QOBJ_SIZE - sizeof(struct msg_ext_item))
 
 struct msg_req {
-    struct msg_req *        next;
-    unsigned short          size;
-    int                     flags;
-    struct sysv_client      dest;
+    struct msg_req* next;
+    unsigned short size;
+    int flags;
+    struct sysv_client dest;
 } __attribute__((packed));
 
-#define INIT_MSG_TYPE_SIZE      32
+#define INIT_MSG_TYPE_SIZE 32
 
 struct msg_type {
-    long type;              /* type of the messages */
-    struct msg_item * msgs, * msg_tail;
-    struct msg_req  * reqs, * req_tail;
+    long type; /* type of the messages */
+    struct msg_item* msgs;
+    struct msg_item* msg_tail;
+    struct msg_req* reqs;
+    struct msg_req* req_tail;
 };
 
-#define DEFAULT_MSG_QUEUE_SIZE      2048
+#define DEFAULT_MSG_QUEUE_SIZE 2048
 
-#define MSG_SND_SCORE             1
-#define MSG_RCV_SCORE             20
-#define MSG_SCORE_DECAY           10
-#define MSG_SCORE_MAX             200
-#define MSG_BALANCE_THRESHOLD     100
+#define MSG_SND_SCORE         1
+#define MSG_RCV_SCORE         20
+#define MSG_SCORE_DECAY       10
+#define MSG_SCORE_MAX         200
+#define MSG_BALANCE_THRESHOLD 100
 
 struct msg_handle_backup {
-    int     perm;           /* access permissions */
-    int     nmsgs;          /* number of msgs */
-    int     currentsize;    /* current size in bytes */
+    int perm;        /* access permissions */
+    int nmsgs;       /* number of msgs */
+    int currentsize; /* current size in bytes */
 };
 
 struct msg_backup {
-    long    type;
-    int     size;
-    char    data[];
+    long type;
+    int size;
+    char data[];
 };
 
 struct shim_msg_handle;
 
-int add_msg_handle (unsigned long key, IDTYPE id, bool owned);
-int del_msg_handle (struct shim_msg_handle * msgq);
+int add_msg_handle(unsigned long key, IDTYPE id, bool owned);
+int del_msg_handle(struct shim_msg_handle* msgq);
 
-struct shim_msg_handle * get_msg_handle_by_key (unsigned long key);
-struct shim_msg_handle * get_msg_handle_by_id (IDTYPE id);
+struct shim_msg_handle* get_msg_handle_by_key(unsigned long key);
+struct shim_msg_handle* get_msg_handle_by_id(IDTYPE id);
 
-void put_msg_handle (struct shim_msg_handle * msgq);
+void put_msg_handle(struct shim_msg_handle* msgq);
 
-int recover_msg_ownership (struct shim_msg_handle * msgq);
+int recover_msg_ownership(struct shim_msg_handle* msgq);
 
-int add_sysv_msg (struct shim_msg_handle * msgq,
-                  long type, size_t size, const void * data,
-                  struct sysv_client * src);
-int get_sysv_msg (struct shim_msg_handle * msgq,
-                  long type, size_t size, void * data, int flags,
-                  struct sysv_client * src);
+int add_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, const void* data,
+                 struct sysv_client* src);
+int get_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, void* data, int flags,
+                 struct sysv_client* src);
 
-int store_all_msg_persist (void);
+int store_all_msg_persist(void);
 
-#define HOST_SEM_NUM        65535
+#define HOST_SEM_NUM 65535
 
 DEFINE_LIST(sem_ops);
 struct sem_ops {
-    LIST_TYPE(sem_ops)      progress;
+    LIST_TYPE(sem_ops) progress;
     struct sem_stat {
-        bool                completed;
-        bool                failed;
-        int                 nops;
-        int                 current;
-        unsigned long       timeout;
+        bool completed;
+        bool failed;
+        int nops;
+        int current;
+        unsigned long timeout;
     } stat;
     struct sysv_client client;
     struct sembuf ops[];
@@ -173,57 +169,53 @@ struct sem_ops {
 
 DEFINE_LISTP(sem_ops);
 struct sem_obj {
-    unsigned short      num;
-    unsigned short      val;
-    unsigned short      zcnt;
-    unsigned short      ncnt;
-    IDTYPE              pid;
-    PAL_NUM             host_sem_id;
-    PAL_HANDLE          host_sem;
+    unsigned short num;
+    unsigned short val;
+    unsigned short zcnt;
+    unsigned short ncnt;
+    IDTYPE pid;
+    PAL_NUM host_sem_id;
+    PAL_HANDLE host_sem;
     LISTP_TYPE(sem_ops) ops;
     LISTP_TYPE(sem_ops) next_ops;
 };
 
-
-#define SEM_POSITIVE_SCORE(num)  ((num) < 5 ? 5 - (num) : 1)
-#define SEM_ZERO_SCORE           20
-#define SEM_NEGATIVE_SCORE(num)  (20 * (num))
-#define SEM_SCORE_DECAY          10
-#define SEM_SCORE_MAX            200
-#define SEM_BALANCE_THRESHOLD    100
+#define SEM_POSITIVE_SCORE(num) ((num) < 5 ? 5 - (num) : 1)
+#define SEM_ZERO_SCORE          20
+#define SEM_NEGATIVE_SCORE(num) (20 * (num))
+#define SEM_SCORE_DECAY         10
+#define SEM_SCORE_MAX           200
+#define SEM_BALANCE_THRESHOLD   100
 
 struct sem_backup {
-    unsigned short      val;
-    unsigned short      zcnt;
-    unsigned short      ncnt;
-    IDTYPE              pid;
+    unsigned short val;
+    unsigned short zcnt;
+    unsigned short ncnt;
+    IDTYPE pid;
 };
 
 struct sem_client_backup {
-    IDTYPE          vmid;
-    unsigned long   seq;
-    int             current;
-    int             nops;
+    IDTYPE vmid;
+    unsigned long seq;
+    int current;
+    int nops;
 };
 
-int add_sem_handle (unsigned long key, IDTYPE id, int nsems, bool owned);
-struct shim_sem_handle * get_sem_handle_by_key (unsigned long key);
-struct shim_sem_handle * get_sem_handle_by_id (IDTYPE semid);
-void put_sem_handle (struct shim_sem_handle * sem);
-int del_sem_handle (struct shim_sem_handle * sem);
+int add_sem_handle(unsigned long key, IDTYPE id, int nsems, bool owned);
+struct shim_sem_handle* get_sem_handle_by_key(unsigned long key);
+struct shim_sem_handle* get_sem_handle_by_id(IDTYPE semid);
+void put_sem_handle(struct shim_sem_handle* sem);
+int del_sem_handle(struct shim_sem_handle* sem);
 
-int recover_sem_ownership (struct shim_sem_handle * sem,
-                           struct sem_backup * backups, int nbackups,
-                           struct sem_client_backup * clients, int nclients);
+int recover_sem_ownership(struct shim_sem_handle* sem, struct sem_backup* backups, int nbackups,
+                          struct sem_client_backup* clients, int nclients);
 
-int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
-                     int nsops, unsigned long timeout,
-                     struct sysv_client * client);
+int submit_sysv_sem(struct shim_sem_handle* sem, struct sembuf* sops, int nsops,
+                    unsigned long timeout, struct sysv_client* client);
 
 #ifdef USE_SHARED_SEMAPHORE
-int send_sem_host_ids (struct shim_sem_handle * sem,
-                       struct shim_ipc_port * port, IDTYPE dest,
-                       unsigned long seq);
+int send_sem_host_ids(struct shim_sem_handle* sem, struct shim_ipc_port* port, IDTYPE dest,
+                      unsigned long seq);
 #endif
 
 #endif /* __SHIM_SYSV_H__ */

+ 783 - 848
LibOS/shim/include/shim_table.h

@@ -1,10 +1,9 @@
 #ifndef _SHIM_TABLE_H_
 #define _SHIM_TABLE_H_
 
-#include <stdnoreturn.h>
-
 #include <shim_types.h>
 #include <shim_unistd.h>
+#include <stdnoreturn.h>
 
 #ifdef IN_SHIM
 
@@ -13,869 +12,805 @@ typedef void (*shim_fp)(void);
 extern shim_fp shim_table[];
 
 /* syscall entries */
-long __shim_read (long, long , long);
-long __shim_write (long, long, long);
-long __shim_open (long, long , long);
-long __shim_close (long);
-long __shim_stat (long, long);
-long __shim_fstat (long, long);
-long __shim_lstat (long, long);
-long __shim_poll (long, long, long);
-long __shim_lseek (long, long, long);
-long __shim_mmap (long, long, long, long, long, long);
-long __shim_mprotect (long, long, long);
-long __shim_munmap (long, long);
-long __shim_brk (long);
-long __shim_rt_sigaction (long, long, long, long);
-long __shim_rt_sigprocmask (long, long, long);
-long __shim_rt_sigreturn (long);
-long __shim_ioctl (long, long, long);
-long __shim_pread64 (long, long, long, long);
-long __shim_pwrite64 (long, long, long, long);
-long __shim_readv (long, long, long);
-long __shim_writev (long, long, long);
-long __shim_access (long, long);
-long __shim_pipe (long);
-long __shim_select (long, long, long, long, long);
-long __shim_sched_yield (void);
-long __shim_mremap (long, long, long, long, long);
-long __shim_msync (long, long, long);
-long __shim_mincore (long, long, long);
-long __shim_madvise (long, long, long);
-long __shim_shmget (long, long, long);
-long __shim_shmat (long, long, long);
-long __shim_shmctl (long, long, long);
-long __shim_dup (long);
-long __shim_dup2 (long, long);
-long __shim_pause (void);
-long __shim_nanosleep (long, long);
-long __shim_getitimer (long, long);
-long __shim_alarm (long);
-long __shim_setitimer (long, long, long);
-long __shim_getpid (void);
-long __shim_sendfile (long, long, long, long);
-long __shim_socket (long, long, long);
-long __shim_connect (long, long, long);
-long __shim_accept (long, long, long);
-long __shim_sendto (long, long, long, long, long, long);
-long __shim_recvfrom (long, long, long, long, long, long);
-long __shim_sendmsg (long, long, long);
-long __shim_recvmsg (long, long, long);
-long __shim_shutdown (long, long);
-long __shim_bind (long, long, long);
-long __shim_listen (long, long);
-long __shim_getsockname (long, long, long);
-long __shim_getpeername (long, long, long);
-long __shim_socketpair (long, long, long, long);
-long __shim_setsockopt (long, long, long, long, long);
-long __shim_getsockopt (long, long, long, long, long);
-long __shim_clone (long, long, long, long, long);
-long __shim_fork (void);
-long __shim_vfork (void);
-long __shim_execve (long, long, long);
-long __shim_exit (long);
-long __shim_wait4 (long, long, long, long);
-long __shim_kill (long, long);
-long __shim_uname (long);
-long __shim_semget (long, long, long);
-long __shim_semop (long, long, long);
-long __shim_semctl (long, long, long, long);
-long __shim_shmdt (long);
-long __shim_msgget (long, long);
-long __shim_msgsnd (long, long, long, long);
-long __shim_msgrcv (long, long, long, long, long);
-long __shim_msgctl (long, long, long);
-long __shim_fcntl (long, long, long);
-long __shim_flock (long, long);
-long __shim_fsync (long);
-long __shim_fdatasync (long);
-long __shim_truncate (long, long);
-long __shim_ftruncate (long, long);
-long __shim_getdents (long, long, long);
-long __shim_getcwd (long, long);
-long __shim_chdir (long);
-long __shim_fchdir (long);
-long __shim_rename (long, long);
-long __shim_mkdir (long, long);
-long __shim_rmdir (long);
-long __shim_creat (long, long);
-long __shim_link (long, long);
-long __shim_unlink (long);
-long __shim_symlink (long, long);
-long __shim_readlink (long, long, long);
-long __shim_chmod (long, long);
-long __shim_fchmod (long, long);
-long __shim_chown (long, long, long);
-long __shim_fchown (long, long, long);
-long __shim_lchown (long, long, long);
-long __shim_umask (long);
-long __shim_gettimeofday (long, long);
-long __shim_getrlimit (long, long);
-long __shim_getrusage (long, long);
-long __shim_sysinfo (long);
-long __shim_times (long);
-long __shim_ptrace (long, long, long, long);
-long __shim_getuid (void);
-long __shim_syslog (long, long, long);
-long __shim_getgid (void);
-long __shim_setuid (long);
-long __shim_setgid (long);
-long __shim_geteuid (void);
-long __shim_getegid (void);
-long __shim_setpgid (long, long);
-long __shim_getppid (void);
-long __shim_getpgrp (void);
-long __shim_setsid (void);
-long __shim_setreuid (long, long);
-long __shim_setregid (long, long);
-long __shim_getgroups (long, long);
-long __shim_setgroups (long, long);
-long __shim_setresuid (long, long, long);
-long __shim_getresuid (long, long, long);
-long __shim_setresgid (long, long, long);
-long __shim_getresgid (long, long, long);
-long __shim_getpgid (long);
-long __shim_setfsuid (long);
-long __shim_setfsgid (long);
-long __shim_getsid (long);
-long __shim_capget (long, long);
-long __shim_capset (long, long);
-long __shim_rt_sigpending (long, long);
-long __shim_rt_sigtimedwait (long, long, long, long);
-long __shim_rt_sigqueueinfo (long, long, long);
-long __shim_rt_sigsuspend (long);
-long __shim_sigaltstack (long, long);
-long __shim_utime (long, long);
-long __shim_mknod (long, long, long);
-long __shim_uselib (long);
-long __shim_personality (long);
-long __shim_ustat (long, long);
-long __shim_statfs (long, long);
-long __shim_fstatfs (long, long);
-long __shim_sysfs (long, long, long);
-long __shim_getpriority (long, long);
-long __shim_setpriority (long, long, long);
-long __shim_sched_setparam (long, long);
-long __shim_sched_getparam (long, long);
-long __shim_sched_setscheduler (long, long, long);
-long __shim_sched_getscheduler (long);
-long __shim_sched_get_priority_max (long);
-long __shim_sched_get_priority_min (long);
-long __shim_sched_rr_get_interval (long, long);
-long __shim_mlock (long, long);
-long __shim_munlock (long, long);
-long __shim_mlockall (long);
-long __shim_munlockall (void);
-long __shim_vhangup (void);
-long __shim_modify_ldt (long, long, long);
-long __shim_pivot_root (long, long);
-long __shim__sysctl (long);
-long __shim_prctl (long, long, long, long, long);
-long __shim_arch_prctl (long, long);
-long __shim_adjtimex (long);
-long __shim_setrlimit (long, long);
-long __shim_chroot (long);
-long __shim_sync (void);
-long __shim_acct (long);
-long __shim_settimeofday (long, long);
-long __shim_mount (long, long, long, long, long);
-long __shim_umount2 (long, long);
-long __shim_swapon (long, long);
-long __shim_swapoff (long);
-long __shim_reboot (long, long, long, long);
-long __shim_sethostname (long, long);
-long __shim_setdomainname (long, long);
-long __shim_iopl (long);
-long __shim_ioperm (long, long, long);
-long __shim_create_module (long, long);
-long __shim_init_module (long, long, long);
-long __shim_delete_module (long, long);
-long __shim_get_kernel_syms (long);
-long __shim_query_module (long, long, long, long, long);
-long __shim_quotactl (long, long, long, long);
-long __shim_nfsservctl (long, long, long);
-long __shim_gettid (void);
-long __shim_readahead (long, long, long);
-long __shim_setxattr (long, long, long, long, long);
-long __shim_lsetxattr (long, long, long, long, long);
-long __shim_fsetxattr (long, long, long, long, long);
-long __shim_getxattr (long, long, long, long);
-long __shim_lgetxattr (long, long, long, long);
-long __shim_fgetxattr (long, long, long, long);
-long __shim_listxattr (long, long, long);
-long __shim_llistxattr (long, long, long);
-long __shim_flistxattr (long, long, long);
-long __shim_removexattr (long, long);
-long __shim_lremovexattr (long, long);
-long __shim_fremovexattr (long, long);
-long __shim_tkill (long, long);
-long __shim_time (long);
-long __shim_futex (long, long, long, long, long, long);
-long __shim_sched_setaffinity (long, long, long);
-long __shim_sched_getaffinity (long, long, long);
-long __shim_set_thread_area (long);
-long __shim_io_setup (long, long);
-long __shim_io_destroy (long);
-long __shim_io_getevents (long, long, long, long, long);
-long __shim_io_submit (long, long, long);
-long __shim_io_cancel (long, long, long);
-long __shim_get_thread_area (long);
-long __shim_lookup_dcookie (long, long, long);
-long __shim_epoll_create (long);
-long __shim_remap_file_pages (long, long, long, long, long);
-long __shim_getdents64 (long, long, long);
-long __shim_set_tid_address (long);
-long __shim_restart_syscall (void);
-long __shim_semtimedop (long, long, long, long);
-long __shim_fadvise64 (long, long, long, long);
-long __shim_timer_create (long, long, long);
-long __shim_timer_settime (long, long, long, long);
-long __shim_timer_gettime (long, long);
-long __shim_timer_getoverrun (long);
-long __shim_timer_delete (long);
-long __shim_clock_settime (long, long);
-long __shim_clock_gettime (long, long);
-long __shim_clock_getres (long, long);
-long __shim_clock_nanosleep (long, long, long, long);
-long __shim_exit_group (long);
-long __shim_epoll_wait (long, long, long, long);
-long __shim_epoll_ctl (long, long, long, long);
-long __shim_tgkill (long, long, long);
-long __shim_utimes (long, long);
-long __shim_mbind (long, long, long, long, long, long);
-long __shim_set_mempolicy (long, long, long);
-long __shim_get_mempolicy (long, long, long, long, long);
-long __shim_mq_open (long, long, long, long);
-long __shim_mq_unlink (long);
-long __shim_mq_timedsend (long, long, long, long, long);
-long __shim_mq_timedreceive (long, long, long, long, long);
-long __shim_mq_notify (long, long);
-long __shim_mq_getsetattr (long, long, long);
-long __shim_kexec_load (long, long, long, long);
-long __shim_waitid (long, long, long, long, long);
-long __shim_add_key (long, long, long, long, long);
-long __shim_request_key (long, long, long, long);
-long __shim_keyctl (long, long, long, long, long);
-long __shim_ioprio_set (long, long, long);
-long __shim_ioprio_get (long, long);
-long __shim_inotify_init (void);
-long __shim_inotify_add_watch (long, long, long);
-long __shim_inotify_rm_watch (long, long);
-long __shim_migrate_pages (long, long, long, long);
-long __shim_openat (long, long, long, long);
-long __shim_mkdirat (long, long, long);
-long __shim_mknodat (long, long, long, long);
-long __shim_fchownat (long, long, long, long, long);
-long __shim_futimesat (long, long, long);
-long __shim_newfstatat (long, long, long, long);
-long __shim_unlinkat (long, long, long);
-long __shim_renameat (long, long, long, long);
-long __shim_linkat (long, long, long, long, long);
-long __shim_symlinkat (long, long, long);
-long __shim_readlinkat (long, long, long, long);
-long __shim_fchmodat (long, long, long);
-long __shim_faccessat (long, long, long);
-long __shim_pselect6 (long, long, long, long, long, long);
-long __shim_ppoll (long, long, long, long, long);
-long __shim_unshare (long);
-long __shim_set_robust_list (long, long);
-long __shim_get_robust_list (long, long, long);
-long __shim_splice (long, long, long, long, long, long);
-long __shim_tee (long, long, long, long);
-long __shim_sync_file_range (long, long, long, long);
-long __shim_vmsplice (long, long, long, long);
-long __shim_move_pages (long, long, long, long, long, long);
-long __shim_utimensat (long, long, long, long);
-long __shim_epoll_pwait (long, long, long, long, long, long);
-long __shim_signalfd (long, long, long);
-long __shim_timerfd_create (long, long);
-long __shim_eventfd (long);
-long __shim_fallocate (long, long, long, long);
-long __shim_timerfd_settime (long, long, long, long);
-long __shim_timerfd_gettime (long, long);
-long __shim_accept4 (long, long, long, long);
-long __shim_signalfd4 (long, long, long, long);
-long __shim_eventfd2 (long, long);
-long __shim_epoll_create1 (long);
-long __shim_dup3 (long, long, long);
-long __shim_pipe2 (long, long);
-long __shim_inotify_init1 (long);
-long __shim_preadv (long, long, long, long, long);
-long __shim_pwritev (long, long, long, long, long);
-long __shim_rt_tgsigqueueinfo (long, long, long, long);
-long __shim_perf_event_open (long, long, long, long, long);
-long __shim_recvmmsg (long, long, long, long, long);
-long __shim_fanotify_init (long, long);
-long __shim_fanotify_mark (long, long, long, long, long);
-long __shim_prlimit64 (long, long, long, long);
-long __shim_name_to_handle_at (long, long, long, long, long);
-long __shim_open_by_handle_at (long, long, long);
-long __shim_clock_adjtime (long, long);
-long __shim_syncfs (long);
-long __shim_sendmmsg (long, long, long, long);
-long __shim_setns (long, long);
-long __shim_getcpu (long, long, long);
+long __shim_read(long, long, long);
+long __shim_write(long, long, long);
+long __shim_open(long, long, long);
+long __shim_close(long);
+long __shim_stat(long, long);
+long __shim_fstat(long, long);
+long __shim_lstat(long, long);
+long __shim_poll(long, long, long);
+long __shim_lseek(long, long, long);
+long __shim_mmap(long, long, long, long, long, long);
+long __shim_mprotect(long, long, long);
+long __shim_munmap(long, long);
+long __shim_brk(long);
+long __shim_rt_sigaction(long, long, long, long);
+long __shim_rt_sigprocmask(long, long, long);
+long __shim_rt_sigreturn(long);
+long __shim_ioctl(long, long, long);
+long __shim_pread64(long, long, long, long);
+long __shim_pwrite64(long, long, long, long);
+long __shim_readv(long, long, long);
+long __shim_writev(long, long, long);
+long __shim_access(long, long);
+long __shim_pipe(long);
+long __shim_select(long, long, long, long, long);
+long __shim_sched_yield(void);
+long __shim_mremap(long, long, long, long, long);
+long __shim_msync(long, long, long);
+long __shim_mincore(long, long, long);
+long __shim_madvise(long, long, long);
+long __shim_shmget(long, long, long);
+long __shim_shmat(long, long, long);
+long __shim_shmctl(long, long, long);
+long __shim_dup(long);
+long __shim_dup2(long, long);
+long __shim_pause(void);
+long __shim_nanosleep(long, long);
+long __shim_getitimer(long, long);
+long __shim_alarm(long);
+long __shim_setitimer(long, long, long);
+long __shim_getpid(void);
+long __shim_sendfile(long, long, long, long);
+long __shim_socket(long, long, long);
+long __shim_connect(long, long, long);
+long __shim_accept(long, long, long);
+long __shim_sendto(long, long, long, long, long, long);
+long __shim_recvfrom(long, long, long, long, long, long);
+long __shim_sendmsg(long, long, long);
+long __shim_recvmsg(long, long, long);
+long __shim_shutdown(long, long);
+long __shim_bind(long, long, long);
+long __shim_listen(long, long);
+long __shim_getsockname(long, long, long);
+long __shim_getpeername(long, long, long);
+long __shim_socketpair(long, long, long, long);
+long __shim_setsockopt(long, long, long, long, long);
+long __shim_getsockopt(long, long, long, long, long);
+long __shim_clone(long, long, long, long, long);
+long __shim_fork(void);
+long __shim_vfork(void);
+long __shim_execve(long, long, long);
+long __shim_exit(long);
+long __shim_wait4(long, long, long, long);
+long __shim_kill(long, long);
+long __shim_uname(long);
+long __shim_semget(long, long, long);
+long __shim_semop(long, long, long);
+long __shim_semctl(long, long, long, long);
+long __shim_shmdt(long);
+long __shim_msgget(long, long);
+long __shim_msgsnd(long, long, long, long);
+long __shim_msgrcv(long, long, long, long, long);
+long __shim_msgctl(long, long, long);
+long __shim_fcntl(long, long, long);
+long __shim_flock(long, long);
+long __shim_fsync(long);
+long __shim_fdatasync(long);
+long __shim_truncate(long, long);
+long __shim_ftruncate(long, long);
+long __shim_getdents(long, long, long);
+long __shim_getcwd(long, long);
+long __shim_chdir(long);
+long __shim_fchdir(long);
+long __shim_rename(long, long);
+long __shim_mkdir(long, long);
+long __shim_rmdir(long);
+long __shim_creat(long, long);
+long __shim_link(long, long);
+long __shim_unlink(long);
+long __shim_symlink(long, long);
+long __shim_readlink(long, long, long);
+long __shim_chmod(long, long);
+long __shim_fchmod(long, long);
+long __shim_chown(long, long, long);
+long __shim_fchown(long, long, long);
+long __shim_lchown(long, long, long);
+long __shim_umask(long);
+long __shim_gettimeofday(long, long);
+long __shim_getrlimit(long, long);
+long __shim_getrusage(long, long);
+long __shim_sysinfo(long);
+long __shim_times(long);
+long __shim_ptrace(long, long, long, long);
+long __shim_getuid(void);
+long __shim_syslog(long, long, long);
+long __shim_getgid(void);
+long __shim_setuid(long);
+long __shim_setgid(long);
+long __shim_geteuid(void);
+long __shim_getegid(void);
+long __shim_setpgid(long, long);
+long __shim_getppid(void);
+long __shim_getpgrp(void);
+long __shim_setsid(void);
+long __shim_setreuid(long, long);
+long __shim_setregid(long, long);
+long __shim_getgroups(long, long);
+long __shim_setgroups(long, long);
+long __shim_setresuid(long, long, long);
+long __shim_getresuid(long, long, long);
+long __shim_setresgid(long, long, long);
+long __shim_getresgid(long, long, long);
+long __shim_getpgid(long);
+long __shim_setfsuid(long);
+long __shim_setfsgid(long);
+long __shim_getsid(long);
+long __shim_capget(long, long);
+long __shim_capset(long, long);
+long __shim_rt_sigpending(long, long);
+long __shim_rt_sigtimedwait(long, long, long, long);
+long __shim_rt_sigqueueinfo(long, long, long);
+long __shim_rt_sigsuspend(long);
+long __shim_sigaltstack(long, long);
+long __shim_utime(long, long);
+long __shim_mknod(long, long, long);
+long __shim_uselib(long);
+long __shim_personality(long);
+long __shim_ustat(long, long);
+long __shim_statfs(long, long);
+long __shim_fstatfs(long, long);
+long __shim_sysfs(long, long, long);
+long __shim_getpriority(long, long);
+long __shim_setpriority(long, long, long);
+long __shim_sched_setparam(long, long);
+long __shim_sched_getparam(long, long);
+long __shim_sched_setscheduler(long, long, long);
+long __shim_sched_getscheduler(long);
+long __shim_sched_get_priority_max(long);
+long __shim_sched_get_priority_min(long);
+long __shim_sched_rr_get_interval(long, long);
+long __shim_mlock(long, long);
+long __shim_munlock(long, long);
+long __shim_mlockall(long);
+long __shim_munlockall(void);
+long __shim_vhangup(void);
+long __shim_modify_ldt(long, long, long);
+long __shim_pivot_root(long, long);
+long __shim__sysctl(long);
+long __shim_prctl(long, long, long, long, long);
+long __shim_arch_prctl(long, long);
+long __shim_adjtimex(long);
+long __shim_setrlimit(long, long);
+long __shim_chroot(long);
+long __shim_sync(void);
+long __shim_acct(long);
+long __shim_settimeofday(long, long);
+long __shim_mount(long, long, long, long, long);
+long __shim_umount2(long, long);
+long __shim_swapon(long, long);
+long __shim_swapoff(long);
+long __shim_reboot(long, long, long, long);
+long __shim_sethostname(long, long);
+long __shim_setdomainname(long, long);
+long __shim_iopl(long);
+long __shim_ioperm(long, long, long);
+long __shim_create_module(long, long);
+long __shim_init_module(long, long, long);
+long __shim_delete_module(long, long);
+long __shim_get_kernel_syms(long);
+long __shim_query_module(long, long, long, long, long);
+long __shim_quotactl(long, long, long, long);
+long __shim_nfsservctl(long, long, long);
+long __shim_gettid(void);
+long __shim_readahead(long, long, long);
+long __shim_setxattr(long, long, long, long, long);
+long __shim_lsetxattr(long, long, long, long, long);
+long __shim_fsetxattr(long, long, long, long, long);
+long __shim_getxattr(long, long, long, long);
+long __shim_lgetxattr(long, long, long, long);
+long __shim_fgetxattr(long, long, long, long);
+long __shim_listxattr(long, long, long);
+long __shim_llistxattr(long, long, long);
+long __shim_flistxattr(long, long, long);
+long __shim_removexattr(long, long);
+long __shim_lremovexattr(long, long);
+long __shim_fremovexattr(long, long);
+long __shim_tkill(long, long);
+long __shim_time(long);
+long __shim_futex(long, long, long, long, long, long);
+long __shim_sched_setaffinity(long, long, long);
+long __shim_sched_getaffinity(long, long, long);
+long __shim_set_thread_area(long);
+long __shim_io_setup(long, long);
+long __shim_io_destroy(long);
+long __shim_io_getevents(long, long, long, long, long);
+long __shim_io_submit(long, long, long);
+long __shim_io_cancel(long, long, long);
+long __shim_get_thread_area(long);
+long __shim_lookup_dcookie(long, long, long);
+long __shim_epoll_create(long);
+long __shim_remap_file_pages(long, long, long, long, long);
+long __shim_getdents64(long, long, long);
+long __shim_set_tid_address(long);
+long __shim_restart_syscall(void);
+long __shim_semtimedop(long, long, long, long);
+long __shim_fadvise64(long, long, long, long);
+long __shim_timer_create(long, long, long);
+long __shim_timer_settime(long, long, long, long);
+long __shim_timer_gettime(long, long);
+long __shim_timer_getoverrun(long);
+long __shim_timer_delete(long);
+long __shim_clock_settime(long, long);
+long __shim_clock_gettime(long, long);
+long __shim_clock_getres(long, long);
+long __shim_clock_nanosleep(long, long, long, long);
+long __shim_exit_group(long);
+long __shim_epoll_wait(long, long, long, long);
+long __shim_epoll_ctl(long, long, long, long);
+long __shim_tgkill(long, long, long);
+long __shim_utimes(long, long);
+long __shim_mbind(long, long, long, long, long, long);
+long __shim_set_mempolicy(long, long, long);
+long __shim_get_mempolicy(long, long, long, long, long);
+long __shim_mq_open(long, long, long, long);
+long __shim_mq_unlink(long);
+long __shim_mq_timedsend(long, long, long, long, long);
+long __shim_mq_timedreceive(long, long, long, long, long);
+long __shim_mq_notify(long, long);
+long __shim_mq_getsetattr(long, long, long);
+long __shim_kexec_load(long, long, long, long);
+long __shim_waitid(long, long, long, long, long);
+long __shim_add_key(long, long, long, long, long);
+long __shim_request_key(long, long, long, long);
+long __shim_keyctl(long, long, long, long, long);
+long __shim_ioprio_set(long, long, long);
+long __shim_ioprio_get(long, long);
+long __shim_inotify_init(void);
+long __shim_inotify_add_watch(long, long, long);
+long __shim_inotify_rm_watch(long, long);
+long __shim_migrate_pages(long, long, long, long);
+long __shim_openat(long, long, long, long);
+long __shim_mkdirat(long, long, long);
+long __shim_mknodat(long, long, long, long);
+long __shim_fchownat(long, long, long, long, long);
+long __shim_futimesat(long, long, long);
+long __shim_newfstatat(long, long, long, long);
+long __shim_unlinkat(long, long, long);
+long __shim_renameat(long, long, long, long);
+long __shim_linkat(long, long, long, long, long);
+long __shim_symlinkat(long, long, long);
+long __shim_readlinkat(long, long, long, long);
+long __shim_fchmodat(long, long, long);
+long __shim_faccessat(long, long, long);
+long __shim_pselect6(long, long, long, long, long, long);
+long __shim_ppoll(long, long, long, long, long);
+long __shim_unshare(long);
+long __shim_set_robust_list(long, long);
+long __shim_get_robust_list(long, long, long);
+long __shim_splice(long, long, long, long, long, long);
+long __shim_tee(long, long, long, long);
+long __shim_sync_file_range(long, long, long, long);
+long __shim_vmsplice(long, long, long, long);
+long __shim_move_pages(long, long, long, long, long, long);
+long __shim_utimensat(long, long, long, long);
+long __shim_epoll_pwait(long, long, long, long, long, long);
+long __shim_signalfd(long, long, long);
+long __shim_timerfd_create(long, long);
+long __shim_eventfd(long);
+long __shim_fallocate(long, long, long, long);
+long __shim_timerfd_settime(long, long, long, long);
+long __shim_timerfd_gettime(long, long);
+long __shim_accept4(long, long, long, long);
+long __shim_signalfd4(long, long, long, long);
+long __shim_eventfd2(long, long);
+long __shim_epoll_create1(long);
+long __shim_dup3(long, long, long);
+long __shim_pipe2(long, long);
+long __shim_inotify_init1(long);
+long __shim_preadv(long, long, long, long, long);
+long __shim_pwritev(long, long, long, long, long);
+long __shim_rt_tgsigqueueinfo(long, long, long, long);
+long __shim_perf_event_open(long, long, long, long, long);
+long __shim_recvmmsg(long, long, long, long, long);
+long __shim_fanotify_init(long, long);
+long __shim_fanotify_mark(long, long, long, long, long);
+long __shim_prlimit64(long, long, long, long);
+long __shim_name_to_handle_at(long, long, long, long, long);
+long __shim_open_by_handle_at(long, long, long);
+long __shim_clock_adjtime(long, long);
+long __shim_syncfs(long);
+long __shim_sendmmsg(long, long, long, long);
+long __shim_setns(long, long);
+long __shim_getcpu(long, long, long);
 
 /* libos call entries */
-long __shim_msgpersist (long, long);
-long __shim_benchmark_rpc (long, long, long, long);
-long __shim_send_rpc (long, long, long);
-long __shim_recv_rpc (long, long, long);
+long __shim_msgpersist(long, long);
+long __shim_benchmark_rpc(long, long, long, long);
+long __shim_send_rpc(long, long, long);
+long __shim_recv_rpc(long, long, long);
 long __shim_checkpoint(long);
 
 /* syscall implementation */
-size_t shim_do_read (int fd, void * buf, size_t count);
-size_t shim_do_write (int fd, const void * buf, size_t count);
-int shim_do_open (const char * file, int flags, mode_t mode);
-int shim_do_close (int fd);
-int shim_do_stat (const char * file, struct stat * statbuf);
-int shim_do_fstat (int fd, struct stat * statbuf);
-int shim_do_lstat (const char * file, struct stat * stat);
-int shim_do_poll (struct pollfd * fds, nfds_t nfds, int timeout);
-off_t shim_do_lseek (int fd, off_t offset, int origin);
-void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
-                     off_t offset);
-int shim_do_mprotect (void * addr, size_t len, int prot);
-int shim_do_munmap (void * addr, size_t len);
-void * shim_do_brk (void * brk);
-int shim_do_sigaction (int signum, const struct __kernel_sigaction * act,
-                       struct __kernel_sigaction * oldact, size_t sigsetsize);
-int shim_do_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset);
-int shim_do_sigreturn (int __unused);
-int shim_do_ioctl (int fd, int cmd, unsigned long arg);
-ssize_t shim_do_pread64 (int fd, char * buf, size_t count, loff_t pos);
-ssize_t shim_do_pwrite64 (int fd, char * buf,  size_t count, loff_t pos);
-ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen);
-ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen);
-int shim_do_access (const char * file, mode_t mode);
-int shim_do_pipe (int * fildes);
-int shim_do_select (int nfds, fd_set * readfds, fd_set * writefds,
-                    fd_set * errorfds, struct __kernel_timeval * timeout);
-int shim_do_sched_yield (void);
-void * shim_do_mremap (void * addr, size_t old_len, size_t new_len,
-                       int flags, void * new_addr);
-int shim_do_msync (void * start, size_t len, int flags);
-int shim_do_mincore (void * start, size_t len, unsigned char * vec);
-int shim_do_dup (int fd);
-int shim_do_dup2 (int oldfd, int newfd);
-int shim_do_pause (void);
-int shim_do_nanosleep (const struct __kernel_timespec * rqtp,
-                       struct __kernel_timespec * rmtp);
-int shim_do_getitimer (int which, struct __kernel_itimerval * value);
-int shim_do_alarm (unsigned int seconds);
-int shim_do_setitimer (int which, struct __kernel_itimerval * value,
-                       struct __kernel_itimerval * ovalue);
-pid_t shim_do_getpid (void);
-ssize_t shim_do_sendfile (int out_fd, int in_fd, off_t * offset, size_t count);
-int shim_do_socket (int family, int type, int protocol);
-int shim_do_connect (int sockfd, struct sockaddr * addr, int addrlen);
-int shim_do_accept (int fd, struct sockaddr * addr, socklen_t * addrlen);
-ssize_t shim_do_sendto (int fd, const void * buf, size_t len, int flags,
-                        const struct sockaddr * dest_addr, socklen_t addrlen);
-ssize_t shim_do_recvfrom (int fd, void * buf, size_t len, int flags,
-                          struct sockaddr * addr, socklen_t * addrlen);
-int shim_do_bind (int sockfd, struct sockaddr * addr, socklen_t addrlen);
-int shim_do_listen (int sockfd, int backlog);
-ssize_t shim_do_sendmsg (int fd, struct msghdr * msg, int flags);
-ssize_t shim_do_recvmsg (int fd, struct msghdr * msg, int flags);
-int shim_do_shutdown (int sockfd, int how);
-int shim_do_getsockname (int sockfd, struct sockaddr * addr, int * addrlen);
-int shim_do_getpeername (int sockfd, struct sockaddr * addr, int * addrlen);
-int shim_do_socketpair (int domain, int type, int protocol, int * sv);
-int shim_do_setsockopt (int fd, int level, int optname, char * optval,
-                        int optlen);
-int shim_do_getsockopt (int fd, int level, int optname, char * optval,
-                        int * optlen);
-int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
-                   int * child_tidptr, void * tls);
-int shim_do_fork (void);
-int shim_do_vfork (void);
-int shim_do_execve (const char * file, const char ** argv, const char ** envp);
-noreturn int shim_do_exit (int error_code);
-pid_t shim_do_wait4 (pid_t pid, int * stat_addr, int option,
-                     struct __kernel_rusage * ru);
-int shim_do_kill (pid_t pid, int sig);
-int shim_do_uname (struct old_utsname * buf);
-int shim_do_semget (key_t key, int nsems, int semflg);
-int shim_do_semop (int semid, struct sembuf * sops, unsigned int nsops);
-int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg);
-int shim_do_msgget (key_t key, int msgflg);
-int shim_do_msgsnd ( int msqid, const void * msgp, size_t msgsz, int msgflg);
-int shim_do_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp,
-                    int msgflg);
-int shim_do_msgctl (int msqid, int cmd, struct msqid_ds * buf);
-int shim_do_fcntl (int fd, int cmd, unsigned long arg);
-int shim_do_fsync (int fd);
-int shim_do_fdatasync (int fd);
-int shim_do_truncate (const char * path, loff_t length);
-int shim_do_ftruncate (int fd, loff_t length);
-size_t shim_do_getdents (int fd, struct linux_dirent * buf, size_t count);
-int shim_do_getcwd (char *buf, size_t size);
-int shim_do_chdir (const char * filename);
-int shim_do_fchdir (int fd);
-int shim_do_rename (const char * oldname, const char * newname);
-int shim_do_mkdir (const char * pathname, int mode);
-int shim_do_rmdir (const char * pathname);
-int shim_do_creat (const char * path, mode_t mode);
-int shim_do_unlink (const char * file);
-int shim_do_readlink (const char * file, char * buf, size_t bufsize);
-int shim_do_chmod (const char * filename, mode_t mode);
-int shim_do_fchmod (int fd, mode_t mode);
-int shim_do_chown (const char * filename, uid_t user, gid_t group);
-int shim_do_fchown (int fd, uid_t user, gid_t group);
-mode_t shim_do_umask (mode_t mask);
-int shim_do_gettimeofday (struct __kernel_timeval * tv,
-                          struct __kernel_timezone * tz);
-int shim_do_getrlimit (int resource, struct __kernel_rlimit * rlim);
-uid_t shim_do_getuid (void);
-gid_t shim_do_getgid (void);
-int shim_do_setuid (uid_t uid);
-int shim_do_setgid (gid_t gid);
-int shim_do_setgroups (int gidsetsize, gid_t * grouplist);
-int shim_do_getgroups (int gidsetsize, gid_t * grouplist);
-uid_t shim_do_geteuid (void);
-gid_t shim_do_getegid (void);
-pid_t shim_do_getppid (void);
-int shim_do_setpgid (pid_t pid, pid_t pgid);
-pid_t shim_do_getpgrp (void);
-int shim_do_setsid (void);
-int shim_do_getpgid (pid_t pid);
-int shim_do_getsid (pid_t pid);
-int shim_do_sigpending (__sigset_t * set, size_t sigsetsize);
-int shim_do_sigaltstack (const stack_t * ss, stack_t * oss);
-int shim_do_sigsuspend (const __sigset_t * mask);
-void * shim_do_arch_prctl (int code, void * addr);
-int shim_do_setrlimit (int resource, struct __kernel_rlimit * rlim);
-int shim_do_chroot (const char * filename);
-pid_t shim_do_gettid (void);
-int shim_do_tkill (int pid, int sig);
-time_t shim_do_time (time_t * tloc);
-int shim_do_futex (int * uaddr, int op, int val, void * utime,
-                   int * uaddr2, int val3);
-int shim_do_sched_getaffinity (pid_t pid, size_t len,
-                               __kernel_cpu_set_t * user_mask_ptr);
-int shim_do_set_tid_address (int * tidptr);
-int shim_do_semtimedop (int semid, struct sembuf * sops, unsigned int nsops,
-                        const struct timespec * timeout);
-int shim_do_epoll_create (int size);
-size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count);
-int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
-                        int maxevents, int timeout_ms);
-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);
-noreturn 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);
-int shim_do_mkdirat (int dfd, const char * pathname, int mode);
-int shim_do_newfstatat (int dirfd, const char* pathname,
-                        struct stat* statbuf, int flags);
-int shim_do_unlinkat (int dfd, const char * pathname, int flag);
-int shim_do_renameat (int olddfd, const char * pathname, int newdfd,
-                      const char * newname);
-int shim_do_fchmodat (int dfd, const char * filename, mode_t mode);
-int shim_do_fchownat (int dfd, const char * filename, uid_t user, gid_t group,
-                      int flags);
-int shim_do_faccessat (int dfd, const char * filename, mode_t mode);
-int shim_do_pselect6 (int nfds, fd_set * readfds, fd_set * writefds,
-                      fd_set * exceptfds, const struct __kernel_timespec * tsp,
-                      const __sigset_t * sigmask);
-int shim_do_ppoll (struct pollfd * fds, int nfds, struct timespec * tsp,
-                   const __sigset_t * sigmask, size_t sigsetsize);
-int shim_do_set_robust_list (struct robust_list_head * head, size_t len);
-int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
-                             size_t * len);
-int shim_do_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
-                         int maxevents, int timeout_ms, const __sigset_t * sigmask,
-                         size_t sigsetsize);
-int shim_do_accept4 (int sockfd, struct sockaddr * addr, socklen_t * addrlen,
-                     int flags);
-int shim_do_dup3 (int oldfd, int newfd, int flags);
-int shim_do_epoll_create1 (int flags);
-int shim_do_pipe2 (int * fildes, int flags);
-ssize_t shim_do_recvmmsg (int sockfd, struct mmsghdr * msg, size_t vlen, int flags,
-                          struct __kernel_timespec * timeout);
+size_t shim_do_read(int fd, void* buf, size_t count);
+size_t shim_do_write(int fd, const void* buf, size_t count);
+int shim_do_open(const char* file, int flags, mode_t mode);
+int shim_do_close(int fd);
+int shim_do_stat(const char* file, struct stat* statbuf);
+int shim_do_fstat(int fd, struct stat* statbuf);
+int shim_do_lstat(const char* file, struct stat* stat);
+int shim_do_poll(struct pollfd* fds, nfds_t nfds, int timeout);
+off_t shim_do_lseek(int fd, off_t offset, int origin);
+void* shim_do_mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset);
+int shim_do_mprotect(void* addr, size_t len, int prot);
+int shim_do_munmap(void* addr, size_t len);
+void* shim_do_brk(void* brk);
+int shim_do_sigaction(int signum, const struct __kernel_sigaction* act,
+                      struct __kernel_sigaction* oldact, size_t sigsetsize);
+int shim_do_sigprocmask(int how, const __sigset_t* set, __sigset_t* oldset);
+int shim_do_sigreturn(int __unused);
+int shim_do_ioctl(int fd, int cmd, unsigned long arg);
+ssize_t shim_do_pread64(int fd, char* buf, size_t count, loff_t pos);
+ssize_t shim_do_pwrite64(int fd, char* buf, size_t count, loff_t pos);
+ssize_t shim_do_readv(int fd, const struct iovec* vec, int vlen);
+ssize_t shim_do_writev(int fd, const struct iovec* vec, int vlen);
+int shim_do_access(const char* file, mode_t mode);
+int shim_do_pipe(int* fildes);
+int shim_do_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
+                   struct __kernel_timeval* timeout);
+int shim_do_sched_yield(void);
+void* shim_do_mremap(void* addr, size_t old_len, size_t new_len, int flags, void* new_addr);
+int shim_do_msync(void* start, size_t len, int flags);
+int shim_do_mincore(void* start, size_t len, unsigned char* vec);
+int shim_do_dup(int fd);
+int shim_do_dup2(int oldfd, int newfd);
+int shim_do_pause(void);
+int shim_do_nanosleep(const struct __kernel_timespec* rqtp, struct __kernel_timespec* rmtp);
+int shim_do_getitimer(int which, struct __kernel_itimerval* value);
+int shim_do_alarm(unsigned int seconds);
+int shim_do_setitimer(int which, struct __kernel_itimerval* value,
+                      struct __kernel_itimerval* ovalue);
+pid_t shim_do_getpid(void);
+ssize_t shim_do_sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
+int shim_do_socket(int family, int type, int protocol);
+int shim_do_connect(int sockfd, struct sockaddr* addr, int addrlen);
+int shim_do_accept(int fd, struct sockaddr* addr, socklen_t* addrlen);
+ssize_t shim_do_sendto(int fd, const void* buf, size_t len, int flags,
+                       const struct sockaddr* dest_addr, socklen_t addrlen);
+ssize_t shim_do_recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* addr,
+                         socklen_t* addrlen);
+int shim_do_bind(int sockfd, struct sockaddr* addr, socklen_t addrlen);
+int shim_do_listen(int sockfd, int backlog);
+ssize_t shim_do_sendmsg(int fd, struct msghdr* msg, int flags);
+ssize_t shim_do_recvmsg(int fd, struct msghdr* msg, int flags);
+int shim_do_shutdown(int sockfd, int how);
+int shim_do_getsockname(int sockfd, struct sockaddr* addr, int* addrlen);
+int shim_do_getpeername(int sockfd, struct sockaddr* addr, int* addrlen);
+int shim_do_socketpair(int domain, int type, int protocol, int* sv);
+int shim_do_setsockopt(int fd, int level, int optname, char* optval, int optlen);
+int shim_do_getsockopt(int fd, int level, int optname, char* optval, int* optlen);
+int shim_do_clone(int flags, void* user_stack_addr, int* parent_tidptr, int* child_tidptr,
+                  void* tls);
+int shim_do_fork(void);
+int shim_do_vfork(void);
+int shim_do_execve(const char* file, const char** argv, const char** envp);
+noreturn int shim_do_exit(int error_code);
+pid_t shim_do_wait4(pid_t pid, int* stat_addr, int option, struct __kernel_rusage* ru);
+int shim_do_kill(pid_t pid, int sig);
+int shim_do_uname(struct old_utsname* buf);
+int shim_do_semget(key_t key, int nsems, int semflg);
+int shim_do_semop(int semid, struct sembuf* sops, unsigned int nsops);
+int shim_do_semctl(int semid, int semnum, int cmd, unsigned long arg);
+int shim_do_msgget(key_t key, int msgflg);
+int shim_do_msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg);
+int shim_do_msgrcv(int msqid, void* msgp, size_t msgsz, long msgtyp, int msgflg);
+int shim_do_msgctl(int msqid, int cmd, struct msqid_ds* buf);
+int shim_do_fcntl(int fd, int cmd, unsigned long arg);
+int shim_do_fsync(int fd);
+int shim_do_fdatasync(int fd);
+int shim_do_truncate(const char* path, loff_t length);
+int shim_do_ftruncate(int fd, loff_t length);
+size_t shim_do_getdents(int fd, struct linux_dirent* buf, size_t count);
+int shim_do_getcwd(char* buf, size_t size);
+int shim_do_chdir(const char* filename);
+int shim_do_fchdir(int fd);
+int shim_do_rename(const char* oldname, const char* newname);
+int shim_do_mkdir(const char* pathname, int mode);
+int shim_do_rmdir(const char* pathname);
+int shim_do_creat(const char* path, mode_t mode);
+int shim_do_unlink(const char* file);
+int shim_do_readlink(const char* file, char* buf, size_t bufsize);
+int shim_do_chmod(const char* filename, mode_t mode);
+int shim_do_fchmod(int fd, mode_t mode);
+int shim_do_chown(const char* filename, uid_t user, gid_t group);
+int shim_do_fchown(int fd, uid_t user, gid_t group);
+mode_t shim_do_umask(mode_t mask);
+int shim_do_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone* tz);
+int shim_do_getrlimit(int resource, struct __kernel_rlimit* rlim);
+uid_t shim_do_getuid(void);
+gid_t shim_do_getgid(void);
+int shim_do_setuid(uid_t uid);
+int shim_do_setgid(gid_t gid);
+int shim_do_setgroups(int gidsetsize, gid_t* grouplist);
+int shim_do_getgroups(int gidsetsize, gid_t* grouplist);
+uid_t shim_do_geteuid(void);
+gid_t shim_do_getegid(void);
+pid_t shim_do_getppid(void);
+int shim_do_setpgid(pid_t pid, pid_t pgid);
+pid_t shim_do_getpgrp(void);
+int shim_do_setsid(void);
+int shim_do_getpgid(pid_t pid);
+int shim_do_getsid(pid_t pid);
+int shim_do_sigpending(__sigset_t* set, size_t sigsetsize);
+int shim_do_sigaltstack(const stack_t* ss, stack_t* oss);
+int shim_do_sigsuspend(const __sigset_t* mask);
+void* shim_do_arch_prctl(int code, void* addr);
+int shim_do_setrlimit(int resource, struct __kernel_rlimit* rlim);
+int shim_do_chroot(const char* filename);
+pid_t shim_do_gettid(void);
+int shim_do_tkill(int pid, int sig);
+time_t shim_do_time(time_t* tloc);
+int shim_do_futex(int* uaddr, int op, int val, void* utime, int* uaddr2, int val3);
+int shim_do_sched_getaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr);
+int shim_do_set_tid_address(int* tidptr);
+int shim_do_semtimedop(int semid, struct sembuf* sops, unsigned int nsops,
+                       const struct timespec* timeout);
+int shim_do_epoll_create(int size);
+size_t shim_do_getdents64(int fd, struct linux_dirent64* buf, size_t count);
+int shim_do_epoll_wait(int epfd, struct __kernel_epoll_event* events, int maxevents,
+                       int timeout_ms);
+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);
+noreturn 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);
+int shim_do_mkdirat(int dfd, const char* pathname, int mode);
+int shim_do_newfstatat(int dirfd, const char* pathname, struct stat* statbuf, int flags);
+int shim_do_unlinkat(int dfd, const char* pathname, int flag);
+int shim_do_renameat(int olddfd, const char* pathname, int newdfd, const char* newname);
+int shim_do_fchmodat(int dfd, const char* filename, mode_t mode);
+int shim_do_fchownat(int dfd, const char* filename, uid_t user, gid_t group, int flags);
+int shim_do_faccessat(int dfd, const char* filename, mode_t mode);
+int shim_do_pselect6(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
+                     const struct __kernel_timespec* tsp, const __sigset_t* sigmask);
+int shim_do_ppoll(struct pollfd* fds, int nfds, struct timespec* tsp, const __sigset_t* sigmask,
+                  size_t sigsetsize);
+int shim_do_set_robust_list(struct robust_list_head* head, size_t len);
+int shim_do_get_robust_list(pid_t pid, struct robust_list_head** head, size_t* len);
+int shim_do_epoll_pwait(int epfd, struct __kernel_epoll_event* events, int maxevents,
+                        int timeout_ms, const __sigset_t* sigmask, size_t sigsetsize);
+int shim_do_accept4(int sockfd, struct sockaddr* addr, socklen_t* addrlen, int flags);
+int shim_do_dup3(int oldfd, int newfd, int flags);
+int shim_do_epoll_create1(int flags);
+int shim_do_pipe2(int* fildes, int flags);
+ssize_t shim_do_recvmmsg(int sockfd, struct mmsghdr* msg, size_t vlen, int flags,
+                         struct __kernel_timespec* timeout);
 int shim_do_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
                       struct __kernel_rlimit64* old_rlim);
-ssize_t shim_do_sendmmsg (int sockfd, struct mmsghdr * msg, size_t vlen, int flags);
+ssize_t shim_do_sendmmsg(int sockfd, struct mmsghdr* msg, size_t vlen, int flags);
 
 /* libos call implementation */
-int shim_do_msgpersist (int msqid, int cmd);
-int shim_do_benchmark_rpc (pid_t pid, int times, const void * buf, size_t size);
-size_t shim_do_send_rpc (pid_t pid, const void * buf, size_t size);
-size_t shim_do_recv_rpc (pid_t * pid, void * buf, size_t size);
-int shim_do_checkpoint(const char * filename);
+int shim_do_msgpersist(int msqid, int cmd);
+int shim_do_benchmark_rpc(pid_t pid, int times, const void* buf, size_t size);
+size_t shim_do_send_rpc(pid_t pid, const void* buf, size_t size);
+size_t shim_do_recv_rpc(pid_t* pid, void* buf, size_t size);
+int shim_do_checkpoint(const char* filename);
 
 #endif /* ! IN_SHIM */
 
 /* syscall wrappers */
-size_t shim_read (int fd, void * buf, size_t count);
-size_t shim_write (int fd, const void * buf, size_t count);
-int shim_open (const char * file, int flags, mode_t mode);
-int shim_close (int fd);
-int shim_stat (const char * file, struct stat * statbuf);
-int shim_fstat (int fd, struct stat * statbuf);
-int shim_lstat (const char * file, struct stat * statbuf);
-int shim_poll (struct pollfd * fds, nfds_t nfds, int timeout);
-off_t shim_lseek (int fd, off_t offset, int origin);
-void * shim_mmap (void * addr, size_t length, int prot, int flags, int fd,
-                  off_t offset);
-int shim_mprotect (void * addr, size_t len, int prot);
-int shim_munmap (void * addr, size_t len);
-void * shim_brk (void * brk);
-int shim_rt_sigaction (int signum, const struct __kernel_sigaction * act,
-                       struct __kernel_sigaction * oldact, size_t sigsetsize);
-int shim_rt_sigprocmask (int how, const __sigset_t * set, __sigset_t * oldset);
-int shim_rt_sigreturn (int __unused);
-int shim_ioctl (int fd, int cmd, unsigned long arg);
-size_t shim_pread64 (int fd, char * buf, size_t count, loff_t pos);
-size_t shim_pwrite64 (int fd, char * buf, size_t count, loff_t pos);
-ssize_t shim_readv (int fd, const struct iovec * vec, int vlen);
-ssize_t shim_writev (int fd, const struct iovec * vec, int vlen);
-int shim_access (const char * file, mode_t mode);
-int shim_pipe (int * fildes);
-int shim_select (int nfds, fd_set * readfds, fd_set * writefds,
-                 fd_set * errorfds, struct __kernel_timeval * timeout);
-int shim_sched_yield (void);
-void * shim_mremap (void * addr, size_t old_len, size_t new_len, int flags,
-                    void * new_addr);
-int shim_msync (void * start, size_t len, int flags);
-int shim_mincore (void * start, size_t len, unsigned char * vec);
-int shim_madvise (void * start, size_t len, int behavior);
-int shim_shmget (key_t key, size_t size, int shmflg);
-void * shim_shmat (int shmid, const void * shmaddr, int shmflg);
-int shim_shmctl (int shmid, int cmd, struct shmid_ds * buf);
-int shim_dup (int fd);
-int shim_dup2 (int oldfd, int newfd);
-int shim_pause (void);
-int shim_nanosleep (const struct __kernel_timespec * rqtp,
-                    struct __kernel_timespec * rmtp);
-int shim_getitimer (int which, struct __kernel_itimerval * value);
-int shim_alarm (unsigned int seconds);
-int shim_setitimer (int which, struct __kernel_itimerval * value,
-                    struct __kernel_itimerval * ovalue);
-pid_t shim_getpid (void);
-ssize_t shim_sendfile (int out_fd, int in_fd, off_t * offset, size_t count);
-int shim_socket (int family, int type, int protocol);
-int shim_connect (int sockfd, struct sockaddr * addr, int addrlen);
-int shim_accept (int fd, struct sockaddr * addr, socklen_t * addrlen);
-ssize_t shim_sendto (int fd, const void * buf, size_t len, int flags,
-                     const struct sockaddr * dest_addr, socklen_t addrlen);
-ssize_t shim_recvfrom (int fd, void * buf, size_t len, int flags,
-                       struct sockaddr * addr, socklen_t * addrlen);
-int shim_bind (int sockfd, struct sockaddr * addr, socklen_t addrlen);
-int shim_listen (int sockfd, int backlog);
-ssize_t shim_sendmsg (int fd, struct msghdr * msg, int flags);
-ssize_t shim_recvmsg (int fd, struct msghdr * msg, int flags);
-int shim_shutdown (int sockfd, int how);
-int shim_getsockname (int sockfd, struct sockaddr * addr, int * addrlen);
-int shim_getpeername (int sockfd, struct sockaddr * addr, int * addrlen);
-int shim_socketpair (int domain, int type, int protocol, int * sv);
-int shim_setsockopt (int fd, int level, int optname, char * optval, int optlen);
-int shim_getsockopt (int fd, int level, int optname, char * optval,
-                     int * optlen);
-int shim_clone (int flags, void * user_stack_addr, int * parent_tidptr,
-                int * child_tidptr, void * tls);
-int shim_fork (void);
-int shim_vfork (void);
-int shim_execve (const char * file, const char ** argv, const char ** envp);
-int shim_exit (int error_code);
-pid_t shim_wait4 (pid_t pid, int * stat_addr, int option,
-                  struct __kernel_rusage * ru);
-int shim_kill (pid_t pid, int sig);
-int shim_uname (struct old_utsname * buf);
-int shim_semget (key_t key, int nsems, int semflg);
-int shim_semop (int semid, struct sembuf * sops, unsigned int nsops);
-int shim_semctl (int semid, int semnum, int cmd, unsigned long arg);
-int shim_shmdt (const void * shmaddr);
-int shim_msgget (key_t key, int msgflg);
-int shim_msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg);
-int shim_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg);
-int shim_msgctl (int msqid, int cmd, struct msqid_ds * buf);
-int shim_fcntl (int fd, int cmd, unsigned long arg);
-int shim_flock (int fd, int cmd);
-int shim_fsync (int fd);
-int shim_fdatasync (int fd);
-int shim_truncate (const char * path, loff_t length);
-int shim_ftruncate (int fd, loff_t length);
-size_t shim_getdents (int fd, struct linux_dirent * buf, size_t count);
-int shim_getcwd (char * buf, size_t size);
-int shim_chdir (const char * filename);
-int shim_fchdir (int fd);
-int shim_rename (const char * oldname, const char * newname);
-int shim_mkdir (const char * pathname, int mode);
-int shim_rmdir (const char * pathname);
-int shim_creat (const char * path, mode_t mode);
-int shim_link (const char * oldname, const char * newname);
-int shim_unlink (const char * file);
-int shim_symlink (const char * old, const char * new);
-int shim_readlink (const char * file, char * buf, size_t bufsize);
-int shim_chmod (const char * filename, mode_t mode);
-int shim_fchmod (int fd, mode_t mode);
-int shim_chown (const char * filename, uid_t user, gid_t group);
-int shim_fchown (int fd, uid_t user, gid_t group);
-int shim_lchown (const char * filename, uid_t user, gid_t group);
-mode_t shim_umask (mode_t mask);
-int shim_gettimeofday (struct __kernel_timeval * tv,
-                       struct __kernel_timezone * tz);
-int shim_getrlimit (int resource, struct __kernel_rlimit * rlim);
-int shim_getrusage (int who, struct __kernel_rusage * ru);
-int shim_sysinfo (struct sysinfo * info);
-int shim_times (struct tms * tbuf);
-int shim_ptrace (long request, pid_t pid, void * addr, void * data);
-uid_t shim_getuid (void);
-int shim_syslog (int type, char * buf, int len);
-gid_t shim_getgid (void);
-int shim_setuid (uid_t uid);
-int shim_setgid (gid_t gid);
-uid_t shim_geteuid (void);
-gid_t shim_getegid (void);
-int shim_setpgid (pid_t pid, pid_t pgid);
-pid_t shim_getppid (void);
-pid_t shim_getpgrp (void);
-int shim_setsid (void);
-int shim_setreuid (uid_t ruid, uid_t euid);
-int shim_setregid (gid_t rgid, gid_t egid);
-int shim_getgroups (int gidsetsize, gid_t * grouplist);
-int shim_setgroups (int gidsetsize, gid_t * grouplist);
-int shim_setresuid (uid_t ruid, uid_t euid, uid_t suid);
-int shim_getresuid (uid_t * ruid, uid_t * euid, uid_t * suid);
-int shim_setresgid (gid_t rgid, gid_t egid, gid_t sgid);
-int shim_getresgid (gid_t * rgid, gid_t * egid, gid_t * sgid);
-int shim_getpgid (pid_t pid);
-int shim_setfsuid (uid_t uid);
-int shim_setfsgid (gid_t gid);
-int shim_getsid (pid_t pid);
-int shim_capget (cap_user_header_t header, cap_user_data_t dataptr);
-int shim_capset (cap_user_header_t header, const cap_user_data_t data);
-int shim_rt_sigpending (__sigset_t * set, size_t sigsetsize);
-int shim_rt_sigtimedwait (const __sigset_t * uthese, siginfo_t * uinfo,
-                          const struct timespec * uts, size_t sigsetsize);
-int shim_rt_sigqueueinfo (int pid, int sig, siginfo_t * uinfo);
-int shim_rt_sigsuspend (const __sigset_t * mask);
-int shim_sigaltstack (const stack_t * ss, stack_t * oss);
-int shim_utime (char * filename, struct utimbuf * times);
-int shim_mknod (const char * filename, int mode, unsigned dev);
-int shim_uselib (const char * library);
-int shim_personality (unsigned int personality);
-int shim_ustat (unsigned dev, struct __kernel_ustat * ubuf);
-int shim_statfs (const char * path, struct statfs * buf);
-int shim_fstatfs (int fd, struct statfs * buf);
-int shim_sysfs (int option, unsigned long arg1, unsigned long arg2);
-int shim_getpriority (int which, int who);
-int shim_setpriority (int which, int who, int niceval);
-int shim_sched_setparam (pid_t pid, struct __kernel_sched_param * param);
-int shim_sched_getparam (pid_t pid, struct __kernel_sched_param * param);
-int shim_sched_setscheduler (pid_t pid, int policy,
-                             struct __kernel_sched_param * param);
-int shim_sched_getscheduler (pid_t pid);
-int shim_sched_get_priority_max (int policy);
-int shim_sched_get_priority_min (int policy);
-int shim_sched_rr_get_interval (pid_t pid, struct timespec * interval);
-int shim_mlock (void * start, size_t len);
-int shim_munlock (void * start, size_t len);
-int shim_mlockall (int flags);
-int shim_munlockall (void);
-int shim_vhangup (void);
-int shim_modify_ldt (int func, void * ptr, unsigned long bytecount);
-int shim_pivot_root (const char * new_root, const char * put_old);
-int shim__sysctl (struct __kernel_sysctl_args * args);
-int shim_prctl (int option, unsigned long arg2, unsigned long arg3,
-                unsigned long arg4, unsigned long arg5);
-void * shim_arch_prctl (int code, void * addr);
-int shim_adjtimex (struct ____kernel_timex * txc_p);
-int shim_setrlimit (int resource, struct __kernel_rlimit * rlim);
-int shim_chroot (const char * filename);
-int shim_sync (void);
-int shim_acct (const char * name);
-int shim_settimeofday (struct timeval * tv, struct __kernel_timezone * tz);
-int shim_mount (char * dev_name, char * dir_name, char * type,
-                unsigned long flags, void * data);
-int shim_umount2 (const char * target, int flags);
-int shim_swapon (const char * specialfile, int swap_flags);
-int shim_swapoff (const char * specialfile);
-int shim_reboot (int magic1, int magic2, int cmd, void * arg);
-int shim_sethostname (char * name, int len);
-int shim_setdomainname (char * name, int len);
-int shim_iopl (int level);
-int shim_ioperm (unsigned long from, unsigned long num, int on);
-int shim_create_module (const char * name, size_t size);
-int shim_init_module (void * umod, unsigned long len, const char * uargs);
-int shim_delete_module (const char * name_user, unsigned int flags);
-int shim_query_module (const char * name, int which, void * buf, size_t bufsize,
-                       size_t * retsize);
-int shim_quotactl (int cmd, const char * special, qid_t id, void * addr);
-pid_t shim_gettid (void);
-int shim_readahead (int fd, loff_t offset, size_t count);
-int shim_setxattr (const char * path, const char * name, const void * value,
-                   size_t size, int flags);
-int shim_lsetxattr (const char * path, const char * name, const void * value,
-                    size_t size, int flags);
-int shim_fsetxattr (int fd, const char * name, const void * value, size_t size,
+size_t shim_read(int fd, void* buf, size_t count);
+size_t shim_write(int fd, const void* buf, size_t count);
+int shim_open(const char* file, int flags, mode_t mode);
+int shim_close(int fd);
+int shim_stat(const char* file, struct stat* statbuf);
+int shim_fstat(int fd, struct stat* statbuf);
+int shim_lstat(const char* file, struct stat* statbuf);
+int shim_poll(struct pollfd* fds, nfds_t nfds, int timeout);
+off_t shim_lseek(int fd, off_t offset, int origin);
+void* shim_mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset);
+int shim_mprotect(void* addr, size_t len, int prot);
+int shim_munmap(void* addr, size_t len);
+void* shim_brk(void* brk);
+int shim_rt_sigaction(int signum, const struct __kernel_sigaction* act,
+                      struct __kernel_sigaction* oldact, size_t sigsetsize);
+int shim_rt_sigprocmask(int how, const __sigset_t* set, __sigset_t* oldset);
+int shim_rt_sigreturn(int __unused);
+int shim_ioctl(int fd, int cmd, unsigned long arg);
+size_t shim_pread64(int fd, char* buf, size_t count, loff_t pos);
+size_t shim_pwrite64(int fd, char* buf, size_t count, loff_t pos);
+ssize_t shim_readv(int fd, const struct iovec* vec, int vlen);
+ssize_t shim_writev(int fd, const struct iovec* vec, int vlen);
+int shim_access(const char* file, mode_t mode);
+int shim_pipe(int* fildes);
+int shim_select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds,
+                struct __kernel_timeval* timeout);
+int shim_sched_yield(void);
+void* shim_mremap(void* addr, size_t old_len, size_t new_len, int flags, void* new_addr);
+int shim_msync(void* start, size_t len, int flags);
+int shim_mincore(void* start, size_t len, unsigned char* vec);
+int shim_madvise(void* start, size_t len, int behavior);
+int shim_shmget(key_t key, size_t size, int shmflg);
+void* shim_shmat(int shmid, const void* shmaddr, int shmflg);
+int shim_shmctl(int shmid, int cmd, struct shmid_ds* buf);
+int shim_dup(int fd);
+int shim_dup2(int oldfd, int newfd);
+int shim_pause(void);
+int shim_nanosleep(const struct __kernel_timespec* rqtp, struct __kernel_timespec* rmtp);
+int shim_getitimer(int which, struct __kernel_itimerval* value);
+int shim_alarm(unsigned int seconds);
+int shim_setitimer(int which, struct __kernel_itimerval* value, struct __kernel_itimerval* ovalue);
+pid_t shim_getpid(void);
+ssize_t shim_sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
+int shim_socket(int family, int type, int protocol);
+int shim_connect(int sockfd, struct sockaddr* addr, int addrlen);
+int shim_accept(int fd, struct sockaddr* addr, socklen_t* addrlen);
+ssize_t shim_sendto(int fd, const void* buf, size_t len, int flags,
+                    const struct sockaddr* dest_addr, socklen_t addrlen);
+ssize_t shim_recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* addr,
+                      socklen_t* addrlen);
+int shim_bind(int sockfd, struct sockaddr* addr, socklen_t addrlen);
+int shim_listen(int sockfd, int backlog);
+ssize_t shim_sendmsg(int fd, struct msghdr* msg, int flags);
+ssize_t shim_recvmsg(int fd, struct msghdr* msg, int flags);
+int shim_shutdown(int sockfd, int how);
+int shim_getsockname(int sockfd, struct sockaddr* addr, int* addrlen);
+int shim_getpeername(int sockfd, struct sockaddr* addr, int* addrlen);
+int shim_socketpair(int domain, int type, int protocol, int* sv);
+int shim_setsockopt(int fd, int level, int optname, char* optval, int optlen);
+int shim_getsockopt(int fd, int level, int optname, char* optval, int* optlen);
+int shim_clone(int flags, void* user_stack_addr, int* parent_tidptr, int* child_tidptr, void* tls);
+int shim_fork(void);
+int shim_vfork(void);
+int shim_execve(const char* file, const char** argv, const char** envp);
+int shim_exit(int error_code);
+pid_t shim_wait4(pid_t pid, int* stat_addr, int option, struct __kernel_rusage* ru);
+int shim_kill(pid_t pid, int sig);
+int shim_uname(struct old_utsname* buf);
+int shim_semget(key_t key, int nsems, int semflg);
+int shim_semop(int semid, struct sembuf* sops, unsigned int nsops);
+int shim_semctl(int semid, int semnum, int cmd, unsigned long arg);
+int shim_shmdt(const void* shmaddr);
+int shim_msgget(key_t key, int msgflg);
+int shim_msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg);
+int shim_msgrcv(int msqid, void* msgp, size_t msgsz, long msgtyp, int msgflg);
+int shim_msgctl(int msqid, int cmd, struct msqid_ds* buf);
+int shim_fcntl(int fd, int cmd, unsigned long arg);
+int shim_flock(int fd, int cmd);
+int shim_fsync(int fd);
+int shim_fdatasync(int fd);
+int shim_truncate(const char* path, loff_t length);
+int shim_ftruncate(int fd, loff_t length);
+size_t shim_getdents(int fd, struct linux_dirent* buf, size_t count);
+int shim_getcwd(char* buf, size_t size);
+int shim_chdir(const char* filename);
+int shim_fchdir(int fd);
+int shim_rename(const char* oldname, const char* newname);
+int shim_mkdir(const char* pathname, int mode);
+int shim_rmdir(const char* pathname);
+int shim_creat(const char* path, mode_t mode);
+int shim_link(const char* oldname, const char* newname);
+int shim_unlink(const char* file);
+int shim_symlink(const char* old, const char* new);
+int shim_readlink(const char* file, char* buf, size_t bufsize);
+int shim_chmod(const char* filename, mode_t mode);
+int shim_fchmod(int fd, mode_t mode);
+int shim_chown(const char* filename, uid_t user, gid_t group);
+int shim_fchown(int fd, uid_t user, gid_t group);
+int shim_lchown(const char* filename, uid_t user, gid_t group);
+mode_t shim_umask(mode_t mask);
+int shim_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone* tz);
+int shim_getrlimit(int resource, struct __kernel_rlimit* rlim);
+int shim_getrusage(int who, struct __kernel_rusage* ru);
+int shim_sysinfo(struct sysinfo* info);
+int shim_times(struct tms* tbuf);
+int shim_ptrace(long request, pid_t pid, void* addr, void* data);
+uid_t shim_getuid(void);
+int shim_syslog(int type, char* buf, int len);
+gid_t shim_getgid(void);
+int shim_setuid(uid_t uid);
+int shim_setgid(gid_t gid);
+uid_t shim_geteuid(void);
+gid_t shim_getegid(void);
+int shim_setpgid(pid_t pid, pid_t pgid);
+pid_t shim_getppid(void);
+pid_t shim_getpgrp(void);
+int shim_setsid(void);
+int shim_setreuid(uid_t ruid, uid_t euid);
+int shim_setregid(gid_t rgid, gid_t egid);
+int shim_getgroups(int gidsetsize, gid_t* grouplist);
+int shim_setgroups(int gidsetsize, gid_t* grouplist);
+int shim_setresuid(uid_t ruid, uid_t euid, uid_t suid);
+int shim_getresuid(uid_t* ruid, uid_t* euid, uid_t* suid);
+int shim_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
+int shim_getresgid(gid_t* rgid, gid_t* egid, gid_t* sgid);
+int shim_getpgid(pid_t pid);
+int shim_setfsuid(uid_t uid);
+int shim_setfsgid(gid_t gid);
+int shim_getsid(pid_t pid);
+int shim_capget(cap_user_header_t header, cap_user_data_t dataptr);
+int shim_capset(cap_user_header_t header, const cap_user_data_t data);
+int shim_rt_sigpending(__sigset_t* set, size_t sigsetsize);
+int shim_rt_sigtimedwait(const __sigset_t* uthese, siginfo_t* uinfo, const struct timespec* uts,
+                         size_t sigsetsize);
+int shim_rt_sigqueueinfo(int pid, int sig, siginfo_t* uinfo);
+int shim_rt_sigsuspend(const __sigset_t* mask);
+int shim_sigaltstack(const stack_t* ss, stack_t* oss);
+int shim_utime(char* filename, struct utimbuf* times);
+int shim_mknod(const char* filename, int mode, unsigned dev);
+int shim_uselib(const char* library);
+int shim_personality(unsigned int personality);
+int shim_ustat(unsigned dev, struct __kernel_ustat* ubuf);
+int shim_statfs(const char* path, struct statfs* buf);
+int shim_fstatfs(int fd, struct statfs* buf);
+int shim_sysfs(int option, unsigned long arg1, unsigned long arg2);
+int shim_getpriority(int which, int who);
+int shim_setpriority(int which, int who, int niceval);
+int shim_sched_setparam(pid_t pid, struct __kernel_sched_param* param);
+int shim_sched_getparam(pid_t pid, struct __kernel_sched_param* param);
+int shim_sched_setscheduler(pid_t pid, int policy, struct __kernel_sched_param* param);
+int shim_sched_getscheduler(pid_t pid);
+int shim_sched_get_priority_max(int policy);
+int shim_sched_get_priority_min(int policy);
+int shim_sched_rr_get_interval(pid_t pid, struct timespec* interval);
+int shim_mlock(void* start, size_t len);
+int shim_munlock(void* start, size_t len);
+int shim_mlockall(int flags);
+int shim_munlockall(void);
+int shim_vhangup(void);
+int shim_modify_ldt(int func, void* ptr, unsigned long bytecount);
+int shim_pivot_root(const char* new_root, const char* put_old);
+int shim__sysctl(struct __kernel_sysctl_args* args);
+int shim_prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
+               unsigned long arg5);
+void* shim_arch_prctl(int code, void* addr);
+int shim_adjtimex(struct ____kernel_timex* txc_p);
+int shim_setrlimit(int resource, struct __kernel_rlimit* rlim);
+int shim_chroot(const char* filename);
+int shim_sync(void);
+int shim_acct(const char* name);
+int shim_settimeofday(struct timeval* tv, struct __kernel_timezone* tz);
+int shim_mount(char* dev_name, char* dir_name, char* type, unsigned long flags, void* data);
+int shim_umount2(const char* target, int flags);
+int shim_swapon(const char* specialfile, int swap_flags);
+int shim_swapoff(const char* specialfile);
+int shim_reboot(int magic1, int magic2, int cmd, void* arg);
+int shim_sethostname(char* name, int len);
+int shim_setdomainname(char* name, int len);
+int shim_iopl(int level);
+int shim_ioperm(unsigned long from, unsigned long num, int on);
+int shim_create_module(const char* name, size_t size);
+int shim_init_module(void* umod, unsigned long len, const char* uargs);
+int shim_delete_module(const char* name_user, unsigned int flags);
+int shim_query_module(const char* name, int which, void* buf, size_t bufsize, size_t* retsize);
+int shim_quotactl(int cmd, const char* special, qid_t id, void* addr);
+pid_t shim_gettid(void);
+int shim_readahead(int fd, loff_t offset, size_t count);
+int shim_setxattr(const char* path, const char* name, const void* value, size_t size, int flags);
+int shim_lsetxattr(const char* path, const char* name, const void* value, size_t size, int flags);
+int shim_fsetxattr(int fd, const char* name, const void* value, size_t size, int flags);
+int shim_getxattr(const char* path, const char* name, void* value, size_t size);
+int shim_lgetxattr(const char* path, const char* name, void* value, size_t size);
+int shim_fgetxattr(int fd, const char* name, void* value, size_t size);
+int shim_listxattr(const char* path, char* list, size_t size);
+int shim_llistxattr(const char* path, char* list, size_t size);
+int shim_flistxattr(int fd, char* list, size_t size);
+int shim_removexattr(const char* path, const char* name);
+int shim_lremovexattr(const char* path, const char* name);
+int shim_fremovexattr(int fd, const char* name);
+int shim_tkill(int pid, int sig);
+time_t shim_time(time_t* tloc);
+int shim_futex(int* uaddr, int op, int val, void* utime, int* uaddr2, int val3);
+int shim_sched_setaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr);
+int shim_sched_getaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr);
+int shim_set_thread_area(struct user_desc* u_info);
+int shim_io_setup(unsigned nr_reqs, aio_context_t* ctx);
+int shim_io_destroy(aio_context_t ctx);
+int shim_io_getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event* events,
+                      struct timespec* timeout);
+int shim_io_submit(aio_context_t ctx_id, long nr, struct iocb** iocbpp);
+int shim_io_cancel(aio_context_t ctx_id, struct iocb* iocb, struct io_event* result);
+int shim_get_thread_area(struct user_desc* u_info);
+int shim_lookup_dcookie(unsigned long cookie64, char* buf, size_t len);
+int shim_epoll_create(int size);
+int shim_remap_file_pages(void* start, size_t size, int prot, ssize_t pgoff, int flags);
+size_t shim_getdents64(int fd, struct linux_dirent64* buf, size_t count);
+int shim_set_tid_address(int* tidptr);
+int shim_restart_syscall(void);
+int shim_semtimedop(int semid, struct sembuf* sops, unsigned nsops, const struct timespec* timeout);
+int shim_fadvise64(int fd, loff_t offset, size_t len, int advice);
+int shim_timer_create(clockid_t which_clock, struct sigevent* timer_event_spec,
+                      timer_t* created_timer_id);
+int shim_timer_settime(timer_t timer_id, int flags, const struct __kernel_itimerspec* new_setting,
+                       struct __kernel_itimerspec* old_setting);
+int shim_timer_gettime(timer_t timer_id, struct __kernel_itimerspec* setting);
+int shim_timer_getoverrun(timer_t timer_id);
+int shim_timer_delete(timer_t timer_id);
+int shim_clock_settime(clockid_t which_clock, const struct timespec* tp);
+int shim_clock_gettime(clockid_t which_clock, struct timespec* tp);
+int shim_clock_getres(clockid_t which_clock, struct timespec* tp);
+int shim_clock_nanosleep(clockid_t which_clock, int flags, const struct timespec* rqtp,
+                         struct timespec* rmtp);
+int shim_exit_group(int error_code);
+int shim_epoll_wait(int epfd, struct __kernel_epoll_event* events, int maxevents, int timeout_ms);
+int shim_epoll_ctl(int epfd, int op, int fd, struct __kernel_epoll_event* event);
+int shim_tgkill(int tgid, int pid, int sig);
+int shim_utimes(char* filename, struct timeval* utimes);
+int shim_mbind(void* start, unsigned long len, int mode, unsigned long* nmask,
+               unsigned long maxnode, int flags);
+int shim_set_mempolicy(int mode, unsigned long* nmask, unsigned long maxnode);
+int shim_get_mempolicy(int* policy, unsigned long* nmask, unsigned long maxnode, unsigned long addr,
+                       unsigned long flags);
+int shim_mq_open(const char* name, int oflag, mode_t mode, struct __kernel_mq_attr* attr);
+int shim_mq_unlink(const char* name);
+int shim_mq_timedsend(__kernel_mqd_t mqdes, const char* msg_ptr, size_t msg_len,
+                      unsigned int msg_prio, const struct timespec* abs_timeout);
+int shim_mq_timedreceive(__kernel_mqd_t mqdes, char* msg_ptr, size_t msg_len,
+                         unsigned int* msg_prio, const struct timespec* abs_timeout);
+int shim_mq_notify(__kernel_mqd_t mqdes, const struct sigevent* notification);
+int shim_mq_getsetattr(__kernel_mqd_t mqdes, const struct __kernel_mq_attr* mqstat,
+                       struct __kernel_mq_attr* omqstat);
+int shim_waitid(int which, pid_t pid, siginfo_t* infop, int options, struct __kernel_rusage* ru);
+int shim_ioprio_set(int which, int who, int ioprio);
+int shim_ioprio_get(int which, int who);
+int shim_inotify_init(void);
+int shim_inotify_add_watch(int fd, const char* path, unsigned int mask);
+int shim_inotify_rm_watch(int fd, unsigned int wd);
+int shim_migrate_pages(pid_t pid, unsigned long maxnode, const unsigned long* from,
+                       const unsigned long* to);
+int shim_openat(int dfd, const char* filename, int flags, int mode);
+int shim_mkdirat(int dfd, const char* pathname, int mode);
+int shim_mknodat(int dfd, const char* filename, int mode, unsigned dev);
+int shim_fchownat(int dfd, const char* filename, uid_t user, gid_t group, int flag);
+int shim_futimesat(int dfd, const char* filename, struct timeval* utimes);
+int shim_newfstatat(int dfd, const char* filename, struct stat* statbuf, int flag);
+int shim_unlinkat(int dfd, const char* pathname, int flag);
+int shim_renameat(int olddfd, const char* oldname, int newdfd, const char* newname);
+int shim_linkat(int olddfd, const char* oldname, int newdfd, const char* newname, int flags);
+int shim_symlinkat(const char* oldname, int newdfd, const char* newname);
+int shim_readlinkat(int dfd, const char* path, char* buf, int bufsiz);
+int shim_fchmodat(int dfd, const char* filename, mode_t mode);
+int shim_faccessat(int dfd, const char* filename, int mode);
+int shim_pselect6(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
+                  const struct __kernel_timespec* tsp, const __sigset_t* sigmask);
+int shim_ppoll(struct pollfd* fds, int nfds, struct timespec* tsp, const __sigset_t* sigmask,
+               size_t sigsetsize);
+int shim_unshare(int unshare_flags);
+int shim_set_robust_list(struct robust_list_head* head, size_t len);
+int shim_get_robust_list(pid_t pid, struct robust_list_head** head, size_t* len);
+int shim_splice(int fd_in, loff_t* off_in, int fd_out, loff_t* off_out, size_t len, int flags);
+int shim_tee(int fdin, int fdout, size_t len, unsigned int flags);
+int shim_sync_file_range(int fd, loff_t offset, loff_t nbytes, int flags);
+int shim_vmsplice(int fd, const struct iovec* iov, unsigned long nr_segs, int flags);
+int shim_move_pages(pid_t pid, unsigned long nr_pages, void** pages, const int* nodes, int* status,
                     int flags);
-int shim_getxattr (const char * path, const char * name, void * value,
-                   size_t size);
-int shim_lgetxattr (const char * path, const char * name, void * value,
-                    size_t size);
-int shim_fgetxattr (int fd, const char * name, void * value, size_t size);
-int shim_listxattr (const char * path, char * list, size_t size);
-int shim_llistxattr (const char * path, char * list, size_t size);
-int shim_flistxattr (int fd, char * list, size_t size);
-int shim_removexattr (const char * path, const char * name);
-int shim_lremovexattr (const char * path, const char * name);
-int shim_fremovexattr (int fd, const char * name);
-int shim_tkill (int pid, int sig);
-time_t shim_time (time_t * tloc);
-int shim_futex (int * uaddr, int op, int val, void * utime,
-                int * uaddr2, int val3);
-int shim_sched_setaffinity (pid_t pid, size_t len,
-                            __kernel_cpu_set_t * user_mask_ptr);
-int shim_sched_getaffinity (pid_t pid, size_t len,
-                            __kernel_cpu_set_t * user_mask_ptr);
-int shim_set_thread_area (struct user_desc * u_info);
-int shim_io_setup (unsigned nr_reqs, aio_context_t * ctx);
-int shim_io_destroy (aio_context_t ctx);
-int shim_io_getevents (aio_context_t ctx_id, long min_nr, long nr,
-                       struct io_event * events, struct timespec * timeout);
-int shim_io_submit (aio_context_t ctx_id, long nr, struct iocb ** iocbpp);
-int shim_io_cancel (aio_context_t ctx_id, struct iocb * iocb,
-                    struct io_event * result);
-int shim_get_thread_area (struct user_desc * u_info);
-int shim_lookup_dcookie (unsigned long cookie64, char * buf, size_t len);
-int shim_epoll_create (int size);
-int shim_remap_file_pages (void * start, size_t size, int prot, ssize_t pgoff,
-                           int flags);
-size_t shim_getdents64 (int fd, struct linux_dirent64 * buf, size_t count);
-int shim_set_tid_address (int * tidptr);
-int shim_restart_syscall (void);
-int shim_semtimedop (int semid, struct sembuf * sops, unsigned nsops,
-                     const struct timespec * timeout);
-int shim_fadvise64 (int fd, loff_t offset, size_t len, int advice);
-int shim_timer_create (clockid_t which_clock,
-                       struct sigevent * timer_event_spec,
-                       timer_t * created_timer_id);
-int shim_timer_settime (timer_t timer_id, int flags,
-                        const struct __kernel_itimerspec * new_setting,
-                        struct __kernel_itimerspec * old_setting);
-int shim_timer_gettime (timer_t timer_id, struct __kernel_itimerspec * setting);
-int shim_timer_getoverrun (timer_t timer_id);
-int shim_timer_delete (timer_t timer_id);
-int shim_clock_settime (clockid_t which_clock, const struct timespec * tp);
-int shim_clock_gettime (clockid_t which_clock, struct timespec * tp);
-int shim_clock_getres (clockid_t which_clock, struct timespec * tp);
-int shim_clock_nanosleep (clockid_t which_clock, int flags,
-                          const struct timespec * rqtp, struct timespec * rmtp);
-int shim_exit_group (int error_code);
-int shim_epoll_wait (int epfd, struct __kernel_epoll_event * events,
-                     int maxevents, int timeout_ms);
-int shim_epoll_ctl (int epfd, int op, int fd,
-                    struct __kernel_epoll_event * event);
-int shim_tgkill (int tgid, int pid, int sig);
-int shim_utimes (char * filename, struct timeval * utimes);
-int shim_mbind (void * start, unsigned long len, int mode,
-                unsigned long * nmask, unsigned long maxnode, int flags);
-int shim_set_mempolicy (int mode, unsigned long * nmask, unsigned long maxnode);
-int shim_get_mempolicy (int * policy, unsigned long * nmask,
-                        unsigned long maxnode, unsigned long addr,
-                        unsigned long flags);
-int shim_mq_open (const char * name, int oflag, mode_t mode,
-                  struct __kernel_mq_attr * attr);
-int shim_mq_unlink (const char * name);
-int shim_mq_timedsend (__kernel_mqd_t mqdes, const char * msg_ptr,
-                       size_t msg_len, unsigned int msg_prio,
-                       const struct timespec * abs_timeout);
-int shim_mq_timedreceive (__kernel_mqd_t mqdes, char * msg_ptr, size_t msg_len,
-                          unsigned int * msg_prio,
-                          const struct timespec * abs_timeout);
-int shim_mq_notify (__kernel_mqd_t mqdes, const struct sigevent * notification);
-int shim_mq_getsetattr (__kernel_mqd_t mqdes,
-                        const struct __kernel_mq_attr * mqstat,
-                        struct __kernel_mq_attr * omqstat);
-int shim_waitid (int which, pid_t pid, siginfo_t * infop, int options,
-                 struct __kernel_rusage * ru);
-int shim_ioprio_set (int which, int who, int ioprio);
-int shim_ioprio_get (int which, int who);
-int shim_inotify_init (void);
-int shim_inotify_add_watch (int fd, const char * path, unsigned int mask);
-int shim_inotify_rm_watch (int fd, unsigned int wd);
-int shim_migrate_pages (pid_t pid, unsigned long maxnode,
-                        const unsigned long * from, const unsigned long * to);
-int shim_openat (int dfd, const char * filename, int flags, int mode);
-int shim_mkdirat (int dfd, const char * pathname, int mode);
-int shim_mknodat (int dfd, const char * filename, int mode, unsigned dev);
-int shim_fchownat (int dfd, const char * filename, uid_t user, gid_t group,
-                   int flag);
-int shim_futimesat (int dfd, const char * filename, struct timeval * utimes);
-int shim_newfstatat (int dfd, const char* filename, struct stat* statbuf,
-                     int flag);
-int shim_unlinkat (int dfd, const char * pathname, int flag);
-int shim_renameat (int olddfd, const char * oldname, int newdfd,
-                   const char * newname);
-int shim_linkat (int olddfd, const char * oldname, int newdfd,
-                 const char * newname, int flags);
-int shim_symlinkat (const char * oldname, int newdfd, const char * newname);
-int shim_readlinkat (int dfd, const char * path, char * buf, int bufsiz);
-int shim_fchmodat (int dfd, const char * filename, mode_t mode);
-int shim_faccessat (int dfd, const char * filename, int mode);
-int shim_pselect6 (int nfds, fd_set * readfds, fd_set * writefds,
-                   fd_set * exceptfds, const struct __kernel_timespec * tsp,
-                   const __sigset_t * sigmask);
-int shim_ppoll (struct pollfd * fds, int nfds, struct timespec * tsp,
-                const __sigset_t * sigmask, size_t sigsetsize);
-int shim_unshare (int unshare_flags);
-int shim_set_robust_list (struct robust_list_head * head, size_t len);
-int shim_get_robust_list (pid_t pid, struct robust_list_head ** head,
-                          size_t * len);
-int shim_splice (int fd_in, loff_t * off_in, int fd_out, loff_t * off_out,
-                 size_t len, int flags);
-int shim_tee (int fdin, int fdout, size_t len, unsigned int flags);
-int shim_sync_file_range (int fd, loff_t offset, loff_t nbytes, int flags);
-int shim_vmsplice (int fd, const struct iovec * iov, unsigned long nr_segs,
-                   int flags);
-int shim_move_pages (pid_t pid, unsigned long nr_pages, void ** pages,
-                     const int * nodes, int * status, int flags);
-int shim_utimensat (int dfd, const char * filename, struct timespec *
-                    utimes, int flags);
-int shim_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
-                      int maxevents, int timeout_ms, const __sigset_t * sigmask,
-                      size_t sigsetsize);
-int shim_signalfd (int ufd, __sigset_t * user_mask, size_t sizemask);
-int shim_timerfd_create (int clockid, int flags);
-int shim_eventfd (int count);
-int shim_fallocate (int fd, int mode, loff_t offset, loff_t len);
-int shim_timerfd_settime (int ufd, int flags,
-                          const struct __kernel_itimerspec * utmr,
-                          struct __kernel_itimerspec * otmr);
-int shim_timerfd_gettime (int ufd, struct __kernel_itimerspec * otmr);
-int shim_accept4 (int sockfd, struct sockaddr * addr, socklen_t * addrlen,
-                  int flags);
-int shim_signalfd4 (int ufd, __sigset_t * user_mask, size_t sizemask, int flags);
-int shim_eventfd2 (int count, int flags);
-int shim_epoll_create1 (int flags);
-int shim_dup3 (int oldfd, int newfd, int flags);
-int shim_pipe2 (int * fildes, int flags);
-int shim_inotify_init1 (int flags);
-int shim_preadv (unsigned long fd, const struct iovec * vec,
-                 unsigned long vlen, unsigned long pos_l, unsigned long pos_h);
-int shim_pwritev (unsigned long fd, const struct iovec * vec,
-                  unsigned long vlen, unsigned long pos_l, unsigned long pos_h);
-int shim_rt_tgsigqueueinfo (pid_t tgid, pid_t pid, int sig, siginfo_t * uinfo);
-int shim_perf_event_open (struct perf_event_attr * attr_uptr, pid_t pid,
-                          int cpu, int group_fd, int flags);
-ssize_t shim_recvmmsg (int sockfd, struct mmsghdr * msg, size_t vlen, int flags,
-                       struct __kernel_timespec * timeout);
+int shim_utimensat(int dfd, const char* filename, struct timespec* utimes, int flags);
+int shim_epoll_pwait(int epfd, struct __kernel_epoll_event* events, int maxevents, int timeout_ms,
+                     const __sigset_t* sigmask, size_t sigsetsize);
+int shim_signalfd(int ufd, __sigset_t* user_mask, size_t sizemask);
+int shim_timerfd_create(int clockid, int flags);
+int shim_eventfd(int count);
+int shim_fallocate(int fd, int mode, loff_t offset, loff_t len);
+int shim_timerfd_settime(int ufd, int flags, const struct __kernel_itimerspec* utmr,
+                         struct __kernel_itimerspec* otmr);
+int shim_timerfd_gettime(int ufd, struct __kernel_itimerspec* otmr);
+int shim_accept4(int sockfd, struct sockaddr* addr, socklen_t* addrlen, int flags);
+int shim_signalfd4(int ufd, __sigset_t* user_mask, size_t sizemask, int flags);
+int shim_eventfd2(int count, int flags);
+int shim_epoll_create1(int flags);
+int shim_dup3(int oldfd, int newfd, int flags);
+int shim_pipe2(int* fildes, int flags);
+int shim_inotify_init1(int flags);
+int shim_preadv(unsigned long fd, const struct iovec* vec, unsigned long vlen, unsigned long pos_l,
+                unsigned long pos_h);
+int shim_pwritev(unsigned long fd, const struct iovec* vec, unsigned long vlen, unsigned long pos_l,
+                 unsigned long pos_h);
+int shim_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t* uinfo);
+int shim_perf_event_open(struct perf_event_attr* attr_uptr, pid_t pid, int cpu, int group_fd,
+                         int flags);
+ssize_t shim_recvmmsg(int sockfd, struct mmsghdr* msg, size_t vlen, int flags,
+                      struct __kernel_timespec* timeout);
 int shim_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
                    struct __kernel_rlimit64* old_rlim);
-ssize_t shim_sendmmsg (int sockfd, struct mmsghdr * msg, size_t vlen, int flags);
+ssize_t shim_sendmmsg(int sockfd, struct mmsghdr* msg, size_t vlen, int flags);
 
 /* libos call wrappers */
-int shim_msgpersist (int msqid, int cmd);
-int shim_benchmark_rpc (pid_t pid, int times, const void * buf, size_t size);
-size_t shim_send_rpc (pid_t pid, const void * buf, size_t size);
-size_t shim_recv_rpc (pid_t * pid, void * buf, size_t size);
-int shim_checkpoint(const char * filename);
+int shim_msgpersist(int msqid, int cmd);
+int shim_benchmark_rpc(pid_t pid, int times, const void* buf, size_t size);
+size_t shim_send_rpc(pid_t pid, const void* buf, size_t size);
+size_t shim_recv_rpc(pid_t* pid, void* buf, size_t size);
+int shim_checkpoint(const char* filename);
 
 #endif /* _SHIM_TABLE_H_ */

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

@@ -15,7 +15,6 @@
 struct shim_handle;
 struct shim_fd_map;
 struct shim_dentry;
-struct shim_signal_handle;
 struct shim_signal_log;
 
 DEFINE_LIST(shim_thread);

+ 18 - 19
LibOS/shim/include/shim_unistd.h

@@ -7,40 +7,39 @@
 #include <unistd.h>
 #endif
 
-#define __NR_msgpersist         (LIBOS_SYSCALL_BASE + 1)
+#define __NR_msgpersist (LIBOS_SYSCALL_BASE + 1)
 
-#define MSGPERSIST_STORE    0
-#define MSGPERSIST_LOAD     1
-int msgpersist (int msqid, int cmd);
+#define MSGPERSIST_STORE 0
+#define MSGPERSIST_LOAD  1
+int msgpersist(int msqid, int cmd);
 
-#define __NR_benchmark_rpc      (LIBOS_SYSCALL_BASE + 2)
-#define __NR_send_rpc           (LIBOS_SYSCALL_BASE + 3)
-#define __NR_recv_rpc           (LIBOS_SYSCALL_BASE + 4)
+#define __NR_benchmark_rpc (LIBOS_SYSCALL_BASE + 2)
+#define __NR_send_rpc      (LIBOS_SYSCALL_BASE + 3)
+#define __NR_recv_rpc      (LIBOS_SYSCALL_BASE + 4)
 
-int benchmark_rpc (pid_t pid, int times, const void * buf, size_t size);
+int benchmark_rpc(pid_t pid, int times, const void* buf, size_t size);
 
-size_t send_rpc (pid_t pid, const void * buf, size_t size);
-size_t recv_rpc (pid_t * pid, void * buf, size_t size);
+size_t send_rpc(pid_t pid, const void* buf, size_t size);
+size_t recv_rpc(pid_t* pid, void* buf, size_t size);
 
 struct nameinfo {
-     char * host;
-     size_t hostlen;
-     char * serv;
-     size_t servlen;
+    char* host;
+    size_t hostlen;
+    char* serv;
+    size_t servlen;
 };
 
-#define __NR_checkpoint         (LIBOS_SYSCALL_BASE + 5)
+#define __NR_checkpoint (LIBOS_SYSCALL_BASE + 5)
 
-int checkpoint (const char * filename);
+int checkpoint(const char* filename);
 
 struct sigcp {
     int si_session;
 };
 
-#define SI_CP_SESSION(info) \
-    (((struct sigcp *) &(info)->_sifields)->si_session)
+#define SI_CP_SESSION(info) (((struct sigcp*)&(info)->_sifields)->si_session)
 
-#define SIGCP                   33
+#define SIGCP 33
 
 #include "shim_unistd_defs.h"
 

+ 2 - 2
LibOS/shim/include/shim_unistd_defs.h

@@ -1,7 +1,7 @@
 #ifndef _SHIM_UNISTD_DEFS_H_
 #define _SHIM_UNISTD_DEFS_H_
 
-#define LIBOS_SYSCALL_BASE      340
-#define LIBOS_SYSCALL_BOUND     380
+#define LIBOS_SYSCALL_BASE  340
+#define LIBOS_SYSCALL_BOUND 380
 
 #endif

+ 66 - 87
LibOS/shim/include/shim_utils.h

@@ -21,44 +21,40 @@
 #ifndef _SHIM_UTILS_H_
 #define _SHIM_UTILS_H_
 
+#include <api.h>
+#include <list.h>
+#include <pal.h>
 #include <shim_handle.h>
 #include <shim_internal.h>
 
-#include <pal.h>
-#include <list.h>
-#include <api.h>
-
 struct shim_handle;
 
-void sysparser_printf (const char * fmt, ...);
+void sysparser_printf(const char* fmt, ...);
 
 /* string object */
-struct shim_str * get_str_obj (void);
-int free_str_obj (struct shim_str * str);
-int init_str_mgr (void);
+struct shim_str* get_str_obj(void);
+int free_str_obj(struct shim_str* str);
+int init_str_mgr(void);
 
 /* qstring object */
-#define QSTR_INIT  { .len = 0, .oflow = NULL }
+#define QSTR_INIT \
+    { .len = 0, .oflow = NULL }
 
-static inline const char * qstrgetstr (const struct shim_qstr * qstr)
-{
+static inline const char* qstrgetstr(const struct shim_qstr* qstr) {
     return qstr->oflow ? qstr->oflow->str : qstr->name;
 }
 
-static inline void qstrfree (struct shim_qstr * qstr)
-{
+static inline void qstrfree(struct shim_qstr* qstr) {
     if (qstr->oflow) {
         free_str_obj(qstr->oflow);
         qstr->oflow = NULL;
     }
 
     qstr->name[0] = 0;
-    qstr->len = 0;
+    qstr->len     = 0;
 }
 
-static inline char * qstrsetstr (struct shim_qstr * qstr,
-                                 const char * str, size_t size)
-{
+static inline char* qstrsetstr(struct shim_qstr* qstr, const char* str, size_t size) {
     if (!str) {
         qstrfree(qstr);
         return NULL;
@@ -67,7 +63,7 @@ static inline char * qstrsetstr (struct shim_qstr * qstr,
     if (size >= STR_SIZE)
         return NULL;
 
-    char * buf = qstr->name;
+    char* buf = qstr->name;
 
     if (size >= QSTR_SIZE) {
         if (!qstr->oflow) {
@@ -90,19 +86,18 @@ static inline char * qstrsetstr (struct shim_qstr * qstr,
     return buf;
 }
 
-static inline char * qstrsetstrs (struct shim_qstr * qstr,
-                                  int nstrs,
-                                  const char ** strs, size_t * sizes)
-{
+static inline char* qstrsetstrs(struct shim_qstr* qstr, int nstrs, const char** strs,
+                                size_t* sizes) {
     size_t total_size = 0;
 
-    for (int i = 0 ; i < nstrs ; i++)
+    for (int i = 0; i < nstrs; i++) {
         total_size += sizes[i];
+    }
 
     if (total_size >= STR_SIZE)
         return NULL;
 
-    char * buf = qstr->name;
+    char* buf = qstr->name;
 
     if (total_size >= QSTR_SIZE) {
         if (!qstr->oflow) {
@@ -114,10 +109,10 @@ static inline char * qstrsetstrs (struct shim_qstr * qstr,
         buf = qstr->oflow->str;
     }
 
-    char * ptr = buf;
+    char* ptr = buf;
     qstr->len = 0;
 
-    for (int i = 0 ; i < nstrs ; i++) {
+    for (int i = 0; i < nstrs; i++) {
         int size = sizes[i];
         memcpy(ptr, strs[i], size);
         ptr[size] = 0;
@@ -128,21 +123,16 @@ static inline char * qstrsetstrs (struct shim_qstr * qstr,
     return buf;
 }
 
-static inline int qstrempty (const struct shim_qstr * qstr)
-{
+static inline int qstrempty(const struct shim_qstr* qstr) {
     return qstr->len == 0;
 }
 
-static inline void qstrcopy (struct shim_qstr * to,
-                             const struct shim_qstr * from)
-{
+static inline void qstrcopy(struct shim_qstr* to, const struct shim_qstr* from) {
     qstrsetstr(to, qstrgetstr(from), from->len);
     to->hash = from->hash;
 }
 
-static inline int qstrcmpstr (const struct shim_qstr * qstr,
-                              const char * str, size_t size)
-{
+static inline int qstrcmpstr(const struct shim_qstr* qstr, const char* str, size_t size) {
     if (qstr->len != size)
         return 1;
 
@@ -153,27 +143,24 @@ static inline int qstrcmpstr (const struct shim_qstr * qstr,
 //#define SLAB_DEBUG_TRACE
 
 /* heap allocation functions */
-int init_slab (void);
+int init_slab(void);
 
 #if defined(SLAB_DEBUG_PRINT) || defined(SLAB_DEBUG_TRACE)
-void * __malloc_debug (size_t size, const char * file, int line);
+void* __malloc_debug(size_t size, const char* file, int line);
 #define malloc(size) __malloc_debug(size, __FILE__, __LINE__)
-void __free_debug (void * mem, const char * file, int line);
+void __free_debug(void* mem, const char* file, int line);
 #define free(mem) __free_debug(mem, __FILE__, __LINE__)
-void * __malloc_copy_debug (const void * mem, size_t size,
-                             const char * file, int line);
+void* __malloc_copy_debug(const void* mem, size_t size, const char* file, int line);
 #define malloc_copy(mem, size) __malloc_copy_debug(mem, size, __FILE__, __LINE__)
 #else
-void * malloc (size_t size);
-void free (void * mem);
-void * malloc_copy (const void * mem, size_t size);
+void* malloc(size_t size);
+void free(void* mem);
+void* malloc_copy(const void* mem, size_t size);
 #endif
 
-static_always_inline
-char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
-{
-    int len = qstr->len;
-    char * buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
+static_always_inline char* qstrtostr(struct shim_qstr* qstr, bool on_stack) {
+    int len   = qstr->len;
+    char* buf = on_stack ? __alloca(len + 1) : malloc(len + 1);
 
     if (!buf)
         return NULL;
@@ -184,62 +171,54 @@ char * qstrtostr (struct shim_qstr * qstr, bool on_stack)
     return buf;
 }
 
-
 /* typedef a 32 bit type */
-# ifndef UINT4
-# define UINT4 uint32_t
-# endif
+#ifndef UINT4
+#define UINT4 uint32_t
+#endif
 
 /* Data structure for MD5 (Message Digest) computation */
 struct shim_md5_ctx {
-    UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */
-    UINT4 buf[4];                                    /* scratch buffer */
-    unsigned char in[64];                              /* input buffer */
-    unsigned char digest[16];     /* actual digest after MD5Final call */
+    UINT4 i[2];               /* number of _bits_ handled mod 2^64 */
+    UINT4 buf[4];             /* scratch buffer */
+    unsigned char in[64];     /* input buffer */
+    unsigned char digest[16]; /* actual digest after MD5Final call */
 };
 
-void md5_init (struct shim_md5_ctx * mdContext);
-void md5_update (struct shim_md5_ctx * mdContext, const void * buf,
-                       size_t len);
-void md5_final (struct shim_md5_ctx * mdContext);
+void md5_init(struct shim_md5_ctx* mdContext);
+void md5_update(struct shim_md5_ctx* mdContext, const void* buf, size_t len);
+void md5_final(struct shim_md5_ctx* mdContext);
 
 /* prompt user for confirmation */
-int message_confirm (const char * message, const char * options);
+int message_confirm(const char* message, const char* options);
 
 /* ELF binary loading */
-int check_elf_object (struct shim_handle * file);
-int load_elf_object (struct shim_handle * file, void * addr, size_t mapped);
-int load_elf_interp (struct shim_handle * exec);
-int free_elf_interp (void);
-noreturn void execute_elf_object (struct shim_handle * exec,
-                                  int * argcp, const char ** argp,
-                                  elf_auxv_t * auxp);
-int remove_loaded_libraries (void);
+int check_elf_object(struct shim_handle* file);
+int load_elf_object(struct shim_handle* file, void* addr, size_t mapped);
+int load_elf_interp(struct shim_handle* exec);
+int free_elf_interp(void);
+noreturn void execute_elf_object(struct shim_handle* exec, int* argcp, const char** argp,
+                                 elf_auxv_t* auxp);
+int remove_loaded_libraries(void);
 
 /* gdb debugging support */
-void remove_r_debug (void * addr);
-void append_r_debug (const char * uri, void * addr, void * dyn_addr);
-void clean_link_map_list (void);
+void remove_r_debug(void* addr);
+void append_r_debug(const char* uri, void* addr, void* dyn_addr);
+void clean_link_map_list(void);
 
 /* create unique files/pipes */
-#define PIPE_URI_SIZE   40
-int create_pipe (IDTYPE * pipeid, char * uri, size_t size, PAL_HANDLE * hdl,
-                 struct shim_qstr * qstr, bool use_vmid_for_name);
-int create_dir (const char * prefix, char * path, size_t size,
-                struct shim_handle ** hdl);
-int create_file (const char * prefix, char * path, size_t size,
-                 struct shim_handle ** hdl);
-int create_handle (const char * prefix, char * path, size_t size,
-                   PAL_HANDLE * hdl, unsigned int * id);
-
+#define PIPE_URI_SIZE 40
+int create_pipe(IDTYPE* pipeid, char* uri, size_t size, PAL_HANDLE* hdl, struct shim_qstr* qstr,
+                bool use_vmid_for_name);
+int create_dir(const char* prefix, char* path, size_t size, struct shim_handle** hdl);
+int create_file(const char* prefix, char* path, size_t size, struct shim_handle** hdl);
+int create_handle(const char* prefix, char* path, size_t size, PAL_HANDLE* hdl, unsigned int* id);
 
 /* Asynchronous event support */
-int init_async (void);
-int64_t install_async_event (PAL_HANDLE object, unsigned long time,
-                              void (*callback) (IDTYPE caller, void * arg),
-                              void * arg);
-struct shim_thread * terminate_async_helper (void);
+int init_async(void);
+int64_t install_async_event(PAL_HANDLE object, unsigned long time,
+                            void (*callback)(IDTYPE caller, void* arg), void* arg);
+struct shim_thread* terminate_async_helper(void);
 
-extern struct config_store * root_config;
+extern struct config_store* root_config;
 
 #endif /* _SHIM_UTILS_H */

+ 48 - 60
LibOS/shim/include/shim_vma.h

@@ -23,38 +23,34 @@
 #ifndef _SHIM_VMA_H_
 #define _SHIM_VMA_H_
 
-#include <shim_types.h>
-#include <shim_defs.h>
-#include <shim_handle.h>
-
-#include <pal.h>
 #include <api.h>
-#include <list.h>
-
 #include <linux/mman.h>
+#include <list.h>
+#include <pal.h>
+#include <shim_defs.h>
+#include <shim_handle.h>
+#include <shim_types.h>
 
 struct shim_handle;
 
-#define VMA_COMMENT_LEN     16
+#define VMA_COMMENT_LEN 16
 
 /*
  * struct shim_vma_val is the published version of struct shim_vma
  * (struct shim_vma is defined in bookkeep/shim_vma.c).
  */
 struct shim_vma_val {
-    void *                  addr;
-    size_t                  length;
-    int                     prot;
-    int                     flags;
-    off_t                   offset;
-    struct shim_handle *    file;
-    char                    comment[VMA_COMMENT_LEN];
+    void* addr;
+    size_t length;
+    int prot;
+    int flags;
+    off_t offset;
+    struct shim_handle* file;
+    char comment[VMA_COMMENT_LEN];
 };
 
-static inline
-void free_vma_val_array (struct shim_vma_val * vmas, size_t count)
-{
-    for (size_t i = 0 ; i < count ; i++) {
+static inline void free_vma_val_array(struct shim_vma_val* vmas, size_t count) {
+    for (size_t i = 0; i < count; i++) {
         /* need to release the file handle */
         if (vmas[i].file)
             put_handle(vmas[i].file);
@@ -64,36 +60,31 @@ void free_vma_val_array (struct shim_vma_val * vmas, size_t count)
 }
 
 /* an additional flag */
-#define VMA_UNMAPPED 0x10000000   /* vma is kept for bookkeeping, but the
-                                     memory is not actually allocated */
-#define VMA_INTERNAL 0x20000000   /* vma is used internally */
+#define VMA_UNMAPPED 0x10000000 /* vma is kept for bookkeeping, but the memory is not actually
+                                   allocated */
+#define VMA_INTERNAL 0x20000000 /* vma is used internally */
 
-#define VMA_TAINTED  0x40000000   /* vma has been protected as writable,
-                                     so it has to be checkpointed during
-                                     migration */
+#define VMA_TAINTED 0x40000000 /* vma has been protected as writable, so it has to be checkpointed
+                                  during migration */
 
-#define VMA_CP       0x80000000   /* vma is used for dumping checkpoint
-                                     data */
+#define VMA_CP 0x80000000 /* vma is used for dumping checkpoint data */
 
-#define VMA_TYPE(flags)     ((flags) & (VMA_INTERNAL | VMA_CP))
+#define VMA_TYPE(flags) ((flags) & (VMA_INTERNAL | VMA_CP))
 
 /*
  * We distinguish checkpoint VMAs from user VMAs and other internal VMAs,
  * to prevent corrupting internal data when creating processes.
  */
-#define CP_VMA_FLAGS  (MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL|VMA_CP)
+#define CP_VMA_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | VMA_INTERNAL | VMA_CP)
 
-#define NEED_MIGRATE_MEMORY(vma)                                \
-        (((vma)->flags & VMA_TAINTED || !(vma)->file) &&        \
-        !((vma)->flags & VMA_UNMAPPED))
+#define NEED_MIGRATE_MEMORY(vma) \
+    (((vma)->flags & VMA_TAINTED || !(vma)->file) && !((vma)->flags & VMA_UNMAPPED))
 
-#define NEED_MIGRATE_MEMORY_IF_GIPC(vma)                        \
-        (!((vma)->flags & VMA_UNMAPPED) &&                      \
-         !(!(vma)->prot && !((vma)->flags & VMA_TAINTED)) &&    \
-         !((vma)->file && ((vma)->flags & MAP_SHARED)))
+#define NEED_MIGRATE_MEMORY_IF_GIPC(vma)                                                   \
+    (!((vma)->flags & VMA_UNMAPPED) && !(!(vma)->prot && !((vma)->flags & VMA_TAINTED)) && \
+     !((vma)->file && ((vma)->flags & MAP_SHARED)))
 
-static inline PAL_FLG PAL_PROT (int prot, int flags)
-{
+static inline PAL_FLG PAL_PROT(int prot, int flags) {
     PAL_FLG pal_prot = 0;
 
     if (prot & PROT_READ)
@@ -109,26 +100,26 @@ static inline PAL_FLG PAL_PROT (int prot, int flags)
     return pal_prot;
 }
 
-int init_vma (void);
+int init_vma(void);
 
 /* Bookkeeping mmap() system call */
-int bkeep_mmap (void * addr, size_t length, int prot, int flags,
-                struct shim_handle * file, off_t offset, const char * comment);
+int bkeep_mmap(void* addr, size_t length, int prot, int flags, struct shim_handle* file,
+               off_t offset, const char* comment);
 
 /* Bookkeeping munmap() system call */
-int bkeep_munmap (void * addr, size_t length, int flags);
+int bkeep_munmap(void* addr, size_t length, int flags);
 
 /* Bookkeeping mprotect() system call */
-int bkeep_mprotect (void * addr, size_t length, int prot, int flags);
+int bkeep_mprotect(void* addr, size_t length, int prot, int flags);
 
 /* Looking up VMA that contains [addr, length) */
-int lookup_vma (void * addr, struct shim_vma_val * vma);
+int lookup_vma(void* addr, struct shim_vma_val* vma);
 
 /* Looking up VMA that overlaps with [addr, length) */
-int lookup_overlap_vma (void * addr, size_t length, struct shim_vma_val * vma);
+int lookup_overlap_vma(void* addr, size_t length, struct shim_vma_val* vma);
 
 /* True if [addr, addr+length) is found in one VMA (valid memory region) */
-bool is_in_adjacent_vmas (void * addr, size_t length);
+bool is_in_adjacent_vmas(void* addr, size_t length);
 
 /*
  * Looking for an unmapped space and then adding the corresponding bookkeeping
@@ -136,29 +127,26 @@ bool is_in_adjacent_vmas (void * addr, size_t length);
  *
  * Note: the first argument is "top_addr" because the search is top-down.
  */
-void * bkeep_unmapped (void * top_addr, void * bottom_addr, size_t length, int prot, int flags,
-                       struct shim_handle * file, off_t offset, const char * comment);
-
-static inline void *
-bkeep_unmapped_any (size_t length, int prot, int flags, struct shim_handle * file,
-                    off_t offset, const char * comment)
-{
-    return bkeep_unmapped(PAL_CB(user_address.end),
-                          PAL_CB(user_address.start),
-                          length, prot, flags, file, offset, comment);
+void* bkeep_unmapped(void* top_addr, void* bottom_addr, size_t length, int prot, int flags,
+                     struct shim_handle* file, off_t offset, const char* comment);
+
+static inline void* bkeep_unmapped_any(size_t length, int prot, int flags, struct shim_handle* file,
+                                       off_t offset, const char* comment) {
+    return bkeep_unmapped(PAL_CB(user_address.end), PAL_CB(user_address.start), length, prot, flags,
+                          file, offset, comment);
 }
 
-void * bkeep_unmapped_heap (size_t length, int prot, int flags, struct shim_handle * file,
-                            off_t offset, const char * comment);
+void* bkeep_unmapped_heap(size_t length, int prot, int flags, struct shim_handle* file,
+                          off_t offset, const char* comment);
 
 /*
  * Dumping all *non-internal* VMAs into a user-allocated buffer ("max_count" is
  * the maximal number of entries in the buffer). Return number of filled entries
  * if succeeded, or -EOVERFLOW if the buffer is too small.
  */
-int dump_all_vmas (struct shim_vma_val * vmas, size_t max_count);
+int dump_all_vmas(struct shim_vma_val* vmas, size_t max_count);
 
 /* Debugging */
-void debug_print_vma_list (void);
+void debug_print_vma_list(void);
 
 #endif /* _SHIM_VMA_H_ */

+ 157 - 227
LibOS/shim/src/bookkeep/shim_handle.c

@@ -20,18 +20,17 @@
  * This file contains codes to maintain bookkeeping for handles in library OS.
  */
 
-#include <shim_internal.h>
-#include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_checkpoint.h>
-#include <shim_fs.h>
-
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_checkpoint.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_thread.h>
 
 static struct shim_lock handle_mgr_lock;
 
-#define HANDLE_MGR_ALLOC        32
+#define HANDLE_MGR_ALLOC 32
 
 #define SYSTEM_LOCK()   lock(&handle_mgr_lock)
 #define SYSTEM_UNLOCK() unlock(&handle_mgr_lock)
@@ -42,15 +41,14 @@ static struct shim_lock handle_mgr_lock;
 
 static MEM_MGR handle_mgr = NULL;
 
-#define INIT_HANDLE_MAP_SIZE    32
+#define INIT_HANDLE_MAP_SIZE 32
 
 //#define DEBUG_REF
 
-static inline int init_tty_handle (struct shim_handle * hdl, bool write)
-{
-    struct shim_dentry * dent = NULL;
+static inline int init_tty_handle(struct shim_handle* hdl, bool write) {
+    struct shim_dentry* dent = NULL;
     int ret;
-    struct shim_thread * cur_thread = get_cur_thread();
+    struct shim_thread* cur_thread = get_cur_thread();
 
     /* XXX: Try getting the root FS from current thread? */
     assert(cur_thread);
@@ -58,18 +56,18 @@ static inline int init_tty_handle (struct shim_handle * hdl, bool write)
     if ((ret = path_lookupat(NULL, "/dev/tty", LOOKUP_OPEN, &dent, NULL)) < 0)
         return ret;
 
-    int flags = (write ? O_WRONLY : O_RDONLY)|O_APPEND;
-    struct shim_mount * fs = dent->fs;
-    ret = fs->d_ops->open(hdl, dent, flags);
+    int flags             = (write ? O_WRONLY : O_RDONLY) | O_APPEND;
+    struct shim_mount* fs = dent->fs;
+    ret                   = fs->d_ops->open(hdl, dent, flags);
     if (ret < 0)
         return ret;
 
     set_handle_fs(hdl, fs);
     hdl->dentry = dent;
-    hdl->flags = O_RDWR|O_APPEND|0100000;
+    hdl->flags  = O_RDWR | O_APPEND | 0100000;
 
     size_t size;
-    char * path = dentry_get_path(dent, true, &size);
+    char* path = dentry_get_path(dent, true, &size);
     if (path)
         qstrsetstr(&hdl->path, path, size);
     else
@@ -78,12 +76,11 @@ static inline int init_tty_handle (struct shim_handle * hdl, bool write)
     return 0;
 }
 
-static inline int init_exec_handle (struct shim_thread * thread)
-{
+static inline int init_exec_handle(struct shim_thread* thread) {
     if (!PAL_CB(executable))
         return 0;
 
-    struct shim_handle * exec = get_new_handle();
+    struct shim_handle* exec = get_new_handle();
     if (!exec)
         return -ENOMEM;
 
@@ -92,21 +89,22 @@ static inline int init_exec_handle (struct shim_thread * thread)
     exec->flags    = O_RDONLY;
     exec->acc_mode = MAY_READ;
 
-    struct shim_mount * fs = find_mount_from_uri(PAL_CB(executable));
+    struct shim_mount* fs = find_mount_from_uri(PAL_CB(executable));
     if (fs) {
-        const char * p = PAL_CB(executable) + fs->uri.len;
+        const char* p = PAL_CB(executable) + fs->uri.len;
         /*
          * Lookup for PAL_CB(executable) needs to be done under a given
          * mount point. which requires a relative path name.
          * On the other hand, the one in manifest file can be absolute path.
          */
-        while (*p == '/')
+        while (*p == '/') {
             p++;
+        }
         path_lookupat(fs->root, p, 0, &exec->dentry, fs);
         set_handle_fs(exec, fs);
         if (exec->dentry) {
             size_t len;
-            const char * path = dentry_get_path(exec->dentry, true, &len);
+            const char* path = dentry_get_path(exec->dentry, true, &len);
             qstrsetstr(&exec->path, path, len);
         }
         put_mount(fs);
@@ -121,18 +119,16 @@ static inline int init_exec_handle (struct shim_thread * thread)
     return 0;
 }
 
-static struct shim_handle_map * get_new_handle_map (FDTYPE size);
+static struct shim_handle_map* get_new_handle_map(FDTYPE size);
 
 PAL_HANDLE shim_stdio = NULL;
 
-static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
-                               struct shim_handle * hdl, int flags);
+static int __set_new_fd_handle(struct shim_fd_handle** fdhdl, FDTYPE fd, struct shim_handle* hdl,
+                               int flags);
 
-static struct shim_handle_map * __enlarge_handle_map
-                     (struct shim_handle_map * map, FDTYPE size);
+static struct shim_handle_map* __enlarge_handle_map(struct shim_handle_map* map, FDTYPE size);
 
-int init_handle (void)
-{
+int init_handle(void) {
     create_lock(&handle_mgr_lock);
     handle_mgr = create_mem_mgr(init_align_up(HANDLE_MGR_ALLOC));
     if (!handle_mgr)
@@ -140,14 +136,13 @@ int init_handle (void)
     return 0;
 }
 
-int init_important_handles (void)
-{
-    struct shim_thread * thread = get_cur_thread();
+int init_important_handles(void) {
+    struct shim_thread* thread = get_cur_thread();
 
     if (thread->handle_map)
         goto done;
 
-    struct shim_handle_map * handle_map = get_cur_handle_map(thread);
+    struct shim_handle_map* handle_map = get_cur_handle_map(thread);
 
     if (!handle_map) {
         handle_map = get_new_handle_map(INIT_HANDLE_MAP_SIZE);
@@ -166,10 +161,10 @@ int init_important_handles (void)
         }
     }
 
-    struct shim_handle * hdl = NULL;
+    struct shim_handle* hdl = NULL;
     int ret;
 
-    for (int fd = 0 ; fd < 3 ; fd++)
+    for (int fd = 0; fd < 3; fd++)
         if (!HANDLE_ALLOCATED(handle_map->map[fd])) {
             if (!hdl) {
                 hdl = get_new_handle();
@@ -203,13 +198,10 @@ done:
     return 0;
 }
 
-struct shim_handle * __get_fd_handle (FDTYPE fd, int * flags,
-                                      struct shim_handle_map * map)
-{
-    struct shim_fd_handle * fd_handle = NULL;
+struct shim_handle* __get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map) {
+    struct shim_fd_handle* fd_handle = NULL;
 
-    if (map->fd_top != FD_NULL &&
-        fd <= map->fd_top) {
+    if (map->fd_top != FD_NULL && fd <= map->fd_top) {
         fd_handle = map->map[fd];
         if (!HANDLE_ALLOCATED(fd_handle))
             return NULL;
@@ -222,13 +214,11 @@ struct shim_handle * __get_fd_handle (FDTYPE fd, int * flags,
     return NULL;
 }
 
-struct shim_handle * get_fd_handle (FDTYPE fd, int * flags,
-                                    struct shim_handle_map * map)
-{
+struct shim_handle* get_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* map) {
     if (!map)
         map = get_cur_handle_map(NULL);
 
-    struct shim_handle * hdl = NULL;
+    struct shim_handle* hdl = NULL;
     lock(&map->lock);
     if ((hdl = __get_fd_handle(fd, flags, map)))
         get_handle(hdl);
@@ -236,37 +226,32 @@ struct shim_handle * get_fd_handle (FDTYPE fd, int * flags,
     return hdl;
 }
 
-struct shim_handle *
-__detach_fd_handle (struct shim_fd_handle * fd, int * flags,
-                    struct shim_handle_map * map)
-{
-    struct shim_handle * handle = NULL;
+struct shim_handle* __detach_fd_handle(struct shim_fd_handle* fd, int* flags,
+                                       struct shim_handle_map* map) {
+    struct shim_handle* handle = NULL;
 
     if (HANDLE_ALLOCATED(fd)) {
         int vfd = fd->vfd;
-        handle = fd->handle;
+        handle  = fd->handle;
         if (flags)
             *flags = fd->flags;
 
-        fd->vfd = FD_NULL;
+        fd->vfd    = FD_NULL;
         fd->handle = NULL;
-        fd->flags = 0;
+        fd->flags  = 0;
 
         if (vfd == map->fd_top)
             do {
                 map->fd_top = vfd ? vfd - 1 : FD_NULL;
                 vfd--;
-            } while (vfd >= 0 &&
-                     !HANDLE_ALLOCATED(map->map[vfd]));
+            } while (vfd >= 0 && !HANDLE_ALLOCATED(map->map[vfd]));
     }
 
     return handle;
 }
 
-struct shim_handle * detach_fd_handle (FDTYPE fd, int * flags,
-                                       struct shim_handle_map * handle_map)
-{
-    struct shim_handle * handle = NULL;
+struct shim_handle* detach_fd_handle(FDTYPE fd, int* flags, struct shim_handle_map* handle_map) {
+    struct shim_handle* handle = NULL;
 
     if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
         return NULL;
@@ -274,18 +259,15 @@ struct shim_handle * detach_fd_handle (FDTYPE fd, int * flags,
     lock(&handle_map->lock);
 
     if (fd < handle_map->fd_size)
-        handle = __detach_fd_handle(handle_map->map[fd], flags,
-                                    handle_map);
+        handle = __detach_fd_handle(handle_map->map[fd], flags, handle_map);
 
     unlock(&handle_map->lock);
     return handle;
 }
 
-struct shim_handle * get_new_handle (void)
-{
-    struct shim_handle * new_handle =
-                get_mem_obj_from_mgr_enlarge(handle_mgr,
-                                             size_align_up(HANDLE_MGR_ALLOC));
+struct shim_handle* get_new_handle(void) {
+    struct shim_handle* new_handle =
+        get_mem_obj_from_mgr_enlarge(handle_mgr, size_align_up(HANDLE_MGR_ALLOC));
     if (!new_handle)
         return NULL;
 
@@ -297,10 +279,9 @@ struct shim_handle * get_new_handle (void)
     return new_handle;
 }
 
-static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
-                               struct shim_handle * hdl, int flags)
-{
-    struct shim_fd_handle * new_handle = *fdhdl;
+static int __set_new_fd_handle(struct shim_fd_handle** fdhdl, FDTYPE fd, struct shim_handle* hdl,
+                               int flags) {
+    struct shim_fd_handle* new_handle = *fdhdl;
 
     if (!new_handle) {
         new_handle = malloc(sizeof(struct shim_fd_handle));
@@ -309,27 +290,24 @@ static int __set_new_fd_handle(struct shim_fd_handle ** fdhdl, FDTYPE fd,
         *fdhdl = new_handle;
     }
 
-    new_handle->vfd    = fd;
-    new_handle->flags  = flags;
+    new_handle->vfd   = fd;
+    new_handle->flags = flags;
     get_handle(hdl);
     new_handle->handle = hdl;
     return 0;
 }
 
-int set_new_fd_handle (struct shim_handle * hdl, int flags,
-                       struct shim_handle_map * handle_map)
-{
-    FDTYPE fd = 0;
+int set_new_fd_handle(struct shim_handle* hdl, int flags, struct shim_handle_map* handle_map) {
+    FDTYPE fd    = 0;
     int new_size = 0;
-    int ret = 0;
+    int ret      = 0;
 
     if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
         return -EBADF;
 
     lock(&handle_map->lock);
 
-    if (!handle_map->map ||
-        handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
+    if (!handle_map->map || handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
         new_size = INIT_HANDLE_MAP_SIZE;
 
     if (!handle_map->map)
@@ -339,20 +317,17 @@ int set_new_fd_handle (struct shim_handle * hdl, int flags,
         do {
             ++fd;
             if (fd == handle_map->fd_size) {
-                new_size = handle_map->fd_size < new_size ? new_size :
-                           handle_map->fd_size * 2;
-extend:
+                new_size = handle_map->fd_size < new_size ? new_size : handle_map->fd_size * 2;
+            extend:
                 if (!__enlarge_handle_map(handle_map, new_size)) {
                     ret = -ENOMEM;
                     goto out;
                 }
             }
-        } while (handle_map->fd_top != FD_NULL &&
-                 fd <= handle_map->fd_top &&
+        } while (handle_map->fd_top != FD_NULL && fd <= handle_map->fd_top &&
                  HANDLE_ALLOCATED(handle_map->map[fd]));
 
-    if (handle_map->fd_top == FD_NULL ||
-        fd > handle_map->fd_top)
+    if (handle_map->fd_top == FD_NULL || fd > handle_map->fd_top)
         handle_map->fd_top = fd;
 
     ret = __set_new_fd_handle(&handle_map->map[fd], fd, hdl, flags);
@@ -366,30 +341,26 @@ out:
     return ret;
 }
 
-int set_new_fd_handle_by_fd (FDTYPE fd, struct shim_handle * hdl, int flags,
-                             struct shim_handle_map * handle_map)
-{
+int set_new_fd_handle_by_fd(FDTYPE fd, struct shim_handle* hdl, int flags,
+                            struct shim_handle_map* handle_map) {
     int new_size = 0;
-    int ret = 0;
+    int ret      = 0;
 
     if (!handle_map && !(handle_map = get_cur_handle_map(NULL)))
         return -EBADF;
 
     lock(&handle_map->lock);
 
-    if (!handle_map->map ||
-        handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
+    if (!handle_map->map || handle_map->fd_size < INIT_HANDLE_MAP_SIZE)
         new_size = INIT_HANDLE_MAP_SIZE;
 
     if (!handle_map->map)
         goto extend;
 
     if (fd >= handle_map->fd_size) {
-        new_size = handle_map->fd_size < new_size ? new_size :
-                   handle_map->fd_size;
-extend:
-        while (new_size <= fd)
-            new_size *= 2;
+        new_size = handle_map->fd_size < new_size ? new_size : handle_map->fd_size;
+    extend:
+        while (new_size <= fd) new_size *= 2;
 
         if (!__enlarge_handle_map(handle_map, new_size)) {
             ret = -ENOMEM;
@@ -397,18 +368,16 @@ extend:
         }
     }
 
-    if (handle_map->fd_top != FD_NULL &&
-        fd <= handle_map->fd_top &&
+    if (handle_map->fd_top != FD_NULL && fd <= handle_map->fd_top &&
         HANDLE_ALLOCATED(handle_map->map[fd])) {
         ret = -EBADF;
         goto out;
     }
 
-    if (handle_map->fd_top == FD_NULL ||
-        fd > handle_map->fd_top)
+    if (handle_map->fd_top == FD_NULL || fd > handle_map->fd_top)
         handle_map->fd_top = fd;
 
-    struct shim_fd_handle * new_handle = handle_map->map[fd];
+    struct shim_fd_handle* new_handle = handle_map->map[fd];
 
     if (!new_handle) {
         new_handle = malloc(sizeof(struct shim_fd_handle));
@@ -430,16 +399,12 @@ out:
     return ret;
 }
 
-void flush_handle (struct shim_handle * hdl)
-{
-    if (hdl->fs && hdl->fs->fs_ops &&
-        hdl->fs->fs_ops->flush)
+void flush_handle(struct shim_handle* hdl) {
+    if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->flush)
         hdl->fs->fs_ops->flush(hdl);
 }
 
-static inline __attribute__((unused))
-const char * __handle_name (struct shim_handle * hdl)
-{
+static inline __attribute__((unused)) const char* __handle_name(struct shim_handle* hdl) {
     if (!qstrempty(&hdl->path))
         return qstrgetstr(&hdl->path);
     if (!qstrempty(&hdl->uri))
@@ -449,20 +414,17 @@ const char * __handle_name (struct shim_handle * hdl)
     return "(unknown)";
 }
 
-void get_handle (struct shim_handle * hdl)
-{
+void get_handle(struct shim_handle* hdl) {
 #ifdef DEBUG_REF
     int ref_count = REF_INC(hdl->ref_count);
 
-    debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
-          ref_count);
+    debug("get handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl), ref_count);
 #else
     REF_INC(hdl->ref_count);
 #endif
 }
 
-static void destroy_handle (struct shim_handle * hdl)
-{
+static void destroy_handle(struct shim_handle* hdl) {
     destroy_lock(&hdl->lock);
 
     if (memory_migrated(hdl))
@@ -471,20 +433,18 @@ static void destroy_handle (struct shim_handle * hdl)
         free_mem_obj_to_mgr(handle_mgr, hdl);
 }
 
-extern int delete_from_epoll_handles (struct shim_handle * handle);
+extern int delete_from_epoll_handles(struct shim_handle* handle);
 
-void put_handle (struct shim_handle * hdl)
-{
+void put_handle(struct shim_handle* hdl) {
     int ref_count = REF_DEC(hdl->ref_count);
 
 #ifdef DEBUG_REF
-    debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl),
-          ref_count);
+    debug("put handle %p(%s) (ref_count = %d)\n", hdl, __handle_name(hdl), ref_count);
 #endif
 
     if (!ref_count) {
         if (hdl->type == TYPE_DIR) {
-            struct shim_dir_handle * dir = &hdl->dir_info;
+            struct shim_dir_handle* dir = &hdl->dir_info;
 
             if (dir->dot) {
                 put_dentry(dir->dot);
@@ -496,23 +456,21 @@ void put_handle (struct shim_handle * hdl)
                 dir->dotdot = NULL;
             }
 
-            if (dir->ptr != (void *) -1) {
+            if (dir->ptr != (void*)-1) {
                 while (dir->ptr && *dir->ptr) {
-                    struct shim_dentry * dent = *dir->ptr;
+                    struct shim_dentry* dent = *dir->ptr;
                     put_dentry(dent);
                     *(dir->ptr++) = NULL;
                 }
             }
         } else {
-            if (hdl->fs && hdl->fs->fs_ops &&
-                hdl->fs->fs_ops->close)
+            if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->close)
                 hdl->fs->fs_ops->close(hdl);
         }
 
         delete_from_epoll_handles(hdl);
 
-        if (hdl->fs && hdl->fs->fs_ops &&
-            hdl->fs->fs_ops->hput)
+        if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->hput)
             hdl->fs->fs_ops->hput(hdl);
 
         qstrfree(&hdl->path);
@@ -536,8 +494,7 @@ void put_handle (struct shim_handle * hdl)
     }
 }
 
-off_t get_file_size (struct shim_handle * hdl)
-{
+off_t get_file_size(struct shim_handle* hdl) {
     if (!hdl->fs || !hdl->fs->fs_ops)
         return -EINVAL;
 
@@ -555,17 +512,15 @@ off_t get_file_size (struct shim_handle * hdl)
     return 0;
 }
 
-void dup_fd_handle (struct shim_handle_map * map,
-                    const struct shim_fd_handle * old,
-                    struct shim_fd_handle * new)
-{
-    struct shim_handle * replaced = NULL;
+void dup_fd_handle(struct shim_handle_map* map, const struct shim_fd_handle* old,
+                   struct shim_fd_handle* new) {
+    struct shim_handle* replaced = NULL;
 
     lock(&map->lock);
 
     if (old->vfd != FD_NULL) {
         get_handle(old->handle);
-        replaced = new->handle;
+        replaced    = new->handle;
         new->handle = old->handle;
     }
 
@@ -575,10 +530,8 @@ void dup_fd_handle (struct shim_handle_map * map,
         put_handle(replaced);
 }
 
-static struct shim_handle_map * get_new_handle_map (FDTYPE size)
-{
-    struct shim_handle_map * handle_map =
-        calloc(1, sizeof(struct shim_handle_map));
+static struct shim_handle_map* get_new_handle_map(FDTYPE size) {
+    struct shim_handle_map* handle_map = calloc(1, sizeof(struct shim_handle_map));
 
     if (!handle_map)
         return NULL;
@@ -597,35 +550,29 @@ static struct shim_handle_map * get_new_handle_map (FDTYPE size)
     return handle_map;
 }
 
-static struct shim_handle_map * __enlarge_handle_map
-                     (struct shim_handle_map * map, FDTYPE size)
-{
+static struct shim_handle_map* __enlarge_handle_map(struct shim_handle_map* map, FDTYPE size) {
     if (size <= map->fd_size)
         return map;
 
-    struct shim_fd_handle ** new_map = calloc(size, sizeof(new_map[0]));
+    struct shim_fd_handle** new_map = calloc(size, sizeof(new_map[0]));
 
     if (!new_map)
         return NULL;
 
     memcpy(new_map, map->map, map->fd_size * sizeof(new_map[0]));
-    memset(new_map + map->fd_size, 0,
-           (size - map->fd_size) * sizeof(new_map[0]));
+    memset(new_map + map->fd_size, 0, (size - map->fd_size) * sizeof(new_map[0]));
     free(map->map);
-    map->map = new_map;
+    map->map     = new_map;
     map->fd_size = size;
     return map;
 }
 
-int dup_handle_map (struct shim_handle_map ** new,
-                    struct shim_handle_map * old_map)
-{
+int dup_handle_map(struct shim_handle_map** new, struct shim_handle_map* old_map) {
     lock(&old_map->lock);
 
     /* allocate a new handle mapping with the same size as
        the old one */
-    struct shim_handle_map * new_map =
-                get_new_handle_map(old_map->fd_size);
+    struct shim_handle_map* new_map = get_new_handle_map(old_map->fd_size);
 
     if (!new_map)
         return -ENOMEM;
@@ -636,14 +583,14 @@ int dup_handle_map (struct shim_handle_map ** new,
         goto done;
 
     for (int i = 0; i <= old_map->fd_top; i++) {
-        struct shim_fd_handle * fd_old = old_map->map[i];
-        struct shim_fd_handle * fd_new;
+        struct shim_fd_handle* fd_old = old_map->map[i];
+        struct shim_fd_handle* fd_new;
 
         /* now we go through the handle map and reassign each
            of them being allocated */
         if (HANDLE_ALLOCATED(fd_old)) {
             /* first, get the handle to prevent it from being deleted */
-            struct shim_handle * hdl = fd_old->handle;
+            struct shim_handle* hdl = fd_old->handle;
             get_handle(hdl);
 
             fd_new = malloc(sizeof(struct shim_fd_handle));
@@ -660,9 +607,9 @@ int dup_handle_map (struct shim_handle_map ** new,
 
             /* DP: I assume we really need a deep copy of the handle map? */
             new_map->map[i] = fd_new;
-            fd_new->vfd    = fd_old->vfd;
-            fd_new->handle = hdl;
-            fd_new->flags  = fd_old->flags;
+            fd_new->vfd     = fd_old->vfd;
+            fd_new->handle  = hdl;
+            fd_new->flags   = fd_old->flags;
         }
     }
 
@@ -672,25 +619,23 @@ done:
     return 0;
 }
 
-void get_handle_map (struct shim_handle_map * map)
-{
+void get_handle_map(struct shim_handle_map* map) {
     REF_INC(map->ref_count);
 }
 
-void put_handle_map (struct shim_handle_map * map)
-{
+void put_handle_map(struct shim_handle_map* map) {
     int ref_count = REF_DEC(map->ref_count);
 
     if (!ref_count) {
         if (map->fd_top == FD_NULL)
             goto done;
 
-        for (int i = 0 ; i <= map->fd_top ; i++) {
+        for (int i = 0; i <= map->fd_top; i++) {
             if (!map->map[i])
                 continue;
 
             if (map->map[i]->vfd != FD_NULL) {
-                struct shim_handle * handle = map->map[i]->handle;
+                struct shim_handle* handle = map->map[i]->handle;
 
                 if (handle)
                     put_handle(handle);
@@ -699,15 +644,14 @@ void put_handle_map (struct shim_handle_map * map)
             free(map->map[i]);
         }
 
-done:
+    done:
         destroy_lock(&map->lock);
         free(map->map);
         free(map);
     }
 }
 
-int flush_handle_map (struct shim_handle_map * map)
-{
+int flush_handle_map(struct shim_handle_map* map) {
     get_handle_map(map);
     lock(&map->lock);
 
@@ -715,11 +659,11 @@ int flush_handle_map (struct shim_handle_map * map)
         goto done;
 
     /* now we go through the handle map and flush each handle */
-    for (int i = 0 ; i <= map->fd_top ; i++) {
+    for (int i = 0; i <= map->fd_top; i++) {
         if (!HANDLE_ALLOCATED(map->map[i]))
             continue;
 
-        struct shim_handle * handle = map->map[i]->handle;
+        struct shim_handle* handle = map->map[i]->handle;
 
         if (handle)
             flush_handle(handle);
@@ -731,21 +675,19 @@ done:
     return 0;
 }
 
-int walk_handle_map (int (*callback) (struct shim_fd_handle *,
-                                      struct shim_handle_map *),
-                     struct shim_handle_map * map)
-{
+int walk_handle_map(int (*callback)(struct shim_fd_handle*, struct shim_handle_map*),
+                    struct shim_handle_map* map) {
     int ret = 0;
     lock(&map->lock);
 
     if (map->fd_top == FD_NULL)
         goto done;
 
-    for (int i = 0 ; i <= map->fd_top ; i++) {
+    for (int i = 0; i <= map->fd_top; i++) {
         if (!HANDLE_ALLOCATED(map->map[i]))
             continue;
 
-        if ((ret = (*callback) (map->map[i], map)) < 0)
+        if ((ret = (*callback)(map->map[i], map)) < 0)
             break;
     }
 
@@ -754,23 +696,22 @@ done:
     return ret;
 }
 
-BEGIN_CP_FUNC(handle)
-{
+BEGIN_CP_FUNC(handle) {
     assert(size == sizeof(struct shim_handle));
 
-    struct shim_handle * hdl = (struct shim_handle *) obj;
-    struct shim_handle * new_hdl = NULL;
+    struct shim_handle* hdl     = (struct shim_handle*)obj;
+    struct shim_handle* new_hdl = NULL;
 
     ptr_t off = GET_FROM_CP_MAP(obj);
 
     if (!off) {
         off = ADD_CP_OFFSET(sizeof(struct shim_handle));
         ADD_TO_CP_MAP(obj, off);
-        new_hdl = (struct shim_handle *) (base + off);
+        new_hdl = (struct shim_handle*)(base + off);
 
         lock(&hdl->lock);
-        struct shim_mount * fs = hdl->fs;
-        *new_hdl = *hdl;
+        struct shim_mount* fs = hdl->fs;
+        *new_hdl              = *hdl;
 
         if (fs && fs->fs_ops && fs->fs_ops->checkout)
             fs->fs_ops->checkout(new_hdl);
@@ -792,9 +733,9 @@ BEGIN_CP_FUNC(handle)
             DO_CP_MEMBER(dentry, hdl, new_hdl, dentry);
 
         if (new_hdl->pal_handle) {
-            struct shim_palhdl_entry * entry;
+            struct shim_palhdl_entry* entry;
             DO_CP(palhdl, hdl->pal_handle, &entry);
-            entry->uri = &new_hdl->uri;
+            entry->uri     = &new_hdl->uri;
             entry->phandle = &new_hdl->pal_handle;
         }
 
@@ -806,18 +747,16 @@ BEGIN_CP_FUNC(handle)
         unlock(&hdl->lock);
         ADD_CP_FUNC_ENTRY(off);
     } else {
-        new_hdl = (struct shim_handle *) (base + off);
+        new_hdl = (struct shim_handle*)(base + off);
     }
 
     if (objp)
-        *objp = (void *) new_hdl;
-
+        *objp = (void*)new_hdl;
 }
 END_CP_FUNC(handle)
 
-BEGIN_RS_FUNC(handle)
-{
-    struct shim_handle * hdl = (void *) (base + GET_CP_FUNC_ENTRY());
+BEGIN_RS_FUNC(handle) {
+    struct shim_handle* hdl = (void*)(base + GET_CP_FUNC_ENTRY());
     __UNUSED(offset);
 
     CP_REBASE(hdl->fs);
@@ -833,68 +772,61 @@ BEGIN_RS_FUNC(handle)
             return -EINVAL;
     }
 
-    if (hdl->fs && hdl->fs->fs_ops &&
-        hdl->fs->fs_ops->checkin)
+    if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->checkin)
         hdl->fs->fs_ops->checkin(hdl);
 
-    DEBUG_RS("path=%s,type=%s,uri=%s,flags=%03o",
-             qstrgetstr(&hdl->path), hdl->fs_type, qstrgetstr(&hdl->uri),
-             hdl->flags);
+    DEBUG_RS("path=%s,type=%s,uri=%s,flags=%03o", qstrgetstr(&hdl->path), hdl->fs_type,
+             qstrgetstr(&hdl->uri), hdl->flags);
 }
 END_RS_FUNC(handle)
 
-BEGIN_CP_FUNC(fd_handle)
-{
+BEGIN_CP_FUNC(fd_handle) {
     assert(size == sizeof(struct shim_fd_handle));
 
-    struct shim_fd_handle * fdhdl = (struct shim_fd_handle *) obj;
-    struct shim_fd_handle * new_fdhdl = NULL;
+    struct shim_fd_handle* fdhdl     = (struct shim_fd_handle*)obj;
+    struct shim_fd_handle* new_fdhdl = NULL;
 
     ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_fd_handle));
-    new_fdhdl = (struct shim_fd_handle *) (base + off);
+    new_fdhdl = (struct shim_fd_handle*)(base + off);
     memcpy(new_fdhdl, fdhdl, sizeof(struct shim_fd_handle));
     DO_CP(handle, fdhdl->handle, &new_fdhdl->handle);
     ADD_CP_FUNC_ENTRY(off);
 
     if (objp)
-        *objp = (void *) new_fdhdl;
+        *objp = (void*)new_fdhdl;
 }
 END_CP_FUNC_NO_RS(fd_handle)
 
-BEGIN_CP_FUNC(handle_map)
-{
+BEGIN_CP_FUNC(handle_map) {
     assert(size >= sizeof(struct shim_handle_map));
 
-    struct shim_handle_map * handle_map = (struct shim_handle_map *) obj;
-    struct shim_handle_map * new_handle_map = NULL;
-    struct shim_fd_handle ** ptr_array;
+    struct shim_handle_map* handle_map     = (struct shim_handle_map*)obj;
+    struct shim_handle_map* new_handle_map = NULL;
+    struct shim_fd_handle** ptr_array;
 
     lock(&handle_map->lock);
 
-    int fd_size = handle_map->fd_top != FD_NULL ?
-                  handle_map->fd_top + 1 : 0;
+    int fd_size = handle_map->fd_top != FD_NULL ? handle_map->fd_top + 1 : 0;
 
-    size = sizeof(struct shim_handle_map) +
-           (sizeof(struct shim_fd_handle *) * fd_size);
+    size = sizeof(struct shim_handle_map) + (sizeof(struct shim_fd_handle*) * fd_size);
 
     ptr_t off = GET_FROM_CP_MAP(obj);
 
     if (!off) {
-        off = ADD_CP_OFFSET(size);
-        new_handle_map = (struct shim_handle_map *) (base + off);
+        off            = ADD_CP_OFFSET(size);
+        new_handle_map = (struct shim_handle_map*)(base + off);
 
-        memcpy(new_handle_map, handle_map,
-               sizeof(struct shim_handle_map));
+        memcpy(new_handle_map, handle_map, sizeof(struct shim_handle_map));
 
-        ptr_array = (void *) new_handle_map + sizeof(struct shim_handle_map);
+        ptr_array = (void*)new_handle_map + sizeof(struct shim_handle_map);
 
         new_handle_map->fd_size = fd_size;
-        new_handle_map->map = fd_size ? ptr_array : NULL;
+        new_handle_map->map     = fd_size ? ptr_array : NULL;
 
         REF_SET(new_handle_map->ref_count, 0);
         clear_lock(&new_handle_map->lock);
 
-        for (int i = 0 ; i < fd_size ; i++) {
+        for (int i = 0; i < fd_size; i++) {
             if (HANDLE_ALLOCATED(handle_map->map[i]))
                 DO_CP(fd_handle, handle_map->map[i], &ptr_array[i]);
             else
@@ -903,19 +835,18 @@ BEGIN_CP_FUNC(handle_map)
 
         ADD_CP_FUNC_ENTRY(off);
     } else {
-        new_handle_map = (struct shim_handle_map *) (base + off);
+        new_handle_map = (struct shim_handle_map*)(base + off);
     }
 
     unlock(&handle_map->lock);
 
     if (objp)
-        *objp = (void *) new_handle_map;
+        *objp = (void*)new_handle_map;
 }
 END_CP_FUNC(handle_map)
 
-BEGIN_RS_FUNC(handle_map)
-{
-    struct shim_handle_map * handle_map = (void *) (base + GET_CP_FUNC_ENTRY());
+BEGIN_RS_FUNC(handle_map) {
+    struct shim_handle_map* handle_map = (void*)(base + GET_CP_FUNC_ENTRY());
     __UNUSED(offset);
 
     CP_REBASE(handle_map->map);
@@ -927,15 +858,14 @@ BEGIN_RS_FUNC(handle_map)
     lock(&handle_map->lock);
 
     if (handle_map->fd_top != FD_NULL)
-        for (int i = 0 ; i <= handle_map->fd_top ; i++) {
+        for (int i = 0; i <= handle_map->fd_top; i++) {
             CP_REBASE(handle_map->map[i]);
             if (HANDLE_ALLOCATED(handle_map->map[i])) {
                 CP_REBASE(handle_map->map[i]->handle);
-                struct shim_handle * hdl = handle_map->map[i]->handle;
+                struct shim_handle* hdl = handle_map->map[i]->handle;
                 assert(hdl);
                 get_handle(hdl);
-                DEBUG_RS("[%d]%s", i, qstrempty(&hdl->uri) ? hdl->fs_type :
-                                      qstrgetstr(&hdl->uri));
+                DEBUG_RS("[%d]%s", i, qstrempty(&hdl->uri) ? hdl->fs_type : qstrgetstr(&hdl->uri));
             }
         }
 

+ 31 - 37
LibOS/shim/src/elf/dl-machine-x86_64.h

@@ -29,9 +29,7 @@
 #include "ldsodefs.h"
 
 /* Return nonzero iff ELF header is compatible with the running host.  */
-static inline int __attribute__ ((unused))
-elf_machine_matches_host (const Elf64_Ehdr * ehdr)
-{
+static inline int __attribute__((unused)) elf_machine_matches_host(const Elf64_Ehdr* ehdr) {
     return ehdr->e_machine == EM_X86_64;
 }
 
@@ -40,14 +38,14 @@ elf_machine_matches_host (const Elf64_Ehdr * ehdr)
    define the value.
    ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
    of the main executable's symbols, as for a COPY reloc.  */
-# define elf_machine_type_class(type)                     \
-  ((((type) == R_X86_64_JUMP_SLOT                         \
-     || (type) == R_X86_64_DTPMOD64                       \
-     || (type) == R_X86_64_DTPOFF64                       \
-     || (type) == R_X86_64_TPOFF64                        \
-     || (type) == R_X86_64_TLSDESC)                       \
-    * ELF_RTYPE_CLASS_PLT)                                \
-   | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
+#define elf_machine_type_class(type) \
+    ((((type) == R_X86_64_JUMP_SLOT  \
+    || (type) == R_X86_64_DTPMOD64   \
+    || (type) == R_X86_64_DTPOFF64   \
+    || (type) == R_X86_64_TPOFF64    \
+    || (type) == R_X86_64_TLSDESC)   \
+    * ELF_RTYPE_CLASS_PLT)           \
+    | (((type) == R_X86_64_COPY) * ELF_RTYPE_CLASS_COPY))
 
 /* The x86-64 never uses Elf64_Rel relocations.  */
 #define ELF_MACHINE_NO_REL 1
@@ -57,24 +55,22 @@ elf_machine_matches_host (const Elf64_Ehdr * ehdr)
 
 //#define DEBUG_RELOC
 
-static bool
-elf_machine_rela (struct link_map * l, ElfW(Rela) * reloc, Elf64_Sym * sym,
-                  void * const reloc_addr_arg)
-{
-    Elf64_Addr * const reloc_addr = reloc_addr_arg;
-    const unsigned long int r_type = ELF64_R_TYPE (reloc->r_info);
+static bool elf_machine_rela(struct link_map* l, ElfW(Rela) * reloc, Elf64_Sym* sym,
+                             void* const reloc_addr_arg) {
+    Elf64_Addr* const reloc_addr   = reloc_addr_arg;
+    const unsigned long int r_type = ELF64_R_TYPE(reloc->r_info);
 
-    const char * __attribute__ ((unused)) strtab =
-                            (const void *) D_PTR (l->l_info[DT_STRTAB]);
+    const char* __attribute__((unused)) strtab = (const void*)D_PTR(l->l_info[DT_STRTAB]);
 
 #ifdef DEBUG_RELOC
-#define debug_reloc(r_type, sym, value)                             \
-    ({  if (strtab && (sym) && (sym)->st_name)                      \
-            debug(#r_type ": %s\n", strtab + (sym)->st_name);       \
-        else if (value)                                             \
-            debug(#r_type ": %p\n", (value));                       \
-        else                                                        \
-            debug(#r_type "\n", (value));                           \
+#define debug_reloc(r_type, sym, value)                       \
+    ({                                                        \
+        if (strtab && (sym) && (sym)->st_name)                \
+            debug(#r_type ": %s\n", strtab + (sym)->st_name); \
+        else if (value)                                       \
+            debug(#r_type ": %p\n", (value));                 \
+        else                                                  \
+            debug(#r_type "\n", (value));                     \
     })
 #else
 #define debug_reloc(...) ({})
@@ -83,7 +79,7 @@ elf_machine_rela (struct link_map * l, ElfW(Rela) * reloc, Elf64_Sym * sym,
     if (r_type == R_X86_64_RELATIVE || r_type == R_X86_64_NONE)
         return false;
 
-    Elf64_Sym * refsym = sym;
+    Elf64_Sym* refsym = sym;
     Elf64_Addr value;
     Elf64_Addr sym_map = RESOLVE_MAP(&strtab, &sym);
 
@@ -99,10 +95,9 @@ elf_machine_rela (struct link_map * l, ElfW(Rela) * reloc, Elf64_Sym * sym,
     refsym->st_info = sym->st_info;
     refsym->st_size = sym->st_size;
 
-    if (__builtin_expect (ELFW(ST_TYPE) (sym->st_info)
-                          == STT_GNU_IFUNC, 0)
-        && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)) {
-        value = ((Elf64_Addr (*) (void)) value) ();
+    if (__builtin_expect(ELFW(ST_TYPE)(sym->st_info) == STT_GNU_IFUNC, 0) &&
+        __builtin_expect(sym->st_shndx != SHN_UNDEF, 1)) {
+        value = ((Elf64_Addr(*)(void))value)();
 
         refsym->st_info ^= ELFW(ST_TYPE)(sym->st_info);
         refsym->st_info |= STT_FUNC;
@@ -111,17 +106,16 @@ elf_machine_rela (struct link_map * l, ElfW(Rela) * reloc, Elf64_Sym * sym,
     debug_reloc("shim symbol", sym, value);
 
     refsym->st_value = value - l->l_addr;
-    *reloc_addr = value +
-        ((r_type == R_X86_64_GLOB_DAT ||
-          r_type == R_X86_64_JUMP_SLOT ||
-          r_type == R_X86_64_64) ? reloc->r_addend : 0);
+    *reloc_addr      = value + ((r_type == R_X86_64_GLOB_DAT || r_type == R_X86_64_JUMP_SLOT ||
+                            r_type == R_X86_64_64)
+                               ? reloc->r_addend
+                               : 0);
 
     /* We have relocated the symbol, we don't want the
        interpreter to relocate it again. */
     if (r_type != R_X86_64_NONE) {
         PROTECT_PAGE(l, reloc, sizeof(*reloc));
-        reloc->r_info = (reloc->r_info ^ ELF64_R_TYPE (reloc->r_info))|
-            R_X86_64_NONE;
+        reloc->r_info = (reloc->r_info ^ ELF64_R_TYPE(reloc->r_info)) | R_X86_64_NONE;
     }
 
     return true;

+ 27 - 34
LibOS/shim/src/elf/do-rel.h

@@ -24,70 +24,63 @@
 
 #include "dl-machine-x86_64.h"
 
-#define elf_dynamic_do_rel          elf_dynamic_do_rela
-#define RELCOUNT_IDX                VERSYMIDX(DT_RELACOUNT)
-#define Rel                         Rela
-#define elf_machine_rel             elf_machine_rela
-#define elf_machine_rel_relative    elf_machine_rela_relative
-#define elf_dynamic_redo_rel        elf_dynamic_redo_rela
+#define elf_dynamic_do_rel       elf_dynamic_do_rela
+#define RELCOUNT_IDX             VERSYMIDX(DT_RELACOUNT)
+#define Rel                      Rela
+#define elf_machine_rel          elf_machine_rela
+#define elf_machine_rel_relative elf_machine_rela_relative
+#define elf_dynamic_redo_rel     elf_dynamic_redo_rela
 
 #ifndef DO_ELF_MACHINE_REL_RELATIVE
-# define DO_ELF_MACHINE_REL_RELATIVE(l, relative)           \
-    elf_machine_rel_relative(l, relative,                   \
-                             (void*)((l)->l_addr + relative->r_offset))
+#define DO_ELF_MACHINE_REL_RELATIVE(l, relative) \
+    elf_machine_rel_relative(l, relative, (void*)((l)->l_addr + relative->r_offset))
 #endif
 
 #ifndef VERSYMIDX
-# define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
+#define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
 #endif
 
 #ifndef VALIDX
-# define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM    \
-                      + DT_EXTRANUM + DT_VALTAGIDX(tag))
+#define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALTAGIDX(tag))
 #endif
 
-#define elf_dynamic_copy_rel        elf_dynamic_copy_rela
-#define dt_reloc                    DT_RELA
-#define dt_reloc_sz                 DT_RELASZ
+#define elf_dynamic_copy_rel elf_dynamic_copy_rela
+#define dt_reloc             DT_RELA
+#define dt_reloc_sz          DT_RELASZ
 
 /* Perform the relocations in MAP on the running program image as specified
    by RELTAG, SZTAG.  If LAZY is nonzero, this is the first pass on PLT
    relocations; they should be set up to call _dl_runtime_resolve, rather
    than fully resolved now.  */
 static void __attribute__((unused))
-elf_dynamic_do_rel (struct link_map * l, ElfW(Addr) reladdr, size_t relsize)
-{
+elf_dynamic_do_rel(struct link_map* l, ElfW(Addr) reladdr, size_t relsize) {
     if (!l->l_info[DT_SYMTAB])
         return;
 
-    ElfW(Sym) * symtab = (void *) D_PTR (l->l_info[DT_SYMTAB]);
-    ElfW(Rel) * r = (void *) reladdr;
-    ElfW(Rel) * end = (void *) (reladdr + relsize);
-    ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX] == NULL
-                           ? 0 : l->l_info[RELCOUNT_IDX]->d_un.d_val;
-    size_t nrelsize = relsize / sizeof (ElfW(Rel));
+    ElfW(Sym)* symtab = (void*)D_PTR(l->l_info[DT_SYMTAB]);
+    ElfW(Rel)* r      = (void*)reladdr;
+    ElfW(Rel)* end    = (void*)(reladdr + relsize);
+    ElfW(Word) nrelative =
+        l->l_info[RELCOUNT_IDX] == NULL ? 0 : l->l_info[RELCOUNT_IDX]->d_un.d_val;
+    size_t nrelsize = relsize / sizeof(ElfW(Rel));
 
     r = r + (nrelative < nrelsize ? nrelative : nrelsize);
     for (; r < end; ++r) {
-        ElfW(Sym) * sym = &symtab[ELFW(R_SYM) (r->r_info)];
-        void * reloc = (void *) l->l_addr + r->r_offset;
+        ElfW(Sym)* sym = &symtab[ELFW(R_SYM)(r->r_info)];
+        void* reloc    = (void*)l->l_addr + r->r_offset;
         if (elf_machine_rel(l, r, sym, reloc)) {
             assert(l->nlinksyms < MAX_LINKSYMS);
-            l->linksyms[l->nlinksyms].rel = r;
-            l->linksyms[l->nlinksyms].sym = sym;
+            l->linksyms[l->nlinksyms].rel   = r;
+            l->linksyms[l->nlinksyms].sym   = sym;
             l->linksyms[l->nlinksyms].reloc = reloc;
             l->nlinksyms++;
         }
     }
 }
 
-static void __attribute__((unused))
-elf_dynamic_redo_rel (struct link_map * l)
-{
-    for (int i = 0 ; i < l->nlinksyms ; i++)
-        elf_machine_rel(l, l->linksyms[i].rel,
-                        l->linksyms[i].sym,
-                        l->linksyms[i].reloc);
+static void __attribute__((unused)) elf_dynamic_redo_rel(struct link_map* l) {
+    for (int i = 0; i < l->nlinksyms; i++)
+        elf_machine_rel(l, l->linksyms[i].rel, l->linksyms[i].sym, l->linksyms[i].reloc);
 }
 
 #if 0

+ 10 - 12
LibOS/shim/src/elf/ldsodefs.h

@@ -5,13 +5,13 @@
 
 /* We use this macro to refer to ELF types independent of the native wordsize.
    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
-#define ElfW(type)	_ElfW (Elf, __ELF_NATIVE_CLASS, type)
-#define _ElfW(e,w,t)	_ElfW_1 (e, w, _##t)
-#define _ElfW_1(e,w,t)	e##w##t
+#define ElfW(type)       _ElfW(Elf, __ELF_NATIVE_CLASS, type)
+#define _ElfW(e, w, t)   _ElfW_1(e, w, _##t)
+#define _ElfW_1(e, w, t) e##w##t
 
 /* We use this macro to refer to ELF types independent of the native wordsize.
    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
-#define ELFW(type)	_ElfW (ELF, __ELF_NATIVE_CLASS, type)
+#define ELFW(type) _ElfW(ELF, __ELF_NATIVE_CLASS, type)
 
 /* We don't like the link_map form ld.so. This macro will be redefined */
 #define D_PTR(sym) (sym)->d_un.d_ptr
@@ -20,13 +20,11 @@
    uses PROT_xxx.  In most cases the three macros have the values 1, 2,
    and 3 but not in a matching order.  The following macros allows
    converting from the PF_x values to PROT_xxx values.  */
-#define PF_TO_PROT \
-  ((PROT_READ << (PF_R * 4))                                                  \
-   | (PROT_WRITE << (PF_W * 4))                                               \
-   | (PROT_EXEC << (PF_X * 4))                                                \
-   | ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4))                        \
-   | ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4))                         \
-   | ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4)                          \
-   | ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
+#define PF_TO_PROT                                                                        \
+    ((PROT_READ << (PF_R * 4)) | (PROT_WRITE << (PF_W * 4)) | (PROT_EXEC << (PF_X * 4)) | \
+     ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) |                                  \
+     ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) |                                   \
+     ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) |                                    \
+     ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
 
 #endif /* ldsodefs.h */

+ 105 - 116
LibOS/shim/src/elf/rel.h

@@ -1,36 +1,33 @@
 #include "elf.h"
 
 #ifndef VERSYMIDX
-# define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
+#define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
 #endif
 
 #ifndef DT_THISPROCNUM
-# define DT_THISPROCNUM 0
+#define DT_THISPROCNUM 0
 #endif
 
 #if __ELF_NATIVE_CLASS == 32
-typedef Elf32_Word  d_tag_utype, d_val_utype;
+typedef Elf32_Word d_tag_utype, d_val_utype;
 #elif __ELF_NATIVE_CLASS == 64
 typedef Elf64_Xword d_tag_utype, d_val_utype;
 #endif
 
-#define IN_RANGE(l, addr)   \
-        ((ElfW(Addr)) (addr) >= (l)->l_map_start && (ElfW(Addr)) (addr) < (l)->l_map_end)
+#define IN_RANGE(l, addr) \
+    ((ElfW(Addr))(addr) >= (l)->l_map_start && (ElfW(Addr))(addr) < (l)->l_map_end)
 
-#define RELOCATE(l, addr)   \
-        ((__typeof__(addr)) (IN_RANGE((l), (addr)) ? (ElfW(Addr)) (addr) :    \
-            (ElfW(Addr)) (addr) + (ElfW(Addr)) ((l)->l_addr)))
+#define RELOCATE(l, addr)                                          \
+    ((__typeof__(addr))(IN_RANGE((l), (addr)) ? (ElfW(Addr))(addr) \
+                                              : (ElfW(Addr))(addr) + (ElfW(Addr))((l)->l_addr)))
 
 #ifdef __x86_64__
-# include "dl-machine-x86_64.h"
+#include "dl-machine-x86_64.h"
 #endif
 
 /* Read the dynamic section at DYN and fill in INFO with indices DT_*.  */
-static inline
-void __attribute__ ((unused, always_inline))
-elf_get_dynamic_info (struct link_map * l)
-{
-    ElfW(Dyn) * dyn = l->l_ld;
+static inline void __attribute__((unused, always_inline)) elf_get_dynamic_info(struct link_map* l) {
+    ElfW(Dyn)* dyn = l->l_ld;
 
     if (dyn == NULL)
         return;
@@ -38,27 +35,25 @@ elf_get_dynamic_info (struct link_map * l)
     while (dyn->d_tag != DT_NULL) {
         int tag = 0;
 
-        if ((d_tag_utype) dyn->d_tag < DT_NUM)
+        if ((d_tag_utype)dyn->d_tag < DT_NUM)
             tag = dyn->d_tag;
 
-        else if (dyn->d_tag >= DT_LOPROC &&
-                 dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
+        else if (dyn->d_tag >= DT_LOPROC && dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
             tag = dyn->d_tag - DT_LOPROC + DT_NUM;
 
-        else if ((d_tag_utype) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
-            tag = VERSYMIDX (dyn->d_tag);
+        else if ((d_tag_utype)DT_VERSIONTAGIDX(dyn->d_tag) < DT_VERSIONTAGNUM)
+            tag = VERSYMIDX(dyn->d_tag);
 
-        else if ((d_tag_utype) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
-            tag = DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
-                  + DT_VERSIONTAGNUM;
+        else if ((d_tag_utype)DT_EXTRATAGIDX(dyn->d_tag) < DT_EXTRANUM)
+            tag = DT_EXTRATAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM;
 
-        else if ((d_tag_utype) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
-            tag = DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
-                  + DT_VERSIONTAGNUM + DT_EXTRANUM;
+        else if ((d_tag_utype)DT_VALTAGIDX(dyn->d_tag) < DT_VALNUM)
+            tag =
+                DT_VALTAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM;
 
-        else if ((d_tag_utype) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
-            tag = DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
-                  + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM;
+        else if ((d_tag_utype)DT_ADDRTAGIDX(dyn->d_tag) < DT_ADDRNUM)
+            tag = DT_ADDRTAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM +
+                  DT_EXTRANUM + DT_VALNUM;
 
         if (tag)
             l->l_info[tag] = dyn;
@@ -67,58 +62,57 @@ elf_get_dynamic_info (struct link_map * l)
     }
 
     if (l->l_addr) {
-# define ADJUST_DYN_INFO(tag)                                       \
-        do {                                                        \
-            if (l->l_info[tag] != NULL) {                           \
-                l->l_info[tag]->d_un.d_ptr =                        \
-                RELOCATE(l, l->l_info[tag]->d_un.d_ptr);            \
-                /* debug("relocate info[%d] = %p\n",                \
-                      tag, l->l_info[tag]->d_un.d_ptr); */          \
-            }                                                       \
-        } while(0);
-
-        ADJUST_DYN_INFO (DT_HASH);
-        ADJUST_DYN_INFO (DT_PLTGOT);
-        ADJUST_DYN_INFO (DT_STRTAB);
-        ADJUST_DYN_INFO (DT_SYMTAB);
-
-# if ! ELF_MACHINE_NO_RELA
-        ADJUST_DYN_INFO (DT_RELA);
-# endif
-
-# if ! ELF_MACHINE_NO_REL
-        ADJUST_DYN_INFO (DT_REL);
-# endif
-
-        ADJUST_DYN_INFO (DT_JMPREL);
-        ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
-        ADJUST_DYN_INFO (DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
-                      + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM);
-# undef ADJUST_DYN_INFO
+#define ADJUST_DYN_INFO(tag)                                                      \
+    do {                                                                          \
+        if (l->l_info[tag] != NULL) {                                             \
+            l->l_info[tag]->d_un.d_ptr = RELOCATE(l, l->l_info[tag]->d_un.d_ptr); \
+            /* debug("relocate info[%d] = %p\n",                                  \
+                  tag, l->l_info[tag]->d_un.d_ptr); */                            \
+        }                                                                         \
+    } while (0);
+
+        ADJUST_DYN_INFO(DT_HASH);
+        ADJUST_DYN_INFO(DT_PLTGOT);
+        ADJUST_DYN_INFO(DT_STRTAB);
+        ADJUST_DYN_INFO(DT_SYMTAB);
+
+#if !ELF_MACHINE_NO_RELA
+        ADJUST_DYN_INFO(DT_RELA);
+#endif
+
+#if !ELF_MACHINE_NO_REL
+        ADJUST_DYN_INFO(DT_REL);
+#endif
+
+        ADJUST_DYN_INFO(DT_JMPREL);
+        ADJUST_DYN_INFO(VERSYMIDX(DT_VERSYM));
+        ADJUST_DYN_INFO(DT_ADDRTAGIDX(DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM +
+                        DT_EXTRANUM + DT_VALNUM);
+#undef ADJUST_DYN_INFO
     }
 
     /* Then a bunch of assertion, we could kind of ignore them */
     if (l->l_info[DT_PLTREL] != NULL) {
 #if ELF_MACHINE_NO_RELA
-        assert (l->l_info[DT_PLTREL]->d_un.d_val == DT_REL);
+        assert(l->l_info[DT_PLTREL]->d_un.d_val == DT_REL);
 
 #elif ELF_MACHINE_NO_REL
-        assert (l->l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
+        assert(l->l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
 
 #else
-        assert (l->l_info[DT_PLTREL]->d_un.d_val == DT_REL
-                || l->l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
+        assert(l->l_info[DT_PLTREL]->d_un.d_val == DT_REL ||
+               l->l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
 #endif
     }
 
-#if ! ELF_MACHINE_NO_RELA
+#if !ELF_MACHINE_NO_RELA
     if (l->l_info[DT_RELA] != NULL)
-        assert (l->l_info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
-# endif
+        assert(l->l_info[DT_RELAENT]->d_un.d_val == sizeof(ElfW(Rela)));
+#endif
 
-# if ! ELF_MACHINE_NO_REL
+#if !ELF_MACHINE_NO_REL
     if (l->l_info[DT_REL] != NULL)
-        assert (l->l_info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
+        assert(l->l_info[DT_RELENT]->d_un.d_val == sizeof(ElfW(Rel)));
 #endif
 }
 
@@ -136,106 +130,101 @@ elf_get_dynamic_info (struct link_map * l)
 /* ELF_MACHINE_PLTREL_OVERLAP is only used for s390, powerpc and sparc.
    We will keep it for now */
 
-static void
-_elf_dynamic_do_reloc(struct link_map * l, d_val_utype dt_reloc, d_val_utype dt_reloc_sz,
-                      void (*do_reloc) (struct link_map *, ElfW(Addr), size_t))
-{
-    struct { ElfW(Addr) start, size; } ranges[3];
+static void _elf_dynamic_do_reloc(struct link_map* l, d_val_utype dt_reloc, d_val_utype dt_reloc_sz,
+                                  void (*do_reloc)(struct link_map*, ElfW(Addr), size_t)) {
+    struct {
+        ElfW(Addr) start, size;
+    } ranges[3];
 
     ranges[0].size = ranges[1].size = ranges[2].size = 0;
 
     if (l->l_info[dt_reloc]) {
-        ranges[0].start = D_PTR (l->l_info[dt_reloc]);
-        ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
+        ranges[0].start = D_PTR(l->l_info[dt_reloc]);
+        ranges[0].size  = l->l_info[dt_reloc_sz]->d_un.d_val;
     }
 
     for (int ranges_index = 0; ranges_index < 3; ++ranges_index)
-        (*do_reloc) (l,
-                     ranges[ranges_index].start,
-                     ranges[ranges_index].size);
+        (*do_reloc)(l, ranges[ranges_index].start, ranges[ranges_index].size);
 }
 #else
 /* Now this part is for our x86s machines */
 
 static void __attribute__((unused))
-_elf_dynamic_do_reloc(struct link_map * l, d_val_utype dt_reloc, d_val_utype dt_reloc_sz,
-                      void (*do_reloc) (struct link_map *, ElfW(Addr), size_t))
-{
-    struct { ElfW(Addr) start, size; } ranges[2];
+_elf_dynamic_do_reloc(struct link_map* l, d_val_utype dt_reloc, d_val_utype dt_reloc_sz,
+                      void (*do_reloc)(struct link_map*, ElfW(Addr), size_t)) {
+    struct {
+        ElfW(Addr) start, size;
+    } ranges[2];
     ranges[0].size = ranges[1].size = 0;
     ranges[0].start = ranges[1].start = 0;
 
     if (l->l_info[dt_reloc]) {
-        ranges[0].start = D_PTR (l->l_info[dt_reloc]);
-        ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
+        ranges[0].start = D_PTR(l->l_info[dt_reloc]);
+        ranges[0].size  = l->l_info[dt_reloc_sz]->d_un.d_val;
     }
 
-    if (l->l_info[DT_PLTREL]
-        && l->l_info[DT_PLTREL]->d_un.d_val == dt_reloc) {
-        ElfW(Addr) start = D_PTR (l->l_info[DT_JMPREL]);
+    if (l->l_info[DT_PLTREL] && l->l_info[DT_PLTREL]->d_un.d_val == dt_reloc) {
+        ElfW(Addr) start = D_PTR(l->l_info[DT_JMPREL]);
 
         /* This test does not only detect whether the relocation
            sections are in the right order, it also checks whether
            there is a DT_REL/DT_RELA section.  */
         if (ranges[0].start + ranges[0].size != start) {
             ranges[1].start = start;
-            ranges[1].size = l->l_info[DT_PLTRELSZ]->d_un.d_val;
+            ranges[1].size  = l->l_info[DT_PLTRELSZ]->d_un.d_val;
         } else {
             /* Combine processing the sections.  */
-            assert (ranges[0].start + ranges[0].size == start);
+            assert(ranges[0].start + ranges[0].size == start);
             ranges[0].size += l->l_info[DT_PLTRELSZ]->d_un.d_val;
         }
     }
 
     for (int ranges_index = 0; ranges_index < 2; ++ranges_index)
-        (*do_reloc) (l,
-                     ranges[ranges_index].start,
-                     ranges[ranges_index].size);
+        (*do_reloc)(l, ranges[ranges_index].start, ranges[ranges_index].size);
 }
 #endif
 
-#define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, l)                  \
-    _elf_dynamic_do_reloc(l, DT_##RELOC, DT_##RELOC##SZ,        \
-                          &elf_dynamic_do_##reloc)
+#define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, l) \
+    _elf_dynamic_do_reloc(l, DT_##RELOC, DT_##RELOC##SZ, &elf_dynamic_do_##reloc)
 #define _ELF_DYNAMIC_REDO_RELOC(RELOC, reloc, l) elf_dynamic_redo_##reloc(l)
 
 #if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
-# define _ELF_CHECK_REL 0
+#define _ELF_CHECK_REL 0
 #else
-# define _ELF_CHECK_REL 1
+#define _ELF_CHECK_REL 1
 #endif
 
-#if ! ELF_MACHINE_NO_REL
-# include "do-rel.h"
-# define ELF_DYNAMIC_DO_REL(l) _ELF_DYNAMIC_DO_RELOC (REL, rel, l)
-# define ELF_DYNAMIC_COPY_REL(l1, l2) elf_dynamic_copy_rel(l1, l2)
-# define ELF_DYNAMIC_REDO_REL(l) _ELF_DYNAMIC_REDO_RELOC (REL, rel, l)
+#if !ELF_MACHINE_NO_REL
+#include "do-rel.h"
+#define ELF_DYNAMIC_DO_REL(l)        _ELF_DYNAMIC_DO_RELOC(REL, rel, l)
+#define ELF_DYNAMIC_COPY_REL(l1, l2) elf_dynamic_copy_rel(l1, l2)
+#define ELF_DYNAMIC_REDO_REL(l)      _ELF_DYNAMIC_REDO_RELOC(REL, rel, l)
 #else
 /* nothing to do */
-# define ELF_DYNAMIC_DO_REL(l)
+#define ELF_DYNAMIC_DO_REL(l)
 //# define ELF_DYNAMIC_COPY_REL(l1, l2)
-# define ELF_DYNAMIC_REDO_REL(l)
+#define ELF_DYNAMIC_REDO_REL(l)
 #endif
 
-#if ! ELF_MACHINE_NO_RELA
-# define DO_RELA
-# include "do-rel.h"
-# define ELF_DYNAMIC_DO_RELA(l) _ELF_DYNAMIC_DO_RELOC (RELA, rela, l)
+#if !ELF_MACHINE_NO_RELA
+#define DO_RELA
+#include "do-rel.h"
+#define ELF_DYNAMIC_DO_RELA(l) _ELF_DYNAMIC_DO_RELOC(RELA, rela, l)
 //# define ELF_DYNAMIC_COPY_RELA(l1, l2) elf_dynamic_copy_rela(l, l2)
-# define ELF_DYNAMIC_REDO_RELA(l) _ELF_DYNAMIC_REDO_RELOC (RELA, rela, l)
+#define ELF_DYNAMIC_REDO_RELA(l) _ELF_DYNAMIC_REDO_RELOC(RELA, rela, l)
 #else
 /* nothing to do */
-# define ELF_DYNAMIC_DO_RELA(l)
+#define ELF_DYNAMIC_DO_RELA(l)
 //# define ELF_DYNAMIC_COPY_RELA(l1, l2)
-# define ELF_DYNAMIC_REDO_RELA(l)
+#define ELF_DYNAMIC_REDO_RELA(l)
 #endif
 
 /* This can't just be an inline function because GCC is too dumb
    to inline functions containing inlines themselves.  */
-# define ELF_DYNAMIC_RELOCATE(l)       \
-    do {                               \
-        ELF_DYNAMIC_DO_REL(l);         \
-        ELF_DYNAMIC_DO_RELA(l);        \
+#define ELF_DYNAMIC_RELOCATE(l) \
+    do {                        \
+        ELF_DYNAMIC_DO_REL(l);  \
+        ELF_DYNAMIC_DO_RELA(l); \
     } while (0)
 
 #if 0
@@ -246,8 +235,8 @@ _elf_dynamic_do_reloc(struct link_map * l, d_val_utype dt_reloc, d_val_utype dt_
     } while (0)
 #endif
 
-# define ELF_REDO_DYNAMIC_RELOCATE(l)  \
-    do {                               \
-        ELF_DYNAMIC_REDO_REL(l);       \
-        ELF_DYNAMIC_REDO_RELA(l);      \
+#define ELF_REDO_DYNAMIC_RELOCATE(l) \
+    do {                             \
+        ELF_DYNAMIC_REDO_REL(l);     \
+        ELF_DYNAMIC_REDO_RELA(l);    \
     } while (0)

File diff suppressed because it is too large
+ 239 - 282
LibOS/shim/src/elf/shim_rtld.c


+ 142 - 176
LibOS/shim/src/fs/dev/fs.c

@@ -20,43 +20,41 @@
  * This file contains codes for implementation of 'dev' filesystem.
  */
 
-#include <shim_internal.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-#include <shim_utils.h>
-#include <shim_profile.h>
-
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_profile.h>
+#include <shim_utils.h>
 
-#include <errno.h>
-
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
-
-#include <asm/fcntl.h>
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
 
-#define EMPTY_DEV_OPS                       \
-    {                                       \
-        .open       = NULL,                 \
-        .close      = NULL,                 \
-        .read       = NULL,                 \
-        .write      = NULL,                 \
-        .flush      = NULL,                 \
-        .seek       = NULL,                 \
-        .truncate   = NULL,                 \
-        .mode       = NULL,                 \
-        .stat       = NULL,                 \
-        .hstat      = NULL,                 \
+#define EMPTY_DEV_OPS     \
+    {                     \
+        .open     = NULL, \
+        .close    = NULL, \
+        .read     = NULL, \
+        .write    = NULL, \
+        .flush    = NULL, \
+        .seek     = NULL, \
+        .truncate = NULL, \
+        .mode     = NULL, \
+        .stat     = NULL, \
+        .hstat    = NULL, \
     }
 
-#define DEV_INO_BASE   1025
+#define DEV_INO_BASE 1025
 
-static ssize_t dev_null_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+static ssize_t dev_null_read(struct shim_handle* hdl, void* buf, size_t count) {
     // Arguments for compatibility
     __UNUSED(hdl);
     __UNUSED(buf);
@@ -65,8 +63,7 @@ static ssize_t dev_null_read (struct shim_handle * hdl, void * buf, size_t count
     return 0;
 }
 
-static ssize_t dev_zero_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+static ssize_t dev_zero_read(struct shim_handle* hdl, void* buf, size_t count) {
     // Argument for compatibility
     __UNUSED(hdl);
 
@@ -74,8 +71,7 @@ static ssize_t dev_zero_read (struct shim_handle * hdl, void * buf, size_t count
     return count;
 }
 
-static ssize_t dev_null_write (struct shim_handle * hdl, const void * buf, size_t count)
-{
+static ssize_t dev_null_write(struct shim_handle* hdl, const void* buf, size_t count) {
     // Arguments for compatibility
     __UNUSED(hdl);
     __UNUSED(buf);
@@ -84,40 +80,36 @@ static ssize_t dev_null_write (struct shim_handle * hdl, const void * buf, size_
     return count;
 }
 
-static int dev_null_mode (const char * name, mode_t * mode)
-{
-    __UNUSED(name); // We know it is /dev/null
+static int dev_null_mode(const char* name, mode_t* mode) {
+    __UNUSED(name);  // We know it is /dev/null
 
-    *mode = 0666|S_IFCHR;
+    *mode = 0666 | S_IFCHR;
     return 0;
 }
 
-static int dev_null_stat (const char * name, struct stat * stat)
-{
-    __UNUSED(name); // We know it is /dev/null
+static int dev_null_stat(const char* name, struct stat* stat) {
+    __UNUSED(name);  // We know it is /dev/null
 
-    stat->st_mode = 0666|S_IFCHR;
-    stat->st_uid = 0;
-    stat->st_gid = 0;
-    stat->st_size = 0;
+    stat->st_mode    = 0666 | S_IFCHR;
+    stat->st_uid     = 0;
+    stat->st_gid     = 0;
+    stat->st_size    = 0;
     stat->st_blksize = 0;
     return 0;
 }
 
-static int dev_null_hstat (struct shim_handle * hdl, struct stat * stat)
-{
-    __UNUSED(hdl); // We know it is /dev/null
+static int dev_null_hstat(struct shim_handle* hdl, struct stat* stat) {
+    __UNUSED(hdl);  // We know it is /dev/null
 
-    stat->st_mode = 0666|S_IFCHR;
-    stat->st_uid = 0;
-    stat->st_gid = 0;
-    stat->st_size = 0;
+    stat->st_mode    = 0666 | S_IFCHR;
+    stat->st_uid     = 0;
+    stat->st_gid     = 0;
+    stat->st_size    = 0;
     stat->st_blksize = 0;
     return 0;
 }
 
-static int dev_null_truncate (struct shim_handle * hdl, uint64_t size)
-{
+static int dev_null_truncate(struct shim_handle* hdl, uint64_t size) {
     // Arguments for compatibility
     __UNUSED(hdl);
     __UNUSED(size);
@@ -125,16 +117,14 @@ static int dev_null_truncate (struct shim_handle * hdl, uint64_t size)
     return 0;
 }
 
-static int dev_random_mode (const char * name, mode_t * mode)
-{
-    __UNUSED(name); // We know it is /dev/random
+static int dev_random_mode(const char* name, mode_t* mode) {
+    __UNUSED(name);  // We know it is /dev/random
 
-    *mode = 0666|S_IFCHR;
+    *mode = 0666 | S_IFCHR;
     return 0;
 }
 
-static ssize_t dev_urandom_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+static ssize_t dev_urandom_read(struct shim_handle* hdl, void* buf, size_t count) {
     __UNUSED(hdl);
     ssize_t ret = DkRandomBitsRead(buf, count);
 
@@ -143,72 +133,68 @@ static ssize_t dev_urandom_read (struct shim_handle * hdl, void * buf, size_t co
     return count;
 }
 
-static ssize_t dev_random_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+static ssize_t dev_random_read(struct shim_handle* hdl, void* buf, size_t count) {
     return dev_urandom_read(hdl, buf, count);
 }
 
-static int dev_random_stat (const char * name, struct stat * stat)
-{
-    __UNUSED(name); // we know it is /dev/random
+static int dev_random_stat(const char* name, struct stat* stat) {
+    __UNUSED(name);  // we know it is /dev/random
 
-    stat->st_mode = 0666|S_IFCHR;
-    stat->st_uid = 0;
-    stat->st_gid = 0;
-    stat->st_size = 0;
+    stat->st_mode    = 0666 | S_IFCHR;
+    stat->st_uid     = 0;
+    stat->st_gid     = 0;
+    stat->st_size    = 0;
     stat->st_blksize = 0;
     return 0;
 }
 
-static int dev_random_hstat (struct shim_handle * hdl, struct stat * stat)
-{
-    __UNUSED(hdl); // pseudo-device
+static int dev_random_hstat(struct shim_handle* hdl, struct stat* stat) {
+    __UNUSED(hdl);  // pseudo-device
 
-    stat->st_mode = 0444|S_IFCHR;
-    stat->st_uid = 0;
-    stat->st_gid = 0;
-    stat->st_size = 0;
+    stat->st_mode    = 0444 | S_IFCHR;
+    stat->st_uid     = 0;
+    stat->st_gid     = 0;
+    stat->st_size    = 0;
     stat->st_blksize = 0;
     return 0;
 }
 
-static int search_dev_driver (const char * name, struct shim_dev_ops * ops)
-{
+static int search_dev_driver(const char* name, struct shim_dev_ops* ops) {
     if (strcmp_static(name, "null") || strcmp_static(name, "tty")) {
         if (ops)
-            ops->read   = &dev_null_read;
-null_dev:
+            ops->read = &dev_null_read;
+    null_dev:
         if (ops) {
-            ops->write  = &dev_null_write;
+            ops->write    = &dev_null_write;
             ops->truncate = &dev_null_truncate;
-            ops->mode   = &dev_null_mode;
-            ops->stat   = &dev_null_stat;
-            ops->hstat  = &dev_null_hstat;
+            ops->mode     = &dev_null_mode;
+            ops->stat     = &dev_null_stat;
+            ops->hstat    = &dev_null_hstat;
         }
         return 0;
     }
 
     if (strcmp_static(name, "zero")) {
         if (ops)
-            ops->read   = &dev_zero_read;
+            ops->read = &dev_zero_read;
         goto null_dev;
     }
 
     if (strcmp_static(name, "random")) {
         if (ops)
-            ops->read   = &dev_random_read;
-random_dev:
+            ops->read = &dev_random_read;
+    random_dev:
         if (ops) {
-            ops->mode   = &dev_random_mode;
-            ops->stat   = &dev_random_stat;
-            ops->hstat  = &dev_random_hstat;
+            ops->mode  = &dev_random_mode;
+            ops->stat  = &dev_random_stat;
+            ops->hstat = &dev_random_hstat;
         }
         return 0;
     }
 
     if (strcmp_static(name, "urandom")) {
         if (ops)
-            ops->read   = &dev_urandom_read;
+            ops->read = &dev_urandom_read;
         goto random_dev;
     }
 
@@ -219,8 +205,7 @@ random_dev:
     return -ENOENT;
 }
 
-static int dev_mount (const char * uri, void ** mount_data)
-{
+static int dev_mount(const char* uri, void** mount_data) {
     // Arguments for compatibility
     __UNUSED(uri);
     __UNUSED(mount_data);
@@ -229,8 +214,7 @@ static int dev_mount (const char * uri, void ** mount_data)
     return 0;
 }
 
-static int dev_unmount (void * mount_data)
-{
+static int dev_unmount(void* mount_data) {
     // Arguments for compatibility
     __UNUSED(mount_data);
 
@@ -238,11 +222,9 @@ static int dev_unmount (void * mount_data)
     return 0;
 }
 
-static int dev_open (struct shim_handle * hdl, struct shim_dentry * dent,
-                     int flags)
-{
+static int dev_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
     struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
-    int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
+    int ret                     = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
 
     if (ret < 0)
         return ret;
@@ -251,8 +233,7 @@ static int dev_open (struct shim_handle * hdl, struct shim_dentry * dent,
     hdl->flags    = flags & ~O_ACCMODE;
     hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
 
-    memcpy(&hdl->info.dev.dev_ops, &ops_buf,
-           sizeof(struct shim_dev_ops));
+    memcpy(&hdl->info.dev.dev_ops, &ops_buf, sizeof(struct shim_dev_ops));
 
     if (!ops_buf.read && (hdl->acc_mode & MAY_READ))
         return -EACCES;
@@ -266,8 +247,7 @@ static int dev_open (struct shim_handle * hdl, struct shim_dentry * dent,
     return 0;
 }
 
-static int dev_lookup (struct shim_dentry * dent)
-{
+static int dev_lookup(struct shim_dentry* dent) {
     if (qstrempty(&dent->rel_path)) {
         dent->state |= DENTRY_ISDIRECTORY;
         dent->ino = DEV_INO_BASE;
@@ -278,17 +258,16 @@ static int dev_lookup (struct shim_dentry * dent)
     return search_dev_driver(qstrgetstr(&dent->rel_path), NULL);
 }
 
-static int dev_mode (struct shim_dentry * dent, mode_t * mode)
-{
+static int dev_mode(struct shim_dentry* dent, mode_t* mode) {
     if (qstrempty(&dent->rel_path)) {
         dent->ino = DEV_INO_BASE;
-        *mode = 0555|S_IFDIR;
+        *mode     = 0555 | S_IFDIR;
         return 0;
     }
 
     /* we don't care about forced or not */
     struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
-    int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
+    int ret                     = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
 
     if (ret < 0)
         return ret;
@@ -296,59 +275,52 @@ static int dev_mode (struct shim_dentry * dent, mode_t * mode)
     return ops_buf.mode(qstrgetstr(&dent->rel_path), mode);
 }
 
-static int dev_flush (struct shim_handle * hdl)
-{
+static int dev_flush(struct shim_handle* hdl) {
     if (!hdl->info.dev.dev_ops.flush)
         return 0;
 
     return hdl->info.dev.dev_ops.flush(hdl);
 }
 
-static int dev_close (struct shim_handle * hdl)
-{
+static int dev_close(struct shim_handle* hdl) {
     if (!hdl->info.dev.dev_ops.close)
         return 0;
 
     return hdl->info.dev.dev_ops.close(hdl);
 }
 
-static ssize_t dev_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+static ssize_t dev_read(struct shim_handle* hdl, void* buf, size_t count) {
     if (!hdl->info.dev.dev_ops.read)
         return -EACCES;
 
     return hdl->info.dev.dev_ops.read(hdl, buf, count);
 }
 
-static ssize_t dev_write (struct shim_handle * hdl, const void * buf, size_t count)
-{
+static ssize_t dev_write(struct shim_handle* hdl, const void* buf, size_t count) {
     if (!hdl->info.dev.dev_ops.write)
         return -EACCES;
 
     return hdl->info.dev.dev_ops.write(hdl, buf, count);
 }
 
-static off_t dev_seek (struct shim_handle * hdl, off_t offset, int wence)
-{
+static off_t dev_seek(struct shim_handle* hdl, off_t offset, int wence) {
     if (!hdl->info.dev.dev_ops.seek)
         return -EACCES;
 
     return hdl->info.dev.dev_ops.seek(hdl, offset, wence);
 }
 
-static int dev_truncate (struct shim_handle * hdl, off_t len)
-{
+static int dev_truncate(struct shim_handle* hdl, off_t len) {
     if (!hdl->info.dev.dev_ops.truncate)
         return -EACCES;
 
     return hdl->info.dev.dev_ops.truncate(hdl, len);
 }
 
-static int dev_readdir (struct shim_dentry * dent, struct shim_dirent ** dirent)
-{
+static int dev_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
     if (!qstrempty(&dent->rel_path)) {
         struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
-        int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
+        int ret                     = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
 
         if (ret < 0 && ret != -EISLINK)
             return ret;
@@ -356,33 +328,32 @@ static int dev_readdir (struct shim_dentry * dent, struct shim_dirent ** dirent)
         return -ENOTDIR;
     }
 
-    struct shim_dirent * buf, * ptr;
+    struct shim_dirent *buf, *ptr;
     int buf_size = MAX_PATH;
 
 retry:
-    buf = malloc(buf_size);
+    buf     = malloc(buf_size);
     *dirent = ptr = buf;
-    struct shim_dirent ** last = dirent;
-
-#define COPY_ENTRY(devname, devtype)                                \
-    do {                                                            \
-        int name_len = strlen(devname);                             \
-                                                                    \
-        if ((void *) (ptr + 1) + name_len + 1 >                     \
-            (void *) buf + buf_size)                                \
-            goto nomem;                                             \
-                                                                    \
-        ptr->next = (void *) (ptr + 1) + name_len + 1;              \
-        ptr->ino = 1;                                               \
-        ptr->type = (devtype);                                      \
-        memcpy(ptr->name, (devname), name_len + 1);                 \
-        last = &ptr->next;                                          \
-        ptr = ptr->next;                                            \
+    struct shim_dirent** last = dirent;
+
+#define COPY_ENTRY(devname, devtype)                                 \
+    do {                                                             \
+        int name_len = strlen(devname);                              \
+                                                                     \
+        if ((void*)(ptr + 1) + name_len + 1 > (void*)buf + buf_size) \
+            goto nomem;                                              \
+                                                                     \
+        ptr->next = (void*)(ptr + 1) + name_len + 1;                 \
+        ptr->ino  = 1;                                               \
+        ptr->type = (devtype);                                       \
+        memcpy(ptr->name, (devname), name_len + 1);                  \
+        last = &ptr->next;                                           \
+        ptr  = ptr->next;                                            \
     } while (0)
 
-    COPY_ENTRY("null",   LINUX_DT_CHR);
-    COPY_ENTRY("zero",   LINUX_DT_CHR);
-    COPY_ENTRY("stdin",  LINUX_DT_LNK);
+    COPY_ENTRY("null", LINUX_DT_CHR);
+    COPY_ENTRY("zero", LINUX_DT_CHR);
+    COPY_ENTRY("stdin", LINUX_DT_LNK);
     COPY_ENTRY("stdout", LINUX_DT_LNK);
     COPY_ENTRY("stderr", LINUX_DT_LNK);
 #undef COPY_ENTRY
@@ -396,19 +367,18 @@ nomem:
     goto retry;
 }
 
-static int dev_stat (struct shim_dentry * dent, struct stat * buf)
-{
+static int dev_stat(struct shim_dentry* dent, struct stat* buf) {
     if (qstrempty(&dent->rel_path)) {
         buf->st_dev     = DEV_INO_BASE;
         buf->st_ino     = DEV_INO_BASE;
-        buf->st_mode    = 0777|S_IFDIR;
+        buf->st_mode    = 0777 | S_IFDIR;
         buf->st_size    = 4096;
         buf->st_blksize = 4096;
         return 0;
     }
 
     struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
-    int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
+    int ret                     = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
 
     if (ret < 0 && ret != -EISLINK)
         return ret;
@@ -416,29 +386,26 @@ static int dev_stat (struct shim_dentry * dent, struct stat * buf)
     if (ret == -EISLINK) {
         buf->st_dev     = DEV_INO_BASE;
         buf->st_ino     = DEV_INO_BASE;
-        buf->st_mode    = 0777|S_IFLNK;
+        buf->st_mode    = 0777 | S_IFLNK;
         buf->st_size    = 0;
         buf->st_blksize = 0;
         return 0;
     }
 
-    buf->st_dev     = DEV_INO_BASE;
-    buf->st_ino     = DEV_INO_BASE;
+    buf->st_dev = DEV_INO_BASE;
+    buf->st_ino = DEV_INO_BASE;
 
-    return ops_buf.stat ? ops_buf.stat(qstrgetstr(&dent->rel_path), buf) :
-           -EACCES;
+    return ops_buf.stat ? ops_buf.stat(qstrgetstr(&dent->rel_path), buf) : -EACCES;
 }
 
-static int dev_hstat (struct shim_handle * hdl, struct stat * buf)
-{
+static int dev_hstat(struct shim_handle* hdl, struct stat* buf) {
     if (!hdl->info.dev.dev_ops.hstat)
         return -EACCES;
 
     return hdl->info.dev.dev_ops.hstat(hdl, buf);
 }
 
-static off_t dev_poll (struct shim_handle * hdl, int poll_type)
-{
+static off_t dev_poll(struct shim_handle* hdl, int poll_type) {
     if (poll_type == FS_POLL_SZ)
         return 0;
 
@@ -451,9 +418,8 @@ static off_t dev_poll (struct shim_handle * hdl, int poll_type)
     return ret;
 }
 
-static int dev_follow_link (struct shim_dentry * dent, struct shim_qstr * link)
-{
-    const char * name = qstrgetstr(&dent->rel_path);
+static int dev_follow_link(struct shim_dentry* dent, struct shim_qstr* link) {
+    const char* name = qstrgetstr(&dent->rel_path);
 
     if (strcmp_static(name, "stdin")) {
         qstrsetstr(link, "/proc/self/0", static_strlen("/proc/self/0"));
@@ -473,23 +439,23 @@ static int dev_follow_link (struct shim_dentry * dent, struct shim_qstr * link)
 }
 
 struct shim_fs_ops dev_fs_ops = {
-        .mount          = &dev_mount,
-        .unmount        = &dev_unmount,
-        .flush          = &dev_flush,
-        .close          = &dev_close,
-        .read           = &dev_read,
-        .write          = &dev_write,
-        .seek           = &dev_seek,
-        .hstat          = &dev_hstat,
-        .poll           = &dev_poll,
-        .truncate       = &dev_truncate,
-    };
+    .mount    = &dev_mount,
+    .unmount  = &dev_unmount,
+    .flush    = &dev_flush,
+    .close    = &dev_close,
+    .read     = &dev_read,
+    .write    = &dev_write,
+    .seek     = &dev_seek,
+    .hstat    = &dev_hstat,
+    .poll     = &dev_poll,
+    .truncate = &dev_truncate,
+};
 
 struct shim_d_ops dev_d_ops = {
-        .open           = &dev_open,
-        .lookup         = &dev_lookup,
-        .mode           = &dev_mode,
-        .readdir        = &dev_readdir,
-        .stat           = &dev_stat,
-        .follow_link    = &dev_follow_link,
-    };
+    .open        = &dev_open,
+    .lookup      = &dev_lookup,
+    .mode        = &dev_mode,
+    .readdir     = &dev_readdir,
+    .stat        = &dev_stat,
+    .follow_link = &dev_follow_link,
+};

+ 53 - 62
LibOS/shim/src/fs/pipe/fs.c

@@ -20,31 +20,26 @@
  * This file contains codes for implementation of 'pipe' filesystem.
  */
 
-#include <shim_internal.h>
-#include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-#include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-#include <pal_debug.h>
-
-#include <errno.h>
-
-#include <linux/stat.h>
-#include <linux/fcntl.h>
-
 #include <asm/fcntl.h>
 #include <asm/mman.h>
-#include <asm/unistd.h>
 #include <asm/prctl.h>
-#include <asm/fcntl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_debug.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
 #include <shim_profile.h>
+#include <shim_thread.h>
 
-static ssize_t pipe_read (struct shim_handle * hdl, void * buf,
-                          size_t count)
-{
+// TODO: For some reason S_I{R,W}USR macros are missing if this file is included before our headers.
+// We should investigate and fix this behavior.
+#include <linux/stat.h>
+
+static ssize_t pipe_read(struct shim_handle* hdl, void* buf, size_t count) {
     if (!count)
         return 0;
 
@@ -53,27 +48,24 @@ static ssize_t pipe_read (struct shim_handle * hdl, void * buf,
     if (!bytes)
         return -PAL_ERRNO;
 
-    assert((ssize_t) bytes > 0);
-    return (ssize_t) bytes;
+    assert((ssize_t)bytes > 0);
+    return (ssize_t)bytes;
 }
 
-static ssize_t pipe_write (struct shim_handle * hdl, const void * buf,
-                           size_t count)
-{
+static ssize_t pipe_write(struct shim_handle* hdl, const void* buf, size_t count) {
     if (!count)
         return 0;
 
-    PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void *) buf, NULL);
+    PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void*)buf, NULL);
 
     if (!bytes)
         return -PAL_ERRNO;
 
-    assert((ssize_t) bytes > 0);
-    return (ssize_t) bytes;
+    assert((ssize_t)bytes > 0);
+    return (ssize_t)bytes;
 }
 
-static int pipe_hstat (struct shim_handle * hdl, struct stat * stat)
-{
+static int pipe_hstat(struct shim_handle* hdl, struct stat* stat) {
     /* XXX: Is any of this right?
      * Shouldn't we be using hdl to figure something out?
      * if stat is NULL, should we not return -EFAULT?
@@ -82,33 +74,31 @@ static int pipe_hstat (struct shim_handle * hdl, struct stat * stat)
     if (!stat)
         return 0;
 
-    struct shim_thread * thread = get_cur_thread();
-
-    stat->st_dev    = (dev_t) 0;     /* ID of device containing file */
-    stat->st_ino    = (ino_t) 0;     /* inode number */
-    stat->st_nlink  = (nlink_t) 0;   /* number of hard links */
-    stat->st_uid    = (uid_t) thread->uid;    /* user ID of owner */
-    stat->st_gid    = (gid_t) thread->gid;    /* group ID of owner */
-    stat->st_rdev   = (dev_t) 0;     /* device ID (if special file) */
-    stat->st_size   = (off_t) 0;     /* total size, in bytes */
-    stat->st_blksize = 0;            /* blocksize for file system I/O */
-    stat->st_blocks = 0;             /* number of 512B blocks allocated */
-    stat->st_atime  = (time_t) 0;    /* access time */
-    stat->st_mtime  = (time_t) 0;    /* last modification */
-    stat->st_ctime  = (time_t) 0;    /* last status change */
-    stat->st_mode   = S_IRUSR|S_IWUSR|S_IFIFO;
+    struct shim_thread* thread = get_cur_thread();
+
+    stat->st_dev     = (dev_t)0;           /* ID of device containing file */
+    stat->st_ino     = (ino_t)0;           /* inode number */
+    stat->st_nlink   = (nlink_t)0;         /* number of hard links */
+    stat->st_uid     = (uid_t)thread->uid; /* user ID of owner */
+    stat->st_gid     = (gid_t)thread->gid; /* group ID of owner */
+    stat->st_rdev    = (dev_t)0;           /* device ID (if special file) */
+    stat->st_size    = (off_t)0;           /* total size, in bytes */
+    stat->st_blksize = 0;                  /* blocksize for file system I/O */
+    stat->st_blocks  = 0;                  /* number of 512B blocks allocated */
+    stat->st_atime   = (time_t)0;          /* access time */
+    stat->st_mtime   = (time_t)0;          /* last modification */
+    stat->st_ctime   = (time_t)0;          /* last status change */
+    stat->st_mode    = S_IRUSR | S_IWUSR | S_IFIFO;
 
     return 0;
 }
 
-static int pipe_checkout (struct shim_handle * hdl)
-{
+static int pipe_checkout(struct shim_handle* hdl) {
     hdl->fs = NULL;
     return 0;
 }
 
-static off_t pipe_poll (struct shim_handle * hdl, int poll_type)
-{
+static off_t pipe_poll(struct shim_handle* hdl, int poll_type) {
     off_t ret = 0;
 
     lock(&hdl->lock);
@@ -142,8 +132,7 @@ out:
     return ret;
 }
 
-static int pipe_setflags (struct shim_handle * hdl, int flags)
-{
+static int pipe_setflags(struct shim_handle* hdl, int flags) {
     if (!hdl->pal_handle)
         return 0;
 
@@ -165,19 +154,21 @@ static int pipe_setflags (struct shim_handle * hdl, int flags)
     }
 
     if (!DkStreamAttributesSetByHandle(hdl->pal_handle, &attr))
-       return -PAL_ERRNO;
+        return -PAL_ERRNO;
 
     return 0;
 }
 
 struct shim_fs_ops pipe_fs_ops = {
-        .read       = &pipe_read,
-        .write      = &pipe_write,
-        .hstat      = &pipe_hstat,
-        .checkout   = &pipe_checkout,
-        .poll       = &pipe_poll,
-        .setflags   = &pipe_setflags,
-    };
-
-struct shim_mount pipe_builtin_fs = { .type = "pipe",
-                                      .fs_ops = &pipe_fs_ops, };
+    .read     = &pipe_read,
+    .write    = &pipe_write,
+    .hstat    = &pipe_hstat,
+    .checkout = &pipe_checkout,
+    .poll     = &pipe_poll,
+    .setflags = &pipe_setflags,
+};
+
+struct shim_mount pipe_builtin_fs = {
+    .type   = "pipe",
+    .fs_ops = &pipe_fs_ops,
+};

+ 140 - 142
LibOS/shim/src/fs/proc/fs.c

@@ -20,24 +20,23 @@
  * This file contains codes for implementation of 'proc' filesystem.
  */
 
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
-
-#include <asm/fcntl.h>
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
 
 extern const struct proc_nm_ops nm_thread;
 extern const struct proc_fs_ops fs_thread;
@@ -50,71 +49,86 @@ extern const struct proc_fs_ops fs_cpuinfo;
 
 const struct proc_dir proc_root = {
     .size = 5,
-    .ent = {
-        { .name = "self", .fs_ops = &fs_thread, .dir = &dir_thread, },
-        { .nm_ops = &nm_thread, .fs_ops = &fs_thread, .dir = &dir_thread, },
-        { .nm_ops = &nm_ipc_thread, .fs_ops = &fs_ipc_thread,
-          .dir = &dir_ipc_thread, },
-        { .name = "meminfo", .fs_ops = &fs_meminfo, },
-        { .name = "cpuinfo", .fs_ops = &fs_cpuinfo, },
-    }, };
-
-#define PROC_INO_BASE      1
-
-static int proc_root_mode (const char * name, mode_t * mode)
-{
-    __UNUSED(name); // We know this is /proc
+    .ent =
+        {
+            {
+                .name   = "self",
+                .fs_ops = &fs_thread,
+                .dir    = &dir_thread,
+            },
+            {
+                .nm_ops = &nm_thread,
+                .fs_ops = &fs_thread,
+                .dir    = &dir_thread,
+            },
+            {
+                .nm_ops = &nm_ipc_thread,
+                .fs_ops = &fs_ipc_thread,
+                .dir    = &dir_ipc_thread,
+            },
+            {
+                .name   = "meminfo",
+                .fs_ops = &fs_meminfo,
+            },
+            {
+                .name   = "cpuinfo",
+                .fs_ops = &fs_cpuinfo,
+            },
+        },
+};
+
+#define PROC_INO_BASE 1
+
+static int proc_root_mode(const char* name, mode_t* mode) {
+    __UNUSED(name);  // We know this is /proc
     *mode = 0555;
     return 0;
 }
 
-static int proc_root_stat (const char * name, struct stat * buf)
-{
-    __UNUSED(name); // We know this is /proc
+static int proc_root_stat(const char* name, struct stat* buf) {
+    __UNUSED(name);  // We know this is /proc
     memset(buf, 0, sizeof(struct stat));
 
     buf->st_dev = buf->st_ino = 1;
-    buf->st_mode = 0555|S_IFDIR;
-    buf->st_uid = 0;
-    buf->st_gid = 0;
-    buf->st_size = 4096;
+    buf->st_mode              = 0555 | S_IFDIR;
+    buf->st_uid               = 0;
+    buf->st_gid               = 0;
+    buf->st_size              = 4096;
 
     return 0;
 }
 
+static int proc_root_open(struct shim_handle* hdl, const char* name, int flags) {
+    __UNUSED(hdl);   // this is a placeholder function
+    __UNUSED(name);  // We know this is /proc
 
-static int proc_root_open (struct shim_handle * hdl,
-                           const char * name, int flags)
-{
-    __UNUSED(hdl); // this is a placeholder function
-    __UNUSED(name); // We know this is /proc
-
-
-    if (flags & (O_WRONLY|O_RDWR))
+    if (flags & (O_WRONLY | O_RDWR))
         return -EISDIR;
 
     // Don't really need to do any work here, but keeping as a placeholder,
     // just in case.
 
     return 0;
-
 }
 
 struct proc_fs_ops fs_proc_root = {
-        .open     = &proc_root_open,
-        .mode     = &proc_root_mode,
-        .stat     = &proc_root_stat,
-    };
+    .open = &proc_root_open,
+    .mode = &proc_root_mode,
+    .stat = &proc_root_stat,
+};
 
-const struct proc_ent proc_root_ent =
-    { .name = "", .fs_ops = &fs_proc_root, .dir = &proc_root, };
+const struct proc_ent proc_root_ent = {
+    .name   = "",
+    .fs_ops = &fs_proc_root,
+    .dir    = &proc_root,
+};
 
-static inline int token_len (const char * str, const char ** next_str)
-{
-    const char * t = str;
+static inline int token_len(const char* str, const char** next_str) {
+    const char* t = str;
 
-    while (*t && *t != '/')
+    while (*t && *t != '/') {
         t++;
+    }
 
     if (next_str)
         *next_str = *t ? t + 1 : NULL;
@@ -122,17 +136,16 @@ static inline int token_len (const char * str, const char ** next_str)
     return t - str;
 }
 
-static int proc_match_name (const char * trim_name,
-                            const struct proc_ent ** ent)
-{
+static int proc_match_name(const char* trim_name, const struct proc_ent** ent) {
     if (!trim_name || !trim_name[0]) {
         *ent = &proc_root_ent;
         return 0;
     }
 
-    const char * token = trim_name, * next_token;
-    const struct proc_ent * tmp = proc_root.ent;
-    const struct proc_ent * last = NULL;
+    const char* token           = trim_name;
+    const char* next_token;
+    const struct proc_ent* tmp  = proc_root.ent;
+    const struct proc_ent* last = NULL;
 
     if (*token == '/')
         token++;
@@ -140,23 +153,22 @@ static int proc_match_name (const char * trim_name,
     while (token) {
         int tlen = token_len(token, &next_token);
 
-        for ( ; tmp->name || tmp->nm_ops ; tmp++) {
+        for (; tmp->name || tmp->nm_ops; tmp++) {
             if (tmp->name && !memcmp(tmp->name, token, tlen))
                 goto found;
 
-            if (tmp->nm_ops && tmp->nm_ops->match_name &&
-                tmp->nm_ops->match_name(trim_name))
+            if (tmp->nm_ops && tmp->nm_ops->match_name && tmp->nm_ops->match_name(trim_name))
                 goto found;
         }
 
         return -ENOENT;
 
-found:
+    found:
         if (!tmp->dir && next_token)
             return -ENOENT;
 
-        last = tmp;
-        tmp = tmp->dir->ent;
+        last  = tmp;
+        tmp   = tmp->dir->ent;
         token = next_token;
     }
 
@@ -164,17 +176,16 @@ found:
     return 0;
 }
 
-static int proc_mode (struct shim_dentry * dent, mode_t * mode)
-{
+static int proc_mode(struct shim_dentry* dent, mode_t* mode) {
     if (qstrempty(&dent->rel_path)) {
         dent->ino = PROC_INO_BASE;
-        *mode = 0555|S_IFDIR;
+        *mode     = 0555 | S_IFDIR;
         return 0;
     }
 
     /* don't care about forced or not */
-    const char * rel_path = qstrgetstr(&dent->rel_path);
-    const struct proc_ent * ent;
+    const char* rel_path = qstrgetstr(&dent->rel_path);
+    const struct proc_ent* ent;
     int ret = proc_match_name(rel_path, &ent);
 
     if (ret < 0)
@@ -186,8 +197,7 @@ static int proc_mode (struct shim_dentry * dent, mode_t * mode)
     return ent->fs_ops->mode(rel_path, mode);
 }
 
-static int proc_lookup (struct shim_dentry * dent)
-{
+static int proc_lookup(struct shim_dentry* dent) {
     if (qstrempty(&dent->rel_path)) {
         dent->ino = PROC_INO_BASE;
         dent->state |= DENTRY_ISDIRECTORY;
@@ -195,8 +205,8 @@ static int proc_lookup (struct shim_dentry * dent)
     }
 
     /* don't care about forced or not */
-    const struct proc_ent * ent = NULL;
-    int ret = proc_match_name(qstrgetstr(&dent->rel_path), &ent);
+    const struct proc_ent* ent = NULL;
+    int ret                    = proc_match_name(qstrgetstr(&dent->rel_path), &ent);
 
     if (!ret && ent->dir)
         dent->state |= DENTRY_ISDIRECTORY;
@@ -204,11 +214,10 @@ static int proc_lookup (struct shim_dentry * dent)
     if (ent && ent->fs_ops && ent->fs_ops->follow_link)
         dent->state |= DENTRY_ISLINK;
 
-     return ret;
+    return ret;
 }
 
-static int proc_mount (const char * uri, void ** mount_data)
-{
+static int proc_mount(const char* uri, void** mount_data) {
     // Arguments for compatibility with other FSes
     __UNUSED(uri);
     __UNUSED(mount_data);
@@ -216,23 +225,20 @@ static int proc_mount (const char * uri, void ** mount_data)
     return 0;
 }
 
-static int proc_unmount (void * mount_data)
-{
+static int proc_unmount(void* mount_data) {
     // Arguments for compatibility with other FSes
     __UNUSED(mount_data);
     /* do nothing */
     return 0;
 }
 
-static int proc_open (struct shim_handle * hdl, struct shim_dentry * dent,
-                      int flags)
-{
-    const char * rel_path = qstrgetstr(&dent->rel_path);
+static int proc_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
+    const char* rel_path = qstrgetstr(&dent->rel_path);
 
-    if (flags & (O_CREAT|O_EXCL))
+    if (flags & (O_CREAT | O_EXCL))
         return -EACCES;
 
-    const struct proc_ent * ent;
+    const struct proc_ent* ent;
     int ret;
 
     if ((ret = proc_match_name(rel_path, &ent)) < 0)
@@ -246,11 +252,9 @@ static int proc_open (struct shim_handle * hdl, struct shim_dentry * dent,
     return ent->fs_ops->open(hdl, rel_path, flags);
 }
 
-static int proc_readdir (struct shim_dentry * dent,
-                         struct shim_dirent ** dirent)
-{
-    const char * rel_path = qstrgetstr(&dent->rel_path);
-    const struct proc_ent * ent;
+static int proc_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
+    const char* rel_path = qstrgetstr(&dent->rel_path);
+    const struct proc_ent* ent;
     int ret;
 
     if ((ret = proc_match_name(rel_path, &ent)) < 0)
@@ -259,46 +263,43 @@ static int proc_readdir (struct shim_dentry * dent,
     if (!ent->dir)
         return -ENOTDIR;
 
-    const struct proc_ent * tmp = ent->dir->ent;
-    const struct proc_ent * end = tmp + ent->dir->size;
+    const struct proc_ent* tmp = ent->dir->ent;
+    const struct proc_ent* end = tmp + ent->dir->size;
 
     HASHTYPE self_hash = hash_path(rel_path, dent->rel_path.len);
     HASHTYPE new_hash;
-    struct shim_dirent * buf, * ptr;
+    struct shim_dirent* buf;
+    struct shim_dirent* ptr;
     int buf_size = MAX_PATH;
 
 retry:
-    buf = malloc(buf_size);
-    *dirent = ptr = buf;
-    struct shim_dirent ** last = dirent;
+    buf     = malloc(buf_size);
+    *dirent = ptr             = buf;
+    struct shim_dirent** last = dirent;
 
-    for ( ; tmp < end ; tmp++) {
+    for (; tmp < end; tmp++) {
         if (tmp->name) {
             int name_len = strlen(tmp->name);
 
-            if ((void *) (ptr + 1) + name_len + 1 > (void *) buf + buf_size)
+            if ((void*)(ptr + 1) + name_len + 1 > (void*)buf + buf_size)
                 goto enlarge;
 
-            new_hash = rehash_name(self_hash,
-                                   tmp->name, name_len);
+            new_hash = rehash_name(self_hash, tmp->name, name_len);
 
-            ptr->next = (void *) (ptr + 1) + name_len + 1;
-            ptr->ino = new_hash;
-            ptr->type = tmp->dir ? LINUX_DT_DIR : (
-                        tmp->fs_ops && tmp->fs_ops->follow_link ?
-                        LINUX_DT_LNK : LINUX_DT_REG);
+            ptr->next = (void*)(ptr + 1) + name_len + 1;
+            ptr->ino  = new_hash;
+            ptr->type =
+                tmp->dir ? LINUX_DT_DIR
+                         : (tmp->fs_ops && tmp->fs_ops->follow_link ? LINUX_DT_LNK : LINUX_DT_REG);
             memcpy(ptr->name, tmp->name, name_len + 1);
             last = &ptr->next;
-            ptr = *last;
+            ptr  = *last;
             continue;
         }
 
         if (tmp->nm_ops && tmp->nm_ops->list_name) {
-            struct shim_dirent * d = ptr;
-            int ret = tmp->nm_ops->list_name(rel_path,
-                                             &ptr,
-                                             (void *) buf + buf_size -
-                                             (void *) ptr);
+            struct shim_dirent* d = ptr;
+            int ret = tmp->nm_ops->list_name(rel_path, &ptr, (void*)buf + buf_size - (void*)ptr);
 
             if (ret == -ENOBUFS)
                 goto enlarge;
@@ -306,8 +307,9 @@ retry:
             if (ret < 0)
                 ptr = d;
             else
-                for ( ; d && d != ptr ; d = d->next)
+                for (; d && d != ptr; d = d->next) {
                     last = &d->next;
+                }
             continue;
         }
     }
@@ -323,10 +325,9 @@ enlarge:
     goto retry;
 }
 
-static int proc_stat (struct shim_dentry * dent, struct stat * buf)
-{
-    const char * rel_path = qstrgetstr(&dent->rel_path);
-    const struct proc_ent * ent;
+static int proc_stat(struct shim_dentry* dent, struct stat* buf) {
+    const char* rel_path = qstrgetstr(&dent->rel_path);
+    const struct proc_ent* ent;
     int ret;
 
     if ((ret = proc_match_name(rel_path, &ent)) < 0)
@@ -338,11 +339,9 @@ static int proc_stat (struct shim_dentry * dent, struct stat * buf)
     return ent->fs_ops->stat(rel_path, buf);
 }
 
-static int proc_follow_link (struct shim_dentry * dent,
-                             struct shim_qstr * link)
-{
-    const char * rel_path = qstrgetstr(&dent->rel_path);
-    const struct proc_ent * ent;
+static int proc_follow_link(struct shim_dentry* dent, struct shim_qstr* link) {
+    const char* rel_path = qstrgetstr(&dent->rel_path);
+    const struct proc_ent* ent;
     int ret;
 
     if ((ret = proc_match_name(rel_path, &ent)) < 0)
@@ -354,13 +353,12 @@ static int proc_follow_link (struct shim_dentry * dent,
     return ent->fs_ops->follow_link(rel_path, link);
 }
 
-static int proc_hstat (struct shim_handle * hdl, struct stat * buf)
-{
-    struct shim_dentry * dent = hdl->dentry;
+static int proc_hstat(struct shim_handle* hdl, struct stat* buf) {
+    struct shim_dentry* dent = hdl->dentry;
     assert(dent);
 
-    const char * rel_path = qstrgetstr(&dent->rel_path);
-    const struct proc_ent * ent;
+    const char* rel_path = qstrgetstr(&dent->rel_path);
+    const struct proc_ent* ent;
     int ret;
 
     if ((ret = proc_match_name(rel_path, &ent)) < 0)
@@ -373,21 +371,21 @@ static int proc_hstat (struct shim_handle * hdl, struct stat * buf)
 }
 
 struct shim_fs_ops proc_fs_ops = {
-        .mount          = &proc_mount,
-        .unmount        = &proc_unmount,
-        .close          = &str_close,
-        .read           = &str_read,
-        .write          = &str_write,
-        .seek           = &str_seek,
-        .flush          = &str_flush,
-        .hstat          = &proc_hstat,
-    };
+    .mount   = &proc_mount,
+    .unmount = &proc_unmount,
+    .close   = &str_close,
+    .read    = &str_read,
+    .write   = &str_write,
+    .seek    = &str_seek,
+    .flush   = &str_flush,
+    .hstat   = &proc_hstat,
+};
 
 struct shim_d_ops proc_d_ops = {
-        .open           = &proc_open,
-        .stat           = &proc_stat,
-        .mode           = &proc_mode,
-        .lookup         = &proc_lookup,
-        .follow_link    = &proc_follow_link,
-        .readdir        = &proc_readdir,
-    };
+    .open        = &proc_open,
+    .stat        = &proc_stat,
+    .mode        = &proc_mode,
+    .lookup      = &proc_lookup,
+    .follow_link = &proc_follow_link,
+    .readdir     = &proc_readdir,
+};

+ 103 - 77
LibOS/shim/src/fs/proc/info.c

@@ -1,56 +1,59 @@
-#include <shim_internal.h>
-#include <shim_fs.h>
-
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_internal.h>
 
-#include <errno.h>
-
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
 
-#include <asm/fcntl.h>
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
-
-static int proc_info_mode (const char * name, mode_t * mode)
-{
+static int proc_info_mode(const char* name, mode_t* mode) {
     // The path is implicitly set by calling this function
     __UNUSED(name);
     *mode = 0444;
     return 0;
 }
 
-static int proc_info_stat (const char * name, struct stat * buf)
-{
+static int proc_info_stat(const char* name, struct stat* buf) {
     // The path is implicitly set by calling this function
     __UNUSED(name);
     memset(buf, 0, sizeof(struct stat));
     buf->st_dev = buf->st_ino = 1;
-    buf->st_mode = 0444|S_IFREG;
-    buf->st_uid = 0;
-    buf->st_gid = 0;
-    buf->st_size = 0;
+    buf->st_mode              = 0444 | S_IFREG;
+    buf->st_uid               = 0;
+    buf->st_gid               = 0;
+    buf->st_size              = 0;
     return 0;
 }
 
-static int proc_meminfo_open (struct shim_handle * hdl, const char * name,
-                              int flags)
-{
+static int proc_meminfo_open(struct shim_handle* hdl, const char* name, int flags) {
     // This function only serves one file
     __UNUSED(name);
-    if (flags & (O_WRONLY|O_RDWR))
+    if (flags & (O_WRONLY | O_RDWR))
         return -EACCES;
 
     int len, max = 128;
-    char * str = NULL;
-
-    struct { const char * fmt; unsigned long val; }
-        meminfo[] = {
-            { "MemTotal:      %8lu kB\n", pal_control.mem_info.mem_total / 1024, },
-            { "MemFree:       %8lu kB\n", DkMemoryAvailableQuota() / 1024, },
-        };
+    char* str = NULL;
+
+    struct {
+        const char* fmt;
+        unsigned long val;
+    } meminfo[] = {
+        {
+            "MemTotal:      %8lu kB\n",
+            pal_control.mem_info.mem_total / 1024,
+        },
+        {
+            "MemFree:       %8lu kB\n",
+            DkMemoryAvailableQuota() / 1024,
+        },
+    };
 
 retry:
     max *= 2;
@@ -60,9 +63,8 @@ retry:
     if (!str)
         return -ENOMEM;
 
-    for (size_t i = 0 ; i < sizeof(meminfo) / sizeof(meminfo[0]) ; i++) {
-        int ret = snprintf(str + len, max - len, meminfo[i].fmt,
-                           meminfo[i].val);
+    for (size_t i = 0; i < sizeof(meminfo) / sizeof(meminfo[0]); i++) {
+        int ret = snprintf(str + len, max - len, meminfo[i].fmt, meminfo[i].val);
 
         if (len + ret == max)
             goto retry;
@@ -70,47 +72,72 @@ retry:
         len += ret;
     }
 
-    struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
+    struct shim_str_data* data = malloc(sizeof(struct shim_str_data));
     if (!data) {
         free(str);
         return -ENOMEM;
     }
 
     memset(data, 0, sizeof(struct shim_str_data));
-    data->str = str;
-    data->len = len;
-    hdl->type = TYPE_STR;
-    hdl->flags = flags & ~O_RDONLY;
-    hdl->acc_mode = MAY_READ;
+    data->str          = str;
+    data->len          = len;
+    hdl->type          = TYPE_STR;
+    hdl->flags         = flags & ~O_RDONLY;
+    hdl->acc_mode      = MAY_READ;
     hdl->info.str.data = data;
     return 0;
 }
 
-static int proc_cpuinfo_open (struct shim_handle * hdl, const char * name,
-                              int flags)
-{
+static int proc_cpuinfo_open(struct shim_handle* hdl, const char* name, int flags) {
     // This function only serves one file
     __UNUSED(name);
 
-    if (flags & (O_WRONLY|O_RDWR))
+    if (flags & (O_WRONLY | O_RDWR))
         return -EACCES;
 
     int len, max = 128;
-    char * str = NULL;
-
-    struct { const char * fmt; unsigned long val; }
-        /* below strings must match exactly the strings retrieved from
-         * /proc/cpuinfo (see Linux's arch/x86/kernel/cpu/proc.c) */
-        cpuinfo[] = {
-            { "processor\t: %lu\n",  0, },
-            { "vendor_id\t: %s\n",   (unsigned long) pal_control.cpu_info.cpu_vendor, },
-            { "cpu family\t: %lu\n", pal_control.cpu_info.cpu_family, },
-            { "model\t\t: %lu\n",    pal_control.cpu_info.cpu_model, },
-            { "model name\t: %s\n",  (unsigned long) pal_control.cpu_info.cpu_brand, },
-            { "stepping\t: %lu\n",   pal_control.cpu_info.cpu_stepping, },
-            { "core id\t\t: %lu\n",  0, },
-            { "cpu cores\t: %lu\n",  pal_control.cpu_info.cpu_num, },
-        };
+    char* str = NULL;
+
+    struct {
+        const char* fmt;
+        unsigned long val;
+    }
+    /* below strings must match exactly the strings retrieved from
+     * /proc/cpuinfo (see Linux's arch/x86/kernel/cpu/proc.c) */
+    cpuinfo[] = {
+        {
+            "processor\t: %lu\n",
+            0,
+        },
+        {
+            "vendor_id\t: %s\n",
+            (unsigned long)pal_control.cpu_info.cpu_vendor,
+        },
+        {
+            "cpu family\t: %lu\n",
+            pal_control.cpu_info.cpu_family,
+        },
+        {
+            "model\t\t: %lu\n",
+            pal_control.cpu_info.cpu_model,
+        },
+        {
+            "model name\t: %s\n",
+            (unsigned long)pal_control.cpu_info.cpu_brand,
+        },
+        {
+            "stepping\t: %lu\n",
+            pal_control.cpu_info.cpu_stepping,
+        },
+        {
+            "core id\t\t: %lu\n",
+            0,
+        },
+        {
+            "cpu cores\t: %lu\n",
+            pal_control.cpu_info.cpu_num,
+        },
+    };
 
 retry:
     max *= 2;
@@ -120,12 +147,11 @@ retry:
     if (!str)
         return -ENOMEM;
 
-    for (size_t n = 0 ; n < pal_control.cpu_info.cpu_num ; n++) {
+    for (size_t n = 0; n < pal_control.cpu_info.cpu_num; n++) {
         cpuinfo[0].val = n;
         cpuinfo[6].val = n;
-        for (size_t i = 0 ; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]) ; i++) {
-            int ret = snprintf(str + len, max - len, cpuinfo[i].fmt,
-                               cpuinfo[i].val);
+        for (size_t i = 0; i < sizeof(cpuinfo) / sizeof(cpuinfo[0]); i++) {
+            int ret = snprintf(str + len, max - len, cpuinfo[i].fmt, cpuinfo[i].val);
 
             if (len + ret == max)
                 goto retry;
@@ -137,32 +163,32 @@ retry:
             goto retry;
 
         str[len++] = '\n';
-        str[len] = 0;
+        str[len]   = 0;
     }
 
-    struct shim_str_data * data = calloc(1, sizeof(struct shim_str_data));
+    struct shim_str_data* data = calloc(1, sizeof(struct shim_str_data));
     if (!data) {
         free(str);
         return -ENOMEM;
     }
 
-    data->str = str;
-    data->len = len;
-    hdl->type = TYPE_STR;
-    hdl->flags = flags & ~O_RDONLY;
-    hdl->acc_mode = MAY_READ;
+    data->str          = str;
+    data->len          = len;
+    hdl->type          = TYPE_STR;
+    hdl->flags         = flags & ~O_RDONLY;
+    hdl->acc_mode      = MAY_READ;
     hdl->info.str.data = data;
     return 0;
 }
 
 struct proc_fs_ops fs_meminfo = {
-        .mode     = &proc_info_mode,
-        .stat     = &proc_info_stat,
-        .open     = &proc_meminfo_open,
-    };
+    .mode = &proc_info_mode,
+    .stat = &proc_info_stat,
+    .open = &proc_meminfo_open,
+};
 
 struct proc_fs_ops fs_cpuinfo = {
-        .mode     = &proc_info_mode,
-        .stat     = &proc_info_stat,
-        .open     = &proc_cpuinfo_open,
-    };
+    .mode = &proc_info_mode,
+    .stat = &proc_info_stat,
+    .open = &proc_cpuinfo_open,
+};

+ 98 - 95
LibOS/shim/src/fs/proc/ipc-thread.c

@@ -1,35 +1,32 @@
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
+#include <shim_ipc.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
-#include <shim_ipc.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
 
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
 
-#include <asm/fcntl.h>
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
-
-static int parse_ipc_thread_name (const char * name, IDTYPE * pidptr,
-                                  const char ** next, size_t * next_len,
-                                  const char ** nextnext)
-{
-    const char * p = name;
-    IDTYPE pid = 0;
+static int parse_ipc_thread_name(const char* name, IDTYPE* pidptr, const char** next,
+                                 size_t* next_len, const char** nextnext) {
+    const char* p = name;
+    IDTYPE pid    = 0;
 
     if (*p == '/')
         p++;
 
-    for ( ; *p && *p != '/' ; p++) {
+    for (; *p && *p != '/'; p++) {
         if (*p < '0' || *p > '9')
             return -ENOENT;
 
@@ -41,7 +38,8 @@ static int parse_ipc_thread_name (const char * name, IDTYPE * pidptr,
             *next = p;
 
             if (next_len || nextnext)
-                for ( ; *p && *p != '/' ; p++);
+                for (; *p && *p != '/'; p++)
+                    ;
 
             if (next_len)
                 *next_len = p - *next;
@@ -53,14 +51,15 @@ static int parse_ipc_thread_name (const char * name, IDTYPE * pidptr,
         }
     }
 
-    if (pidptr) *pidptr = pid;
+    if (pidptr)
+        *pidptr = pid;
     return 0;
 }
 
-static int find_ipc_thread_link (const char * name, struct shim_qstr * link,
-                                 struct shim_dentry ** dentptr)
-{
-    const char * next, * nextnext;
+static int find_ipc_thread_link(const char* name, struct shim_qstr* link,
+                                struct shim_dentry** dentptr) {
+    const char *next;
+    const char *nextnext;
     size_t next_len;
     IDTYPE pid;
 
@@ -68,9 +67,9 @@ static int find_ipc_thread_link (const char * name, struct shim_qstr * link,
     if (ret < 0)
         return ret;
 
-    struct shim_dentry * dent = NULL;
+    struct shim_dentry* dent = NULL;
     enum pid_meta_code ipc_code;
-    void * ipc_data = NULL;
+    void* ipc_data = NULL;
 
     if (!memcmp(next, "root", next_len)) {
         ipc_code = PID_META_ROOT;
@@ -95,12 +94,12 @@ do_ipc:
         goto out;
 
     if (link)
-        qstrsetstr(link, (char *) ipc_data, strlen((char *) ipc_data));
+        qstrsetstr(link, (char*)ipc_data, strlen((char*)ipc_data));
 
     if (dentptr) {
         /* XXX: Not sure how to handle this case yet */
-        assert (0);
-        ret = path_lookupat(NULL, (char *) ipc_data, 0, &dent, NULL);
+        assert(0);
+        ret = path_lookupat(NULL, (char*)ipc_data, 0, &dent, NULL);
         if (ret < 0)
             goto out;
 
@@ -114,10 +113,8 @@ out:
     return ret;
 }
 
-static int proc_ipc_thread_link_open (struct shim_handle * hdl,
-                                      const char * name, int flags)
-{
-    struct shim_dentry * dent;
+static int proc_ipc_thread_link_open(struct shim_handle* hdl, const char* name, int flags) {
+    struct shim_dentry* dent;
 
     int ret = find_ipc_thread_link(name, NULL, &dent);
     if (ret < 0)
@@ -134,9 +131,8 @@ out:
     return 0;
 }
 
-static int proc_ipc_thread_link_mode (const char * name, mode_t * mode)
-{
-    struct shim_dentry * dent;
+static int proc_ipc_thread_link_mode(const char* name, mode_t* mode) {
+    struct shim_dentry* dent;
 
     int ret = find_ipc_thread_link(name, NULL, &dent);
     if (ret < 0)
@@ -153,9 +149,8 @@ out:
     return ret;
 }
 
-static int proc_ipc_thread_link_stat (const char * name, struct stat * buf)
-{
-    struct shim_dentry * dent;
+static int proc_ipc_thread_link_stat(const char* name, struct stat* buf) {
+    struct shim_dentry* dent;
 
     int ret = find_ipc_thread_link(name, NULL, &dent);
     if (ret < 0)
@@ -172,30 +167,27 @@ out:
     return ret;
 }
 
-static int proc_ipc_thread_link_follow_link (const char * name,
-                                             struct shim_qstr * link)
-{
+static int proc_ipc_thread_link_follow_link(const char* name, struct shim_qstr* link) {
     return find_ipc_thread_link(name, link, NULL);
 }
 
 static const struct proc_fs_ops fs_ipc_thread_link = {
-            .open           = &proc_ipc_thread_link_open,
-            .mode           = &proc_ipc_thread_link_mode,
-            .stat           = &proc_ipc_thread_link_stat,
-            .follow_link    = &proc_ipc_thread_link_follow_link,
-        };
+    .open        = &proc_ipc_thread_link_open,
+    .mode        = &proc_ipc_thread_link_mode,
+    .stat        = &proc_ipc_thread_link_stat,
+    .follow_link = &proc_ipc_thread_link_follow_link,
+};
 
 static struct pid_status_cache {
     uint32_t ref_count;
     bool dirty;
     size_t nstatus;
-    struct pid_status * status;
+    struct pid_status* status;
 } * pid_status_cache;
 
 static struct shim_lock status_lock;
 
-static int proc_match_ipc_thread (const char * name)
-{
+static int proc_match_ipc_thread(const char* name) {
     IDTYPE pid;
     if (parse_ipc_thread_name(name, &pid, NULL, NULL, NULL) < 0)
         return 0;
@@ -204,7 +196,7 @@ static int proc_match_ipc_thread (const char * name)
     lock(&status_lock);
 
     if (pid_status_cache)
-        for (size_t i = 0 ; i < pid_status_cache->nstatus ; i++)
+        for (size_t i = 0; i < pid_status_cache->nstatus; i++)
             if (pid_status_cache->status[i].pid == pid) {
                 unlock(&status_lock);
                 return 1;
@@ -214,9 +206,8 @@ static int proc_match_ipc_thread (const char * name)
     return 0;
 }
 
-static int proc_ipc_thread_dir_mode (const char * name, mode_t * mode)
-{
-    const char * next;
+static int proc_ipc_thread_dir_mode(const char* name, mode_t* mode) {
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_ipc_thread_name(name, &pid, &next, &next_len, NULL);
@@ -227,7 +218,7 @@ static int proc_ipc_thread_dir_mode (const char * name, mode_t * mode)
     lock(&status_lock);
 
     if (pid_status_cache)
-        for (size_t i = 0 ; i < pid_status_cache->nstatus ; i++)
+        for (size_t i = 0; i < pid_status_cache->nstatus; i++)
             if (pid_status_cache->status[i].pid == pid) {
                 unlock(&status_lock);
                 *mode = 0500;
@@ -238,9 +229,8 @@ static int proc_ipc_thread_dir_mode (const char * name, mode_t * mode)
     return -ENOENT;
 }
 
-static int proc_ipc_thread_dir_stat (const char * name, struct stat * buf)
-{
-    const char * next;
+static int proc_ipc_thread_dir_stat(const char* name, struct stat* buf) {
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_ipc_thread_name(name, &pid, &next, &next_len, NULL);
@@ -251,14 +241,14 @@ static int proc_ipc_thread_dir_stat (const char * name, struct stat * buf)
     lock(&status_lock);
 
     if (pid_status_cache)
-        for (size_t i = 0 ; i < pid_status_cache->nstatus ; i++)
+        for (size_t i = 0; i < pid_status_cache->nstatus; i++)
             if (pid_status_cache->status[i].pid == pid) {
                 memset(buf, 0, sizeof(struct stat));
                 buf->st_dev = buf->st_ino = 1;
-                buf->st_mode = 0500|S_IFDIR;
-                buf->st_uid = 0; /* XXX */
-                buf->st_gid = 0; /* XXX */
-                buf->st_size = 4096;
+                buf->st_mode              = 0500 | S_IFDIR;
+                buf->st_uid               = 0; /* XXX */
+                buf->st_gid               = 0; /* XXX */
+                buf->st_size              = 4096;
                 unlock(&status_lock);
                 return 0;
             }
@@ -267,15 +257,13 @@ static int proc_ipc_thread_dir_stat (const char * name, struct stat * buf)
     return -ENOENT;
 }
 
-int get_all_pid_status (struct pid_status ** status);
+int get_all_pid_status(struct pid_status** status);
 
-static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
-                                 int len)
-{
+static int proc_list_ipc_thread(const char* name, struct shim_dirent** buf, int len) {
     // Only one valid name
     __UNUSED(name);
-    struct pid_status_cache * status = NULL;
-    int ret = 0;
+    struct pid_status_cache* status = NULL;
+    int ret                         = 0;
 
     create_lock_runtime(&status_lock);
 
@@ -297,9 +285,9 @@ static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
             return ret;
         }
 
-        status->nstatus = ret;
+        status->nstatus   = ret;
         status->ref_count = 1;
-        status->dirty = false;
+        status->dirty     = false;
 
         lock(&status_lock);
         if (pid_status_cache) {
@@ -323,28 +311,30 @@ static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
     if (!status->nstatus)
         goto success;
 
-    struct shim_dirent * ptr = (*buf);
-    void * buf_end = (void *) ptr + len;
+    struct shim_dirent* ptr = (*buf);
+    void* buf_end           = (void*)ptr + len;
 
-    for (size_t i = 0 ; i < status->nstatus ; i++) {
+    for (size_t i = 0; i < status->nstatus; i++) {
         if (status->status[i].pid != status->status[i].tgid)
             continue;
 
         IDTYPE pid = status->status[i].pid;
         int p = pid, l = 0;
-        for ( ; p ; p /= 10, l++);
+        for (; p; p /= 10, l++)
+            ;
 
-        if ((void *) (ptr + 1) + l + 1 > buf_end) {
+        if ((void*)(ptr + 1) + l + 1 > buf_end) {
             ret = -ENOBUFS;
             goto err;
         }
 
-        ptr->next = (void *) (ptr + 1) + l + 1;
-        ptr->ino = 1;
-        ptr->type = LINUX_DT_DIR;
+        ptr->next      = (void*)(ptr + 1) + l + 1;
+        ptr->ino       = 1;
+        ptr->type      = LINUX_DT_DIR;
         ptr->name[l--] = 0;
-        for (p = pid ; p ; p /= 10)
+        for (p = pid; p; p /= 10) {
             ptr->name[l--] = p % 10 + '0';
+        }
 
         ptr = ptr->next;
     }
@@ -368,17 +358,30 @@ err:
 }
 
 const struct proc_nm_ops nm_ipc_thread = {
-            .match_name = &proc_match_ipc_thread,
-            .list_name  = &proc_list_ipc_thread,
-        };
+    .match_name = &proc_match_ipc_thread,
+    .list_name  = &proc_list_ipc_thread,
+};
 
 const struct proc_fs_ops fs_ipc_thread = {
-            .mode   = &proc_ipc_thread_dir_mode,
-            .stat   = &proc_ipc_thread_dir_stat,
-        };
-
-const struct proc_dir dir_ipc_thread = { .size = 0, .ent = {
-        { .name = "cwd",  .fs_ops = &fs_ipc_thread_link, },
-        { .name = "exe",  .fs_ops = &fs_ipc_thread_link, },
-        { .name = "root", .fs_ops = &fs_ipc_thread_link, },
-    }, };
+    .mode = &proc_ipc_thread_dir_mode,
+    .stat = &proc_ipc_thread_dir_stat,
+};
+
+const struct proc_dir dir_ipc_thread = {
+    .size = 0,
+    .ent =
+        {
+            {
+                .name   = "cwd",
+                .fs_ops = &fs_ipc_thread_link,
+            },
+            {
+                .name   = "exe",
+                .fs_ops = &fs_ipc_thread_link,
+            },
+            {
+                .name   = "root",
+                .fs_ops = &fs_ipc_thread_link,
+            },
+        },
+};

+ 214 - 217
LibOS/shim/src/fs/proc/thread.c

@@ -1,29 +1,26 @@
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
 
-#include <asm/fcntl.h>
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
-
-static int parse_thread_name (const char * name, IDTYPE * pidptr,
-                              const char ** next, size_t * next_len,
-                              const char ** nextnext)
-{
-    const char * p = name;
-    IDTYPE pid = 0;
+static int parse_thread_name(const char* name, IDTYPE* pidptr, const char** next, size_t* next_len,
+                             const char** nextnext) {
+    const char* p = name;
+    IDTYPE pid    = 0;
 
     if (*p == '/')
         p++;
@@ -34,7 +31,7 @@ static int parse_thread_name (const char * name, IDTYPE * pidptr,
             return -ENOENT;
         pid = get_cur_tid();
     } else {
-        for ( ; *p && *p != '/' ; p++) {
+        for (; *p && *p != '/'; p++) {
             if (*p < '0' || *p > '9')
                 return -ENOENT;
 
@@ -47,7 +44,8 @@ static int parse_thread_name (const char * name, IDTYPE * pidptr,
             *next = p;
 
             if (next_len || nextnext)
-                for ( ; *p && *p != '/' ; p++);
+                for (; *p && *p != '/'; p++)
+                    ;
 
             if (next_len)
                 *next_len = p - *next;
@@ -59,23 +57,23 @@ static int parse_thread_name (const char * name, IDTYPE * pidptr,
         }
     }
 
-    if (pidptr) *pidptr = pid;
+    if (pidptr)
+        *pidptr = pid;
     return 0;
 }
 
-static int find_thread_link (const char * name, struct shim_qstr * link,
-                             struct shim_dentry ** dentptr,
-                             struct shim_thread ** threadptr)
-{
-    const char * next, * nextnext;
+static int find_thread_link(const char* name, struct shim_qstr* link, struct shim_dentry** dentptr,
+                            struct shim_thread** threadptr) {
+    const char* next;
+    const char* nextnext;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, &nextnext);
     if (ret < 0)
         return ret;
 
-    struct shim_thread * thread = lookup_thread(pid);
-    struct shim_dentry * dent = NULL;
+    struct shim_thread* thread = lookup_thread(pid);
+    struct shim_dentry* dent   = NULL;
 
     if (!thread)
         return -ENOENT;
@@ -98,7 +96,7 @@ static int find_thread_link (const char * name, struct shim_qstr * link,
     }
 
     if (next_len == static_strlen("exe") && !memcmp(next, "exe", next_len)) {
-        struct shim_handle * exec = thread->exec;
+        struct shim_handle* exec = thread->exec;
         if (!exec->dentry) {
             unlock(&thread->lock);
             ret = -EINVAL;
@@ -111,7 +109,7 @@ static int find_thread_link (const char * name, struct shim_qstr * link,
     unlock(&thread->lock);
 
     if (nextnext) {
-        struct shim_dentry * next_dent = NULL;
+        struct shim_dentry* next_dent = NULL;
 
         ret = path_lookupat(dent, nextnext, 0, &next_dent, dent->fs);
         if (ret < 0)
@@ -123,7 +121,7 @@ static int find_thread_link (const char * name, struct shim_qstr * link,
 
     if (link) {
         size_t size;
-        char * path = dentry_get_path(dent, true, &size);
+        char* path = dentry_get_path(dent, true, &size);
         qstrsetstr(link, path, size);
     }
 
@@ -146,10 +144,8 @@ out:
     return ret;
 }
 
-static int proc_thread_link_open (struct shim_handle * hdl,
-                                  const char * name, int flags)
-{
-    struct shim_dentry * dent;
+static int proc_thread_link_open(struct shim_handle* hdl, const char* name, int flags) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_link(name, NULL, &dent, NULL);
     if (ret < 0)
@@ -166,9 +162,8 @@ out:
     return 0;
 }
 
-static int proc_thread_link_mode (const char * name, mode_t * mode)
-{
-    struct shim_dentry * dent;
+static int proc_thread_link_mode(const char* name, mode_t* mode) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_link(name, NULL, &dent, NULL);
     if (ret < 0)
@@ -185,9 +180,8 @@ out:
     return ret;
 }
 
-static int proc_thread_link_stat (const char * name, struct stat * buf)
-{
-    struct shim_dentry * dent;
+static int proc_thread_link_stat(const char* name, struct stat* buf) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_link(name, NULL, &dent, NULL);
     if (ret < 0)
@@ -204,24 +198,21 @@ out:
     return ret;
 }
 
-static int proc_thread_link_follow_link (const char * name,
-                                         struct shim_qstr * link)
-{
+static int proc_thread_link_follow_link(const char* name, struct shim_qstr* link) {
     return find_thread_link(name, link, NULL, NULL);
 }
 
 static const struct proc_fs_ops fs_thread_link = {
-            .open           = &proc_thread_link_open,
-            .mode           = &proc_thread_link_mode,
-            .stat           = &proc_thread_link_stat,
-            .follow_link    = &proc_thread_link_follow_link,
-        };
+    .open        = &proc_thread_link_open,
+    .mode        = &proc_thread_link_mode,
+    .stat        = &proc_thread_link_stat,
+    .follow_link = &proc_thread_link_follow_link,
+};
 
 /* If *phdl is returned on success, the ref count is incremented */
-static int parse_thread_fd (const char * name, const char ** rest,
-                            struct shim_handle ** phdl)
-{
-    const char * next, * nextnext;
+static int parse_thread_fd(const char* name, const char** rest, struct shim_handle** phdl) {
+    const char* next;
+    const char* nextnext;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, &nextnext);
@@ -231,28 +222,27 @@ static int parse_thread_fd (const char * name, const char ** rest,
     if (!next || !nextnext || memcmp(next, "fd", next_len))
         return -EINVAL;
 
-    const char * p = nextnext;
-    FDTYPE fd = 0;
+    const char* p = nextnext;
+    FDTYPE fd     = 0;
 
-    for ( ; *p && *p != '/' ; p++) {
+    for (; *p && *p != '/'; p++) {
         if (*p < '0' || *p > '9')
             return -ENOENT;
         fd = fd * 10 + *p - '0';
-        if ((uint64_t) fd >= get_rlimit_cur(RLIMIT_NOFILE))
+        if ((uint64_t)fd >= get_rlimit_cur(RLIMIT_NOFILE))
             return -ENOENT;
     }
 
-    struct shim_thread * thread = lookup_thread(pid);
+    struct shim_thread* thread = lookup_thread(pid);
 
     if (!thread)
         return -ENOENT;
 
-    struct shim_handle_map * handle_map = get_cur_handle_map(thread);
+    struct shim_handle_map* handle_map = get_cur_handle_map(thread);
 
     lock(&handle_map->lock);
 
-    if (fd >= handle_map->fd_top ||
-        handle_map->map[fd] == NULL ||
+    if (fd >= handle_map->fd_top || handle_map->map[fd] == NULL ||
         handle_map->map[fd]->handle == NULL) {
         unlock(&handle_map->lock);
         return -ENOENT;
@@ -271,15 +261,12 @@ static int parse_thread_fd (const char * name, const char ** rest,
     return 0;
 }
 
-static int proc_match_thread_each_fd (const char * name)
-{
+static int proc_match_thread_each_fd(const char* name) {
     return parse_thread_fd(name, NULL, NULL) == 0 ? 1 : 0;
 }
 
-static int proc_list_thread_each_fd (const char * name,
-                                     struct shim_dirent ** buf, int count)
-{
-    const char * next;
+static int proc_list_thread_each_fd(const char* name, struct shim_dirent** buf, int count) {
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
@@ -289,22 +276,23 @@ static int proc_list_thread_each_fd (const char * name,
     if (!next || memcmp(next, "fd", next_len))
         return -EINVAL;
 
-    struct shim_thread * thread = lookup_thread(pid);
+    struct shim_thread* thread = lookup_thread(pid);
     if (!thread)
         return -ENOENT;
 
-    struct shim_handle_map * handle_map = get_cur_handle_map(thread);
+    struct shim_handle_map* handle_map = get_cur_handle_map(thread);
     int err = 0, bytes = 0;
-    struct shim_dirent * dirent = *buf, ** last = NULL;
+    struct shim_dirent* dirent = *buf;
+    struct shim_dirent** last  = NULL;
 
     lock(&handle_map->lock);
 
-    for (int i = 0 ; i < handle_map->fd_size ; i++)
-        if (handle_map->map[i] &&
-            handle_map->map[i]->handle) {
+    for (int i = 0; i < handle_map->fd_size; i++)
+        if (handle_map->map[i] && handle_map->map[i]->handle) {
             int d = i, l = 0;
-            for ( ; d ; d /= 10, l++);
-            l = l ? : 1;
+            for (; d; d /= 10, l++)
+                ;
+            l = l ?: 1;
 
             bytes += sizeof(struct shim_dirent) + l + 1;
             if (bytes > count) {
@@ -312,14 +300,15 @@ static int proc_list_thread_each_fd (const char * name,
                 break;
             }
 
-            dirent->next = (void *) (dirent + 1) + l + 1;
-            dirent->ino = 1;
-            dirent->type = LINUX_DT_LNK;
-            dirent->name[0] = '0';
+            dirent->next      = (void*)(dirent + 1) + l + 1;
+            dirent->ino       = 1;
+            dirent->type      = LINUX_DT_LNK;
+            dirent->name[0]   = '0';
             dirent->name[l--] = 0;
-            for (d = i ; d ; d /= 10)
+            for (d = i; d; d /= 10) {
                 dirent->name[l--] = '0' + d % 10;
-            last = &dirent->next;
+            }
+            last   = &dirent->next;
             dirent = dirent->next;
         }
 
@@ -334,16 +323,15 @@ static int proc_list_thread_each_fd (const char * name,
 }
 
 static const struct proc_nm_ops nm_thread_each_fd = {
-            .match_name = &proc_match_thread_each_fd,
-            .list_name  = &proc_list_thread_each_fd,
-        };
+    .match_name = &proc_match_thread_each_fd,
+    .list_name  = &proc_list_thread_each_fd,
+};
 
-static int find_thread_each_fd (const char * name, struct shim_qstr * link,
-                                struct shim_dentry ** dentptr)
-{
-    const char * rest;
-    struct shim_handle * handle;
-    struct shim_dentry * dent = NULL;
+static int find_thread_each_fd(const char* name, struct shim_qstr* link,
+                               struct shim_dentry** dentptr) {
+    const char* rest;
+    struct shim_handle* handle;
+    struct shim_dentry* dent = NULL;
     int ret;
 
     if ((ret = parse_thread_fd(name, &rest, &handle)) < 0)
@@ -364,7 +352,7 @@ static int find_thread_each_fd (const char * name, struct shim_qstr * link,
     }
 
     if (rest) {
-        struct shim_dentry * next_dent = NULL;
+        struct shim_dentry* next_dent = NULL;
 
         ret = path_lookupat(dent, rest, 0, &next_dent, dent->fs);
         if (ret < 0)
@@ -376,7 +364,7 @@ static int find_thread_each_fd (const char * name, struct shim_qstr * link,
 
     if (link) {
         size_t size;
-        char * path = dentry_get_path(dent, true, &size);
+        char* path = dentry_get_path(dent, true, &size);
         qstrsetstr(link, path, size);
     }
 
@@ -393,10 +381,8 @@ out:
     return ret;
 }
 
-static int proc_thread_each_fd_open (struct shim_handle * hdl,
-                                     const char * name, int flags)
-{
-    struct shim_dentry * dent;
+static int proc_thread_each_fd_open(struct shim_handle* hdl, const char* name, int flags) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_each_fd(name, NULL, &dent);
     if (ret < 0)
@@ -413,9 +399,8 @@ out:
     return 0;
 }
 
-static int proc_thread_each_fd_mode (const char * name, mode_t * mode)
-{
-    struct shim_dentry * dent;
+static int proc_thread_each_fd_mode(const char* name, mode_t* mode) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_each_fd(name, NULL, &dent);
     if (ret < 0)
@@ -432,9 +417,8 @@ out:
     return 0;
 }
 
-static int proc_thread_each_fd_stat (const char * name, struct stat * buf)
-{
-    struct shim_dentry * dent;
+static int proc_thread_each_fd_stat(const char* name, struct stat* buf) {
+    struct shim_dentry* dent;
 
     int ret = find_thread_each_fd(name, NULL, &dent);
     if (ret < 0)
@@ -451,43 +435,46 @@ out:
     return 0;
 }
 
-static int proc_thread_each_fd_follow_link (const char * name,
-                                            struct shim_qstr * link)
-{
+static int proc_thread_each_fd_follow_link(const char* name, struct shim_qstr* link) {
     return find_thread_each_fd(name, link, NULL);
 }
 
 static const struct proc_fs_ops fs_thread_each_fd = {
-            .open           = &proc_thread_each_fd_open,
-            .mode           = &proc_thread_each_fd_mode,
-            .stat           = &proc_thread_each_fd_stat,
-            .follow_link    = &proc_thread_each_fd_follow_link,
-        };
+    .open        = &proc_thread_each_fd_open,
+    .mode        = &proc_thread_each_fd_mode,
+    .stat        = &proc_thread_each_fd_stat,
+    .follow_link = &proc_thread_each_fd_follow_link,
+};
 
-static const struct proc_dir dir_fd = { .size = 1, .ent = { {
-            .nm_ops = &nm_thread_each_fd, .fs_ops = &fs_thread_each_fd,
-        }, }, };
+static const struct proc_dir dir_fd = {
+    .size = 1,
+    .ent =
+        {
+            {
+                .nm_ops = &nm_thread_each_fd,
+                .fs_ops = &fs_thread_each_fd,
+            },
+        },
+};
 
-static int proc_thread_maps_open (struct shim_handle * hdl,
-                                  const char * name, int flags)
-{
-    if (flags & (O_WRONLY|O_RDWR))
+static int proc_thread_maps_open(struct shim_handle* hdl, const char* name, int flags) {
+    if (flags & (O_WRONLY | O_RDWR))
         return -EACCES;
 
-    const char * next;
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
     if (ret < 0)
         return ret;
 
-    struct shim_thread * thread = lookup_thread(pid);
+    struct shim_thread* thread = lookup_thread(pid);
 
     if (!thread)
         return -ENOENT;
 
-    size_t count = DEFAULT_VMA_COUNT;
-    struct shim_vma_val * vmas = malloc(sizeof(struct shim_vma_val) * count);
+    size_t count              = DEFAULT_VMA_COUNT;
+    struct shim_vma_val* vmas = malloc(sizeof(struct shim_vma_val) * count);
 
     if (!vmas) {
         ret = -ENOMEM;
@@ -498,8 +485,7 @@ retry_dump_vmas:
     ret = dump_all_vmas(vmas, count);
 
     if (ret == -EOVERFLOW) {
-        struct shim_vma_val * new_vmas
-                = malloc(sizeof(struct shim_vma_val) * count * 2);
+        struct shim_vma_val* new_vmas = malloc(sizeof(struct shim_vma_val) * count * 2);
         if (!new_vmas) {
             ret = -ENOMEM;
             goto err;
@@ -513,61 +499,59 @@ retry_dump_vmas:
     if (ret < 0)
         goto err;
 
-#define DEFAULT_VMA_BUFFER_SIZE     256
+#define DEFAULT_VMA_BUFFER_SIZE 256
 
-    count = ret;
+    count              = ret;
     size_t buffer_size = DEFAULT_VMA_BUFFER_SIZE, offset = 0;
-    char * buffer = malloc(buffer_size);
+    char* buffer = malloc(buffer_size);
     if (!buffer) {
         ret = -ENOMEM;
         goto err;
     }
 
-    for (struct shim_vma_val * vma = vmas ; vma < vmas + count ; vma++) {
+    for (struct shim_vma_val* vma = vmas; vma < vmas + count; vma++) {
         size_t old_offset = offset;
-        uintptr_t start = (uintptr_t) vma->addr;
-        uintptr_t end   = (uintptr_t) vma->addr + vma->length;
-        char pt[3] = {
-            (vma->prot & PROT_READ)  ? 'r' : '-',
+        uintptr_t start   = (uintptr_t)vma->addr;
+        uintptr_t end     = (uintptr_t)vma->addr + vma->length;
+        char pt[3]        = {
+            (vma->prot & PROT_READ) ? 'r' : '-',
             (vma->prot & PROT_WRITE) ? 'w' : '-',
-            (vma->prot & PROT_EXEC)  ? 'x' : '-',
+            (vma->prot & PROT_EXEC) ? 'x' : '-',
         };
         char pr = (vma->flags & MAP_PRIVATE) ? 'p' : 's';
 
 #define ADDR_FMT(addr) ((addr) > 0xffffffff ? "%lx" : "%08lx")
-#define EMIT(fmt ...)                                                   \
-        do {                                                            \
-            offset += snprintf(buffer + offset, buffer_size - offset,   \
-                               fmt);                                    \
-        } while (0)
+#define EMIT(fmt...)                                                    \
+    do {                                                                \
+        offset += snprintf(buffer + offset, buffer_size - offset, fmt); \
+    } while (0)
 
-retry_emit_vma:
+    retry_emit_vma:
         if (vma->file) {
             int dev_major = 0, dev_minor = 0;
             unsigned long ino = vma->file->dentry ? vma->file->dentry->ino : 0;
-            const char * name = "[unknown]";
+            const char* name  = "[unknown]";
 
             if (!qstrempty(&vma->file->path))
                 name = qstrgetstr(&vma->file->path);
 
             EMIT(ADDR_FMT(start), start);
             EMIT("-");
-            EMIT(ADDR_FMT(end),   end);
-            EMIT(" %c%c%c%c %08lx %02d:%02d %lu %s\n", pt[0], pt[1], pt[2], pr,
-                 vma->offset, dev_major, dev_minor, ino, name);
+            EMIT(ADDR_FMT(end), end);
+            EMIT(" %c%c%c%c %08lx %02d:%02d %lu %s\n", pt[0], pt[1], pt[2], pr, vma->offset,
+                 dev_major, dev_minor, ino, name);
         } else {
             EMIT(ADDR_FMT(start), start);
             EMIT("-");
-            EMIT(ADDR_FMT(end),   end);
+            EMIT(ADDR_FMT(end), end);
             if (vma->comment[0])
-                EMIT(" %c%c%c%c 00000000 00:00 0 %s\n", pt[0], pt[1], pt[2], pr,
-                     vma->comment);
+                EMIT(" %c%c%c%c 00000000 00:00 0 %s\n", pt[0], pt[1], pt[2], pr, vma->comment);
             else
                 EMIT(" %c%c%c%c 00000000 00:00 0\n", pt[0], pt[1], pt[2], pr);
         }
 
         if (offset >= buffer_size) {
-            char * new_buffer = malloc(buffer_size * 2);
+            char* new_buffer = malloc(buffer_size * 2);
             if (!new_buffer) {
                 ret = -ENOMEM;
                 goto err;
@@ -582,19 +566,19 @@ retry_emit_vma:
         }
     }
 
-    struct shim_str_data * data = calloc(1, sizeof(struct shim_str_data));
+    struct shim_str_data* data = calloc(1, sizeof(struct shim_str_data));
     if (!data) {
         ret = -ENOMEM;
         goto err;
     }
 
-    data->str  = buffer;
-    data->len  = offset;
-    hdl->type  = TYPE_STR;
-    hdl->flags = flags & ~O_RDONLY;
-    hdl->acc_mode = MAY_READ;
+    data->str          = buffer;
+    data->len          = offset;
+    hdl->type          = TYPE_STR;
+    hdl->flags         = flags & ~O_RDONLY;
+    hdl->acc_mode      = MAY_READ;
     hdl->info.str.data = data;
-    ret = 0;
+    ret                = 0;
 out:
     put_thread(thread);
     if (vmas)
@@ -607,54 +591,48 @@ err:
     goto out;
 }
 
-static int proc_thread_maps_mode (const char * name, mode_t * mode)
-{
+static int proc_thread_maps_mode(const char* name, mode_t* mode) {
     // Only used by one file
     __UNUSED(name);
     *mode = 0400;
     return 0;
 }
 
-static int proc_thread_maps_stat (const char * name, struct stat * buf)
-{
+static int proc_thread_maps_stat(const char* name, struct stat* buf) {
     // Only used by one file
     __UNUSED(name);
     memset(buf, 0, sizeof(struct stat));
 
     buf->st_dev = buf->st_ino = 1;
-    buf->st_mode = 0400|S_IFREG;
-    buf->st_uid = 0;
-    buf->st_gid = 0;
-    buf->st_size = 0;
+    buf->st_mode              = 0400 | S_IFREG;
+    buf->st_uid               = 0;
+    buf->st_gid               = 0;
+    buf->st_size              = 0;
 
     return 0;
 }
 
 static const struct proc_fs_ops fs_thread_maps = {
-            .open           = &proc_thread_maps_open,
-            .mode           = &proc_thread_maps_mode,
-            .stat           = &proc_thread_maps_stat,
-        };
+    .open = &proc_thread_maps_open,
+    .mode = &proc_thread_maps_mode,
+    .stat = &proc_thread_maps_stat,
+};
 
-static int proc_thread_dir_open (struct shim_handle * hdl,
-                                 const char * name, int flags)
-{
+static int proc_thread_dir_open(struct shim_handle* hdl, const char* name, int flags) {
     __UNUSED(hdl);
     __UNUSED(name);
 
-    if (flags & (O_WRONLY|O_RDWR))
+    if (flags & (O_WRONLY | O_RDWR))
         return -EISDIR;
 
     // Don't really need to do any work here, but keeping as a placeholder,
     // just in case.
 
     return 0;
-
 }
 
-static int proc_thread_dir_mode (const char * name, mode_t * mode)
-{
-    const char * next;
+static int proc_thread_dir_mode(const char* name, mode_t* mode) {
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
@@ -665,23 +643,22 @@ static int proc_thread_dir_mode (const char * name, mode_t * mode)
     return 0;
 }
 
-static int proc_thread_dir_stat (const char * name, struct stat * buf)
-{
-    const char * next;
+static int proc_thread_dir_stat(const char* name, struct stat* buf) {
+    const char* next;
     size_t next_len;
     IDTYPE pid;
     int ret = parse_thread_name(name, &pid, &next, &next_len, NULL);
     if (ret < 0)
         return ret;
 
-    struct shim_thread * thread = lookup_thread(pid);
+    struct shim_thread* thread = lookup_thread(pid);
 
     if (!thread)
         return -ENOENT;
 
     memset(buf, 0, sizeof(struct stat));
     buf->st_dev = buf->st_ino = 1;
-    buf->st_mode = 0500|S_IFDIR;
+    buf->st_mode              = 0500 | S_IFDIR;
     lock(&thread->lock);
     buf->st_uid = thread->uid;
     buf->st_gid = thread->gid;
@@ -691,56 +668,56 @@ static int proc_thread_dir_stat (const char * name, struct stat * buf)
 }
 
 static const struct proc_fs_ops fs_thread_fd = {
-            .mode       = &proc_thread_dir_mode,
-            .stat       = &proc_thread_dir_stat,
-        };
+    .mode = &proc_thread_dir_mode,
+    .stat = &proc_thread_dir_stat,
+};
 
-static int proc_match_thread (const char * name)
-{
+static int proc_match_thread(const char* name) {
     IDTYPE pid;
     if (parse_thread_name(name, &pid, NULL, NULL, NULL) < 0)
         return 0;
 
-    struct shim_thread * thread = lookup_thread(pid);
+    struct shim_thread* thread = lookup_thread(pid);
 
     return thread ? 1 : 0;
 }
 
 struct walk_thread_arg {
-    struct shim_dirent * buf, * buf_end;
+    struct shim_dirent *buf, *buf_end;
 };
 
-static int walk_cb (struct shim_thread * thread, void * arg, bool * unlocked)
-{
+static int walk_cb(struct shim_thread* thread, void* arg, bool* unlocked) {
     // unlocked needed for kill
     __UNUSED(unlocked);
-    struct walk_thread_arg * args = (struct walk_thread_arg *) arg;
-    IDTYPE pid = thread->tid;
+    struct walk_thread_arg* args = (struct walk_thread_arg*)arg;
+    IDTYPE pid                   = thread->tid;
     int p = pid, l = 0;
-    for ( ; p ; p /= 10, l++);
+    for (; p; p /= 10, l++)
+        ;
 
-    if ((void *) (args->buf + 1) + l + 1 > (void *) args->buf_end)
+    if ((void*)(args->buf + 1) + l + 1 > (void*)args->buf_end)
         return -ENOBUFS;
 
-    struct shim_dirent * buf = args->buf;
+    struct shim_dirent* buf = args->buf;
 
-    buf->next = (void *) (buf + 1) + l + 1;
-    buf->ino = 1;
-    buf->type = LINUX_DT_DIR;
+    buf->next      = (void*)(buf + 1) + l + 1;
+    buf->ino       = 1;
+    buf->type      = LINUX_DT_DIR;
     buf->name[l--] = 0;
-    for (p = pid ; p ; p /= 10)
+    for (p = pid; p; p /= 10) {
         buf->name[l--] = p % 10 + '0';
+    }
 
     args->buf = buf->next;
     return 1;
 }
 
-static int proc_list_thread (const char * name, struct shim_dirent ** buf,
-                             int len)
-{
-    __UNUSED(name); // We know this is for "/proc/self"
-    struct walk_thread_arg args =
-        { .buf = *buf, .buf_end = (void *) *buf + len, };
+static int proc_list_thread(const char* name, struct shim_dirent** buf, int len) {
+    __UNUSED(name);  // We know this is for "/proc/self"
+    struct walk_thread_arg args = {
+        .buf     = *buf,
+        .buf_end = (void*)*buf + len,
+    };
 
     int ret = walk_thread_list(&walk_cb, &args);
     if (ret < 0)
@@ -751,20 +728,40 @@ static int proc_list_thread (const char * name, struct shim_dirent ** buf,
 }
 
 const struct proc_nm_ops nm_thread = {
-            .match_name = &proc_match_thread,
-            .list_name  = &proc_list_thread,
-        };
+    .match_name = &proc_match_thread,
+    .list_name  = &proc_list_thread,
+};
 
 const struct proc_fs_ops fs_thread = {
-            .open   = &proc_thread_dir_open,
-            .mode   = &proc_thread_dir_mode,
-            .stat   = &proc_thread_dir_stat,
-        };
+    .open = &proc_thread_dir_open,
+    .mode = &proc_thread_dir_mode,
+    .stat = &proc_thread_dir_stat,
+};
 
-const struct proc_dir dir_thread = { .size = 5, .ent = {
-        { .name = "cwd", .fs_ops = &fs_thread_link, },
-        { .name = "exe", .fs_ops = &fs_thread_link, },
-        { .name = "root", .fs_ops = &fs_thread_link, },
-        { .name = "fd", .dir = &dir_fd, .fs_ops = &fs_thread_fd, },
-        { .name = "maps", .fs_ops = &fs_thread_maps, },
-    }, };
+const struct proc_dir dir_thread = {
+    .size = 5,
+    .ent =
+        {
+            {
+                .name   = "cwd",
+                .fs_ops = &fs_thread_link,
+            },
+            {
+                .name   = "exe",
+                .fs_ops = &fs_thread_link,
+            },
+            {
+                .name   = "root",
+                .fs_ops = &fs_thread_link,
+            },
+            {
+                .name   = "fd",
+                .dir    = &dir_fd,
+                .fs_ops = &fs_thread_fd,
+            },
+            {
+                .name   = "maps",
+                .fs_ops = &fs_thread_maps,
+            },
+        },
+};

+ 57 - 75
LibOS/shim/src/fs/socket/fs.c

@@ -20,50 +20,44 @@
  * This file contains codes for implementation of 'socket' filesystem.
  */
 
-#include <shim_internal.h>
-#include <shim_fs.h>
-#include <shim_profile.h>
-
+#include <asm/fcntl.h>
+#include <asm/mman.h>
+#include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_internal.h>
+#include <shim_profile.h>
 
-#include <errno.h>
-
+// TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
+// should investigate and fix this behavior.
 #include <linux/stat.h>
-#include <linux/fcntl.h>
 
-#include <asm/mman.h>
-#include <asm/unistd.h>
-#include <asm/prctl.h>
-#include <asm/fcntl.h>
-
-static int socket_close (struct shim_handle * hdl)
-{
+static int socket_close(struct shim_handle* hdl) {
     /* XXX: Shouldn't this do something? */
     __UNUSED(hdl);
     return 0;
 }
 
-static ssize_t socket_read (struct shim_handle * hdl, void * buf, size_t count)
-{
-    struct shim_sock_handle * sock = &hdl->info.sock;
+static ssize_t socket_read(struct shim_handle* hdl, void* buf, size_t count) {
+    struct shim_sock_handle* sock = &hdl->info.sock;
 
     if (!count)
         return 0;
 
     lock(&hdl->lock);
 
-    if (sock->sock_type == SOCK_STREAM &&
-        sock->sock_state != SOCK_ACCEPTED &&
-        sock->sock_state != SOCK_CONNECTED &&
-        sock->sock_state != SOCK_BOUNDCONNECTED) {
+    if (sock->sock_type == SOCK_STREAM && sock->sock_state != SOCK_ACCEPTED &&
+        sock->sock_state != SOCK_CONNECTED && sock->sock_state != SOCK_BOUNDCONNECTED) {
         sock->error = ENOTCONN;
         unlock(&hdl->lock);
         return -ENOTCONN;
     }
 
-    if (sock->sock_type == SOCK_DGRAM &&
-        sock->sock_state != SOCK_CONNECTED &&
+    if (sock->sock_type == SOCK_DGRAM && sock->sock_state != SOCK_CONNECTED &&
         sock->sock_state != SOCK_BOUNDCONNECTED) {
         sock->error = EDESTADDRREQ;
         unlock(&hdl->lock);
@@ -75,7 +69,7 @@ static ssize_t socket_read (struct shim_handle * hdl, void * buf, size_t count)
     PAL_NUM bytes = DkStreamRead(hdl->pal_handle, 0, count, buf, NULL, 0);
 
     if (!bytes)
-        switch(PAL_NATIVE_ERRNO) {
+        switch (PAL_NATIVE_ERRNO) {
             case PAL_ERROR_ENDOFSTREAM:
                 return 0;
             default: {
@@ -87,27 +81,23 @@ static ssize_t socket_read (struct shim_handle * hdl, void * buf, size_t count)
             }
         }
 
-    assert((ssize_t) bytes > 0);
-    return (ssize_t) bytes;
+    assert((ssize_t)bytes > 0);
+    return (ssize_t)bytes;
 }
 
-static ssize_t socket_write (struct shim_handle * hdl, const void * buf, size_t count)
-{
-    struct shim_sock_handle * sock = &hdl->info.sock;
+static ssize_t socket_write(struct shim_handle* hdl, const void* buf, size_t count) {
+    struct shim_sock_handle* sock = &hdl->info.sock;
 
     lock(&hdl->lock);
 
-    if (sock->sock_type == SOCK_STREAM &&
-        sock->sock_state != SOCK_ACCEPTED &&
-        sock->sock_state != SOCK_CONNECTED &&
-        sock->sock_state != SOCK_BOUNDCONNECTED) {
+    if (sock->sock_type == SOCK_STREAM && sock->sock_state != SOCK_ACCEPTED &&
+        sock->sock_state != SOCK_CONNECTED && sock->sock_state != SOCK_BOUNDCONNECTED) {
         sock->error = ENOTCONN;
         unlock(&hdl->lock);
         return -ENOTCONN;
     }
 
-    if (sock->sock_type == SOCK_DGRAM &&
-        sock->sock_state != SOCK_CONNECTED &&
+    if (sock->sock_type == SOCK_DGRAM && sock->sock_state != SOCK_CONNECTED &&
         sock->sock_state != SOCK_BOUNDCONNECTED) {
         sock->error = EDESTADDRREQ;
         unlock(&hdl->lock);
@@ -119,11 +109,11 @@ static ssize_t socket_write (struct shim_handle * hdl, const void * buf, size_t
     if (!count)
         return 0;
 
-    PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void *) buf, NULL);
+    PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void*)buf, NULL);
 
     if (!bytes) {
         int err;
-        switch(PAL_NATIVE_ERRNO) {
+        switch (PAL_NATIVE_ERRNO) {
             case PAL_ERROR_CONNFAILED:
                 err = EPIPE;
                 break;
@@ -137,12 +127,11 @@ static ssize_t socket_write (struct shim_handle * hdl, const void * buf, size_t
         return -err;
     }
 
-    assert((ssize_t) bytes > 0);
-    return (ssize_t) bytes;
+    assert((ssize_t)bytes > 0);
+    return (ssize_t)bytes;
 }
 
-static int socket_hstat (struct shim_handle * hdl, struct stat * stat)
-{
+static int socket_hstat(struct shim_handle* hdl, struct stat* stat) {
     if (!stat)
         return 0;
 
@@ -153,38 +142,34 @@ static int socket_hstat (struct shim_handle * hdl, struct stat * stat)
 
     memset(stat, 0, sizeof(struct stat));
 
-    stat->st_ino    = 0;
-    stat->st_size   = (off_t) attr.pending_size;
-    stat->st_mode   = S_IFSOCK;
+    stat->st_ino  = 0;
+    stat->st_size = (off_t)attr.pending_size;
+    stat->st_mode = S_IFSOCK;
 
     return 0;
 }
 
-static int socket_checkout (struct shim_handle * hdl)
-{
+static int socket_checkout(struct shim_handle* hdl) {
     hdl->fs = NULL;
     return 0;
 }
 
-static off_t socket_poll (struct shim_handle * hdl, int poll_type)
-{
-    struct shim_sock_handle * sock = &hdl->info.sock;
-    off_t ret = 0;
+static off_t socket_poll(struct shim_handle* hdl, int poll_type) {
+    struct shim_sock_handle* sock = &hdl->info.sock;
+    off_t ret                     = 0;
 
     lock(&hdl->lock);
 
     if (poll_type & FS_POLL_RD) {
         if (sock->sock_type == SOCK_STREAM) {
-            if (sock->sock_state == SOCK_CREATED ||
-                sock->sock_state == SOCK_BOUND ||
+            if (sock->sock_state == SOCK_CREATED || sock->sock_state == SOCK_BOUND ||
                 sock->sock_state == SOCK_SHUTDOWN) {
                 ret = -ENOTCONN;
                 goto out;
             }
         }
 
-        if (sock->sock_type == SOCK_DGRAM &&
-            sock->sock_state == SOCK_SHUTDOWN) {
+        if (sock->sock_type == SOCK_DGRAM && sock->sock_state == SOCK_SHUTDOWN) {
             ret = -ENOTCONN;
             goto out;
         }
@@ -192,21 +177,17 @@ static off_t socket_poll (struct shim_handle * hdl, int poll_type)
 
     if (poll_type & FS_POLL_WR) {
         if (sock->sock_type == SOCK_STREAM) {
-            if (sock->sock_state == SOCK_CREATED ||
-                sock->sock_state == SOCK_BOUND ||
-                sock->sock_state == SOCK_LISTENED ||
-                sock->sock_state == SOCK_SHUTDOWN) {
+            if (sock->sock_state == SOCK_CREATED || sock->sock_state == SOCK_BOUND ||
+                sock->sock_state == SOCK_LISTENED || sock->sock_state == SOCK_SHUTDOWN) {
                 ret = -ENOTCONN;
                 goto out;
             }
         }
 
-        if (sock->sock_type == SOCK_DGRAM &&
-            sock->sock_state == SOCK_SHUTDOWN) {
+        if (sock->sock_type == SOCK_DGRAM && sock->sock_state == SOCK_SHUTDOWN) {
             ret = -ENOTCONN;
             goto out;
         }
-
     }
 
     if (!hdl->pal_handle) {
@@ -243,8 +224,7 @@ out:
     return ret;
 }
 
-static int socket_setflags (struct shim_handle * hdl, int flags)
-{
+static int socket_setflags(struct shim_handle* hdl, int flags) {
     if (!hdl->pal_handle)
         return 0;
 
@@ -266,20 +246,22 @@ static int socket_setflags (struct shim_handle * hdl, int flags)
     }
 
     if (!DkStreamAttributesSetByHandle(hdl->pal_handle, &attr))
-       return -PAL_ERRNO;
+        return -PAL_ERRNO;
 
     return 0;
 }
 
 struct shim_fs_ops socket_fs_ops = {
-        .close    = &socket_close,
-        .read     = &socket_read,
-        .write    = &socket_write,
-        .hstat    = &socket_hstat,
-        .checkout = &socket_checkout,
-        .poll     = &socket_poll,
-        .setflags = &socket_setflags,
-    };
-
-struct shim_mount socket_builtin_fs = { .type = "socket",
-                                        .fs_ops = &socket_fs_ops, };
+    .close    = &socket_close,
+    .read     = &socket_read,
+    .write    = &socket_write,
+    .hstat    = &socket_hstat,
+    .checkout = &socket_checkout,
+    .poll     = &socket_poll,
+    .setflags = &socket_setflags,
+};
+
+struct shim_mount socket_builtin_fs = {
+    .type   = "socket",
+    .fs_ops = &socket_fs_ops,
+};

+ 46 - 58
LibOS/shim/src/fs/str/fs.c

@@ -20,27 +20,21 @@
  * This file contains codes for implementation of 'str' filesystem.
  */
 
-#include <shim_internal.h>
-#include <shim_fs.h>
-#include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
-#include <linux/stat.h>
-#include <linux/fcntl.h>
-
 #include <asm/fcntl.h>
 #include <asm/mman.h>
-#include <asm/unistd.h>
 #include <asm/prctl.h>
+#include <asm/unistd.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <linux/stat.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_internal.h>
+#include <shim_profile.h>
 
-int str_open (struct shim_handle * hdl, struct shim_dentry * dent,
-                 int flags)
-{
-    struct shim_str_data * data = dent->data;
+int str_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
+    struct shim_str_data* data = dent->data;
 
     /* when str file is opened, it must have a data */
     if (!dent->data)
@@ -49,14 +43,13 @@ int str_open (struct shim_handle * hdl, struct shim_dentry * dent,
     REF_INC(data->ref_count);
 
     hdl->dentry = dent;
-    hdl->flags = flags;
+    hdl->flags  = flags;
 
     return 0;
 }
 
-int str_dput (struct shim_dentry * dent)
-{
-    struct shim_str_data * data = dent->data;
+int str_dput(struct shim_dentry* dent) {
+    struct shim_str_data* data = dent->data;
 
     if (!data || REF_DEC(data->ref_count) > 1)
         return 0;
@@ -66,7 +59,7 @@ int str_dput (struct shim_dentry * dent)
         data->str = NULL;
     }
 
-    data->len = 0;
+    data->len      = 0;
     data->buf_size = 0;
 
     free(dent->data);
@@ -74,9 +67,8 @@ int str_dput (struct shim_dentry * dent)
     return 0;
 }
 
-int str_close (struct shim_handle * hdl)
-{
-    if (hdl->flags & (O_WRONLY|O_RDWR)) {
+int str_close(struct shim_handle* hdl) {
+    if (hdl->flags & (O_WRONLY | O_RDWR)) {
         int ret = str_flush(hdl);
 
         if (ret < 0)
@@ -87,8 +79,7 @@ int str_close (struct shim_handle * hdl)
     return 0;
 }
 
-ssize_t str_read (struct shim_handle * hdl, void * buf, size_t count)
-{
+ssize_t str_read(struct shim_handle* hdl, void* buf, size_t count) {
     ssize_t ret = 0;
 
     if (!(hdl->acc_mode & MAY_READ)) {
@@ -96,12 +87,12 @@ ssize_t str_read (struct shim_handle * hdl, void * buf, size_t count)
         goto out;
     }
 
-    struct shim_str_handle * strhdl = &hdl->info.str;
+    struct shim_str_handle* strhdl = &hdl->info.str;
 
     assert(hdl->dentry);
     assert(strhdl->data);
 
-    struct shim_str_data * data = strhdl->data;
+    struct shim_str_data* data = strhdl->data;
 
     if (!data->str) {
         debug("str_data has no str\n");
@@ -112,7 +103,7 @@ ssize_t str_read (struct shim_handle * hdl, void * buf, size_t count)
     if (!strhdl->ptr)
         strhdl->ptr = data->str;
 
-    off_t  offset = strhdl->ptr - data->str;
+    off_t offset  = strhdl->ptr - data->str;
     size_t remain = data->len - offset;
 
     if (count >= remain) {
@@ -132,32 +123,31 @@ out:
     return ret;
 }
 
-ssize_t str_write (struct shim_handle * hdl, const void * buf, size_t count)
-{
+ssize_t str_write(struct shim_handle* hdl, const void* buf, size_t count) {
     if (!(hdl->acc_mode & MAY_WRITE))
         return -EACCES;
 
-    struct shim_str_handle * strhdl = &hdl->info.str;
+    struct shim_str_handle* strhdl = &hdl->info.str;
 
     assert(hdl->dentry);
     assert(strhdl->data);
 
-    struct shim_str_data * data = strhdl->data;
+    struct shim_str_data* data = strhdl->data;
 
-    if (!data->str ||
-        strhdl->ptr + count > data->str + data->buf_size) {
+    if (!data->str || strhdl->ptr + count > data->str + data->buf_size) {
         int newlen = 0;
 
         if (data->str) {
             newlen = data->buf_size * 2;
 
-            while (strhdl->ptr + count > data->str + newlen)
+            while (strhdl->ptr + count > data->str + newlen) {
                 newlen *= 2;
+            }
         } else {
             newlen = count;
         }
 
-        char * newbuf = malloc(newlen);
+        char* newbuf = malloc(newlen);
         if (!newbuf)
             return -ENOMEM;
 
@@ -166,8 +156,8 @@ ssize_t str_write (struct shim_handle * hdl, const void * buf, size_t count)
             free(data->str);
         }
 
-        strhdl->ptr = newbuf + (strhdl->ptr - data->str);
-        data->str = newbuf;
+        strhdl->ptr    = newbuf + (strhdl->ptr - data->str);
+        data->str      = newbuf;
         data->buf_size = newlen;
     }
 
@@ -181,16 +171,15 @@ ssize_t str_write (struct shim_handle * hdl, const void * buf, size_t count)
     return count;
 }
 
-off_t str_seek (struct shim_handle * hdl, off_t offset, int whence)
-{
-    struct shim_str_handle * strhdl = &hdl->info.str;
+off_t str_seek(struct shim_handle* hdl, off_t offset, int whence) {
+    struct shim_str_handle* strhdl = &hdl->info.str;
 
     assert(hdl->dentry);
     assert(strhdl->data);
 
-    struct shim_str_data * data = strhdl->data;
+    struct shim_str_data* data = strhdl->data;
 
-    switch(whence) {
+    switch (whence) {
         case SEEK_SET:
             if (offset < 0)
                 return -EINVAL;
@@ -223,14 +212,13 @@ off_t str_seek (struct shim_handle * hdl, off_t offset, int whence)
     return strhdl->ptr - data->str;
 }
 
-int str_flush (struct shim_handle * hdl)
-{
-    struct shim_str_handle * strhdl = &hdl->info.str;
+int str_flush(struct shim_handle* hdl) {
+    struct shim_str_handle* strhdl = &hdl->info.str;
 
     assert(hdl->dentry);
     assert(strhdl->data);
 
-    struct shim_str_data * data = strhdl->data;
+    struct shim_str_data* data = strhdl->data;
 
     if (!data->dirty)
         return 0;
@@ -242,14 +230,14 @@ int str_flush (struct shim_handle * hdl)
 }
 
 struct shim_fs_ops str_fs_ops = {
-        .close      = &str_close,
-        .read       = &str_read,
-        .write      = &str_write,
-        .seek       = &str_seek,
-        .flush      = &str_flush,
-    };
+    .close = &str_close,
+    .read  = &str_read,
+    .write = &str_write,
+    .seek  = &str_seek,
+    .flush = &str_flush,
+};
 
 struct shim_d_ops str_d_ops = {
-        .open       = &str_open,
-        .dput       = &str_dput,
-    };
+    .open = &str_open,
+    .dput = &str_dput,
+};

+ 69 - 69
LibOS/shim/src/ipc/shim_ipc.c

@@ -22,22 +22,21 @@
  * with acknowledgement), shim_ipc_info (IPC ports of process), shim_process.
  */
 
-#include <shim_internal.h>
-#include <shim_utils.h>
-#include <shim_thread.h>
+#include <list.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
 #include <shim_handle.h>
+#include <shim_internal.h>
 #include <shim_ipc.h>
-#include <shim_checkpoint.h>
-#include <shim_unistd.h>
 #include <shim_profile.h>
+#include <shim_thread.h>
+#include <shim_unistd.h>
+#include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-#include <list.h>
-
-#define IPC_INFO_MGR_ALLOC  32
-#define PAGE_SIZE           allocsize
-#define OBJ_TYPE struct shim_ipc_info
+#define IPC_INFO_MGR_ALLOC 32
+#define PAGE_SIZE          allocsize
+#define OBJ_TYPE           struct shim_ipc_info
 #include "memmgr.h"
 static MEM_MGR ipc_info_mgr;
 
@@ -45,10 +44,10 @@ struct shim_lock ipc_info_lock;
 
 struct shim_process cur_process;
 
-#define CLIENT_HASH_BITLEN  6
-#define CLIENT_HASH_NUM     (1 << CLIENT_HASH_BITLEN)
-#define CLIENT_HASH_MASK    (CLIENT_HASH_NUM - 1)
-#define CLIENT_HASH(vmid)   ((vmid) & CLIENT_HASH_MASK)
+#define CLIENT_HASH_BITLEN 6
+#define CLIENT_HASH_NUM    (1 << CLIENT_HASH_BITLEN)
+#define CLIENT_HASH_MASK   (CLIENT_HASH_NUM - 1)
+#define CLIENT_HASH(vmid)  ((vmid)&CLIENT_HASH_MASK)
 DEFINE_LISTP(shim_ipc_info);
 static LISTP_TYPE(shim_ipc_info) info_hlist[CLIENT_HASH_NUM];
 
@@ -153,12 +152,13 @@ struct shim_ipc_info* create_ipc_info_in_list(IDTYPE vmid, const char* uri, size
 
     /* check if info with this vmid & uri already exists and return it */
     LISTP_TYPE(shim_ipc_info)* info_bucket = &info_hlist[CLIENT_HASH(vmid)];
-    LISTP_FOR_EACH_ENTRY(info, info_bucket, hlist)
+    LISTP_FOR_EACH_ENTRY(info, info_bucket, hlist) {
         if (info->vmid == vmid && !qstrcmpstr(&info->uri, uri, len)) {
             get_ipc_info(info);
             unlock(&ipc_info_lock);
             return info;
         }
+    }
 
     /* otherwise create new info and return it */
     info = __create_ipc_info(vmid, uri, len);
@@ -189,12 +189,13 @@ struct shim_ipc_info* lookup_ipc_info(IDTYPE vmid) {
 
     struct shim_ipc_info* info;
     LISTP_TYPE(shim_ipc_info)* info_bucket = &info_hlist[CLIENT_HASH(vmid)];
-    LISTP_FOR_EACH_ENTRY(info, info_bucket, hlist)
+    LISTP_FOR_EACH_ENTRY(info, info_bucket, hlist) {
         if (info->vmid == vmid && !qstrempty(&info->uri)) {
             __get_ipc_info(info);
             unlock(&ipc_info_lock);
             return info;
         }
+    }
 
     unlock(&ipc_info_lock);
     return NULL;
@@ -219,9 +220,8 @@ struct shim_process* create_process(bool dup_cur_process) {
          */
         new_process->vmid = cur_process.vmid;
 
-        new_process->self = create_ipc_info(cur_process.self->vmid,
-                                            qstrgetstr(&cur_process.self->uri),
-                                            cur_process.self->uri.len);
+        new_process->self = create_ipc_info(
+            cur_process.self->vmid, qstrgetstr(&cur_process.self->uri), cur_process.self->uri.len);
         new_process->self->pal_handle = cur_process.self->pal_handle;
         if (!new_process->self) {
             unlock(&cur_process.lock);
@@ -231,19 +231,17 @@ struct shim_process* create_process(bool dup_cur_process) {
         /* there is a corner case of execve in very first process; such process does
          * not have parent process, so cannot copy parent IPC info */
         if (cur_process.parent) {
-            new_process->parent = create_ipc_info(cur_process.parent->vmid,
-                                                  qstrgetstr(&cur_process.parent->uri),
-                                                  cur_process.parent->uri.len);
+            new_process->parent =
+                create_ipc_info(cur_process.parent->vmid, qstrgetstr(&cur_process.parent->uri),
+                                cur_process.parent->uri.len);
             new_process->parent->pal_handle = cur_process.parent->pal_handle;
         }
-    }
-    else {
+    } else {
         /* fork/clone case, new process has new identity but inherits parent  */
-        new_process->vmid = 0;
-        new_process->self = NULL;
-        new_process->parent = create_ipc_info(cur_process.self->vmid,
-                                              qstrgetstr(&cur_process.self->uri),
-                                              cur_process.self->uri.len);
+        new_process->vmid   = 0;
+        new_process->self   = NULL;
+        new_process->parent = create_ipc_info(
+            cur_process.self->vmid, qstrgetstr(&cur_process.self->uri), cur_process.self->uri.len);
     }
 
     if (cur_process.parent && !new_process->parent) {
@@ -256,16 +254,17 @@ struct shim_process* create_process(bool dup_cur_process) {
     /* new process inherits the same namespace leaders */
     for (int i = 0; i < TOTAL_NS; i++) {
         if (cur_process.ns[i]) {
-            new_process->ns[i] = create_ipc_info(cur_process.ns[i]->vmid,
-                                                 qstrgetstr(&cur_process.ns[i]->uri),
-                                                 cur_process.ns[i]->uri.len);
+            new_process->ns[i] =
+                create_ipc_info(cur_process.ns[i]->vmid, qstrgetstr(&cur_process.ns[i]->uri),
+                                cur_process.ns[i]->uri.len);
             if (!new_process->ns[i]) {
                 if (new_process->self)
                     put_ipc_info(new_process->self);
                 if (new_process->parent)
                     put_ipc_info(new_process->parent);
-                for (int j = 0; j < i; j++)
+                for (int j = 0; j < i; j++) {
                     put_ipc_info(new_process->ns[j]);
+                }
                 unlock(&cur_process.lock);
                 return NULL;
             }
@@ -290,16 +289,16 @@ void free_process(struct shim_process* process) {
 void init_ipc_msg(struct shim_ipc_msg* msg, int code, size_t size, IDTYPE dest) {
     msg->code = code;
     msg->size = get_ipc_msg_size(size);
-    msg->src = cur_process.vmid;
-    msg->dst = dest;
-    msg->seq = 0;
+    msg->src  = cur_process.vmid;
+    msg->dst  = dest;
+    msg->seq  = 0;
 }
 
 void init_ipc_msg_duplex(struct shim_ipc_msg_duplex* msg, int code, size_t size, IDTYPE dest) {
     init_ipc_msg(&msg->msg, code, size, dest);
     msg->thread = NULL;
     INIT_LIST_HEAD(msg, list);
-    msg->retval = 0;
+    msg->retval  = 0;
     msg->private = NULL;
 }
 
@@ -310,11 +309,11 @@ int send_ipc_message(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
     debug("Sending ipc message to port %p (handle %p)\n", port, port->pal_handle);
 
     size_t total_bytes = msg->size;
-    size_t bytes = 0;
+    size_t bytes       = 0;
 
     do {
-        size_t ret = DkStreamWrite(port->pal_handle, 0, total_bytes - bytes,
-                                   (void *)msg + bytes, NULL);
+        size_t ret =
+            DkStreamWrite(port->pal_handle, 0, total_bytes - bytes, (void*)msg + bytes, NULL);
 
         if (!ret) {
             if (PAL_ERRNO == EINTR || PAL_ERRNO == EAGAIN || PAL_ERRNO == EWOULDBLOCK)
@@ -336,22 +335,23 @@ struct shim_ipc_msg_duplex* pop_ipc_msg_duplex(struct shim_ipc_port* port, unsig
 
     lock(&port->msgs_lock);
     struct shim_ipc_msg_duplex* tmp;
-    LISTP_FOR_EACH_ENTRY(tmp, &port->msgs, list)
+    LISTP_FOR_EACH_ENTRY(tmp, &port->msgs, list) {
         if (tmp->msg.seq == seq) {
             found = tmp;
             LISTP_DEL_INIT(tmp, &port->msgs, list);
             break;
         }
+    }
     unlock(&port->msgs_lock);
 
     return found;
 }
 
 int send_ipc_message_duplex(struct shim_ipc_msg_duplex* msg, struct shim_ipc_port* port,
-                  unsigned long* seq, void* private_data) {
+                            unsigned long* seq, void* private_data) {
     int ret = 0;
 
-    struct shim_thread * thread = get_cur_thread();
+    struct shim_thread* thread = get_cur_thread();
     assert(thread);
 
     /* prepare thread which will send the message for waiting for response
@@ -385,8 +385,7 @@ int send_ipc_message_duplex(struct shim_ipc_msg_duplex* msg, struct shim_ipc_por
             goto out;
     } while (ret != 0);
 
-    debug("Finished waiting for response (seq = %lu, ret = %d)\n",
-          msg->msg.seq, msg->retval);
+    debug("Finished waiting for response (seq = %lu, ret = %d)\n", msg->msg.seq, msg->retval);
     ret = msg->retval;
 out:
     lock(&port->msgs_lock);
@@ -411,13 +410,13 @@ struct shim_ipc_info* create_ipc_info_cur_process(bool is_self_ipc_info) {
 
     /* pipe for cur_process.self is of format "pipe:<cur_process.vmid>", others with random name */
     char uri[PIPE_URI_SIZE];
-    if (create_pipe(NULL, uri, PIPE_URI_SIZE, &info->pal_handle, &info->uri, is_self_ipc_info) < 0) {
+    if (create_pipe(NULL, uri, PIPE_URI_SIZE, &info->pal_handle, &info->uri, is_self_ipc_info) <
+        0) {
         put_ipc_info(info);
         return NULL;
     }
 
-    add_ipc_port_by_id(cur_process.vmid, info->pal_handle, IPC_PORT_SERVER,
-            NULL, &info->port);
+    add_ipc_port_by_id(cur_process.vmid, info->pal_handle, IPC_PORT_SERVER, NULL, &info->port);
 
     return info;
 }
@@ -451,18 +450,19 @@ int ipc_checkpoint_send(const char* cpdir, IDTYPE cpsession) {
     int ret;
     size_t len = strlen(cpdir);
 
-    size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_checkpoint) + len);
+    size_t total_msg_size    = get_ipc_msg_size(sizeof(struct shim_ipc_checkpoint) + len);
     struct shim_ipc_msg* msg = __alloca(total_msg_size);
     init_ipc_msg(msg, IPC_CHECKPOINT, total_msg_size, 0);
 
-    struct shim_ipc_checkpoint* msgin = (struct shim_ipc_checkpoint *)&msg->msg;
-    msgin->cpsession = cpsession;
+    struct shim_ipc_checkpoint* msgin = (struct shim_ipc_checkpoint*)&msg->msg;
+    msgin->cpsession                  = cpsession;
     memcpy(&msgin->cpdir, cpdir, len + 1);
 
     debug("IPC broadcast to all: IPC_CHECKPOINT(%u, %s)\n", cpsession, cpdir);
 
     /* broadcast to all including myself (so I can also checkpoint) */
-    ret = broadcast_ipc(msg, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT, /*exclude_port=*/NULL);
+    ret = broadcast_ipc(msg, IPC_PORT_DIRCLD | IPC_PORT_DIRPRT,
+                        /*exclude_port=*/NULL);
     SAVE_PROFILE_INTERVAL(ipc_checkpoint_send);
     return ret;
 }
@@ -473,18 +473,18 @@ int ipc_checkpoint_send(const char* cpdir, IDTYPE cpsession) {
  * - broadcasts checkpoint msg further to other processes. */
 int ipc_checkpoint_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
     BEGIN_PROFILE_INTERVAL();
-    int ret = 0;
-    struct shim_ipc_checkpoint* msgin = (struct shim_ipc_checkpoint *)msg->msg;
+    int ret                           = 0;
+    struct shim_ipc_checkpoint* msgin = (struct shim_ipc_checkpoint*)msg->msg;
 
-    debug("IPC callback from %u: IPC_CHECKPOINT(%u, %s)\n",
-          msg->src, msgin->cpsession, msgin->cpdir);
+    debug("IPC callback from %u: IPC_CHECKPOINT(%u, %s)\n", msg->src, msgin->cpsession,
+          msgin->cpdir);
 
     ret = create_checkpoint(msgin->cpdir, &msgin->cpsession);
     if (ret < 0)
         goto out;
 
     kill_all_threads(NULL, msgin->cpsession, SIGCP);
-    broadcast_ipc(msg, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT, port);
+    broadcast_ipc(msg, IPC_PORT_DIRCLD | IPC_PORT_DIRPRT, port);
 out:
     SAVE_PROFILE_INTERVAL(ipc_checkpoint_callback);
     return ret;
@@ -493,7 +493,7 @@ out:
 BEGIN_CP_FUNC(ipc_info) {
     assert(size == sizeof(struct shim_ipc_info));
 
-    struct shim_ipc_info* info = (struct shim_ipc_info *)obj;
+    struct shim_ipc_info* info     = (struct shim_ipc_info*)obj;
     struct shim_ipc_info* new_info = NULL;
 
     ptr_t off = GET_FROM_CP_MAP(obj);
@@ -502,7 +502,7 @@ BEGIN_CP_FUNC(ipc_info) {
         off = ADD_CP_OFFSET(sizeof(struct shim_ipc_info));
         ADD_TO_CP_MAP(obj, off);
 
-        new_info = (struct shim_ipc_info *)(base + off);
+        new_info = (struct shim_ipc_info*)(base + off);
         memcpy(new_info, info, sizeof(struct shim_ipc_info));
         REF_SET(new_info->ref_count, 0);
 
@@ -516,23 +516,23 @@ BEGIN_CP_FUNC(ipc_info) {
             DO_CP(palhdl, info->pal_handle, &entry);
             /* info's PAL handle will be re-opened with new URI during
              * palhdl restore (see checkpoint.c) */
-            entry->uri = &new_info->uri;
+            entry->uri     = &new_info->uri;
             entry->phandle = &new_info->pal_handle;
         }
     } else {
         /* already checkpointed */
-        new_info = (struct shim_ipc_info *)(base + off);
+        new_info = (struct shim_ipc_info*)(base + off);
     }
 
     if (new_info && objp)
-        *objp = (void *)new_info;
+        *objp = (void*)new_info;
 }
 END_CP_FUNC_NO_RS(ipc_info)
 
 BEGIN_CP_FUNC(process) {
     assert(size == sizeof(struct shim_process));
 
-    struct shim_process* process = (struct shim_process *)obj;
+    struct shim_process* process     = (struct shim_process*)obj;
     struct shim_process* new_process = NULL;
 
     ptr_t off = GET_FROM_CP_MAP(obj);
@@ -541,7 +541,7 @@ BEGIN_CP_FUNC(process) {
         off = ADD_CP_OFFSET(sizeof(struct shim_process));
         ADD_TO_CP_MAP(obj, off);
 
-        new_process = (struct shim_process *)(base + off);
+        new_process = (struct shim_process*)(base + off);
         memcpy(new_process, process, sizeof(struct shim_process));
 
         /* call ipc_info-specific checkpointing functions
@@ -557,17 +557,17 @@ BEGIN_CP_FUNC(process) {
         ADD_CP_FUNC_ENTRY(off);
     } else {
         /* already checkpointed */
-        new_process = (struct shim_process *)(base + off);
+        new_process = (struct shim_process*)(base + off);
     }
 
     if (objp)
-        *objp = (void *) new_process;
+        *objp = (void*)new_process;
 }
 END_CP_FUNC(process)
 
 BEGIN_RS_FUNC(process) {
     __UNUSED(offset);
-    struct shim_process* process = (void *)(base + GET_CP_FUNC_ENTRY());
+    struct shim_process* process = (void*)(base + GET_CP_FUNC_ENTRY());
 
     /* process vmid  = 0: fork/clone case, forces to pick up new host-OS vmid
      * process vmid != 0: execve case, forces to re-use vmid of parent */

+ 47 - 47
LibOS/shim/src/ipc/shim_ipc_child.c

@@ -21,16 +21,15 @@
  * processes and their children.
  */
 
-#include <shim_internal.h>
-#include <shim_thread.h>
+#include <errno.h>
+#include <pal.h>
+#include <pal_error.h>
 #include <shim_handle.h>
+#include <shim_internal.h>
 #include <shim_ipc.h>
-#include <shim_utils.h>
 #include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-#include <errno.h>
+#include <shim_thread.h>
+#include <shim_utils.h>
 
 struct thread_info {
     IDTYPE vmid;
@@ -42,7 +41,7 @@ struct thread_info {
 static int child_sthread_exit(struct shim_simple_thread* thread, void* arg, bool* unlocked) {
     __UNUSED(unlocked); /* FYI: notifies about unlocked thread_list_lock */
 
-    struct thread_info* info = (struct thread_info *) arg;
+    struct thread_info* info = (struct thread_info*)arg;
     int found_exiting_thread = 0;
 
     lock(&thread->lock);
@@ -50,9 +49,9 @@ static int child_sthread_exit(struct shim_simple_thread* thread, void* arg, bool
         found_exiting_thread = 1;
 
         if (thread->is_alive) {
-            thread->exit_code = -info->exitcode;
+            thread->exit_code   = -info->exitcode;
             thread->term_signal = info->term_signal;
-            thread->is_alive = false;
+            thread->is_alive    = false;
 
             /* arrange exit event for subsequent wait4(thread->tid) */
             DkEventSet(thread->exit_event);
@@ -66,7 +65,7 @@ static int child_sthread_exit(struct shim_simple_thread* thread, void* arg, bool
 static int child_thread_exit(struct shim_thread* thread, void* arg, bool* unlocked) {
     __UNUSED(unlocked); /* FYI: notifies about unlocked thread_list_lock */
 
-    struct thread_info* info = (struct thread_info *) arg;
+    struct thread_info* info = (struct thread_info*)arg;
     int found_exiting_thread = 0;
 
     lock(&thread->lock);
@@ -74,7 +73,7 @@ static int child_thread_exit(struct shim_thread* thread, void* arg, bool* unlock
         found_exiting_thread = 1;
 
         if (thread->is_alive) {
-            thread->exit_code = -info->exitcode;
+            thread->exit_code   = -info->exitcode;
             thread->term_signal = info->term_signal;
             unlock(&thread->lock);
 
@@ -108,7 +107,7 @@ void ipc_port_with_child_fini(struct shim_ipc_port* port, IDTYPE vmid, unsigned
      *       we previously sent SIGINT/SIGTERM/SIGKILL to this child and
      *       use the corresponding termination signal. For now, we simply
      *       report that child process was killed by SIGKILL. */
-    struct thread_info info = { .vmid = vmid, .exitcode = exitcode, .term_signal = SIGKILL };
+    struct thread_info info = {.vmid = vmid, .exitcode = exitcode, .term_signal = SIGKILL};
 
     /* message cannot come from our own threads (from ourselves as process) */
     assert(vmid != cur_process.vmid);
@@ -121,8 +120,10 @@ void ipc_port_with_child_fini(struct shim_ipc_port* port, IDTYPE vmid, unsigned
     if ((ret = walk_simple_thread_list(&child_sthread_exit, &info)) > 0)
         exited_threads_cnt += ret;
 
-    debug("Child process %u got disconnected: assuming that child exited and "
-          "forcing %d of its threads to exit\n", vmid & 0xFFFF, exited_threads_cnt);
+    debug(
+        "Child process %u got disconnected: assuming that child exited and "
+        "forcing %d of its threads to exit\n",
+        vmid & 0xFFFF, exited_threads_cnt);
 }
 
 DEFINE_PROFILE_INTERVAL(ipc_cld_exit_turnaround, ipc);
@@ -137,23 +138,23 @@ int ipc_cld_exit_send(IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned i
     __attribute__((unused)) unsigned long send_time = GET_PROFILE_INTERVAL();
     BEGIN_PROFILE_INTERVAL_SET(send_time);
 
-    size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_exit));
+    size_t total_msg_size    = get_ipc_msg_size(sizeof(struct shim_ipc_cld_exit));
     struct shim_ipc_msg* msg = __alloca(total_msg_size);
     init_ipc_msg(msg, IPC_CLD_EXIT, total_msg_size, 0);
 
-    struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit *) &msg->msg;
-    msgin->ppid        = ppid;
-    msgin->tid         = tid;
-    msgin->exitcode    = exitcode;
-    msgin->term_signal = term_signal;
+    struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit*)&msg->msg;
+    msgin->ppid                     = ppid;
+    msgin->tid                      = tid;
+    msgin->exitcode                 = exitcode;
+    msgin->term_signal              = term_signal;
 #ifdef PROFILE
-    msgin->time        = send_time;
+    msgin->time = send_time;
 #endif
 
-    debug("IPC broadcast: IPC_CLD_EXIT(%u, %u, %d, %u)\n",
-          ppid, tid, exitcode, term_signal);
+    debug("IPC broadcast: IPC_CLD_EXIT(%u, %u, %d, %u)\n", ppid, tid, exitcode, term_signal);
 
-    int ret = broadcast_ipc(msg, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD, /*exclude_port=*/NULL);
+    int ret = broadcast_ipc(msg, IPC_PORT_DIRPRT | IPC_PORT_DIRCLD,
+                            /*exclude_port=*/NULL);
     SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
     return ret;
 }
@@ -173,7 +174,7 @@ int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port)
     __UNUSED(port);
     int ret = 0;
 
-    struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit *) &msg->msg;
+    struct shim_ipc_cld_exit* msgin = (struct shim_ipc_cld_exit*)&msg->msg;
 
 #ifdef PROFILE
     unsigned long time = msgin->time;
@@ -183,8 +184,8 @@ int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port)
     BEGIN_PROFILE_INTERVAL_SET(time);
     SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
 
-    debug("IPC callback from %u: IPC_CLD_EXIT(%u, %u, %d, %u)\n",
-          msg->src & 0xFFFF, msgin->ppid, msgin->tid, msgin->exitcode, msgin->term_signal);
+    debug("IPC callback from %u: IPC_CLD_EXIT(%u, %u, %d, %u)\n", msg->src & 0xFFFF, msgin->ppid,
+          msgin->tid, msgin->exitcode, msgin->term_signal);
 
     /* message cannot come from our own threads (from ourselves as process) */
     assert(msg->src != cur_process.vmid);
@@ -216,7 +217,7 @@ int ipc_cld_exit_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port)
         struct shim_simple_thread* sthread = lookup_simple_thread(msgin->tid);
 
         if (!sthread) {
-            sthread = get_new_simple_thread();
+            sthread       = get_new_simple_thread();
             sthread->vmid = msg->src;
             sthread->tid  = msgin->tid;
             add_simple_thread(sthread);
@@ -244,7 +245,7 @@ DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
 #ifdef PROFILE
 int ipc_cld_profile_send(void) {
     struct shim_ipc_port* port = NULL;
-    IDTYPE dest = (IDTYPE)-1;
+    IDTYPE dest                = (IDTYPE)-1;
 
     /* port and dest are initialized to parent process */
     lock(&cur_process.lock);
@@ -258,8 +259,8 @@ int ipc_cld_profile_send(void) {
         return -ESRCH;
 
     unsigned long time = GET_PROFILE_INTERVAL();
-    size_t nsending = 0;
-    for (size_t i = 0; i < N_PROFILE; i++)
+    size_t nsending    = 0;
+    for (size_t i = 0; i < N_PROFILE; i++) {
         switch (PROFILES[i].type) {
             case OCCURENCE:
                 if (atomic_read(&PROFILES[i].val.occurence.count))
@@ -272,21 +273,22 @@ int ipc_cld_profile_send(void) {
             case CATEGORY:
                 break;
         }
+    }
 
-    size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_cld_profile) +
+    size_t total_msg_size    = get_ipc_msg_size(sizeof(struct shim_ipc_cld_profile) +
                                              sizeof(struct profile_val) * nsending);
     struct shim_ipc_msg* msg = __alloca(total_msg_size);
     init_ipc_msg(msg, IPC_CLD_PROFILE, total_msg_size, dest);
 
-    struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile *)&msg->msg;
+    struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile*)&msg->msg;
 
     size_t nsent = 0;
-    for (size_t i = 0; i < N_PROFILE && nsent < nsending; i++)
+    for (size_t i = 0; i < N_PROFILE && nsent < nsending; i++) {
         switch (PROFILES[i].type) {
             case OCCURENCE: {
                 unsigned long count = atomic_read(&PROFILES[i].val.occurence.count);
                 if (count) {
-                    msgin->profile[nsent].idx = i + 1;
+                    msgin->profile[nsent].idx                 = i + 1;
                     msgin->profile[nsent].val.occurence.count = count;
                     debug("Send %s: %lu times\n", PROFILES[i].name, count);
                     nsent++;
@@ -296,12 +298,12 @@ int ipc_cld_profile_send(void) {
             case INTERVAL: {
                 unsigned long count = atomic_read(&PROFILES[i].val.interval.count);
                 if (count) {
-                    msgin->profile[nsent].idx = i + 1;
+                    msgin->profile[nsent].idx                = i + 1;
                     msgin->profile[nsent].val.interval.count = count;
                     msgin->profile[nsent].val.interval.time =
                         atomic_read(&PROFILES[i].val.interval.time);
-                    debug("Send %s: %lu times, %lu msec\n", PROFILES[i].name,
-                          count, msgin->profile[nsent].val.interval.time);
+                    debug("Send %s: %lu times, %lu msec\n", PROFILES[i].name, count,
+                          msgin->profile[nsent].val.interval.time);
                     nsent++;
                 }
                 break;
@@ -309,8 +311,9 @@ int ipc_cld_profile_send(void) {
             case CATEGORY:
                 break;
         }
+    }
 
-    msgin->time = time;
+    msgin->time     = time;
     msgin->nprofile = nsent;
 
     debug("IPC send to %u: IPC_CLD_PROFILE\n", dest & 0xFFFF);
@@ -323,7 +326,7 @@ int ipc_cld_profile_send(void) {
 int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* port) {
     debug("IPC callback from %u: IPC_CLD_PROFILE\n", msg->src & 0xFFFF);
 
-    struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile *)&msg->msg;
+    struct shim_ipc_cld_profile* msgin = (struct shim_ipc_cld_profile*)&msg->msg;
 
     for (int i = 0; i < msgin->nprofile; i++) {
         int idx = msgin->profile[i].idx;
@@ -339,12 +342,9 @@ int ipc_cld_profile_callback(struct shim_ipc_msg* msg, struct shim_ipc_port* por
                 break;
             case INTERVAL:
                 debug("Receive %s: %u times, %lu msec\n", PROFILES[idx].name,
-                      msgin->profile[i].val.interval.count,
-                      msgin->profile[i].val.interval.time);
-                atomic_add(msgin->profile[i].val.interval.count,
-                           &PROFILES[idx].val.interval.count);
-                atomic_add(msgin->profile[i].val.interval.time,
-                           &PROFILES[idx].val.interval.time);
+                      msgin->profile[i].val.interval.count, msgin->profile[i].val.interval.time);
+                atomic_add(msgin->profile[i].val.interval.count, &PROFILES[idx].val.interval.count);
+                atomic_add(msgin->profile[i].val.interval.time, &PROFILES[idx].val.interval.time);
                 break;
             case CATEGORY:
                 break;

File diff suppressed because it is too large
+ 197 - 241
LibOS/shim/src/ipc/shim_ipc_nsimpl.h


File diff suppressed because it is too large
+ 226 - 306
LibOS/shim/src/ipc/shim_ipc_sysv.c


+ 39 - 49
LibOS/shim/src/shim_debug.c

@@ -20,32 +20,28 @@
  * This file contains codes for registering libraries to GDB.
  */
 
-#include <shim_internal.h>
-#include <shim_tls.h>
-#include <shim_handle.h>
-#include <shim_vma.h>
+#include <pal.h>
+#include <pal_error.h>
 #include <shim_checkpoint.h>
 #include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
 #include <shim_ipc.h>
-
-#include <pal.h>
-#include <pal_error.h>
+#include <shim_tls.h>
+#include <shim_vma.h>
 
 #ifndef DEBUG
 
-void clean_link_map_list (void)
-{
+void clean_link_map_list(void) {
     /* do nothing */
 }
 
-void remove_r_debug (void * addr)
-{
+void remove_r_debug(void* addr) {
     __UNUSED(addr);
     /* do nothing */
 }
 
-void append_r_debug (const char * uri, void * addr, void * dyn_addr)
-{
+void append_r_debug(const char* uri, void* addr, void* dyn_addr) {
     __UNUSED(uri);
     __UNUSED(addr);
     __UNUSED(dyn_addr);
@@ -54,27 +50,25 @@ void append_r_debug (const char * uri, void * addr, void * dyn_addr)
 
 #else /* !DEBUG */
 
-struct gdb_link_map
-{
-    void * l_addr;
-    char * l_name;
-    void * l_ld;
+struct gdb_link_map {
+    void* l_addr;
+    char* l_name;
+    void* l_ld;
     struct gdb_link_map *l_next, *l_prev;
 };
 
 /* XXX: What lock protects this?  vma_list_lock? */
-static struct gdb_link_map * link_map_list = NULL;
+static struct gdb_link_map* link_map_list = NULL;
 
-void clean_link_map_list (void)
-{
+void clean_link_map_list(void) {
     if (!link_map_list)
         return;
 
     if (link_map_list->l_prev)
         link_map_list->l_prev->l_next = NULL;
 
-    struct gdb_link_map * m = link_map_list;
-    for ( ; m ; m = m->l_next) {
+    struct gdb_link_map* m = link_map_list;
+    for (; m; m = m->l_next) {
         DkDebugDetachBinary(m->l_addr);
         free(m);
     }
@@ -82,11 +76,10 @@ void clean_link_map_list (void)
     link_map_list = NULL;
 }
 
-void remove_r_debug (void * addr)
-{
-    struct gdb_link_map * m = link_map_list;
+void remove_r_debug(void* addr) {
+    struct gdb_link_map* m = link_map_list;
 
-    for ( ; m ; m = m->l_next)
+    for (; m; m = m->l_next)
         if (m->l_addr == addr)
             break;
 
@@ -103,14 +96,13 @@ void remove_r_debug (void * addr)
     DkDebugDetachBinary(addr);
 }
 
-void append_r_debug (const char * uri, void * addr, void * dyn_addr)
-{
-    struct gdb_link_map * new = malloc(sizeof(struct gdb_link_map));
+void append_r_debug(const char* uri, void* addr, void* dyn_addr) {
+    struct gdb_link_map* new = malloc(sizeof(struct gdb_link_map));
     if (!new)
         return;
 
-    int uri_len = strlen(uri);
-    char * new_uri = malloc(uri_len + 1);
+    int uri_len   = strlen(uri);
+    char* new_uri = malloc(uri_len + 1);
     if (!new_uri) {
         free(new);
         return;
@@ -118,11 +110,11 @@ void append_r_debug (const char * uri, void * addr, void * dyn_addr)
     memcpy(new_uri, uri, uri_len + 1);
 
     new->l_addr = addr;
-    new->l_ld = dyn_addr;
+    new->l_ld   = dyn_addr;
     new->l_name = new_uri;
 
-    struct gdb_link_map *prev = NULL;
-    struct gdb_link_map **tail = &link_map_list;
+    struct gdb_link_map* prev  = NULL;
+    struct gdb_link_map** tail = &link_map_list;
 
     while (*tail) {
         prev = *tail;
@@ -133,28 +125,27 @@ void append_r_debug (const char * uri, void * addr, void * dyn_addr)
 
     new->l_prev = prev;
     new->l_next = NULL;
-    *tail = new;
+    *tail       = new;
 
     DkDebugAttachBinary(uri, addr);
 }
 
-BEGIN_CP_FUNC(gdb_map)
-{
+BEGIN_CP_FUNC(gdb_map) {
     __UNUSED(obj);
     __UNUSED(size);
     __UNUSED(objp);
-    struct gdb_link_map *m = link_map_list;
-    struct gdb_link_map *newm = NULL;
+    struct gdb_link_map* m    = link_map_list;
+    struct gdb_link_map* newm = NULL;
 
     while (m) {
         ptr_t off = ADD_CP_OFFSET(sizeof(struct gdb_link_map));
-        newm = (struct gdb_link_map *) (base + off);
+        newm      = (struct gdb_link_map*)(base + off);
 
         memcpy(newm, m, sizeof(struct gdb_link_map));
         newm->l_prev = newm->l_next = NULL;
 
-        size_t len = strlen(newm->l_name);
-        newm->l_name = (char *) (base + ADD_CP_OFFSET(len + 1));
+        size_t len   = strlen(newm->l_name);
+        newm->l_name = (char*)(base + ADD_CP_OFFSET(len + 1));
         memcpy(newm->l_name, m->l_name, len + 1);
 
         ADD_CP_FUNC_ENTRY(off);
@@ -163,17 +154,16 @@ BEGIN_CP_FUNC(gdb_map)
 }
 END_CP_FUNC(gdb_map)
 
-BEGIN_RS_FUNC(gdb_map)
-{
+BEGIN_RS_FUNC(gdb_map) {
     __UNUSED(offset);
-    struct gdb_link_map * map = (void *) (base + GET_CP_FUNC_ENTRY());
+    struct gdb_link_map* map = (void*)(base + GET_CP_FUNC_ENTRY());
 
     CP_REBASE(map->l_name);
     CP_REBASE(map->l_prev);
     CP_REBASE(map->l_next);
 
-    struct gdb_link_map *prev = NULL;
-    struct gdb_link_map **tail = &link_map_list;
+    struct gdb_link_map* prev  = NULL;
+    struct gdb_link_map** tail = &link_map_list;
 
     while (*tail) {
         prev = *tail;
@@ -181,7 +171,7 @@ BEGIN_RS_FUNC(gdb_map)
     }
 
     map->l_prev = prev;
-    *tail = map;
+    *tail       = map;
 
     DkDebugAttachBinary(map->l_name, map->l_addr);
 

+ 89 - 98
LibOS/shim/src/shim_malloc.c

@@ -24,29 +24,27 @@
  * allocation is requested, it ends up here (__system_alloc and __system_free).
  */
 
+#include <asm/mman.h>
+#include <pal.h>
+#include <pal_debug.h>
+#include <shim_checkpoint.h>
 #include <shim_internal.h>
-#include <shim_utils.h>
 #include <shim_profile.h>
-#include <shim_checkpoint.h>
+#include <shim_utils.h>
 #include <shim_vma.h>
 
-#include <pal.h>
-#include <pal_debug.h>
-
-#include <asm/mman.h>
-
 static struct shim_lock slab_mgr_lock;
 
-#define SYSTEM_LOCK()       lock(&slab_mgr_lock)
-#define SYSTEM_UNLOCK()     unlock(&slab_mgr_lock)
-#define PAGE_SIZE           allocsize
+#define SYSTEM_LOCK()   lock(&slab_mgr_lock)
+#define SYSTEM_UNLOCK() unlock(&slab_mgr_lock)
+#define PAGE_SIZE       allocsize
 
 #ifdef SLAB_DEBUG_TRACE
-# define SLAB_DEBUG
+#define SLAB_DEBUG
 #endif
 
 #define SLAB_CANARY
-#define STARTUP_SIZE    16
+#define STARTUP_SIZE 16
 
 #include <slabmgr.h>
 
@@ -55,11 +53,11 @@ static SLAB_MGR slab_mgr = NULL;
 DEFINE_PROFILE_CATEGORY(memory, );
 
 /* Returns NULL on failure */
-void * __system_malloc (size_t size)
-{
+void* __system_malloc(size_t size) {
     size_t alloc_size = ALIGN_UP(size);
-    void * addr, * ret_addr;
-    int flags = MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL;
+    void* addr;
+    void* ret_addr;
+    int flags = MAP_PRIVATE | MAP_ANONYMOUS | VMA_INTERNAL;
 
     /*
      * If vmas are initialized, we need to request a free address range
@@ -68,15 +66,13 @@ void * __system_malloc (size_t size)
      * pattern.  It is not safe to just call DkVirtualMemoryAlloc directly
      * without reserving the vma region first.
      */
-    addr = bkeep_unmapped_any(alloc_size, PROT_READ|PROT_WRITE, flags,
-                              NULL, 0, "slab");
+    addr = bkeep_unmapped_any(alloc_size, PROT_READ | PROT_WRITE, flags, NULL, 0, "slab");
 
     if (!addr)
         return NULL;
 
     do {
-        ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0,
-                                        PAL_PROT_WRITE|PAL_PROT_READ);
+        ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0, PAL_PROT_WRITE | PAL_PROT_READ);
 
         if (!ret_addr) {
             /* If the allocation is interrupted by signal, try to handle the
@@ -95,16 +91,14 @@ void * __system_malloc (size_t size)
     return addr;
 }
 
-void __system_free (void * addr, size_t size)
-{
+void __system_free(void* addr, size_t size) {
     DkVirtualMemoryFree(addr, ALIGN_UP(size));
 
     if (bkeep_munmap(addr, ALIGN_UP(size), VMA_INTERNAL) < 0)
         BUG();
 }
 
-int init_slab (void)
-{
+int init_slab(void) {
     create_lock(&slab_mgr_lock);
     slab_mgr = create_slab_mgr();
     return 0;
@@ -112,8 +106,7 @@ int init_slab (void)
 
 EXTERN_ALIAS(init_slab);
 
-int reinit_slab (void)
-{
+int reinit_slab(void) {
     if (slab_mgr) {
         destroy_slab_mgr(slab_mgr);
         slab_mgr = NULL;
@@ -132,54 +125,54 @@ DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
 DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
 
 #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
-void * __malloc_debug (size_t size, const char * file, int line)
+void* __malloc_debug(size_t size, const char* file, int line)
 #else
-void * malloc (size_t size)
+void* malloc(size_t size)
 #endif
 {
 #ifdef PROFILE
     int level = -1;
 
-    for (size_t i = 0 ; i < SLAB_LEVEL ; i++)
+    for (size_t i = 0; i < SLAB_LEVEL; i++)
         if (size < slab_levels[i]) {
             level = i;
             break;
         }
-    switch(level) {
-    case 0:
-        INC_PROFILE_OCCURENCE(malloc_0);
-        break;
-    case 1:
-        INC_PROFILE_OCCURENCE(malloc_1);
-        break;
-    case 2:
-        INC_PROFILE_OCCURENCE(malloc_2);
-        break;
-    case 3:
-        INC_PROFILE_OCCURENCE(malloc_3);
-        break;
-    case 4:
-        INC_PROFILE_OCCURENCE(malloc_4);
-        break;
-    case 5:
-        INC_PROFILE_OCCURENCE(malloc_5);
-        break;
-    case 6:
-        INC_PROFILE_OCCURENCE(malloc_6);
-        break;
-    case 7:
-        INC_PROFILE_OCCURENCE(malloc_7);
-        break;
-    case -1:
-        INC_PROFILE_OCCURENCE(malloc_big);
-        break;
+    switch (level) {
+        case 0:
+            INC_PROFILE_OCCURENCE(malloc_0);
+            break;
+        case 1:
+            INC_PROFILE_OCCURENCE(malloc_1);
+            break;
+        case 2:
+            INC_PROFILE_OCCURENCE(malloc_2);
+            break;
+        case 3:
+            INC_PROFILE_OCCURENCE(malloc_3);
+            break;
+        case 4:
+            INC_PROFILE_OCCURENCE(malloc_4);
+            break;
+        case 5:
+            INC_PROFILE_OCCURENCE(malloc_5);
+            break;
+        case 6:
+            INC_PROFILE_OCCURENCE(malloc_6);
+            break;
+        case 7:
+            INC_PROFILE_OCCURENCE(malloc_7);
+            break;
+        case -1:
+            INC_PROFILE_OCCURENCE(malloc_big);
+            break;
     }
 #endif
 
 #ifdef SLAB_DEBUG_TRACE
-    void * mem = slab_alloc_debug(slab_mgr, size, file, line);
+    void* mem = slab_alloc_debug(slab_mgr, size, file, line);
 #else
-    void * mem = slab_alloc(slab_mgr, size);
+    void* mem = slab_alloc(slab_mgr, size);
 #endif
 
     if (!mem) {
@@ -201,13 +194,12 @@ void * malloc (size_t size)
 EXTERN_ALIAS(malloc);
 #endif
 
-void * calloc (size_t nmemb, size_t size)
-{
+void* calloc(size_t nmemb, size_t size) {
     // This overflow checking is not a UB, because the operands are unsigned.
     size_t total = nmemb * size;
     if (total / size != nmemb)
         return NULL;
-    void *ptr = malloc(total);
+    void* ptr = malloc(total);
     if (ptr)
         memset(ptr, 0, total);
     return ptr;
@@ -245,16 +237,15 @@ EXTERN_ALIAS(realloc);
 
 // Copies data from `mem` to a newly allocated buffer of a specified size.
 #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
-void * __malloc_copy_debug (const void * mem, size_t size,
-                         const char * file, int line)
+void* __malloc_copy_debug(const void* mem, size_t size, const char* file, int line)
 #else
-void * malloc_copy (const void * mem, size_t size)
+void* malloc_copy(const void* mem, size_t size)
 #endif
 {
 #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
-    void * buff = __malloc_debug(size, file, line);
+    void* buff = __malloc_debug(size, file, line);
 #else
-    void * buff = malloc(size);
+    void* buff = malloc(size);
 #endif
     if (buff)
         memcpy(buff, mem, size);
@@ -276,9 +267,9 @@ DEFINE_PROFILE_OCCURENCE(free_big, memory);
 DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
 
 #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
-void __free_debug (void * mem, const char * file, int line)
+void __free_debug(void* mem, const char* file, int line)
 #else
-void free (void * mem)
+void free(void* mem)
 #endif
 {
     if (!mem)
@@ -290,35 +281,35 @@ void free (void * mem)
 
 #ifdef PROFILE
     int level = RAW_TO_LEVEL(mem);
-    switch(level) {
-    case 0:
-        INC_PROFILE_OCCURENCE(free_0);
-        break;
-    case 1:
-        INC_PROFILE_OCCURENCE(free_1);
-        break;
-    case 2:
-        INC_PROFILE_OCCURENCE(free_2);
-        break;
-    case 3:
-        INC_PROFILE_OCCURENCE(free_3);
-        break;
-    case 4:
-        INC_PROFILE_OCCURENCE(free_4);
-        break;
-    case 5:
-        INC_PROFILE_OCCURENCE(free_5);
-        break;
-    case 6:
-        INC_PROFILE_OCCURENCE(free_6);
-        break;
-    case 7:
-        INC_PROFILE_OCCURENCE(free_7);
-        break;
-    case -1:
-    case 255:
-        INC_PROFILE_OCCURENCE(free_big);
-        break;
+    switch (level) {
+        case 0:
+            INC_PROFILE_OCCURENCE(free_0);
+            break;
+        case 1:
+            INC_PROFILE_OCCURENCE(free_1);
+            break;
+        case 2:
+            INC_PROFILE_OCCURENCE(free_2);
+            break;
+        case 3:
+            INC_PROFILE_OCCURENCE(free_3);
+            break;
+        case 4:
+            INC_PROFILE_OCCURENCE(free_4);
+            break;
+        case 5:
+            INC_PROFILE_OCCURENCE(free_5);
+            break;
+        case 6:
+            INC_PROFILE_OCCURENCE(free_6);
+            break;
+        case 7:
+            INC_PROFILE_OCCURENCE(free_7);
+            break;
+        case -1:
+        case 255:
+            INC_PROFILE_OCCURENCE(free_big);
+            break;
     }
 #endif
 

+ 4 - 6
LibOS/shim/src/shim_object.c

@@ -1,16 +1,14 @@
 #include <pal.h>
 #include <shim_internal.h>
 
-int object_wait_with_retry(PAL_HANDLE handle)
-{
+int object_wait_with_retry(PAL_HANDLE handle) {
     PAL_HANDLE ret;
     do {
         ret = DkObjectsWaitAny(1, &handle, NO_TIMEOUT);
-    } while (ret == NULL && (PAL_NATIVE_ERRNO == PAL_ERROR_INTERRUPTED ||
-                             PAL_NATIVE_ERRNO == PAL_ERROR_TRYAGAIN));
+    } while (ret == NULL &&
+             (PAL_NATIVE_ERRNO == PAL_ERROR_INTERRUPTED || PAL_NATIVE_ERRNO == PAL_ERROR_TRYAGAIN));
     if (ret == NULL) {
-        debug("waiting on %p resulted in error %s",
-              handle, PAL_STRERROR(PAL_NATIVE_ERRNO));
+        debug("waiting on %p resulted in error %s", handle, PAL_STRERROR(PAL_NATIVE_ERRNO));
         return -PAL_NATIVE_ERRNO;
     }
     assert(ret == handle);

File diff suppressed because it is too large
+ 431 - 432
LibOS/shim/src/shim_parser.c


File diff suppressed because it is too large
+ 321 - 429
LibOS/shim/src/shim_syscalls.c


+ 320 - 320
LibOS/shim/src/shim_table.c

@@ -20,330 +20,330 @@
  * This file contains the system call table used by application libraries.
  */
 
-#include <shim_table.h>
 #include <shim_internal.h>
+#include <shim_table.h>
 
-void debug_unsupp (int num){
-    debug ("Unsupported system call %d\n", num);
+void debug_unsupp(int num) {
+    debug("Unsupported system call %d\n", num);
 }
 
-shim_fp shim_table [LIBOS_SYSCALL_BOUND] = {
-    (shim_fp) __shim_read,
-    (shim_fp) __shim_write,
-    (shim_fp) __shim_open,
-    (shim_fp) __shim_close,
-    (shim_fp) __shim_stat,
-    (shim_fp) __shim_fstat,
-    (shim_fp) __shim_lstat,
-    (shim_fp) __shim_poll,
-    (shim_fp) __shim_lseek,
-    (shim_fp) __shim_mmap,
-    (shim_fp) __shim_mprotect,
-    (shim_fp) __shim_munmap,
-    (shim_fp) __shim_brk,
-    (shim_fp) __shim_rt_sigaction,
-    (shim_fp) __shim_rt_sigprocmask,
-    (shim_fp) __shim_rt_sigreturn,
-    (shim_fp) __shim_ioctl,
-    (shim_fp) __shim_pread64,
-    (shim_fp) __shim_pwrite64,
-    (shim_fp) __shim_readv,
-    (shim_fp) __shim_writev,
-    (shim_fp) __shim_access,
-    (shim_fp) __shim_pipe,
-    (shim_fp) __shim_select,
-    (shim_fp) __shim_sched_yield,
-    (shim_fp) __shim_mremap,
-    (shim_fp) __shim_msync,
-    (shim_fp) __shim_mincore,
-    (shim_fp) __shim_madvise,
-    (shim_fp) __shim_shmget,
-    (shim_fp) __shim_shmat,
-    (shim_fp) __shim_shmctl,
-    (shim_fp) __shim_dup,
-    (shim_fp) __shim_dup2,
-    (shim_fp) __shim_pause,
-    (shim_fp) __shim_nanosleep,
-    (shim_fp) __shim_getitimer,
-    (shim_fp) __shim_alarm,
-    (shim_fp) __shim_setitimer,
-    (shim_fp) __shim_getpid,
-    (shim_fp) __shim_sendfile,
-    (shim_fp) __shim_socket,
-    (shim_fp) __shim_connect,
-    (shim_fp) __shim_accept,
-    (shim_fp) __shim_sendto,
-    (shim_fp) __shim_recvfrom,
-    (shim_fp) __shim_sendmsg,
-    (shim_fp) __shim_recvmsg,
-    (shim_fp) __shim_shutdown,
-    (shim_fp) __shim_bind,
-    (shim_fp) __shim_listen,
-    (shim_fp) __shim_getsockname,
-    (shim_fp) __shim_getpeername,
-    (shim_fp) __shim_socketpair,
-    (shim_fp) __shim_setsockopt,
-    (shim_fp) __shim_getsockopt,
-    (shim_fp) __shim_clone,
-    (shim_fp) __shim_fork,
-    (shim_fp) __shim_vfork,
-    (shim_fp) __shim_execve,
-    (shim_fp) __shim_exit,
-    (shim_fp) __shim_wait4,
-    (shim_fp) __shim_kill,
-    (shim_fp) __shim_uname,
-    (shim_fp) __shim_semget,
-    (shim_fp) __shim_semop,
-    (shim_fp) __shim_semctl,
-    (shim_fp) __shim_shmdt,
-    (shim_fp) __shim_msgget,
-    (shim_fp) __shim_msgsnd,
-    (shim_fp) __shim_msgrcv,
-    (shim_fp) __shim_msgctl,
-    (shim_fp) __shim_fcntl,
-    (shim_fp) __shim_flock,
-    (shim_fp) __shim_fsync,
-    (shim_fp) __shim_fdatasync,
-    (shim_fp) __shim_truncate,
-    (shim_fp) __shim_ftruncate,
-    (shim_fp) __shim_getdents,
-    (shim_fp) __shim_getcwd,
-    (shim_fp) __shim_chdir,
-    (shim_fp) __shim_fchdir,
-    (shim_fp) __shim_rename,
-    (shim_fp) __shim_mkdir,
-    (shim_fp) __shim_rmdir,
-    (shim_fp) __shim_creat,
-    (shim_fp) __shim_link,
-    (shim_fp) __shim_unlink,
-    (shim_fp) __shim_symlink,
-    (shim_fp) __shim_readlink,
-    (shim_fp) __shim_chmod,
-    (shim_fp) __shim_fchmod,
-    (shim_fp) __shim_chown,
-    (shim_fp) __shim_fchown,
-    (shim_fp) __shim_lchown,
-    (shim_fp) __shim_umask,
-    (shim_fp) __shim_gettimeofday,
-    (shim_fp) __shim_getrlimit,
-    (shim_fp) __shim_getrusage,
-    (shim_fp) __shim_sysinfo,
-    (shim_fp) __shim_times,
-    (shim_fp) __shim_ptrace,
-    (shim_fp) __shim_getuid,
-    (shim_fp) __shim_syslog,
-    (shim_fp) __shim_getgid,
-    (shim_fp) __shim_setuid,
-    (shim_fp) __shim_setgid,
-    (shim_fp) __shim_geteuid,
-    (shim_fp) __shim_getegid,
-    (shim_fp) __shim_setpgid,
-    (shim_fp) __shim_getppid,
-    (shim_fp) __shim_getpgrp,
-    (shim_fp) __shim_setsid,
-    (shim_fp) __shim_setreuid,
-    (shim_fp) __shim_setregid,
-    (shim_fp) __shim_getgroups,
-    (shim_fp) __shim_setgroups,
-    (shim_fp) __shim_setresuid,
-    (shim_fp) __shim_getresuid,
-    (shim_fp) __shim_setresgid,
-    (shim_fp) __shim_getresgid,
-    (shim_fp) __shim_getpgid,
-    (shim_fp) __shim_setfsuid,
-    (shim_fp) __shim_setfsgid,
-    (shim_fp) __shim_getsid,
-    (shim_fp) __shim_capget,
-    (shim_fp) __shim_capset,
-    (shim_fp) __shim_rt_sigpending,
-    (shim_fp) __shim_rt_sigtimedwait,
-    (shim_fp) __shim_rt_sigqueueinfo,
-    (shim_fp) __shim_rt_sigsuspend,
-    (shim_fp) __shim_sigaltstack,
-    (shim_fp) __shim_utime,
-    (shim_fp) __shim_mknod,
-    (shim_fp) __shim_uselib,
-    (shim_fp) __shim_personality,
-    (shim_fp) __shim_ustat,
-    (shim_fp) __shim_statfs,
-    (shim_fp) __shim_fstatfs,
-    (shim_fp) __shim_sysfs,
-    (shim_fp) __shim_getpriority,
-    (shim_fp) __shim_setpriority,
-    (shim_fp) __shim_sched_setparam,
-    (shim_fp) __shim_sched_getparam,
-    (shim_fp) __shim_sched_setscheduler,
-    (shim_fp) __shim_sched_getscheduler,
-    (shim_fp) __shim_sched_get_priority_max,
-    (shim_fp) __shim_sched_get_priority_min,
-    (shim_fp) __shim_sched_rr_get_interval,
-    (shim_fp) __shim_mlock,
-    (shim_fp) __shim_munlock,
-    (shim_fp) __shim_mlockall,
-    (shim_fp) __shim_munlockall,
-    (shim_fp) __shim_vhangup,
-    (shim_fp) __shim_modify_ldt,
-    (shim_fp) __shim_pivot_root,
-    (shim_fp) __shim__sysctl,
-    (shim_fp) __shim_prctl,
-    (shim_fp) __shim_arch_prctl,
-    (shim_fp) __shim_adjtimex,
-    (shim_fp) __shim_setrlimit,
-    (shim_fp) __shim_chroot,
-    (shim_fp) __shim_sync,
-    (shim_fp) __shim_acct,
-    (shim_fp) __shim_settimeofday,
-    (shim_fp) __shim_mount,
-    (shim_fp) __shim_umount2,
-    (shim_fp) __shim_swapon,
-    (shim_fp) __shim_swapoff,
-    (shim_fp) __shim_reboot,
-    (shim_fp) __shim_sethostname,
-    (shim_fp) __shim_setdomainname,
-    (shim_fp) __shim_iopl,
-    (shim_fp) __shim_ioperm,
-    (shim_fp) __shim_create_module,
-    (shim_fp) __shim_init_module,
-    (shim_fp) __shim_delete_module,
-    (shim_fp) 0, // shim_get_kernel_syms,
-    (shim_fp) __shim_query_module,
-    (shim_fp) __shim_quotactl,
-    (shim_fp) 0, // shim_nfsservctl,
-    (shim_fp) 0, // shim_getpmsg,
-    (shim_fp) 0, // shim_putpmsg,
-    (shim_fp) 0, // shim_afs_syscall,
-    (shim_fp) 0, // shim_tuxcall,
-    (shim_fp) 0, // shim_security,
-    (shim_fp) __shim_gettid,
-    (shim_fp) __shim_readahead,
-    (shim_fp) __shim_setxattr,
-    (shim_fp) __shim_lsetxattr,
-    (shim_fp) __shim_fsetxattr,
-    (shim_fp) __shim_getxattr,
-    (shim_fp) __shim_lgetxattr,
-    (shim_fp) __shim_fgetxattr,
-    (shim_fp) __shim_listxattr,
-    (shim_fp) __shim_llistxattr,
-    (shim_fp) __shim_flistxattr,
-    (shim_fp) __shim_removexattr,
-    (shim_fp) __shim_lremovexattr,
-    (shim_fp) __shim_fremovexattr,
-    (shim_fp) __shim_tkill,
-    (shim_fp) __shim_time,
-    (shim_fp) __shim_futex,
-    (shim_fp) __shim_sched_setaffinity,
-    (shim_fp) __shim_sched_getaffinity,
-    (shim_fp) __shim_set_thread_area,
-    (shim_fp) __shim_io_setup,
-    (shim_fp) __shim_io_destroy,
-    (shim_fp) __shim_io_getevents,
-    (shim_fp) __shim_io_submit,
-    (shim_fp) __shim_io_cancel,
-    (shim_fp) __shim_get_thread_area,
-    (shim_fp) __shim_lookup_dcookie,
-    (shim_fp) __shim_epoll_create,
-    (shim_fp) 0, // shim_epoll_ctl_old,
-    (shim_fp) 0, // shim_epoll_wait_old,
-    (shim_fp) __shim_remap_file_pages,
-    (shim_fp) __shim_getdents64,
-    (shim_fp) __shim_set_tid_address,
-    (shim_fp) __shim_restart_syscall,
-    (shim_fp) __shim_semtimedop,
-    (shim_fp) __shim_fadvise64,
-    (shim_fp) __shim_timer_create,
-    (shim_fp) __shim_timer_settime,
-    (shim_fp) __shim_timer_gettime,
-    (shim_fp) __shim_timer_getoverrun,
-    (shim_fp) __shim_timer_delete,
-    (shim_fp) __shim_clock_settime,
-    (shim_fp) __shim_clock_gettime,
-    (shim_fp) __shim_clock_getres,
-    (shim_fp) __shim_clock_nanosleep,
-    (shim_fp) __shim_exit_group,
-    (shim_fp) __shim_epoll_wait,
-    (shim_fp) __shim_epoll_ctl,
-    (shim_fp) __shim_tgkill,
-    (shim_fp) __shim_utimes,
-    (shim_fp) 0, // shim_vserver,
-    (shim_fp) __shim_mbind,
-    (shim_fp) __shim_set_mempolicy,
-    (shim_fp) __shim_get_mempolicy,
-    (shim_fp) __shim_mq_open,
-    (shim_fp) __shim_mq_unlink,
-    (shim_fp) __shim_mq_timedsend,
-    (shim_fp) __shim_mq_timedreceive,
-    (shim_fp) __shim_mq_notify,
-    (shim_fp) __shim_mq_getsetattr,
-    (shim_fp) 0, // shim_kexec_load,
-    (shim_fp) __shim_waitid,
-    (shim_fp) 0, // shim_add_key,
-    (shim_fp) 0, // shim_request_key,
-    (shim_fp) 0, // shim_keyctl,
-    (shim_fp) __shim_ioprio_set,
-    (shim_fp) __shim_ioprio_get,
-    (shim_fp) __shim_inotify_init,
-    (shim_fp) __shim_inotify_add_watch,
-    (shim_fp) __shim_inotify_rm_watch,
-    (shim_fp) __shim_migrate_pages,
-    (shim_fp) __shim_openat,
-    (shim_fp) __shim_mkdirat,
-    (shim_fp) __shim_mknodat,
-    (shim_fp) __shim_fchownat,
-    (shim_fp) __shim_futimesat,
-    (shim_fp) __shim_newfstatat,
-    (shim_fp) __shim_unlinkat,
-    (shim_fp) __shim_renameat,
-    (shim_fp) __shim_linkat,
-    (shim_fp) __shim_symlinkat,
-    (shim_fp) __shim_readlinkat,
-    (shim_fp) __shim_fchmodat,
-    (shim_fp) __shim_faccessat,
-    (shim_fp) __shim_pselect6,
-    (shim_fp) __shim_ppoll,
-    (shim_fp) __shim_unshare,
-    (shim_fp) __shim_set_robust_list,
-    (shim_fp) __shim_get_robust_list,
-    (shim_fp) __shim_splice,
-    (shim_fp) __shim_tee,
-    (shim_fp) __shim_sync_file_range,
-    (shim_fp) __shim_vmsplice,
-    (shim_fp) __shim_move_pages,
-    (shim_fp) __shim_utimensat,
-    (shim_fp) __shim_epoll_pwait,
-    (shim_fp) __shim_signalfd,
-    (shim_fp) __shim_timerfd_create,
-    (shim_fp) __shim_eventfd,
-    (shim_fp) __shim_fallocate,
-    (shim_fp) __shim_timerfd_settime,
-    (shim_fp) __shim_timerfd_gettime,
-    (shim_fp) __shim_accept4,
-    (shim_fp) __shim_signalfd4,
-    (shim_fp) __shim_eventfd2,
-    (shim_fp) __shim_epoll_create1,
-    (shim_fp) __shim_dup3,
-    (shim_fp) __shim_pipe2,
-    (shim_fp) __shim_inotify_init1,
-    (shim_fp) __shim_preadv,
-    (shim_fp) __shim_pwritev,
-    (shim_fp) __shim_rt_tgsigqueueinfo,
-    (shim_fp) __shim_perf_event_open,
-    (shim_fp) __shim_recvmmsg,
-    (shim_fp) __shim_fanotify_init,
-    (shim_fp) __shim_fanotify_mark,
-    (shim_fp) __shim_prlimit64,
-    (shim_fp) __shim_name_to_handle_at,
-    (shim_fp) __shim_open_by_handle_at,
-    (shim_fp) __shim_clock_adjtime,
-    (shim_fp) __shim_syncfs,
-    (shim_fp) __shim_sendmmsg,
-    (shim_fp) __shim_setns,
-    (shim_fp) __shim_getcpu,
+shim_fp shim_table[LIBOS_SYSCALL_BOUND] = {
+    (shim_fp)__shim_read,
+    (shim_fp)__shim_write,
+    (shim_fp)__shim_open,
+    (shim_fp)__shim_close,
+    (shim_fp)__shim_stat,
+    (shim_fp)__shim_fstat,
+    (shim_fp)__shim_lstat,
+    (shim_fp)__shim_poll,
+    (shim_fp)__shim_lseek,
+    (shim_fp)__shim_mmap,
+    (shim_fp)__shim_mprotect,
+    (shim_fp)__shim_munmap,
+    (shim_fp)__shim_brk,
+    (shim_fp)__shim_rt_sigaction,
+    (shim_fp)__shim_rt_sigprocmask,
+    (shim_fp)__shim_rt_sigreturn,
+    (shim_fp)__shim_ioctl,
+    (shim_fp)__shim_pread64,
+    (shim_fp)__shim_pwrite64,
+    (shim_fp)__shim_readv,
+    (shim_fp)__shim_writev,
+    (shim_fp)__shim_access,
+    (shim_fp)__shim_pipe,
+    (shim_fp)__shim_select,
+    (shim_fp)__shim_sched_yield,
+    (shim_fp)__shim_mremap,
+    (shim_fp)__shim_msync,
+    (shim_fp)__shim_mincore,
+    (shim_fp)__shim_madvise,
+    (shim_fp)__shim_shmget,
+    (shim_fp)__shim_shmat,
+    (shim_fp)__shim_shmctl,
+    (shim_fp)__shim_dup,
+    (shim_fp)__shim_dup2,
+    (shim_fp)__shim_pause,
+    (shim_fp)__shim_nanosleep,
+    (shim_fp)__shim_getitimer,
+    (shim_fp)__shim_alarm,
+    (shim_fp)__shim_setitimer,
+    (shim_fp)__shim_getpid,
+    (shim_fp)__shim_sendfile,
+    (shim_fp)__shim_socket,
+    (shim_fp)__shim_connect,
+    (shim_fp)__shim_accept,
+    (shim_fp)__shim_sendto,
+    (shim_fp)__shim_recvfrom,
+    (shim_fp)__shim_sendmsg,
+    (shim_fp)__shim_recvmsg,
+    (shim_fp)__shim_shutdown,
+    (shim_fp)__shim_bind,
+    (shim_fp)__shim_listen,
+    (shim_fp)__shim_getsockname,
+    (shim_fp)__shim_getpeername,
+    (shim_fp)__shim_socketpair,
+    (shim_fp)__shim_setsockopt,
+    (shim_fp)__shim_getsockopt,
+    (shim_fp)__shim_clone,
+    (shim_fp)__shim_fork,
+    (shim_fp)__shim_vfork,
+    (shim_fp)__shim_execve,
+    (shim_fp)__shim_exit,
+    (shim_fp)__shim_wait4,
+    (shim_fp)__shim_kill,
+    (shim_fp)__shim_uname,
+    (shim_fp)__shim_semget,
+    (shim_fp)__shim_semop,
+    (shim_fp)__shim_semctl,
+    (shim_fp)__shim_shmdt,
+    (shim_fp)__shim_msgget,
+    (shim_fp)__shim_msgsnd,
+    (shim_fp)__shim_msgrcv,
+    (shim_fp)__shim_msgctl,
+    (shim_fp)__shim_fcntl,
+    (shim_fp)__shim_flock,
+    (shim_fp)__shim_fsync,
+    (shim_fp)__shim_fdatasync,
+    (shim_fp)__shim_truncate,
+    (shim_fp)__shim_ftruncate,
+    (shim_fp)__shim_getdents,
+    (shim_fp)__shim_getcwd,
+    (shim_fp)__shim_chdir,
+    (shim_fp)__shim_fchdir,
+    (shim_fp)__shim_rename,
+    (shim_fp)__shim_mkdir,
+    (shim_fp)__shim_rmdir,
+    (shim_fp)__shim_creat,
+    (shim_fp)__shim_link,
+    (shim_fp)__shim_unlink,
+    (shim_fp)__shim_symlink,
+    (shim_fp)__shim_readlink,
+    (shim_fp)__shim_chmod,
+    (shim_fp)__shim_fchmod,
+    (shim_fp)__shim_chown,
+    (shim_fp)__shim_fchown,
+    (shim_fp)__shim_lchown,
+    (shim_fp)__shim_umask,
+    (shim_fp)__shim_gettimeofday,
+    (shim_fp)__shim_getrlimit,
+    (shim_fp)__shim_getrusage,
+    (shim_fp)__shim_sysinfo,
+    (shim_fp)__shim_times,
+    (shim_fp)__shim_ptrace,
+    (shim_fp)__shim_getuid,
+    (shim_fp)__shim_syslog,
+    (shim_fp)__shim_getgid,
+    (shim_fp)__shim_setuid,
+    (shim_fp)__shim_setgid,
+    (shim_fp)__shim_geteuid,
+    (shim_fp)__shim_getegid,
+    (shim_fp)__shim_setpgid,
+    (shim_fp)__shim_getppid,
+    (shim_fp)__shim_getpgrp,
+    (shim_fp)__shim_setsid,
+    (shim_fp)__shim_setreuid,
+    (shim_fp)__shim_setregid,
+    (shim_fp)__shim_getgroups,
+    (shim_fp)__shim_setgroups,
+    (shim_fp)__shim_setresuid,
+    (shim_fp)__shim_getresuid,
+    (shim_fp)__shim_setresgid,
+    (shim_fp)__shim_getresgid,
+    (shim_fp)__shim_getpgid,
+    (shim_fp)__shim_setfsuid,
+    (shim_fp)__shim_setfsgid,
+    (shim_fp)__shim_getsid,
+    (shim_fp)__shim_capget,
+    (shim_fp)__shim_capset,
+    (shim_fp)__shim_rt_sigpending,
+    (shim_fp)__shim_rt_sigtimedwait,
+    (shim_fp)__shim_rt_sigqueueinfo,
+    (shim_fp)__shim_rt_sigsuspend,
+    (shim_fp)__shim_sigaltstack,
+    (shim_fp)__shim_utime,
+    (shim_fp)__shim_mknod,
+    (shim_fp)__shim_uselib,
+    (shim_fp)__shim_personality,
+    (shim_fp)__shim_ustat,
+    (shim_fp)__shim_statfs,
+    (shim_fp)__shim_fstatfs,
+    (shim_fp)__shim_sysfs,
+    (shim_fp)__shim_getpriority,
+    (shim_fp)__shim_setpriority,
+    (shim_fp)__shim_sched_setparam,
+    (shim_fp)__shim_sched_getparam,
+    (shim_fp)__shim_sched_setscheduler,
+    (shim_fp)__shim_sched_getscheduler,
+    (shim_fp)__shim_sched_get_priority_max,
+    (shim_fp)__shim_sched_get_priority_min,
+    (shim_fp)__shim_sched_rr_get_interval,
+    (shim_fp)__shim_mlock,
+    (shim_fp)__shim_munlock,
+    (shim_fp)__shim_mlockall,
+    (shim_fp)__shim_munlockall,
+    (shim_fp)__shim_vhangup,
+    (shim_fp)__shim_modify_ldt,
+    (shim_fp)__shim_pivot_root,
+    (shim_fp)__shim__sysctl,
+    (shim_fp)__shim_prctl,
+    (shim_fp)__shim_arch_prctl,
+    (shim_fp)__shim_adjtimex,
+    (shim_fp)__shim_setrlimit,
+    (shim_fp)__shim_chroot,
+    (shim_fp)__shim_sync,
+    (shim_fp)__shim_acct,
+    (shim_fp)__shim_settimeofday,
+    (shim_fp)__shim_mount,
+    (shim_fp)__shim_umount2,
+    (shim_fp)__shim_swapon,
+    (shim_fp)__shim_swapoff,
+    (shim_fp)__shim_reboot,
+    (shim_fp)__shim_sethostname,
+    (shim_fp)__shim_setdomainname,
+    (shim_fp)__shim_iopl,
+    (shim_fp)__shim_ioperm,
+    (shim_fp)__shim_create_module,
+    (shim_fp)__shim_init_module,
+    (shim_fp)__shim_delete_module,
+    (shim_fp)0,  // shim_get_kernel_syms,
+    (shim_fp)__shim_query_module,
+    (shim_fp)__shim_quotactl,
+    (shim_fp)0,  // shim_nfsservctl,
+    (shim_fp)0,  // shim_getpmsg,
+    (shim_fp)0,  // shim_putpmsg,
+    (shim_fp)0,  // shim_afs_syscall,
+    (shim_fp)0,  // shim_tuxcall,
+    (shim_fp)0,  // shim_security,
+    (shim_fp)__shim_gettid,
+    (shim_fp)__shim_readahead,
+    (shim_fp)__shim_setxattr,
+    (shim_fp)__shim_lsetxattr,
+    (shim_fp)__shim_fsetxattr,
+    (shim_fp)__shim_getxattr,
+    (shim_fp)__shim_lgetxattr,
+    (shim_fp)__shim_fgetxattr,
+    (shim_fp)__shim_listxattr,
+    (shim_fp)__shim_llistxattr,
+    (shim_fp)__shim_flistxattr,
+    (shim_fp)__shim_removexattr,
+    (shim_fp)__shim_lremovexattr,
+    (shim_fp)__shim_fremovexattr,
+    (shim_fp)__shim_tkill,
+    (shim_fp)__shim_time,
+    (shim_fp)__shim_futex,
+    (shim_fp)__shim_sched_setaffinity,
+    (shim_fp)__shim_sched_getaffinity,
+    (shim_fp)__shim_set_thread_area,
+    (shim_fp)__shim_io_setup,
+    (shim_fp)__shim_io_destroy,
+    (shim_fp)__shim_io_getevents,
+    (shim_fp)__shim_io_submit,
+    (shim_fp)__shim_io_cancel,
+    (shim_fp)__shim_get_thread_area,
+    (shim_fp)__shim_lookup_dcookie,
+    (shim_fp)__shim_epoll_create,
+    (shim_fp)0,  // shim_epoll_ctl_old,
+    (shim_fp)0,  // shim_epoll_wait_old,
+    (shim_fp)__shim_remap_file_pages,
+    (shim_fp)__shim_getdents64,
+    (shim_fp)__shim_set_tid_address,
+    (shim_fp)__shim_restart_syscall,
+    (shim_fp)__shim_semtimedop,
+    (shim_fp)__shim_fadvise64,
+    (shim_fp)__shim_timer_create,
+    (shim_fp)__shim_timer_settime,
+    (shim_fp)__shim_timer_gettime,
+    (shim_fp)__shim_timer_getoverrun,
+    (shim_fp)__shim_timer_delete,
+    (shim_fp)__shim_clock_settime,
+    (shim_fp)__shim_clock_gettime,
+    (shim_fp)__shim_clock_getres,
+    (shim_fp)__shim_clock_nanosleep,
+    (shim_fp)__shim_exit_group,
+    (shim_fp)__shim_epoll_wait,
+    (shim_fp)__shim_epoll_ctl,
+    (shim_fp)__shim_tgkill,
+    (shim_fp)__shim_utimes,
+    (shim_fp)0,  // shim_vserver,
+    (shim_fp)__shim_mbind,
+    (shim_fp)__shim_set_mempolicy,
+    (shim_fp)__shim_get_mempolicy,
+    (shim_fp)__shim_mq_open,
+    (shim_fp)__shim_mq_unlink,
+    (shim_fp)__shim_mq_timedsend,
+    (shim_fp)__shim_mq_timedreceive,
+    (shim_fp)__shim_mq_notify,
+    (shim_fp)__shim_mq_getsetattr,
+    (shim_fp)0,  // shim_kexec_load,
+    (shim_fp)__shim_waitid,
+    (shim_fp)0,  // shim_add_key,
+    (shim_fp)0,  // shim_request_key,
+    (shim_fp)0,  // shim_keyctl,
+    (shim_fp)__shim_ioprio_set,
+    (shim_fp)__shim_ioprio_get,
+    (shim_fp)__shim_inotify_init,
+    (shim_fp)__shim_inotify_add_watch,
+    (shim_fp)__shim_inotify_rm_watch,
+    (shim_fp)__shim_migrate_pages,
+    (shim_fp)__shim_openat,
+    (shim_fp)__shim_mkdirat,
+    (shim_fp)__shim_mknodat,
+    (shim_fp)__shim_fchownat,
+    (shim_fp)__shim_futimesat,
+    (shim_fp)__shim_newfstatat,
+    (shim_fp)__shim_unlinkat,
+    (shim_fp)__shim_renameat,
+    (shim_fp)__shim_linkat,
+    (shim_fp)__shim_symlinkat,
+    (shim_fp)__shim_readlinkat,
+    (shim_fp)__shim_fchmodat,
+    (shim_fp)__shim_faccessat,
+    (shim_fp)__shim_pselect6,
+    (shim_fp)__shim_ppoll,
+    (shim_fp)__shim_unshare,
+    (shim_fp)__shim_set_robust_list,
+    (shim_fp)__shim_get_robust_list,
+    (shim_fp)__shim_splice,
+    (shim_fp)__shim_tee,
+    (shim_fp)__shim_sync_file_range,
+    (shim_fp)__shim_vmsplice,
+    (shim_fp)__shim_move_pages,
+    (shim_fp)__shim_utimensat,
+    (shim_fp)__shim_epoll_pwait,
+    (shim_fp)__shim_signalfd,
+    (shim_fp)__shim_timerfd_create,
+    (shim_fp)__shim_eventfd,
+    (shim_fp)__shim_fallocate,
+    (shim_fp)__shim_timerfd_settime,
+    (shim_fp)__shim_timerfd_gettime,
+    (shim_fp)__shim_accept4,
+    (shim_fp)__shim_signalfd4,
+    (shim_fp)__shim_eventfd2,
+    (shim_fp)__shim_epoll_create1,
+    (shim_fp)__shim_dup3,
+    (shim_fp)__shim_pipe2,
+    (shim_fp)__shim_inotify_init1,
+    (shim_fp)__shim_preadv,
+    (shim_fp)__shim_pwritev,
+    (shim_fp)__shim_rt_tgsigqueueinfo,
+    (shim_fp)__shim_perf_event_open,
+    (shim_fp)__shim_recvmmsg,
+    (shim_fp)__shim_fanotify_init,
+    (shim_fp)__shim_fanotify_mark,
+    (shim_fp)__shim_prlimit64,
+    (shim_fp)__shim_name_to_handle_at,
+    (shim_fp)__shim_open_by_handle_at,
+    (shim_fp)__shim_clock_adjtime,
+    (shim_fp)__shim_syncfs,
+    (shim_fp)__shim_sendmmsg,
+    (shim_fp)__shim_setns,
+    (shim_fp)__shim_getcpu,
 
-    [LIBOS_SYSCALL_BASE] = (shim_fp) NULL,
+    [LIBOS_SYSCALL_BASE] = (shim_fp)NULL,
 
-    (shim_fp) __shim_msgpersist,
-    (shim_fp) __shim_benchmark_rpc,
-    (shim_fp) __shim_send_rpc,
-    (shim_fp) __shim_recv_rpc,
-    (shim_fp) __shim_checkpoint,
+    (shim_fp)__shim_msgpersist,
+    (shim_fp)__shim_benchmark_rpc,
+    (shim_fp)__shim_send_rpc,
+    (shim_fp)__shim_recv_rpc,
+    (shim_fp)__shim_checkpoint,
 };

+ 31 - 37
LibOS/shim/src/sys/shim_alarm.c

@@ -21,19 +21,18 @@
  */
 
 #include <shim_internal.h>
+#include <shim_signal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
 #include <shim_utils.h>
-#include <shim_signal.h>
 
-static void signal_alarm (IDTYPE target, void * arg)
-{
+static void signal_alarm(IDTYPE target, void* arg) {
     // Kept for API compatibility wtih signal_itimer
     __UNUSED(arg);
 
     debug("alarm goes off, signaling thread %u\n", target);
 
-    struct shim_thread * thread = lookup_thread(target);
+    struct shim_thread* thread = lookup_thread(target);
     if (!thread)
         return;
 
@@ -42,33 +41,32 @@ static void signal_alarm (IDTYPE target, void * arg)
     unlock(&thread->lock);
 }
 
-int shim_do_alarm (unsigned int seconds)
-{
+int shim_do_alarm(unsigned int seconds) {
     uint64_t usecs = 1000000ULL * seconds;
 
     int64_t ret = install_async_event(NULL, usecs, &signal_alarm, NULL);
     if (ret < 0)
         return ret;
 
-    uint64_t usecs_left = (uint64_t) ret;
-    int secs = usecs_left / 1000000ULL;
-    if (usecs_left % 1000000ULL) secs++;
+    uint64_t usecs_left = (uint64_t)ret;
+    int secs            = usecs_left / 1000000ULL;
+    if (usecs_left % 1000000ULL)
+        secs++;
     return secs;
 }
 
 static struct {
-    unsigned long   timeout;
-    unsigned long   reset;
+    unsigned long timeout;
+    unsigned long reset;
 } real_itimer;
 
-void signal_itimer (IDTYPE target, void * arg)
-{
+void signal_itimer(IDTYPE target, void* arg) {
     // XXX: Can we simplify this code or streamline with the other callback?
     __UNUSED(target);
 
     MASTER_LOCK();
 
-    if (real_itimer.timeout != (unsigned long) arg) {
+    if (real_itimer.timeout != (unsigned long)arg) {
         MASTER_UNLOCK();
         return;
     }
@@ -79,12 +77,11 @@ void signal_itimer (IDTYPE target, void * arg)
 }
 
 #ifndef ITIMER_REAL
-# define ITIMER_REAL 0
+#define ITIMER_REAL 0
 #endif
 
-int shim_do_setitimer (int which, struct __kernel_itimerval * value,
-                       struct __kernel_itimerval * ovalue)
-{
+int shim_do_setitimer(int which, struct __kernel_itimerval* value,
+                      struct __kernel_itimerval* ovalue) {
     if (which != ITIMER_REAL)
         return -ENOSYS;
 
@@ -97,19 +94,17 @@ int shim_do_setitimer (int which, struct __kernel_itimerval * value,
 
     unsigned long setup_time = DkSystemTimeQuery();
 
-    unsigned long next_value = value->it_value.tv_sec * 1000000
-                               + value->it_value.tv_usec;
-    unsigned long next_reset = value->it_interval.tv_sec * 1000000
-                               + value->it_interval.tv_usec;
+    unsigned long next_value = value->it_value.tv_sec * 1000000 + value->it_value.tv_usec;
+    unsigned long next_reset = value->it_interval.tv_sec * 1000000 + value->it_interval.tv_usec;
 
     MASTER_LOCK();
 
-    unsigned long current_timeout = real_itimer.timeout > setup_time ?
-                                    real_itimer.timeout - setup_time : 0;
+    unsigned long current_timeout =
+        real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
     unsigned long current_reset = real_itimer.reset;
 
-    int64_t ret = install_async_event(NULL, next_value, &signal_itimer,
-                                       (void *) (setup_time + next_value));
+    int64_t ret =
+        install_async_event(NULL, next_value, &signal_itimer, (void*)(setup_time + next_value));
 
     if (ret < 0) {
         MASTER_UNLOCK();
@@ -117,22 +112,21 @@ int shim_do_setitimer (int which, struct __kernel_itimerval * value,
     }
 
     real_itimer.timeout = setup_time + next_value;
-    real_itimer.reset = next_reset;
+    real_itimer.reset   = next_reset;
 
     MASTER_UNLOCK();
 
     if (ovalue) {
-        ovalue->it_interval.tv_sec = current_reset / 1000000;
+        ovalue->it_interval.tv_sec  = current_reset / 1000000;
         ovalue->it_interval.tv_usec = current_reset % 1000000;
-        ovalue->it_value.tv_sec = current_timeout / 1000000;
-        ovalue->it_value.tv_usec = current_timeout % 1000000;
+        ovalue->it_value.tv_sec     = current_timeout / 1000000;
+        ovalue->it_value.tv_usec    = current_timeout % 1000000;
     }
 
     return 0;
 }
 
-int shim_do_getitimer (int which, struct __kernel_itimerval * value)
-{
+int shim_do_getitimer(int which, struct __kernel_itimerval* value) {
     if (which != ITIMER_REAL)
         return -ENOSYS;
 
@@ -144,14 +138,14 @@ int shim_do_getitimer (int which, struct __kernel_itimerval * value)
     unsigned long setup_time = DkSystemTimeQuery();
 
     MASTER_LOCK();
-    unsigned long current_timeout = real_itimer.timeout > setup_time ?
-                                    real_itimer.timeout - setup_time : 0;
+    unsigned long current_timeout =
+        real_itimer.timeout > setup_time ? real_itimer.timeout - setup_time : 0;
     unsigned long current_reset = real_itimer.reset;
     MASTER_UNLOCK();
 
-    value->it_interval.tv_sec = current_reset / 1000000;
+    value->it_interval.tv_sec  = current_reset / 1000000;
     value->it_interval.tv_usec = current_reset % 1000000;
-    value->it_value.tv_sec = current_timeout / 1000000;
-    value->it_value.tv_usec = current_timeout % 1000000;
+    value->it_value.tv_sec     = current_timeout / 1000000;
+    value->it_value.tv_usec    = current_timeout % 1000000;
     return 0;
 }

+ 9 - 14
LibOS/shim/src/sys/shim_benchmark.c

@@ -21,23 +21,20 @@
  * (These system calls are added for benchmarking purpose.)
  */
 
+#include <errno.h>
 #include <shim_internal.h>
-#include <shim_table.h>
 #include <shim_ipc.h>
-#include <shim_unistd.h>
 #include <shim_profile.h>
+#include <shim_table.h>
+#include <shim_unistd.h>
 
-#include <errno.h>
-
-int get_pid_port (IDTYPE pid, IDTYPE * dest, struct shim_ipc_port ** port);
+int get_pid_port(IDTYPE pid, IDTYPE* dest, struct shim_ipc_port** port);
 
-int shim_do_benchmark_rpc (pid_t pid, int times, const void * buf,
-                           size_t size)
-{
+int shim_do_benchmark_rpc(pid_t pid, int times, const void* buf, size_t size) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
     int ret = 0;
     IDTYPE dest;
-    struct shim_ipc_port * port = NULL;
+    struct shim_ipc_port* port = NULL;
 
     if ((ret = get_pid_port(pid, &dest, &port)) < 0)
         return ret;
@@ -47,15 +44,13 @@ int shim_do_benchmark_rpc (pid_t pid, int times, const void * buf,
     return ret;
 }
 
-size_t shim_do_send_rpc (pid_t pid, const void * buf, size_t size)
-{
+size_t shim_do_send_rpc(pid_t pid, const void* buf, size_t size) {
     return ipc_pid_sendrpc_send(pid, get_cur_tid(), buf, size);
 }
 
-int get_rpc_msg (IDTYPE * sender, void * buf, int len);
+int get_rpc_msg(IDTYPE* sender, void* buf, int len);
 
-size_t shim_do_recv_rpc (pid_t * pid, void * buf, size_t size)
-{
+size_t shim_do_recv_rpc(pid_t* pid, void* buf, size_t size) {
     IDTYPE sender;
     int ret = get_rpc_msg(&sender, buf, size);
     if (ret < 0)

+ 17 - 22
LibOS/shim/src/sys/shim_dup.c

@@ -20,24 +20,21 @@
  * Implementation of system call "dup", "dup2" and "dup3".
  */
 
+#include <errno.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
-int shim_do_dup (int fd)
-{
-    struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
-    int flags = 0;
+int shim_do_dup(int fd) {
+    struct shim_handle_map* handle_map = get_cur_handle_map(NULL);
+    int flags                          = 0;
 
-    struct shim_handle * hdl = get_fd_handle(fd, &flags, handle_map);
+    struct shim_handle* hdl = get_fd_handle(fd, &flags, handle_map);
     if (!hdl)
         return -EBADF;
 
@@ -46,15 +43,14 @@ int shim_do_dup (int fd)
     return vfd < 0 ? -EMFILE : vfd;
 }
 
-int shim_do_dup2 (int oldfd, int newfd)
-{
-    struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
+int shim_do_dup2(int oldfd, int newfd) {
+    struct shim_handle_map* handle_map = get_cur_handle_map(NULL);
 
-    struct shim_handle * hdl = get_fd_handle(oldfd, NULL, handle_map);
+    struct shim_handle* hdl = get_fd_handle(oldfd, NULL, handle_map);
     if (!hdl)
         return -EBADF;
 
-    struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
+    struct shim_handle* new_hdl = detach_fd_handle(newfd, NULL, handle_map);
 
     if (new_hdl)
         put_handle(new_hdl);
@@ -64,14 +60,13 @@ int shim_do_dup2 (int oldfd, int newfd)
     return vfd < 0 ? -EMFILE : vfd;
 }
 
-int shim_do_dup3 (int oldfd, int newfd, int flags)
-{
-    struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
-    struct shim_handle * hdl = get_fd_handle(oldfd, NULL, handle_map);
+int shim_do_dup3(int oldfd, int newfd, int flags) {
+    struct shim_handle_map* handle_map = get_cur_handle_map(NULL);
+    struct shim_handle* hdl            = get_fd_handle(oldfd, NULL, handle_map);
     if (!hdl)
         return -EBADF;
 
-    struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
+    struct shim_handle* new_hdl = detach_fd_handle(newfd, NULL, handle_map);
 
     if (new_hdl)
         put_handle(new_hdl);

+ 115 - 136
LibOS/shim/src/sys/shim_epoll.c

@@ -21,103 +21,96 @@
  * and "epoll_wait".
  */
 
+#include <errno.h>
+#include <linux/eventpoll.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-#include <shim_checkpoint.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
-#include <linux/eventpoll.h>
 
 /* Avoid duplicated definitions */
 #ifndef EPOLLIN
 
-#define EPOLLIN         0x001
-#define EPOLLPRI        0x002
-#define EPOLLOUT        0x004
-#define EPOLLRDNORM     0x040
-#define EPOLLRDBAND     0x080
-#define EPOLLWRNORM     0x100
-#define EPOLLERBAND     0x200
-#define EPOLLERR        0x008
-#define EPOLLHUP        0x010
-#define EPOLLRDHUP      0x2000
+#define EPOLLIN     0x001
+#define EPOLLPRI    0x002
+#define EPOLLOUT    0x004
+#define EPOLLRDNORM 0x040
+#define EPOLLRDBAND 0x080
+#define EPOLLWRNORM 0x100
+#define EPOLLERBAND 0x200
+#define EPOLLERR    0x008
+#define EPOLLHUP    0x010
+#define EPOLLRDHUP  0x2000
 
 #endif
 
-#define MAX_EPOLL_FDS       1024
+#define MAX_EPOLL_FDS 1024
 
 struct shim_mount epoll_builtin_fs;
 
 /* shim_epoll_fds are linked as a list (by the list field),
  * hanging off of a shim_epoll_handle (by the fds field) */
 struct shim_epoll_fd {
-    FDTYPE                      fd;
-    unsigned int                events;
-    __u64                       data;
-    unsigned int                revents;
-    struct shim_handle *        handle;
-    struct shim_handle *        epoll;
-    PAL_HANDLE                  pal_handle;
-    LIST_TYPE(shim_epoll_fd)    list;
-    LIST_TYPE(shim_epoll_fd)    back;
+    FDTYPE fd;
+    unsigned int events;
+    __u64 data;
+    unsigned int revents;
+    struct shim_handle* handle;
+    struct shim_handle* epoll;
+    PAL_HANDLE pal_handle;
+    LIST_TYPE(shim_epoll_fd) list;
+    LIST_TYPE(shim_epoll_fd) back;
 };
 
-int shim_do_epoll_create1 (int flags)
-{
+int shim_do_epoll_create1(int flags) {
     if ((flags & ~EPOLL_CLOEXEC))
         return -EINVAL;
 
-    struct shim_handle * hdl = get_new_handle();
+    struct shim_handle* hdl = get_new_handle();
     if (!hdl)
         return -ENOMEM;
 
-    struct shim_epoll_handle * epoll = &hdl->info.epoll;
+    struct shim_epoll_handle* epoll = &hdl->info.epoll;
 
     hdl->type = TYPE_EPOLL;
     set_handle_fs(hdl, &epoll_builtin_fs);
-    epoll->maxfds = MAX_EPOLL_FDS;
-    epoll->nfds = 0;
-    epoll->pal_fds = malloc(sizeof(FDTYPE) * MAX_EPOLL_FDS);
+    epoll->maxfds      = MAX_EPOLL_FDS;
+    epoll->nfds        = 0;
+    epoll->pal_fds     = malloc(sizeof(FDTYPE) * MAX_EPOLL_FDS);
     epoll->pal_handles = malloc(sizeof(PAL_HANDLE) * MAX_EPOLL_FDS);
     create_event(&epoll->event);
     INIT_LISTP(&epoll->fds);
 
-    int vfd = set_new_fd_handle(hdl, (flags & EPOLL_CLOEXEC) ? FD_CLOEXEC : 0,
-                                NULL);
+    int vfd = set_new_fd_handle(hdl, (flags & EPOLL_CLOEXEC) ? FD_CLOEXEC : 0, NULL);
     put_handle(hdl);
     return vfd;
 }
 
 /* the 'size' argument of epoll_create is not used */
-int shim_do_epoll_create (int size)
-{
+int shim_do_epoll_create(int size) {
     if (size < 0)
         return -EINVAL;
 
     return shim_do_epoll_create1(0);
 }
 
-static void update_epoll (struct shim_epoll_handle * epoll)
-{
-    struct shim_epoll_fd * tmp;
-    int npals = 0;
+static void update_epoll(struct shim_epoll_handle* epoll) {
+    struct shim_epoll_fd* tmp;
+    int npals    = 0;
     epoll->nread = 0;
 
     LISTP_FOR_EACH_ENTRY(tmp, &epoll->fds, list) {
         if (!tmp->pal_handle)
             continue;
 
-        debug("found handle %p (pal handle %p) from epoll handle %p\n",
-              tmp->handle, tmp->pal_handle, epoll);
+        debug("found handle %p (pal handle %p) from epoll handle %p\n", tmp->handle,
+              tmp->pal_handle, epoll);
 
-        epoll->pal_fds[npals] = tmp->fd;
+        epoll->pal_fds[npals]     = tmp->fd;
         epoll->pal_handles[npals] = tmp->pal_handle;
         npals++;
         if (tmp->handle->acc_mode & MAY_READ)
@@ -130,8 +123,7 @@ static void update_epoll (struct shim_epoll_handle * epoll)
         set_event(&epoll->event, epoll->nwaiters);
 }
 
-int delete_from_epoll_handles (struct shim_handle * handle)
-{
+int delete_from_epoll_handles(struct shim_handle* handle) {
     while (1) {
         lock(&handle->lock);
 
@@ -140,18 +132,17 @@ int delete_from_epoll_handles (struct shim_handle * handle)
             break;
         }
 
-        struct shim_epoll_fd * epoll_fd = LISTP_FIRST_ENTRY(&handle->epolls,
-                                 struct shim_epoll_fd, back);
+        struct shim_epoll_fd* epoll_fd =
+            LISTP_FIRST_ENTRY(&handle->epolls, struct shim_epoll_fd, back);
 
         LISTP_DEL(epoll_fd, &handle->epolls, back);
         unlock(&handle->lock);
         put_handle(handle);
 
-        struct shim_handle * epoll_hdl = epoll_fd->epoll;
-        struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
+        struct shim_handle* epoll_hdl   = epoll_fd->epoll;
+        struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
 
-        debug("delete handle %p from epoll handle %p\n", handle,
-              &epoll_hdl->info.epoll);
+        debug("delete handle %p from epoll handle %p\n", handle, &epoll_hdl->info.epoll);
 
         lock(&epoll_hdl->lock);
 
@@ -167,13 +158,11 @@ int delete_from_epoll_handles (struct shim_handle * handle)
     return 0;
 }
 
-int shim_do_epoll_ctl (int epfd, int op, int fd,
-                       struct __kernel_epoll_event * event)
-{
-    struct shim_thread * cur = get_cur_thread();
-    int ret = 0;
+int shim_do_epoll_ctl(int epfd, int op, int fd, struct __kernel_epoll_event* event) {
+    struct shim_thread* cur = get_cur_thread();
+    int ret                 = 0;
 
-    struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, cur->handle_map);
+    struct shim_handle* epoll_hdl = get_fd_handle(epfd, NULL, cur->handle_map);
     if (!epoll_hdl)
         return -EBADF;
     if (epoll_hdl->type != TYPE_EPOLL) {
@@ -181,26 +170,26 @@ int shim_do_epoll_ctl (int epfd, int op, int fd,
         return -EINVAL;
     }
 
-    struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
-    struct shim_epoll_fd * epoll_fd;
+    struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
+    struct shim_epoll_fd* epoll_fd;
 
     lock(&epoll_hdl->lock);
 
     switch (op) {
         case EPOLL_CTL_ADD: {
-            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
+            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
                 if (epoll_fd->fd == fd) {
                     ret = -EEXIST;
                     goto out;
                 }
+            }
 
-            struct shim_handle * hdl = get_fd_handle(fd, NULL, cur->handle_map);
+            struct shim_handle* hdl = get_fd_handle(fd, NULL, cur->handle_map);
             if (!hdl) {
                 ret = -EBADF;
                 goto out;
             }
-            if ((hdl->type != TYPE_PIPE && hdl->type != TYPE_SOCK) ||
-                !hdl->pal_handle) {
+            if ((hdl->type != TYPE_PIPE && hdl->type != TYPE_SOCK) || !hdl->pal_handle) {
                 ret = -EPERM;
                 put_handle(hdl);
                 goto out;
@@ -213,13 +202,13 @@ int shim_do_epoll_ctl (int epfd, int op, int fd,
 
             debug("add handle %p to epoll handle %p\n", hdl, epoll);
 
-            epoll_fd = malloc(sizeof(struct shim_epoll_fd));
-            epoll_fd->fd = fd;
-            epoll_fd->events = event->events;
-            epoll_fd->data = event->data;
-            epoll_fd->revents = 0;
-            epoll_fd->handle = hdl;
-            epoll_fd->epoll = epoll_hdl;
+            epoll_fd             = malloc(sizeof(struct shim_epoll_fd));
+            epoll_fd->fd         = fd;
+            epoll_fd->events     = event->events;
+            epoll_fd->data       = event->data;
+            epoll_fd->revents    = 0;
+            epoll_fd->handle     = hdl;
+            epoll_fd->epoll      = epoll_hdl;
             epoll_fd->pal_handle = hdl->pal_handle;
 
             /* Register the epoll handle */
@@ -236,29 +225,29 @@ int shim_do_epoll_ctl (int epfd, int op, int fd,
         }
 
         case EPOLL_CTL_MOD: {
-            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
+            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
                 if (epoll_fd->fd == fd) {
                     epoll_fd->events = event->events;
-                    epoll_fd->data = event->data;
+                    epoll_fd->data   = event->data;
                     goto update;
                 }
+            }
 
             ret = -ENOENT;
             goto out;
         }
 
         case EPOLL_CTL_DEL: {
-            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
+            LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
                 if (epoll_fd->fd == fd) {
-                    struct shim_handle * hdl = epoll_fd->handle;
+                    struct shim_handle* hdl = epoll_fd->handle;
 
                     /* Unregister the epoll handle */
                     lock(&hdl->lock);
                     LISTP_DEL(epoll_fd, &hdl->epolls, back);
                     unlock(&hdl->lock);
 
-                    debug("delete handle %p from epoll handle %p\n",
-                          hdl, epoll);
+                    debug("delete handle %p from epoll handle %p\n", hdl, epoll);
 
                     put_handle(epoll_hdl);
                     put_handle(hdl);
@@ -268,6 +257,7 @@ int shim_do_epoll_ctl (int epfd, int op, int fd,
                     free(epoll_fd);
                     goto update;
                 }
+            }
 
             ret = -ENOENT;
             goto out;
@@ -286,11 +276,10 @@ out:
     return ret;
 }
 
-int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
-                        int maxevents, int timeout_ms)
-{
-    int ret = 0;
-    struct shim_handle * epoll_hdl = get_fd_handle(epfd, NULL, NULL);
+int shim_do_epoll_wait(int epfd, struct __kernel_epoll_event* events, int maxevents,
+                       int timeout_ms) {
+    int ret                       = 0;
+    struct shim_handle* epoll_hdl = get_fd_handle(epfd, NULL, NULL);
     if (!epoll_hdl)
         return -EBADF;
     if (epoll_hdl->type != TYPE_EPOLL) {
@@ -298,8 +287,8 @@ int shim_do_epoll_wait (int epfd, struct __kernel_epoll_event * events,
         return -EINVAL;
     }
 
-    struct shim_epoll_handle * epoll = &epoll_hdl->info.epoll;
-    struct shim_epoll_fd * epoll_fd;
+    struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
+    struct shim_epoll_fd* epoll_fd;
     int nevents = 0;
     int npals, nread;
     bool need_update = false;
@@ -309,8 +298,8 @@ retry:
     if (!(npals = epoll->npals))
         goto reply;
 
-    PAL_HANDLE * pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
-    FDTYPE * fds = __alloca(sizeof(FDTYPE) * npals);
+    PAL_HANDLE* pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
+    FDTYPE* fds             = __alloca(sizeof(FDTYPE) * npals);
     memcpy(fds, epoll->pal_fds, sizeof(FDTYPE) * npals);
     memcpy(pal_handles, epoll->pal_handles, sizeof(PAL_HANDLE) * npals);
     pal_handles[npals] = epoll->event.event;
@@ -320,9 +309,9 @@ retry:
 
     unlock(&epoll_hdl->lock);
 
-    PAL_NUM pal_timeout = timeout_ms == -1 ? NO_TIMEOUT : (PAL_NUM) timeout_ms * 1000;
-    PAL_HANDLE polled = DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles,
-                                         nread ? pal_timeout : 0);
+    PAL_NUM pal_timeout = timeout_ms == -1 ? NO_TIMEOUT : (PAL_NUM)timeout_ms * 1000;
+    PAL_HANDLE polled =
+        DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles, nread ? pal_timeout : 0);
 
     lock(&epoll_hdl->lock);
 
@@ -341,16 +330,14 @@ retry:
     if (!DkStreamAttributesQueryByHandle(polled, &attr))
         goto reply;
 
-    LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list)
+    LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
         if (polled == epoll_fd->pal_handle) {
-
-            debug("epoll: fd %d (handle %p) polled\n", epoll_fd->fd,
-                  epoll_fd->handle);
+            debug("epoll: fd %d (handle %p) polled\n", epoll_fd->fd, epoll_fd->handle);
 
             if (attr.disconnected) {
-                epoll_fd->revents |= EPOLLERR|EPOLLHUP|EPOLLRDHUP;
+                epoll_fd->revents |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
                 epoll_fd->pal_handle = NULL;
-                need_update = true;
+                need_update          = true;
             }
             if (attr.readable)
                 epoll_fd->revents |= EPOLLIN;
@@ -358,20 +345,19 @@ retry:
                 epoll_fd->revents |= EPOLLOUT;
             break;
         }
+    }
 
 reply:
     LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
         if (nevents == maxevents)
             break;
 
-        if ((epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents) {
-            events[nevents].events =
-                    (epoll_fd->events|EPOLLERR|EPOLLHUP) & epoll_fd->revents;
-            events[nevents].data = epoll_fd->data;
+        if ((epoll_fd->events | EPOLLERR | EPOLLHUP) & epoll_fd->revents) {
+            events[nevents].events = (epoll_fd->events | EPOLLERR | EPOLLHUP) & epoll_fd->revents;
+            events[nevents].data   = epoll_fd->data;
             nevents++;
             epoll_fd->revents &= ~epoll_fd->events;
         }
-
     }
 
     if (need_update)
@@ -383,36 +369,34 @@ reply:
     return ret;
 }
 
-int shim_do_epoll_pwait (int epfd, struct __kernel_epoll_event * events,
-                         int maxevents, int timeout_ms, const __sigset_t * sigmask,
-                         size_t sigsetsize)
-{
+int shim_do_epoll_pwait(int epfd, struct __kernel_epoll_event* events, int maxevents,
+                        int timeout_ms, const __sigset_t* sigmask, size_t sigsetsize) {
     __UNUSED(sigmask);
     __UNUSED(sigsetsize);
-    int ret = shim_do_epoll_wait (epfd, events, maxevents, timeout_ms);
+    int ret = shim_do_epoll_wait(epfd, events, maxevents, timeout_ms);
     return ret;
 }
 
-static int epoll_close (struct shim_handle * hdl)
-{
+static int epoll_close(struct shim_handle* hdl) {
     __UNUSED(hdl);
     return 0;
 }
 
 struct shim_fs_ops epoll_fs_ops = {
-        .close    = &epoll_close,
-    };
+    .close = &epoll_close,
+};
 
-struct shim_mount epoll_builtin_fs = { .type = "epoll",
-                                       .fs_ops = &epoll_fs_ops, };
+struct shim_mount epoll_builtin_fs = {
+    .type   = "epoll",
+    .fs_ops = &epoll_fs_ops,
+};
 
-BEGIN_CP_FUNC(epoll_fd)
-{
+BEGIN_CP_FUNC(epoll_fd) {
     assert(size == sizeof(LISTP_TYPE(shim_epoll_fd)));
 
-    LISTP_TYPE(shim_epoll_fd) * old_list = (LISTP_TYPE(shim_epoll_fd) *) obj;
-    LISTP_TYPE(shim_epoll_fd) * new_list = (LISTP_TYPE(shim_epoll_fd) *) objp;
-    struct shim_epoll_fd * epoll_fd;
+    LISTP_TYPE(shim_epoll_fd)* old_list = (LISTP_TYPE(shim_epoll_fd)*)obj;
+    LISTP_TYPE(shim_epoll_fd)* new_list = (LISTP_TYPE(shim_epoll_fd)*)objp;
+    struct shim_epoll_fd* epoll_fd;
 
     debug("checkpoint epoll: %p -> %p (base = 0x%08lx)\n", old_list, new_list, base);
 
@@ -421,13 +405,12 @@ BEGIN_CP_FUNC(epoll_fd)
     LISTP_FOR_EACH_ENTRY(epoll_fd, old_list, list) {
         ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_epoll_fd));
 
-        struct shim_epoll_fd * new_epoll_fd =
-                    (struct shim_epoll_fd *) (base + off);
+        struct shim_epoll_fd* new_epoll_fd = (struct shim_epoll_fd*)(base + off);
 
-        new_epoll_fd->fd      = epoll_fd->fd;
-        new_epoll_fd->events  = epoll_fd->events;
-        new_epoll_fd->data    = epoll_fd->data;
-        new_epoll_fd->revents = epoll_fd->revents;
+        new_epoll_fd->fd         = epoll_fd->fd;
+        new_epoll_fd->events     = epoll_fd->events;
+        new_epoll_fd->data       = epoll_fd->data;
+        new_epoll_fd->revents    = epoll_fd->revents;
         new_epoll_fd->pal_handle = NULL;
 
         LISTP_ADD(new_epoll_fd, new_list, list);
@@ -435,29 +418,25 @@ BEGIN_CP_FUNC(epoll_fd)
         DO_CP(handle, epoll_fd->handle, &new_epoll_fd->handle);
     }
 
-    ADD_CP_FUNC_ENTRY((ptr_t) objp - base);
+    ADD_CP_FUNC_ENTRY((ptr_t)objp - base);
 }
 END_CP_FUNC(epoll_fd)
 
-BEGIN_RS_FUNC(epoll_fd)
-{
+BEGIN_RS_FUNC(epoll_fd) {
     __UNUSED(offset);
-    LISTP_TYPE(shim_epoll_fd) * list = (void *) (base + GET_CP_FUNC_ENTRY());
-    struct shim_epoll_fd * epoll_fd;
+    LISTP_TYPE(shim_epoll_fd)* list = (void*)(base + GET_CP_FUNC_ENTRY());
+    struct shim_epoll_fd* epoll_fd;
 
     CP_REBASE(*list);
 
     LISTP_FOR_EACH_ENTRY(epoll_fd, list, list) {
-
         CP_REBASE(epoll_fd->handle);
         CP_REBASE(epoll_fd->back);
         epoll_fd->pal_handle = epoll_fd->handle->pal_handle;
         CP_REBASE(epoll_fd->list);
 
-        DEBUG_RS("fd=%d,path=%s,type=%s,uri=%s",
-                 epoll_fd->fd, qstrgetstr(&epoll_fd->handle->path),
-                 epoll_fd->handle->fs_type,
-                 qstrgetstr(&epoll_fd->handle->uri));
+        DEBUG_RS("fd=%d,path=%s,type=%s,uri=%s", epoll_fd->fd, qstrgetstr(&epoll_fd->handle->path),
+                 epoll_fd->handle->fs_type, qstrgetstr(&epoll_fd->handle->uri));
     }
 }
 END_RS_FUNC(epoll_fd)

+ 15 - 24
LibOS/shim/src/sys/shim_fcntl.c

@@ -20,27 +20,23 @@
  * Implementation of system call "fcntl".
  */
 
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
-#include <shim_utils.h>
 #include <shim_table.h>
-#include <shim_handle.h>
 #include <shim_thread.h>
-#include <shim_fs.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
-#include <linux/fcntl.h>
+#include <shim_utils.h>
 
-int shim_do_fcntl (int fd, int cmd, unsigned long arg)
-{
-    struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
+int shim_do_fcntl(int fd, int cmd, unsigned long arg) {
+    struct shim_handle_map* handle_map = get_cur_handle_map(NULL);
     int flags;
     int ret = -ENOSYS;
 
-    struct shim_handle * hdl = get_fd_handle(fd, &flags, handle_map);
+    struct shim_handle* hdl = get_fd_handle(fd, &flags, handle_map);
     if (!hdl)
         return -EBADF;
 
@@ -54,7 +50,7 @@ int shim_do_fcntl (int fd, int cmd, unsigned long arg)
         case F_DUPFD: {
             int vfd = arg;
 
-            while(1) {
+            while (1) {
                 if (set_new_fd_handle_by_fd(vfd, hdl, flags, handle_map) == vfd)
                     break;
                 vfd++;
@@ -75,7 +71,7 @@ int shim_do_fcntl (int fd, int cmd, unsigned long arg)
             int vfd = arg;
             flags |= FD_CLOEXEC;
 
-            while(1) {
+            while (1) {
                 if (set_new_fd_handle_by_fd(vfd, hdl, flags, handle_map) == vfd)
                     break;
                 vfd++;
@@ -121,7 +117,6 @@ int shim_do_fcntl (int fd, int cmd, unsigned long arg)
          * F_GETFL (void)
          *   Read the file status flags; arg is ignored.
          */
-
         case F_GETFL:
             lock(&hdl->lock);
             flags = hdl->flags;
@@ -136,16 +131,12 @@ int shim_do_fcntl (int fd, int cmd, unsigned long arg)
          *   Linux this command can only change the O_APPEND, O_DIRECT,
          *   O_NOATIME, and O_NONBLOCK flags.
          */
-
-#define FCNTL_SETFL_MASK (O_APPEND|O_NONBLOCK)
-
+#define FCNTL_SETFL_MASK (O_APPEND | O_NONBLOCK)
         case F_SETFL:
             lock(&hdl->lock);
-            if (hdl->fs && hdl->fs->fs_ops &&
-                hdl->fs->fs_ops->setflags)
+            if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->setflags)
                 hdl->fs->fs_ops->setflags(hdl, arg & FCNTL_SETFL_MASK);
-            hdl->flags = (hdl->flags & ~FCNTL_SETFL_MASK) |
-                         (arg & FCNTL_SETFL_MASK);
+            hdl->flags = (hdl->flags & ~FCNTL_SETFL_MASK) | (arg & FCNTL_SETFL_MASK);
             unlock(&hdl->lock);
             ret = 0;
             break;

+ 138 - 118
LibOS/shim/src/sys/shim_futex.c

@@ -21,21 +21,19 @@
  * "get_robust_list".
  */
 
+#include <asm/prctl.h>
+#include <errno.h>
+#include <linux/futex.h>
+#include <list.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
 #include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_checkpoint.h>
 #include <shim_utils.h>
-
-#include <pal.h>
-#include <pal_error.h>
-#include <list.h>
-
-#include <sys/syscall.h>
 #include <sys/mman.h>
-#include <asm/prctl.h>
-#include <linux/futex.h>
-#include <errno.h>
+#include <sys/syscall.h>
 
 #define FUTEX_MIN_VALUE 0
 #define FUTEX_MAX_VALUE 255
@@ -43,7 +41,7 @@
 /* futex_waiters are linked off of shim_futex_handle by the waiters
  * listp */
 struct futex_waiter {
-    struct shim_thread * thread;
+    struct shim_thread* thread;
     uint32_t bitset;
     LIST_TYPE(futex_waiter) list;
 };
@@ -53,27 +51,29 @@ DEFINE_LISTP(shim_futex_handle);
 static LISTP_TYPE(shim_futex_handle) futex_list = LISTP_INIT;
 static struct shim_lock futex_list_lock;
 
-int shim_do_futex (int * uaddr, int op, int val, void * utime,
-                   int * uaddr2, int val3)
-{
-    struct shim_futex_handle * tmp = NULL, * futex = NULL, * futex2 = NULL;
-    struct shim_handle * hdl = NULL, * hdl2 = NULL;
+int shim_do_futex(int* uaddr, int op, int val, void* utime, int* uaddr2, int val3) {
+    struct shim_futex_handle* tmp = NULL;
+    struct shim_futex_handle* futex = NULL;
+    struct shim_futex_handle* futex2 = NULL;
+    struct shim_handle* hdl = NULL;
+    struct shim_handle* hdl2 = NULL;
     uint32_t futex_op = (op & FUTEX_CMD_MASK);
 
     uint32_t val2 = 0;
-    int ret = 0;
+    int ret       = 0;
 
-    if (!uaddr || ((uintptr_t) uaddr % sizeof(unsigned int)))
+    if (!uaddr || ((uintptr_t)uaddr % sizeof(unsigned int)))
         return -EINVAL;
 
     create_lock_runtime(&futex_list_lock);
     lock(&futex_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list)
+    LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list) {
         if (tmp->uaddr == uaddr) {
             futex = tmp;
             break;
         }
+    }
 
     if (futex) {
         hdl = container_of(futex, struct shim_handle, info.futex);
@@ -84,8 +84,8 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
             return -ENOMEM;
         }
 
-        hdl->type = TYPE_FUTEX;
-        futex = &hdl->info.futex;
+        hdl->type    = TYPE_FUTEX;
+        futex        = &hdl->info.futex;
         futex->uaddr = uaddr;
         get_handle(hdl);
         INIT_LISTP(&futex->waiters);
@@ -93,13 +93,13 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
         LISTP_ADD_TAIL(futex, &futex_list, list);
     }
 
-    if (futex_op == FUTEX_WAKE_OP || futex_op == FUTEX_REQUEUE ||
-            futex_op == FUTEX_CMP_REQUEUE) {
-        LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list)
+    if (futex_op == FUTEX_WAKE_OP || futex_op == FUTEX_REQUEUE || futex_op == FUTEX_CMP_REQUEUE) {
+        LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list) {
             if (tmp->uaddr == uaddr2) {
                 futex2 = tmp;
                 break;
             }
+        }
 
         if (futex2) {
             hdl2 = container_of(futex2, struct shim_handle, info.futex);
@@ -110,8 +110,8 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
                 return -ENOMEM;
             }
 
-            hdl2->type = TYPE_FUTEX;
-            futex2 = &hdl2->info.futex;
+            hdl2->type    = TYPE_FUTEX;
+            futex2        = &hdl2->info.futex;
             futex2->uaddr = uaddr2;
             get_handle(hdl2);
             INIT_LISTP(&futex2->waiters);
@@ -119,7 +119,7 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
             LISTP_ADD_TAIL(futex2, &futex_list, list);
         }
 
-        val2 = (uint32_t)(uint64_t) utime;
+        val2 = (uint32_t)(uint64_t)utime;
     }
 
     unlock(&futex_list_lock);
@@ -129,7 +129,7 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
     switch (futex_op) {
         case FUTEX_WAIT_BITSET:
             if (utime && timeout_us == NO_TIMEOUT) {
-                struct timespec *ts = (struct timespec*) utime;
+                struct timespec* ts = (struct timespec*)utime;
                 // Round to microsecs
                 timeout_us = (ts->tv_sec * 1000000) + (ts->tv_nsec / 1000);
 
@@ -153,68 +153,67 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
          * FUTEX_WAIT_BITSET with val3 specified as
          * FUTEX_BITSET_MATCH_ANY. */
 
-            /* FALLTHROUGH */
+        /* FALLTHROUGH */
         case FUTEX_WAIT:
             if (utime && timeout_us == NO_TIMEOUT) {
-                struct timespec *ts = (struct timespec*) utime;
+                struct timespec* ts = (struct timespec*)utime;
                 // Round to microsecs
                 timeout_us = (ts->tv_sec * 1000000) + (ts->tv_nsec / 1000);
             }
 
-        {
-            uint32_t bitset = (futex_op == FUTEX_WAIT_BITSET) ? (uint32_t) val3 :
-                              0xffffffff;
+            {
+                uint32_t bitset = (futex_op == FUTEX_WAIT_BITSET) ? (uint32_t)val3 : 0xffffffff;
 
-            debug("FUTEX_WAIT: %p (val = %d) vs %d mask = %08x, timeout ptr %p\n",
-                  uaddr, *uaddr, val, bitset, utime);
+                debug("FUTEX_WAIT: %p (val = %d) vs %d mask = %08x, timeout ptr %p\n", uaddr,
+                      *uaddr, val, bitset, utime);
 
-            if (*uaddr != val) {
-                ret = -EAGAIN;
+                if (*uaddr != val) {
+                    ret = -EAGAIN;
+                    break;
+                }
+
+                struct futex_waiter waiter;
+                thread_setwait(&waiter.thread, NULL);
+                INIT_LIST_HEAD(&waiter, list);
+                waiter.bitset = bitset;
+                LISTP_ADD_TAIL(&waiter, &futex->waiters, list);
+
+                unlock(&hdl->lock);
+                ret = thread_sleep(timeout_us);
+                /* DEP 1/28/17: Should return ETIMEDOUT, not EAGAIN, on timeout. */
+                if (ret == -EAGAIN)
+                    ret = -ETIMEDOUT;
+                if (ret == -ETIMEDOUT)
+                    LISTP_DEL(&waiter, &futex->waiters, list);
+                lock(&hdl->lock);
+                /* Chia-Che 10/17/17: FUTEX_WAKE should remove the waiter
+                 * from the list; if not, we should remove it now. */
+                if (!LIST_EMPTY(&waiter, list))
+                    LISTP_DEL(&waiter, &futex->waiters, list);
                 break;
             }
 
-            struct futex_waiter waiter;
-            thread_setwait(&waiter.thread, NULL);
-            INIT_LIST_HEAD(&waiter, list);
-            waiter.bitset = bitset;
-            LISTP_ADD_TAIL(&waiter, &futex->waiters, list);
-
-            unlock(&hdl->lock);
-            ret = thread_sleep(timeout_us);
-            /* DEP 1/28/17: Should return ETIMEDOUT, not EAGAIN, on timeout. */
-            if (ret == -EAGAIN)
-                ret = -ETIMEDOUT;
-            if (ret == -ETIMEDOUT)
-                LISTP_DEL(&waiter, &futex->waiters, list);
-            lock(&hdl->lock);
-            /* Chia-Che 10/17/17: FUTEX_WAKE should remove the waiter
-             * from the list; if not, we should remove it now. */
-            if (!LIST_EMPTY(&waiter, list))
-                LISTP_DEL(&waiter, &futex->waiters, list);
-            break;
-        }
-
         case FUTEX_WAKE:
         case FUTEX_WAKE_BITSET: {
-            struct futex_waiter * waiter, * wtmp;
-            int nwaken = 0;
-            uint32_t bitset = (futex_op == FUTEX_WAKE_BITSET) ? (uint32_t) val3 :
-                              0xffffffff;
+            struct futex_waiter* waiter;
+            struct futex_waiter* wtmp;
+            int nwaken      = 0;
+            uint32_t bitset = (futex_op == FUTEX_WAKE_BITSET) ? (uint32_t)val3 : 0xffffffff;
 
-            debug("FUTEX_WAKE: %p (val = %d) count = %d mask = %08x\n",
-                  uaddr, *uaddr, val, bitset);
+            debug("FUTEX_WAKE: %p (val = %d) count = %d mask = %08x\n", uaddr, *uaddr, val, bitset);
 
             LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex->waiters, list) {
                 if (!(bitset & waiter->bitset))
                     continue;
 
-                debug("FUTEX_WAKE wake thread %d: %p (val = %d)\n",
-                      waiter->thread->tid, uaddr, *uaddr);
+                debug("FUTEX_WAKE wake thread %d: %p (val = %d)\n", waiter->thread->tid, uaddr,
+                      *uaddr);
 
                 LISTP_DEL_INIT(waiter, &futex->waiters, list);
                 thread_wakeup(waiter->thread);
                 nwaken++;
-                if (nwaken >= val) break;
+                if (nwaken >= val)
+                    break;
             }
 
             ret = nwaken;
@@ -224,34 +223,56 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
 
         case FUTEX_WAKE_OP: {
             assert(futex2);
-            int oldval = *(int *) uaddr2, newval, cmpval;
+            int oldval = *(int*)uaddr2, newval, cmpval;
 
             newval = (val3 >> 12) & 0xfff;
             switch ((val3 >> 28) & 0xf) {
-                case FUTEX_OP_SET:  break;
-                case FUTEX_OP_ADD:  newval = oldval + newval;  break;
-                case FUTEX_OP_OR:   newval = oldval | newval;  break;
-                case FUTEX_OP_ANDN: newval = oldval & ~newval; break;
-                case FUTEX_OP_XOR:  newval = oldval ^ newval;  break;
+                case FUTEX_OP_SET:
+                    break;
+                case FUTEX_OP_ADD:
+                    newval = oldval + newval;
+                    break;
+                case FUTEX_OP_OR:
+                    newval = oldval | newval;
+                    break;
+                case FUTEX_OP_ANDN:
+                    newval = oldval & ~newval;
+                    break;
+                case FUTEX_OP_XOR:
+                    newval = oldval ^ newval;
+                    break;
             }
 
             cmpval = val3 & 0xfff;
             switch ((val3 >> 24) & 0xf) {
-                case FUTEX_OP_CMP_EQ: cmpval = (oldval == cmpval); break;
-                case FUTEX_OP_CMP_NE: cmpval = (oldval != cmpval); break;
-                case FUTEX_OP_CMP_LT: cmpval = (oldval < cmpval);  break;
-                case FUTEX_OP_CMP_LE: cmpval = (oldval <= cmpval); break;
-                case FUTEX_OP_CMP_GT: cmpval = (oldval > cmpval);  break;
-                case FUTEX_OP_CMP_GE: cmpval = (oldval >= cmpval); break;
+                case FUTEX_OP_CMP_EQ:
+                    cmpval = (oldval == cmpval);
+                    break;
+                case FUTEX_OP_CMP_NE:
+                    cmpval = (oldval != cmpval);
+                    break;
+                case FUTEX_OP_CMP_LT:
+                    cmpval = (oldval < cmpval);
+                    break;
+                case FUTEX_OP_CMP_LE:
+                    cmpval = (oldval <= cmpval);
+                    break;
+                case FUTEX_OP_CMP_GT:
+                    cmpval = (oldval > cmpval);
+                    break;
+                case FUTEX_OP_CMP_GE:
+                    cmpval = (oldval >= cmpval);
+                    break;
             }
 
-            *(int *) uaddr2 = newval;
-            struct futex_waiter * waiter, * wtmp;
+            *(int*)uaddr2 = newval;
+            struct futex_waiter* waiter;
+            struct futex_waiter* wtmp;
             int nwaken = 0;
             debug("FUTEX_WAKE_OP: %p (val = %d) count = %d\n", uaddr, *uaddr, val);
             LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex->waiters, list) {
-                debug("FUTEX_WAKE_OP wake thread %d: %p (val = %d)\n",
-                      waiter->thread->tid, uaddr, *uaddr);
+                debug("FUTEX_WAKE_OP wake thread %d: %p (val = %d)\n", waiter->thread->tid, uaddr,
+                      *uaddr);
                 LISTP_DEL_INIT(waiter, &futex->waiters, list);
                 thread_wakeup(waiter->thread);
                 nwaken++;
@@ -262,11 +283,10 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
                 put_handle(hdl);
                 hdl = hdl2;
                 lock(&hdl->lock);
-                debug("FUTEX_WAKE: %p (val = %d) count = %d\n", uaddr2,
-                      *uaddr2, val2);
+                debug("FUTEX_WAKE: %p (val = %d) count = %d\n", uaddr2, *uaddr2, val2);
                 LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex2->waiters, list) {
-                    debug("FUTEX_WAKE_OP(2) wake thread %d: %p (val = %d)\n",
-                          waiter->thread->tid, uaddr2, *uaddr2);
+                    debug("FUTEX_WAKE_OP(2) wake thread %d: %p (val = %d)\n", waiter->thread->tid,
+                          uaddr2, *uaddr2);
                     LISTP_DEL_INIT(waiter, &futex2->waiters, list);
                     thread_wakeup(waiter->thread);
                     nwaken++;
@@ -281,10 +301,11 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
                 ret = -EAGAIN;
                 break;
             }
-            /* FALLTHROUGH */
+        /* FALLTHROUGH */
         case FUTEX_REQUEUE: {
             assert(futex2);
-            struct futex_waiter * waiter, * wtmp;
+            struct futex_waiter* waiter;
+            struct futex_waiter* wtmp;
             int nwaken = 0;
             LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex->waiters, list) {
                 LISTP_DEL_INIT(waiter, &futex->waiters, list);
@@ -317,9 +338,8 @@ int shim_do_futex (int * uaddr, int op, int val, void * utime,
     return ret;
 }
 
-int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
-{
-    struct shim_thread * self = get_cur_thread();
+int shim_do_set_robust_list(struct robust_list_head* head, size_t len) {
+    struct shim_thread* self = get_cur_thread();
     assert(self);
 
     if (len != sizeof(struct robust_list_head))
@@ -329,13 +349,11 @@ int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
     return 0;
 }
 
-int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
-                             size_t * len)
-{
+int shim_do_get_robust_list(pid_t pid, struct robust_list_head** head, size_t* len) {
     if (!head)
         return -EFAULT;
 
-    struct shim_thread * thread;
+    struct shim_thread* thread;
 
     if (pid) {
         thread = lookup_thread(pid);
@@ -345,44 +363,45 @@ int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
         thread = get_cur_thread();
     }
 
-    *head = (struct robust_list_head *) thread->robust_list;
-    *len = sizeof(struct robust_list_head);
+    *head = (struct robust_list_head*)thread->robust_list;
+    *len  = sizeof(struct robust_list_head);
     return 0;
 }
 
-void release_robust_list (struct robust_list_head * head)
-{
+void release_robust_list(struct robust_list_head* head) {
     long futex_offset = head->futex_offset;
-    struct robust_list * robust, * prev = &head->list;
+    struct robust_list* robust;
+    struct robust_list* prev = &head->list;
 
     create_lock_runtime(&futex_list_lock);
 
-    for (robust = prev->next ; robust && robust != prev ;
-         prev = robust, robust = robust->next) {
-        void * futex_addr = (void *) robust + futex_offset;
-        struct shim_futex_handle * tmp, * futex = NULL;
+    for (robust = prev->next; robust && robust != prev; prev = robust, robust = robust->next) {
+        void* futex_addr = (void*)robust + futex_offset;
+        struct shim_futex_handle* tmp;
+        struct shim_futex_handle* futex = NULL;
 
         lock(&futex_list_lock);
 
-        LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list)
+        LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list) {
             if (tmp->uaddr == futex_addr) {
                 futex = tmp;
                 break;
             }
+        }
 
         unlock(&futex_list_lock);
 
         if (!futex)
             continue;
 
-        struct futex_waiter * waiter, * wtmp;
-        struct shim_handle * hdl =
-            container_of(futex, struct shim_handle, info.futex);
+        struct futex_waiter* waiter;
+        struct futex_waiter* wtmp;
+        struct shim_handle* hdl = container_of(futex, struct shim_handle, info.futex);
         get_handle(hdl);
         lock(&hdl->lock);
 
         debug("release robust list: %p\n", futex_addr);
-        *(int *) futex_addr = 0;
+        *(int*)futex_addr = 0;
         LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex->waiters, list) {
             LISTP_DEL_INIT(waiter, &futex->waiters, list);
             thread_wakeup(waiter->thread);
@@ -393,30 +412,31 @@ void release_robust_list (struct robust_list_head * head)
     }
 }
 
-void release_clear_child_id (int * clear_child_tid)
-{
+void release_clear_child_id(int* clear_child_tid) {
     debug("clear child tid at %p\n", clear_child_tid);
     *clear_child_tid = 0;
 
     create_lock_runtime(&futex_list_lock);
 
-    struct shim_futex_handle * tmp, * futex = NULL;
+    struct shim_futex_handle* tmp;
+    struct shim_futex_handle* futex = NULL;
     lock(&futex_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list)
-        if (tmp->uaddr == (void *) clear_child_tid) {
+    LISTP_FOR_EACH_ENTRY(tmp, &futex_list, list) {
+        if (tmp->uaddr == (void*)clear_child_tid) {
             futex = tmp;
             break;
         }
+    }
 
     unlock(&futex_list_lock);
 
     if (!futex)
         return;
 
-    struct futex_waiter * waiter, * wtmp;
-    struct shim_handle * hdl =
-            container_of(futex, struct shim_handle, info.futex);
+    struct futex_waiter* waiter;
+    struct futex_waiter* wtmp;
+    struct shim_handle* hdl = container_of(futex, struct shim_handle, info.futex);
     get_handle(hdl);
     lock(&hdl->lock);
 

+ 17 - 22
LibOS/shim/src/sys/shim_getcwd.c

@@ -20,37 +20,34 @@
  * Implementation of system call "getcwd", "chdir" and "fchdir".
  */
 
+#include <errno.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
 #ifndef ERANGE
-# define ERANGE 34
+#define ERANGE 34
 #endif
 
-int shim_do_getcwd (char * buf, size_t len)
-{
+int shim_do_getcwd(char* buf, size_t len) {
     if (!buf || !len)
         return -EINVAL;
 
     if (test_user_memory(buf, len, true))
         return -EFAULT;
 
-    struct shim_thread * thread = get_cur_thread();
+    struct shim_thread* thread = get_cur_thread();
     assert(thread);
 
-    struct shim_dentry * cwd = thread->cwd;
+    struct shim_dentry* cwd = thread->cwd;
 
     size_t plen;
-    const char * path = dentry_get_path(cwd, true, &plen);
+    const char* path = dentry_get_path(cwd, true, &plen);
 
     int ret;
     if (plen >= MAX_PATH) {
@@ -64,11 +61,10 @@ int shim_do_getcwd (char * buf, size_t len)
     return ret;
 }
 
-int shim_do_chdir (const char * filename)
-{
-    struct shim_thread * thread = get_cur_thread();
+int shim_do_chdir(const char* filename) {
+    struct shim_thread* thread = get_cur_thread();
     assert(thread);
-    struct shim_dentry * dent = NULL;
+    struct shim_dentry* dent = NULL;
     int ret;
 
     if (!filename)
@@ -99,14 +95,13 @@ int shim_do_chdir (const char * filename)
     return 0;
 }
 
-int shim_do_fchdir (int fd)
-{
-    struct shim_thread * thread = get_cur_thread();
-    struct shim_handle * hdl = get_fd_handle(fd, NULL, thread->handle_map);
+int shim_do_fchdir(int fd) {
+    struct shim_thread* thread = get_cur_thread();
+    struct shim_handle* hdl    = get_fd_handle(fd, NULL, thread->handle_map);
     if (!hdl)
         return -EBADF;
 
-    struct shim_dentry * dent = hdl->dentry;
+    struct shim_dentry* dent = hdl->dentry;
 
     if (!(dent->state & DENTRY_ISDIRECTORY)) {
         debug("%s is not a directory\n", dentry_get_path(dent, false, NULL));

+ 48 - 66
LibOS/shim/src/sys/shim_getpid.c

@@ -23,79 +23,69 @@
  * "setsid" and "getsid".
  */
 
+#include <asm/prctl.h>
+#include <errno.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
 #include <shim_internal.h>
+#include <shim_ipc.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_ipc.h>
-#include <shim_checkpoint.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <sys/syscall.h>
 #include <sys/mman.h>
-#include <asm/prctl.h>
-#include <errno.h>
+#include <sys/syscall.h>
 
-pid_t shim_do_getpid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+pid_t shim_do_getpid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->tgid : 0;
 }
 
-pid_t shim_do_gettid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+pid_t shim_do_gettid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->tid : 0;
 }
 
-pid_t shim_do_getppid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+pid_t shim_do_getppid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? (cur->parent ? cur->parent->tid : cur->ppid) : 0;
 }
 
-int shim_do_set_tid_address (int * tidptr)
-{
+int shim_do_set_tid_address(int* tidptr) {
     /* http://man7.org/linux/man-pages/man2/set_tid_address.2.html */
-    struct shim_thread * cur = get_cur_thread();
-    cur->clear_child_tid = tidptr;
+    struct shim_thread* cur = get_cur_thread();
+    cur->clear_child_tid    = tidptr;
     return cur->tid;
 }
 
-uid_t shim_do_getuid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+uid_t shim_do_getuid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->uid : 0;
 }
 
-gid_t shim_do_getgid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+gid_t shim_do_getgid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->gid : 0;
 }
 
-int shim_do_setuid (uid_t uid)
-{
-    struct shim_thread * cur = get_cur_thread();
-    cur->euid = (uint16_t) uid;
+int shim_do_setuid(uid_t uid) {
+    struct shim_thread* cur = get_cur_thread();
+    cur->euid               = (uint16_t)uid;
     return 0;
 }
 
-int shim_do_setgid (gid_t gid)
-{
-    struct shim_thread * cur = get_cur_thread();
-    cur->egid = (uint16_t) gid;
+int shim_do_setgid(gid_t gid) {
+    struct shim_thread* cur = get_cur_thread();
+    cur->egid               = (uint16_t)gid;
     return 0;
 }
 
 /* shim_do_set{get}groups() do not propagate group info to host OS but rather are dummies */
-#define NGROUPS_MAX 65536  /* # of supplemental group IDs; has to be same as host OS */
+#define NGROUPS_MAX 65536 /* # of supplemental group IDs; has to be same as host OS */
 
 static struct groups_info_t {
     int size;
     gid_t spl_gid[NGROUPS_MAX];
-} g_groups_info __attribute_migratable = { .size = -1 };
+} g_groups_info __attribute_migratable = {.size = -1};
 
 int shim_do_setgroups(int gidsetsize, gid_t* grouplist) {
     if ((unsigned)gidsetsize > NGROUPS_MAX)
@@ -106,8 +96,9 @@ int shim_do_setgroups(int gidsetsize, gid_t* grouplist) {
 
     lock(&cur_process.lock);
     g_groups_info.size = gidsetsize;
-    for (int i = 0; i < gidsetsize; i++)
+    for (int i = 0; i < gidsetsize; i++) {
         g_groups_info.spl_gid[i] = grouplist[i];
+    }
     unlock(&cur_process.lock);
 
     return 0;
@@ -126,7 +117,7 @@ int shim_do_getgroups(int gidsetsize, gid_t* grouplist) {
 
     if (g_groups_info.size == -1) {
         /* initialize with getgid() */
-        g_groups_info.size = 1;
+        g_groups_info.size       = 1;
         g_groups_info.spl_gid[0] = shim_do_getgid();
     }
 
@@ -137,30 +128,27 @@ int shim_do_getgroups(int gidsetsize, gid_t* grouplist) {
             return -EINVAL;
         }
 
-        for (int i = 0; i < cur_groups_size; i++)
+        for (int i = 0; i < cur_groups_size; i++) {
             grouplist[i] = g_groups_info.spl_gid[i];
+        }
     }
 
     unlock(&cur_process.lock);
     return cur_groups_size;
 }
 
-uid_t shim_do_geteuid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+uid_t shim_do_geteuid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->euid : 0;
 }
 
-gid_t shim_do_getegid (void)
-{
-    struct shim_thread * cur = get_cur_thread();
+gid_t shim_do_getegid(void) {
+    struct shim_thread* cur = get_cur_thread();
     return cur ? cur->egid : 0;
 }
 
-int shim_do_setpgid (pid_t pid, pid_t pgid)
-{
-    struct shim_thread * thread =
-            pid ? lookup_thread(pid) : get_cur_thread();
+int shim_do_setpgid(pid_t pid, pid_t pgid) {
+    struct shim_thread* thread = pid ? lookup_thread(pid) : get_cur_thread();
 
     if (!pid)
         assert(thread);
@@ -168,15 +156,13 @@ int shim_do_setpgid (pid_t pid, pid_t pgid)
     if (!thread)
         return -ESRCH;
 
-    thread->pgid = (IDTYPE) pgid ? : thread->tgid;
+    thread->pgid = (IDTYPE)pgid ?: thread->tgid;
 
     return 0;
 }
 
-int shim_do_getpgid (pid_t pid)
-{
-    struct shim_thread * thread =
-            pid ? lookup_thread(pid) : get_cur_thread();
+int shim_do_getpgid(pid_t pid) {
+    struct shim_thread* thread = pid ? lookup_thread(pid) : get_cur_thread();
 
     if (!thread)
         return -ESRCH;
@@ -184,16 +170,14 @@ int shim_do_getpgid (pid_t pid)
     return thread->pgid;
 }
 
-pid_t shim_do_getpgrp (void)
-{
-    struct shim_thread * cur_thread = get_cur_thread();
+pid_t shim_do_getpgrp(void) {
+    struct shim_thread* cur_thread = get_cur_thread();
     assert(cur_thread);
     return cur_thread->pgid;
 }
 
-int shim_do_setsid (void)
-{
-    struct shim_thread * cur_thread = get_cur_thread();
+int shim_do_setsid(void) {
+    struct shim_thread* cur_thread = get_cur_thread();
     assert(cur_thread);
 
     if (cur_thread->pgid == cur_thread->tgid)
@@ -206,10 +190,8 @@ int shim_do_setsid (void)
     return 0;
 }
 
-int shim_do_getsid (pid_t pid)
-{
-    struct shim_thread * thread =
-            pid ? lookup_thread(pid) : get_cur_thread();
+int shim_do_getsid(pid_t pid) {
+    struct shim_thread* thread = pid ? lookup_thread(pid) : get_cur_thread();
 
     if (!thread)
         return -ESRCH;

+ 21 - 26
LibOS/shim/src/sys/shim_getrlimit.c

@@ -20,15 +20,13 @@
  * Implementation of system call "getrlimit" and "setrlimit".
  */
 
-#include <shim_internal.h>
+#include <asm/resource.h>
 #include <shim_checkpoint.h>
+#include <shim_internal.h>
 #include <shim_table.h>
 #include <shim_utils.h>
 #include <shim_vma.h>
 
-#include <asm/resource.h>
-
-
 /*
  * TODO: implement actual limitation on each resource.
  *
@@ -39,27 +37,27 @@
 #define MAX_THREADS     (0x3fffffff / 2)
 #define DEFAULT_MAX_FDS (1024)
 #define MAX_MAX_FDS     (65536) /* 4096: Linux initial value */
-#define MLOCK_LIMIT     (64*1024)
+#define MLOCK_LIMIT     (64 * 1024)
 #define MQ_BYTES_MAX    819200
 
 static struct __kernel_rlimit64 __rlim[RLIM_NLIMITS] __attribute_migratable = {
-    [RLIMIT_CPU]        = {   RLIM_INFINITY, RLIM_INFINITY },
-    [RLIMIT_FSIZE]      = {   RLIM_INFINITY, RLIM_INFINITY },
-    [RLIMIT_DATA]       = {   RLIM_INFINITY, RLIM_INFINITY },
-    [RLIMIT_STACK]      = { DEFAULT_SYS_STACK_SIZE, RLIM_INFINITY },
-    [RLIMIT_CORE]       = {               0, RLIM_INFINITY },
-    [RLIMIT_RSS]        = {   RLIM_INFINITY, RLIM_INFINITY },
-    [RLIMIT_NPROC]      = {     MAX_THREADS,   MAX_THREADS },
-    [RLIMIT_NOFILE]     = { DEFAULT_MAX_FDS,   MAX_MAX_FDS },
-    [RLIMIT_MEMLOCK]    = {     MLOCK_LIMIT,   MLOCK_LIMIT },
-    [RLIMIT_AS]         = {   RLIM_INFINITY, RLIM_INFINITY },
-    [RLIMIT_LOCKS]      = {   RLIM_INFINITY, RLIM_INFINITY },
+    [RLIMIT_CPU]     = {RLIM_INFINITY, RLIM_INFINITY},
+    [RLIMIT_FSIZE]   = {RLIM_INFINITY, RLIM_INFINITY},
+    [RLIMIT_DATA]    = {RLIM_INFINITY, RLIM_INFINITY},
+    [RLIMIT_STACK]   = {DEFAULT_SYS_STACK_SIZE, RLIM_INFINITY},
+    [RLIMIT_CORE]    = {0, RLIM_INFINITY},
+    [RLIMIT_RSS]     = {RLIM_INFINITY, RLIM_INFINITY},
+    [RLIMIT_NPROC]   = {MAX_THREADS, MAX_THREADS},
+    [RLIMIT_NOFILE]  = {DEFAULT_MAX_FDS, MAX_MAX_FDS},
+    [RLIMIT_MEMLOCK] = {MLOCK_LIMIT, MLOCK_LIMIT},
+    [RLIMIT_AS]      = {RLIM_INFINITY, RLIM_INFINITY},
+    [RLIMIT_LOCKS]   = {RLIM_INFINITY, RLIM_INFINITY},
     /* [RLIMIT_SIGPENDING] = [RLIMIT_NPROC] for initial value */
-    [RLIMIT_SIGPENDING] = {     MAX_THREADS,   MAX_THREADS },
-    [RLIMIT_MSGQUEUE]   = {    MQ_BYTES_MAX,  MQ_BYTES_MAX },
-    [RLIMIT_NICE]       = {               0,             0 },
-    [RLIMIT_RTPRIO]     = {               0,             0 },
-    [RLIMIT_RTTIME]     = {   RLIM_INFINITY, RLIM_INFINITY },
+    [RLIMIT_SIGPENDING] = {MAX_THREADS, MAX_THREADS},
+    [RLIMIT_MSGQUEUE]   = {MQ_BYTES_MAX, MQ_BYTES_MAX},
+    [RLIMIT_NICE]       = {0, 0},
+    [RLIMIT_RTPRIO]     = {0, 0},
+    [RLIMIT_RTTIME]     = {RLIM_INFINITY, RLIM_INFINITY},
 };
 
 static struct shim_lock rlimit_lock;
@@ -84,8 +82,7 @@ void set_rlimit_cur(int resource, uint64_t rlim) {
     unlock(&rlimit_lock);
 }
 
-int shim_do_getrlimit (int resource, struct __kernel_rlimit * rlim)
-{
+int shim_do_getrlimit(int resource, struct __kernel_rlimit* rlim) {
     if (resource < 0 || RLIM_NLIMITS <= resource)
         return -EINVAL;
     if (!rlim || test_user_memory(rlim, sizeof(*rlim), true))
@@ -98,8 +95,7 @@ int shim_do_getrlimit (int resource, struct __kernel_rlimit * rlim)
     return 0;
 }
 
-int shim_do_setrlimit (int resource, struct __kernel_rlimit * rlim)
-{
+int shim_do_setrlimit(int resource, struct __kernel_rlimit* rlim) {
     struct shim_thread* cur_thread = get_cur_thread();
     assert(cur_thread);
 
@@ -122,7 +118,6 @@ int shim_do_setrlimit (int resource, struct __kernel_rlimit * rlim)
 
 int shim_do_prlimit64(pid_t pid, int resource, const struct __kernel_rlimit64* new_rlim,
                       struct __kernel_rlimit64* old_rlim) {
-
     struct shim_thread* cur_thread = get_cur_thread();
     assert(cur_thread);
 

+ 111 - 123
LibOS/shim/src/sys/shim_ioctl.c

@@ -20,43 +20,38 @@
  * Implementation of system call "ioctl".
  */
 
-#include <shim_internal.h>
-#include <shim_table.h>
-#include <shim_thread.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <asm/unistd.h>
-#include <errno.h>
 #include <asm/ioctl.h>
 #include <asm/ioctls.h>
-#include <asm/termios.h>
 #include <asm/termbits.h>
+#include <asm/termios.h>
+#include <asm/unistd.h>
+#include <errno.h>
 #include <linux/fd.h>
 #include <linux/sockios.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_table.h>
+#include <shim_thread.h>
 
-#define TERM_DEFAULT_IFLAG (ICRNL|IUTF8)
-#define TERM_DEFAULT_OFLAG (OPOST|ONLCR)
-#define TERM_DEFAULT_CFLAG (B38400|CS8|CREAD)
-#define TERM_DEFAULT_LFLAG (ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN)
+#define TERM_DEFAULT_IFLAG (ICRNL | IUTF8)
+#define TERM_DEFAULT_OFLAG (OPOST | ONLCR)
+#define TERM_DEFAULT_CFLAG (B38400 | CS8 | CREAD)
+#define TERM_DEFAULT_LFLAG (ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN)
 
-static int ioctl_termios (struct shim_handle * hdl, unsigned int cmd,
-                          unsigned long arg)
-{
-    if (hdl->type != TYPE_FILE ||
-        hdl->info.file.type != FILE_TTY)
+static int ioctl_termios(struct shim_handle* hdl, unsigned int cmd, unsigned long arg) {
+    if (hdl->type != TYPE_FILE || hdl->info.file.type != FILE_TTY)
         return -ENOTTY;
 
     if (!arg)
         return -EINVAL;
 
-    switch(cmd) {
+    switch (cmd) {
         /* <include/asm/termios.h> */
         case TIOCGPGRP:
-            *(int *) arg = get_cur_thread()->pgid;
+            *(int*)arg = get_cur_thread()->pgid;
             return 0;
 
         case TIOCSPGRP:
@@ -159,19 +154,17 @@ static int ioctl_termios (struct shim_handle * hdl, unsigned int cmd,
             goto passthrough;
     }
 
- passthrough:
+passthrough:
     return -EAGAIN;
 }
 
-static int ioctl_fd (struct shim_handle * hdl, unsigned int cmd,
-                     unsigned long arg)
-{
+static int ioctl_fd(struct shim_handle* hdl, unsigned int cmd, unsigned long arg) {
     // This is just a placeholder function; arguments are not actually used
     // right now
     __UNUSED(hdl);
     __UNUSED(arg);
 
-    switch(cmd) {
+    switch (cmd) {
         /* <include/linux/fd.h> */
 
         /* 0x00000000 FDCLRPRM void */
@@ -226,13 +219,11 @@ static int ioctl_fd (struct shim_handle * hdl, unsigned int cmd,
             goto passthrough;
     }
 
- passthrough:
+passthrough:
     return -EAGAIN;
 }
 
-static int ioctl_netdevice (struct shim_handle * hdl, unsigned int cmd,
-                            unsigned long arg)
-{
+static int ioctl_netdevice(struct shim_handle* hdl, unsigned int cmd, unsigned long arg) {
     // This is just a placeholder function; arguments are not actually used
     // right now
     __UNUSED(arg);
@@ -240,70 +231,69 @@ static int ioctl_netdevice (struct shim_handle * hdl, unsigned int cmd,
     if (hdl->type != TYPE_SOCK)
         return -ENOTSOCK;
 
-    struct shim_sock_handle * sock = &hdl->info.sock;
+    struct shim_sock_handle* sock = &hdl->info.sock;
 
     if (sock->sock_state == SOCK_CREATED) {
         if (sock->sock_type == SOCK_STREAM)
             return -ENOTCONN;
     }
 
-    switch(cmd) {
+    switch (cmd) {
         /* Socket configuration controls. */
-        case SIOCGIFNAME:       /* 0x8910 get iface name */
-        case SIOCSIFLINK:       /* 0x8911 set iface channel */
-        case SIOCGIFCONF:       /* 0x8912 get iface list */
-        case SIOCGIFFLAGS:      /* 0x8913 get flags */
-        case SIOCSIFFLAGS:      /* 0x8914 set flags */
-        case SIOCGIFADDR:       /* 0x8915 get PA address */
-        case SIOCSIFADDR:       /* 0x8916 set PA address */
-        case SIOCGIFDSTADDR:    /* 0x8917 get remote PA address */
-        case SIOCSIFDSTADDR:    /* 0x8918 set remote PA address */
-        case SIOCGIFBRDADDR:    /* 0x8919 get broadcast PA address */
-        case SIOCSIFBRDADDR:    /* 0x891a set broadcast PA address */
-        case SIOCGIFNETMASK:    /* 0x891b get network PA mask */
-        case SIOCSIFNETMASK:    /* 0x891c set network PA mask */
-        case SIOCGIFMETRIC:     /* 0x891d get metric */
-        case SIOCSIFMETRIC:     /* 0x891e set metric */
-        case SIOCGIFMEM:        /* 0x891f get memory address (BSD) */
-        case SIOCSIFMEM:        /* 0x8920 set memory address (BSD) */
-        case SIOCGIFMTU:        /* 0x8921 get MTU size */
-        case SIOCSIFMTU:        /* 0x8922 set MTU size */
-        case SIOCSIFNAME:       /* 0x8923 set interface name */
-        case SIOCSIFHWADDR:     /* 0x8924 set hardware address */
-        case SIOCGIFENCAP:      /* 0x8925 get/set encapsulations       */
-        case SIOCSIFENCAP:      /* 0x8926 */
-        case SIOCGIFHWADDR:     /* 0x8927 Get hardware address */
-        case SIOCGIFSLAVE:      /* 0x8929 Driver slaving support */
-        case SIOCSIFSLAVE:      /* 0x8930 */
-        case SIOCADDMULTI:      /* 0x8931 Multicast address lists */
-        case SIOCDELMULTI:      /* 0x8932 */
-        case SIOCGIFINDEX:      /* 0x8933 name -> if_index mapping */
+        case SIOCGIFNAME:    /* 0x8910 get iface name */
+        case SIOCSIFLINK:    /* 0x8911 set iface channel */
+        case SIOCGIFCONF:    /* 0x8912 get iface list */
+        case SIOCGIFFLAGS:   /* 0x8913 get flags */
+        case SIOCSIFFLAGS:   /* 0x8914 set flags */
+        case SIOCGIFADDR:    /* 0x8915 get PA address */
+        case SIOCSIFADDR:    /* 0x8916 set PA address */
+        case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
+        case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
+        case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
+        case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
+        case SIOCGIFNETMASK: /* 0x891b get network PA mask */
+        case SIOCSIFNETMASK: /* 0x891c set network PA mask */
+        case SIOCGIFMETRIC:  /* 0x891d get metric */
+        case SIOCSIFMETRIC:  /* 0x891e set metric */
+        case SIOCGIFMEM:     /* 0x891f get memory address (BSD) */
+        case SIOCSIFMEM:     /* 0x8920 set memory address (BSD) */
+        case SIOCGIFMTU:     /* 0x8921 get MTU size */
+        case SIOCSIFMTU:     /* 0x8922 set MTU size */
+        case SIOCSIFNAME:    /* 0x8923 set interface name */
+        case SIOCSIFHWADDR:  /* 0x8924 set hardware address */
+        case SIOCGIFENCAP:   /* 0x8925 get/set encapsulations       */
+        case SIOCSIFENCAP:   /* 0x8926 */
+        case SIOCGIFHWADDR:  /* 0x8927 Get hardware address */
+        case SIOCGIFSLAVE:   /* 0x8929 Driver slaving support */
+        case SIOCSIFSLAVE:   /* 0x8930 */
+        case SIOCADDMULTI:   /* 0x8931 Multicast address lists */
+        case SIOCDELMULTI:   /* 0x8932 */
+        case SIOCGIFINDEX:   /* 0x8933 name -> if_index mapping */
         /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
-        case SIOCSIFPFLAGS:     /* 0x8934 set/get extended flags set */
-        case SIOCGIFPFLAGS:     /* 0x8935 */
-        case SIOCDIFADDR:       /* 0x8936 delete PA address */
+        case SIOCSIFPFLAGS:      /* 0x8934 set/get extended flags set */
+        case SIOCGIFPFLAGS:      /* 0x8935 */
+        case SIOCDIFADDR:        /* 0x8936 delete PA address */
         case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
-        case SIOCGIFCOUNT:      /* 0x8938 get number of devices */
-        case SIOCGIFBR:         /* 0x8940 Bridging support */
-        case SIOCSIFBR:         /* 0x8941 Set bridging options  */
-        case SIOCGIFTXQLEN:     /* 0x8942 Get the tx queue length */
-        case SIOCSIFTXQLEN:     /* 0x8943 Set the tx queue length  */
+        case SIOCGIFCOUNT:       /* 0x8938 get number of devices */
+        case SIOCGIFBR:          /* 0x8940 Bridging support */
+        case SIOCSIFBR:          /* 0x8941 Set bridging options  */
+        case SIOCGIFTXQLEN:      /* 0x8942 Get the tx queue length */
+        case SIOCSIFTXQLEN:      /* 0x8943 Set the tx queue length  */
         default:
             goto passthrough;
     }
 
- passthrough:
+passthrough:
     return -EAGAIN;
 }
 
-void signal_io (IDTYPE target, void *arg)
-{
+void signal_io(IDTYPE target, void* arg) {
     // Kept for compatibility with signal_itimer
     __UNUSED(arg);
 
     debug("detecting input, signaling thread %u\n", target);
 
-    struct shim_thread * thread = lookup_thread(target);
+    struct shim_thread* thread = lookup_thread(target);
     if (!thread)
         return;
 
@@ -312,14 +302,13 @@ void signal_io (IDTYPE target, void *arg)
     unlock(&thread->lock);
 }
 
-int shim_do_ioctl (int fd, int cmd, unsigned long arg)
-{
-    struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
+int shim_do_ioctl(int fd, int cmd, unsigned long arg) {
+    struct shim_handle* hdl = get_fd_handle(fd, NULL, NULL);
     if (!hdl)
         return -EBADF;
 
     int ret = -EAGAIN;
-    switch(cmd) {
+    switch (cmd) {
         /* <include/asm/termios.h> */
         case TCGETS:
         case TCSETS:
@@ -359,8 +348,7 @@ int shim_do_ioctl (int fd, int cmd, unsigned long arg)
             ret = ioctl_termios(hdl, cmd, arg);
             break;
         case FIONBIO:
-            if (hdl->fs && hdl->fs->fs_ops &&
-                hdl->fs->fs_ops->setflags)
+            if (hdl->fs && hdl->fs->fs_ops && hdl->fs->fs_ops->setflags)
                 hdl->fs->fs_ops->setflags(hdl, hdl->flags | O_NONBLOCK);
             hdl->flags |= O_NONBLOCK;
             ret = 0;
@@ -416,9 +404,9 @@ int shim_do_ioctl (int fd, int cmd, unsigned long arg)
             break;
 
         case FIONREAD: {
-            struct shim_mount * fs = hdl->fs;
-            int size = 0;
-            int offset = 0;
+            struct shim_mount* fs = hdl->fs;
+            int size              = 0;
+            int offset            = 0;
 
             if (!fs || !fs->fs_ops) {
                 ret = -EACCES;
@@ -448,51 +436,51 @@ int shim_do_ioctl (int fd, int cmd, unsigned long arg)
                 offset = ret;
             }
 
-            *(int *) arg = size - offset;
-            ret = 0;
+            *(int*)arg = size - offset;
+            ret        = 0;
             break;
         }
 
         /* Socket configuration controls. */
-        case SIOCGIFNAME:       /* 0x8910 get iface name */
-        case SIOCSIFLINK:       /* 0x8911 set iface channel */
-        case SIOCGIFCONF:       /* 0x8912 get iface list */
-        case SIOCGIFFLAGS:      /* 0x8913 get flags */
-        case SIOCSIFFLAGS:      /* 0x8914 set flags */
-        case SIOCGIFADDR:       /* 0x8915 get PA address */
-        case SIOCSIFADDR:       /* 0x8916 set PA address */
-        case SIOCGIFDSTADDR:    /* 0x8917 get remote PA address */
-        case SIOCSIFDSTADDR:    /* 0x8918 set remote PA address */
-        case SIOCGIFBRDADDR:    /* 0x8919 get broadcast PA address */
-        case SIOCSIFBRDADDR:    /* 0x891a set broadcast PA address */
-        case SIOCGIFNETMASK:    /* 0x891b get network PA mask */
-        case SIOCSIFNETMASK:    /* 0x891c set network PA mask */
-        case SIOCGIFMETRIC:     /* 0x891d get metric */
-        case SIOCSIFMETRIC:     /* 0x891e set metric */
-        case SIOCGIFMEM:        /* 0x891f get memory address (BSD) */
-        case SIOCSIFMEM:        /* 0x8920 set memory address (BSD) */
-        case SIOCGIFMTU:        /* 0x8921 get MTU size */
-        case SIOCSIFMTU:        /* 0x8922 set MTU size */
-        case SIOCSIFNAME:       /* 0x8923 set interface name */
-        case SIOCSIFHWADDR:     /* 0x8924 set hardware address */
-        case SIOCGIFENCAP:      /* 0x8925 get/set encapsulations       */
-        case SIOCSIFENCAP:      /* 0x8926 */
-        case SIOCGIFHWADDR:     /* 0x8927 Get hardware address */
-        case SIOCGIFSLAVE:      /* 0x8929 Driver slaving support */
-        case SIOCSIFSLAVE:      /* 0x8930 */
-        case SIOCADDMULTI:      /* 0x8931 Multicast address lists */
-        case SIOCDELMULTI:      /* 0x8932 */
-        case SIOCGIFINDEX:      /* 0x8933 name -> if_index mapping */
+        case SIOCGIFNAME:    /* 0x8910 get iface name */
+        case SIOCSIFLINK:    /* 0x8911 set iface channel */
+        case SIOCGIFCONF:    /* 0x8912 get iface list */
+        case SIOCGIFFLAGS:   /* 0x8913 get flags */
+        case SIOCSIFFLAGS:   /* 0x8914 set flags */
+        case SIOCGIFADDR:    /* 0x8915 get PA address */
+        case SIOCSIFADDR:    /* 0x8916 set PA address */
+        case SIOCGIFDSTADDR: /* 0x8917 get remote PA address */
+        case SIOCSIFDSTADDR: /* 0x8918 set remote PA address */
+        case SIOCGIFBRDADDR: /* 0x8919 get broadcast PA address */
+        case SIOCSIFBRDADDR: /* 0x891a set broadcast PA address */
+        case SIOCGIFNETMASK: /* 0x891b get network PA mask */
+        case SIOCSIFNETMASK: /* 0x891c set network PA mask */
+        case SIOCGIFMETRIC:  /* 0x891d get metric */
+        case SIOCSIFMETRIC:  /* 0x891e set metric */
+        case SIOCGIFMEM:     /* 0x891f get memory address (BSD) */
+        case SIOCSIFMEM:     /* 0x8920 set memory address (BSD) */
+        case SIOCGIFMTU:     /* 0x8921 get MTU size */
+        case SIOCSIFMTU:     /* 0x8922 set MTU size */
+        case SIOCSIFNAME:    /* 0x8923 set interface name */
+        case SIOCSIFHWADDR:  /* 0x8924 set hardware address */
+        case SIOCGIFENCAP:   /* 0x8925 get/set encapsulations       */
+        case SIOCSIFENCAP:   /* 0x8926 */
+        case SIOCGIFHWADDR:  /* 0x8927 Get hardware address */
+        case SIOCGIFSLAVE:   /* 0x8929 Driver slaving support */
+        case SIOCSIFSLAVE:   /* 0x8930 */
+        case SIOCADDMULTI:   /* 0x8931 Multicast address lists */
+        case SIOCDELMULTI:   /* 0x8932 */
+        case SIOCGIFINDEX:   /* 0x8933 name -> if_index mapping */
         /* SIOGIFINDEX = SIOCGIFINDEX misprint compatibility :-) */
-        case SIOCSIFPFLAGS:     /* 0x8934 set/get extended flags set */
-        case SIOCGIFPFLAGS:     /* 0x8935 */
-        case SIOCDIFADDR:       /* 0x8936 delete PA address */
+        case SIOCSIFPFLAGS:      /* 0x8934 set/get extended flags set */
+        case SIOCGIFPFLAGS:      /* 0x8935 */
+        case SIOCDIFADDR:        /* 0x8936 delete PA address */
         case SIOCSIFHWBROADCAST: /* 0x8937 set hardware broadcast addr */
-        case SIOCGIFCOUNT:      /* 0x8938 get number of devices */
-        case SIOCGIFBR:         /* 0x8940 Bridging support */
-        case SIOCSIFBR:         /* 0x8941 Set bridging options  */
-        case SIOCGIFTXQLEN:     /* 0x8942 Get the tx queue length */
-        case SIOCSIFTXQLEN:     /* 0x8943 Set the tx queue length  */
+        case SIOCGIFCOUNT:       /* 0x8938 get number of devices */
+        case SIOCGIFBR:          /* 0x8940 Bridging support */
+        case SIOCSIFBR:          /* 0x8941 Set bridging options  */
+        case SIOCGIFTXQLEN:      /* 0x8942 Get the tx queue length */
+        case SIOCSIFTXQLEN:      /* 0x8943 Set the tx queue length  */
             ret = ioctl_netdevice(hdl, cmd, arg);
             break;
 

+ 61 - 73
LibOS/shim/src/sys/shim_migrate.c

@@ -20,23 +20,19 @@
  * Implementation of system call "checkpoint" and "restore".
  */
 
+#include <asm/mman.h>
+#include <errno.h>
+#include <linux/fcntl.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
+#include <shim_ipc.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_handle.h>
 #include <shim_vma.h>
-#include <shim_fs.h>
-#include <shim_ipc.h>
-#include <shim_checkpoint.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <errno.h>
-
-#include <linux/fcntl.h>
-
-#include <asm/mman.h>
 
 /* cp_session objects are on the cp_sessions list, by the list field */
 /* cp_threads are organized onto a list, handing off of the
@@ -44,28 +40,26 @@
 
 DEFINE_LIST(cp_thread);
 struct cp_thread {
-    struct shim_thread *    thread;
-    LIST_TYPE(cp_thread)    list;
+    struct shim_thread* thread;
+    LIST_TYPE(cp_thread) list;
 };
 
-
 DEFINE_LIST(cp_session);
 DEFINE_LISTP(cp_thread);
 struct cp_session {
-    IDTYPE                  sid;
-    struct shim_handle *    cpfile;
-    LISTP_TYPE(cp_thread)   registered_threads;
-    LIST_TYPE(cp_session)   list;
-    PAL_HANDLE              finish_event;
-    struct shim_cp_store    cpstore;
+    IDTYPE sid;
+    struct shim_handle* cpfile;
+    LISTP_TYPE(cp_thread) registered_threads;
+    LIST_TYPE(cp_session) list;
+    PAL_HANDLE finish_event;
+    struct shim_cp_store cpstore;
 };
 
 DEFINE_LISTP(cp_session);
 LISTP_TYPE(cp_session) cp_sessions;
 
-int create_checkpoint (const char * cpdir, IDTYPE * sid)
-{
-    struct cp_session * cpsession = malloc(sizeof(struct cp_session));
+int create_checkpoint(const char* cpdir, IDTYPE* sid) {
+    struct cp_session* cpsession = malloc(sizeof(struct cp_session));
     if (!cpsession)
         return -ENOMEM;
 
@@ -74,10 +68,10 @@ int create_checkpoint (const char * cpdir, IDTYPE * sid)
     INIT_LISTP(&cpsession->registered_threads);
     INIT_LIST_HEAD(cpsession, list);
     cpsession->finish_event = DkNotificationEventCreate(PAL_FALSE);
-    cpsession->cpfile = NULL;
+    cpsession->cpfile       = NULL;
 
-    int len = strlen(cpdir);
-    char * filename = __alloca(len + 10);
+    int len        = strlen(cpdir);
+    char* filename = __alloca(len + 10);
     memcpy(filename, cpdir, len);
     filename[len] = '/';
     snprintf(filename + len + 1, 9, "%08x", cur_process.vmid);
@@ -89,36 +83,37 @@ int create_checkpoint (const char * cpdir, IDTYPE * sid)
     }
 
     /* the directory might not be created. At least try to create it */
-    if ((ret = open_namei(NULL, NULL, cpdir, O_CREAT|O_DIRECTORY, 0700,
-                          NULL)) < 0
-        && ret != -EEXIST)
+    if ((ret = open_namei(NULL, NULL, cpdir, O_CREAT | O_DIRECTORY, 0700, NULL)) < 0 &&
+        ret != -EEXIST)
         goto err;
 
-    if ((ret = open_namei(cpsession->cpfile, NULL, filename,
-                          O_CREAT|O_EXCL|O_RDWR, 0600, NULL)) < 0)
+    if ((ret = open_namei(cpsession->cpfile, NULL, filename, O_CREAT | O_EXCL | O_RDWR, 0600,
+                          NULL)) < 0)
         goto err;
 
     get_handle(cpsession->cpfile);
     MASTER_LOCK();
 
-    struct cp_session * s;
+    struct cp_session* s;
     if (*sid) {
-        LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list)
+        LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list) {
             if (s->sid == *sid) {
                 ret = 0;
                 goto err_locked;
             }
+        }
     } else {
-retry:
+    retry:
         ret = DkRandomBitsRead(&cpsession->sid, sizeof(cpsession->sid));
         if (ret < 0) {
             ret = -convert_pal_errno(-ret);
             goto err_locked;
         }
 
-        LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list)
+        LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list) {
             if (s->sid == cpsession->sid)
                 goto retry;
+        }
 
         *sid = cpsession->sid;
     }
@@ -138,39 +133,39 @@ err:
     return ret;
 }
 
-static int finish_checkpoint (struct cp_session * session);
+static int finish_checkpoint(struct cp_session* session);
 
-static int check_thread (struct shim_thread * thread, void * arg,
-                         bool * unlocked)
-{
-    __UNUSED(unlocked); // Retained for API compatibility
-    LISTP_TYPE(cp_thread) * registered = (LISTP_TYPE(cp_thread) *) arg;
-    struct cp_thread * t;
+static int check_thread(struct shim_thread* thread, void* arg, bool* unlocked) {
+    __UNUSED(unlocked);  // Retained for API compatibility
+    LISTP_TYPE(cp_thread)* registered = (LISTP_TYPE(cp_thread)*)arg;
+    struct cp_thread* t;
 
     if (!thread->in_vm || !thread->is_alive)
         return 0;
 
-    LISTP_FOR_EACH_ENTRY(t, registered, list)
+    LISTP_FOR_EACH_ENTRY(t, registered, list) {
         if (t->thread == thread)
             return 0;
+    }
 
     return 1;
 }
 
-int join_checkpoint (struct shim_thread * thread, IDTYPE sid)
-{
-    struct cp_session * s, * cpsession = NULL;
+int join_checkpoint(struct shim_thread* thread, IDTYPE sid) {
+    struct cp_session* s;
+    struct cp_session* cpsession = NULL;
     struct cp_thread cpthread;
-    int ret = 0;
+    int ret            = 0;
     bool do_checkpoint = false;
 
     MASTER_LOCK();
 
-    LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list)
+    LISTP_FOR_EACH_ENTRY(s, &cp_sessions, list) {
         if (s->sid == sid) {
             cpsession = s;
             break;
         }
+    }
 
     if (!cpsession) {
         MASTER_UNLOCK();
@@ -182,8 +177,7 @@ int join_checkpoint (struct shim_thread * thread, IDTYPE sid)
     LISTP_ADD_TAIL(&cpthread, &cpsession->registered_threads, list);
 
     /* find out if there is any thread that is not registered yet */
-    ret = walk_thread_list(&check_thread,
-                           &cpsession->registered_threads);
+    ret = walk_thread_list(&check_thread, &cpsession->registered_threads);
 
     if (ret == -ESRCH)
         do_checkpoint = true;
@@ -209,35 +203,30 @@ int join_checkpoint (struct shim_thread * thread, IDTYPE sid)
     return ret;
 }
 
-static void * file_alloc (struct shim_cp_store * store, void * addr, size_t size)
-{
+static void* file_alloc(struct shim_cp_store* store, void* addr, size_t size) {
     assert(store->cp_file);
-    struct shim_mount * fs = store->cp_file->fs;
+    struct shim_mount* fs = store->cp_file->fs;
 
-    if (!fs || !fs->fs_ops ||
-        !fs->fs_ops->truncate || !fs->fs_ops->mmap)
+    if (!fs || !fs->fs_ops || !fs->fs_ops->truncate || !fs->fs_ops->mmap)
         return NULL;
 
     if (fs->fs_ops->truncate(store->cp_file, size) < 0)
         return NULL;
 
-    if (fs->fs_ops->mmap(store->cp_file, &addr, size,
-                         PROT_READ|PROT_WRITE,
-                         MAP_FILE|MAP_SHARED, 0) < 0)
+    if (fs->fs_ops->mmap(store->cp_file, &addr, size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED,
+                         0) < 0)
         return NULL;
 
     return addr;
 }
 
-static int finish_checkpoint (struct cp_session * cpsession)
-{
-    struct shim_cp_store * cpstore = &cpsession->cpstore;
+static int finish_checkpoint(struct cp_session* cpsession) {
+    struct shim_cp_store* cpstore = &cpsession->cpstore;
     int ret;
 
     cpstore->alloc = file_alloc;
 
-    BEGIN_MIGRATION_DEF(checkpoint)
-    {
+    BEGIN_MIGRATION_DEF(checkpoint) {
         DEFINE_MIGRATE(process, &cur_process, sizeof(struct shim_process));
         DEFINE_MIGRATE(all_mounts, NULL, 0);
         DEFINE_MIGRATE(all_vmas, NULL, 0);
@@ -254,26 +243,25 @@ static int finish_checkpoint (struct cp_session * cpsession)
     if ((ret = START_MIGRATE(cpstore, checkpoint)) < 0)
         return ret;
 
-    struct cp_header * hdr = (struct cp_header *) cpstore->base;
-    hdr->addr = (void *) cpstore->base;
-    hdr->size = cpstore->offset;
+    struct cp_header* hdr = (struct cp_header*)cpstore->base;
+    hdr->addr             = (void*)cpstore->base;
+    hdr->size             = cpstore->offset;
 
-    DkStreamUnmap((void *) cpstore->base, cpstore->bound);
+    DkStreamUnmap((void*)cpstore->base, cpstore->bound);
 
     put_handle(cpstore->cp_file);
     return 0;
 }
 
-int shim_do_checkpoint (const char * filename)
-{
+int shim_do_checkpoint(const char* filename) {
     IDTYPE session = 0;
-    int ret = 0;
+    int ret        = 0;
 
     ret = shim_do_mkdir(filename, 0700);
     if (ret < 0)
         return ret;
 
-    shim_tcb_t * tcb = shim_get_tls();
+    shim_tcb_t* tcb = shim_get_tls();
     assert(tcb && tcb->tp);
     struct shim_signal signal;
     __store_context(tcb, NULL, &signal);

+ 42 - 51
LibOS/shim/src/sys/shim_mmap.c

@@ -20,85 +20,82 @@
  * Implementation of system call "mmap", "munmap" and "mprotect".
  */
 
-#include <stdatomic.h>
-
+#include <errno.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
+#include <shim_profile.h>
 #include <shim_table.h>
-#include <shim_handle.h>
 #include <shim_vma.h>
-#include <shim_fs.h>
-#include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
+#include <stdatomic.h>
 #include <sys/mman.h>
-#include <errno.h>
 
 DEFINE_PROFILE_OCCURENCE(mmap, memory);
 
-void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
-                     off_t offset)
-{
-    struct shim_handle * hdl = NULL;
-    long ret = 0;
+void* shim_do_mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
+    struct shim_handle* hdl = NULL;
+    long ret                = 0;
 
     /*
      * According to the manpage, both addr and offset have to be page-aligned,
      * but not the length. mmap() will automatically round up the length.
      */
     if (addr && !ALIGNED(addr))
-        return (void *) -EINVAL;
+        return (void*)-EINVAL;
 
     if (fd >= 0 && !ALIGNED(offset))
-        return (void *) -EINVAL;
+        return (void*)-EINVAL;
 
     if (!length || !access_ok(addr, length))
-        return (void*) -EINVAL;
+        return (void*)-EINVAL;
 
     if (!ALIGNED(length))
         length = ALIGN_UP(length);
 
     /* ignore MAP_32BIT when MAP_FIXED is set */
-    if ((flags & (MAP_32BIT|MAP_FIXED)) == (MAP_32BIT|MAP_FIXED))
+    if ((flags & (MAP_32BIT | MAP_FIXED)) == (MAP_32BIT | MAP_FIXED))
         flags &= ~MAP_32BIT;
 
-    assert(!(flags & (VMA_UNMAPPED|VMA_TAINTED)));
+    assert(!(flags & (VMA_UNMAPPED | VMA_TAINTED)));
 
     int pal_alloc_type = 0;
 
     if ((flags & MAP_FIXED) || addr) {
         struct shim_vma_val tmp;
 
-        if (addr < PAL_CB(user_address.start) ||
-            PAL_CB(user_address.end) <= addr ||
+        if (addr < PAL_CB(user_address.start) || PAL_CB(user_address.end) <= addr ||
             (uintptr_t)PAL_CB(user_address.end) - (uintptr_t)addr < length) {
-            debug("mmap: user specified address %p with length %lu "
-                  "not in allowed user space, ignoring this hint\n",
-                  addr, length);
+            debug(
+                "mmap: user specified address %p with length %lu "
+                "not in allowed user space, ignoring this hint\n",
+                addr, length);
             if (flags & MAP_FIXED)
-                return (void *)-EINVAL;
+                return (void*)-EINVAL;
             addr = NULL;
         } else if (!lookup_overlap_vma(addr, length, &tmp)) {
             if (flags & MAP_FIXED)
-                debug("mmap: allowing overlapping MAP_FIXED allocation at %p with length %lu\n",
-                      addr, length);
+                debug(
+                    "mmap: allowing overlapping MAP_FIXED allocation at %p with "
+                    "length %lu\n",
+                    addr, length);
             else
                 addr = NULL;
         }
     }
 
-    if ((flags & (MAP_ANONYMOUS|MAP_FILE)) == MAP_FILE) {
+    if ((flags & (MAP_ANONYMOUS | MAP_FILE)) == MAP_FILE) {
         if (fd < 0)
-            return (void *) -EINVAL;
+            return (void*)-EINVAL;
 
         hdl = get_fd_handle(fd, NULL, NULL);
         if (!hdl)
-            return (void *) -EBADF;
+            return (void*)-EBADF;
 
         if (!hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->mmap) {
             put_handle(hdl);
-            return (void *) -ENODEV;
+            return (void*)-ENODEV;
         }
     }
 
@@ -111,18 +108,17 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
          * proper space to allocate the memory, simply return failure.
          */
         if (!addr)
-            return (void *) -ENOMEM;
+            return (void*)-ENOMEM;
     }
 
     // Approximate check only, to help root out bugs.
-    void * cur_stack = current_stack();
+    void* cur_stack = current_stack();
     assert(cur_stack < addr || cur_stack > addr + length);
 
     /* addr needs to be kept for bkeep_munmap() below */
-    void * ret_addr = addr;
+    void* ret_addr = addr;
     if (!hdl) {
-        ret_addr = (void *) DkVirtualMemoryAlloc(
-            ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
+        ret_addr = (void*)DkVirtualMemoryAlloc(ret_addr, length, pal_alloc_type, PAL_PROT(prot, 0));
 
         if (!ret_addr) {
             if (PAL_NATIVE_ERRNO == PAL_ERROR_DENIED)
@@ -131,8 +127,7 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
                 ret = -PAL_ERRNO;
         }
     } else {
-        ret = hdl->fs->fs_ops->mmap(
-            hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
+        ret = hdl->fs->fs_ops->mmap(hdl, &ret_addr, length, PAL_PROT(prot, flags), flags, offset);
     }
 
     if (hdl)
@@ -140,15 +135,14 @@ void * shim_do_mmap (void * addr, size_t length, int prot, int flags, int fd,
 
     if (ret < 0) {
         bkeep_munmap(addr, length, flags);
-        return (void *) ret;
+        return (void*)ret;
     }
 
     ADD_PROFILE_OCCURENCE(mmap, length);
     return ret_addr;
 }
 
-int shim_do_mprotect (void * addr, size_t length, int prot)
-{
+int shim_do_mprotect(void* addr, size_t length, int prot) {
     /*
      * According to the manpage, addr has to be page-aligned, but not the
      * length. mprotect() will automatically round up the length.
@@ -168,8 +162,7 @@ int shim_do_mprotect (void * addr, size_t length, int prot)
     return 0;
 }
 
-int shim_do_munmap (void * addr, size_t length)
-{
+int shim_do_munmap(void* addr, size_t length) {
     /*
      * According to the manpage, addr has to be page-aligned, but not the
      * length. munmap() will automatically round up the length.
@@ -186,8 +179,7 @@ int shim_do_munmap (void * addr, size_t length)
     struct shim_vma_val vma;
 
     if (lookup_overlap_vma(addr, length, &vma) < 0) {
-        debug("can't find addr %p - %p in map, quit unmapping\n",
-              addr, addr + length);
+        debug("can't find addr %p - %p in map, quit unmapping\n", addr, addr + length);
 
         /* Really not an error */
         return -EFAULT;
@@ -214,8 +206,7 @@ int shim_do_munmap (void * addr, size_t length)
  * pessimistically due to lack of a good way to know it.
  * Possibly it may cause performance(or other) issue due to this lying.
  */
-int shim_do_mincore(void *addr, size_t len, unsigned char * vec)
-{
+int shim_do_mincore(void* addr, size_t len, unsigned char* vec) {
     if (!ALIGNED(addr))
         return -EINVAL;
 
@@ -244,14 +235,14 @@ int shim_do_mincore(void *addr, size_t len, unsigned char * vec)
     static atomic_bool warned = false;
     if (!warned) {
         warned = true;
-        warn("mincore emulation always tells pages are _NOT_ in RAM. "
-             "This may cause issues.\n");
+        warn("mincore emulation always tells pages are _NOT_ in RAM. This may cause issues.\n");
     }
 
     /* There is no good way to know if the page is in RAM.
      * Conservatively tell that it's not in RAM. */
-    for (unsigned long i = 0; i < pages; i++)
+    for (unsigned long i = 0; i < pages; i++) {
         vec[i] = 0;
+    }
 
     return 0;
 }

+ 206 - 266
LibOS/shim/src/sys/shim_msgget.c

@@ -20,54 +20,48 @@
  * Implementation of system call "msgget", "msgsnd", "msgrcv" and "msgctl".
  */
 
-#include <shim_internal.h>
+#include <errno.h>
+#include <list.h>
+#include <pal.h>
+#include <pal_error.h>
 #include <shim_handle.h>
-#include <shim_utils.h>
+#include <shim_internal.h>
 #include <shim_ipc.h>
+#include <shim_profile.h>
 #include <shim_sysv.h>
 #include <shim_unistd.h>
-#include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-#include <list.h>
-#include <errno.h>
+#include <shim_utils.h>
 
-#define MSGQ_HASH_LEN     8
-#define MSGQ_HASH_NUM     (1 << MSGQ_HASH_LEN)
-#define MSGQ_HASH_MASK    (MSGQ_HASH_NUM - 1)
-#define MSGQ_HASH(idx)    ((idx) & MSGQ_HASH_MASK)
+#define MSGQ_HASH_LEN  8
+#define MSGQ_HASH_NUM  (1 << MSGQ_HASH_LEN)
+#define MSGQ_HASH_MASK (MSGQ_HASH_NUM - 1)
+#define MSGQ_HASH(idx) ((idx) & MSGQ_HASH_MASK)
 
 /* The msgq_list links shim_msg_handle objects by the list field.
  * The msgq_key_hlist links them by key_hlist, and qid_hlist by qid_hlist */
 DEFINE_LISTP(shim_msg_handle);
 static LISTP_TYPE(shim_msg_handle) msgq_list;
-static LISTP_TYPE(shim_msg_handle) msgq_key_hlist [MSGQ_HASH_NUM];
-static LISTP_TYPE(shim_msg_handle) msgq_qid_hlist [MSGQ_HASH_NUM];
+static LISTP_TYPE(shim_msg_handle) msgq_key_hlist[MSGQ_HASH_NUM];
+static LISTP_TYPE(shim_msg_handle) msgq_qid_hlist[MSGQ_HASH_NUM];
 static struct shim_lock msgq_list_lock;
 
-static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg);
-static int __store_msg_persist(struct shim_msg_handle * msgq);
+static int __load_msg_persist(struct shim_msg_handle* msgq, bool readmsg);
+static int __store_msg_persist(struct shim_msg_handle* msgq);
 
 DEFINE_PROFILE_CATEGORY(sysv_msg, );
 
-#define MSG_TO_HANDLE(msghdl)   \
-        container_of((msghdl), struct shim_handle, info.msg)
+#define MSG_TO_HANDLE(msghdl) container_of((msghdl), struct shim_handle, info.msg)
 
-static int __add_msg_handle (unsigned long key, IDTYPE msqid, bool owned,
-                             struct shim_msg_handle ** msghdl)
-{
-    LISTP_TYPE(shim_msg_handle) * key_head = (key != IPC_PRIVATE) ?
-                                   &msgq_key_hlist[MSGQ_HASH(key)] :
-                                   NULL;
-    LISTP_TYPE(shim_msg_handle) * qid_head = msqid ?
-                                   &msgq_qid_hlist[MSGQ_HASH(msqid)] :
-                                   NULL;
+static int __add_msg_handle(unsigned long key, IDTYPE msqid, bool owned,
+                            struct shim_msg_handle** msghdl) {
+    LISTP_TYPE(shim_msg_handle)* key_head =
+        (key != IPC_PRIVATE) ? &msgq_key_hlist[MSGQ_HASH(key)] : NULL;
+    LISTP_TYPE(shim_msg_handle)* qid_head = msqid ? &msgq_qid_hlist[MSGQ_HASH(msqid)] : NULL;
 
-    struct shim_msg_handle * tmp;
+    struct shim_msg_handle* tmp;
 
     if (key_head)
-        LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist)
+        LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
             if (tmp->msqkey == key) {
                 if (tmp->msqid == msqid) {
                     if (msghdl)
@@ -76,9 +70,10 @@ static int __add_msg_handle (unsigned long key, IDTYPE msqid, bool owned,
                 }
                 return -EEXIST;
             }
+        }
 
     if (qid_head)
-        LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist)
+        LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist) {
             if (tmp->msqid == msqid) {
                 if (key)
                     tmp->msqkey = key;
@@ -86,29 +81,30 @@ static int __add_msg_handle (unsigned long key, IDTYPE msqid, bool owned,
                     *msghdl = tmp;
                 return 0;
             }
+        }
 
-    struct shim_handle * hdl = get_new_handle();
+    struct shim_handle* hdl = get_new_handle();
     if (!hdl)
         return -ENOMEM;
 
-    struct shim_msg_handle * msgq = &hdl->info.msg;
+    struct shim_msg_handle* msgq = &hdl->info.msg;
 
-    hdl->type       = TYPE_MSG;
-    msgq->msqkey    = key;
-    msgq->msqid     = msqid;
-    msgq->owned     = owned;
-    msgq->deleted   = false;
+    hdl->type         = TYPE_MSG;
+    msgq->msqkey      = key;
+    msgq->msqid       = msqid;
+    msgq->owned       = owned;
+    msgq->deleted     = false;
     msgq->currentsize = 0;
-    msgq->event     = DkSynchronizationEventCreate(PAL_FALSE);
+    msgq->event       = DkSynchronizationEventCreate(PAL_FALSE);
 
     msgq->queue     = malloc(MSG_QOBJ_SIZE * DEFAULT_MSG_QUEUE_SIZE);
     msgq->queuesize = DEFAULT_MSG_QUEUE_SIZE;
     msgq->queueused = 0;
     msgq->freed     = NULL;
 
-    msgq->ntypes    = 0;
-    msgq->maxtypes  = INIT_MSG_TYPE_SIZE;
-    msgq->types     = malloc(sizeof(struct msg_type) * INIT_MSG_TYPE_SIZE);
+    msgq->ntypes   = 0;
+    msgq->maxtypes = INIT_MSG_TYPE_SIZE;
+    msgq->types    = malloc(sizeof(struct msg_type) * INIT_MSG_TYPE_SIZE);
 
     INIT_LIST_HEAD(msgq, list);
     get_handle(hdl);
@@ -134,27 +130,26 @@ static int __add_msg_handle (unsigned long key, IDTYPE msqid, bool owned,
     return 0;
 }
 
-int add_msg_handle (unsigned long key, IDTYPE id, bool owned)
-{
+int add_msg_handle(unsigned long key, IDTYPE id, bool owned) {
     lock(&msgq_list_lock);
     int ret = __add_msg_handle(key, id, owned, NULL);
     unlock(&msgq_list_lock);
     return ret;
 }
 
-struct shim_msg_handle * get_msg_handle_by_key (unsigned long key)
-{
-
-    LISTP_TYPE(shim_msg_handle) * key_head = &msgq_key_hlist[MSGQ_HASH(key)];
-    struct shim_msg_handle * tmp, * found = NULL;
+struct shim_msg_handle* get_msg_handle_by_key(unsigned long key) {
+    LISTP_TYPE(shim_msg_handle)* key_head = &msgq_key_hlist[MSGQ_HASH(key)];
+    struct shim_msg_handle* tmp;
+    struct shim_msg_handle* found = NULL;
 
     lock(&msgq_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist)
+    LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
         if (tmp->msqkey == key) {
             found = tmp;
             break;
         }
+    }
 
     if (found)
         get_handle(MSG_TO_HANDLE(found));
@@ -163,18 +158,19 @@ struct shim_msg_handle * get_msg_handle_by_key (unsigned long key)
     return found;
 }
 
-struct shim_msg_handle * get_msg_handle_by_id (IDTYPE msqid)
-{
-    LISTP_TYPE(shim_msg_handle) * qid_head = &msgq_qid_hlist[MSGQ_HASH(msqid)];
-    struct shim_msg_handle * tmp, * found = NULL;
+struct shim_msg_handle* get_msg_handle_by_id(IDTYPE msqid) {
+    LISTP_TYPE(shim_msg_handle)* qid_head = &msgq_qid_hlist[MSGQ_HASH(msqid)];
+    struct shim_msg_handle* tmp;
+    struct shim_msg_handle* found = NULL;
 
     lock(&msgq_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist)
+    LISTP_FOR_EACH_ENTRY(tmp, qid_head, qid_hlist) {
         if (tmp->msqid == msqid) {
             found = tmp;
             break;
         }
+    }
 
     if (found)
         get_handle(MSG_TO_HANDLE(found));
@@ -183,19 +179,17 @@ struct shim_msg_handle * get_msg_handle_by_id (IDTYPE msqid)
     return found;
 }
 
-void put_msg_handle (struct shim_msg_handle * msgq)
-{
+void put_msg_handle(struct shim_msg_handle* msgq) {
     put_handle(MSG_TO_HANDLE(msgq));
 }
 
-static void * __get_msg_qobj (struct shim_msg_handle * msgq)
-{
-    struct msg_qobj * obj = NULL;
+static void* __get_msg_qobj(struct shim_msg_handle* msgq) {
+    struct msg_qobj* obj = NULL;
 
     if (msgq->freed) {
-        obj = msgq->freed;
+        obj         = msgq->freed;
         msgq->freed = obj->next;
-        obj->next = NULL;
+        obj->next   = NULL;
         return obj;
     }
 
@@ -209,24 +203,21 @@ static void * __get_msg_qobj (struct shim_msg_handle * msgq)
     return NULL;
 }
 
-static void __free_msg_qobj (struct shim_msg_handle * msgq, void * obj)
-{
-    ((struct msg_qobj *) obj)->next = msgq->freed;
-    msgq->freed = obj;
+static void __free_msg_qobj(struct shim_msg_handle* msgq, void* obj) {
+    ((struct msg_qobj*)obj)->next = msgq->freed;
+    msgq->freed                   = obj;
 }
 
-static void __free_msg_linked_qobjs (struct shim_msg_handle * msgq, void * obj)
-{
-    struct msg_qobj * qobj = obj;
+static void __free_msg_linked_qobjs(struct shim_msg_handle* msgq, void* obj) {
+    struct msg_qobj* qobj = obj;
     while (qobj) {
-        struct msg_qobj * next = qobj->next;
+        struct msg_qobj* next = qobj->next;
         __free_msg_qobj(msgq, qobj);
         qobj = next;
     }
 }
 
-static int __del_msg_handle (struct shim_msg_handle * msgq)
-{
+static int __del_msg_handle(struct shim_msg_handle* msgq) {
     if (msgq->deleted)
         return -EIDRM;
 
@@ -237,20 +228,20 @@ static int __del_msg_handle (struct shim_msg_handle * msgq)
     free(msgq->types);
     msgq->ntypes = 0;
 
-    struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
+    struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
 
     lock(&msgq_list_lock);
     LISTP_DEL_INIT(msgq, &msgq_list, list);
     put_handle(hdl);
     if (!LIST_EMPTY(msgq, key_hlist)) {
         // DEP: Yuck, re-find the head; maybe we can do better...
-        LISTP_TYPE(shim_msg_handle) * key_head = &msgq_key_hlist[MSGQ_HASH(msgq->msqkey)];
+        LISTP_TYPE(shim_msg_handle)* key_head = &msgq_key_hlist[MSGQ_HASH(msgq->msqkey)];
         LISTP_DEL_INIT(msgq, key_head, key_hlist);
         put_handle(hdl);
     }
     if (!LIST_EMPTY(msgq, qid_hlist)) {
         // DEP: Yuck, re-find the head; maybe we can do better...
-        LISTP_TYPE(shim_msg_handle) * qid_head = &msgq_qid_hlist[MSGQ_HASH(msgq->msqid)];
+        LISTP_TYPE(shim_msg_handle)* qid_head = &msgq_qid_hlist[MSGQ_HASH(msgq->msqid)];
         LISTP_DEL_INIT(msgq, qid_head, qid_hlist);
         put_handle(hdl);
     }
@@ -258,33 +249,30 @@ static int __del_msg_handle (struct shim_msg_handle * msgq)
     return 0;
 }
 
-int del_msg_handle (struct shim_msg_handle * msgq)
-{
-    struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
+int del_msg_handle(struct shim_msg_handle* msgq) {
+    struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
     lock(&hdl->lock);
     int ret = __del_msg_handle(msgq);
     unlock(&hdl->lock);
     return ret;
 }
 
-static void __try_create_lock (void)
-{
+static void __try_create_lock(void) {
     create_lock_runtime(&msgq_list_lock);
 }
 
-int shim_do_msgget (key_t key, int msgflg)
-{
+int shim_do_msgget(key_t key, int msgflg) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
     IDTYPE msgid = 0;
     int ret;
     __try_create_lock();
 
     if (key != IPC_PRIVATE) {
-        struct shim_msg_handle * msgq = get_msg_handle_by_key(key);
+        struct shim_msg_handle* msgq = get_msg_handle_by_key(key);
         if (msgq) {
             msgid = msgq->msqid;
             put_msg_handle(msgq);
-            return (msgflg & IPC_EXCL) ? -EEXIST : (int) msgid;
+            return (msgflg & IPC_EXCL) ? -EEXIST : (int)msgid;
         }
     }
 
@@ -324,9 +312,8 @@ int shim_do_msgget (key_t key, int msgflg)
     return msgid;
 }
 
-static int connect_msg_handle (int msqid, struct shim_msg_handle ** msgqp)
-{
-    struct shim_msg_handle * msgq = get_msg_handle_by_id(msqid);
+static int connect_msg_handle(int msqid, struct shim_msg_handle** msgqp) {
+    struct shim_msg_handle* msgq = get_msg_handle_by_id(msqid);
     int ret;
 
     if (!msgq) {
@@ -349,9 +336,8 @@ static int connect_msg_handle (int msqid, struct shim_msg_handle ** msgqp)
     return 0;
 }
 
-int recover_msg_ownership (struct shim_msg_handle * msgq)
-{
-    struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
+int recover_msg_ownership(struct shim_msg_handle* msgq) {
+    struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
     lock(&hdl->lock);
     assert(!msgq->owned);
     int ret = __load_msg_persist(msgq, true);
@@ -368,8 +354,7 @@ out:
     return 0;
 }
 
-int shim_do_msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg)
-{
+int shim_do_msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
     // Issue #755 - https://github.com/oscarlab/graphene/issues/755
     __UNUSED(msgflg);
@@ -382,12 +367,12 @@ int shim_do_msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg)
     if (!msgp)
         return -EFAULT;
 
-    struct __kernel_msgbuf * msgbuf = (struct __kernel_msgbuf *) msgp;
+    struct __kernel_msgbuf* msgbuf = (struct __kernel_msgbuf*)msgp;
 
     if (msgbuf->mtype < 0)
         return -EINVAL;
 
-    struct shim_msg_handle * msgq;
+    struct shim_msg_handle* msgq;
     __try_create_lock();
 
     if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
@@ -398,9 +383,7 @@ int shim_do_msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg)
     return ret;
 }
 
-int shim_do_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtype,
-                    int msgflg)
-{
+int shim_do_msgrcv(int msqid, void* msgp, size_t msgsz, long msgtype, int msgflg) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
 
     // Issue #755 - https://github.com/oscarlab/graphene/issues/755
@@ -413,8 +396,8 @@ int shim_do_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtype,
     if (!msgp)
         return -EFAULT;
 
-    struct __kernel_msgbuf * msgbuf = (struct __kernel_msgbuf *) msgp;
-    struct shim_msg_handle * msgq;
+    struct __kernel_msgbuf* msgbuf = (struct __kernel_msgbuf*)msgp;
+    struct shim_msg_handle* msgq;
     __try_create_lock();
 
     if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
@@ -425,19 +408,18 @@ int shim_do_msgrcv (int msqid, void * msgp, size_t msgsz, long msgtype,
     return ret;
 }
 
-int shim_do_msgctl (int msqid, int cmd, struct msqid_ds * buf)
-{
+int shim_do_msgctl(int msqid, int cmd, struct msqid_ds* buf) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
 
     // Issue #756 - https://github.com/oscarlab/graphene/issues/756
     __UNUSED(buf);
 
-    struct shim_msg_handle * msgq;
+    struct shim_msg_handle* msgq;
     int ret;
     __try_create_lock();
 
     if ((ret = connect_msg_handle(msqid, &msgq)) < 0)
-       return ret;
+        return ret;
 
     switch (cmd) {
         case IPC_RMID:
@@ -459,52 +441,46 @@ int shim_do_msgctl (int msqid, int cmd, struct msqid_ds * buf)
     return ret;
 }
 
-static struct msg_type *
-__add_msg_type (int type, struct msg_type ** ptypes, int * pntypes,
-                int * pmaxtypes)
-{
-    struct msg_type * types = *ptypes;
-    int ntypes = *pntypes;
-    int maxtypes = *pmaxtypes;
-
-    struct msg_type * mtype;
-    for (mtype = types ;
-         mtype < &types[ntypes] && mtype->type <= type ; mtype++)
+static struct msg_type* __add_msg_type(int type, struct msg_type** ptypes, int* pntypes,
+                                       int* pmaxtypes) {
+    struct msg_type* types = *ptypes;
+    int ntypes             = *pntypes;
+    int maxtypes           = *pmaxtypes;
+
+    struct msg_type* mtype;
+    for (mtype = types; mtype < &types[ntypes] && mtype->type <= type; mtype++)
         if (mtype->type == type)
             return mtype;
 
-    int off = mtype - types;
-    struct msg_type * new_types = types;
+    int off                    = mtype - types;
+    struct msg_type* new_types = types;
 
     if (ntypes == maxtypes)
         new_types = malloc(sizeof(struct msg_type) * maxtypes * 2);
 
     if (mtype < &types[ntypes])
-        memmove(new_types + off + 1, mtype,
-                sizeof(struct msg_type) * (ntypes - off));
+        memmove(new_types + off + 1, mtype, sizeof(struct msg_type) * (ntypes - off));
 
     if (new_types != types) {
         memcpy(new_types, types, sizeof(struct msg_type) * off);
         free(types);
-        mtype = new_types + off;
-        *ptypes = new_types;
+        mtype      = new_types + off;
+        *ptypes    = new_types;
         *pmaxtypes = maxtypes * 2;
     }
 
-    mtype->type = type;
-    mtype->msgs = NULL;
+    mtype->type     = type;
+    mtype->msgs     = NULL;
     mtype->msg_tail = NULL;
-    mtype->reqs = NULL;
+    mtype->reqs     = NULL;
     mtype->req_tail = NULL;
 
     (*pntypes)++;
     return mtype;
 }
 
-static int __load_msg_qobjs (struct shim_msg_handle * msgq,
-                             struct msg_type * mtype,
-                             struct msg_item * msg, void * data)
-{
+static int __load_msg_qobjs(struct shim_msg_handle* msgq, struct msg_type* mtype,
+                            struct msg_item* msg, void* data) {
     size_t copysize = MSG_ITEM_DATA_SIZE(msg->size);
     memcpy(data, msg->data, copysize);
     mtype->msgs = msg->next;
@@ -512,8 +488,8 @@ static int __load_msg_qobjs (struct shim_msg_handle * msgq,
 
     while (copysize < msg->size) {
         assert(mtype->msgs);
-        struct msg_ext_item * ext = (struct msg_ext_item *) mtype->msgs;
-        size_t sz = MSG_EXT_ITEM_DATA_SIZE(msg->size - copysize);
+        struct msg_ext_item* ext = (struct msg_ext_item*)mtype->msgs;
+        size_t sz                = MSG_EXT_ITEM_DATA_SIZE(msg->size - copysize);
         memcpy(data + copysize, ext->data, sz);
         copysize += sz;
         mtype->msgs = ext->next;
@@ -528,39 +504,37 @@ static int __load_msg_qobjs (struct shim_msg_handle * msgq,
     return 0;
 }
 
-static int __store_msg_qobjs (struct shim_msg_handle * msgq,
-                              struct msg_type * mtype,
-                              size_t size, const void * data)
-{
-    struct msg_item * newmsg = __get_msg_qobj(msgq);
+static int __store_msg_qobjs(struct shim_msg_handle* msgq, struct msg_type* mtype, size_t size,
+                             const void* data) {
+    struct msg_item* newmsg = __get_msg_qobj(msgq);
     if (!newmsg)
         return -EAGAIN;
 
-    struct msg_item * old_tail = mtype->msg_tail;
+    struct msg_item* old_tail = mtype->msg_tail;
 
-    newmsg->next = NULL;
-    newmsg->size = size;
+    newmsg->next    = NULL;
+    newmsg->size    = size;
     size_t copysize = MSG_ITEM_DATA_SIZE(size);
     memcpy(newmsg->data, data, copysize);
 
     if (mtype->msg_tail) {
         mtype->msg_tail->next = newmsg;
-        mtype->msg_tail = newmsg;
+        mtype->msg_tail       = newmsg;
     } else {
         assert(!mtype->msgs);
         mtype->msgs = mtype->msg_tail = newmsg;
     }
 
     while (copysize < size) {
-        struct msg_ext_item * ext = __get_msg_qobj(msgq);
+        struct msg_ext_item* ext = __get_msg_qobj(msgq);
         if (!ext)
             goto eagain;
 
         size_t sz = MSG_EXT_ITEM_DATA_SIZE(size - copysize);
         memcpy(ext->data, data + copysize, sz);
-        ext->next = NULL;
+        ext->next             = NULL;
         mtype->msg_tail->next = ext;
-        mtype->msg_tail = (struct msg_item *) ext;
+        mtype->msg_tail       = (struct msg_item*)ext;
         copysize += sz;
     }
 
@@ -579,27 +553,24 @@ eagain:
 }
 
 #if MIGRATE_SYSV_MSG == 1
-static int msg_balance_migrate (struct shim_handle * hdl,
-                                struct sysv_client * client);
-
-static struct sysv_balance_policy msg_policy  = {
-        .score_decay        = MSG_SCORE_DECAY,
-        .score_max          = MSG_SCORE_MAX,
-        .balance_threshold  = MSG_BALANCE_THRESHOLD,
-        .migrate            = &msg_balance_migrate,
-    };
+static int msg_balance_migrate(struct shim_handle* hdl, struct sysv_client* client);
+
+static struct sysv_balance_policy msg_policy = {
+    .score_decay       = MSG_SCORE_DECAY,
+    .score_max         = MSG_SCORE_MAX,
+    .balance_threshold = MSG_BALANCE_THRESHOLD,
+    .migrate           = &msg_balance_migrate,
+};
 #endif
 
 DEFINE_PROFILE_INTERVAL(add_sysv_msg, sysv_msg);
 
-int add_sysv_msg (struct shim_msg_handle * msgq,
-                  long type, size_t size, const void * data,
-                  struct sysv_client * src)
-{
+int add_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, const void* data,
+                 struct sysv_client* src) {
     BEGIN_PROFILE_INTERVAL();
 
-    struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
-    int ret = 0;
+    struct shim_handle* hdl = MSG_TO_HANDLE(msgq);
+    int ret                 = 0;
     lock(&hdl->lock);
 
     if (msgq->deleted) {
@@ -609,25 +580,21 @@ int add_sysv_msg (struct shim_msg_handle * msgq,
 
     if (!msgq->owned) {
         unlock(&hdl->lock);
-        ret = ipc_sysv_msgsnd_send(src->port, src->vmid, msgq->msqid,
-                                   type, data, size, src->seq);
+        ret = ipc_sysv_msgsnd_send(src->port, src->vmid, msgq->msqid, type, data, size, src->seq);
         goto out;
     }
 
-    struct msg_type * mtype = __add_msg_type(type, &msgq->types,
-                                             &msgq->ntypes,
-                                             &msgq->maxtypes);
+    struct msg_type* mtype = __add_msg_type(type, &msgq->types, &msgq->ntypes, &msgq->maxtypes);
 
     if ((ret = __store_msg_qobjs(msgq, mtype, size, data)) < 0)
         goto out_locked;
 
 #if MIGRATE_SYSV_MSG == 1
     if (msgq->owned)
-        __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
-                             src, MSG_SND_SCORE);
+        __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS, src, MSG_SND_SCORE);
 #endif
     DkEventSet(msgq->event);
-    ret  = 0;
+    ret = 0;
 out_locked:
     unlock(&hdl->lock);
 out:
@@ -635,37 +602,32 @@ out:
     return ret;
 }
 
-static struct msg_type *
-__find_msg_type (int type, struct msg_type * types, int ntypes)
-{
-    for (struct msg_type * mtype = types ;
-         mtype < &types[ntypes] && mtype->type <= type; mtype++)
+static struct msg_type* __find_msg_type(int type, struct msg_type* types, int ntypes) {
+    for (struct msg_type* mtype = types; mtype < &types[ntypes] && mtype->type <= type; mtype++)
         if (mtype->type == type)
             return mtype;
     return NULL;
 }
 
-static int __add_msg_req (struct shim_msg_handle * msgq,
-                          struct msg_type * mtype,
-                          int size, int flags, struct sysv_client * src)
-{
+static int __add_msg_req(struct shim_msg_handle* msgq, struct msg_type* mtype, int size, int flags,
+                         struct sysv_client* src) {
     if (msgq->deleted)
         return -EIDRM;
 
-    struct msg_req * req = __get_msg_qobj(msgq);
+    struct msg_req* req = __get_msg_qobj(msgq);
     if (!req)
         return -ENOMEM;
 
     get_ipc_port(src->port);
 
-    req->next   = NULL;
-    req->size   = size;
-    req->flags  = flags;
-    req->dest   = *src;
+    req->next  = NULL;
+    req->size  = size;
+    req->flags = flags;
+    req->dest  = *src;
 
     if (mtype->req_tail) {
         mtype->req_tail->next = req;
-        mtype->req_tail = req;
+        mtype->req_tail       = req;
     } else {
         assert(!mtype->reqs);
         mtype->reqs = mtype->req_tail = req;
@@ -676,15 +638,14 @@ static int __add_msg_req (struct shim_msg_handle * msgq,
 
 DEFINE_PROFILE_INTERVAL(get_sysv_msg, sysv_msg);
 
-int get_sysv_msg (struct shim_msg_handle * msgq,
-                  long type, size_t size, void * data, int flags,
-                  struct sysv_client * src)
-{
+int get_sysv_msg(struct shim_msg_handle* msgq, long type, size_t size, void* data, int flags,
+                 struct sysv_client* src) {
     BEGIN_PROFILE_INTERVAL();
-    int ret = 0;
-    struct shim_handle * hdl = MSG_TO_HANDLE(msgq);
-    struct msg_item * msg = NULL;
-    struct msg_type * alltypes = NULL, * mtype = NULL;
+    int ret                   = 0;
+    struct shim_handle* hdl   = MSG_TO_HANDLE(msgq);
+    struct msg_item* msg      = NULL;
+    struct msg_type* alltypes = NULL;
+    struct msg_type* mtype    = NULL;
     lock(&hdl->lock);
 
     if (msgq->deleted) {
@@ -694,14 +655,12 @@ int get_sysv_msg (struct shim_msg_handle * msgq,
 
 #if MIGRATE_SYSV_MSG == 1
     if (msgq->owned) {
-        __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS,
-                             src, MSG_RCV_SCORE);
+        __balance_sysv_score(&msg_policy, hdl, msgq->scores, MAX_SYSV_CLIENTS, src, MSG_RCV_SCORE);
 
         if (!msgq->owned && src) {
-            struct shim_ipc_info * owner = msgq->owner;
+            struct shim_ipc_info* owner = msgq->owner;
             assert(owner);
-            ret = ipc_sysv_movres_send(src, owner->vmid,
-                                       qstrgetstr(&owner->uri), msgq->lease,
+            ret = ipc_sysv_movres_send(src, owner->vmid, qstrgetstr(&owner->uri), msgq->lease,
                                        msgq->msqid, SYSV_MSGQ);
             goto out_locked;
         }
@@ -712,20 +671,17 @@ int get_sysv_msg (struct shim_msg_handle * msgq,
         IDTYPE msqid = msgq->msqid;
 
         if (src) {
-            struct shim_ipc_info * owner = msgq->owner;
-            ret = owner ?
-                  ipc_sysv_movres_send(src, owner->vmid,
-                                       qstrgetstr(&owner->uri), msgq->lease,
-                                       msgq->msqid, SYSV_MSGQ) :
-                  -ECONNREFUSED;
+            struct shim_ipc_info* owner = msgq->owner;
+            ret = owner ? ipc_sysv_movres_send(src, owner->vmid, qstrgetstr(&owner->uri),
+                                               msgq->lease, msgq->msqid, SYSV_MSGQ)
+                        : -ECONNREFUSED;
             goto out_locked;
         }
 
-unowned:
+    unowned:
         unlock(&hdl->lock);
         ret = ipc_sysv_msgrcv_send(msqid, type, flags, data, size);
-        if (ret != -EAGAIN &&
-            ret != -ECONNREFUSED)
+        if (ret != -EAGAIN && ret != -ECONNREFUSED)
             goto out;
 
         lock(&hdl->lock);
@@ -737,7 +693,7 @@ unowned:
     while (1) {
         if (alltypes != msgq->types || !mtype || mtype->type != type) {
             alltypes = msgq->types;
-            mtype = __find_msg_type(type, alltypes, msgq->ntypes);
+            mtype    = __find_msg_type(type, alltypes, msgq->ntypes);
         }
 
         if (mtype && mtype->msgs) {
@@ -753,7 +709,8 @@ unowned:
             break;
 
         unlock(&hdl->lock);
-        while (!DkObjectsWaitAny(1, &msgq->event, NO_TIMEOUT));
+        while (!DkObjectsWaitAny(1, &msgq->event, NO_TIMEOUT))
+            ;
         lock(&hdl->lock);
 
         if (!msgq->owned)
@@ -761,13 +718,13 @@ unowned:
     }
 
     if (!msg) {
-        ret = (!(flags & IPC_NOWAIT) && src) ?
-            __add_msg_req(msgq, mtype, size, flags, src) : -ENOMSG;
+        ret =
+            (!(flags & IPC_NOWAIT) && src) ? __add_msg_req(msgq, mtype, size, flags, src) : -ENOMSG;
         goto out_locked;
     }
 
     if ((ret = __load_msg_qobjs(msgq, mtype, msg, data)) < 0)
-        goto out_locked;;
+        goto out_locked;
 
     ret = msg->size;
 out_locked:
@@ -777,11 +734,10 @@ out:
     return ret;
 }
 
-static int __store_msg_persist (struct shim_msg_handle * msgq)
-{
+static int __store_msg_persist(struct shim_msg_handle* msgq) {
     int ret = 0;
 
-   if (msgq->deleted)
+    if (msgq->deleted)
         goto out;
 
     debug("store msgq %d to persistent store\n", msgq->msqid);
@@ -789,40 +745,36 @@ static int __store_msg_persist (struct shim_msg_handle * msgq)
     char fileuri[20];
     snprintf(fileuri, 20, "file:msgq.%08x", msgq->msqid);
 
-    PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDWR, 0600,
-                                   PAL_CREATE_TRY, 0);
+    PAL_HANDLE file = DkStreamOpen(fileuri, PAL_ACCESS_RDWR, 0600, PAL_CREATE_TRY, 0);
     if (!file) {
         ret = -PAL_ERRNO;
         goto out;
     }
 
-    int expected_size = sizeof(struct msg_handle_backup) +
-                        sizeof(struct msg_backup) * msgq->nmsgs +
+    int expected_size = sizeof(struct msg_handle_backup) + sizeof(struct msg_backup) * msgq->nmsgs +
                         msgq->currentsize;
 
     if (DkStreamSetLength(file, expected_size))
         goto err_file;
 
-    void * mem = (void *) DkStreamMap(file, NULL,
-                                      PAL_PROT_READ|PAL_PROT_WRITE,
-                                      0, ALIGN_UP(expected_size));
+    void* mem =
+        (void*)DkStreamMap(file, NULL, PAL_PROT_READ | PAL_PROT_WRITE, 0, ALIGN_UP(expected_size));
     if (!mem) {
         ret = -EFAULT;
         goto err_file;
     }
 
-    struct msg_handle_backup * mback = mem;
+    struct msg_handle_backup* mback = mem;
     mem += sizeof(struct msg_handle_backup);
 
     mback->perm        = msgq->perm;
     mback->nmsgs       = msgq->nmsgs;
     mback->currentsize = msgq->currentsize;
 
-    struct msg_type * mtype;
-    for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
-         mtype++) {
+    struct msg_type* mtype;
+    for (mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
         while (mtype->msgs) {
-            struct msg_backup * msg = mem;
+            struct msg_backup* msg = mem;
             mem += sizeof(struct msg_backup) + mtype->msgs->size;
 
             msg->type = mtype->type;
@@ -836,13 +788,12 @@ static int __store_msg_persist (struct shim_msg_handle * msgq)
     DkStreamUnmap(mem, ALIGN_UP(expected_size));
 
     if (msgq->owned)
-        for (mtype = msgq->types ; mtype < &msgq->types[msgq->ntypes] ;
-             mtype++) {
-            struct msg_req * req = mtype->reqs;
+        for (mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
+            struct msg_req* req = mtype->reqs;
             mtype->reqs = mtype->req_tail = NULL;
             while (req) {
-                struct sysv_client * c = &req->dest;
-                struct msg_req * next = req->next;
+                struct sysv_client* c = &req->dest;
+                struct msg_req* next  = req->next;
 
                 send_response_ipc_message(c->port, c->vmid, -EIDRM, c->seq);
 
@@ -853,7 +804,7 @@ static int __store_msg_persist (struct shim_msg_handle * msgq)
         }
 
     msgq->owned = false;
-    ret = 0;
+    ret         = 0;
     goto out;
 
 err_file:
@@ -867,8 +818,7 @@ out:
     return ret;
 }
 
-static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
-{
+static int __load_msg_persist(struct shim_msg_handle* msgq, bool readmsg) {
     int ret = 0;
 
     char fileuri[20];
@@ -881,8 +831,7 @@ static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
 
     struct msg_handle_backup mback;
 
-    size_t bytes = DkStreamRead(file, 0, sizeof(struct msg_handle_backup),
-                                &mback, NULL, 0);
+    size_t bytes = DkStreamRead(file, 0, sizeof(struct msg_handle_backup), &mback, NULL, 0);
 
     if (bytes < sizeof(struct msg_handle_backup)) {
         ret = bytes ? -EFAULT : -PAL_ERRNO;
@@ -894,12 +843,10 @@ static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
     if (!readmsg || !mback.nmsgs)
         goto done;
 
-    int expected_size = sizeof(struct msg_handle_backup) +
-                        sizeof(struct msg_backup) * mback.nmsgs +
+    int expected_size = sizeof(struct msg_handle_backup) + sizeof(struct msg_backup) * mback.nmsgs +
                         mback.currentsize;
 
-    void * mem = (void *) DkStreamMap(file, NULL, PAL_PROT_READ, 0,
-                                      ALIGN_UP(expected_size));
+    void* mem = (void*)DkStreamMap(file, NULL, PAL_PROT_READ, 0, ALIGN_UP(expected_size));
 
     if (!mem) {
         ret = -PAL_ERRNO;
@@ -908,16 +855,15 @@ static int __load_msg_persist (struct shim_msg_handle * msgq, bool readmsg)
 
     mem += sizeof(struct msg_handle_backup);
 
-    struct msg_type * mtype = NULL;
-    for (int i = 0 ; i < mback.nmsgs ; i++) {
-        struct msg_backup * m = mem;
+    struct msg_type* mtype = NULL;
+    for (int i = 0; i < mback.nmsgs; i++) {
+        struct msg_backup* m = mem;
         mem += sizeof(struct msg_backup) + m->size;
 
         debug("load msg: type=%ld, size=%d\n", m->type, m->size);
 
         if (!mtype || mtype->type != m->type)
-            mtype = __add_msg_type(m->type, &msgq->types, &msgq->ntypes,
-                                   &msgq->maxtypes);
+            mtype = __add_msg_type(m->type, &msgq->types, &msgq->ntypes, &msgq->maxtypes);
 
         if ((ret = __store_msg_qobjs(msgq, mtype, m->size, m->data)) < 0)
             goto out;
@@ -935,29 +881,28 @@ out:
     return ret;
 }
 
-int store_all_msg_persist (void)
-{
-    struct shim_msg_handle * msgq, *n;
+int store_all_msg_persist(void) {
+    struct shim_msg_handle* msgq;
+    struct shim_msg_handle* n;
 
     lock(&msgq_list_lock);
 
-    LISTP_FOR_EACH_ENTRY_SAFE(msgq, n, &msgq_list, list)
+    LISTP_FOR_EACH_ENTRY_SAFE(msgq, n, &msgq_list, list) {
         if (msgq->owned) {
-            struct shim_handle * hdl = container_of(msgq, struct shim_handle,
-                                                    info.msg);
+            struct shim_handle* hdl = container_of(msgq, struct shim_handle, info.msg);
             lock(&hdl->lock);
             __store_msg_persist(msgq);
             unlock(&hdl->lock);
         }
+    }
 
     unlock(&msgq_list_lock);
     return 0;
 }
 
-int shim_do_msgpersist (int msqid, int cmd)
-{
-    struct shim_msg_handle * msgq;
-    struct shim_handle * hdl;
+int shim_do_msgpersist(int msqid, int cmd) {
+    struct shim_msg_handle* msgq;
+    struct shim_handle* hdl;
     int ret = -EINVAL;
 
     switch (cmd) {
@@ -987,41 +932,36 @@ int shim_do_msgpersist (int msqid, int cmd)
 }
 
 #if MIGRATE_SYSV_MSG == 1
-static int msg_balance_migrate (struct shim_handle * hdl,
-                                struct sysv_client * src)
-{
-    struct shim_msg_handle * msgq = &hdl->info.msg;
-    int ret = 0;
+static int msg_balance_migrate(struct shim_handle* hdl, struct sysv_client* src) {
+    struct shim_msg_handle* msgq = &hdl->info.msg;
+    int ret                      = 0;
 
     debug("trigger msg queue balancing, migrate to process %u\n", src->vmid);
 
     if ((ret = __store_msg_persist(msgq)) < 0)
         return 0;
 
-    struct shim_ipc_info * info = lookup_ipc_info(src->vmid);
+    struct shim_ipc_info* info = lookup_ipc_info(src->vmid);
     if (!info)
         goto failed;
 
-    ipc_sysv_sublease_send(src->vmid, msgq->msqid,
-                           qstrgetstr(&info->uri),
-                           &msgq->lease);
+    ipc_sysv_sublease_send(src->vmid, msgq->msqid, qstrgetstr(&info->uri), &msgq->lease);
 
-    ret = ipc_sysv_msgmov_send(src->port, src->vmid, msgq->msqid, msgq->lease,
-                               msgq->scores, MAX_SYSV_CLIENTS);
+    ret = ipc_sysv_msgmov_send(src->port, src->vmid, msgq->msqid, msgq->lease, msgq->scores,
+                               MAX_SYSV_CLIENTS);
     if (ret < 0)
         goto failed_info;
 
     msgq->owner = info;
 
-    for (struct msg_type * mtype = msgq->types ;
-         mtype < &msgq->types[msgq->ntypes] ; mtype++) {
-        struct msg_req * req = mtype->reqs;
+    for (struct msg_type* mtype = msgq->types; mtype < &msgq->types[msgq->ntypes]; mtype++) {
+        struct msg_req* req = mtype->reqs;
         mtype->reqs = mtype->req_tail = NULL;
         while (req) {
-            struct msg_req * next = req->next;
+            struct msg_req* next = req->next;
 
-            ipc_sysv_movres_send(&req->dest, info->vmid, qstrgetstr(&info->uri),
-                                 msgq->lease, msgq->msqid, SYSV_MSGQ);
+            ipc_sysv_movres_send(&req->dest, info->vmid, qstrgetstr(&info->uri), msgq->lease,
+                                 msgq->msqid, SYSV_MSGQ);
 
             put_ipc_port(req->dest.port);
             __free_msg_qobj(msgq, req);

+ 54 - 64
LibOS/shim/src/sys/shim_pipe.c

@@ -20,34 +20,29 @@
  * Implementation of system call "pipe", "pipe2" and "socketpair".
  */
 
-#include <shim_internal.h>
-#include <shim_utils.h>
-#include <shim_table.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-
+#include <asm/fcntl.h>
+#include <errno.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_table.h>
+#include <shim_utils.h>
 
-#include <errno.h>
-
-#include <asm/fcntl.h>
-
-int create_pipes (IDTYPE * pipeid, PAL_HANDLE * srv, PAL_HANDLE * cli,
-                  struct shim_qstr * qstr, int flags)
-{
+int create_pipes(IDTYPE* pipeid, PAL_HANDLE* srv, PAL_HANDLE* cli, struct shim_qstr* qstr,
+                 int flags) {
     PAL_HANDLE hdl0 = NULL, hdl1 = NULL, hdl2 = NULL;
     int ret = 0;
     char uri[PIPE_URI_SIZE];
 
-    if ((ret = create_pipe(pipeid, uri, PIPE_URI_SIZE, &hdl0,
-                           qstr, /*use_vmid_for_name=*/false)) < 0) {
+    if ((ret = create_pipe(pipeid, uri, PIPE_URI_SIZE, &hdl0, qstr,
+                           /*use_vmid_for_name=*/false)) < 0) {
         debug("pipe creation failure\n");
         return ret;
     }
 
-    if (!(hdl2 = DkStreamOpen(uri, 0, 0, 0,
-                              flags & O_NONBLOCK))) {
+    if (!(hdl2 = DkStreamOpen(uri, 0, 0, 0, flags & O_NONBLOCK))) {
         ret = -PAL_ERRNO;
         debug("pipe connection failure\n");
         goto err;
@@ -74,50 +69,48 @@ err:
     return ret;
 }
 
-int shim_do_pipe2 (int * filedes, int flags)
-{
+int shim_do_pipe2(int* filedes, int flags) {
     if (!filedes || test_user_memory(filedes, 2 * sizeof(int), true))
         return -EFAULT;
 
     int ret = 0;
 
-    struct shim_handle * hdl1 = get_new_handle();
-    struct shim_handle * hdl2 = get_new_handle();
+    struct shim_handle* hdl1 = get_new_handle();
+    struct shim_handle* hdl2 = get_new_handle();
 
     if (!hdl1 || !hdl2) {
         ret = -ENOMEM;
         goto out;
     }
 
-    hdl1->type       = TYPE_PIPE;
+    hdl1->type = TYPE_PIPE;
     set_handle_fs(hdl1, &pipe_builtin_fs);
-    hdl1->flags      = O_RDONLY;
-    hdl1->acc_mode   = MAY_READ;
+    hdl1->flags    = O_RDONLY;
+    hdl1->acc_mode = MAY_READ;
 
-    hdl2->type       = TYPE_PIPE;
+    hdl2->type = TYPE_PIPE;
     set_handle_fs(hdl2, &pipe_builtin_fs);
-    hdl2->flags      = O_WRONLY;
-    hdl2->acc_mode   = MAY_WRITE;
+    hdl2->flags    = O_WRONLY;
+    hdl2->acc_mode = MAY_WRITE;
 
-    if ((ret = create_pipes(&hdl1->info.pipe.pipeid,
-                            &hdl1->pal_handle, &hdl2->pal_handle,
+    if ((ret = create_pipes(&hdl1->info.pipe.pipeid, &hdl1->pal_handle, &hdl2->pal_handle,
                             &hdl1->uri, flags)) < 0)
         goto out;
 
     qstrcopy(&hdl2->uri, &hdl2->uri);
 
-    flags = flags & O_CLOEXEC ? FD_CLOEXEC : 0;
+    flags    = flags & O_CLOEXEC ? FD_CLOEXEC : 0;
     int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
     int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
 
     if (vfd1 < 0 || vfd2 < 0) {
         if (vfd1 >= 0) {
-            struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
+            struct shim_handle* tmp = detach_fd_handle(vfd1, NULL, NULL);
             if (tmp)
                 put_handle(tmp);
         }
         if (vfd2 >= 0) {
-            struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
+            struct shim_handle* tmp = detach_fd_handle(vfd2, NULL, NULL);
             if (tmp)
                 put_handle(tmp);
         }
@@ -134,13 +127,11 @@ out:
     return ret;
 }
 
-int shim_do_pipe (int * filedes)
-{
+int shim_do_pipe(int* filedes) {
     return shim_do_pipe2(filedes, 0);
 }
 
-int shim_do_socketpair (int domain, int type, int protocol, int * sv)
-{
+int shim_do_socketpair(int domain, int type, int protocol, int* sv) {
     if (domain != AF_UNIX)
         return -EAFNOSUPPORT;
 
@@ -150,56 +141,55 @@ int shim_do_socketpair (int domain, int type, int protocol, int * sv)
     if (!sv || test_user_memory(sv, 2 * sizeof(int), true))
         return -EFAULT;
 
-    int ret = 0;
-    struct shim_handle * hdl1 = get_new_handle();
-    struct shim_handle * hdl2 = get_new_handle();
+    int ret                  = 0;
+    struct shim_handle* hdl1 = get_new_handle();
+    struct shim_handle* hdl2 = get_new_handle();
 
     if (!hdl1 || !hdl2) {
         ret = -ENOMEM;
         goto out;
     }
 
-    struct shim_sock_handle * sock1 = &hdl1->info.sock;
-    struct shim_sock_handle * sock2 = &hdl2->info.sock;
+    struct shim_sock_handle* sock1 = &hdl1->info.sock;
+    struct shim_sock_handle* sock2 = &hdl2->info.sock;
 
-    hdl1->type          = TYPE_SOCK;
+    hdl1->type = TYPE_SOCK;
     set_handle_fs(hdl1, &socket_builtin_fs);
-    hdl1->flags         = O_RDONLY;
-    hdl1->acc_mode      = MAY_READ|MAY_WRITE;
-    sock1->domain       = domain;
-    sock1->sock_type    = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
-    sock1->protocol     = protocol;
-    sock1->sock_state   = SOCK_ACCEPTED;
-
-    hdl2->type          = TYPE_SOCK;
+    hdl1->flags       = O_RDONLY;
+    hdl1->acc_mode    = MAY_READ | MAY_WRITE;
+    sock1->domain     = domain;
+    sock1->sock_type  = type & ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
+    sock1->protocol   = protocol;
+    sock1->sock_state = SOCK_ACCEPTED;
+
+    hdl2->type = TYPE_SOCK;
     set_handle_fs(hdl2, &socket_builtin_fs);
-    hdl1->flags         = O_WRONLY;
-    hdl2->acc_mode      = MAY_READ|MAY_WRITE;
-    sock2->domain       = domain;
-    sock2->sock_type    = type & ~(SOCK_NONBLOCK|SOCK_CLOEXEC);
-    sock2->protocol     = protocol;
-    sock2->sock_state   = SOCK_CONNECTED;
-
-    if ((ret = create_pipes(&sock1->addr.un.pipeid, &hdl1->pal_handle,
-                            &hdl2->pal_handle, &hdl1->uri,
-                            type & SOCK_NONBLOCK ? O_NONBLOCK : 0)) < 0)
+    hdl1->flags       = O_WRONLY;
+    hdl2->acc_mode    = MAY_READ | MAY_WRITE;
+    sock2->domain     = domain;
+    sock2->sock_type  = type & ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
+    sock2->protocol   = protocol;
+    sock2->sock_state = SOCK_CONNECTED;
+
+    if ((ret = create_pipes(&sock1->addr.un.pipeid, &hdl1->pal_handle, &hdl2->pal_handle,
+                            &hdl1->uri, type & SOCK_NONBLOCK ? O_NONBLOCK : 0)) < 0)
         goto out;
 
     sock2->addr.un.pipeid = sock1->addr.un.pipeid;
     qstrcopy(&hdl2->uri, &hdl1->uri);
 
     int flags = type & SOCK_CLOEXEC ? FD_CLOEXEC : 0;
-    int vfd1 = set_new_fd_handle(hdl1, flags, NULL);
-    int vfd2 = set_new_fd_handle(hdl2, flags, NULL);
+    int vfd1  = set_new_fd_handle(hdl1, flags, NULL);
+    int vfd2  = set_new_fd_handle(hdl2, flags, NULL);
 
     if (vfd1 < 0 || vfd2 < 0) {
         if (vfd1 >= 0) {
-            struct shim_handle * tmp = detach_fd_handle(vfd1, NULL, NULL);
+            struct shim_handle* tmp = detach_fd_handle(vfd1, NULL, NULL);
             if (tmp)
                 put_handle(tmp);
         }
         if (vfd2 >= 0) {
-            struct shim_handle * tmp = detach_fd_handle(vfd2, NULL, NULL);
+            struct shim_handle* tmp = detach_fd_handle(vfd2, NULL, NULL);
             if (tmp)
                 put_handle(tmp);
         }

+ 9 - 13
LibOS/shim/src/sys/shim_sched.c

@@ -20,22 +20,18 @@
  * Implementation of system call "sched_yield".
  */
 
-#include <shim_internal.h>
-#include <shim_table.h>
 #include <api.h>
-#include <pal.h>
-
 #include <errno.h>
+#include <pal.h>
+#include <shim_internal.h>
+#include <shim_table.h>
 
-int shim_do_sched_yield (void)
-{
+int shim_do_sched_yield(void) {
     DkThreadYieldExecution();
     return 0;
 }
 
-int shim_do_sched_getaffinity (pid_t pid, size_t len,
-                               __kernel_cpu_set_t * user_mask_ptr)
-{
+int shim_do_sched_getaffinity(pid_t pid, size_t len, __kernel_cpu_set_t* user_mask_ptr) {
     __UNUSED(pid);
     int ncpus = PAL_CB(cpu_info.cpu_num);
 
@@ -45,8 +41,7 @@ int shim_do_sched_getaffinity (pid_t pid, size_t len,
 
     /* Linux kernel bitmap is based on long. So according to its
      * implementation, round up the result to sizeof(long) */
-    size_t bitmask_long_count = (ncpus + sizeof(long) * 8 - 1) /
-                                (sizeof(long) * 8);
+    size_t bitmask_long_count    = (ncpus + sizeof(long) * 8 - 1) / (sizeof(long) * 8);
     size_t bitmask_size_in_bytes = bitmask_long_count * sizeof(long);
     if (len < bitmask_size_in_bytes)
         return -EINVAL;
@@ -55,8 +50,9 @@ int shim_do_sched_getaffinity (pid_t pid, size_t len,
         return -EINVAL;
 
     memset(user_mask_ptr, 0, len);
-    for (int i = 0 ; i < ncpus ; i++)
-        ((uint8_t *) user_mask_ptr)[i / 8] |= 1 << (i % 8);
+    for (int i = 0; i < ncpus; i++) {
+        ((uint8_t*)user_mask_ptr)[i / 8] |= 1 << (i % 8);
+    }
     /* imitate the Linux kernel implementation
      * See SYSCALL_DEFINE3(sched_getaffinity) */
     return bitmask_size_in_bytes;

+ 200 - 249
LibOS/shim/src/sys/shim_semget.c

@@ -20,71 +20,67 @@
  * Implementation of system call "semget", "semop", "semtimedop" and "semctl".
  */
 
-#include <shim_internal.h>
-#include <shim_table.h>
-#include <shim_utils.h>
+#include <errno.h>
+#include <list.h>
+#include <pal.h>
+#include <pal_error.h>
 #include <shim_handle.h>
+#include <shim_internal.h>
 #include <shim_ipc.h>
-#include <shim_sysv.h>
 #include <shim_profile.h>
+#include <shim_sysv.h>
+#include <shim_table.h>
+#include <shim_utils.h>
 
-#include <pal.h>
-#include <pal_error.h>
-#include <list.h>
-
-#include <errno.h>
-
-#define SEM_HASH_LEN        8
-#define SEM_HASH_NUM        (1 << SEM_HASH_LEN)
-#define SEM_HASH_MASK       (SEM_HASH_NUM - 1)
-#define SEM_HASH(idx)       ((idx) & SEM_HASH_MASK)
+#define SEM_HASH_LEN  8
+#define SEM_HASH_NUM  (1 << SEM_HASH_LEN)
+#define SEM_HASH_MASK (SEM_HASH_NUM - 1)
+#define SEM_HASH(idx) ((idx) & SEM_HASH_MASK)
 
 /* The sem_list links shim_sem_handle objects by the list field.
  * The sem_key_hlist links them by key_hlist, and qid_hlist by qid_hlist */
 DEFINE_LISTP(shim_sem_handle);
 static LISTP_TYPE(shim_sem_handle) sem_list;
-static LISTP_TYPE(shim_sem_handle) sem_key_hlist [SEM_HASH_NUM];
-static LISTP_TYPE(shim_sem_handle) sem_sid_hlist [SEM_HASH_NUM];
+static LISTP_TYPE(shim_sem_handle) sem_key_hlist[SEM_HASH_NUM];
+static LISTP_TYPE(shim_sem_handle) sem_sid_hlist[SEM_HASH_NUM];
 static struct shim_lock sem_list_lock;
 
 DEFINE_PROFILE_CATEGORY(sysv_sem, );
 
-#define SEM_TO_HANDLE(semhdl) \
-        container_of((semhdl), struct shim_handle, info.sem)
+#define SEM_TO_HANDLE(semhdl) container_of((semhdl), struct shim_handle, info.sem)
 
-static int __add_sem_handle (unsigned long key, IDTYPE semid,
-                             int nsems, bool owned,
-                             struct shim_sem_handle ** semhdl)
-{
-    LISTP_TYPE(shim_sem_handle) * key_head = (key != IPC_PRIVATE) ?
-                                   &sem_key_hlist[SEM_HASH(key)] : NULL;
-    LISTP_TYPE(shim_sem_handle) * sid_head = semid ?
-                                   &sem_sid_hlist[SEM_HASH(semid)] : NULL;
+static int __add_sem_handle(unsigned long key, IDTYPE semid, int nsems, bool owned,
+                            struct shim_sem_handle** semhdl) {
+    LISTP_TYPE(shim_sem_handle)* key_head =
+        (key != IPC_PRIVATE) ? &sem_key_hlist[SEM_HASH(key)] : NULL;
+    LISTP_TYPE(shim_sem_handle)* sid_head = semid ? &sem_sid_hlist[SEM_HASH(semid)] : NULL;
 
-    struct shim_sem_handle * tmp;
+    struct shim_sem_handle* tmp;
     int ret = 0;
 
     if (key_head)
-        LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist)
+        LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
             if (tmp->semkey == key) {
                 if (tmp->semid == semid)
                     goto out;
                 return -EEXIST;
             }
+        }
 
     if (sid_head)
-        LISTP_FOR_EACH_ENTRY(tmp, sid_head, sid_hlist)
+        LISTP_FOR_EACH_ENTRY(tmp, sid_head, sid_hlist) {
             if (tmp->semid == semid) {
                 if (key)
                     tmp->semkey = key;
                 goto out;
             }
+        }
 
-    struct shim_handle * hdl = get_new_handle();
+    struct shim_handle* hdl = get_new_handle();
     if (!hdl)
         return -ENOMEM;
 
-    tmp = &hdl->info.sem;
+    tmp         = &hdl->info.sem;
     hdl->type   = TYPE_SEM;
     tmp->semkey = key;
     tmp->semid  = semid;
@@ -92,18 +88,18 @@ static int __add_sem_handle (unsigned long key, IDTYPE semid,
     tmp->event  = DkNotificationEventCreate(PAL_FALSE);
 
     if (owned && nsems) {
-        tmp->nsems  = nsems;
-        tmp->sems   = malloc(sizeof(struct sem_obj) * nsems);
+        tmp->nsems = nsems;
+        tmp->sems  = malloc(sizeof(struct sem_obj) * nsems);
         if (!tmp->sems) {
             ret = -ENOMEM;
             goto failed;
         }
 
-        for (int i = 0 ; i < nsems ; i++) {
-            tmp->sems[i].num = i;
-            tmp->sems[i].val = 0;
+        for (int i = 0; i < nsems; i++) {
+            tmp->sems[i].num         = i;
+            tmp->sems[i].val         = 0;
             tmp->sems[i].host_sem_id = 0;
-            tmp->sems[i].host_sem = NULL;
+            tmp->sems[i].host_sem    = NULL;
             INIT_LISTP(&tmp->sems[i].ops);
             INIT_LISTP(&tmp->sems[i].next_ops);
         }
@@ -137,26 +133,26 @@ failed:
     return ret;
 }
 
-int add_sem_handle (unsigned long key, IDTYPE id, int nsems, bool owned)
-{
+int add_sem_handle(unsigned long key, IDTYPE id, int nsems, bool owned) {
     lock(&sem_list_lock);
     int ret = __add_sem_handle(key, id, nsems, owned, NULL);
     unlock(&sem_list_lock);
     return ret;
 }
 
-struct shim_sem_handle * get_sem_handle_by_key (unsigned long key)
-{
-    LISTP_TYPE(shim_sem_handle) * key_head = &sem_key_hlist[SEM_HASH(key)];
-    struct shim_sem_handle * tmp, * found = NULL;
+struct shim_sem_handle* get_sem_handle_by_key(unsigned long key) {
+    LISTP_TYPE(shim_sem_handle)* key_head = &sem_key_hlist[SEM_HASH(key)];
+    struct shim_sem_handle* tmp;
+    struct shim_sem_handle* found = NULL;
 
     lock(&sem_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist)
+    LISTP_FOR_EACH_ENTRY(tmp, key_head, key_hlist) {
         if (tmp->semkey == key) {
             found = tmp;
             break;
         }
+    }
 
     if (found)
         get_handle(SEM_TO_HANDLE(found));
@@ -165,18 +161,19 @@ struct shim_sem_handle * get_sem_handle_by_key (unsigned long key)
     return found;
 }
 
-struct shim_sem_handle * get_sem_handle_by_id (IDTYPE semid)
-{
-    LISTP_TYPE(shim_sem_handle) * sid_head = &sem_sid_hlist[SEM_HASH(semid)];
-    struct shim_sem_handle * tmp, * found = NULL;
+struct shim_sem_handle* get_sem_handle_by_id(IDTYPE semid) {
+    LISTP_TYPE(shim_sem_handle)* sid_head = &sem_sid_hlist[SEM_HASH(semid)];
+    struct shim_sem_handle* tmp;
+    struct shim_sem_handle* found = NULL;
 
     lock(&sem_list_lock);
 
-    LISTP_FOR_EACH_ENTRY(tmp, sid_head, sid_hlist)
+    LISTP_FOR_EACH_ENTRY(tmp, sid_head, sid_hlist) {
         if (tmp->semid == semid) {
             found = tmp;
             break;
         }
+    }
 
     if (found)
         get_handle(SEM_TO_HANDLE(found));
@@ -185,32 +182,30 @@ struct shim_sem_handle * get_sem_handle_by_id (IDTYPE semid)
     return found;
 }
 
-void put_sem_handle (struct shim_sem_handle * sem)
-{
+void put_sem_handle(struct shim_sem_handle* sem) {
     put_handle(SEM_TO_HANDLE(sem));
 }
 
-static int __del_sem_handle (struct shim_sem_handle * sem)
-{
+static int __del_sem_handle(struct shim_sem_handle* sem) {
     if (sem->deleted)
         return 0;
 
     sem->deleted = true;
 
-    struct shim_handle * hdl = SEM_TO_HANDLE(sem);
+    struct shim_handle* hdl = SEM_TO_HANDLE(sem);
 
     lock(&sem_list_lock);
     LISTP_DEL_INIT(sem, &sem_list, list);
     put_handle(hdl);
     if (!LIST_EMPTY(sem, key_hlist)) {
         // DEP: Yuck
-        LISTP_TYPE(shim_sem_handle) * key_head = &sem_key_hlist[SEM_HASH(sem->semkey)];
+        LISTP_TYPE(shim_sem_handle)* key_head = &sem_key_hlist[SEM_HASH(sem->semkey)];
         LISTP_DEL_INIT(sem, key_head, key_hlist);
         put_handle(hdl);
     }
     if (!LIST_EMPTY(sem, sid_hlist)) {
         // DEP: Yuck
-        LISTP_TYPE(shim_sem_handle) * sid_head = &sem_sid_hlist[SEM_HASH(sem->semid)];
+        LISTP_TYPE(shim_sem_handle)* sid_head = &sem_sid_hlist[SEM_HASH(sem->semid)];
         LISTP_DEL_INIT(sem, sid_head, sid_hlist);
         put_handle(hdl);
     }
@@ -218,33 +213,30 @@ static int __del_sem_handle (struct shim_sem_handle * sem)
     return 0;
 }
 
-int del_sem_handle (struct shim_sem_handle * sem)
-{
-    struct shim_handle * hdl = SEM_TO_HANDLE(sem);
+int del_sem_handle(struct shim_sem_handle* sem) {
+    struct shim_handle* hdl = SEM_TO_HANDLE(sem);
     lock(&hdl->lock);
     int ret = del_sem_handle(sem);
     unlock(&hdl->lock);
     return ret;
 }
 
-static void __try_create_lock (void)
-{
+static void __try_create_lock(void) {
     create_lock_runtime(&sem_list_lock);
 }
 
-int shim_do_semget (key_t key, int nsems, int semflg)
-{
+int shim_do_semget(key_t key, int nsems, int semflg) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
     IDTYPE semid = 0;
     int ret;
     __try_create_lock();
 
     if (key != IPC_PRIVATE) {
-        struct shim_sem_handle * sem = get_sem_handle_by_key(key);
+        struct shim_sem_handle* sem = get_sem_handle_by_key(key);
         if (sem) {
             semid = sem->semid;
             put_sem_handle(sem);
-            return (semflg & IPC_EXCL) ? -EEXIST : (int) semid;
+            return (semflg & IPC_EXCL) ? -EEXIST : (int)semid;
         }
     }
 
@@ -279,10 +271,8 @@ int shim_do_semget (key_t key, int nsems, int semflg)
     return semid;
 }
 
-static int connect_sem_handle (int semid, int nsems,
-                               struct shim_sem_handle ** semp)
-{
-    struct shim_sem_handle * sem = get_sem_handle_by_id(semid);
+static int connect_sem_handle(int semid, int nsems, struct shim_sem_handle** semp) {
+    struct shim_sem_handle* sem = get_sem_handle_by_id(semid);
     int ret;
 
     if (!sem) {
@@ -302,11 +292,9 @@ static int connect_sem_handle (int semid, int nsems,
     return 0;
 }
 
-int recover_sem_ownership (struct shim_sem_handle * sem,
-                           struct sem_backup * backups, int nbackups,
-                           struct sem_client_backup * clients, int nclients)
-{
-    struct shim_handle * hdl = SEM_TO_HANDLE(sem);
+int recover_sem_ownership(struct shim_sem_handle* sem, struct sem_backup* backups, int nbackups,
+                          struct sem_client_backup* clients, int nclients) {
+    struct shim_handle* hdl = SEM_TO_HANDLE(sem);
     lock(&hdl->lock);
     assert(!sem->owned);
     assert(!sem->nsems && !sem->sems);
@@ -315,7 +303,7 @@ int recover_sem_ownership (struct shim_sem_handle * sem,
     if (!sem->sems && !(sem->sems = malloc(sizeof(struct sem_obj) * nbackups)))
         goto out;
 
-    for (int i = 0 ; i < nbackups ; i++) {
+    for (int i = 0; i < nbackups; i++) {
         sem->sems[i].num  = i;
         sem->sems[i].val  = backups[i].val;
         sem->sems[i].zcnt = backups[i].zcnt;
@@ -325,19 +313,19 @@ int recover_sem_ownership (struct shim_sem_handle * sem,
         INIT_LISTP(&sem->sems[i].next_ops);
     }
 
-    for (int i = 0 ; i < nclients ; i++) {
-        struct sem_ops * op = malloc(sizeof(struct sem_ops));
+    for (int i = 0; i < nclients; i++) {
+        struct sem_ops* op = malloc(sizeof(struct sem_ops));
         if (!op)
             continue;
 
         op->stat.completed = false;
-        op->stat.failed = false;
-        op->stat.nops = clients[i].nops;
-        op->stat.current = clients[i].current;
-        op->stat.timeout = -1;
-        op->client.vmid = clients[i].vmid;
-        op->client.port = NULL;
-        op->client.seq  = clients[i].seq;
+        op->stat.failed    = false;
+        op->stat.nops      = clients[i].nops;
+        op->stat.current   = clients[i].current;
+        op->stat.timeout   = -1;
+        op->client.vmid    = clients[i].vmid;
+        op->client.port    = NULL;
+        op->client.seq     = clients[i].seq;
         INIT_LIST_HEAD(op, progress);
         LISTP_ADD_TAIL(op, &sem->migrated, progress);
     }
@@ -349,14 +337,12 @@ out:
     return 0;
 }
 
-static int __do_semop (int semid, struct sembuf * sops, unsigned int nsops,
-                       unsigned long timeout)
-{
+static int __do_semop(int semid, struct sembuf* sops, unsigned int nsops, unsigned long timeout) {
     int ret;
-    struct shim_sem_handle * sem;
+    struct shim_sem_handle* sem;
     size_t nsems = 0;
 
-    for (size_t i = 0 ; i < nsops ; i++)
+    for (size_t i = 0; i < nsops; i++)
         if (sops[i].sem_num >= nsems)
             nsems = sops[i].sem_num + 1;
 
@@ -370,31 +356,27 @@ static int __do_semop (int semid, struct sembuf * sops, unsigned int nsops,
     return ret;
 }
 
-int shim_do_semop (int semid, struct sembuf * sops, unsigned int nsops)
-{
+int shim_do_semop(int semid, struct sembuf* sops, unsigned int nsops) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
     return __do_semop(semid, sops, nsops, IPC_SEM_NOTIMEOUT);
 }
 
-int shim_do_semtimedop (int semid, struct sembuf * sops, unsigned int nsops,
-                        const struct timespec * timeout)
-{
+int shim_do_semtimedop(int semid, struct sembuf* sops, unsigned int nsops,
+                       const struct timespec* timeout) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
-    return __do_semop(semid, sops, nsops,
-                      timeout->tv_sec * 1000000000ULL + timeout->tv_nsec);
+    return __do_semop(semid, sops, nsops, timeout->tv_sec * 1000000000ULL + timeout->tv_nsec);
 }
 
-int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
-{
+int shim_do_semctl(int semid, int semnum, int cmd, unsigned long arg) {
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
-    struct shim_sem_handle * sem;
+    struct shim_sem_handle* sem;
     int ret;
     __try_create_lock();
 
     if ((ret = connect_sem_handle(semid, 0, &sem)) < 0)
-       return ret;
+        return ret;
 
-    struct shim_handle * hdl = SEM_TO_HANDLE(sem);
+    struct shim_handle* hdl = SEM_TO_HANDLE(sem);
     lock(&hdl->lock);
 
     switch (cmd) {
@@ -418,9 +400,9 @@ int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
 
         switch (cmd) {
             case GETALL:
-                for (int i = 0 ; i < sem->nsems ; i++) {
-                    unsigned short val = sem->sems[i].val;
-                    ((unsigned short *) arg)[i] = val;
+                for (int i = 0; i < sem->nsems; i++) {
+                    unsigned short val        = sem->sems[i].val;
+                    ((unsigned short*)arg)[i] = val;
                 }
                 break;
 
@@ -441,14 +423,14 @@ int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
                 break;
 
             case SETALL:
-                for (int i = 0 ; i < sem->nsems ; i++) {
-                    unsigned short val = ((unsigned short *) arg)[i];
-                    sem->sems[i].val = val;
+                for (int i = 0; i < sem->nsems; i++) {
+                    unsigned short val = ((unsigned short*)arg)[i];
+                    sem->sems[i].val   = val;
                 }
                 break;
 
             case SETVAL: {
-                unsigned short val = arg;
+                unsigned short val    = arg;
                 sem->sems[semnum].val = val;
                 break;
             }
@@ -458,8 +440,7 @@ int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
             case GETALL:
             case SETALL: {
                 int valsize = sem->nsems * sizeof(unsigned short);
-                ret = ipc_sysv_semctl_send(sem->semid, 0, cmd,
-                                           (unsigned short *) arg, valsize);
+                ret = ipc_sysv_semctl_send(sem->semid, 0, cmd, (unsigned short*)arg, valsize);
                 break;
             }
 
@@ -469,8 +450,7 @@ int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
             case GETZCNT: {
                 int valsize = sizeof(unsigned short);
                 unsigned short val;
-                ret = ipc_sysv_semctl_send(sem->semid, semnum, cmd,
-                                           &val, valsize);
+                ret = ipc_sysv_semctl_send(sem->semid, semnum, cmd, &val, valsize);
                 if (!ret)
                     ret = val;
                 break;
@@ -478,8 +458,7 @@ int shim_do_semctl (int semid, int semnum, int cmd, unsigned long arg)
 
             case SETVAL: {
                 unsigned short val = arg;
-                ret = ipc_sysv_semctl_send(sem->semid, semnum, cmd,
-                                           &val, sizeof(unsigned short));
+                ret = ipc_sysv_semctl_send(sem->semid, semnum, cmd, &val, sizeof(unsigned short));
                 break;
             }
         }
@@ -491,43 +470,40 @@ out:
     return ret;
 }
 
-static bool __handle_sysv_sems (struct shim_sem_handle * sem)
-{
+static bool __handle_sysv_sems(struct shim_sem_handle* sem) {
     bool progressed = false;
-    bool setevent = false;
+    bool setevent   = false;
 
-    struct sem_obj * sobj;
-    for (sobj = sem->sems ; sobj < &sem->sems[sem->nsems] ; sobj++)
+    struct sem_obj* sobj;
+    for (sobj = sem->sems; sobj < &sem->sems[sem->nsems]; sobj++)
         LISTP_SPLICE_TAIL_INIT(&sobj->next_ops, &sobj->ops, progress, sem_ops);
 
-    for (sobj = sem->sems ; sobj < &sem->sems[sem->nsems] ; sobj++) {
-        struct sem_ops * sops, * n;
+    for (sobj = sem->sems; sobj < &sem->sems[sem->nsems]; sobj++) {
+        struct sem_ops* sops;
+        struct sem_ops* n;
 
         LISTP_FOR_EACH_ENTRY_SAFE(sops, n, &sobj->ops, progress) {
-            struct sembuf * op = &sops->ops[sops->stat.current];
+            struct sembuf* op = &sops->ops[sops->stat.current];
             assert(op->sem_num == sobj->num);
             // first_iter is a variable defined by LISTP_FOR_EACH_ENTRY_SAFE
             // The second part of this assertion is only valid after the first attempt
             assert(first_iter || (sops != n));
             if (sops->stat.completed)
                 goto send_result;
-again:
+        again:
             if (op->sem_op > 0) {
                 sobj->val += op->sem_op;
-                debug("sem %u: add %u => %u\n", sobj->num, op->sem_op,
-                      sobj->val);
+                debug("sem %u: add %u => %u\n", sobj->num, op->sem_op, sobj->val);
             } else if (op->sem_op < 0) {
                 if (sobj->val < -op->sem_op) {
                     if (op->sem_flg & IPC_NOWAIT) {
-                        debug("sem %u: wait for %u failed\n", sobj->num,
-                              -op->sem_op);
+                        debug("sem %u: wait for %u failed\n", sobj->num, -op->sem_op);
                         goto failed;
                     }
                     continue;
                 }
                 sobj->val -= -op->sem_op;
-                debug("sem %u: wait for %u => %u\n", sobj->num, -op->sem_op,
-                      sobj->val);
+                debug("sem %u: wait for %u => %u\n", sobj->num, -op->sem_op, sobj->val);
             } else {
                 if (sobj->val) {
                     if (op->sem_flg & IPC_NOWAIT) {
@@ -548,18 +524,15 @@ again:
 
             op = &sops->ops[sops->stat.current];
             if (op->sem_num != sobj->num) {
-                LISTP_MOVE_TAIL(sops,
-                                &sem->sems[op->sem_num].next_ops,
-                                &sobj->ops,
-                                progress);
+                LISTP_MOVE_TAIL(sops, &sem->sems[op->sem_num].next_ops, &sobj->ops, progress);
                 continue;
             }
 
             goto again;
-failed:
-            progressed = true;
+        failed:
+            progressed        = true;
             sops->stat.failed = true;
-send_result:
+        send_result:
             /* Chia-Che 10/17/17: If the code reaches this point, sops should
              * still be in sobj->ops. */
             LISTP_DEL_INIT(sops, &sobj->ops, progress);
@@ -569,13 +542,13 @@ send_result:
                 continue;
             }
 
-            size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_resp));
+            size_t total_msg_size         = get_ipc_msg_size(sizeof(struct shim_ipc_resp));
             struct shim_ipc_msg* resp_msg = __alloca(total_msg_size);
             init_ipc_msg(resp_msg, IPC_RESP, total_msg_size, sops->client.vmid);
             resp_msg->seq = sops->client.seq;
 
-            struct shim_ipc_resp* resp = (struct shim_ipc_resp *)resp_msg->msg;
-            resp->retval = sops->stat.completed ? 0 : -EAGAIN;
+            struct shim_ipc_resp* resp = (struct shim_ipc_resp*)resp_msg->msg;
+            resp->retval               = sops->stat.completed ? 0 : -EAGAIN;
 
             send_ipc_message(resp_msg, sops->client.port);
 
@@ -593,36 +566,31 @@ send_result:
     return progressed;
 }
 
-static void __handle_one_sysv_sem (struct shim_sem_handle * sem,
-                                   struct sem_stat * stat,
-                                   struct sembuf * sops)
-{
+static void __handle_one_sysv_sem(struct shim_sem_handle* sem, struct sem_stat* stat,
+                                  struct sembuf* sops) {
     bool progressed = false;
 
 again:
     while (stat->current < stat->nops) {
-        struct sem_obj * sobj = &sem->sems[sops[stat->current].sem_num];
-        struct sembuf * op = &sops[stat->current];
+        struct sem_obj* sobj = &sem->sems[sops[stat->current].sem_num];
+        struct sembuf* op    = &sops[stat->current];
 
         if (op->sem_op > 0) {
             progressed = true;
             sobj->val += op->sem_op;
-            debug("sem %u: add %u => %u\n", sobj->num, op->sem_op,
-                  sobj->val);
+            debug("sem %u: add %u => %u\n", sobj->num, op->sem_op, sobj->val);
         } else if (op->sem_op < 0) {
             if (sobj->val < -op->sem_op) {
                 if (op->sem_flg & IPC_NOWAIT) {
                     stat->failed = true;
-                    debug("sem %u: wait for %u failed\n", sobj->num,
-                          -op->sem_op);
+                    debug("sem %u: wait for %u failed\n", sobj->num, -op->sem_op);
                     return;
                 }
                 goto failed;
             }
             progressed = true;
             sobj->val -= -op->sem_op;
-            debug("sem %u: wait for %u => %u\n", sobj->num, -op->sem_op,
-                  sobj->val);
+            debug("sem %u: wait for %u => %u\n", sobj->num, -op->sem_op, sobj->val);
         } else {
             if (sobj->val) {
                 if (op->sem_flg & IPC_NOWAIT) {
@@ -642,7 +610,8 @@ again:
     stat->completed = true;
 failed:
     if (progressed) {
-        while (__handle_sysv_sems(sem));
+        while (__handle_sysv_sems(sem))
+            ;
         progressed = false;
         if (!stat->completed)
             goto again;
@@ -650,15 +619,14 @@ failed:
 }
 
 #if MIGRATE_SYSV_SEM == 1
-static int sem_balance_migrate (struct shim_handle * hdl,
-                                struct sysv_client * client);
-
-static struct sysv_balance_policy sem_policy  = {
-        .score_decay        = SEM_SCORE_DECAY,
-        .score_max          = SEM_SCORE_MAX,
-        .balance_threshold  = SEM_BALANCE_THRESHOLD,
-        .migrate            = &sem_balance_migrate,
-    };
+static int sem_balance_migrate(struct shim_handle* hdl, struct sysv_client* client);
+
+static struct sysv_balance_policy sem_policy = {
+    .score_decay       = SEM_SCORE_DECAY,
+    .score_max         = SEM_SCORE_MAX,
+    .balance_threshold = SEM_BALANCE_THRESHOLD,
+    .migrate           = &sem_balance_migrate,
+};
 #endif
 
 DEFINE_PROFILE_CATEGORY(submit_sysv_sem, sysv_sem);
@@ -674,21 +642,19 @@ DEFINE_PROFILE_INTERVAL(sem_alloc_semop, submit_sysv_sem);
 DEFINE_PROFILE_INTERVAL(sem_append_semop, submit_sysv_sem);
 DEFINE_PROFILE_INTERVAL(sem_wait_for_complete, submit_sysv_sem);
 
-int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
-                     int nsops, unsigned long timeout,
-                     struct sysv_client * client)
-{
+int submit_sysv_sem(struct shim_sem_handle* sem, struct sembuf* sops, int nsops,
+                    unsigned long timeout, struct sysv_client* client) {
     BEGIN_PROFILE_INTERVAL();
-    int ret = 0;
-    struct shim_handle * hdl = SEM_TO_HANDLE(sem);
-    struct sem_ops * sem_ops = NULL;
-    bool malloced = false;
+    int ret                 = 0;
+    struct shim_handle* hdl = SEM_TO_HANDLE(sem);
+    struct sem_ops* sem_ops = NULL;
+    bool malloced           = false;
     struct sem_stat stat;
-    stat.nops       = nsops;
-    stat.current    = 0;
-    stat.timeout    = timeout;
-    stat.completed  = false;
-    stat.failed     = false;
+    stat.nops      = nsops;
+    stat.current   = 0;
+    stat.timeout   = timeout;
+    stat.completed = false;
+    stat.failed    = false;
     SAVE_PROFILE_INTERVAL(sem_prepare_stat);
 
     lock(&hdl->lock);
@@ -699,13 +665,13 @@ int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
         goto out_locked;
     }
 
-    IDTYPE semid = sem->semid;
-    bool sendreply = false;
+    IDTYPE semid      = sem->semid;
+    bool sendreply    = false;
     unsigned long seq = client ? client->seq : 0;
-    int score = 0;
+    int score         = 0;
 
-    for (int i = 0 ; i < nsops ; i++) {
-        struct sembuf * op = &sops[i];
+    for (int i = 0; i < nsops; i++) {
+        struct sembuf* op = &sops[i];
 
         if (op->sem_op > 0) {
             score += SEM_POSITIVE_SCORE(op->sem_num);
@@ -725,21 +691,18 @@ int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
             goto out_locked;
         }
 
-        ret = ipc_sysv_delres_send(client->port, client->vmid, sem->semid,
-                                   SYSV_SEM);
+        ret = ipc_sysv_delres_send(client->port, client->vmid, sem->semid, SYSV_SEM);
         goto out_locked;
     }
 
 #if MIGRATE_SYSV_SEM == 1
     if (sem->owned) {
-        __balance_sysv_score(&sem_policy, hdl, sem->scores, MAX_SYSV_CLIENTS,
-                             client, score);
+        __balance_sysv_score(&sem_policy, hdl, sem->scores, MAX_SYSV_CLIENTS, client, score);
 
         if (!sem->owned && client) {
-            struct shim_ipc_info * owner = sem->owner;
+            struct shim_ipc_info* owner = sem->owner;
             assert(owner);
-            ret = ipc_sysv_movres_send(client, owner->vmid,
-                                       qstrgetstr(&owner->uri), sem->lease,
+            ret = ipc_sysv_movres_send(client, owner->vmid, qstrgetstr(&owner->uri), sem->lease,
                                        sem->semid, SYSV_SEM);
             goto out_locked;
         }
@@ -748,22 +711,19 @@ int submit_sysv_sem (struct shim_sem_handle * sem, struct sembuf * sops,
 
     if (!sem->owned) {
         if (client) {
-            struct shim_ipc_info * owner = sem->owner;
-            ret = owner ?
-                  ipc_sysv_movres_send(client, owner->vmid,
-                                       qstrgetstr(&owner->uri), sem->lease,
-                                       sem->semid, SYSV_SEM) :
-                  -ECONNREFUSED;
+            struct shim_ipc_info* owner = sem->owner;
+            ret = owner ? ipc_sysv_movres_send(client, owner->vmid, qstrgetstr(&owner->uri),
+                                               sem->lease, sem->semid, SYSV_SEM)
+                        : -ECONNREFUSED;
 
             SAVE_PROFILE_INTERVAL(sem_send_ipc_movres);
             goto out_locked;
         }
 
-unowned:
+    unowned:
         unlock(&hdl->lock);
         ret = ipc_sysv_semop_send(semid, sops, nsops, timeout, &seq);
-        if (ret != -EAGAIN &&
-            ret != -ECONNREFUSED)
+        if (ret != -EAGAIN && ret != -ECONNREFUSED)
             goto out;
 
         lock(&hdl->lock);
@@ -773,17 +733,18 @@ unowned:
     }
 
     if (seq) {
-        struct sem_ops * op;
+        struct sem_ops* op;
 
-        LISTP_FOR_EACH_ENTRY(op, &sem->migrated, progress)
-            if (op->client.vmid == (client ? client->vmid : cur_process.vmid)
-                && seq == op->client.seq) {
+        LISTP_FOR_EACH_ENTRY(op, &sem->migrated, progress) {
+            if (op->client.vmid == (client ? client->vmid : cur_process.vmid) &&
+                seq == op->client.seq) {
                 LISTP_DEL_INIT(op, &sem->migrated, progress);
-                sem_ops = op;
-                stat = sem_ops->stat;
+                sem_ops  = op;
+                stat     = sem_ops->stat;
                 malloced = true;
                 break;
             }
+        }
     }
 
     __handle_one_sysv_sem(sem, &stat, sops);
@@ -792,13 +753,13 @@ unowned:
     if (stat.completed || stat.failed) {
         ret = stat.completed ? 0 : -EAGAIN;
         if (client && sendreply) {
-            size_t total_msg_size = get_ipc_msg_size(sizeof(struct shim_ipc_resp));
+            size_t total_msg_size         = get_ipc_msg_size(sizeof(struct shim_ipc_resp));
             struct shim_ipc_msg* resp_msg = __alloca(total_msg_size);
             init_ipc_msg(resp_msg, IPC_RESP, total_msg_size, client->vmid);
             resp_msg->seq = client->seq;
 
-            struct shim_ipc_resp* resp = (struct shim_ipc_resp *)resp_msg->msg;
-            resp->retval = ret;
+            struct shim_ipc_resp* resp = (struct shim_ipc_resp*)resp_msg->msg;
+            resp->retval               = ret;
 
             ret = send_ipc_message(resp_msg, client->port);
         }
@@ -810,8 +771,7 @@ unowned:
     if (client) {
         assert(sendreply);
         if (!sem_ops || !malloced) {
-            sem_ops = malloc(sizeof(struct sem_ops) +
-                             sizeof(struct sembuf) * nsops);
+            sem_ops = malloc(sizeof(struct sem_ops) + sizeof(struct sembuf) * nsops);
             if (!sem_ops) {
                 ret = -ENOMEM;
                 goto out_locked;
@@ -826,8 +786,7 @@ unowned:
         }
     } else {
         if (!sem_ops) {
-            sem_ops = __alloca(sizeof(struct sem_ops) +
-                               sizeof(struct sembuf) * nsops);
+            sem_ops              = __alloca(sizeof(struct sem_ops) + sizeof(struct sembuf) * nsops);
             sem_ops->client.vmid = 0;
             sem_ops->client.port = NULL;
             sem_ops->client.seq  = 0;
@@ -837,14 +796,14 @@ unowned:
     }
 
     sem_ops->stat = stat;
-    for (int i = 0 ; i < nsops ; i++)
+    for (int i = 0; i < nsops; i++) {
         sem_ops->ops[i] = sops[i];
+    }
 
-    LISTP_TYPE(sem_ops) * next_ops =
-            &sem->sems[sops[stat.current].sem_num].next_ops;
+    LISTP_TYPE(sem_ops)* next_ops = &sem->sems[sops[stat.current].sem_num].next_ops;
     assert(LIST_EMPTY(sem_ops, progress));
     LISTP_ADD_TAIL(sem_ops, next_ops, progress);
-    //CHECK_LIST_HEAD(next_ops);
+    // CHECK_LIST_HEAD(next_ops);
     sem->nreqs++;
     SAVE_PROFILE_INTERVAL(sem_append_semop);
 
@@ -853,12 +812,11 @@ unowned:
         add_ipc_port(client->port, client->vmid, IPC_PORT_SYSVCON, NULL);
         get_ipc_port(client->port);
         sem_ops->client = *client;
-        sem_ops = NULL;
+        sem_ops         = NULL;
         goto out_locked;
     }
 
-    while (!sem_ops->stat.completed &&
-           !sem_ops->stat.failed) {
+    while (!sem_ops->stat.completed && !sem_ops->stat.failed) {
         if (!sem->owned) {
             /* Chia-Che 10/17/17: sem_ops may move from semaphore to semaphore
                base on its current state */
@@ -884,71 +842,64 @@ out:
 }
 
 #if MIGRATE_SYSV_SEM == 1
-static int sem_balance_migrate (struct shim_handle * hdl,
-                                struct sysv_client * src)
-{
-    struct shim_sem_handle * sem = &hdl->info.sem;
-    int ret = 0;
+static int sem_balance_migrate(struct shim_handle* hdl, struct sysv_client* src) {
+    struct shim_sem_handle* sem = &hdl->info.sem;
+    int ret                     = 0;
 
     debug("trigger semaphore balancing, migrate to process %u\n", src->vmid);
 
-    struct sem_backup * sem_backups = __alloca(sizeof(struct sem_backup) *
-                                               sem->nsems);
+    struct sem_backup* sem_backups = __alloca(sizeof(struct sem_backup) * sem->nsems);
 
-    struct sem_client_backup * clients =
-            __alloca(sizeof(struct sem_client_backup) * sem->nreqs);
+    struct sem_client_backup* clients = __alloca(sizeof(struct sem_client_backup) * sem->nreqs);
 
     int sem_cnt = 0, client_cnt = 0;
 
-    struct sem_obj * sobj;
-    for (sobj = sem->sems ; sobj < &sem->sems[sem->nsems] ; sobj++) {
+    struct sem_obj* sobj;
+    for (sobj = sem->sems; sobj < &sem->sems[sem->nsems]; sobj++) {
         assert(sem_cnt < sem->nsems);
-        struct sem_backup * b = sem_backups + (sem_cnt++);
-        b->val  = sobj->val;
-        b->zcnt = sobj->zcnt;
-        b->ncnt = sobj->ncnt;
-        b->pid  = sobj->pid;
+        struct sem_backup* b = sem_backups + (sem_cnt++);
+        b->val               = sobj->val;
+        b->zcnt              = sobj->zcnt;
+        b->ncnt              = sobj->ncnt;
+        b->pid               = sobj->pid;
 
         LISTP_SPLICE_TAIL(&sobj->next_ops, &sobj->ops, progress, sem_ops);
 
-        struct sem_ops * sops;
+        struct sem_ops* sops;
         LISTP_FOR_EACH_ENTRY(sops, &sobj->ops, progress) {
             assert(client_cnt < sem->nreqs);
-            struct sem_client_backup * c = clients + (client_cnt)++;
-            c->vmid = sops->client.vmid;
-            c->seq  = sops->client.seq;
-            c->current = sops->stat.current;
-            c->nops = sops->stat.nops;
+            struct sem_client_backup* c = clients + (client_cnt)++;
+            c->vmid                     = sops->client.vmid;
+            c->seq                      = sops->client.seq;
+            c->current                  = sops->stat.current;
+            c->nops                     = sops->stat.nops;
         }
     }
 
-    struct shim_ipc_info * info = lookup_ipc_info(src->vmid);
+    struct shim_ipc_info* info = lookup_ipc_info(src->vmid);
     if (!info)
         goto out;
 
-    ipc_sysv_sublease_send(src->vmid, sem->semid,
-                           qstrgetstr(&info->uri),
-                           &sem->lease);
+    ipc_sysv_sublease_send(src->vmid, sem->semid, qstrgetstr(&info->uri), &sem->lease);
 
-    ret = ipc_sysv_semmov_send(src->port, src->vmid, sem->semid, sem->lease,
-                               sem_backups, sem_cnt, clients, client_cnt,
-                               sem->scores, MAX_SYSV_CLIENTS);
+    ret = ipc_sysv_semmov_send(src->port, src->vmid, sem->semid, sem->lease, sem_backups, sem_cnt,
+                               clients, client_cnt, sem->scores, MAX_SYSV_CLIENTS);
     if (ret < 0)
         goto failed_info;
 
     sem->owned = false;
     sem->owner = info;
 
-    for (sobj = sem->sems ; sobj < &sem->sems[sem->nsems] ; sobj++) {
-        struct sem_ops * sops, * n;
+    for (sobj = sem->sems; sobj < &sem->sems[sem->nsems]; sobj++) {
+        struct sem_ops* sops;
+        struct sem_ops* n;
         LISTP_FOR_EACH_ENTRY_SAFE(sops, n, &sobj->ops, progress) {
             LISTP_DEL_INIT(sops, &sobj->ops, progress);
             sem->nreqs--;
             sops->stat.failed = true;
             if (!sops->client.vmid)
                 continue;
-            ipc_sysv_movres_send(&sops->client, src->vmid,
-                                 qstrgetstr(&info->uri), sem->lease,
+            ipc_sysv_movres_send(&sops->client, src->vmid, qstrgetstr(&info->uri), sem->lease,
                                  sem->semid, SYSV_SEM);
             put_ipc_port(sops->client.port);
             free(sops);
@@ -958,7 +909,7 @@ static int sem_balance_migrate (struct shim_handle * hdl,
     sem->nsems = 0;
     free(sem->sems);
     sem->sems = NULL;
-    ret = 0;
+    ret       = 0;
     DkEventSet(sem->event);
     goto out;
 

File diff suppressed because it is too large
+ 205 - 277
LibOS/shim/src/sys/shim_socket.c


+ 9 - 18
LibOS/shim/src/sys/shim_time.c

@@ -20,19 +20,15 @@
  * Implementation of system call "gettimeofday", "time" and "clock_gettime".
  */
 
-#include <shim_internal.h>
-#include <shim_table.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-
+#include <errno.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_table.h>
 
-#include <errno.h>
-
-int shim_do_gettimeofday (struct __kernel_timeval * tv,
-                          struct __kernel_timezone * tz)
-{
+int shim_do_gettimeofday(struct __kernel_timeval* tv, struct __kernel_timezone* tz) {
     if (!tv)
         return -EINVAL;
 
@@ -52,8 +48,7 @@ int shim_do_gettimeofday (struct __kernel_timeval * tv,
     return 0;
 }
 
-time_t shim_do_time (time_t * tloc)
-{
+time_t shim_do_time(time_t* tloc) {
     long time = DkSystemTimeQuery();
 
     if (time == -1)
@@ -70,9 +65,7 @@ time_t shim_do_time (time_t * tloc)
     return t;
 }
 
-int shim_do_clock_gettime (clockid_t which_clock,
-                           struct timespec * tp)
-{
+int shim_do_clock_gettime(clockid_t which_clock, struct timespec* tp) {
     /* all clock are the same */
     __UNUSED(which_clock);
 
@@ -92,9 +85,7 @@ int shim_do_clock_gettime (clockid_t which_clock,
     return 0;
 }
 
-int shim_do_clock_getres (clockid_t which_clock,
-                          struct timespec * tp)
-{
+int shim_do_clock_getres(clockid_t which_clock, struct timespec* tp) {
     /* all clock are the same */
     __UNUSED(which_clock);
 

+ 10 - 14
LibOS/shim/src/sys/shim_uname.c

@@ -20,26 +20,22 @@
  * Implementation of system call "uname".
  */
 
+#include <errno.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
 #include <shim_internal.h>
 #include <shim_table.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
 #include <shim_utils.h>
-
-#include <errno.h>
 #include <sys/utsname.h>
 
 /* DP: Damned lies */
-static struct old_utsname graphene_uname = {
-    .sysname = "Linux",
-    .nodename = "localhost",
-    .release = "3.10.0",
-    .version = "1",
-    .machine = "x86_64"
-};
-
-int shim_do_uname (struct old_utsname * buf)
-{
+static struct old_utsname graphene_uname = {.sysname  = "Linux",
+                                            .nodename = "localhost",
+                                            .release  = "3.10.0",
+                                            .version  = "1",
+                                            .machine  = "x86_64"};
+
+int shim_do_uname(struct old_utsname* buf) {
     if (!buf || test_user_memory(buf, sizeof(*buf), true))
         return -EFAULT;
 

+ 16 - 21
LibOS/shim/src/sys/shim_vfork.c

@@ -20,23 +20,20 @@
  * Implementation of system call "vfork".
  */
 
+#include <asm/prctl.h>
+#include <errno.h>
+#include <linux/futex.h>
+#include <pal.h>
+#include <pal_error.h>
+#include <shim_checkpoint.h>
 #include <shim_internal.h>
-#include <shim_utils.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_checkpoint.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <sys/syscall.h>
+#include <shim_utils.h>
 #include <sys/mman.h>
-#include <asm/prctl.h>
-#include <linux/futex.h>
-#include <errno.h>
+#include <sys/syscall.h>
 
-int shim_do_vfork (void)
-{
+int shim_do_vfork(void) {
 #ifdef ALIAS_VFORK_AS_FORK
     debug("vfork() is an alias to fork() in Graphene, calling fork() now\n");
     return shim_do_fork();
@@ -51,20 +48,18 @@ int shim_do_vfork (void)
      * Because rdi might be used in SHIM, I cache rdi in r13 (reference:
      * syscallas.S).
      */
-    struct shim_thread * cur_thread = get_cur_thread();
-    struct shim_thread * new_thread = get_new_thread(0);
+    struct shim_thread* cur_thread = get_cur_thread();
+    struct shim_thread* new_thread = get_new_thread(0);
     /* put the new thread in a new process (thread group) */
 
-    __asm__ volatile ("movq %%rbp, %0\r\n"
-                      : "=r"(new_thread->frameptr));
+    __asm__ volatile ("movq %%rbp, %0\r\n" : "=r"(new_thread->frameptr));
 
     size_t stack_size = 4096;
 
-    if (new_thread->frameptr <= cur_thread->stack_top &&
-        new_thread->frameptr > cur_thread->stack)
+    if (new_thread->frameptr <= cur_thread->stack_top && new_thread->frameptr > cur_thread->stack)
         stack_size = cur_thread->stack_top - new_thread->frameptr;
 
-    void * dummy_stack = system_malloc(stack_size);
+    void* dummy_stack = system_malloc(stack_size);
 
     if (!dummy_stack) {
         debug("creation of stack failed\n");
@@ -94,10 +89,10 @@ int shim_do_vfork (void)
     add_thread(new_thread);
     new_thread->dummy = cur_thread;
 
-    struct shim_handle_map * handle_map = get_cur_handle_map(cur_thread);
+    struct shim_handle_map* handle_map = get_cur_handle_map(cur_thread);
     /* pop the ref count of current handle map to prevent revocation */
     get_handle_map(handle_map);
-    struct shim_handle_map * new_map = NULL;
+    struct shim_handle_map* new_map = NULL;
     /* duplicate handle map intp a new handle map */
     dup_handle_map(&new_map, handle_map);
     /* set the new handle map to new thread */

+ 20 - 25
LibOS/shim/src/sys/shim_wait.c

@@ -20,30 +20,26 @@
  * Implementation of system call "wait4".
  */
 
+#include <asm/prctl.h>
+#include <errno.h>
+#include <linux/wait.h>
+#include <pal.h>
+#include <pal_error.h>
 #include <shim_internal.h>
-#include <shim_utils.h>
+#include <shim_profile.h>
 #include <shim_table.h>
 #include <shim_thread.h>
-#include <shim_profile.h>
-
-#include <pal.h>
-#include <pal_error.h>
-
-#include <sys/syscall.h>
+#include <shim_utils.h>
 #include <sys/mman.h>
-#include <asm/prctl.h>
-#include <linux/wait.h>
-#include <errno.h>
+#include <sys/syscall.h>
 
 DEFINE_PROFILE_CATEGORY(wait, );
 DEFINE_PROFILE_INTERVAL(child_exit_notification, wait);
 
-pid_t shim_do_wait4 (pid_t pid, int * status, int option,
-                     struct __kernel_rusage * ru)
-{
-    struct shim_thread * cur = get_cur_thread();
-    struct shim_thread * thread = NULL;
-    int ret = 0;
+pid_t shim_do_wait4(pid_t pid, int* status, int option, struct __kernel_rusage* ru) {
+    struct shim_thread* cur    = get_cur_thread();
+    struct shim_thread* thread = NULL;
+    int ret                    = 0;
     __UNUSED(ru);
 
     INC_PROFILE_OCCURENCE(syscall_use_ipc);
@@ -53,7 +49,7 @@ pid_t shim_do_wait4 (pid_t pid, int * status, int option,
             return -ECHILD;
 
         if (!(option & WNOHANG)) {
-block_pid:
+        block_pid:
             object_wait_with_retry(thread->exit_event);
         }
 
@@ -69,7 +65,7 @@ block_pid:
 
         if (!LIST_EMPTY(thread, siblings)) {
             debug("reaping thread %p\n", thread);
-            struct shim_thread * parent = thread->parent;
+            struct shim_thread* parent = thread->parent;
             assert(parent);
 
             lock(&parent->lock);
@@ -89,14 +85,13 @@ block_pid:
 
     lock(&cur->lock);
 
-    if (LISTP_EMPTY(&cur->children) &&
-        LISTP_EMPTY(&cur->exited_children)) {
+    if (LISTP_EMPTY(&cur->children) && LISTP_EMPTY(&cur->exited_children)) {
         unlock(&cur->lock);
         return -ECHILD;
     }
 
     if (!(option & WNOHANG)) {
-block:
+    block:
         if (cur->child_exit_event)
             while (LISTP_EMPTY(&cur->exited_children)) {
                 unlock(&cur->lock);
@@ -109,16 +104,16 @@ block:
         if (pid == 0)
             pid = -cur->pgid;
 
-        LISTP_FOR_EACH_ENTRY(thread, &cur->exited_children, siblings)
-            if (thread->pgid == (IDTYPE) -pid)
+        LISTP_FOR_EACH_ENTRY(thread, &cur->exited_children, siblings) {
+            if (thread->pgid == (IDTYPE)-pid)
                 goto found_child;
+        }
 
         if (!(option & WNOHANG))
             goto block;
     } else {
         if (!LISTP_EMPTY(&cur->exited_children)) {
-            thread = LISTP_FIRST_ENTRY(&cur->exited_children,
-                                       struct shim_thread, siblings);
+            thread = LISTP_FIRST_ENTRY(&cur->exited_children, struct shim_thread, siblings);
             goto found_child;
         }
     }

+ 20 - 28
LibOS/shim/src/sys/shim_wrappers.c

@@ -20,24 +20,20 @@
  * Implementation of system call "readv" and "writev".
  */
 
-#include <shim_internal.h>
-#include <shim_utils.h>
-#include <shim_table.h>
-#include <shim_handle.h>
-#include <shim_fs.h>
-#include <shim_table.h>
-
+#include <errno.h>
 #include <pal.h>
 #include <pal_error.h>
+#include <shim_fs.h>
+#include <shim_handle.h>
+#include <shim_internal.h>
+#include <shim_table.h>
+#include <shim_utils.h>
 
-#include <errno.h>
-
-ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen)
-{
-    if (!vec || test_user_memory((void *) vec, sizeof(*vec) * vlen, false))
+ssize_t shim_do_readv(int fd, const struct iovec* vec, int vlen) {
+    if (!vec || test_user_memory((void*)vec, sizeof(*vec) * vlen, false))
         return -EINVAL;
 
-    for (int i = 0 ; i < vlen ; i++) {
+    for (int i = 0; i < vlen; i++) {
         if (vec[i].iov_base) {
             if (vec[i].iov_base + vec[i].iov_len <= vec[i].iov_base)
                 return -EINVAL;
@@ -46,21 +42,20 @@ ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen)
         }
     }
 
-    struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
+    struct shim_handle* hdl = get_fd_handle(fd, NULL, NULL);
     if (!hdl)
         return -EBADF;
 
     int ret = 0;
 
-    if (!(hdl->acc_mode & MAY_READ) ||
-        !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->read) {
+    if (!(hdl->acc_mode & MAY_READ) || !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->read) {
         ret = -EACCES;
         goto out;
     }
 
     ssize_t bytes = 0;
 
-    for (int i = 0 ; i < vlen ; i++) {
+    for (int i = 0; i < vlen; i++) {
         int b_vec;
 
         if (!vec[i].iov_base)
@@ -68,7 +63,7 @@ ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen)
 
         b_vec = hdl->fs->fs_ops->read(hdl, vec[i].iov_base, vec[i].iov_len);
         if (b_vec < 0) {
-            ret = bytes ? : b_vec;
+            ret = bytes ?: b_vec;
             goto out;
         }
 
@@ -96,12 +91,11 @@ out:
  * actually written. Otherwise, it shall return a value of -1, the file-pointer
  * shall remain unchanged, and errno shall be set to indicate an error
  */
-ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen)
-{
-    if (!vec || test_user_memory((void *) vec, sizeof(*vec) * vlen, false))
+ssize_t shim_do_writev(int fd, const struct iovec* vec, int vlen) {
+    if (!vec || test_user_memory((void*)vec, sizeof(*vec) * vlen, false))
         return -EINVAL;
 
-    for (int i = 0 ; i < vlen ; i++) {
+    for (int i = 0; i < vlen; i++) {
         if (vec[i].iov_base) {
             if (vec[i].iov_base + vec[i].iov_len < vec[i].iov_base)
                 return -EINVAL;
@@ -110,22 +104,20 @@ ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen)
         }
     }
 
-    struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
+    struct shim_handle* hdl = get_fd_handle(fd, NULL, NULL);
     if (!hdl)
         return -EBADF;
 
     int ret = 0;
 
-    if (!(hdl->acc_mode & MAY_WRITE) ||
-        !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->write) {
+    if (!(hdl->acc_mode & MAY_WRITE) || !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->write) {
         ret = -EACCES;
         goto out;
     }
 
     ssize_t bytes = 0;
 
-    for (int i = 0 ; i < vlen ; i++)
-    {
+    for (int i = 0; i < vlen; i++) {
         int b_vec;
 
         if (!vec[i].iov_base)
@@ -133,7 +125,7 @@ ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen)
 
         b_vec = hdl->fs->fs_ops->write(hdl, vec[i].iov_base, vec[i].iov_len);
         if (b_vec < 0) {
-            ret = bytes ? : b_vec;
+            ret = bytes ?: b_vec;
             goto out;
         }
 

+ 119 - 126
LibOS/shim/src/utils/md5.c

@@ -35,18 +35,13 @@
 #include <shim_utils.h>
 
 /* forward declaration */
-static void __transform (UINT4 *buf, UINT4 *in);
+static void __transform(UINT4* buf, UINT4* in);
 
 static unsigned char PADDING[64] = {
-    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
-};
+    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
 /* F, G and H are basic MD5 functions: selection, majority, parity */
 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
@@ -55,38 +50,42 @@ static unsigned char PADDING[64] = {
 #define I(x, y, z) ((y) ^ ((x) | (~z)))
 
 /* ROTATE_LEFT rotates x left n bits */
-#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 
 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
 /* Rotation is separate from addition to prevent recomputation */
-#define FF(a, b, c, d, x, s, ac) \
-{(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
-    (a) = ROTATE_LEFT ((a), (s)); \
-    (a) += (b); \
-}
-#define GG(a, b, c, d, x, s, ac) \
-{(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
-    (a) = ROTATE_LEFT ((a), (s)); \
-    (a) += (b); \
-}
-#define HH(a, b, c, d, x, s, ac) \
-{(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
-    (a) = ROTATE_LEFT ((a), (s)); \
-    (a) += (b); \
-}
-#define II(a, b, c, d, x, s, ac) \
-{(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
-    (a) = ROTATE_LEFT ((a), (s)); \
-    (a) += (b); \
-}
+#define FF(a, b, c, d, x, s, ac)                     \
+    {                                                \
+        (a) += F((b), (c), (d)) + (x) + (UINT4)(ac); \
+        (a) = ROTATE_LEFT((a), (s));                 \
+        (a) += (b);                                  \
+    }
+#define GG(a, b, c, d, x, s, ac)                     \
+    {                                                \
+        (a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \
+        (a) = ROTATE_LEFT((a), (s));                 \
+        (a) += (b);                                  \
+    }
+#define HH(a, b, c, d, x, s, ac)                     \
+    {                                                \
+        (a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \
+        (a) = ROTATE_LEFT((a), (s));                 \
+        (a) += (b);                                  \
+    }
+#define II(a, b, c, d, x, s, ac)                     \
+    {                                                \
+        (a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \
+        (a) = ROTATE_LEFT((a), (s));                 \
+        (a) += (b);                                  \
+    }
 
-void md5_init (mdContext)
-    struct shim_md5_ctx *mdContext;
+void md5_init(mdContext)
+    struct shim_md5_ctx* mdContext;
 {
     mdContext->i[0] = mdContext->i[1] = (UINT4)0;
 
     /* Load magic initialization constants.
-    */
+     */
     mdContext->buf[0] = (UINT4)0x67452301;
     mdContext->buf[1] = (UINT4)0xefcdab89;
     mdContext->buf[2] = (UINT4)0x98badcfe;
@@ -94,9 +93,9 @@ void md5_init (mdContext)
 }
 EXTERN_ALIAS(md5_init);
 
-void md5_update (mdContext, inBuf, inLen)
-    struct shim_md5_ctx *mdContext;
-    const void *inBuf;
+void md5_update(mdContext, inBuf, inLen)
+    struct shim_md5_ctx* mdContext;
+    const void* inBuf;
     size_t inLen;
 {
     UINT4 in[16];
@@ -114,24 +113,23 @@ void md5_update (mdContext, inBuf, inLen)
 
     while (inLen--) {
         /* add new character to buffer, increment mdi */
-        mdContext->in[mdi++] = *(unsigned char *) inBuf++;
+        mdContext->in[mdi++] = *(unsigned char*)inBuf++;
 
         /* transform if necessary */
         if (mdi == 0x40) {
             for (i = 0, ii = 0; i < 16; i++, ii += 4)
-                in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
-                    (((UINT4)mdContext->in[ii+2]) << 16) |
-                    (((UINT4)mdContext->in[ii+1]) << 8) |
-                    ((UINT4)mdContext->in[ii]);
-            __transform (mdContext->buf, in);
+                in[i] = (((UINT4)mdContext->in[ii + 3]) << 24) |
+                        (((UINT4)mdContext->in[ii + 2]) << 16) |
+                        (((UINT4)mdContext->in[ii + 1]) << 8) | ((UINT4)mdContext->in[ii]);
+            __transform(mdContext->buf, in);
             mdi = 0;
         }
     }
 }
 EXTERN_ALIAS(md5_update);
 
-void md5_final (mdContext)
-    struct shim_md5_ctx *mdContext;
+void md5_final(mdContext)
+    struct shim_md5_ctx* mdContext;
 {
     UINT4 in[16];
     int mdi;
@@ -147,33 +145,28 @@ void md5_final (mdContext)
 
     /* pad out to 56 mod 64 */
     padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
-    md5_update (mdContext, PADDING, padLen);
+    md5_update(mdContext, PADDING, padLen);
 
     /* append length in bits and transform */
     for (i = 0, ii = 0; i < 14; i++, ii += 4)
-        in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
-            (((UINT4)mdContext->in[ii+2]) << 16) |
-            (((UINT4)mdContext->in[ii+1]) << 8) |
-            ((UINT4)mdContext->in[ii]);
-    __transform (mdContext->buf, in);
+        in[i] = (((UINT4)mdContext->in[ii + 3]) << 24) | (((UINT4)mdContext->in[ii + 2]) << 16) |
+                (((UINT4)mdContext->in[ii + 1]) << 8) | ((UINT4)mdContext->in[ii]);
+    __transform(mdContext->buf, in);
 
     /* store buffer in digest */
     for (i = 0, ii = 0; i < 4; i++, ii += 4) {
-        mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
-        mdContext->digest[ii+1] =
-            (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
-        mdContext->digest[ii+2] =
-            (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
-        mdContext->digest[ii+3] =
-            (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
+        mdContext->digest[ii]     = (unsigned char)(mdContext->buf[i] & 0xFF);
+        mdContext->digest[ii + 1] = (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
+        mdContext->digest[ii + 2] = (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
+        mdContext->digest[ii + 3] = (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
     }
 }
 
 /* Basic MD5 step. Transform buf based on in.
-*/
-static void __transform (buf, in)
-    UINT4 *buf;
-    UINT4 *in;
+ */
+static void __transform(buf, in)
+    UINT4* buf;
+    UINT4* in;
 {
     UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
 
@@ -182,88 +175,88 @@ static void __transform (buf, in)
 #define S12 12
 #define S13 17
 #define S14 22
-    FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */
-    FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */
-    FF ( c, d, a, b, in[ 2], S13,  606105819); /* 3 */
-    FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */
-    FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */
-    FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */
-    FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */
-    FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */
-    FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */
-    FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */
-    FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 */
-    FF ( b, c, d, a, in[11], S14, 2304563134); /* 12 */
-    FF ( a, b, c, d, in[12], S11, 1804603682); /* 13 */
-    FF ( d, a, b, c, in[13], S12, 4254626195); /* 14 */
-    FF ( c, d, a, b, in[14], S13, 2792965006); /* 15 */
-    FF ( b, c, d, a, in[15], S14, 1236535329); /* 16 */
+    FF(a, b, c, d, in[0], S11, 3614090360);  /* 1 */
+    FF(d, a, b, c, in[1], S12, 3905402710);  /* 2 */
+    FF(c, d, a, b, in[2], S13, 606105819);   /* 3 */
+    FF(b, c, d, a, in[3], S14, 3250441966);  /* 4 */
+    FF(a, b, c, d, in[4], S11, 4118548399);  /* 5 */
+    FF(d, a, b, c, in[5], S12, 1200080426);  /* 6 */
+    FF(c, d, a, b, in[6], S13, 2821735955);  /* 7 */
+    FF(b, c, d, a, in[7], S14, 4249261313);  /* 8 */
+    FF(a, b, c, d, in[8], S11, 1770035416);  /* 9 */
+    FF(d, a, b, c, in[9], S12, 2336552879);  /* 10 */
+    FF(c, d, a, b, in[10], S13, 4294925233); /* 11 */
+    FF(b, c, d, a, in[11], S14, 2304563134); /* 12 */
+    FF(a, b, c, d, in[12], S11, 1804603682); /* 13 */
+    FF(d, a, b, c, in[13], S12, 4254626195); /* 14 */
+    FF(c, d, a, b, in[14], S13, 2792965006); /* 15 */
+    FF(b, c, d, a, in[15], S14, 1236535329); /* 16 */
 
     /* Round 2 */
 #define S21 5
 #define S22 9
 #define S23 14
 #define S24 20
-    GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */
-    GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */
-    GG ( c, d, a, b, in[11], S23,  643717713); /* 19 */
-    GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */
-    GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */
-    GG ( d, a, b, c, in[10], S22,   38016083); /* 22 */
-    GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */
-    GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */
-    GG ( a, b, c, d, in[ 9], S21,  568446438); /* 25 */
-    GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */
-    GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */
-    GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */
-    GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */
-    GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */
-    GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */
-    GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */
+    GG(a, b, c, d, in[1], S21, 4129170786);  /* 17 */
+    GG(d, a, b, c, in[6], S22, 3225465664);  /* 18 */
+    GG(c, d, a, b, in[11], S23, 643717713);  /* 19 */
+    GG(b, c, d, a, in[0], S24, 3921069994);  /* 20 */
+    GG(a, b, c, d, in[5], S21, 3593408605);  /* 21 */
+    GG(d, a, b, c, in[10], S22, 38016083);   /* 22 */
+    GG(c, d, a, b, in[15], S23, 3634488961); /* 23 */
+    GG(b, c, d, a, in[4], S24, 3889429448);  /* 24 */
+    GG(a, b, c, d, in[9], S21, 568446438);   /* 25 */
+    GG(d, a, b, c, in[14], S22, 3275163606); /* 26 */
+    GG(c, d, a, b, in[3], S23, 4107603335);  /* 27 */
+    GG(b, c, d, a, in[8], S24, 1163531501);  /* 28 */
+    GG(a, b, c, d, in[13], S21, 2850285829); /* 29 */
+    GG(d, a, b, c, in[2], S22, 4243563512);  /* 30 */
+    GG(c, d, a, b, in[7], S23, 1735328473);  /* 31 */
+    GG(b, c, d, a, in[12], S24, 2368359562); /* 32 */
 
     /* Round 3 */
 #define S31 4
 #define S32 11
 #define S33 16
 #define S34 23
-    HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */
-    HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */
-    HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */
-    HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */
-    HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */
-    HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */
-    HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */
-    HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */
-    HH ( a, b, c, d, in[13], S31,  681279174); /* 41 */
-    HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */
-    HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */
-    HH ( b, c, d, a, in[ 6], S34,   76029189); /* 44 */
-    HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */
-    HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */
-    HH ( c, d, a, b, in[15], S33,  530742520); /* 47 */
-    HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */
+    HH(a, b, c, d, in[5], S31, 4294588738);  /* 33 */
+    HH(d, a, b, c, in[8], S32, 2272392833);  /* 34 */
+    HH(c, d, a, b, in[11], S33, 1839030562); /* 35 */
+    HH(b, c, d, a, in[14], S34, 4259657740); /* 36 */
+    HH(a, b, c, d, in[1], S31, 2763975236);  /* 37 */
+    HH(d, a, b, c, in[4], S32, 1272893353);  /* 38 */
+    HH(c, d, a, b, in[7], S33, 4139469664);  /* 39 */
+    HH(b, c, d, a, in[10], S34, 3200236656); /* 40 */
+    HH(a, b, c, d, in[13], S31, 681279174);  /* 41 */
+    HH(d, a, b, c, in[0], S32, 3936430074);  /* 42 */
+    HH(c, d, a, b, in[3], S33, 3572445317);  /* 43 */
+    HH(b, c, d, a, in[6], S34, 76029189);    /* 44 */
+    HH(a, b, c, d, in[9], S31, 3654602809);  /* 45 */
+    HH(d, a, b, c, in[12], S32, 3873151461); /* 46 */
+    HH(c, d, a, b, in[15], S33, 530742520);  /* 47 */
+    HH(b, c, d, a, in[2], S34, 3299628645);  /* 48 */
 
     /* Round 4 */
 #define S41 6
 #define S42 10
 #define S43 15
 #define S44 21
-    II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */
-    II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */
-    II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */
-    II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */
-    II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */
-    II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */
-    II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */
-    II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */
-    II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */
-    II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */
-    II ( c, d, a, b, in[ 6], S43, 2734768916); /* 59 */
-    II ( b, c, d, a, in[13], S44, 1309151649); /* 60 */
-    II ( a, b, c, d, in[ 4], S41, 4149444226); /* 61 */
-    II ( d, a, b, c, in[11], S42, 3174756917); /* 62 */
-    II ( c, d, a, b, in[ 2], S43,  718787259); /* 63 */
-    II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */
+    II(a, b, c, d, in[0], S41, 4096336452);  /* 49 */
+    II(d, a, b, c, in[7], S42, 1126891415);  /* 50 */
+    II(c, d, a, b, in[14], S43, 2878612391); /* 51 */
+    II(b, c, d, a, in[5], S44, 4237533241);  /* 52 */
+    II(a, b, c, d, in[12], S41, 1700485571); /* 53 */
+    II(d, a, b, c, in[3], S42, 2399980690);  /* 54 */
+    II(c, d, a, b, in[10], S43, 4293915773); /* 55 */
+    II(b, c, d, a, in[1], S44, 2240044497);  /* 56 */
+    II(a, b, c, d, in[8], S41, 1873313359);  /* 57 */
+    II(d, a, b, c, in[15], S42, 4264355552); /* 58 */
+    II(c, d, a, b, in[6], S43, 2734768916);  /* 59 */
+    II(b, c, d, a, in[13], S44, 1309151649); /* 60 */
+    II(a, b, c, d, in[4], S41, 4149444226);  /* 61 */
+    II(d, a, b, c, in[11], S42, 3174756917); /* 62 */
+    II(c, d, a, b, in[2], S43, 718787259);   /* 63 */
+    II(b, c, d, a, in[9], S44, 3951481745);  /* 64 */
 
     buf[0] += a;
     buf[1] += b;

+ 32 - 52
LibOS/shim/src/utils/printf.c

@@ -14,36 +14,30 @@
    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <api.h>
+#include <pal.h>
 #include <shim_defs.h>
 #include <shim_internal.h>
 #include <shim_ipc.h>
-
-#include <pal.h>
-#include <api.h>
-
-#include <stdint.h>
 #include <stdarg.h>
+#include <stdint.h>
 
 PAL_HANDLE debug_handle = NULL;
 
-static inline int
-debug_fputs (const char * buf, int len)
-{
-    if (DkStreamWrite(debug_handle, 0, len, (void *) buf, NULL) == (PAL_NUM) len)
+static inline int debug_fputs(const char* buf, int len) {
+    if (DkStreamWrite(debug_handle, 0, len, (void*)buf, NULL) == (PAL_NUM)len)
         return 0;
     else
         return -1;
 }
 
-static int
-debug_fputch (void * f, int ch, void * b)
-{
+static int debug_fputch(void* f, int ch, void* b) {
     __UNUSED(f);
-    struct debug_buf * buf = (struct debug_buf *) b;
-    buf->buf[buf->end++] = ch;
+    struct debug_buf* buf = (struct debug_buf*)b;
+    buf->buf[buf->end++]  = ch;
 
     if (ch == '\n') {
-        int ret = debug_fputs(buf->buf, buf->end);
+        int ret  = debug_fputs(buf->buf, buf->end);
         buf->end = buf->start;
         return ret;
     }
@@ -54,7 +48,7 @@ debug_fputch (void * f, int ch, void * b)
         buf->buf[buf->end++] = '.';
         buf->buf[buf->end++] = '\n';
         debug_fputs(buf->buf, buf->end);
-        buf->end = buf->start;
+        buf->end             = buf->start;
         buf->buf[buf->end++] = '.';
         buf->buf[buf->end++] = '.';
     }
@@ -68,22 +62,22 @@ debug_fputch (void * f, int ch, void * b)
     return 0;
 }
 
-void debug_puts (const char * str)
-{
-    int len = strlen(str);
-    struct debug_buf * buf = shim_get_tls()->debug_buf;
+void debug_puts(const char* str) {
+    int len               = strlen(str);
+    struct debug_buf* buf = shim_get_tls()->debug_buf;
 
     while (len) {
-        int rem = DEBUGBUF_SIZE - 4 - buf->end;
+        int rem     = DEBUGBUF_SIZE - 4 - buf->end;
         bool isfull = true;
 
         if (rem > len) {
-            rem = len;
+            rem    = len;
             isfull = false;
         }
 
-        for (int i = 0 ; i < rem ; i++)
+        for (int i = 0; i < rem; i++) {
             buf->buf[buf->end + i] = str[i];
+        }
         buf->end += rem;
         str += rem;
         len -= rem;
@@ -93,45 +87,39 @@ void debug_puts (const char * str)
             buf->buf[buf->end++] = '.';
             buf->buf[buf->end++] = '\n';
             debug_fputs(buf->buf, buf->end);
-            buf->end = buf->start;
+            buf->end             = buf->start;
             buf->buf[buf->end++] = '.';
             buf->buf[buf->end++] = '.';
         }
     }
 }
 
-void debug_putch (int ch)
-{
+void debug_putch(int ch) {
     debug_fputch(NULL, ch, shim_get_tls()->debug_buf);
 }
 
-void debug_vprintf (const char * fmt, va_list ap)
-{
-    vfprintfmt((void *) debug_fputch, NULL, shim_get_tls()->debug_buf,
-               fmt, ap);
+void debug_vprintf(const char* fmt, va_list ap) {
+    vfprintfmt((void*)debug_fputch, NULL, shim_get_tls()->debug_buf, fmt, ap);
 }
 
-void debug_printf (const char * fmt, ...)
-{
+void debug_printf(const char* fmt, ...) {
     va_list ap;
     va_start(ap, fmt);
     debug_vprintf(fmt, ap);
     va_end(ap);
 }
 
-void debug_setprefix (shim_tcb_t * tcb)
-{
+void debug_setprefix(shim_tcb_t* tcb) {
     if (!debug_handle)
         return;
 
-    struct debug_buf * buf = tcb->debug_buf;
+    struct debug_buf* buf = tcb->debug_buf;
     buf->start = buf->end = 0;
 
     if (tcb->tid && !is_internal_tid(tcb->tid))
         fprintfmt(debug_fputch, NULL, buf, TID_PREFIX, tcb->tid);
     else if (cur_process.vmid)
-        fprintfmt(debug_fputch, NULL, buf, VMID_PREFIX,
-                  cur_process.vmid & 0xFFFF);
+        fprintfmt(debug_fputch, NULL, buf, VMID_PREFIX, cur_process.vmid & 0xFFFF);
     else
         fprintfmt(debug_fputch, NULL, buf, NOID_PREFIX);
 
@@ -143,15 +131,11 @@ struct sysbuf {
     char buf[SYSPRINT_BUFFER_SIZE];
 } sys_putdat;
 
-static inline void
-sys_fputs (void * f, const char * str, int len)
-{
-    DkStreamWrite((PAL_HANDLE) f, 0, len, (void *) str, NULL);
+static inline void sys_fputs(void* f, const char* str, int len) {
+    DkStreamWrite((PAL_HANDLE)f, 0, len, (void*)str, NULL);
 }
 
-static void
-sys_fputch (void * f, int ch, void * b)
-{
+static void sys_fputch(void* f, int ch, void* b) {
     __UNUSED(b);
 
     sys_putdat.buf[sys_putdat.cnt++] = ch;
@@ -168,21 +152,17 @@ sys_fputch (void * f, int ch, void * b)
     }
 }
 
-static void
-sys_vfprintf (PAL_HANDLE hdl, const char * fmt, va_list ap)
-{
-    vfprintfmt((void *) &sys_fputch, hdl, NULL, fmt, ap);
+static void sys_vfprintf(PAL_HANDLE hdl, const char* fmt, va_list ap) {
+    vfprintfmt((void*)&sys_fputch, hdl, NULL, fmt, ap);
 }
 
-void handle_printf (PAL_HANDLE hdl, const char * fmt, ...)
-{
+void handle_printf(PAL_HANDLE hdl, const char* fmt, ...) {
     va_list ap;
     va_start(ap, fmt);
     sys_vfprintf(hdl, fmt, ap);
     va_end(ap);
 }
 
-void handle_vprintf (PAL_HANDLE hdl, const char * fmt, va_list ap)
-{
+void handle_vprintf(PAL_HANDLE hdl, const char* fmt, va_list ap) {
     sys_vfprintf(hdl, fmt, ap);
 }

+ 8 - 12
LibOS/shim/src/utils/strobjs.c

@@ -25,32 +25,28 @@
 
 static struct shim_lock str_mgr_lock;
 
-#define SYSTEM_LOCK()       lock(&str_mgr_lock)
-#define SYSTEM_UNLOCK()     unlock(&str_mgr_lock)
+#define SYSTEM_LOCK()   lock(&str_mgr_lock)
+#define SYSTEM_UNLOCK() unlock(&str_mgr_lock)
 
-#define STR_MGR_ALLOC  32
-#define PAGE_SIZE      allocsize
+#define STR_MGR_ALLOC 32
+#define PAGE_SIZE     allocsize
 
 #define OBJ_TYPE struct shim_str
 #include "memmgr.h"
 
 static MEM_MGR str_mgr = NULL;
 
-int init_str_mgr (void)
-{
+int init_str_mgr(void) {
     create_lock(&str_mgr_lock);
     str_mgr = create_mem_mgr(init_align_up(STR_MGR_ALLOC));
     return 0;
 }
 
-struct shim_str * get_str_obj (void)
-{
-    return get_mem_obj_from_mgr_enlarge(str_mgr,
-                                        size_align_up(STR_MGR_ALLOC));
+struct shim_str* get_str_obj(void) {
+    return get_mem_obj_from_mgr_enlarge(str_mgr, size_align_up(STR_MGR_ALLOC));
 }
 
-int free_str_obj (struct shim_str * str)
-{
+int free_str_obj(struct shim_str* str) {
     if (str == NULL)
         return 0;
 

+ 10 - 15
LibOS/shim/src/vdso/vdso.c

@@ -25,13 +25,12 @@
  * The symbols below need to be exported for libsysdb to inject those values,
  * but relocation (.rela.dyn section) isn't wanted in the code generation.
  */
-#define EXPORT_SYMBOL(name) \
-    extern __typeof__(name) __vdso_ ## name __attribute__((alias(#name)))
+#define EXPORT_SYMBOL(name) extern __typeof__(name) __vdso_##name __attribute__((alias(#name)))
 
-static int (*shim_clock_gettime)(clockid_t clock, struct timespec *t) = NULL;
-static int (*shim_gettimeofday)(struct timeval *tv, struct timezone *tz) = NULL;
-static time_t (*shim_time)(time_t *t) = NULL;
-static long (*shim_getcpu)(unsigned *cpu, struct getcpu_cache *unused) = NULL;
+static int (*shim_clock_gettime)(clockid_t clock, struct timespec* t)    = NULL;
+static int (*shim_gettimeofday)(struct timeval* tv, struct timezone* tz) = NULL;
+static time_t (*shim_time)(time_t* t)                                    = NULL;
+static long (*shim_getcpu)(unsigned* cpu, struct getcpu_cache* unused)   = NULL;
 
 EXPORT_SYMBOL(shim_clock_gettime);
 EXPORT_SYMBOL(shim_gettimeofday);
@@ -39,34 +38,30 @@ EXPORT_SYMBOL(shim_time);
 EXPORT_SYMBOL(shim_getcpu);
 
 #define EXPORT_WEAK_SYMBOL(name) \
-    __typeof__(__vdso_ ## name) name __attribute__((weak, alias("__vdso_" #name)))
+    __typeof__(__vdso_##name) name __attribute__((weak, alias("__vdso_" #name)))
 
-int __vdso_clock_gettime(clockid_t clock, struct timespec *t)
-{
+int __vdso_clock_gettime(clockid_t clock, struct timespec* t) {
     if (shim_clock_gettime)
         return (*shim_clock_gettime)(clock, t);
     return -ENOSYS;
 }
 EXPORT_WEAK_SYMBOL(clock_gettime);
 
-int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
-{
+int __vdso_gettimeofday(struct timeval* tv, struct timezone* tz) {
     if (shim_gettimeofday)
         return (*shim_gettimeofday)(tv, tz);
     return -ENOSYS;
 }
 EXPORT_WEAK_SYMBOL(gettimeofday);
 
-time_t __vdso_time(time_t *t)
-{
+time_t __vdso_time(time_t* t) {
     if (shim_time)
         return (*shim_time)(t);
     return -ENOSYS;
 }
 EXPORT_WEAK_SYMBOL(time);
 
-long __vdso_getcpu(unsigned *cpu, struct getcpu_cache *unused)
-{
+long __vdso_getcpu(unsigned* cpu, struct getcpu_cache* unused) {
     if (shim_getcpu)
         return (*shim_getcpu)(cpu, unused);
     return -ENOSYS;

+ 20 - 23
LibOS/shim/test/benchmark/fork_latency.c

@@ -1,18 +1,17 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/wait.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
-#define DO_BENCH 1
-#define NTRIES   100
+#define DO_BENCH   1
+#define NTRIES     100
 #define TEST_TIMES 64
 
 int pids[TEST_TIMES];
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int times = TEST_TIMES;
     int pipes[6];
     int i = 0;
@@ -27,7 +26,7 @@ int main(int argc, char ** argv)
     pipe(&pipes[2]);
     pipe(&pipes[4]);
 
-    for (i = 0 ; i < times ; i++ ) {
+    for (i = 0; i < times; i++) {
         pids[i] = fork();
 
         if (pids[i] < 0) {
@@ -46,7 +45,7 @@ int main(int argc, char ** argv)
             struct timeval timevals[2];
             gettimeofday(&timevals[0], NULL);
 
-            for (int count = 0 ; count < NTRIES ; count++) {
+            for (int count = 0; count < NTRIES; count++) {
                 int child = fork();
 
                 if (!child)
@@ -79,15 +78,13 @@ int main(int argc, char ** argv)
     close(pipes[1]);
 
     unsigned long long start_time = 0;
-    unsigned long long end_time = 0;
+    unsigned long long end_time   = 0;
     unsigned long long total_time = 0;
     struct timeval timevals[2];
-    for (int i = 0 ; i < times ; i++) {
+    for (int i = 0; i < times; i++) {
         read(pipes[2], timevals, sizeof(struct timeval) * 2);
-        unsigned long s = timevals[0].tv_sec * 1000000ULL +
-                          timevals[0].tv_usec;
-        unsigned long e = timevals[1].tv_sec * 1000000ULL +
-                          timevals[1].tv_usec;
+        unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
+        unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
         if (!start_time || s < start_time)
             start_time = s;
         if (!end_time || e > end_time)
@@ -99,15 +96,15 @@ int main(int argc, char ** argv)
     write(pipes[5], bytes, times);
     close(pipes[5]);
 
-    for (i = 0 ; i < times ; i++)
+    for (i = 0; i < times; i++) {
         waitpid(pids[i], NULL, 0);
+    }
 
-    printf("%d processes fork %d children: throughput = %lf procs/second, "
-           "latency = %lf microseconds\n",
-           times, NTRIES,
-           1.0 * NTRIES * times * 1000000 / (end_time - start_time),
-           1.0 * total_time / (NTRIES * times));
-
+    printf(
+        "%d processes fork %d children: throughput = %lf procs/second, "
+        "latency = %lf microseconds\n",
+        times, NTRIES, 1.0 * NTRIES * times * 1000000 / (end_time - start_time),
+        1.0 * total_time / (NTRIES * times));
 
     return 0;
 }

+ 14 - 19
LibOS/shim/test/benchmark/rpc_latency.libos.c

@@ -1,16 +1,15 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/wait.h>
+#include <shim_unistd.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
-#include <shim_unistd.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
-#define NTRIES    10000
+#define NTRIES     10000
 #define TEST_TIMES 32
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int times = TEST_TIMES;
     int pipes[6];
     int pids[TEST_TIMES][2];
@@ -26,7 +25,7 @@ int main(int argc, char ** argv)
     pipe(&pipes[2]);
     pipe(&pipes[4]);
 
-    for (i = 0 ; i < times ; i++ ) {
+    for (i = 0; i < times; i++) {
         pids[i][0] = fork();
 
         if (pids[i][0] < 0) {
@@ -89,14 +88,12 @@ int main(int argc, char ** argv)
     close(pipes[1]);
 
     unsigned long long start_time = 0;
-    unsigned long long end_time = 0;
+    unsigned long long end_time   = 0;
     struct timeval timevals[2];
-    for (int i = 0 ; i < times ; i++) {
+    for (int i = 0; i < times; i++) {
         read(pipes[4], timevals, sizeof(struct timeval) * 2);
-        unsigned long s = timevals[0].tv_sec * 1000000ULL +
-                          timevals[0].tv_usec;
-        unsigned long e = timevals[1].tv_sec * 1000000ULL +
-                          timevals[1].tv_usec;
+        unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
+        unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
         if (!start_time || s < start_time)
             start_time = s;
         if (!end_time || e > end_time)
@@ -107,15 +104,13 @@ int main(int argc, char ** argv)
     write(pipes[3], bytes, times * 2);
     close(pipes[3]);
 
-    for (i = 0 ; i < times ; i++) {
+    for (i = 0; i < times; i++) {
         waitpid(pids[i][0], NULL, 0);
         waitpid(pids[i][1], NULL, 0);
     }
 
-    printf("throughput for %d processes to send %d message: %lf bytes/second\n",
-           times, NTRIES,
+    printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
            1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
 
-
     return 0;
 }

+ 18 - 22
LibOS/shim/test/benchmark/rpc_latency2.libos.c

@@ -1,16 +1,15 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/wait.h>
+#include <shim_unistd.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
-#include <shim_unistd.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
-#define NTRIES    10000
+#define NTRIES     10000
 #define TEST_TIMES 32
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int times = TEST_TIMES;
     int pipes[6];
     int pids[TEST_TIMES][2];
@@ -26,7 +25,7 @@ int main(int argc, char ** argv)
     pipe(&pipes[2]);
     pipe(&pipes[4]);
 
-    for (i = 0 ; i < times ; i++ ) {
+    for (i = 0; i < times; i++) {
         pids[i][0] = fork();
 
         if (pids[i][0] < 0) {
@@ -42,10 +41,10 @@ int main(int argc, char ** argv)
             close(pipes[5]);
 
             char byte;
-            for (int i = 0 ; i < NTRIES ; i++) {
+            for (int i = 0; i < NTRIES; i++) {
                 pid_t pid;
                 recv_rpc(&pid, &byte, 1);
-                send_rpc(pid,  &byte, 1);
+                send_rpc(pid, &byte, 1);
             }
 
             read(pipes[2], &byte, 1);
@@ -72,8 +71,8 @@ int main(int argc, char ** argv)
             gettimeofday(&timevals[0], NULL);
 
             pid_t pid = pids[i][0];
-            for (int i = 0 ; i < NTRIES ; i++) {
-                send_rpc(pid,  &byte, 1);
+            for (int i = 0; i < NTRIES; i++) {
+                send_rpc(pid, &byte, 1);
                 recv_rpc(NULL, &byte, 1);
             }
 
@@ -100,14 +99,12 @@ int main(int argc, char ** argv)
     close(pipes[1]);
 
     unsigned long long start_time = 0;
-    unsigned long long end_time = 0;
+    unsigned long long end_time   = 0;
     struct timeval timevals[2];
-    for (int i = 0 ; i < times ; i++) {
+    for (int i = 0; i < times; i++) {
         read(pipes[4], timevals, sizeof(struct timeval) * 2);
-        unsigned long s = timevals[0].tv_sec * 1000000ULL +
-                          timevals[0].tv_usec;
-        unsigned long e = timevals[1].tv_sec * 1000000ULL +
-                          timevals[1].tv_usec;
+        unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
+        unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
         if (!start_time || s < start_time)
             start_time = s;
         if (!end_time || e > end_time)
@@ -118,13 +115,12 @@ int main(int argc, char ** argv)
     write(pipes[3], bytes, times * 2);
     close(pipes[3]);
 
-    for (i = 0 ; i < times ; i++) {
+    for (i = 0; i < times; i++) {
         waitpid(pids[i][0], NULL, 0);
         waitpid(pids[i][1], NULL, 0);
     }
 
-    printf("throughput for %d processes to send %d message: %lf bytes/second\n",
-           times, NTRIES,
+    printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
            1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
 
     return 0;

+ 31 - 35
LibOS/shim/test/benchmark/sig_latency.c

@@ -1,13 +1,13 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/wait.h>
+#include <sched.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
-#include <sched.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 #define DO_BENCH
-#define NTRIES   1000
+#define NTRIES     1000
 #define TEST_TIMES 32
 
 int count = 0;
@@ -15,42 +15,38 @@ int pids[TEST_TIMES][2];
 int firstpid;
 int secondpid;
 
-void sighand1 (int signum, siginfo_t * sinfo, void * ucontext)
-{
+void sighand1(int signum, siginfo_t* sinfo, void* ucontext) {
     count++;
 #ifndef DO_BENCH
     if (count % 100 == 0)
         printf("Received a SIGUSR1 (%d) (count = %d) from %d\n", signum, count, sinfo->si_pid);
 #endif
     if (count > NTRIES)
-       return;
+        return;
 
     kill(secondpid, SIGUSR1);
 }
 
-void sighand2 (int signum, siginfo_t * sinfo, void * ucontext)
-{
+void sighand2(int signum, siginfo_t* sinfo, void* ucontext) {
     count++;
 #ifndef DO_BENCH
     if (count % 100 == 0)
         printf("Received a SIGUSR1 (%d) (count = %d) from %d\n", signum, count, sinfo->si_pid);
 #endif
     if (count > NTRIES)
-       return;
+        return;
 
     kill(firstpid, SIGUSR1);
 }
 
-void (*sighand) (int signum, siginfo_t * sinfo, void * ucontext) = NULL;
+void (*sighand)(int signum, siginfo_t* sinfo, void* ucontext) = NULL;
 
-void sigact (int signum, siginfo_t * sinfo, void * ucontext)
-{
+void sigact(int signum, siginfo_t* sinfo, void* ucontext) {
     if (sighand)
         sighand(signum, sinfo, ucontext);
 }
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int times = TEST_TIMES;
     int pipes[8];
     int i = 0;
@@ -63,14 +59,14 @@ int main(int argc, char ** argv)
 
     setvbuf(stdout, NULL, _IONBF, 0);
 
-    signal(SIGUSR1, (void *) sigact);
+    signal(SIGUSR1, (void*)sigact);
 
     pipe(&pipes[0]);
     pipe(&pipes[2]);
     pipe(&pipes[4]);
     pipe(&pipes[6]);
 
-    for (i = 0 ; i < times ; i++ ) {
+    for (i = 0; i < times; i++) {
         pids[i][0] = fork();
 
         if (pids[i][0] < 0) {
@@ -96,8 +92,9 @@ int main(int argc, char ** argv)
             write(pipes[5], &byte, 1);
             close(pipes[5]);
 
-            while(count < NTRIES)
+            while (count < NTRIES) {
                 sched_yield();
+            }
 
             read(pipes[2], &byte, 1);
             close(pipes[2]);
@@ -120,7 +117,7 @@ int main(int argc, char ** argv)
             close(pipes[6]);
 
             firstpid = pids[i][0];
-            int pid = getpid();
+            int pid  = getpid();
             write(pipes[7], &pid, sizeof(int));
             close(pipes[7]);
 
@@ -134,8 +131,9 @@ int main(int argc, char ** argv)
             count = 0;
             kill(firstpid, SIGUSR1);
 
-            while (count < NTRIES - 1)
+            while (count < NTRIES - 1) {
                 sched_yield();
+            }
 
             gettimeofday(&timevals[1], NULL);
 
@@ -157,9 +155,10 @@ int main(int argc, char ** argv)
     close(pipes[6]);
     close(pipes[7]);
 
-    for (int i = 0 ; i < times * 2 ; i++) {
+    for (int i = 0; i < times * 2; i++) {
         char i;
-        while (read(pipes[4], &i, 1) < 0);
+        while (read(pipes[4], &i, 1) < 0)
+            ;
     }
 
     printf("all processes ready\n");
@@ -170,14 +169,13 @@ int main(int argc, char ** argv)
     close(pipes[1]);
 
     unsigned long long start_time = 0;
-    unsigned long long end_time = 0;
+    unsigned long long end_time   = 0;
     struct timeval timevals[2];
-    for (int i = 0 ; i < times ; i++) {
-        while (read(pipes[4], timevals, sizeof(struct timeval) * 2) < 0);
-        unsigned long s = timevals[0].tv_sec * 1000000ULL +
-                          timevals[0].tv_usec;
-        unsigned long e = timevals[1].tv_sec * 1000000ULL +
-                          timevals[1].tv_usec;
+    for (int i = 0; i < times; i++) {
+        while (read(pipes[4], timevals, sizeof(struct timeval) * 2) < 0)
+            ;
+        unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
+        unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
         if (!start_time || s < start_time)
             start_time = s;
         if (!end_time || e > end_time)
@@ -188,15 +186,13 @@ int main(int argc, char ** argv)
     write(pipes[3], bytes, times * 2);
     close(pipes[3]);
 
-    for (i = 0 ; i < times ; i++) {
+    for (i = 0; i < times; i++) {
         waitpid(pids[i][0], NULL, 0);
         waitpid(pids[i][1], NULL, 0);
     }
 
-    printf("throughput for %d processes to send %d signals: %lf signals/second\n",
-           times, NTRIES,
+    printf("throughput for %d processes to send %d signals: %lf signals/second\n", times, NTRIES,
            1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
 
-
     return 0;
 }

+ 7 - 7
LibOS/shim/test/benchmark/start.c

@@ -1,11 +1,10 @@
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
 
-#define OVERHEAD_TIMES  30000
+#define OVERHEAD_TIMES 30000
 
-int main (int argc, char ** argv, char ** envp)
-{
+int main(int argc, char** argv, char** envp) {
     struct timeval tv;
     gettimeofday(&tv, NULL);
 
@@ -17,11 +16,12 @@ int main (int argc, char ** argv, char ** envp)
 
     struct timeval tv1, tv2;
     gettimeofday(&tv1, NULL);
-    for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
+    for (int j = 0; j < OVERHEAD_TIMES; j++) {
         gettimeofday(&tv, NULL);
+    }
     gettimeofday(&tv2, NULL);
-    unsigned long long msec3 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
-    unsigned long long msec4 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
+    unsigned long long msec3    = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
+    unsigned long long msec4    = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
     unsigned long long overhead = (msec4 - msec3) / OVERHEAD_TIMES;
 
     printf("%llu\n", msec2 - msec1 - overhead);

+ 25 - 26
LibOS/shim/test/benchmark/test_start.m.c

@@ -1,10 +1,10 @@
-#include <stdlib.h>
+#include <math.h>
 #include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
-#include <sys/wait.h>
 #include <sys/time.h>
-#include <math.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 /*
  *  USAGE:
@@ -15,24 +15,23 @@
  *      ./test_start ./libpal.so    => graphene start time
  */
 
-#define OVERHEAD_TIMES  30000
-#define TEST_TIMES      1000
+#define OVERHEAD_TIMES 30000
+#define TEST_TIMES     1000
 
-void get_time (char * time_arg, unsigned long overhead)
-{
+void get_time(char* time_arg, unsigned long overhead) {
     struct timeval tv;
     gettimeofday(&tv, NULL);
     unsigned long long msec = tv.tv_sec * 1000000ULL + tv.tv_usec;
     snprintf(time_arg, 30, "%llu", msec + overhead);
 }
 
-int main (int argc, char ** argv, char ** envp)
-{
-    char * new_argv[argc + 2];
+int main(int argc, char** argv, char** envp) {
+    char* new_argv[argc + 2];
     char time_arg[30];
 
-    for (int i = 1 ; i < argc ; i++)
+    for (int i = 1; i < argc; i++) {
         new_argv[i - 1] = argv[i];
+    }
 
     new_argv[argc - 1] = "./start";
     new_argv[argc]     = time_arg;
@@ -42,7 +41,7 @@ int main (int argc, char ** argv, char ** envp)
     unsigned long long sum = 0, ssum = 0;
     memset(times, 0, sizeof(times));
 
-    for (int i = 1 ; i < TEST_TIMES ; i++) {
+    for (int i = 1; i < TEST_TIMES; i++) {
         int pipes[2];
         if (pipe(pipes) < 0)
             break;
@@ -55,11 +54,12 @@ int main (int argc, char ** argv, char ** envp)
         if (!pid) {
             struct timeval tv1, tv2;
             gettimeofday(&tv1, NULL);
-            for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
+            for (int j = 0; j < OVERHEAD_TIMES; j++) {
                 get_time(time_arg, 0);
+            }
             gettimeofday(&tv2, NULL);
-            unsigned long long msec1 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
-            unsigned long long msec2 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
+            unsigned long long msec1    = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
+            unsigned long long msec2    = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
             unsigned long long overhead = (msec2 - msec1) / OVERHEAD_TIMES;
 
             close(pipes[0]);
@@ -86,22 +86,21 @@ int main (int argc, char ** argv, char ** envp)
         close(pipes[0]);
     }
 
-    int compar (const void * arg1, const void * arg2)
-    {
-        register unsigned long long a1 = *((unsigned long long *) arg1);
-        register unsigned long long a2 = *((unsigned long long *) arg2);
+    int compar(const void* arg1, const void* arg2) {
+        register unsigned long long a1 = *((unsigned long long*)arg1);
+        register unsigned long long a2 = *((unsigned long long*)arg2);
         return a1 < a2 ? -1 : (a1 == a2 ? 0 : 1);
     }
 
     qsort(times, TEST_TIMES, sizeof(unsigned long long), compar);
 
-    double median =
-            (TEST_TIMES % 2) ? (double) times[TEST_TIMES / 2] :
-            (double) (times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
+    double median = (TEST_TIMES % 2)
+                        ? (double)times[TEST_TIMES / 2]
+                        : (double)(times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
 
-    double mean = (double) sum / TEST_TIMES;
-    double stddev = sqrt((double) ssum / TEST_TIMES - mean * mean);
-    double ci = 1.96 * stddev / sqrt((double) TEST_TIMES);
+    double mean   = (double)sum / TEST_TIMES;
+    double stddev = sqrt((double)ssum / TEST_TIMES - mean * mean);
+    double ci     = 1.96 * stddev / sqrt((double)TEST_TIMES);
 
     printf("median = %lf, mean = %lf (+/-%lf)\n", median, mean, ci);
 

+ 4 - 6
LibOS/shim/test/inline/fork.c

@@ -1,8 +1,7 @@
-#include <shim_table.h>
 #include <errno.h>
+#include <shim_table.h>
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     pid_t pid = shim_fork();
 
     if (pid < 0) {
@@ -12,11 +11,10 @@ int main (int argc, char ** argv)
 
     if (pid == 0) {
         shim_write(1, "Hello, Dad!\n", 12);
-    }
-    else {
+    } else {
         shim_write(1, "Hello, Kid!\n", 12);
     }
 
     shim_exit_group(0);
-    return 0; // should not reach here.
+    return 0;  // should not reach here.
 }

+ 2 - 3
LibOS/shim/test/inline/helloworld.c

@@ -1,8 +1,7 @@
 #include <shim_table.h>
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     shim_write(1, "Hello world\n", 12);
     shim_exit_group(0);
-    return 0; // should not reach here.
+    return 0;  // should not reach here.
 }

+ 4 - 6
LibOS/shim/test/inline/vfork.c

@@ -1,8 +1,7 @@
-#include <shim_table.h>
 #include <errno.h>
+#include <shim_table.h>
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     pid_t pid = shim_vfork();
 
     if (pid < 0) {
@@ -12,11 +11,10 @@ int main (int argc, char ** argv)
 
     if (pid == 0) {
         shim_write(1, "Hello, Dad!\n", 12);
-    }
-    else {
+    } else {
         shim_write(1, "Hello, Kid!\n", 12);
     }
 
     shim_exit_group(0);
-    return 0; // should not reach here.
+    return 0;  // should not reach here.
 }

+ 5 - 7
LibOS/shim/test/native/alarm.c

@@ -1,15 +1,13 @@
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
 
-void handler (int signal)
-{
+void handler(int signal) {
     printf("alarm goes off\n");
 }
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     signal(SIGALRM, &handler);
     alarm(1);
     sleep(3);

+ 2 - 3
LibOS/shim/test/native/brk.c

@@ -2,9 +2,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-int main(int argc, char ** argv)
-{
-    char * mem = malloc(40);
+int main(int argc, char** argv) {
+    char* mem = malloc(40);
     sprintf(mem, "Hello world (%s)!\n", argv[0]);
     printf("%s", mem);
     return 0;

+ 17 - 21
LibOS/shim/test/native/clone.c

@@ -1,31 +1,29 @@
 #define _GNU_SOURCE
+#include <asm/prctl.h>
 #include <malloc.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <signal.h>
 #include <sched.h>
+#include <signal.h>
 #include <stdio.h>
 #include <sys/syscall.h>
-#include <asm/prctl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 // 64kB stack
 #define FIBER_STACK (1024 * 64)
 
 __thread int mypid = 0;
 
-unsigned long gettls (void)
-{
+unsigned long gettls(void) {
     unsigned long tls;
     syscall(__NR_arch_prctl, ARCH_GET_FS, &tls);
     return tls;
 }
 
-int thread_function (void * argument)
-{
-    mypid = getpid();
-    int * ptr = (int *) argument;
-    printf("in the child: pid (%016lx) = %d\n", (unsigned long) &mypid, mypid);
+int thread_function(void* argument) {
+    mypid    = getpid();
+    int* ptr = (int*)argument;
+    printf("in the child: pid (%016lx) = %d\n", (unsigned long)&mypid, mypid);
     printf("in the child: pid = %d\n", getpid());
     printf("in the child: tls = %08lx\n", gettls());
     printf("child thread exiting\n");
@@ -33,9 +31,8 @@ int thread_function (void * argument)
     return 0;
 }
 
-int main (int argc, const char ** argv)
-{
-    void * stack;
+int main(int argc, const char** argv) {
+    void* stack;
     pid_t pid;
     int varx = 143;
 
@@ -50,13 +47,12 @@ int main (int argc, const char ** argv)
         _exit(1);
     }
 
-    printf("child_stack: %016lx-%016lx\n", (unsigned long) stack,
-           (unsigned long) stack + FIBER_STACK);
+    printf("child_stack: %016lx-%016lx\n", (unsigned long)stack,
+           (unsigned long)stack + FIBER_STACK);
 
     // Call the clone system call to create the child thread
-    pid = clone(&thread_function, (void *) stack + FIBER_STACK,
-                CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM,
-                &varx);
+    pid = clone(&thread_function, (void*)stack + FIBER_STACK,
+                CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_VM, &varx);
 
     printf("clone() creates new thread %d\n", pid);
 
@@ -75,7 +71,7 @@ int main (int argc, const char ** argv)
     // Free the stack
     free(stack);
 
-    printf("in the parent: pid (%016lx) = %d\n", (unsigned long) &mypid, mypid);
+    printf("in the parent: pid (%016lx) = %d\n", (unsigned long)&mypid, mypid);
     printf("in the parent: pid = %d\n", getpid());
     printf("in the parent: tls = %08lx\n", gettls());
 

+ 36 - 39
LibOS/shim/test/native/condvar.pthread.c

@@ -1,69 +1,66 @@
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <pthread.h>
 
 pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
-pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;
+pthread_cond_t condvar      = PTHREAD_COND_INITIALIZER;
 
-void * function1();
-void * function2();
+void* function1();
+void* function2();
 int count = 0;
 
 #define COUNT_DONE  10
-#define COUNT_HALT1  3
-#define COUNT_HALT2  6
+#define COUNT_HALT1 3
+#define COUNT_HALT2 6
 
-int main (int argc, const char ** argv)
-{
-   pthread_t thread1, thread2;
+int main(int argc, const char** argv) {
+    pthread_t thread1, thread2;
 
-   pthread_create(&thread1, NULL, &function1, NULL);
-   pthread_create(&thread2, NULL, &function2, NULL);
+    pthread_create(&thread1, NULL, &function1, NULL);
+    pthread_create(&thread2, NULL, &function2, NULL);
 
-   pthread_join(thread1, NULL);
-   pthread_join(thread2, NULL);
+    pthread_join(thread1, NULL);
+    pthread_join(thread2, NULL);
 
-   printf("Final count: %d\n", count);
-   return 0;
+    printf("Final count: %d\n", count);
+    return 0;
 }
 
-void * function1 (void)
-{
-   for(;;) {
-      // Lock mutex and then wait for signal to relase mutex
-      pthread_mutex_lock(&count_mutex);
+void* function1(void) {
+    for (;;) {
+        // Lock mutex and then wait for signal to relase mutex
+        pthread_mutex_lock(&count_mutex);
 
-      // Wait while functionCount2() operates on count
-      // mutex unlocked if condition varialbe in functionCount2() signaled.
-      pthread_cond_wait(&condvar, &count_mutex);
-      count++;
-      printf("Counter value in function1: %d\n", count);
+        // Wait while functionCount2() operates on count
+        // mutex unlocked if condition varialbe in functionCount2() signaled.
+        pthread_cond_wait(&condvar, &count_mutex);
+        count++;
+        printf("Counter value in function1: %d\n", count);
 
-      pthread_mutex_unlock(&count_mutex);
+        pthread_mutex_unlock(&count_mutex);
 
-      if (count >= COUNT_DONE)
-          return NULL;
-   }
+        if (count >= COUNT_DONE)
+            return NULL;
+    }
 }
 
-void * function2 (void)
-{
-    for(;;) {
-       pthread_mutex_lock(&count_mutex);
+void* function2(void) {
+    for (;;) {
+        pthread_mutex_lock(&count_mutex);
 
-       if (count < COUNT_HALT1 || count > COUNT_HALT2) {
+        if (count < COUNT_HALT1 || count > COUNT_HALT2) {
             // Condition of if statement has been met.
             // Signal to free waiting thread by freeing the mutex.
             // Note: functionCount1() is now permitted to modify "count".
             pthread_cond_signal(&condvar);
-       } else {
+        } else {
             count++;
             printf("Counter value function2: %d\n", count);
-       }
+        }
 
-       pthread_mutex_unlock(&count_mutex);
+        pthread_mutex_unlock(&count_mutex);
 
-       if (count >= COUNT_DONE)
-           return NULL;
+        if (count >= COUNT_DONE)
+            return NULL;
     }
 }

+ 7 - 7
LibOS/shim/test/native/cpuinfo.c

@@ -1,19 +1,19 @@
 #define _GNU_SOURCE
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 
-int main (int argc, char ** argv)
-{
-    FILE * cpuinfo = fopen("/proc/cpuinfo", "rb");
-    char * arg = 0;
-    size_t size = 0;
+int main(int argc, char** argv) {
+    FILE* cpuinfo = fopen("/proc/cpuinfo", "rb");
+    char* arg     = 0;
+    size_t size   = 0;
 
     if (!cpuinfo)
         return errno;
 
-    while(getdelim(&arg, &size, 0, cpuinfo) != -1)
+    while (getdelim(&arg, &size, 0, cpuinfo) != -1) {
         puts(arg);
+    }
 
     free(arg);
     fclose(cpuinfo);

+ 5 - 6
LibOS/shim/test/native/dir.c

@@ -1,14 +1,13 @@
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <errno.h>
-#include <sys/stat.h>
 #include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #define TESTDIR "testdir"
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int ret = 0;
 
     if ((ret = rmdir(TESTDIR)) < 0 && errno != ENOENT) {

+ 5 - 7
LibOS/shim/test/native/divzero.c

@@ -1,17 +1,15 @@
-#include <stdlib.h>
-#include <stdio.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 
-void handler (int signal)
-{
+void handler(int signal) {
     printf("get signal: %d\n", signal);
     exit(0);
 }
 
-int main (void)
-{
+int main(void) {
     int i = 0;
     signal(SIGFPE, &handler);
-    i =  1 / i;
+    i = 1 / i;
     return 0;
 }

+ 2 - 3
LibOS/shim/test/native/dup.c

@@ -1,11 +1,10 @@
 #include <stdio.h>
 #include <unistd.h>
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     dup2(1, 255);
     close(1);
-    FILE * new_stdout = fdopen(255, "a");
+    FILE* new_stdout = fdopen(255, "a");
     fprintf(new_stdout, "Hello World!\n");
     fflush(new_stdout);
     return 0;

+ 12 - 12
LibOS/shim/test/native/epoll.c

@@ -1,15 +1,14 @@
-#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <sys/epoll.h>
-#include <fcntl.h>
-#include <errno.h>
+#include <unistd.h>
 
-#define TEST_TIMES    4
+#define TEST_TIMES 4
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int ret = 0;
     int fds[TEST_TIMES][2];
 
@@ -21,14 +20,14 @@ int main (int argc, char ** argv)
 
     struct epoll_event event;
 
-    for (int i = 0 ; i < TEST_TIMES ; i++) {
+    for (int i = 0; i < TEST_TIMES; i++) {
         ret = pipe(fds[i]);
         if (ret < 0) {
             perror("pipe");
             exit(1);
         }
 
-        event.events = EPOLLIN;
+        event.events  = EPOLLIN;
         event.data.fd = fds[i][0];
 
         ret = epoll_ctl(efd, EPOLL_CTL_ADD, fds[i][0], &event);
@@ -45,7 +44,7 @@ int main (int argc, char ** argv)
     }
 
     if (!ret) {
-        for (int i = 0 ; i < TEST_TIMES ; i++) {
+        for (int i = 0; i < TEST_TIMES; i++) {
             close(fds[i][0]);
             char c = 0;
             write(fds[i][1], &c, 1);
@@ -55,10 +54,11 @@ int main (int argc, char ** argv)
         exit(0);
     }
 
-    for (int i = 0 ; i < TEST_TIMES ; i++)
+    for (int i = 0; i < TEST_TIMES; i++) {
         close(fds[i][1]);
+    }
 
-    for (int i = 0 ; i < TEST_TIMES ; i++) {
+    for (int i = 0; i < TEST_TIMES; i++) {
         ret = epoll_wait(efd, &event, 1, -1);
         if (ret < 0) {
             perror("epoll_wait");

+ 39 - 49
LibOS/shim/test/native/epoll_socket.c

@@ -4,94 +4,89 @@
 
 // Meant to be used for edge triggered epoll
 
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+#include <sys/epoll.h>
 #include <sys/socket.h>
-#include <netdb.h>
+#include <sys/types.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <sys/epoll.h>
-#include <errno.h>
 
 #define MAXEVENTS 64
 
-static int
-make_socket_non_blocking (int sfd)
-{
+static int make_socket_non_blocking(int sfd) {
     int flags, s;
 
-    flags = fcntl (sfd, F_GETFL, 0);
+    flags = fcntl(sfd, F_GETFL, 0);
     if (flags == -1) {
-        perror ("fcntl");
+        perror("fcntl");
         return -1;
     }
 
     flags |= O_NONBLOCK;
-    s = fcntl (sfd, F_SETFL, flags);
+    s = fcntl(sfd, F_SETFL, flags);
     if (s == -1) {
-        perror ("fcntl");
+        perror("fcntl");
         return -1;
     }
 
     return 0;
 }
 
-static int
-create_and_bind (char *port)
-{
+static int create_and_bind(char* port) {
     struct addrinfo hints;
     struct addrinfo *result, *rp;
     int s, sfd;
 
-    memset (&hints, 0, sizeof (struct addrinfo));
-    hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family   = AF_UNSPEC;   /* Return IPv4 and IPv6 choices */
     hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
-    hints.ai_flags = AI_PASSIVE;     /* All interfaces */
+    hints.ai_flags    = AI_PASSIVE;  /* All interfaces */
 
-    s = getaddrinfo (NULL, port, &hints, &result);
+    s = getaddrinfo(NULL, port, &hints, &result);
     if (s != 0) {
-        fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
         return -1;
     }
 
     for (rp = result; rp != NULL; rp = rp->ai_next) {
-        sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
         if (sfd == -1)
             continue;
 
-        s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
+        s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
         if (s == 0)
             /* We managed to bind successfully! */
             break;
 
-        close (sfd);
+        close(sfd);
     }
 
     if (rp == NULL) {
-        fprintf (stderr, "Could not bind\n");
+        fprintf(stderr, "Could not bind\n");
         return -1;
     }
 
-    freeaddrinfo (result);
+    freeaddrinfo(result);
 
     return sfd;
 }
 
-int main (int argc, char *argv[])
-{
+int main(int argc, char* argv[]) {
     int sfd, s;
     int efd;
     struct epoll_event event;
-    struct epoll_event *events;
+    struct epoll_event* events;
 
     if (argc != 2) {
         fprintf(stderr, "Usage: %s [port]\n", argv[0]);
         exit(EXIT_FAILURE);
     }
 
-    sfd = create_and_bind (argv[1]);
+    sfd = create_and_bind(argv[1]);
     if (sfd == -1)
         abort();
 
@@ -112,8 +107,8 @@ int main (int argc, char *argv[])
     }
 
     event.data.fd = sfd;
-    event.events = EPOLLIN | EPOLLET;
-    s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
+    event.events  = EPOLLIN | EPOLLET;
+    s             = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
     if (s == -1) {
         perror("epoll_ctl");
         abort();
@@ -128,8 +123,7 @@ int main (int argc, char *argv[])
 
         n = epoll_wait(efd, events, MAXEVENTS, -1);
         for (i = 0; i < n; i++) {
-            if ((events[i].events & EPOLLERR) ||
-                (events[i].events & EPOLLHUP) ||
+            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) ||
                 (!(events[i].events & EPOLLIN))) {
                 /* An error has occured on this fd, or the socket is not
                    ready for reading (why were we notified then?) */
@@ -148,26 +142,24 @@ int main (int argc, char *argv[])
                     char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
 
                     in_len = sizeof in_addr;
-                    infd = accept (sfd, &in_addr, &in_len);
+                    infd   = accept(sfd, &in_addr, &in_len);
                     if (infd == -1) {
-                        if ((errno == EAGAIN) ||
-                            (errno == EWOULDBLOCK)) {
+                        if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                             /* We have processed all incoming
                                connections. */
                             break;
                         } else {
-                            perror ("accept");
+                            perror("accept");
                             break;
                         }
                     }
 
-                    s = getnameinfo(&in_addr, in_len,
-                                    hbuf, sizeof hbuf,
-                                    sbuf, sizeof sbuf,
+                    s = getnameinfo(&in_addr, in_len, hbuf, sizeof hbuf, sbuf, sizeof sbuf,
                                     NI_NUMERICHOST | NI_NUMERICSERV);
                     if (s == 0)
-                        printf("Accepted connection on descriptor %d "
-                               "(host=%s, port=%s)\n", infd, hbuf, sbuf);
+                        printf(
+                            "Accepted connection on descriptor %d (host=%s, port=%s)\n",
+                            infd, hbuf, sbuf);
 
                     /* Make the incoming socket non-blocking and add it to the
                        list of fds to monitor. */
@@ -176,8 +168,8 @@ int main (int argc, char *argv[])
                         abort();
 
                     event.data.fd = infd;
-                    event.events = EPOLLIN | EPOLLET;
-                    s = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
+                    event.events  = EPOLLIN | EPOLLET;
+                    s             = epoll_ctl(efd, EPOLL_CTL_ADD, infd, &event);
                     if (s == -1) {
                         perror("epoll_ctl");
                         abort();
@@ -201,7 +193,7 @@ int main (int argc, char *argv[])
                         /* If errno == EAGAIN, that means we have read all
                            data. So go back to the main loop. */
                         if (errno != EAGAIN) {
-                            perror ("read");
+                            perror("read");
                             done = 1;
                         }
                         break;
@@ -221,12 +213,11 @@ int main (int argc, char *argv[])
                 }
 
                 if (done) {
-                    printf("Closed connection on descriptor %d\n",
-                           events[i].data.fd);
+                    printf("Closed connection on descriptor %d\n", events[i].data.fd);
 
                     /* Closing the descriptor will make epoll remove it
                        from the set of descriptors which are monitored. */
-                    close (events[i].data.fd);
+                    close(events[i].data.fd);
                 }
             }
         }
@@ -237,4 +228,3 @@ int main (int argc, char *argv[])
 
     return EXIT_SUCCESS;
 }
-

+ 2 - 2
LibOS/shim/test/native/errno.c

@@ -1,7 +1,7 @@
-#include <stdio.h>
 #include <errno.h>
+#include <stdio.h>
 
-int main(int argc, char ** argv) {
+int main(int argc, char** argv) {
     errno = EINVAL;
     printf("errno = %d\n", errno);
     return 0;

+ 2 - 2
LibOS/shim/test/native/exec_fork.c

@@ -1,9 +1,9 @@
-#include <stdlib.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 int main() {
-    char * argv[] = { "./fork", NULL };
+    char* argv[] = {"./fork", NULL};
     execv(argv[0], argv);
     return 0;
 }

+ 3 - 4
LibOS/shim/test/native/file.c

@@ -1,9 +1,8 @@
+#include <fcntl.h>
 #include <stdio.h>
 #include <unistd.h>
-#include <fcntl.h>
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int fd1 = creat("testfile", 0600);
 
     if (fd1 < 0) {
@@ -34,7 +33,7 @@ int main(int argc, char ** argv)
     close(fd2);
     unlink("testfile");
 
-    int fd3 = open("testfile", O_RDWR|O_CREAT|O_EXCL, 0600);
+    int fd3 = open("testfile", O_RDWR | O_CREAT | O_EXCL, 0600);
 
     if (fd3 < 0) {
         perror("open with O_CREAT and O_EXCL");

+ 3 - 4
LibOS/shim/test/native/fork.c

@@ -1,12 +1,11 @@
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-#include <unistd.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
-int main (int argc, const char ** argv)
-{
+int main(int argc, const char** argv) {
     pid_t pid1 = fork();
 
     if (pid1 < 0) {

+ 21 - 25
LibOS/shim/test/native/fork_bomb.c

@@ -1,42 +1,39 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/wait.h>
 #include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/time.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 int firstpid;
 int secondpid;
 
-#define NTRIES 1000
+#define NTRIES     1000
 #define TEST_TIMES 32
-#define DO_BENCH 1
+#define DO_BENCH   1
 
 int count = 0;
 
-void sighand1 (int signum)
-{
+void sighand1(int signum) {
     count++;
 #ifndef DO_BENCH
-    printf("%d receive a SIGUSR (count = %d)\n",getpid(), count);
+    printf("%d receive a SIGUSR (count = %d)\n", getpid(), count);
 #endif
     kill(secondpid, SIGUSR1);
 }
 
-void sighand2 (int signum)
-{
+void sighand2(int signum) {
     count++;
 #ifndef DO_BENCH
-    printf("%d receive a SIGUSR (count = %d)\n",getpid(), count);
+    printf("%d receive a SIGUSR (count = %d)\n", getpid(), count);
 #endif
     kill(firstpid, SIGUSR1);
 }
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int times = TEST_TIMES;
 
-    for (int i = 0 ; i < times; i++) {
+    for (int i = 0; i < times; i++) {
         int pipes[2];
 
         pipe(pipes);
@@ -54,20 +51,21 @@ int main(int argc, char ** argv)
             signal(SIGUSR1, sighand1);
             read(pipes[0], &secondpid, sizeof(int));
 #ifndef DO_BENCH
-            printf("%d killing %d\n",getpid(), secondpid);
+            printf("%d killing %d\n", getpid(), secondpid);
 #endif
             struct timeval start_time;
             gettimeofday(&start_time, NULL);
             kill(secondpid, SIGUSR1);
-            while (count < NTRIES - 1)
+            while (count < NTRIES - 1) {
                 sleep(1);
+            }
 
             struct timeval finish_time;
             gettimeofday(&finish_time, NULL);
 
             printf("%d time spent: %lu microsecond\n", getpid(),
-                   (finish_time.tv_sec * 1000000L + finish_time.tv_usec)
-                   - (start_time.tv_sec * 1000000L + start_time.tv_usec));
+                   (finish_time.tv_sec * 1000000L + finish_time.tv_usec) -
+                       (start_time.tv_sec * 1000000L + start_time.tv_usec));
 
             exit(0);
         }
@@ -88,19 +86,17 @@ int main(int argc, char ** argv)
             struct timeval start_time;
             gettimeofday(&start_time, NULL);
 
-            while (count < NTRIES)
-                sleep(1);
+            while (count < NTRIES) sleep(1);
 
             struct timeval finish_time;
             gettimeofday(&finish_time, NULL);
 
-            printf("%d time spent: %lu microsecond\n",getpid(),
-                   (finish_time.tv_sec * 1000000L + finish_time.tv_usec)
-                   - (start_time.tv_sec * 1000000L + start_time.tv_usec));
+            printf("%d time spent: %lu microsecond\n", getpid(),
+                   (finish_time.tv_sec * 1000000L + finish_time.tv_usec) -
+                       (start_time.tv_sec * 1000000L + start_time.tv_usec));
 
             exit(0);
         }
-
     }
     return 0;
 }

+ 5 - 6
LibOS/shim/test/native/fork_exec.c

@@ -1,15 +1,14 @@
-#include <unistd.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
-int main (int argc, char * const * argv, const char * const * envp)
-{
+int main(int argc, char* const* argv, const char* const* envp) {
     int newfd = dup(1), outfd = dup(1);
     char fd_argv[4];
     snprintf(fd_argv, 4, "%d", newfd);
-    char * const new_argv[] = { "./exec_victim", fd_argv, NULL };
+    char* const new_argv[] = {"./exec_victim", fd_argv, NULL};
 
     setenv("IN_EXECVE", "1", 1);
 
@@ -21,7 +20,7 @@ int main (int argc, char * const * argv, const char * const * envp)
 
     wait(NULL);
 
-    FILE * out = fdopen(outfd, "a");
+    FILE* out = fdopen(outfd, "a");
     if (!out) {
         printf("cannot open file descriptor\n");
         return -1;

+ 3 - 4
LibOS/shim/test/native/fs.c

@@ -1,17 +1,16 @@
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
 #include <unistd.h>
-#include <fcntl.h>
 
 int main() {
-
     int fd = open("test.open.file", O_CREAT | O_RDWR, S_IRWXU);
 
     int fd2 = open("fs.manifest", O_RDONLY);
 
-    char * buf = malloc (4096);
+    char* buf = malloc(4096);
 
     int ret;
     while ((ret = read(fd2, buf, 4096)) > 0) {

+ 10 - 14
LibOS/shim/test/native/futextest.pthread.c

@@ -1,35 +1,31 @@
-#include <stdio.h>
 #include <pthread.h>
+#include <stdio.h>
 #include <unistd.h>
 
-void * print1 (void *arg)
-{
+void* print1(void* arg) {
     printf("This is Function 1 - go to sleep\n");
     sleep(5);
     printf("Function1 out of sleep\n");
-    printf("%s",(char *) arg);
+    printf("%s", (char*)arg);
     return NULL;
 }
 
-void * print2(void * arg)
-{
+void* print2(void* arg) {
     printf("This is Function 2 - go to sleep\n");
     sleep(5);
     printf("Function2 out of sleep\n");
-    printf("%s",(char *) arg);
+    printf("%s", (char*)arg);
     return NULL;
 }
 
-void * func(void * arg )
-{
-    int * ptr = (int *) arg;
-    printf("Parent gave %d\n",*ptr);
+void* func(void* arg) {
+    int* ptr = (int*)arg;
+    printf("Parent gave %d\n", *ptr);
     return NULL;
 }
 
-int main(int argc, char ** argv)
-{
-    pthread_t thread1, thread2,thread3;
+int main(int argc, char** argv) {
+    pthread_t thread1, thread2, thread3;
     int intvar = 12;
 
     pthread_create(&thread1, NULL, print1, "Thread1 Executing ...\n");

+ 25 - 26
LibOS/shim/test/native/get_time.m.c

@@ -1,10 +1,10 @@
-#include <stdlib.h>
+#include <math.h>
 #include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
 #include <string.h>
-#include <sys/wait.h>
 #include <sys/time.h>
-#include <math.h>
+#include <sys/wait.h>
+#include <unistd.h>
 
 /*
  *  USAGE:
@@ -15,24 +15,23 @@
  *      ./test_start ./libpal.so    => graphene start time
  */
 
-#define OVERHEAD_TIMES  30000
-#define TEST_TIMES      1000
+#define OVERHEAD_TIMES 30000
+#define TEST_TIMES     1000
 
-void get_time (char * time_arg, unsigned long overhead)
-{
+void get_time(char* time_arg, unsigned long overhead) {
     struct timeval tv;
     gettimeofday(&tv, NULL);
     unsigned long long msec = tv.tv_sec * 1000000ULL + tv.tv_usec;
     snprintf(time_arg, 30, "%llu", msec + overhead);
 }
 
-int main (int argc, char ** argv, char ** envp)
-{
-    char * new_argv[argc + 1];
+int main(int argc, char** argv, char** envp) {
+    char* new_argv[argc + 1];
     char time_arg[30];
 
-    for (int i = 1 ; i < argc ; i++)
+    for (int i = 1; i < argc; i++) {
         new_argv[i - 1] = argv[i];
+    }
 
     new_argv[argc] = NULL;
 
@@ -40,7 +39,7 @@ int main (int argc, char ** argv, char ** envp)
     unsigned long long sum = 0, ssum = 0;
     memset(times, 0, sizeof(times));
 
-    for (int i = 1 ; i < TEST_TIMES ; i++) {
+    for (int i = 1; i < TEST_TIMES; i++) {
         int pipes[2];
         if (pipe(pipes) < 0)
             break;
@@ -53,11 +52,12 @@ int main (int argc, char ** argv, char ** envp)
         if (!pid) {
             struct timeval tv1, tv2;
             gettimeofday(&tv1, NULL);
-            for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
+            for (int j = 0; j < OVERHEAD_TIMES; j++) {
                 get_time(time_arg, 0);
+            }
             gettimeofday(&tv2, NULL);
-            unsigned long long msec1 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
-            unsigned long long msec2 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
+            unsigned long long msec1    = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
+            unsigned long long msec2    = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
             unsigned long long overhead = (msec2 - msec1) / OVERHEAD_TIMES;
 
             get_time(time_arg, overhead);
@@ -89,22 +89,21 @@ int main (int argc, char ** argv, char ** envp)
         close(pipes[0]);
     }
 
-    int compar (const void * arg1, const void * arg2)
-    {
-        register unsigned long long a1 = *((unsigned long long *) arg1);
-        register unsigned long long a2 = *((unsigned long long *) arg2);
+    int compar(const void* arg1, const void* arg2) {
+        register unsigned long long a1 = *((unsigned long long*)arg1);
+        register unsigned long long a2 = *((unsigned long long*)arg2);
         return a1 < a2 ? -1 : (a1 == a2 ? 0 : 1);
     }
 
     qsort(times, TEST_TIMES, sizeof(unsigned long long), compar);
 
-    double median =
-            (TEST_TIMES % 2) ? (double) times[TEST_TIMES / 2] :
-            (double) (times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
+    double median = (TEST_TIMES % 2)
+                        ? (double)times[TEST_TIMES / 2]
+                        : (double)(times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
 
-    double mean = (double) sum / TEST_TIMES;
-    double stddev = sqrt((double) ssum / TEST_TIMES - mean * mean);
-    double ci = 1.96 * stddev / sqrt((double) TEST_TIMES);
+    double mean   = (double)sum / TEST_TIMES;
+    double stddev = sqrt((double)ssum / TEST_TIMES - mean * mean);
+    double ci     = 1.96 * stddev / sqrt((double)TEST_TIMES);
 
     printf("median = %lf, mean = %lf (+/-%lf)\n", median, mean, ci);
 

+ 1 - 2
LibOS/shim/test/native/helloworld.c

@@ -2,8 +2,7 @@
 
 #include <stdio.h>
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     printf("Hello world (%s)!\n", argv[0]);
     return 0;
 }

+ 4 - 6
LibOS/shim/test/native/helloworld.pthread.c

@@ -1,19 +1,17 @@
 /* a simple helloworld test, with pthread usage */
 
+#include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <pthread.h>
 #include <unistd.h>
 
-void * print (void *arg)
-{
+void* print(void* arg) {
     printf("child: pid %d\n", getpid());
-    puts((char *) arg);
+    puts((char*)arg);
     return NULL;
 }
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     pthread_t thread;
     printf("parent: pid %d\n", getpid());
     pthread_create(&thread, NULL, print, "Hello World!");

+ 7 - 9
LibOS/shim/test/native/kill.c

@@ -1,15 +1,14 @@
+#include <errno.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <signal.h>
 #include <time.h>
+#include <unistd.h>
 
 int kill_parent = 0;
 
-int main(int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     if (argc == 2 && !strcmp("parent", argv[1]))
         kill_parent = 1;
 
@@ -23,7 +22,7 @@ int main(int argc, char ** argv)
 
     if (pid == 0) {
         struct timespec rem;
-        rem.tv_sec = kill_parent ? 1 : 60;
+        rem.tv_sec  = kill_parent ? 1 : 60;
         rem.tv_nsec = 0;
 
         printf("[pid=%d|ppid=%d] Going to sleep...\n", getpid(), getppid());
@@ -35,10 +34,9 @@ int main(int argc, char ** argv)
 
         printf("[pid=%d|ppid=%d] Hello, Dad!\n", getpid(), getppid());
         return 0;
-    }
-    else {
+    } else {
         struct timespec rem;
-        rem.tv_sec = kill_parent ? 60 : 1;
+        rem.tv_sec  = kill_parent ? 60 : 1;
         rem.tv_nsec = 0;
 
         printf("[pid=%d|ppid=%d] Going to sleep...\n", getpid(), getppid());

+ 3 - 4
LibOS/shim/test/native/malloc.c

@@ -1,9 +1,8 @@
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
 
-int main(int argc, char ** argv)
-{
-    for (int i = 0 ; i < 100000 ; i++) {
+int main(int argc, char** argv) {
+    for (int i = 0; i < 100000; i++) {
         malloc(16);
         malloc(32);
         malloc(64);

+ 7 - 7
LibOS/shim/test/native/meminfo.c

@@ -1,19 +1,19 @@
 #define _GNU_SOURCE
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 
-int main (int argc, char ** argv)
-{
-    FILE * cpuinfo = fopen("/proc/meminfo", "rb");
-    char * arg = 0;
-    size_t size = 0;
+int main(int argc, char** argv) {
+    FILE* cpuinfo = fopen("/proc/meminfo", "rb");
+    char* arg     = 0;
+    size_t size   = 0;
 
     if (!cpuinfo)
         return errno;
 
-    while(getdelim(&arg, &size, 0, cpuinfo) != -1)
+    while (getdelim(&arg, &size, 0, cpuinfo) != -1) {
         puts(arg);
+    }
 
     free(arg);
     fclose(cpuinfo);

+ 31 - 37
LibOS/shim/test/native/msg_create.c

@@ -1,15 +1,14 @@
 /* Test to create 100 message queues and query them from another process*/
 
-#include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/ipc.h>
-#include <sys/types.h>
 #include <sys/msg.h>
-#include <string.h>
-#include <stdlib.h>
 #include <sys/time.h>
+#include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 struct msg_buf {
     long mtype;
@@ -19,12 +18,11 @@ struct msg_buf {
 #define TEST_TIMES 1000
 #define DO_BENCH   1
 
-int create_q (int key)
-{
-    int r = msgget(key, IPC_CREAT|0600);
+int create_q(int key) {
+    int r = msgget(key, IPC_CREAT | 0600);
 
 #ifndef DO_BENCH
-    printf("The identifier used is %d\n",r);
+    printf("The identifier used is %d\n", r);
 #endif
 
     if (r < 0) {
@@ -39,24 +37,23 @@ int create_q (int key)
     return r;
 }
 
-int connect_q (int key)
-{
-   int r = msgget(key, 0);
+int connect_q(int key) {
+    int r = msgget(key, 0);
 
 #ifndef DO_BENCH
-   printf("The identifier used is %d\n",r);
+    printf("The identifier used is %d\n", r);
 #endif
 
-   if (r < 0) {
-       perror("msgget");
-       exit(-1);
-   }
+    if (r < 0) {
+        perror("msgget");
+        exit(-1);
+    }
 #ifndef DO_BENCH
-   else
+    else
         printf("Connected the message queue\n");
 #endif
 
-   return r;
+    return r;
 }
 
 enum { PARALLEL, SERIAL, IN_PROCESS } mode = PARALLEL;
@@ -64,15 +61,15 @@ int pipefds[4];
 int keys[TEST_TIMES];
 
 /* server always creates queues */
-void server (void)
-{
+void server(void) {
     struct timeval tv1, tv2;
     int i;
 
     gettimeofday(&tv1, NULL);
 
-    for (i = 0; i < TEST_TIMES; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         create_q(keys[i]);
+    }
 
     gettimeofday(&tv2, NULL);
 
@@ -88,18 +85,15 @@ void server (void)
         read(pipefds[2], &byte, 1);
     }
 
-    printf("time spent on %d creation: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
+    printf("time spent on %d creation: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
 
     if (mode != IN_PROCESS)
         exit(0);
 }
 
 /* client always connects queues */
-void client (void)
-{
+void client(void) {
     struct timeval tv1, tv2;
     int i;
     int ids[TEST_TIMES];
@@ -112,13 +106,15 @@ void client (void)
 
     gettimeofday(&tv1, NULL);
 
-    for (i= 0; i < TEST_TIMES ; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         ids[i] = connect_q(keys[i]);
+    }
 
     gettimeofday(&tv2, NULL);
 
-    for (i= 0; i < TEST_TIMES ; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         msgctl(ids[i], IPC_RMID, NULL);
+    }
 
     if (mode == PARALLEL) {
         close(pipefds[2]);
@@ -126,21 +122,19 @@ void client (void)
         write(pipefds[3], &byte, 1);
     }
 
-    printf("time spent on %d connection: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
+    printf("time spent on %d connection: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
 
     if (mode != IN_PROCESS)
         exit(0);
 }
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int i;
 
-    for (i = 0; i < TEST_TIMES; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         keys[i] = rand();
+    }
 
     pipe(pipefds);
     pipe(pipefds + 2);

+ 34 - 39
LibOS/shim/test/native/msg_create.libos.c

@@ -1,16 +1,15 @@
 /* Test to create 100 message queues and query them from another process*/
 
-#include <stdlib.h>
+#include <shim_unistd.h>
 #include <stdio.h>
-#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/ipc.h>
-#include <sys/types.h>
 #include <sys/msg.h>
-#include <string.h>
-#include <stdlib.h>
 #include <sys/time.h>
+#include <sys/types.h>
 #include <sys/wait.h>
-#include <shim_unistd.h>
+#include <unistd.h>
 
 struct msg_buf {
     long mtype;
@@ -20,12 +19,11 @@ struct msg_buf {
 #define TEST_TIMES 1000
 #define DO_BENCH   1
 
-int create_q (int key)
-{
-    int r = msgget(key, IPC_CREAT|0600);
+int create_q(int key) {
+    int r = msgget(key, IPC_CREAT | 0600);
 
 #ifndef DO_BENCH
-    printf("The identifier used is %d\n",r);
+    printf("The identifier used is %d\n", r);
 #endif
 
     if (r < 0) {
@@ -40,77 +38,74 @@ int create_q (int key)
     return r;
 }
 
-int connect_q (int key)
-{
-   int r = msgget(key, 0);
+int connect_q(int key) {
+    int r = msgget(key, 0);
 
 #ifndef DO_BENCH
-   printf("The identifier used is %d\n",r);
+    printf("The identifier used is %d\n", r);
 #endif
 
-   if (r < 0) {
-       perror("msgget");
-       exit(-1);
-   }
+    if (r < 0) {
+        perror("msgget");
+        exit(-1);
+    }
 #ifndef DO_BENCH
-   else
+    else
         printf("Connected the message queue\n");
 #endif
 
-   return r;
+    return r;
 }
 
 int keys[TEST_TIMES];
 int ids[TEST_TIMES];
 
 /* server always creates queues */
-void server (void)
-{
+void server(void) {
     struct timeval tv1, tv2;
     int i;
 
     gettimeofday(&tv1, NULL);
 
-    for (i = 0; i < TEST_TIMES; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         ids[i] = create_q(keys[i]);
+    }
 
-    for (i = 0; i < TEST_TIMES; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         msgpersist(ids[i], MSGPERSIST_STORE);
+    }
 
     gettimeofday(&tv2, NULL);
 
-    printf("time spent on %d creation: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
+    printf("time spent on %d creation: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
 }
 
 /* client always connects queues */
-void client (void)
-{
+void client(void) {
     struct timeval tv1, tv2;
     int i;
 
     gettimeofday(&tv1, NULL);
 
-    for (i = 0; i < TEST_TIMES; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         msgpersist(ids[i], MSGPERSIST_LOAD);
+    }
 
     gettimeofday(&tv2, NULL);
 
-    printf("time spent on %d connection: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
+    printf("time spent on %d connection: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ULL + tv2.tv_usec) - (tv1.tv_sec * 1000000ULL + tv1.tv_usec));
 
-    for (i= 0; i < TEST_TIMES ; i++)
+    for (i = 0; i < TEST_TIMES; i++) {
         msgctl(ids[i], IPC_RMID, NULL);
+    }
 }
 
-int main (int argc, char ** argv)
-{
-    for (int i = 0; i < TEST_TIMES; i++)
+int main(int argc, char** argv) {
+    for (int i = 0; i < TEST_TIMES; i++) {
         keys[i] = rand();
+    }
 
     server();
     client();

+ 17 - 24
LibOS/shim/test/native/msg_send.c

@@ -1,18 +1,18 @@
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>
 #include <sys/time.h>
+#include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
-#define PAYLOAD_SIZE    10
+#define PAYLOAD_SIZE 10
 
 struct msgbuf {
-    long    mtype;
-    char    mtext[PAYLOAD_SIZE];
+    long mtype;
+    char mtext[PAYLOAD_SIZE];
 };
 
 #define TEST_TIMES 1000
@@ -23,20 +23,19 @@ enum { PARALLEL, SERIAL, IN_PROCESS } mode = PARALLEL;
 int pipefds[4], key;
 
 /* server always sends messages */
-void server (void)
-{
+void server(void) {
     struct timeval tv1, tv2;
     int msqid;
     struct msgbuf buf;
 
-    if ((msqid = msgget(key, mode == SERIAL ? 0600|IPC_CREAT : 0)) < 0) {
+    if ((msqid = msgget(key, mode == SERIAL ? 0600 | IPC_CREAT : 0)) < 0) {
         perror("msgget");
         exit(1);
     }
 
     gettimeofday(&tv1, NULL);
 
-    for (int i = 0 ; i < TEST_TIMES ; i++) {
+    for (int i = 0; i < TEST_TIMES; i++) {
         buf.mtype = (i % TEST_TYPES) + 1;
         if (msgsnd(msqid, &buf, PAYLOAD_SIZE, 0) < 0) {
             perror("msgsnd");
@@ -60,18 +59,15 @@ void server (void)
         read(pipefds[2], &byte, 1);
     }
 
-    printf("time spent on %d msgsnd: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ull + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ull + tv1.tv_usec));
+    printf("time spent on %d msgsnd: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ull + tv2.tv_usec) - (tv1.tv_sec * 1000000ull + tv1.tv_usec));
 
     if (mode != IN_PROCESS)
         exit(0);
 }
 
 /* client always sends messages */
-void client (void)
-{
+void client(void) {
     struct timeval tv1, tv2;
     int msqid;
     struct msgbuf buf;
@@ -90,7 +86,7 @@ void client (void)
 
     gettimeofday(&tv1, NULL);
 
-    for (int i = 0 ; i < TEST_TIMES ; i++) {
+    for (int i = 0; i < TEST_TIMES; i++) {
         int type = (i % TEST_TYPES) + 1;
         if ((ret = msgrcv(msqid, &buf, PAYLOAD_SIZE, type, 0)) < 0) {
             perror("msgrcv");
@@ -111,17 +107,14 @@ void client (void)
         write(pipefds[3], &byte, 1);
     }
 
-    printf("time spent on %d msgrcv: %llu microsecond\n",
-           TEST_TIMES,
-           (tv2.tv_sec * 1000000ull + tv2.tv_usec) -
-           (tv1.tv_sec * 1000000ull + tv1.tv_usec));
+    printf("time spent on %d msgrcv: %llu microsecond\n", TEST_TIMES,
+           (tv2.tv_sec * 1000000ull + tv2.tv_usec) - (tv1.tv_sec * 1000000ull + tv1.tv_usec));
 
     if (mode != IN_PROCESS)
         exit(0);
 }
 
-int main (int argc, char ** argv)
-{
+int main(int argc, char** argv) {
     int msqid;
 
     key = rand();
@@ -142,7 +135,7 @@ int main (int argc, char ** argv)
         return 0;
     }
 
-    if ((msqid = msgget(key, 0600|IPC_CREAT)) < 0) {
+    if ((msqid = msgget(key, 0600 | IPC_CREAT)) < 0) {
         perror("msgget");
         exit(1);
     }

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