[otbn,dv] Fix segfault when otbn_top_sim exited with failure
The verilator_top.reset() line was only being run when we exited
successfully. If not, we ended up destructing the std::unique_ptr and
thus its contents somewhere inside the CRT's run_exit_handlers.
It turns out that there's a race where a singleton in the Verilator
library *must* exist when you run the constructor or destructor and
you get a segfault if verilator_top happens to have a longer lifetime
than the singleton.
Fix things by being stupider: use a raw pointer to pass the object to
OtbnTopApplyLoopWarp (the DPI function that needs it). This will be
dangling after we exit main() and have destroyed the object, but we
don't really care.
Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
diff --git a/hw/ip/otbn/dv/verilator/otbn_top_sim.cc b/hw/ip/otbn/dv/verilator/otbn_top_sim.cc
index 9d70691..79c595c 100644
--- a/hw/ip/otbn/dv/verilator/otbn_top_sim.cc
+++ b/hw/ip/otbn/dv/verilator/otbn_top_sim.cc
@@ -95,18 +95,22 @@
}
};
-static std::unique_ptr<otbn_top_sim> verilator_top;
+static otbn_top_sim *verilator_top;
static OtbnMemUtil otbn_memutil("TOP.otbn_top_sim");
int main(int argc, char **argv) {
VerilatorMemUtil memutil(&otbn_memutil);
OtbnTraceUtil traceutil;
- verilator_top.reset(new otbn_top_sim());
+ otbn_top_sim top;
+ // Make the otbn_top_sim object visible to OtbnTopApplyLoopWarp.
+ // This will leave a dangling pointer when we exit main, but that
+ // doesn't really matter because we don't have anything that uses it
+ // running in atexit hooks.
+ verilator_top = ⊤
VerilatorSimCtrl &simctrl = VerilatorSimCtrl::GetInstance();
- simctrl.SetTop(verilator_top.get(), &verilator_top->IO_CLK,
- &verilator_top->IO_RST_N,
+ simctrl.SetTop(&top, &top.IO_CLK, &top.IO_RST_N,
VerilatorSimCtrlFlags::ResetPolarityNegative);
simctrl.RegisterExtension(&memutil);
simctrl.RegisterExtension(&traceutil);
@@ -199,8 +203,6 @@
}
}
- verilator_top.reset();
-
return 0;
}