Skip to content

Fix a use-after-free in DANA's merge_groups and stale neighbors after split_group - #703

Merged
julianspeith merged 2 commits into
masterfrom
fix/dana-merge-groups
Nov 30, 2026
Merged

julianspeith merged 2 commits into
masterfrom
fix/dana-merge-groups

Conversation

@julianspeith

Copy link
Copy Markdown
Contributor

Summary

Result::merge_groups read the gates of a merged group after erasing them. Depending on the standard library this either left gates pointing to groups that no longer exist (libc++, silently) or failed with unordered_map::at (libstdc++, an IndexError in Python). Result::split_group had a related bug: the neighbors of a split group kept referring to the old group, so a later merge_groups involving such a neighbor failed as well.

None of this was caught because the dataflow analysis had no tests. This PR adds runTest-dataflow, which covers the plugin's functionality, not only these two bugs.

Cause

auto& gates_at_i = this->m_gates_of_group.at(group_id);
this->m_gates_of_group[target_group_id].insert(gates_at_i.begin(), gates_at_i.end());
this->m_gates_of_group.erase(group_id);   // destroys the set gates_at_i refers to
std::for_each(gates_at_i.begin(), gates_at_i.end(), ...);   // reads freed memory

The loop that moves the gates to the new group iterates a reference into the map entry that was just erased. With libc++ the freed set usually looks empty, so the loop does nothing and every merged gate keeps its old group ID in m_parent_group_of_gate. With libstdc++ the loop reads stale gates and at() throws.

The same function also kept iterators into m_group_signals, m_group_successors and m_group_predecessors across operator[] on the target group, which can rehash the map and invalidate them.

In split_group, the old group's entry was removed, but its predecessors and successors still listed it, and they were never told about the new groups.

Change

  • merge_groups reassigns the gates before erasing the old group, moves the signals out before erasing their entry, and iterates copies of the successor and predecessor sets.
  • split_group removes the old group from its neighbors and adds the new groups to them.
  • plugins/dataflow_analysis/test adds the plugin's first test, runTest-dataflow (26 tests), on a netlist of four 8-bit registers that feed each other, with self-loops, enable and reset inputs, and a shared clock:
    • Configuration: defaults, chainable setters, gate types by type and property, control pin types, known groups and known structures in all their forms, use without a netlist
    • Analysis: incomplete configurations are rejected; the four registers are found; group- and gate-level dataflow graph; control nets per group and per gate; only target gate types are grouped; known groups are kept; expected sizes, minimum size, stage identification, type consistency and known structures
    • Result: get_groups_as_list; write_dot and write_txt (content, filters, extension replacement, directories, empty path); create_modules (names, gates, filters, reruns, nesting, suffixes, pin groups and prefixes)
    • Merging and splitting: pairwise and multi-group merges, control nets after merging, merging single-gate groups, splitting into single gates, back into registers and into halves, invalid inputs. Every merge and split is followed by a full consistency check of the result's maps.
    • Plugin: name and version, the command line interface, and the GUI extension
  • CHANGELOG.md has entries for both fixes and the tests.

Verification

  • macOS (AppleClang 17), Debug: all 26 tests pass, also with MallocScribble=1 MallocPreScribble=1, which makes reads of freed memory visible.
  • With the old result.cpp, 4 tests fail: the three merge tests and the split test.
  • I have not run the tests on Linux or with GCC; this PR's CI is the first run there.

🤖 Generated with Claude Code

julianspeith and others added 2 commits November 27, 2026 10:07
… split_group

merge_groups erased the gate set of a merged group and only then iterated it
to point its gates to the new group. With libc++ the loop silently did
nothing, so the gates kept pointing to the old group; with libstdc++ it read
freed memory and failed with "unordered_map::at" (an IndexError in Python),
typically already on the first merge. The gates are now reassigned before the
old group is erased. merge_groups also kept the iterators to the signals,
successors, and predecessors of the old group while inserting the new group,
which may rehash these maps; it now moves or copies them first.

split_group did not update the neighbors of the split group: their successors
and predecessors still contained the old group and not the new ones, so a
later merge_groups of such a neighbor failed. The neighbors now refer to the
new groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dataflow analysis had no tests, so CI never ran most of it.
runTest-dataflow builds a netlist of four 8-bit registers that read each
other in a loop, two of them also themselves, with an enable or a reset input
each, and checks

- the configuration: defaults, setters, gate and control pin types, known
  groups and known structures, and a configuration without a netlist,
- the analysis: incomplete configurations, the registers it finds, the
  dataflow graph between groups and between gates, the control nets, the
  target gate types, known groups, and the further options,
- the result: groups as lists, writing DOT and text files, creating modules
  with their names, nesting, suffixes, and pin groups, and merging and
  splitting groups, after which every gate must be in exactly one group whose
  successors and predecessors are those of its gates,
- the plugin: its interface, the command line, and the GUI extension.

Without the fix of merge_groups and split_group in the previous commit, four
of the 26 tests fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@julianspeith
julianspeith enabled auto-merge (squash) November 30, 2026 07:48
@julianspeith
julianspeith merged commit e937513 into master Nov 30, 2026
5 checks passed
@julianspeith
julianspeith deleted the fix/dana-merge-groups branch November 30, 2026 07:49
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