Skip to content

Fix wrong answers and overflows in set, scheduling, and FlatZinc - #302

Open
zayenz wants to merge 8 commits into
mainfrom
fix/model-harness-constraint-bugs
Open

zayenz wants to merge 8 commits into
mainfrom
fix/model-harness-constraint-bugs

Conversation

@zayenz

@zayenz zayenz commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Fixes seven bugs across set constraints, unary and cumulative scheduling, MiniZinc regular, and trace-view range lists, and bounds the auxiliary set created by set order relations with a set operation. Each fix has a regression test that fails (or triggers ASan/UBSan) on main and passes on this branch.

Fixes

  • Missed solutions in set element with SOT_UNION (variable and constant set arrays) when the selector is also the result, as in roots(x, s, s). The propagator modified the selector before consuming an iterator over the result's lower bound. Tests: Set::Element::Union::Shared and Set::Element::UnionConst::Shared — both fail on main, pass here.
  • Accepted wrong solutions in the integer/set channel with a single set variable (also broke roots when the largest value its image can contain is 0). Bounding x[i] to 0..0 matched the initial cache, so propagate() never included i in y[0]. Test: Set::Channel::Int::Single — fails on main, passes here.
  • Missed failure in unary overload detection (also used for cumulative reduced to unary) when task durations sum past the int limits. OmegaNode and OmegaLambdaNode now accumulate in long long int and clamp when reading back. Test: Int::Unary::LongTaskOverload (three tasks of length 10^9 must end by 2*10^9) — on main the space does not fail and UBSan reports signed overflow in gecode/int/unary/tree.hpp; here it fails as expected and UBSan is clean.
  • Order-dependent pruning in unary not-first/not-last: tasks tied on latest start or completion times were not all pruned, so equivalent spaces with different task storage orders could propagate differently. Test: Int::Unary::TiedNotLast (posts the same tasks in two orders and expects the same bound 12) — fails on main, passes here. The sweep replay Int::Unary::Opt::Fix::0::[4,0,3,3,0,5]::Def+A, which previously reported "different result after re-enable", also passes.
  • No solution instead of false for reified MiniZinc regular (Gecode's mznlib) when a value lies outside 1..S. fzn_regular.mzn now uses MiniZinc's standard decomposition for the reified form while keeping gecode_regular for the non-reified form. Test: new case in .github/ci/flatzinc-tools-smoke.sh (run in CI with MiniZinc 2.10.1; the FlatZinc test framework starts from FlatZinc text and cannot see mznlib). Run by hand with MiniZinc 2.11.0: =====UNSATISFIABLE===== on main, regular-x=3 here.
  • Undefined behaviour (signed integer overflow) when posting cumulative with a single task while deciding whether to use the unary propagator; results were not affected in practice. Tests: Int::Cumulative::SingleTask::Typed and Int::Cumulative::SingleTask::Untyped — both pass in Release either way; under UBSan, main reports signed integer overflow: 1 + 2147483647 at gecode/int/cumulative.cpp:70, clean here.
  • Undefined behaviour (uninitialized read) when creating or cloning integer or set trace views: RangeList::copy() read its uninitialized output pointer before overwriting it. Test: existing FlatZinc::Branching::AuxVarClone — under ASan+UBSan, main reports upcast of misaligned address 0xbebebebebebebebe in range-list.hpp, clean here.

Performance

rel(home, x, op, y, r, z) for SRT_LQ, SRT_LT, SRT_GQ, and SRT_GR previously created its auxiliary set variable over the whole set universe; it is now bounded by the operands' upper bounds. Solutions are unchanged. New tests: Set::RelOp::Order::* (16 tests; order relations were not covered for rel with an operation before) — on main they did not finish within 10 minutes and reached 8 GB of memory; here all 16 pass in 0.7 s.

Left out

A FlatZinc parser change that checks the argument count of equality, domain, and on-restart constraints before reading fixed argument positions. On main the constraint posters do not check argument counts either, so guarding only the parser would just move the out-of-range read into the poster. Adding argument-count checks to the posters first is a separate change. Because the parser is unchanged, no regeneration was needed.

Testing

All run on this branch:

  • New tests: each fails (or reports a sanitizer error) on main and passes with its fix, as above.
  • macOS, AppleClang 21, Release: all tests (all tags, including sweeps) of Set::Element (27), Set::Channel (15), Int::Unary (152), Int::Cumulative (5095, includes Cumulatives), Set::RelOp (856), Set::Rel (20), FlatZinc (161), and Int::NoOverlap (17) — all pass.
  • macOS, ASan+UBSan (RelWithDebInfo, halt on error): Set::Element, Set::Channel, Int::Unary, Int::Cumulative, Set::RelOp, and FlatZinc (all tags) — all pass with no report; check suite: 570 tests, all pass with no report.
  • Linux, GCC 15.2, Release: check-standard target (7012 tests) — all pass.
  • Lint and formatting: misc/check.py lint (full tree, clang-tidy 22) exited 0; format-check clean; misc/tidy.py --check passed.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected reified regular constraint results for values outside the valid range.
    • Improved correctness and reliability for unary scheduling, including large task durations and tasks with tied start times.
    • Fixed issues affecting set-element constraints, integer-to-set channeling, and range-list copying.
    • Prevented overflow in cumulative scheduling capacity checks.
  • Performance
    • Reduced auxiliary set bounds used by set-order relations.

When the selector and result views alias, the SOT_UNION element
propagators modified the selector before consuming an iterator over the
result's lower bound, which could drop valid solutions (including from
MiniZinc's roots(x, s, s)).

Cache the lower-bound difference before modifying the selector for
variable sets, and check the subset condition before modifying the
selector for constant sets.
Posting ChannelInt bounds every integer variable to 0..ys.size()-1.
With a single set variable that assigns every x[i] to 0, which matches
the initial cache, so propagate() never sees x[i] as modified and never
includes i in y[0]. This let the channel accept invalid solutions and
broke MiniZinc's roots when the largest image value is 0.

Include i in ys[xs[i].val()] at posting whenever xs[i] is already
assigned.
OmegaNode and OmegaLambdaNode summed task processing and earliest
completion times in int. When task durations summed beyond the integer
limits, the sum overflowed and unary overload detection (also used for
cumulative constraints reduced to unary) missed the overload.

Store processing and completion times in long long int, clamp completion
times to [-Limits::infinity, Limits::infinity] when reading from the
trees, and subtract in long long int when updating task start bounds.
not_last() reset j to -1 on every iteration of the outer loop, so among
tasks with equal latest completion or start times only the first in
storage order received the tighter bound. Because not_first_not_last()
runs not_last() forward and backward, propagation could differ between
equivalent spaces with different task orders.

Keep j across iterations and insert tasks with equal latest start time
together, following Vilím's algorithm, in a shared not_last_bounds()
helper used by both mandatory and optional tasks.
MiniZinc's reified construction for a native regular propagator failed
on values outside 1..S instead of reifying them to false.

Set fzn_regular_is_decomposed = true in fzn_regular.mzn so reified
regular uses MiniZinc's standard decomposition (with an absorbing dead
state), while non-reified regular keeps using gecode_regular.
When posting cumulative, Gecode checks whether the two smallest task
usages exceed the capacity so it can reduce to unary. With a single
task, the second-smallest usage stays INT_MAX, so min_u + min_u2
overflowed signed int when min_u was positive.

