Skip to content

Commit 64fcf41

Browse files
committed
Invoke functions marked __attribute__((destructor))
This is a GCC extension that our front-end parses, but an implementation of the desired semantics was hitherto missing.
1 parent 84dadda commit 64fcf41

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

regression/cbmc/destructor1/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
3+
#ifdef __GNUC__
4+
static __attribute__((destructor)) void assert_false(void)
5+
{
6+
assert(0);
7+
}
8+
#endif
9+
10+
int main()
11+
{
12+
#ifndef __GNUC__
13+
// explicitly invoke assert_false as __attribute__((destructor)) isn't
14+
// supported
15+
assert_false();
16+
#endif
17+
}

regression/cbmc/destructor1/test.desc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^\[assert_false.assertion.1\] line 7 assertion 0: FAILURE$
5+
^VERIFICATION FAILED$
6+
^EXIT=10$
7+
^SIGNAL=0$
8+
--
9+
^warning: ignoring

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,26 @@ bool generate_ansi_c_start_function(
504504

505505
record_function_outputs(symbol, init_code, symbol_table);
506506

507+
// now call destructor functions (a GCC extension)
508+
509+
for(const auto &symbol_table_entry : symbol_table.symbols)
510+
{
511+
const symbolt &symbol = symbol_table_entry.second;
512+
513+
if(symbol.type.id() != ID_code)
514+
continue;
515+
516+
const code_typet &code_type = to_code_type(symbol.type);
517+
if(
518+
code_type.return_type().id() == ID_destructor &&
519+
code_type.parameters().empty())
520+
{
521+
code_function_callt function_call(symbol.symbol_expr());
522+
function_call.add_source_location() = symbol.location;
523+
init_code.add(std::move(function_call));
524+
}
525+
}
526+
507527
// add the entry point symbol
508528
symbolt new_symbol;
509529

0 commit comments

Comments
 (0)