Skip to content

Commit 4a68d93

Browse files
committed
Updated WASI PAL to work with data memory instead of fixed heap range
1 parent 3981749 commit 4a68d93

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/pal/pal_wasi.h

+24-24
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include "ds/flaglock.h"
55
#include "pal_plain.h"
66

7-
#include <array>
87
#ifdef WASM_ENV //wasi-libc/libc-bottom-half/headers/public/__header_*
8+
9+
#include <stdio.h>
10+
#include <unistd.h>
11+
#include <array>
12+
913
extern "C" void *memset(void *dst, int c, size_t n);
1014
extern "C" [[noreturn]] void w_abort();
1115

@@ -14,26 +18,11 @@ namespace snmalloc
1418
{
1519
class PALWASI
1620
{
17-
/// Base of wlm heap
18-
static inline void* heap_base = nullptr;
19-
20-
/// Size of heap
21-
static inline size_t heap_size;
22-
2321
// This is infrequently used code, a spin lock simplifies the code
2422
// considerably, and should never be on the fast path.
2523
static inline std::atomic_flag spin_lock;
2624

2725
public:
28-
/**
29-
* This will be called by oe_allocator_init to set up wasm sandbox heap bounds.
30-
*/
31-
static void setup_initial_range(void* base, void* end)
32-
{
33-
heap_size = pointer_diff(base, end);
34-
heap_base = base;
35-
}
36-
3726
/**
3827
* Bitmap of PalFeatures flags indicating the optional features that this
3928
* PAL supports.
@@ -44,22 +33,20 @@ namespace snmalloc
4433

4534
[[noreturn]] static void error(const char* const str)
4635
{
47-
UNUSED(str);
48-
w_abort();
36+
fprintf(stderr, "%s\n", str);
37+
abort();
4938
}
5039

5140
static std::pair<void*, size_t>
5241
reserve_at_least(size_t request_size) noexcept
5342
{
54-
// First call returns the entire address space
55-
// subsequent calls return {nullptr, 0}
5643
FlagLock lock(spin_lock);
57-
if (request_size > heap_size)
44+
intptr_t actual_size = ((request_size + PAGESIZE - 1) / PAGESIZE) * PAGESIZE;
45+
void *start = sbrk(actual_size);
46+
if (start == (void*)-1)
5847
return {nullptr, 0};
5948

60-
auto result = std::make_pair(heap_base, heap_size);
61-
heap_size = 0;
62-
return result;
49+
return std::make_pair(start, actual_size);
6350
}
6451

6552
template<bool page_aligned = false>
@@ -69,4 +56,17 @@ namespace snmalloc
6956
}
7057
};
7158
}
59+
60+
// WASI does not support pthreads and thus neither can it offer __cxa_thread_atexit.
61+
// Delegating to __cxa_atexit.
62+
// Should be changed once pthreads support it live and desired.
63+
extern "C"
64+
{
65+
int __cxa_atexit(void (*func) (void*), void* arg, void* dso_handle);
66+
int __cxa_thread_atexit(void (*func) (void*), void* arg, void* dso_symbol)
67+
{
68+
return __cxa_atexit(func, arg, dso_symbol);
69+
}
70+
}
71+
7272
#endif

0 commit comments

Comments
 (0)