Skip to content

dataflow: use-after-free in Result::merge_groups causes "IndexError: unordered_map::at" #704

Done

Description

@HBPainter

Description

dataflow::Result::merge_groups reads the gate set of a group after erasing it from m_gates_of_group. Depending on the memory state this either works by chance or throws std::out_of_range, which shows up in Python as:

IndexError: unordered_map::at

Location

plugins/dataflow_analysis/src/api/result.cpp, master @ 65f13a2

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);
std::for_each(gates_at_i.begin(), gates_at_i.end(), [this, target_group_id](const Gate* g) { this->m_parent_group_of_gate.at(g) = target_group_id; });

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(), [this, target_group_id](const Gate* g) {
    this->m_parent_group_of_gate.at(g) = target_group_id;   // iterates a dangling reference
});

gates_at_i is a reference to the element that erase(group_id) destroys, so the for_each iterates freed memory. Garbage gate pointers then fail in m_parent_group_of_gate.at(g).

There is a second, related problem a few lines further down (L673–L690). suc_it and pred_it are iterators into m_group_successors and m_group_predecessors. They are kept while operator[] inserts target_group_id into the same map (L675, L687). If that insertion rehashes the map, the iterators are invalidated.

How to reproduce

Run DANA and merge groups repeatedly from Python, e.g.:

from hal_plugins import dataflow

res = dataflow.analyze(dataflow.Configuration(netlist).with_flip_flops())
ids = sorted(res.get_groups())
while len(ids) >= 2:
    res.merge_groups(ids[:2])
    ids = sorted(res.get_groups())

Since this is undefined behavior, it does not fail deterministically. In my case (lsi_10k netlist, ~840 flip-flops, DANA with with_expected_sizes([128]) followed by a series of merge_groups calls on pairs of groups) it failed reliably with the error above.

Suggested fix

Update the parent groups before erasing the set:

auto& gates_at_i = this->m_gates_of_group.at(group_id);
for (const Gate* g : gates_at_i)
{
    this->m_parent_group_of_gate.at(g) = target_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);

For the iterator issue, create the entries for target_group_id in m_group_successors and m_group_predecessors once before the loop, or copy the successor and predecessor sets of group_id before inserting into the maps.

Environment

  • HAL: master @ 65f13a2
  • OS: Ubuntu 26.04.1 LTS
  • Gate library: lsi_10k

Activity

  1. self-assigned this
    on Nov 30, 2026
  2. julianspeith commented on Nov 30, 2026

    @julianspeith
    Contributor

    Thanks for the bug report, it was just fixed by merging PR #703 into the master.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions