Fix dereferencing stale iterator in DfgVertex::scopep() (#6227)

Fix dereferencing stale iterator in DfgVertex::scopep()

Fixes part of #6225
This commit is contained in:
Geza Lore 2025-07-24 15:31:31 +01:00 committed by GitHub
parent 2be257369a
commit 94bebb2bcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View File

@ -682,9 +682,11 @@ AstScope* DfgVertex::scopep(ScopeCache& cache, bool tryResultVar) VL_MT_DISABLED
if (DfgVertexVar* const varp = this->getResultVar()) return varp->varScopep()->scopep();
}
// Look up cache
const auto pair = cache.emplace(this, nullptr);
if (pair.second) {
// Note: the recursive invocation can cause a re-hash but that will not invalidate references
AstScope*& resultr = cache[this];
if (!resultr) {
// Mark to prevent infinite recursion on circular graphs - should never be called on such
resultr = reinterpret_cast<AstScope*>(1);
// Find scope based on sources, falling back on the root scope
AstScope* const rootp = v3Global.rootp()->topScopep()->scopep();
AstScope* foundp = rootp;
@ -694,15 +696,15 @@ AstScope* DfgVertex::scopep(ScopeCache& cache, bool tryResultVar) VL_MT_DISABLED
foundp = edge.sourcep()->scopep(cache, true);
if (foundp != rootp) break;
}
pair.first->second = foundp;
resultr = foundp;
}
// If the cache entry exists, but have not set the mapping yet, then we have a circualr graph
UASSERT_OBJ(pair.first->second, this,
// Die on a graph circular through operation vertices
UASSERT_OBJ(resultr != reinterpret_cast<AstScope*>(1), this,
"DfgVertex::scopep called on graph with circular operations");
// Done
return pair.first->second;
return resultr;
}
void DfgVertex::unlinkDelete(DfgGraph& dfg) {

View File

@ -11,7 +11,7 @@ import vltest_bootstrap
test.scenarios('simulator')
test.compile()
test.compile(verilator_flags2=["--debug", "--debugi", "0", "--dumpi-tree", "0"])
test.execute()