diff --git a/libc/runtime/stack.h b/libc/runtime/stack.h index bd4525ea9..f3f6e618a 100644 --- a/libc/runtime/stack.h +++ b/libc/runtime/stack.h @@ -121,6 +121,13 @@ extern char ape_stack_align[] __attribute__((__weak__)); vAddr; \ }) +/** + * Returns true if at least `n` bytes of stack are available. + */ +#define HaveStackMemory(n) \ + (IsTiny() || \ + (intptr_t)__builtin_frame_address(0) >= GetStackAddr() + PAGESIZE * (n)) + COSMOPOLITAN_C_END_ #endif /* GNU ELF */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/test/tool/net/ljson_test.lua b/test/tool/net/ljson_test.lua index e15c569eb..dc0873db8 100644 --- a/test/tool/net/ljson_test.lua +++ b/test/tool/net/ljson_test.lua @@ -124,9 +124,6 @@ res, err = DecodeJson([[ {"k":{"k":{"k":{"k":0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}} ]]) -if not res then - print('wut', err) -end assert(res) -- 64 objects diff --git a/third_party/lua/luaencodejsondata.c b/third_party/lua/luaencodejsondata.c index 8dd4910e4..315c44728 100644 --- a/third_party/lua/luaencodejsondata.c +++ b/third_party/lua/luaencodejsondata.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/bits/bits.h" +#include "libc/bits/likely.h" #include "libc/fmt/itoa.h" #include "libc/intrin/kprintf.h" #include "libc/log/log.h" @@ -165,7 +166,7 @@ static int SerializeTable(lua_State *L, char **buf, int idx, int rc; bool isarray; lua_Unsigned n; - if ((intptr_t)__builtin_frame_address(0) < GetStackAddr() + PAGESIZE * 2) { + if (UNLIKELY(!HaveStackMemory(PAGESIZE))) { z->reason = "out of stack"; return -1; } diff --git a/third_party/lua/luaencodeluadata.c b/third_party/lua/luaencodeluadata.c index b66d3e9ea..f26e8421d 100644 --- a/third_party/lua/luaencodeluadata.c +++ b/third_party/lua/luaencodeluadata.c @@ -336,7 +336,7 @@ static int SerializeTable(lua_State *L, char **buf, int idx, struct Serializer *z, int depth) { int rc; intptr_t rsp, bot; - if ((intptr_t)__builtin_frame_address(0) < GetStackAddr() + PAGESIZE * 2) { + if (UNLIKELY(!HaveStackMemory(PAGESIZE))) { z->reason = "out of stack"; return -1; } diff --git a/tool/net/ljson.c b/tool/net/ljson.c index fa1f5ecaf..cddbbbee1 100644 --- a/tool/net/ljson.c +++ b/tool/net/ljson.c @@ -50,8 +50,7 @@ static struct DecodeJson Parse(struct lua_State *L, const char *p, if (UNLIKELY(!depth)) { return (struct DecodeJson){-1, "maximum depth exceeded"}; } - if (UNLIKELY((intptr_t)__builtin_frame_address(0) < - GetStackAddr() + PAGESIZE * 2)) { + if (UNLIKELY(!HaveStackMemory(PAGESIZE))) { return (struct DecodeJson){-1, "out of stack"}; } for (a = p, d = +1; p < e;) {