Skip to content

Commit

Permalink
Merge branch 'topic/bbannier/coverity'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 14, 2025
2 parents 651c6b1 + 2dc508d commit 8c73937
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
15 changes: 15 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
1.13.0-dev.71 | 2025-01-14 17:26:02 +0100

* Prevent unneeded copy in setting up unit `parseX` functions. (Benjamin Bannier, Corelight)

* Used fixed initialization for order for global static. (Benjamin Bannier, Corelight)

The `HILTI_PRE_INIT` macros work on the global
`_registered_preinit_functions` container. Since uses of the macro and
the global container are likely in different translation units their
initialization order can be undetermined (even though we have some
manual checks whether the `unique_ptr<vector>>` is null).

This patch cleans up initialization of the global so it should always be
fully initialized when accessed.

1.13.0-dev.68 | 2025-01-13 22:54:50 +0100

* GH-1958: Prevent copies in various places. (Benjamin Bannier, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0-dev.68
1.13.0-dev.71
4 changes: 2 additions & 2 deletions hilti/runtime/include/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern void registerModule(HiltiModule module);
* Macro to schedule a global function to be called at startup time. Execution
* will happen either automatically through a static constructor (default), or
* if `HILTI_MANUAL_PREINIT` is defined, be triggered through a call to
* `executeCustomPreInits()`.
* `executeManualPreInits()`.
*/
#ifdef HILTI_MANUAL_PREINIT
#define HILTI_PRE_INIT(func) static ::hilti::rt::detail::RegisterManualPreInit __pre_init_##__COUNTER__(func);
Expand All @@ -62,7 +62,7 @@ class ExecutePreInit {
ExecutePreInit(void (*f)()) { (*f)(); }
};

/** Helper class to register a global function to execute through `executeCustomPreInits`. */
/** Helper class to register a global function to execute through `executeManualPreInits`. */
class RegisterManualPreInit {
public:
RegisterManualPreInit(void (*f)());
Expand Down
20 changes: 8 additions & 12 deletions hilti/runtime/src/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <sys/resource.h>
#include <unistd.h>

#include <algorithm>
#include <cinttypes>
#include <cstring>
#include <memory>
Expand Down Expand Up @@ -108,21 +107,18 @@ void hilti::rt::detail::registerModule(HiltiModule module) {
globalState()->hilti_modules.emplace_back(module);
}

static std::unique_ptr<std::vector<void (*)()>> _registered_preinit_functions;

RegisterManualPreInit::RegisterManualPreInit(void (*f)()) {
if ( ! _registered_preinit_functions )
_registered_preinit_functions = std::make_unique<std::vector<void (*)()>>();

_registered_preinit_functions->emplace_back(f);
static auto* registered_preinit_functions() {
static std::vector<void (*)()> _registered_preinit_functions;
return &_registered_preinit_functions;
}

RegisterManualPreInit::RegisterManualPreInit(void (*f)()) { registered_preinit_functions()->emplace_back(f); }

void hilti::rt::executeManualPreInits() {
if ( ! _registered_preinit_functions )
return;
auto* functions = registered_preinit_functions();

for ( const auto& f : *_registered_preinit_functions )
for ( const auto& f : *functions )
(*f)();

_registered_preinit_functions.reset();
functions->clear();
}
5 changes: 3 additions & 2 deletions hilti/toolchain/src/compiler/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,9 @@ struct GlobalsVisitor : hilti::visitor::PostOrder {
cb.addReturn("::hilti::rt::Nothing()");
}

body.addLambda("cb", "[args_on_heap](::hilti::rt::resumable::Handle* r) -> ::hilti::rt::any",
std::move(cb));
body.addLambda(
"cb", "[args_on_heap = std::move(args_on_heap)](::hilti::rt::resumable::Handle* r) -> ::hilti::rt::any",
std::move(cb));
body.addLocal({"r", "auto", {}, "std::make_unique<::hilti::rt::Resumable>(std::move(cb))"});
body.addStatement("r->run()");
body.addReturn("std::move(*r)");
Expand Down
2 changes: 1 addition & 1 deletion tests/Baseline/hilti.hiltic.print.yield/output
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern auto __hlt::Foo::test(const std::string& x) -> std::string {
extern auto hlt::Foo::test(const std::string& x) -> ::hilti::rt::Resumable {
auto args = std::make_tuple(::hilti::rt::resumable::detail::copyArg(x));
auto args_on_heap = std::make_shared<decltype(args)>(std::move(args));
auto cb = [args_on_heap](::hilti::rt::resumable::Handle* r) -> ::hilti::rt::any {
auto cb = [args_on_heap = std::move(args_on_heap)](::hilti::rt::resumable::Handle* r) -> ::hilti::rt::any {
return __hlt::Foo::test(std::get<0>(*args_on_heap));
};

Expand Down

0 comments on commit 8c73937

Please sign in to comment.