fix(ir): reject unsafe variables in negated body atoms (#920) - #923
Merged
Conversation
A variable that appears only inside a negated body atom -- e.g. Y in c(X) :- a(X), !b(X, Y). -- has an unbounded range. The IR lowering silently treated such variables like wildcards (setup_join_keys only keys on shared variables), so the unsafe rule was accepted and produced range-dependent results. Add a range-restriction check in the ANTIJOIN lowering: every named variable in a negated atom must also be bound by a positive body atom, otherwise the rule is rejected (returns NULL -> WIRELOG_ERR_INVALID_IR). build_atom_scan already normalizes wildcards and constants to NULL, so !b(X, _) and constant columns remain safe. The error message points at the supported workaround (project the negated relation to a key relation first). Tests (TDD): test_unsafe_negation_variable_rejected and test_safe_negation_via_projection_accepted on the easy facade, mirrored on the advanced facade for cross-facade parity (#785). Existing negation examples (!superseded(id), !src_a(Id, _, _), !in_b(Id)) are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #920.
Problem
wirelog accepts a negated body atom that introduces a variable not bound by any positive body atom:
Yappears only under negation, so its range is unbounded. IR lowering silently treated such a variable like a wildcard (setup_join_keysonly keys on shared variables), so the program parsed, reported as stratified, and lowered to anANTIJOIN. The result depended on whether anyb("x", _)row existed — an unsafe Datalog rule that should be rejected.Fix
Add a range-restriction (safety) check in the
ANTIJOINlowering (wirelog/ir/program.c): every named variable in a negated atom must also be bound by a positive body atom; otherwise the rule is rejected (return NULL→ surfaces asWIRELOG_ERR_INVALID_IR, mirroring the existing negated-side-compound rejection).build_atom_scanalready normalizes wildcards and constants toNULL, so the check is exact and narrow:!b(X, _)(wildcard column) — safe, accepted.!b(X, Y)withYunbound — unsafe, rejected.The error message points at the supported workaround (project the negated relation to a key relation first).
Behavior
c(X) :- a(X), !b(X, Y).b_key(X) :- b(X, Y). c(X) :- a(X), !b_key(X).!superseded(id),!src_a(Id, _, _),!in_b(Id). Blast radius is zero.Tests (TDD)
Written before the implementation:
test_unsafe_negation_variable_rejected— the unsafe rule fails to build.test_safe_negation_via_projection_accepted— the projected-key form and wildcard columns build successfully.Both added on the easy facade and mirrored on the advanced facade for the cross-facade test-parity gate (#785). The check lives in shared IR lowering, so both facades reject identically.
Validation
wirelog_easy,wirelog_advanced, andtest_parityall pass.sbom_snapshot, a local-syft-version artifact unrelated to this change (no dependency is added).