Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion wasm2c/wasm-rt-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@

#if WASM_RT_INSTALL_SIGNAL_HANDLER && !defined(_WIN32)
#include <signal.h>
#include <unistd.h>
#endif

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
#endif

#ifndef NDEBUG
Expand Down Expand Up @@ -78,6 +78,8 @@ WASM_RT_THREAD_LOCAL uint32_t wasm_rt_saved_call_stack_depth;
static WASM_RT_THREAD_LOCAL void* g_alt_stack = NULL;
#endif

size_t g_native_page_size = 0;
Comment thread
shravanrn marked this conversation as resolved.
Resolved

#ifndef WASM_RT_TRAP_HANDLER
WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf;
#endif
Expand All @@ -102,6 +104,12 @@ void wasm_rt_trap(wasm_rt_trap_t code) {

#ifdef _WIN32

static size_t os_page_size(void) {
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwPageSize;
}

#if WASM_RT_INSTALL_SIGNAL_HANDLER

static LONG os_signal_handler(PEXCEPTION_POINTERS info) {
Expand All @@ -126,6 +134,14 @@ static void os_cleanup_signal_handler(void) {

#else

#define DEFAULT_OS_PAGE_SIZE 4096

static size_t os_page_size(void) {
long size = sysconf(_SC_PAGESIZE);
// Fall back to a default size if sysconf returns bad data for any reason
return size > 0 ? (size_t)size : DEFAULT_OS_PAGE_SIZE;
}

#if WASM_RT_INSTALL_SIGNAL_HANDLER
static void os_signal_handler(int sig, siginfo_t* si, void* unused) {
if (si->si_code == SEGV_ACCERR) {
Expand Down Expand Up @@ -254,6 +270,7 @@ static void call_cpuid(uint64_t* rax,
#endif

void wasm_rt_init(void) {
g_native_page_size = os_page_size();
wasm_rt_init_thread();
#if WASM_RT_INSTALL_SIGNAL_HANDLER
if (!g_signal_handler_installed) {
Expand Down
2 changes: 2 additions & 0 deletions wasm2c/wasm-rt-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
extern "C" {
#endif

extern size_t g_native_page_size;

#ifndef WASM_RT_TRAP_HANDLER
/** A setjmp buffer used for handling traps. */
extern WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf;
Expand Down
26 changes: 8 additions & 18 deletions wasm2c/wasm-rt-mem-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
#endif

#ifdef WASM_RT_GROW_FAILED_HANDLER
Expand Down Expand Up @@ -63,12 +62,6 @@ static bool u64_mult_overflow(uint64_t a, uint64_t b, uint64_t* result) {
#if WASM_RT_USE_MMAP

#ifdef _WIN32
static size_t os_page_size(void) {
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwPageSize;
}

static void* os_mmap(size_t size) {
void* ret = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS);
return ret;
Expand Down Expand Up @@ -113,14 +106,6 @@ static void os_print_last_error(const char* msg) {

#else /* !_WIN32 */

#define DEFAULT_OS_PAGE_SIZE 4096

static size_t os_page_size(void) {
long size = sysconf(_SC_PAGESIZE);
// Fall back to a default size if sysconf returns bad data for any reason
return size > 0 ? (size_t)size : DEFAULT_OS_PAGE_SIZE;
}

static void* os_mmap(size_t size) {
int map_prot = PROT_NONE;
int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
Expand Down Expand Up @@ -148,10 +133,15 @@ static void os_print_last_error(const char* msg) {
#endif /* _WIN32 */

static uint64_t round_up_to_os_page_size(uint64_t size) {
uint64_t page_size = os_page_size();
uint64_t remainder = size % page_size;
// page size should be initialized
assert(g_native_page_size);
// Keep an explicit check that isn't removed from the release builds
if (!g_native_page_size) {
Comment thread
shravanrn marked this conversation as resolved.
Resolved
return 0;
}
uint64_t remainder = size % g_native_page_size;
if (remainder != 0) {
uint64_t increment = page_size - remainder;
uint64_t increment = g_native_page_size - remainder;
uint64_t new_size = size + increment;
if (new_size < size) {
// overflow. Return 0 on error
Expand Down
Loading