Skip to content

Gecode 7: state and assert the heap size contract - #289

Open
zayenz wants to merge 3 commits into
feature/breaking-7-05-value-paramsfrom
feature/breaking-7-06-heap-contract
Open

zayenz wants to merge 3 commits into
feature/breaking-7-05-value-paramsfrom
feature/breaking-7-06-heap-contract

Conversation

@zayenz

@zayenz zayenz commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Across several modules, Gecode still requested zero bytes from the heap or a region, or passed null pointers with a zero element count to memcpy. What a zero-byte allocation returns is implementation-defined, and passing a null pointer to memcpy is undefined behavior in C and C++ even when the length is zero.

This PR removes the remaining zero-size requests and null-pointer copies, then makes the heap contract explicit:

  • An Action or CHB created over an empty array keeps its shared storage so it remains initialized with size zero, copyable, and (for Action) still accepts decay updates, but it no longer allocates a zero-size element buffer or posts a recorder propagator.
  • TupleSet, Archive, Space::rrealloc, the FlatZinc set and regular builtins, the example dictionary, DFA and TupleSet-from-DFA construction, and IntSet initializer lists no longer make zero-byte requests or pass null pointers to memcpy. Buffers that can now stay null are freed only when non-null; an empty initializer list constructs an empty IntSet, and a FlatZinc regular constraint with a reversed final-state range treats the range as empty.
  • Heap now documents its size contract: allocation sizes must be positive, realloc may grow from zero elements (while rrealloc no longer accepts zero), and copy requires non-null pointers even for zero elements. Debug builds assert the positive-size precondition in Heap::ralloc and Heap::rrealloc.

Stack

Merge in this order. #242, #282, #283, #241, #284, and #285 are merged. #286 targets main, and each later PR builds on the one before it so its diff shows only its own commits. The first four PRs are standalone search and random-number changes; #284 through #274 are the Gecode 7 changes split out of what used to be a single branch.

  1. Rank solutions with Space::compare in best-solution search #242 Rank solutions with Space::compare (merged)
  2. Rename the meta-search hooks to hub and spoke #282 Rename the meta-search hooks to hub and spoke (merged)
  3. Give meta search hub and spoke contexts and report why search ended #283 Hub and spoke contexts and end reasons (merged)
  4. Add extensible random generators with reproducible stream splitting #241 Splittable random generators (merged)
  5. Gecode 7: CMake-only C++23 build baseline #284 Gecode 7: build baseline (merged)
  6. Gecode 7: format the sources with LLVM 22 #285 Gecode 7: reformat (merged)
  7. Gecode 7: normalized API names, LT constants and Clang checks #286 Gecode 7: API names, LT constants, checks
  8. Gecode 7: post nothing for empty arrays #287 Gecode 7: empty arrays
  9. Gecode 7: pass large parameters by const reference #288 Gecode 7: const& parameters
  10. Gecode 7: state and assert the heap size contract #289 Gecode 7: heap contract ← this PR
  11. Gecode 7: trivially copyable handles and defaulted special members #290 Gecode 7: modern classes
  12. Gecode 7: native float interval instead of Boost #291 Gecode 7: native float interval
  13. Gecode 7: bug fixes and Doxygen warnings #292 Gecode 7: bug fixes and Doxygen
  14. Gecode 7: scoped enums and descriptive constant names #293 Gecode 7: scoped enums
  15. Gecode 7: clang-tidy sweep #274 Gecode 7: clang-tidy sweep
  16. Add down_cast and a typed Space::clone #294 down_cast and a typed Space::clone

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of empty inputs across branching, integer and tuple sets, FlatZinc constraints, and dictionary files.
    • Fixed edge cases involving empty archives, zero-arity tuple sets, space reallocation, and reversed ranges in regular constraints.
    • Empty action and CHB statistics now work without posting unnecessary propagators.
    • Empty set literals and integer-set initializers are handled consistently.
  • Documentation
    • Documented allocation requirements for zero-sized requests and empty inputs.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Changes: Empty-input handling

