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
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
test:
name: cargo test
name: Test
runs-on: ubuntu-latest

steps:
Expand All @@ -18,10 +18,19 @@ jobs:
submodules: recursive

- name: Install system dependencies
run: sudo apt-get update && sudo apt-get install -y clang libclang-dev
run: sudo apt-get update && sudo apt-get install -y clang libclang-dev valgrind

- name: Install Rust
uses: dtolnay/rust-toolchain@stable

- name: Install cargo-nextest
uses: taiki-e/install-action@nextest

- name: Run tests
run: cargo test
run: cargo nextest run

- name: Check for memory leaks
run: scripts/check-memory-leaks.sh

- name: Run doc tests
run: cargo test --doc
18 changes: 14 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ fn main() {
.allowlist_item("pg_query_free_error")
.allowlist_item("pg_query_raw_parse")
.allowlist_item("PgQueryParseMode")
.allowlist_item("PgQuerySplitResult")
.allowlist_item("PgQuerySplitStmt")
.allowlist_item("pg_query_split_with_scanner")
.allowlist_item("pg_query_free_split_result")
.allowlist_item("wrapped_raw_expression_tree_walker_impl")
.override_abi(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for removing this? This will result in panics turning into process aborts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oops! Not sure, let me double check. Would of been good to have a test to catch this one.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, surprised they didn't. I remember having explicit tests for this

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's because we have the catch_unwind in a different spot now. So this change shouldn't break anything, but still seems unrelated to everything else in the PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what happened. It wasn't linking on Mac (but links on Linux). Not sure why though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting it back in #47

bindgen::Abi::CUnwind,
"wrapped_raw_expression_tree_walker_impl",
)
.allowlist_item("StringInfo")
.allowlist_item("wrapped_raw_deparse")
.allowlist_item("wrapped_pnstrdup")
Expand All @@ -146,6 +146,10 @@ fn main() {
.unwrap();

let mut build = cc::Build::new();
println!("cargo:rerun-if-env-changed=PG_RAW_PARSE_USE_VALGRIND");
if env::var_os("PG_RAW_PARSE_USE_VALGRIND").is_some() {
build.define("USE_VALGRIND", None);
}
build
.files(glob("libpg_query/src/*.c").unwrap().map(Result::unwrap))
.files(
Expand Down Expand Up @@ -1309,6 +1313,12 @@ fn build_node_struct(s: &syn::ItemStruct, type_comment_regex: &Regex) -> NodeStr
// Despite the "list of ColumnDef nodes" comment, tableElts also
// contains table-level Constraint nodes.
(("CreateStmt", "table_elts"), NodeFieldType::List),
// Grant targets vary with objtype: they can be RangeVar,
// ObjectWithArgs, or String nodes.
(("GrantStmt", "objects"), NodeFieldType::List),
// The raw grammar accepts a general FROM list here; semantic analysis
// later restricts it to a single table.
(("CreateStatsStmt", "relations"), NodeFieldType::List),
// Comment claims args is A_Const, but that isn't the case for
// `SET TRANSACTION ...`
(("VariableSetStmt", "args"), NodeFieldType::List),
Expand Down
42 changes: 42 additions & 0 deletions scripts/check-memory-leaks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -euo pipefail

if [[ "$(uname -s)" != "Linux" ]]; then
echo "error: memory leak checks require Linux (Valgrind is unsupported on this platform)" >&2
exit 2
fi

if ! command -v valgrind >/dev/null 2>&1; then
echo "error: valgrind is required; install it with your system package manager" >&2
exit 2
fi

workspace_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$workspace_root"
export PG_RAW_PARSE_USE_VALGRIND=1

test_binary="$({
cargo test \
--test postgres_regress \
--no-run \
--color never \
--message-format=json
} | sed -n 's/.*"executable":"\([^"]*\)".*/\1/p' | tail -n 1)"

if [[ -z "$test_binary" || ! -x "$test_binary" ]]; then
echo "error: failed to locate the compiled postgres_regress test binary" >&2
exit 2
fi

exec valgrind \
--tool=memcheck \
--leak-check=full \
--show-leak-kinds=all \
--errors-for-leak-kinds=definite,indirect,possible \
--error-exitcode=1 \
--num-callers=40 \
--suppressions="$workspace_root/libpg_query/test/valgrind.supp" \
--suppressions="$workspace_root/scripts/valgrind.supp" \
"$test_binary" \
--exact postgres_regression_sql_parses_and_round_trips \
--nocapture
10 changes: 10 additions & 0 deletions scripts/valgrind.supp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
rust_test_mpmc_context_thread_local
Memcheck:Leak
match-leak-kinds: possible
fun:malloc
...
fun:*std*sync*mpmc*context*Context*new*
...
fun:*test*run_tests*
}
Loading
Loading