Skip to content

Commit 6982b9d

Browse files
authored
Merge pull request #6098 from tautschnig/alloca-check
Detect use of alloca-allocated object after leaving its scope
2 parents 60abd1e + 7d97f9a commit 6982b9d

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
@@ -2115,6 +2115,28 @@ void goto_checkt::goto_check(
21152115
std::move(lhs), std::move(rhs), i.source_location));
21162116
t->code_nonconst().add_source_location() = i.source_location;
21172117
}
2118+
2119+
if(
2120+
variable.type().id() == ID_pointer &&
2121+
function_identifier != "alloca" &&
2122+
(ns.lookup(variable.get_identifier()).base_name ==
2123+
"return_value___builtin_alloca" ||
2124+
ns.lookup(variable.get_identifier()).base_name ==
2125+
"return_value_alloca"))
2126+
{
2127+
// mark pointer to alloca result as dead
2128+
exprt lhs = ns.lookup(CPROVER_PREFIX "dead_object").symbol_expr();
2129+
exprt alloca_result =
2130+
typecast_exprt::conditional_cast(variable, lhs.type());
2131+
if_exprt rhs(
2132+
side_effect_expr_nondett(bool_typet(), i.source_location),
2133+
std::move(alloca_result),
2134+
lhs);
2135+
goto_programt::targett t =
2136+
new_code.add(goto_programt::make_assignment(
2137+
std::move(lhs), std::move(rhs), i.source_location));
2138+
t->code_nonconst().add_source_location() = i.source_location;
2139+
}
21182140
}
21192141
}
21202142
else if(i.is_end_function())

0 commit comments

Comments
 (0)