Sum in long long int to avoid signed overflow; a single task now takes
the unary branch.
RangeList::copy() stored the incoming value of its output pointer r in
its sentinel before overwriting it. Integer and set trace views pass an
uninitialized RangeList* when created or cloned, so that dead store read
uninitialized memory. Remove the dead sentinel.next(r) call.
For the order relations SRT_LQ, SRT_LT, SRT_GQ, and SRT_GR,
rel(home, x, op, y, r, z) created its auxiliary set variable for x op y
over the full set universe. Because the set order propagator walks every
value of the upper bound, posting these relations was extremely slow and
memory-intensive.

Bound the auxiliary set by the union of lub(x) and lub(y) for SOT_UNION
and SOT_DISJOINT_UNION, their intersection for SOT_INTERSECTION, and
lub(x) for SOT_MINUS.
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: MiniZinc regular reification

Layer / File(s) Summary
Reified regular decomposition and test
gecode/flatzinc/mznlib/fzn_regular.mzn, .github/ci/flatzinc-tools-smoke.sh, changelog.in
The MiniZinc library selects the regular decomposition for reified cases. The smoke test checks a value outside 1..S with the reified constraint false.

Unary scheduling

Layer / File(s) Summary
Widened completion-time calculations
gecode/int/unary.hh, gecode/int/unary/tree.hpp, gecode/int/task.hh, gecode/int/task/tree.hpp, gecode/int/unary/task.hpp, test/int/unary.cpp, changelog.in
Unary tree completion-time values and related arithmetic use long long int intermediates; accessors clamp returned values. Tests cover long-task overload in basic and advanced propagation.
Grouped not-last bounds
gecode/int/unary/not-first-not-last.hpp, test/int/unary.cpp, changelog.in
Not-last propagation computes bounds for tasks grouped by latest start time. Tests check tied-task behavior across posting orders.

