test_rtnetlink: Force bus object to outlive TestRtNetlink lifetime
We were hitting a SEGFAULT by nullptr dereference in the WithSingleInterface
test when calling sd_bus_ref() via the timer callback with code generated at
-O2 from Ubuntu Artful's gcc-7.2.0:
Running main() from gtest_main.cc
Note: Google Test filter = TestRtNetlink.WithSingleInterface
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from TestRtNetlink
[ RUN ] TestRtNetlink.WithSingleInterface
ASAN:DEADLYSIGNAL
=================================================================
==19427==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f8a944922ca bp 0x0fffd7e37fc2 sp 0x7ffebf1bfda8 T0)
==19427==The signal is caused by a READ memory access.
==19427==Hint: address points to the zero page.
#0 0x7f8a944922c9 in sd_bus_ref (/lib/x86_64-linux-gnu/libsystemd.so.0+0x2e2c9)
#1 0x7f8a93a5c616 in sdbusplus::bus::bus::bus(sd_bus*) /usr/local/include/sdbusplus/bus.hpp:340
#2 0x7f8a93a5c616 in sdbusplus::server::interface::interface::interface(sdbusplus::bus::bus&, char const*, char const*, sd_bus_vtable const*, void*) /usr/local/include/sdbusplus/server/interface.hpp:61
...
#16 0x559d611755e7 in phosphor::network::Manager::createInterfaces() ../network_manager.cpp:151
#17 0x559d61178e12 in phosphor::network::Manager::createChildObjects() ../network_manager.cpp:166
#18 0x559d611ed59b in std::function<void ()>::operator()() const /usr/include/c++/7/bits/std_function.h:706
#19 0x559d611ed59b in phosphor::network::Timer::timeoutHandler(sd_event_source*, unsigned long, void*) ../timer.cpp:72
#20 0x7f8a9448e80d (/lib/x86_64-linux-gnu/libsystemd.so.0+0x2a80d)
#21 0x7f8a944903e9 in sd_event_dispatch (/lib/x86_64-linux-gnu/libsystemd.so.0+0x2c3e9)
#22 0x7f8a94490566 in sd_event_run (/lib/x86_64-linux-gnu/libsystemd.so.0+0x2c566)
#23 0x559d610e122e in phosphor::network::TestRtNetlink_WithSingleInterface_Test::TestBody() /tmp/openbmc-build-scripts.wUcrvx/phosphor-networkd/test/test_rtnetlink.cpp:135
...
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libsystemd.so.0+0x2e2c9) in sd_bus_ref
==19427==ABORTING
Move the bus storage to namespace scope alongside the Manager to avoid it
getting cleaned up before the callback fires.
Change-Id: I3126d0b7b92fb6e66d82f9e32b43a52e44356884
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp
index 52008ba..77b771a 100644
--- a/test/test_rtnetlink.cpp
+++ b/test/test_rtnetlink.cpp
@@ -17,7 +17,7 @@
namespace network
{
-
+sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
std::unique_ptr<Manager> manager = nullptr;
std::unique_ptr<Timer> refreshObjectTimer = nullptr;
std::unique_ptr<Timer> restartTimer = nullptr;
@@ -54,7 +54,6 @@
TestRtNetlink()
{
- sdbusplus::bus::bus bus(sdbusplus::bus::new_default());
manager =
std::make_unique<Manager>(bus,
"/xyz/openbmc_test/bcd",