diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 190816500..fddb2e354 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,6 +76,27 @@ jobs: run: make $FLAGS env: FLAGS: ${{ format('{0} {1} {2}', steps.make_flags.outputs.proto_flags, steps.make_flags.outputs.compiler_flags, steps.make_flags.outputs.debug_flags) }} + build_wasm: + runs-on: ubuntu-24.04 + steps: + - name: Check out code + uses: actions/checkout@v4 + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: '20.x' + - name: Install Emscripten + run: | + git clone --branch 6.0.3 --depth 1 https://github.com/emscripten-core/emsdk.git "$RUNNER_TEMP/emsdk" + "$RUNNER_TEMP/emsdk/emsdk" install 6.0.3 + "$RUNNER_TEMP/emsdk/emsdk" activate 6.0.3 + - name: Build and run WASM depth tests + run: | + source "$RUNNER_TEMP/emsdk/emsdk_env.sh" + emmake make -j"$(nproc)" build + 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 build_macos: runs-on: macos-latest steps: diff --git a/test/wasm_depth.c b/test/wasm_depth.c new file mode 100644 index 000000000..090e5d685 --- /dev/null +++ b/test/wasm_depth.c @@ -0,0 +1,64 @@ +#include + +#include +#include +#include +#include + +static bool check_depth(const char *name, const char *open, int depth, + bool nested, bool expect_error) +{ + const char prefix[] = "SELECT "; + size_t open_len = strlen(open); + char *query = malloc(sizeof(prefix) + depth * (open_len + nested) + 1); + 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++) + { + memcpy(p, open, open_len); + p += open_len; + } + *p++ = '1'; + if (nested) + { + memset(p, ')', depth); + p += depth; + } + *p = '\0'; + + PgQueryParseResult result = pg_query_parse(query); + const char *error = result.error ? result.error->message : NULL; + bool ok = expect_error ? error && strcmp(error, "stack depth limit exceeded") == 0 + : result.error == NULL && result.parse_tree != NULL; + if (ok) + printf("PASS %s: %s\n", name, expect_error ? error : "parsed successfully"); + else + fprintf(stderr, "FAIL %s: expected %s, got %s\n", name, + expect_error ? "stack depth limit exceeded" : "a parse tree", + error ? error : result.parse_tree ? "a parse tree" : "no parse tree"); + + pg_query_free_parse_result(result); + free(query); + return ok; +} + +int main(void) +{ + int status = EXIT_SUCCESS; + 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)) + status = EXIT_FAILURE; + return status; +}