Cumulative capacity arithmetic

Layer / File(s) Summary
Widened cumulative usage checks
gecode/int/cumulative.cpp, test/int/cumulative.cpp, changelog.in
Both cumulative overloads widen the minimum-usage sum before comparing it with capacity. Typed and untyped single-task tests exercise posting.

RangeList copying

Layer / File(s) Summary
Copy destination handling
gecode/kernel/range-list.hpp, changelog.in
RangeList::copy builds the destination from the copied ranges without linking it to the previous destination list.

Integer-to-set channel

Layer / File(s) Summary
Assigned-value inclusion and regression test
gecode/set/channel/int.hpp, test/set/channel.cpp, changelog.in
Posting includes an assigned integer's index in the corresponding set variable. A single-set test covers the channel.

Set-union element propagation

Layer / File(s) Summary
Aliased selector and result handling
gecode/set/element/union.hpp, gecode/set/element/unionConst.hpp, test/set/element.cpp, changelog.in
The union propagators revise the order of backward updates and candidate checks. Tests cover cases where the selector and result are the same set.

Set-operation order relations

Layer / File(s) Summary
Bounded temporary set and relation tests
gecode/set/rel-op/post.hpp, test/set/rel-op.cpp, changelog.in
Order-relation posting derives the temporary set's upper bound from the operation operands. Tests cover four set operations and four order relations.

Suggested reviewers: guidotack, chschulte

Change: Bug fix

Merge Risk: Minimal · up to a214d

This change fixes several overflow, aliasing and pruning bugs and adds regression tests. No unresolved defect was found in it, so it looks ready to merge.

Security architecture risk: Minimal · up to a214d

Gecode’s MiniZinc integration now uses the standard decomposition for reified regular constraints so that an out-of-alphabet value can make the constraint false rather than make the model unsatisfiable. The reviewed changes do not introduce a material security, ownership, or deployment risk.

Security review details

Resilience and Maintainability Implications

  • observed — RangeList::copy no longer reads the destination pointer before building a new chain. In both revisions it publishes the replacement only after the copy completes and does not reclaim the old chain. Trace callers can pass an uninitialized destination to copy, but cannot rely on copy to release an existing chain.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: cd4318c6-090a-4fb7-9f49-4a998976d8fd
Commits

Reviewing files that changed from the base of the PR and between f6a34ca and a214dc1.

Files selected for processing: 20

  • .github/ci/flatzinc-tools-smoke.sh
  • changelog.in
  • gecode/flatzinc/mznlib/fzn_regular.mzn
  • gecode/int/cumulative.cpp
  • gecode/int/task.hh
  • gecode/int/task/tree.hpp
  • gecode/int/unary.hh
  • gecode/int/unary/not-first-not-last.hpp
  • gecode/int/unary/task.hpp
  • gecode/int/unary/tree.hpp
  • gecode/kernel/range-list.hpp
  • gecode/set/channel/int.hpp
  • gecode/set/element/union.hpp
  • gecode/set/element/unionConst.hpp
  • gecode/set/rel-op/post.hpp
  • test/int/cumulative.cpp
  • test/int/unary.cpp
  • test/set/channel.cpp
  • test/set/element.cpp
  • test/set/rel-op.cpp
Files with no reviewable changes: 2

  • gecode/int/task.hh
  • gecode/int/task/tree.hpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant