Skip to content

Commit 7d97f9a

Browse files
committed
Detect use of alloca-allocated object after leaving its scope
alloca-allocated objects need no longer be usable outside their scope. Detect such use by non-deterministically setting __CPROVER_dead_object to return_value___builtin_alloca. This fixes the verification result for SV-COMP's memsafety-ext3/getNumbers1-1.c.
1 parent 55d32b5 commit 7d97f9a

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
3+
#ifdef _WIN32
4+
void *alloca(size_t alloca_size);
5+
#endif
6+
7+
int *foo()
8+
{
9+
int *foo_ptr = alloca(sizeof(int));
10+
return foo_ptr;
11+
}
12+
13+
int main()
14+
{
15+
int *from_foo = foo();
16+
*from_foo = 42; // access to object that has gone out of scope
17+
18+
return 0;
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
dereference failure: dead object in \*from_foo: FAILURE$
5+
^\*\* 1 of 6 failed
6+
^VERIFICATION FAILED$
7+
^EXIT=10$
8+
^SIGNAL=0$
9+
--
10+
^warning: ignoring

src/analyses/goto_check.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,28 @@ void goto_checkt::goto_check(
21102110
std::move(lhs), std::move(rhs), i.source_location));
21112111
t->code_nonconst().add_source_location() = i.source_location;
21122112
}
2113+
2114+
if(
2115+
variable.type().id() == ID_pointer &&
2116+
function_identifier != "alloca" &&
2117+
(ns.lookup(variable.get_identifier()).base_name ==
2118+
"return_value___builtin_alloca" ||
2119+
ns.lookup(variable.get_identifier()).base_name ==
2120+
"return_value_alloca"))
2121+
{
2122+
// mark pointer to alloca result as dead
2123+
exprt lhs = ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr();
2124+
exprt alloca_result =
2125+
typecast_exprt::conditional_cast(variable, lhs.type());
2126+
if_exprt rhs(
2127+
side_effect_expr_nondett(bool_typet(), i.source_location),
2128+
std::move(alloca_result),
2129+
lhs);
2130+
goto_programt::targett t =
2131+
new_code.add(goto_programt::make_assignment(
2132+
std::move(lhs), std::move(rhs), i.source_location));
2133+
t->code_nonconst().add_source_location() = i.source_location;
2134+
}
21132135
}
21142136
}
21152137
else if(i.is_end_function())

0 commit comments

Comments
 (0)