Implement libc-WASI for Linux SGX platform and update documents (#343)

This commit is contained in:
Wenyong Huang
2020-08-10 15:12:26 +08:00
committed by GitHub
parent 8edf1e152f
commit 1b6ddb37d0
55 changed files with 3692 additions and 516 deletions

View File

@ -972,9 +972,11 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
return false;
}
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#ifndef BH_PLATFORM_LINUX_SGX
/* address must be in the first 2 Gigabytes of
the process address space */
bh_assert((uintptr_t)data_sections[i].data < INT32_MAX);
#endif
#endif
read_byte_array(buf, buf_end,
@ -1799,9 +1801,11 @@ create_sections(const uint8 *buf, uint32 size,
goto fail;
}
#if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
#ifndef BH_PLATFORM_LINUX_SGX
/* address must be in the first 2 Gigabytes of
the process address space */
bh_assert((uintptr_t)aot_text < INT32_MAX);
#endif
#endif
bh_memcpy_s(aot_text, (uint32)total_size,
section->section_body, (uint32)section_size);

View File

@ -76,7 +76,9 @@ static inline void
generic_vec_init_data(Vector *out, size_t num_of_elems, size_t size_of_elem)
{
if (!bh_vector_init(out, num_of_elems, size_of_elem)) {
memset(out, 0, sizeof(Vector));
out->data = NULL;
out->max_elems = 0;
out->num_elems = 0;
}
else {
memset(out->data, 0, num_of_elems * size_of_elem);

View File

@ -1279,6 +1279,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
struct fd_table *curfds;
struct fd_prestats *prestats;
struct argv_environ_values *argv_environ;
bool fd_table_inited = false, fd_prestats_inited = false;
bool argv_environ_inited = false;
int32 offset_argv_offsets = 0, offset_env_offsets = 0;
int32 offset_argv_buf = 0, offset_env_buf = 0;
int32 offset_curfds = 0;
@ -1373,9 +1375,26 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
wasi_ctx->curfds_offset = offset_curfds;
wasi_ctx->prestats_offset = offset_prestats;
wasi_ctx->argv_environ_offset = offset_argv_environ;
wasi_ctx->argv_buf_offset = offset_argv_buf;
wasi_ctx->argv_offsets_offset = offset_argv_offsets;
wasi_ctx->env_buf_offset = offset_env_buf;
wasi_ctx->env_offsets_offset = offset_env_offsets;
fd_table_init(curfds);
fd_prestats_init(prestats);
if (!fd_table_init(curfds)) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: "
"init fd table failed.");
goto fail;
}
fd_table_inited = true;
if (!fd_prestats_init(prestats)) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: "
"init fd prestats failed.");
goto fail;
}
fd_prestats_inited = true;
if (!argv_environ_init(argv_environ,
argv_offsets, argc,
@ -1387,6 +1406,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
"init argument environment failed.");
goto fail;
}
argv_environ_inited = true;
/* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
if (!fd_table_insert_existing(curfds, 0, 0)
@ -1424,6 +1444,12 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
return true;
fail:
if (argv_environ_inited)
argv_environ_destroy(argv_environ);
if (fd_prestats_inited)
fd_prestats_destroy(prestats);
if (fd_table_inited)
fd_table_destroy(curfds);
if (offset_curfds != 0)
wasm_runtime_module_free(module_inst, offset_curfds);
if (offset_prestats != 0)
@ -1537,6 +1563,14 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
fd_prestats_destroy(prestats);
wasm_runtime_module_free(module_inst, wasi_ctx->prestats_offset);
}
if (wasi_ctx->argv_buf_offset)
wasm_runtime_module_free(module_inst, wasi_ctx->argv_buf_offset);
if (wasi_ctx->argv_offsets_offset)
wasm_runtime_module_free(module_inst, wasi_ctx->argv_offsets_offset);
if (wasi_ctx->env_buf_offset)
wasm_runtime_module_free(module_inst, wasi_ctx->env_buf_offset);
if (wasi_ctx->env_offsets_offset)
wasm_runtime_module_free(module_inst, wasi_ctx->env_offsets_offset);
wasm_runtime_free(wasi_ctx);
}
}

View File

@ -52,6 +52,10 @@ typedef struct WASIContext {
int32 curfds_offset;
int32 prestats_offset;
int32 argv_environ_offset;
int32 argv_buf_offset;
int32 argv_offsets_offset;
int32 env_buf_offset;
int32 env_offsets_offset;
} WASIContext;
#endif

View File

@ -4,8 +4,7 @@
*/
#include "libc_wasi_wrapper.h"
#include "bh_common.h"
#include "bh_log.h"
#include "bh_platform.h"
#include "wasm_export.h"
void

