Skip to content

Commit 6cb2f0e

Browse files
committed
pass.lean: no certificate for a memory pass.bitwidth did not prove
pass.bitwidth proves every Memory port address inside [0, size) and marks the node (mem_address_proven, Bitwidth::check_mem_address). `--set sim.debug=true` skips that proof so a debug simulator can run an unproven memory with a run-time check, and until now the export ran anyway: the certificate got a table of 2^addr_width words and the exporter's address resize, so an address past the end read or wrote a word the hardware does not have. parse_memory_info now refuses a Memory without the mark, naming it and its size, so sim.debug can still build a debug simulator but cannot write a certificate. This is the gate at export; DCERT4's load-time check (Compiler/MemAddr) is the second gate, on the certificate itself. Tests: - design_scan_test: the hand-built memories are marked the way pass.bitwidth marks them (create_proven_memory), and UnprovenMemoryIsRefused checks that the same ROM without the mark is refused with the new message. 78 tests pass. - lhd: the 12-entry {ptr, wid} RAM exports with plain lhd, single-step and through the census flow's three steps (compile to lg:, single_edge, compile to lean:); with --set sim.debug=true the export stops with "Memory node n_16 (size 12): pass.bitwidth did not prove every port address inside [0, size), so no certificate is written".
1 parent 4d12904 commit 6cb2f0e

