Skip to content

Commit

Permalink
spdlog: do not drop logger in destructor
Browse files Browse the repository at this point in the history
For static objects, the order of destruction is in reverse of the order
of construction, but the latter is hard to control except among static
objects defined within the same translation unit. This means that spdlog
may be destructed before spdlog::drop() is invoked in the destructor,
which leads to a segmentation fault due to memory use-after-free.

Move call of spdlog::drop() from destructor to main() function, to
ensure the logger may be re-registered with the same name in tests.

Link: ros2/rclcpp#933
Link: gabime/spdlog#1738
Closes: https://hsdes.intel.com/appstore/article/#/22019839238
Signed-off-by: Peter Colberg <[email protected]>
  • Loading branch information
pcolberg committed Apr 8, 2024
1 parent b96e35c commit 8011090
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
12 changes: 8 additions & 4 deletions libraries/afu-test/afu_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ class afu {
app_.add_option("-t,--timeout", timeout_msec_, "test timeout (msec)")->default_str(std::to_string(timeout_msec_));
}
virtual ~afu() {
if (logger_)
spdlog::drop(logger_->name());
}

CLI::App & cli() { return app_; }
Expand Down Expand Up @@ -315,10 +313,16 @@ class afu {

int res = open_handle(test->afu_id());
if (res != exit_codes::not_run) {
return res;
goto done;
}

return run(app, test);
res = run(app, test);

done:
// Dropping a logger is optional, but not doing so will cause an exception
// when re-registering a logger with the same name, i.e., in test runs.
spdlog::drop(logger_->name());
return res;
}

virtual int run(CLI::App *app, command::ptr_t test)
Expand Down
1 change: 0 additions & 1 deletion samples/cxl_hello_fpga/he_cache_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ class afu {
}
virtual ~afu() {
if (fd_ > 0) close(fd_);
if (logger_) spdlog::drop(logger_->name());
}

CLI::App &cli() { return app_; }
Expand Down
2 changes: 0 additions & 2 deletions samples/cxl_host_exerciser/he_cache_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,6 @@ class afu {

if (fd_ > 0)
close(fd_);
if (logger_)
spdlog::drop(logger_->name());
}

CLI::App &cli() { return app_; }
Expand Down

0 comments on commit 8011090

Please sign in to comment.