From 83823ad34de0bcd909bc367b7af072d6e176fca9 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:43:49 +0000 Subject: [PATCH 1/2] test: parse deeply nested PL/pgSQL in the WASM depth test --- .github/workflows/ci.yml | 3 +++ test/wasm_depth.c | 55 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fddb2e35..67930ed8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,6 +97,9 @@ jobs: emcc -O3 -sENVIRONMENT=node -sSTACK_SIZE=33554432 -sINITIAL_MEMORY=41943040 \ -sALLOW_MEMORY_GROWTH=1 -I. test/wasm_depth.c libpg_query.a -o test/wasm_depth.js node test/wasm_depth.js + emcc -O3 -sENVIRONMENT=node -sSTACK_SIZE=1048576 -sINITIAL_MEMORY=41943040 \ + -sALLOW_MEMORY_GROWTH=1 -I. test/wasm_depth.c libpg_query.a -o test/wasm_depth_small.js + node test/wasm_depth_small.js overflow build_macos: runs-on: macos-latest steps: diff --git a/test/wasm_depth.c b/test/wasm_depth.c index 090e5d68..9e4f7102 100644 --- a/test/wasm_depth.c +++ b/test/wasm_depth.c @@ -49,16 +49,67 @@ static bool check_depth(const char *name, const char *open, int depth, return ok; } -int main(void) +/* depth nested PL/pgSQL blocks, e.g. "IF true THEN " ... "NULL;" ... " END IF;" */ +static bool check_plpgsql_depth(const char *name, const char *open, const char *body, + const char *close, int depth) +{ + const char prefix[] = "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN "; + const char suffix[] = " END $$"; + size_t open_len = strlen(open), body_len = strlen(body), close_len = strlen(close); + char *query = malloc(sizeof(prefix) + depth * (open_len + close_len) + body_len + sizeof(suffix)); + if (query == NULL) + { + fprintf(stderr, "FAIL %s: could not allocate query\n", name); + return false; + } + + char *p = query; + memcpy(p, prefix, sizeof(prefix) - 1); + p += sizeof(prefix) - 1; + for (int i = 0; i < depth; i++, p += open_len) + memcpy(p, open, open_len); + memcpy(p, body, body_len); + p += body_len; + for (int i = 0; i < depth; i++, p += close_len) + memcpy(p, close, close_len); + memcpy(p, suffix, sizeof(suffix)); + + PgQueryPlpgsqlParseResult result = pg_query_parse_plpgsql(query); + bool ok = result.error == NULL && result.plpgsql_funcs != NULL; + if (ok) + printf("PASS %s: parsed successfully\n", name); + else + fprintf(stderr, "FAIL %s: expected a parse tree, got %s\n", name, + result.error ? result.error->message : "no parse tree"); + + pg_query_free_plpgsql_parse_result(result); + free(query); + return ok; +} + +/* + * Without arguments, run with the WebAssembly stack libpg-query-node links + * (32MB). With "overflow", run with a 1MB stack, small enough that the stack + * depth check fires before the host engine's native stack runs out. + */ +int main(int argc, char **argv) { int status = EXIT_SUCCESS; + if (argc > 1 && strcmp(argv[1], "overflow") == 0) + { + if (!check_depth("100000 chained terms", "1+", 99999, false, true)) + status = EXIT_FAILURE; + return status; + } if (!check_depth("2000 chained terms", "1+", 1999, false, false)) status = EXIT_FAILURE; if (!check_depth("1000 nested subqueries", "(SELECT ", 1000, true, false)) status = EXIT_FAILURE; if (!check_depth("2000 nested function calls", "f(", 2000, true, false)) status = EXIT_FAILURE; - if (!check_depth("100000 chained terms", "1+", 99999, false, true)) + if (!check_plpgsql_depth("1000 nested IF blocks", "IF true THEN ", "NULL;", " END IF;", 1000)) + status = EXIT_FAILURE; + if (!check_plpgsql_depth("3000 nested BEGIN blocks", "BEGIN ", "NULL;", " END;", 3000)) status = EXIT_FAILURE; return status; } From f975bb56d6b93b9ce23135da66cee81308ec3a98 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 26 Nov 2026 04:45:51 +0000 Subject: [PATCH 2/2] Use the whole WebAssembly stack for the stack depth limit --- src/pg_query.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/pg_query.c b/src/pg_query.c index 6eefbd3d..0e1cfc31 100644 --- a/src/pg_query.c +++ b/src/pg_query.c @@ -33,17 +33,6 @@ #include -#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; @@ -272,9 +261,15 @@ static void set_max_stack_depth(void) if (new_limit <= 100) return; - /* 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; +#if !defined(__EMSCRIPTEN__) + /* + * Clamp at 2MB, like Postgres auto-detection logic. WebAssembly uses its + * whole stack, whose size is set when linking: the host engine's native + * stack usually runs out first there, which the embedder has to handle. + */ + if (new_limit > 2048) + new_limit = 2048; +#endif max_stack_depth = (int) new_limit; assign_max_stack_depth((int) new_limit, NULL);