View File

@ -832,9 +832,15 @@ __wasi_errno_t wasmtime_ssp_poll_oneoff(
size_t *nevents
) WASMTIME_SSP_SYSCALL_NAME(poll_oneoff) __attribute__((__warn_unused_result__));
#if 0
/**
* We throw exception in libc-wasi wrapper function wasi_proc_exit()
* but not call this function.
*/
_Noreturn void wasmtime_ssp_proc_exit(
__wasi_exitcode_t rval
) WASMTIME_SSP_SYSCALL_NAME(proc_exit);
#endif
__wasi_errno_t wasmtime_ssp_proc_raise(
__wasi_signal_t sig

View File

@ -14,12 +14,6 @@
#include "ssp_config.h"
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <time.h>
#ifndef __has_extension
#define __has_extension(x) 0
#endif
@ -30,7 +24,7 @@
#define LOCK_ANNOTATE(x)
#endif
// Lock annotation macros.
/* Lock annotation macros. */
#define LOCKABLE LOCK_ANNOTATE(lockable)
@ -50,166 +44,216 @@
#define NO_LOCK_ANALYSIS LOCK_ANNOTATE(no_thread_safety_analysis)
// Mutex that uses the lock annotations.
/* Mutex that uses the lock annotations. */
struct LOCKABLE mutex {
pthread_mutex_t object;
pthread_mutex_t object;
};
#define MUTEX_INITIALIZER \
{ PTHREAD_MUTEX_INITIALIZER }
static inline void mutex_init(struct mutex *lock) REQUIRES_UNLOCKED(*lock) {
pthread_mutex_init(&lock->object, NULL);
static inline bool
mutex_init(struct mutex *lock) REQUIRES_UNLOCKED(*lock)
{
return pthread_mutex_init(&lock->object, NULL) == 0 ? true : false;
}
static inline void mutex_destroy(struct mutex *lock) REQUIRES_UNLOCKED(*lock) {
pthread_mutex_destroy(&lock->object);
static inline void
mutex_destroy(struct mutex *lock) REQUIRES_UNLOCKED(*lock)
{
pthread_mutex_destroy(&lock->object);
}
static inline void mutex_lock(struct mutex *lock)
LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS {
pthread_mutex_lock(&lock->object);
static inline void
mutex_lock(struct mutex *lock) LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
{
pthread_mutex_lock(&lock->object);
}
static inline void mutex_unlock(struct mutex *lock)
UNLOCKS(*lock) NO_LOCK_ANALYSIS {
pthread_mutex_unlock(&lock->object);
static inline void
mutex_unlock(struct mutex *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
{
pthread_mutex_unlock(&lock->object);
}
// Read-write lock that uses the lock annotations.
/* Read-write lock that uses the lock annotations. */
struct LOCKABLE rwlock {
pthread_rwlock_t object;
pthread_rwlock_t object;
};
static inline void rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock) {
pthread_rwlock_init(&lock->object, NULL);
static inline bool
rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock)
{
return pthread_rwlock_init(&lock->object, NULL) == 0 ? true : false;
}
static inline void rwlock_rdlock(struct rwlock *lock)
LOCKS_SHARED(*lock) NO_LOCK_ANALYSIS {
pthread_rwlock_rdlock(&lock->object);
static inline void
rwlock_rdlock(struct rwlock *lock) LOCKS_SHARED(*lock) NO_LOCK_ANALYSIS
{
pthread_rwlock_rdlock(&lock->object);
}
static inline void rwlock_wrlock(struct rwlock *lock)
LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS {
pthread_rwlock_wrlock(&lock->object);
static inline void
rwlock_wrlock(struct rwlock *lock) LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
{
pthread_rwlock_wrlock(&lock->object);
}
static inline void rwlock_unlock(struct rwlock *lock)
UNLOCKS(*lock) NO_LOCK_ANALYSIS {
pthread_rwlock_unlock(&lock->object);
static inline void
rwlock_unlock(struct rwlock *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
{
pthread_rwlock_unlock(&lock->object);
}
// Condition variable that uses the lock annotations.
static inline void
rwlock_destroy(struct rwlock *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
{
pthread_rwlock_destroy(&lock->object);
}
/* Condition variable that uses the lock annotations. */
struct LOCKABLE cond {
pthread_cond_t object;
pthread_cond_t object;
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \
!CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
clockid_t clock;
clockid_t clock;
#endif
};
static inline void cond_init_monotonic(struct cond *cond) {
static inline bool
cond_init_monotonic(struct cond *cond) {
bool ret = false;
#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK
pthread_condattr_t attr;
pthread_condattr_init(&attr);
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
pthread_cond_init(&cond->object, &attr);
pthread_condattr_destroy(&attr);
pthread_condattr_t attr;
if (pthread_condattr_init(&attr) != 0)
return false;
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0)
goto fail;
if (pthread_cond_init(&cond->object, &attr) != 0)
goto fail;
ret = true;
fail:
pthread_condattr_destroy(&attr);
#else
pthread_cond_init(&cond->object, NULL);
if (pthread_cond_init(&cond->object, NULL) != 0)
return false;
ret = true;
#endif
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \
!CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
cond->clock = CLOCK_MONOTONIC;
cond->clock = CLOCK_MONOTONIC;
#endif
return ret;
}
static inline void cond_init_realtime(struct cond *cond) {
pthread_cond_init(&cond->object, NULL);
static inline bool
cond_init_realtime(struct cond *cond)
{
if (pthread_cond_init(&cond->object, NULL) != 0)
return false;
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK || \
!CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
cond->clock = CLOCK_REALTIME;
cond->clock = CLOCK_REALTIME;
#endif
return true;
}
static inline void cond_destroy(struct cond *cond) {
pthread_cond_destroy(&cond->object);
static inline void
cond_destroy(struct cond *cond) {
pthread_cond_destroy(&cond->object);
}
static inline void cond_signal(struct cond *cond) {
pthread_cond_signal(&cond->object);
static inline void
cond_signal(struct cond *cond) {
pthread_cond_signal(&cond->object);
}
static inline bool cond_timedwait(struct cond *cond, struct mutex *lock,
uint64_t timeout, bool abstime)
REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS {
struct timespec ts = {
.tv_sec = (time_t)(timeout / 1000000000),
.tv_nsec = (long)(timeout % 1000000000),
};
#if !CONFIG_HAS_CLOCK_NANOSLEEP
static inline bool
cond_timedwait(struct cond *cond, struct mutex *lock,
uint64_t timeout, bool abstime)
REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
{
int ret;
struct timespec ts = {
.tv_sec = (time_t)(timeout / 1000000000),
.tv_nsec = (long)(timeout % 1000000000),
};
if (abstime) {
if (abstime) {
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK
// No native support for sleeping on monotonic clocks. Convert the
// timeout to a relative value and then to an absolute value for the
// realtime clock.
if (cond->clock != CLOCK_REALTIME) {
struct timespec ts_monotonic;
clock_gettime(cond->clock, &ts_monotonic);
ts.tv_sec -= ts_monotonic.tv_sec;
ts.tv_nsec -= ts_monotonic.tv_nsec;
if (ts.tv_nsec < 0) {
ts.tv_nsec += 1000000000;
--ts.tv_sec;
}
/**
* No native support for sleeping on monotonic clocks. Convert the
* timeout to a relative value and then to an absolute value for the
* realtime clock.
*/
if (cond->clock != CLOCK_REALTIME) {
struct timespec ts_monotonic;
struct timespec ts_realtime;
struct timespec ts_realtime;
clock_gettime(CLOCK_REALTIME, &ts_realtime);
ts.tv_sec += ts_realtime.tv_sec;
ts.tv_nsec += ts_realtime.tv_nsec;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
}
clock_gettime(cond->clock, &ts_monotonic);
ts.tv_sec -= ts_monotonic.tv_sec;
ts.tv_nsec -= ts_monotonic.tv_nsec;
if (ts.tv_nsec < 0) {
ts.tv_nsec += 1000000000;
--ts.tv_sec;
}
clock_gettime(CLOCK_REALTIME, &ts_realtime);
ts.tv_sec += ts_realtime.tv_sec;
ts.tv_nsec += ts_realtime.tv_nsec;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
}
#endif
} else {
}
else {
#if CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
// Implementation supports relative timeouts.
int ret =
pthread_cond_timedwait_relative_np(&cond->object, &lock->object, &ts);
assert((ret == 0 || ret == ETIMEDOUT) &&
"pthread_cond_timedwait_relative_np() failed");
return ret == ETIMEDOUT;
/* Implementation supports relative timeouts. */
ret = pthread_cond_timedwait_relative_np(&cond->object,
&lock->object, &ts);
bh_assert((ret == 0 || ret == ETIMEDOUT)
&& "pthread_cond_timedwait_relative_np() failed");
return ret == ETIMEDOUT;
#else
// Convert to absolute timeout.
struct timespec ts_now;
/* Convert to absolute timeout. */
struct timespec ts_now;
#if CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK
clock_gettime(cond->clock, &ts_now);
clock_gettime(cond->clock, &ts_now);
#else
clock_gettime(CLOCK_REALTIME, &ts_now);
clock_gettime(CLOCK_REALTIME, &ts_now);
#endif
ts.tv_sec += ts_now.tv_sec;
ts.tv_nsec += ts_now.tv_nsec;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
#endif
ts.tv_sec += ts_now.tv_sec;
ts.tv_nsec += ts_now.tv_nsec;
if (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
#endif
}
int ret = pthread_cond_timedwait(&cond->object, &lock->object, &ts);
assert((ret == 0 || ret == ETIMEDOUT) && "pthread_cond_timedwait() failed");
return ret == ETIMEDOUT;
ret = pthread_cond_timedwait(&cond->object, &lock->object, &ts);
bh_assert((ret == 0 || ret == ETIMEDOUT)
&& "pthread_cond_timedwait() failed");
return ret == ETIMEDOUT;
}
#endif
static inline void cond_wait(struct cond *cond, struct mutex *lock)
REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS {
pthread_cond_wait(&cond->object, &lock->object);
static inline void
cond_wait(struct cond *cond, struct mutex *lock)
REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
{
pthread_cond_wait(&cond->object, &lock->object);
}
#endif

View File

@ -12,8 +12,6 @@
#ifndef COMMON_LIMITS_H
#define COMMON_LIMITS_H
#include <limits.h>
#define NUMERIC_MIN(t) \
_Generic((t)0, char \
: CHAR_MIN, signed char \

View File

@ -10,34 +10,8 @@
// Copyright (c) 2016-2018 Nuxi, https://nuxi.nl/
#include "ssp_config.h"
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wasmtime_ssp.h>
#include "bh_platform.h"
#include "wasmtime_ssp.h"
#include "locking.h"
#include "numeric_limits.h"
#include "posix.h"
@ -46,9 +20,6 @@
#include "rights.h"
#include "str.h"
#include "bh_common.h"
#include "bh_assert.h"
#if 0 /* TODO: -std=gnu99 causes compile error, comment them first */
// struct iovec must have the same layout as __wasi_iovec_t.
static_assert(offsetof(struct iovec, iov_base) ==
@ -252,16 +223,18 @@ struct fd_prestat {
const char *dir;
};
void fd_prestats_init(
bool fd_prestats_init(
struct fd_prestats *pt
) {
rwlock_init(&pt->lock);
if (!rwlock_init(&pt->lock))
return false;
pt->prestats = NULL;
pt->size = 0;
pt->used = 0;
#if defined(WASMTIME_SSP_STATIC_CURFDS)
prestats = pt;
#endif
return true;
}
// Grows the preopened resource table to a required lower bound and a
@ -359,16 +332,18 @@ struct fd_entry {
__wasi_rights_t rights_inheriting;
};
void fd_table_init(
bool fd_table_init(
struct fd_table *ft
) {
rwlock_init(&ft->lock);
if (!rwlock_init(&ft->lock))
return false;
ft->entries = NULL;
ft->size = 0;
ft->used = 0;
#if defined(WASMTIME_SSP_STATIC_CURFDS)
curfds = ft;
#endif
return true;
}
// Looks up a file descriptor table entry by number and required rights.
@ -598,17 +573,22 @@ bool fd_table_insert_existing(
) {
__wasi_filetype_t type;
__wasi_rights_t rights_base, rights_inheriting;
if (fd_determine_type_rights(out, &type, &rights_base, &rights_inheriting) !=
0)
struct fd_object *fo;
__wasi_errno_t error;
if (fd_determine_type_rights(out, &type, &rights_base,
&rights_inheriting) != 0)
return false;
struct fd_object *fo;
__wasi_errno_t error = fd_object_new(type, &fo);
error = fd_object_new(type, &fo);
if (error != 0)
return false;
fo->number = out;
if (type == __WASI_FILETYPE_DIRECTORY) {
mutex_init(&fo->directory.lock);
if (!mutex_init(&fo->directory.lock)) {
fd_object_release(fo);
return false;
}
fo->directory.handle = NULL;
}
@ -671,13 +651,17 @@ static __wasi_errno_t fd_table_insert_fd(
) REQUIRES_UNLOCKED(ft->lock) {
struct fd_object *fo;
__wasi_errno_t error = fd_object_new(type, &fo);
if (error != 0) {
close(in);
return error;
}
fo->number = in;
if (type == __WASI_FILETYPE_DIRECTORY) {
mutex_init(&fo->directory.lock);
if (!mutex_init(&fo->directory.lock)) {
fd_object_release(fo);
return -1;
}
fo->directory.handle = NULL;
}
return fd_table_insert(ft, fo, rights_base, rights_inheriting, out);
@ -2471,9 +2455,14 @@ __wasi_errno_t wasmtime_ssp_poll_oneoff(
// Sleeping to an absolute point in time can only be done
// by waiting on a condition variable.
struct mutex mutex;
mutex_init(&mutex);
struct cond cond;
cond_init_realtime(&cond);
if (!mutex_init(&mutex))
return -1;
if (!cond_init_realtime(&cond)) {
mutex_destroy(&mutex);
return -1;
}
mutex_lock(&mutex);
cond_timedwait(&cond, &mutex, in[0].u.clock.timeout, true);
mutex_unlock(&mutex);
@ -2649,11 +2638,17 @@ __wasi_errno_t wasmtime_ssp_poll_oneoff(
return error;
}
#if 0
/**
* We throw exception in libc-wasi wrapper function wasi_proc_exit()
* but not call this function.
*/
void wasmtime_ssp_proc_exit(
__wasi_exitcode_t rval
) {
_Exit((int32)rval);
}
#endif
__wasi_errno_t wasmtime_ssp_proc_raise(
__wasi_signal_t sig
@ -2952,6 +2947,7 @@ void fd_table_destroy(struct fd_table *ft)
fd_object_release(ft->entries[i].object);
}
}
rwlock_destroy(&ft->lock);
wasm_runtime_free(ft->entries);
}
}
@ -2964,6 +2960,7 @@ void fd_prestats_destroy(struct fd_prestats *pt)
wasm_runtime_free((void*)pt->prestats[i].dir);
}
}
rwlock_destroy(&pt->lock);
wasm_runtime_free(pt->prestats);
}
}

View File

@ -12,9 +12,7 @@
#ifndef POSIX_H
#define POSIX_H
#include <stdbool.h>
#include <stddef.h>
#include "bh_platform.h"
#include "locking.h"
struct fd_entry;
@ -46,9 +44,9 @@ struct argv_environ_values {
char *environ_buf;
};
void fd_table_init(struct fd_table *);
bool fd_table_init(struct fd_table *);
bool fd_table_insert_existing(struct fd_table *, __wasi_fd_t, int);
void fd_prestats_init(struct fd_prestats *);
bool fd_prestats_init(struct fd_prestats *);
bool fd_prestats_insert(struct fd_prestats *, const char *, __wasi_fd_t);
bool argv_environ_init(struct argv_environ_values *,
const size_t *argv_offsets, size_t argv_offsets_len,

View File

@ -12,8 +12,6 @@
#ifndef QUEUE_H
#define QUEUE_H
#include <stddef.h>
// LIST: Double-linked list.
#define LIST_HEAD(name, type) \

View File

@ -10,15 +10,7 @@
// Copyright (c) 2016 Nuxi, https://nuxi.nl/
#include "ssp_config.h"
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "bh_platform.h"
#include "random.h"
#if CONFIG_HAS_ARC4RANDOM_BUF
@ -29,7 +21,9 @@ void random_buf(void *buf, size_t len) {
#elif CONFIG_HAS_GETRANDOM
#ifndef BH_PLATFORM_LINUX_SGX
#include <sys/random.h>
#endif
void random_buf(void *buf, size_t len) {
for (;;) {
@ -37,7 +31,7 @@ void random_buf(void *buf, size_t len) {
if (x < 0) {
if (errno == EINTR)
continue;
fprintf(stderr, "getrandom failed: %s", strerror(errno));
os_printf("getrandom failed: %s", strerror(errno));
abort();
}
if ((size_t)x == len)
@ -54,7 +48,7 @@ static int urandom;
static void open_urandom(void) {
urandom = open("/dev/urandom", O_RDONLY);
if (urandom < 0) {
fputs("Failed to open /dev/urandom\n", stderr);
os_printf("Failed to open /dev/urandom\n");
abort();
}
}
@ -64,7 +58,7 @@ void random_buf(void *buf, size_t len) {
pthread_once(&open_once, open_urandom);
if ((size_t)read(urandom, buf, len) != len) {
fputs("Short read on /dev/urandom\n", stderr);
os_printf("Short read on /dev/urandom\n");
abort();
}
}

View File

@ -12,8 +12,6 @@
#ifndef RANDOM_H
#define RANDOM_H
#include <stdint.h>
void random_buf(void *, size_t);
uintmax_t random_uniform(uintmax_t);

View File

@ -12,36 +12,89 @@
#ifndef REFCOUNT_H
#define REFCOUNT_H
#include <assert.h>
#include <stdatomic.h>
#include <stdbool.h>
#include "bh_platform.h"
#include "locking.h"
// Simple reference counter.
struct LOCKABLE refcount {
atomic_uint count;
};
#define PRODUCES(...) LOCKS_SHARED(__VA_ARGS__) NO_LOCK_ANALYSIS
#define CONSUMES(...) UNLOCKS(__VA_ARGS__) NO_LOCK_ANALYSIS
// Initialize the reference counter.
static void refcount_init(struct refcount *r, unsigned int count) PRODUCES(*r) {
atomic_init(&r->count, count);
#if CONFIG_HAS_STD_ATOMIC != 0
#include <stdatomic.h>
/* Simple reference counter. */
struct LOCKABLE refcount {
atomic_uint count;
};
/* Initialize the reference counter. */
static inline void
refcount_init(struct refcount *r, unsigned int count) PRODUCES(*r)
{
atomic_init(&r->count, count);
}
// Increment the reference counter.
static inline void refcount_acquire(struct refcount *r) PRODUCES(*r) {
atomic_fetch_add_explicit(&r->count, 1, memory_order_acquire);
/* Increment the reference counter. */
static inline void
refcount_acquire(struct refcount *r) PRODUCES(*r)
{
atomic_fetch_add_explicit(&r->count, 1, memory_order_acquire);
}
// Decrement the reference counter, returning whether the reference
// dropped to zero.
static inline bool refcount_release(struct refcount *r) CONSUMES(*r) {
int old = (int)atomic_fetch_sub_explicit(&r->count, 1, memory_order_release);
assert(old != 0 && "Reference count becoming negative");
return old == 1;
/* Decrement the reference counter, returning whether the reference
dropped to zero. */
static inline bool
refcount_release(struct refcount *r) CONSUMES(*r)
{
int old = (int)atomic_fetch_sub_explicit(&r->count, 1,
memory_order_release);
bh_assert(old != 0 && "Reference count becoming negative");
return old == 1;
}
#endif
#elif defined(BH_PLATFORM_LINUX_SGX)
#include <sgx_spinlock.h>
/* Simple reference counter. */
struct refcount {
sgx_spinlock_t lock;
unsigned int count;
};
/* Initialize the reference counter. */
static inline void
refcount_init(struct refcount *r, unsigned int count)
{
r->lock = SGX_SPINLOCK_INITIALIZER;
r->count = count;
}
/* Increment the reference counter. */
static inline void
refcount_acquire(struct refcount *r)
{
sgx_spin_lock(&r->lock);
r->count++;
sgx_spin_unlock(&r->lock);
}
/* Decrement the reference counter, returning whether the reference
dropped to zero. */
static inline bool
refcount_release(struct refcount *r)
{
int old;
sgx_spin_lock(&r->lock);
old = (int)r->count;
r->count--;
sgx_spin_unlock(&r->lock);
bh_assert(old != 0 && "Reference count becoming negative");
return old == 1;
}
#else /* else of CONFIG_HAS_STD_ATOMIC */
#error "Reference counter isn't implemented"
#endif /* end of CONFIG_HAS_STD_ATOMIC */
#endif /* end of REFCOUNT_H */

View File

@ -73,7 +73,7 @@
#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 0
#endif
#ifndef __APPLE__
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX)
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1
#else
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0
@ -97,4 +97,10 @@
#define CONFIG_TLS_USE_GSBASE 0
#endif
#if !defined(BH_PLATFORM_LINUX_SGX)
#define CONFIG_HAS_STD_ATOMIC 1
#else
#define CONFIG_HAS_STD_ATOMIC 0
#endif
#endif

View File

@ -10,24 +10,35 @@
// Copyright (c) 2016 Nuxi, https://nuxi.nl/
#include "ssp_config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "bh_platform.h"
#include "str.h"
char *str_nullterminate(const char *s, size_t len) {
// Copy string.
char *ret = strndup(s, len);
if (ret == NULL)
return NULL;
static char *
bh_strndup(const char *s, size_t n)
{
size_t l = strnlen(s, n);
char *s1 = wasm_runtime_malloc((uint32)(l + 1));
// Ensure that it contains no null bytes within.
if (strlen(ret) != len) {
free(ret);
errno = EILSEQ;
return NULL;
}
if (!s1)
return NULL;
bh_memcpy_s(s1, (uint32)(l + 1), s, (uint32)l);
s1[l] = 0;
return s1;
}
char *
str_nullterminate(const char *s, size_t len) {
/* Copy string */
char *ret = bh_strndup(s, len);
if (ret == NULL)
return NULL;
/* Ensure that it contains no null bytes within */
if (strlen(ret) != len) {
wasm_runtime_free(ret);
errno = EILSEQ;
return NULL;
}
return ret;
}