Skip to content

Commit d906df2

Browse files
authored
Merge pull request #9674 from The-OpenROAD-Project-staging/secure-fix-dbnetwork-id
dbSta: fix ObjectId collision between ConcreteCell and dbModule
2 parents 4485a4a + 758a57d commit d906df2

6 files changed

Lines changed: 222 additions & 10 deletions

File tree

‎src/dbSta/include/db_sta/dbNetwork.hh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ class dbNetwork : public ConcreteNetwork
470470
static constexpr unsigned DBMODINST_ID = 0x6;
471471
static constexpr unsigned DBMODNET_ID = 0x7;
472472
static constexpr unsigned DBMODULE_ID = 0x8;
473+
static constexpr unsigned CONCRETE_OBJECT_ID = 0xF;
473474
// Number of lower bits used
474475
static constexpr unsigned DBIDTAG_WIDTH = 0x4;
475476

‎src/dbSta/src/dbNetwork.cc‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,9 @@ ObjectId dbNetwork::id(const Port* port) const
752752
const dbObject* obj = reinterpret_cast<const dbObject*>(port);
753753
return getDbNwkObjectId(obj);
754754
}
755-
// default
756-
return ConcreteNetwork::id(port);
755+
756+
// Tag ID to avoid ID collision b/w dbObject and STA object
757+
return (ConcreteNetwork::id(port) << DBIDTAG_WIDTH) | CONCRETE_OBJECT_ID;
757758
}
758759

759760
// Note:
@@ -766,16 +767,16 @@ ObjectId dbNetwork::id(const Port* port) const
766767

767768
ObjectId dbNetwork::id(const Cell* cell) const
768769
{
769-
// in hierarchical flow we use the object id for the index
770-
if (hasHierarchy()) {
771-
if (!isConcreteCell(cell)) {
772-
const dbObject* obj = reinterpret_cast<const dbObject*>(cell);
773-
return getDbNwkObjectId(obj);
774-
}
770+
// top_cell_ is a ConcreteCell but isConcreteCell() returns false for it;
771+
// guard against reinterpret_cast to dbObject which would crash.
772+
if (hasHierarchy() && !isConcreteCell(cell) && cell != top_cell_) {
773+
const dbObject* obj = reinterpret_cast<const dbObject*>(cell);
774+
return getDbNwkObjectId(obj);
775775
}
776-
// default behaviour use the concrete cell.
776+
777+
// Tag ID to avoid ID collision b/w dbObject and STA object
777778
const ConcreteCell* ccell = reinterpret_cast<const ConcreteCell*>(cell);
778-
return ccell->id();
779+
return (ccell->id() << DBIDTAG_WIDTH) | CONCRETE_OBJECT_ID;
779780
}
780781

781782
////////////////////////////////////////////////////////////////

‎src/dbSta/test/BUILD‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,20 @@ cc_test(
318318
"@googletest//:gtest_main",
319319
],
320320
)
321+
322+
cc_test(
323+
name = "test_write_verilog",
324+
srcs = ["cpp/TestWriteVerilog.cpp"],
325+
data = glob(["cpp/TestWriteVerilog*.v"]),
326+
deps = [
327+
"//src/dbSta",
328+
"//src/dbSta:dbNetwork",
329+
"//src/dbSta:dbReadVerilog",
330+
"//src/odb",
331+
"//src/sta:opensta_lib",
332+
"//src/tst:integrated_fixture",
333+
"//src/utl",
334+
"@googletest//:gtest",
335+
"@googletest//:gtest_main",
336+
],
337+
)

