From d253aa3de23239b0e9654b53ce008e5d486cd199 Mon Sep 17 00:00:00 2001 From: vms Date: Tue, 22 Oct 2019 01:44:23 +0300 Subject: [PATCH] Make sbrk(0) deterministic (#115) * fix the behavior of sbrk(0) * review changes --- libc-bottom-half/sources/sbrk.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/sbrk.c b/libc-bottom-half/sources/sbrk.c index 0c78c68..7803edf 100644 --- a/libc-bottom-half/sources/sbrk.c +++ b/libc-bottom-half/sources/sbrk.c @@ -3,8 +3,14 @@ #include #include <__macro_PAGESIZE.h> -/* Bare-bones implementation of sbrk: just call memory.grow. */ +/* Bare-bones implementation of sbrk. */ void *sbrk(intptr_t increment) { + /* sbrk(0) returns the current memory size. */ + if (increment == 0) { + /* The wasm spec doesn't guarantee that memory.grow of 0 always succeeds. */ + return (void *)(__builtin_wasm_memory_size(0) * PAGESIZE); + } + /* We only support page-size increments. */ if (increment % PAGESIZE != 0) { abort();