Layer / File(s) Summary
Heap, archive, and reallocation paths
gecode/support/heap.hpp, gecode/kernel/core.hpp, gecode/kernel/archive.cpp, test/region.cpp, changelog.in
Heap allocation and reallocation requests require positive sizes. Kernel reallocation and archive operations avoid allocation, copying, or freeing when storage is empty. Tests cover empty archives and reallocation from zero elements.
Empty Action and CHB inputs
gecode/kernel/branch/action.*, gecode/kernel/branch/chb.*, test/branch.cpp, test/int/open-var-list.cpp, changelog.in
Empty Action and CHB inputs store no values and post no recorder. Cleanup skips empty storage. Tests cover empty branching and open-variable lists across supported variable types.
Integer sets and extensional tables
gecode/int/int-set.cpp, gecode/int/extensional/*, test/int/dom.cpp, test/int/extensional.cpp, changelog.in
Empty IntSet initializer lists are accepted, and zero-arity tuple sets avoid zero-sized storage requests. DFA and tuple-set paths handle empty buffers. Tests cover empty initializers and nullary tuple sets across three representations.
FlatZinc empty sets and regular constraints
gecode/flatzinc/flatzinc.cpp, gecode/flatzinc/registry.cpp, test/flatzinc/empty_set_literal.cpp, test/flatzinc/native-registry.cpp, cmake/GecodeSources.cmake, changelog.in
FlatZinc set construction uses AST data directly. Regular constraints handle reversed final-state intervals as empty. Tests cover empty set literals and reversed intervals.
Empty dictionary files
examples/scowl.hpp, changelog.in
Dictionary initialization skips allocation when the computed buffer size is zero. Destruction frees the buffer only when it is non-null.

Suggested reviewers: chschulte

Change: Refactor

Merge Risk: Minimal · previous review ceb5c

This change removes zero-byte allocation requests and null-pointer copies for empty inputs, and it documents and asserts the positive-size heap contract. Tests cover the affected empty-input cases. No concrete merge-blocking risk was identified.

Security architecture risk: Low · up to ceb5c

The affected empty-input paths avoid zero-byte allocations. The remaining risk is limited to C++ consumers that still use a zero-size resize to release memory: they must switch to explicit release to avoid an assertion or an exception after the block has already been freed.

Retained concerns

  • Low · reliability · inferred: A legacy C++ caller that uses raw resize to release a block may now see MemoryExhausted after a release-mode allocator has already freed the block. Recovery code that assumes the exception preserved ownership could act on a stale pointer. This is a compatibility risk at the public API, not an identified in-tree attack path.
Security review details

Security Blast Radius

  • inferred — The identified compatibility exposure is in-process, at the C++ heap API. The inspected FlatZinc model paths handle the changed empty cases before they can invoke a zero-size heap resize.
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: 37f598e9-210a-4a75-8120-29e84f0ce9b3
Commits

Reviewing files that changed from the base of the PR and between b8a434e and ceb5cfd.

Files selected for processing: 1

  • gecode/support/heap.hpp

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

@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch from 095445d to ddc2ecc Compare November 25, 2026 20:01
@zayenz
zayenz added this pull request to stack #295 November 25, 2026 20:41
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch from ddc2ecc to 9fdc776 Compare November 25, 2026 21:54
@zayenz
zayenz removed this pull request from stack #295 November 25, 2026 21:55
@zayenz
zayenz added this pull request to stack #296 November 25, 2026 21:55
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch from 9fdc776 to 006c2e4 Compare November 26, 2026 06:05
@zayenz
zayenz marked this pull request as ready for review November 26, 2026 06:06
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch 2 times, most recently from 8064f6d to 9f9a76f Compare November 26, 2026 06:13
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch 2 times, most recently from 4214d14 to b8a434e Compare November 26, 2026 16:24
@zayenz zayenz closed this Nov 26, 2026
@zayenz zayenz reopened this Nov 26, 2026
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch from b8a434e to ceb5cfd Compare November 26, 2026 19:14
An Action or CHB over an empty array keeps its shared storage, so it stays initialized with size zero, copyable, and an empty Action still accepts a decay change. It no longer requests or frees a zero-size element buffer and no longer posts a recorder propagator.

Zdev-Change-Id: Zd4e6b819bd4f6bcd64b83d6fcd1a94d1bdf26ec1dc2e580c53e90ed98a16a1c9
TupleSet, Archive, Space::rrealloc, the FlatZinc set and regular builtins, the example dictionary, DFA and TupleSet construction from a DFA, and IntSet initializer lists no longer request zero bytes from the heap or a region and no longer pass null pointers to memcpy. Buffers that can now be null are freed only when non-null. An empty initializer list gives the empty IntSet, and a FlatZinc regular constraint with a reversed final-state range treats it as empty.

Zdev-Change-Id: Z739fba34334ff1732238cd5ef32e1903b009ce0fe1dcc1ec967ee3213b53009f
Heap requests now document that allocation sizes are positive, that realloc may grow from zero elements, and that copy needs non-null pointers even for zero elements. Debug builds assert a positive size in Heap::ralloc and rrealloc, and rrealloc no longer accepts zero. Two lint findings are fixed: an intentional copy in the branch tests gets a NOLINT, and an assert in TupleSet finalization documents a count that clang-analyzer cannot follow.

Zdev-Change-Id: Z56a1f083f220acb099fd0bc9a9ac161b74b110451fa021068f6b7b92b1e3acca
@zayenz
zayenz force-pushed the feature/breaking-7-06-heap-contract branch from ceb5cfd to 65712d2 Compare November 26, 2026 19:48

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