‎src/dbSta/test/cpp/CMakeLists.txt‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,32 @@ gtest_discover_tests(TestReadVerilog
8282

8383
add_dependencies(build_and_test TestReadVerilog
8484
)
85+
86+
add_executable(TestWriteVerilog TestWriteVerilog.cpp)
87+
target_link_libraries(TestWriteVerilog
88+
OpenSTA
89+
GTest::gtest
90+
GTest::gtest_main
91+
GTest::gmock
92+
dbSta_lib
93+
utl_lib
94+
rsz_lib
95+
grt_lib
96+
dpl_lib
97+
stt_lib
98+
odb
99+
tst
100+
${TCL_LIBRARY}
101+
)
102+
103+
target_include_directories(TestWriteVerilog
104+
PRIVATE
105+
${PROJECT_SOURCE_DIR}/src/dbSta/src
106+
)
107+
108+
gtest_discover_tests(TestWriteVerilog
109+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..
110+
)
111+
112+
add_dependencies(build_and_test TestWriteVerilog
113+
)
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright (c) 2025, The OpenROAD Authors
3+
4+
// Regression test: write_verilog -remove_cells must not drop hierarchical
5+
// module instances.
6+
//
7+
// Background:
8+
// CellSet uses CellIdLess which compares cells by network_->id(cell).
9+
// Before the fix, ConcreteCell used raw sequential id_ (0,1,2,...) while
10+
// dbModule used (db_id << 4) | 0x8 (24,40,56,...). These ID spaces
11+
// overlapped, so CellSet::contains could treat a dbModule as equal to a
12+
// ConcreteCell, causing writeChild to silently drop hierarchical instances.
13+
//
14+
// The fix tags ConcreteCell IDs with CONCRETE_OBJECT_ID (0xF) so they
15+
// never collide with dbModule IDs (tag 0x8).
16+
17+
#include <cstdio>
18+
#include <fstream>
19+
#include <iterator>
20+
#include <string>
21+
22+
#include "gtest/gtest.h"
23+
#include "sta/Network.hh"
24+
#include "sta/NetworkClass.hh"
25+
#include "sta/PatternMatch.hh"
26+
#include "sta/VerilogWriter.hh"
27+
#include "tst/IntegratedFixture.h"
28+
29+
namespace sta {
30+
31+
class TestWriteVerilog : public tst::IntegratedFixture
32+
{
33+
public:
34+
TestWriteVerilog()
35+
: tst::IntegratedFixture(tst::IntegratedFixture::Technology::Nangate45,
36+
"_main/src/dbSta/test/")
37+
{
38+
}
39+
};
40+
41+
// Verifies that write_verilog -remove_cells does not drop hierarchical
42+
// instances when a liberty cell is removed.
43+
//
44+
// Puts a liberty cell (INV_X1) in remove_cells, then verifies that the
45+
// hierarchical instance (sub_inst) is still present in the output.
46+
// Before the fix, this could fail when the ConcreteCell's raw id_
47+
// collided with the dbModule's computed id.
48+
TEST_F(TestWriteVerilog, RemoveCellsIdCollision)
49+
{
50+
const std::string test_name = "TestWriteVerilog_RemoveCellsIdCollision";
51+
52+
// Read a simple hierarchical design: top -> sub_mod -> INV_X1
53+
readVerilogAndSetup(test_name + "_pre.v", /*init_default_sdc=*/false);
54+
55+
Network* network = sta_->network();
56+
Instance* top_inst = network->topInstance();
57+
58+
// Find the hierarchical instance and its Cell* (which is a dbModule*)
59+
Cell* hier_cell = nullptr;
60+
std::string hier_inst_name;
61+
InstanceChildIterator* child_iter = network->childIterator(top_inst);
62+
while (child_iter->hasNext()) {
63+
Instance* child = child_iter->next();
64+
if (network->isHierarchical(child)) {
65+
hier_cell = network->cell(child);
66+
hier_inst_name = network->name(child);
67+
break;
68+
}
69+
}
70+
delete child_iter;
71+
ASSERT_NE(hier_cell, nullptr) << "No hierarchical instance found in design";
72+
73+
ObjectId hier_id = network->id(hier_cell);
74+
75+
// Verify no ConcreteCell shares the same id as the hier module.
76+
// After the fix, ConcreteCell IDs use tag 0xF while dbModule IDs
77+
// use tag 0x8, so no collision is possible.
78+
PatternMatch match_all("*",
79+
/*is_regexp=*/false,
80+
/*nocase=*/false,
81+
sta_->tclInterp());
82+
83+
LibraryIterator* lib_iter = network->libraryIterator();
84+
while (lib_iter->hasNext()) {
85+
Library* lib = lib_iter->next();
86+
CellSeq lib_cells = network->findCellsMatching(lib, &match_all);
87+
for (Cell* cell : lib_cells) {
88+
if (cell != hier_cell) {
89+
EXPECT_NE(network->id(cell), hier_id)
90+
<< "ObjectId collision between ConcreteCell '"
91+
<< network->name(cell) << "' and dbModule '"
92+
<< network->name(hier_cell) << "' — tagging should prevent this";
93+
}
94+
}
95+
}
96+
delete lib_iter;
97+
98+
// Find INV_X1 liberty cell to use in remove_cells
99+
Cell* inv_cell = nullptr;
100+
LibraryIterator* lib_iter2 = network->libraryIterator();
101+
while (lib_iter2->hasNext()) {
102+
Library* lib = lib_iter2->next();
103+
PatternMatch inv_match("INV_X1",
104+
/*is_regexp=*/false,
105+
/*nocase=*/false,
106+
sta_->tclInterp());
107+
CellSeq cells = network->findCellsMatching(lib, &inv_match);
108+
if (cells.empty() == false) {
109+
inv_cell = cells[0];
110+
break;
111+
}
112+
}
113+
delete lib_iter2;
114+
ASSERT_NE(inv_cell, nullptr) << "INV_X1 not found in libraries";
115+
116+
// Put INV_X1 in remove_cells and write verilog
117+
CellSeq remove_cells;
118+
remove_cells.push_back(inv_cell);
119+
120+
const std::string output_file = test_name + "_out.v";
121+
writeVerilog(output_file.c_str(), false, &remove_cells, network);
122+
123+
// Read the output
124+
std::ifstream ifs(output_file);
125+
ASSERT_TRUE(ifs.is_open()) << "Failed to open output file: " << output_file;
126+
std::string content((std::istreambuf_iterator<char>(ifs)),
127+
std::istreambuf_iterator<char>());
128+
ifs.close();
129+
130+
// The hierarchical instance must be present in the output.
131+
// Only leaf INV_X1 instances should be removed, not hierarchical modules.
132+
EXPECT_NE(content.find(hier_inst_name), std::string::npos)
133+
<< "Hierarchical instance '" << hier_inst_name
134+
<< "' was incorrectly removed from write_verilog output.\n"
135+
<< " write_verilog -remove_cells should never drop hierarchical "
136+
"instances.";
137+
138+
// Clean up
139+
std::remove(output_file.c_str());
140+
}
141+
142+
} // namespace sta
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Bug reproduction for write_verilog -remove_cells.
3+
4+
This hierarchical design contains a sub-module "sub_mod" instantiated
5+
in "top". When a liberty cell whose ConcreteCell ID collides with the
6+
dbModule computed ID is placed in remove_cells, the CellIdLess
7+
comparator in CellSet treats them as equal, causing writeChild to
8+
incorrectly drop the hierarchical instance.
9+
*/
10+
module top(in, out);
11+
input in;
12+
output out;
13+
wire w1;
14+
sub_mod sub_inst (.A(in), .Z(w1));
15+
INV_X1 u1 (.A(w1), .ZN(out));
16+
endmodule
17+
18+
module sub_mod(A, Z);
19+
input A;
20+
output Z;
21+
INV_X1 inv1 (.A(A), .ZN(Z));
22+
endmodule

0 commit comments

Comments
 (0)