Skip to content

Commit 036c14e

Browse files
authored
[wasi] Work arounbd WASI's mmap implementation not returning aligned memory. (#91761)
Some code in sgen like sgen_los_free_object () expects the return address to be aligned. Hopefully fixes #88501 and others.
1 parent 00937b2 commit 036c14e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/mono/mono/utils/mono-mmap-wasm.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ mono_setmmapjit (int flag)
9090
/* Ignored on HOST_WASM */
9191
}
9292

93-
void*
94-
mono_valloc (void *addr, size_t size, int flags, MonoMemAccountType type)
93+
static void*
94+
valloc_impl (void *addr, size_t size, int flags, MonoMemAccountType type)
9595
{
9696
void *ptr;
9797
int mflags = 0;
@@ -119,6 +119,19 @@ mono_valloc (void *addr, size_t size, int flags, MonoMemAccountType type)
119119
return ptr;
120120
}
121121

122+
void*
123+
mono_valloc (void *addr, size_t size, int flags, MonoMemAccountType type)
124+
{
125+
#if HOST_WASI
126+
// WASI implements mmap using malloc, so the returned address is not page aligned
127+
// and our code depends on it
128+
g_assert (!addr);
129+
return mono_valloc_aligned (size, mono_pagesize (), flags, type);
130+
#else
131+
return valloc_impl (addr, size, flags, type);
132+
#endif
133+
}
134+
122135
static GHashTable *valloc_hash;
123136

124137
typedef struct {
@@ -130,7 +143,7 @@ void*
130143
mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountType type)
131144
{
132145
/* Allocate twice the memory to be able to put the block on an aligned address */
133-
char *mem = (char *) mono_valloc (NULL, size + alignment, flags, type);
146+
char *mem = (char *) valloc_impl (NULL, size + alignment, flags, type);
134147
char *aligned;
135148

136149
if (!mem)

0 commit comments

Comments
 (0)