Skip to content

Commit 4b9baa9

Browse files
committed
Lift alloca's deallocation behaviour out of goto_check_ct
alloca always results in deallocation taking place, and not just when pointer checks are enabled.
1 parent 28c4f7b commit 4b9baa9

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/ansi-c/goto_check_c.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,28 +2188,6 @@ void goto_check_ct::goto_check(
21882188
std::move(lhs), std::move(rhs), i.source_location()));
21892189
t->code_nonconst().add_source_location() = i.source_location();
21902190
}
2191-
2192-
if(
2193-
variable.type().id() == ID_pointer &&
2194-
function_identifier != "alloca" &&
2195-
(ns.lookup(variable.get_identifier()).base_name ==
2196-
"return_value___builtin_alloca" ||
2197-
ns.lookup(variable.get_identifier()).base_name ==
2198-
"return_value_alloca"))
2199-
{
2200-
// mark pointer to alloca result as dead
2201-
exprt lhs = ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr();
2202-
exprt alloca_result =
2203-
typecast_exprt::conditional_cast(variable, lhs.type());
2204-
if_exprt rhs(
2205-
side_effect_expr_nondett(bool_typet(), i.source_location()),
2206-
std::move(alloca_result),
2207-
lhs);
2208-
goto_programt::targett t =
2209-
new_code.add(goto_programt::make_assignment(
2210-
std::move(lhs), std::move(rhs), i.source_location()));
2211-
t->code_nonconst().add_source_location() = i.source_location();
2212-
}
22132191
}
22142192
}
22152193
else if(i.is_end_function())

src/goto-programs/builtin_functions.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,40 @@ void goto_convertt::do_function_call_symbol(
13891389

13901390
copy(function_call, FUNCTION_CALL, dest);
13911391
}
1392+
else if(
1393+
(identifier == "alloca" || identifier == "__builtin_alloca") &&
1394+
function.source_location().get_function() != "alloca")
1395+
{
1396+
if(lhs.is_nil())
1397+
{
1398+
// this will trigger a recursive call
1399+
side_effect_expr_function_callt call{
1400+
function, arguments, function.type(), function.source_location()};
1401+
make_temp_symbol(call, "alloca", dest, mode);
1402+
}
1403+
else
1404+
{
1405+
code_function_callt function_call(lhs, function, arguments);
1406+
function_call.add_source_location() = function.source_location();
1407+
copy(function_call, FUNCTION_CALL, dest);
1408+
1409+
exprt new_lhs = lhs;
1410+
make_temp_symbol(new_lhs, "alloca", dest, mode);
1411+
1412+
// mark pointer to alloca result as dead
1413+
symbol_exprt dead_object_sym =
1414+
ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr();
1415+
exprt alloca_result =
1416+
typecast_exprt::conditional_cast(new_lhs, dead_object_sym.type());
1417+
if_exprt rhs{
1418+
side_effect_expr_nondett{bool_typet(), function.source_location()},
1419+
std::move(alloca_result),
1420+
dead_object_sym};
1421+
code_assignt assign{std::move(dead_object_sym), std::move(rhs)};
1422+
assign.add_source_location() = function.source_location();
1423+
targets.destructor_stack.add(assign);
1424+
}
1425+
}
13921426
else
13931427
{
13941428
do_function_call_symbol(*symbol);

0 commit comments

Comments
 (0)