Skip to content

Invoke functions marked __attribute__((destructor)) #6603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions regression/cbmc/destructor1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <assert.h>

#ifdef __GNUC__
// Will be implicitly invoked after main() completes if the attribute is
// properly supported
__attribute__((destructor))
#endif
void assert_false(void)
{
assert(0);
}

int main()
{
#ifndef __GNUC__
// explicitly invoke assert_false as __attribute__((destructor)) isn't
// supported in non-GCC modes
assert_false();
#endif
}
9 changes: 9 additions & 0 deletions regression/cbmc/destructor1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c

^\[assert_false.assertion.1\] line 10 assertion 0: FAILURE$
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
^warning: ignoring
20 changes: 20 additions & 0 deletions src/ansi-c/ansi_c_entry_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,26 @@ bool generate_ansi_c_start_function(

record_function_outputs(symbol, init_code, symbol_table);

// now call destructor functions (a GCC extension)

for(const auto &symbol_table_entry : symbol_table.symbols)
{
const symbolt &symbol = symbol_table_entry.second;

if(symbol.type.id() != ID_code)
continue;

const code_typet &code_type = to_code_type(symbol.type);
if(
code_type.return_type().id() == ID_destructor &&
code_type.parameters().empty())
{
code_function_callt destructor_call(symbol.symbol_expr());
destructor_call.add_source_location() = symbol.location;
init_code.add(std::move(destructor_call));
}
}

// add the entry point symbol
symbolt new_symbol;

Expand Down