2 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎pass/lean/Exporter/Frontend/design_scan_test.cpp‎

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ namespace lean_export {
2727
namespace {
2828
using livehd::graph_util::create_typed_node;
2929
using livehd::graph_util::set_bits;
30+
31+
// A Memory node as pass.bitwidth leaves it: every port address proven inside
32+
// [0, size) (Bitwidth::check_mem_address). pass.lean writes no certificate for a
33+
// memory without that mark (UnprovenMemoryIsRefused).
34+
auto create_proven_memory(auto& g) {
35+
auto m = create_typed_node(g, Ntype_op::Memory);
36+
m.attr(livehd::attrs::mem_address_proven).set(uint32_t{1});
37+
return m;
38+
}
3039
TEST(DesignScan, OwnsPinsAndPreservesPortDeclarationRoots) {
3140
DesignScan design;
3241
{
@@ -865,7 +874,7 @@ TEST(DesignScan, SynchronousRomRetainsContentsAndEnable) {
865874
io->add_output("data", 1);
866875
io->set_bits("data", 8);
867876
auto g = io->create_graph();
868-
auto memory = create_typed_node(*g, Ntype_op::Memory);
877+
auto memory = create_proven_memory(*g);
869878
set_bits(memory.create_driver_pin(0), 8);
870879
auto policy = [&](std::string_view name, int64_t value) {
871880
auto pin = livehd::graph_util::create_const(*g, *Dlop::create_integer(value));
@@ -886,6 +895,43 @@ TEST(DesignScan, SynchronousRomRetainsContentsAndEnable) {
886895
EXPECT_EQ(cert.sources[0].rom_contents, (std::vector<std::string>{"1", "2", "3"}));
887896
EXPECT_EQ(cert.nodes.back().op.kind, Operation::MuxBool);
888897
}
898+
899+
// The same ROM WITHOUT pass.bitwidth's address proof (what `--set
900+
// sim.debug=true` leaves): no certificate is written, and the refusal says why.
901+
TEST(DesignScan, UnprovenMemoryIsRefused) {
902+
auto& lib = livehd::Hhds_graph_library::instance("lgdb_scan_unproven");
903+
auto io = lib.create_io("unproven_rom");
904+
io->add_input("address", 1);
905+
io->set_bits("address", 2);
906+
io->add_input("enable", 2);
907+
io->set_bits("enable", 1);
908+
io->add_output("data", 1);
909+
io->set_bits("data", 8);
910+
auto g = io->create_graph();
911+
auto memory = create_typed_node(*g, Ntype_op::Memory);
912+
set_bits(memory.create_driver_pin(0), 8);
913+
auto policy = [&](std::string_view name, int64_t value) {
914+
auto pin = livehd::graph_util::create_const(*g, *Dlop::create_integer(value));
915+
pin.connect_sink(memory.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, name)));
916+
};
917+
policy("bits", 8);
918+
policy("size", 3);
919+
policy("type", 1);
920+
policy("rdport", 1);
921+
policy("initial", 0x030201);
922+
g->get_input_pin("address").connect_sink(memory.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, "addr")));
923+
g->get_input_pin("enable").connect_sink(memory.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, "enable")));
924+
memory.create_driver_pin(0).connect_sink(g->get_output_pin("data"));
925+
try {
926+
const auto design = scan_design(*g, {});
927+
(void)build_certificate(design, {});
928+
FAIL() << "a memory pass.bitwidth did not prove was exported";
929+
} catch (const std::exception& e) {
930+
EXPECT_NE(std::string(e.what()).find("pass.bitwidth did not prove every port address inside [0, size)"),
931+
std::string::npos)
932+
<< e.what();
933+
}
934+
}
889935
// Three transparent wrappers may sit between a primary input and an
890936
// asynchronous reset pin: the emitter's arity-1 Or resize, a zero-based
891937
// Get_mask window covering the source, and a ONE-BIT Sext. The last is what
@@ -1951,7 +1997,7 @@ std::shared_ptr<hhds::Graph> build_mem_fixture(std::string_view lgdb, bool read_
19511997
auto g = io->create_graph();
19521998

19531999
// -- the RAM: write port 0, synchronous read port 1, both on clk_a ---------
1954-
auto ram = create_typed_node(*g, Ntype_op::Memory);
2000+
auto ram = create_proven_memory(*g);
19552001
const uint32_t wport = read_port_first ? 1 : 0;
19562002
const uint32_t rport = 1 - wport;
19572003
// The read port's data output: pin (#write ports + read ordinal) = 1, which
@@ -1989,7 +2035,7 @@ std::shared_ptr<hhds::Graph> build_mem_fixture(std::string_view lgdb, bool read_
19892035
ram.create_driver_pin(1).connect_sink(g->get_output_pin("rdata"));
19902036

19912037
// -- the ROM on the OTHER domain -----------------------------------------
1992-
auto rom = create_typed_node(*g, Ntype_op::Memory);
2038+
auto rom = create_proven_memory(*g);
19932039
set_bits(rom.create_driver_pin(0), 8);
19942040
auto osink = [&](std::string_view n) { return rom.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, n)); };
19952041
auto oconst = [&](std::string_view n, int64_t v) {
@@ -3072,7 +3118,7 @@ TEST(DesignScanClocks, PerReadRowForwardingValueOracle) {
30723118
io->add_output("rd1", 2);
30733119
io->set_bits("rd1", 8);
30743120
auto g = io->create_graph();
3075-
auto mem = create_typed_node(*g, Ntype_op::Memory);
3121+
auto mem = create_proven_memory(*g);
30763122
const auto mstride = static_cast<uint32_t>(Ntype::Memory_port_stride);
30773123
auto sink = [&](std::string_view n, uint32_t port) {
30783124
return mem.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, n) + port * mstride);
@@ -3214,7 +3260,7 @@ TEST(DesignScanClocks, TwoWriteTwoReadCollisionValueOracle) {
32143260
io->add_output("rd1", 2);
32153261
io->set_bits("rd1", 8);
32163262
auto g = io->create_graph();
3217-
auto mem = create_typed_node(*g, Ntype_op::Memory);
3263+
auto mem = create_proven_memory(*g);
32183264
const auto mstride = static_cast<uint32_t>(Ntype::Memory_port_stride);
32193265
auto sink = [&](std::string_view n, uint32_t port) {
32203266
return mem.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, n) + port * mstride);
@@ -3385,7 +3431,7 @@ TEST(DesignScanClocks, WideMatrixHighBitForwardingValueOracle) {
33853431
io->add_output("rd1", 2);
33863432
io->set_bits("rd1", 8);
33873433
auto g = io->create_graph();
3388-
auto mem = create_typed_node(*g, Ntype_op::Memory);
3434+
auto mem = create_proven_memory(*g);
33893435
const auto mstride = static_cast<uint32_t>(Ntype::Memory_port_stride);
33903436
auto sink = [&](std::string_view n, uint32_t port) {
33913437
return mem.create_sink_pin(Ntype::get_sink_pid(Ntype_op::Memory, n) + port * mstride);
@@ -3578,7 +3624,7 @@ MemFix build_mem(hhds::GraphLibrary& lib, const std::string& mod, bool sync_read
35783624
MemFix f;
35793625
f.g = io->create_graph();
35803626
auto* g = f.g.get();
3581-
f.mem = create_typed_node(*g, Ntype_op::Memory);
3627+
f.mem = create_proven_memory(*g);
35823628
const int stride = static_cast<int>(Ntype::Memory_port_stride);
35833629
auto konst = [&](int64_t v) { return livehd::graph_util::create_const(*g, *Dlop::create_integer(v)); };
35843630
auto global = [&](const char* name, int64_t v) {
@@ -3980,7 +4026,7 @@ std::shared_ptr<hhds::Graph> build_chained_memories(hhds::GraphLibrary& lib, con
39804026
const int stride = static_cast<int>(Ntype::Memory_port_stride);
39814027
auto konst = [&](int64_t v) { return livehd::graph_util::create_const(*g, *Dlop::create_integer(v)); };
39824028
auto build = [&](const hhds::Pin_class& wr_data, hhds::Node_class* out_mem) {
3983-
auto mem = create_typed_node(*g, Ntype_op::Memory);
4029+
auto mem = create_proven_memory(*g);
39844030
if (out_mem != nullptr) {
39854031
*out_mem = mem;
39864032
}

‎pass/lean/Exporter/Frontend/graph_access.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ Memory_info parse_memory_info(LeanCtx& ctx, const Node& node) {
368368
if (mi.bits == 0 || mi.size == 0) {
369369
fatal(ctx, "Memory node n_" + std::to_string(mi.nid) + " is missing constant bits/size policy.");
370370
}
371+
// pass.bitwidth proves every port address inside [0, size) and records it on
372+
// the node (Bitwidth::check_mem_address). Without that proof an address may
373+
// reach a word the memory does not have -- the certificate's table is
374+
// 2^addr_width words, and the address resize below wraps a wider index --
375+
// so no certificate is written. `--set sim.debug=true` lets a debug simulator
376+
// run unproven memories with a run-time check; it cannot make a certificate.
377+
if (!node.attr(livehd::attrs::mem_address_proven).has()) {
378+
fatal(ctx,
379+
"Memory node n_" + std::to_string(mi.nid) + " (size " + std::to_string(mi.size)
380+
+ "): pass.bitwidth did not prove every port address inside [0, size), so no certificate is written"
381+
+ " (was the design compiled with --set sim.debug=true?)");
382+
}
371383
check_width(ctx, node, mi.bits, "memory data");
372384
mi.addr_width = ceil_log2_u64(mi.size);
373385
if (mi.addr_width == 0 || mi.addr_width > ctx.max_width) {

0 commit comments

Comments
 (0)