Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/pg_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,29 @@
#include <pthread_np.h>
#endif

#if defined(__EMSCRIPTEN__)
/* For emscripten_stack_get_end() */
#include <emscripten/stack.h>
#endif

#if defined(_WIN32)
/* For GetCurrentThreadStackLimits(); port/win32.h already sets _WIN32_WINNT. */
#include <windows.h>
#endif

#include <signal.h>

#if defined(__EMSCRIPTEN__)
/*
* WebAssembly code also runs on the host engine's native stack (about 1MB in
* Node and browsers), where its frames are larger than on the WebAssembly
* stack, so a larger limit overflows the native stack before it is reached.
*/
#define PG_QUERY_MAX_STACK_DEPTH_KB 512
#else
#define PG_QUERY_MAX_STACK_DEPTH_KB 2048
#endif

const char* progname = "pg_query";

__thread sig_atomic_t pg_query_initialized = 0;
Expand Down Expand Up @@ -169,6 +185,9 @@ static char *get_lowest_stackaddr(void)

pg_query_lowest_stackaddr = (char *) low;
}
#elif defined(__EMSCRIPTEN__)
/* WebAssembly's stack is a region in linear memory that grows down */
pg_query_lowest_stackaddr = (char *) emscripten_stack_get_end();
#elif defined(HAVE_PTHREAD) && defined(__linux__)
{
pthread_attr_t attr;
Expand Down Expand Up @@ -253,9 +272,9 @@ static void set_max_stack_depth(void)
if (new_limit <= 100)
return;

/* Clamp at 2MB, like Postgres auto-detection logic. */
if (new_limit > 2048)
new_limit = 2048;
/* Clamp at 2MB (512kB in WebAssembly), like Postgres auto-detection logic. */
if (new_limit > PG_QUERY_MAX_STACK_DEPTH_KB)
new_limit = PG_QUERY_MAX_STACK_DEPTH_KB;

max_stack_depth = (int) new_limit;
assign_max_stack_depth((int) new_limit, NULL);
Expand Down
Loading