From ce57803c45591191c3deeae2db07512dfb5813c7 Mon Sep 17 00:00:00 2001 From: Adam Goode Date: Sun, 10 Mar 2024 22:14:36 -0400 Subject: [PATCH] Fix broken GC_release() for WASI It was just using free() but it can be called in the middle of an allocation. Instead, ignore shrink requests, since we can't do anything with them. --- runtime/gc/heap.c | 2 ++ runtime/platform.h | 4 ++++ runtime/platform/aix.h | 1 + runtime/platform/cygwin.h | 1 + runtime/platform/darwin.h | 1 + runtime/platform/freebsd.h | 1 + runtime/platform/hpux.h | 1 + runtime/platform/hurd.h | 1 + runtime/platform/linux.h | 1 + runtime/platform/mingw.h | 1 + runtime/platform/netbsd.h | 1 + runtime/platform/openbsd.h | 1 + runtime/platform/solaris.h | 1 + runtime/platform/wasi.h | 1 + 14 files changed, 18 insertions(+) diff --git a/runtime/gc/heap.c b/runtime/gc/heap.c index af1d9f8bb0..668315d510 100644 --- a/runtime/gc/heap.c +++ b/runtime/gc/heap.c @@ -183,7 +183,9 @@ void shrinkHeap (GC_state s, GC_heap h, size_t keepSize) { } assert (isAligned (keepWithMapsSize, s->sysvals.pageSize)); assert (keepWithMapsSize <= h->withMapsSize); +#if HAS_SHRINK_HEAP GC_release (h->start + keepWithMapsSize, h->withMapsSize - keepWithMapsSize); +#endif h->size = keepSize; h->withMapsSize = keepWithMapsSize; } diff --git a/runtime/platform.h b/runtime/platform.h index 6c3a44cfd7..c88ea3b852 100644 --- a/runtime/platform.h +++ b/runtime/platform.h @@ -35,6 +35,10 @@ #error HAS_REMAP not defined #endif +#ifndef HAS_SHRINK_HEAP +#error HAS_SHRINK_HEAP not defined +#endif + #ifndef HAS_SIGALTSTACK #error HAS_SIGALTSTACK not defined #else diff --git a/runtime/platform/aix.h b/runtime/platform/aix.h index 4869551f3e..f7bb19037c 100644 --- a/runtime/platform/aix.h +++ b/runtime/platform/aix.h @@ -34,6 +34,7 @@ #define HAS_MSG_DONTWAIT FALSE #define HAS_PTRACE FALSE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/cygwin.h b/runtime/platform/cygwin.h index 644fec5aa0..29a0e7bae6 100644 --- a/runtime/platform/cygwin.h +++ b/runtime/platform/cygwin.h @@ -35,6 +35,7 @@ #define HAS_FEROUND FALSE #define HAS_REMAP TRUE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK FALSE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN TRUE diff --git a/runtime/platform/darwin.h b/runtime/platform/darwin.h index e5af03e32b..ac2a0d9369 100644 --- a/runtime/platform/darwin.h +++ b/runtime/platform/darwin.h @@ -34,6 +34,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/freebsd.h b/runtime/platform/freebsd.h index 85d685df0a..919127ec15 100644 --- a/runtime/platform/freebsd.h +++ b/runtime/platform/freebsd.h @@ -30,6 +30,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/hpux.h b/runtime/platform/hpux.h index 62d83b649f..aa8de42de3 100644 --- a/runtime/platform/hpux.h +++ b/runtime/platform/hpux.h @@ -52,6 +52,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT FALSE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/hurd.h b/runtime/platform/hurd.h index 7dbc46d5c6..68fc44e88f 100644 --- a/runtime/platform/hurd.h +++ b/runtime/platform/hurd.h @@ -30,6 +30,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP TRUE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/linux.h b/runtime/platform/linux.h index 3f1ce7a608..423f9e11b4 100644 --- a/runtime/platform/linux.h +++ b/runtime/platform/linux.h @@ -38,6 +38,7 @@ #endif #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP TRUE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #if (defined (__hppa__)) #define NEEDS_SIGALTSTACK_EXEC TRUE diff --git a/runtime/platform/mingw.h b/runtime/platform/mingw.h index f99352eb0e..7441ee5e24 100644 --- a/runtime/platform/mingw.h +++ b/runtime/platform/mingw.h @@ -33,6 +33,7 @@ #define HAS_FEROUND FALSE #define HAS_MSG_DONTWAIT FALSE #define HAS_REMAP TRUE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK FALSE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN TRUE diff --git a/runtime/platform/netbsd.h b/runtime/platform/netbsd.h index d2aef2e849..6bea1a501f 100644 --- a/runtime/platform/netbsd.h +++ b/runtime/platform/netbsd.h @@ -30,6 +30,7 @@ #define HAS_FEROUND FALSE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/openbsd.h b/runtime/platform/openbsd.h index c6ca591e6a..ffb816e0c0 100644 --- a/runtime/platform/openbsd.h +++ b/runtime/platform/openbsd.h @@ -30,6 +30,7 @@ #define HAS_FEROUND FALSE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/solaris.h b/runtime/platform/solaris.h index 456d7fc7e4..b752f34549 100644 --- a/runtime/platform/solaris.h +++ b/runtime/platform/solaris.h @@ -34,6 +34,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP TRUE #define HAS_SIGALTSTACK TRUE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE diff --git a/runtime/platform/wasi.h b/runtime/platform/wasi.h index 6b046d2e3f..e978246bff 100644 --- a/runtime/platform/wasi.h +++ b/runtime/platform/wasi.h @@ -20,6 +20,7 @@ #define HAS_FEROUND TRUE #define HAS_MSG_DONTWAIT TRUE #define HAS_REMAP FALSE +#define HAS_SHRINK_HEAP FALSE #define HAS_SIGALTSTACK FALSE #define NEEDS_SIGALTSTACK_EXEC FALSE #define HAS_SPAWN FALSE