Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 9 additions & 14 deletions src/pg_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@

#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 @@ -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);
Expand Down
55 changes: 53 additions & 2 deletions test/wasm_depth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading