-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer loop assigns for DFCC with functions inlined
- Loading branch information
Showing
7 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
regression/contracts-dfcc/loop_assigns_inference-04/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
struct hole | ||
{ | ||
int pos; | ||
}; | ||
|
||
void set_len(struct hole *h, unsigned long int new_len) | ||
{ | ||
h->pos = new_len; | ||
} | ||
|
||
int main() | ||
{ | ||
int i = 0; | ||
struct hole h; | ||
h.pos = 0; | ||
for(i = 0; i < 5; i++) | ||
// __CPROVER_assigns(h.pos, i) | ||
__CPROVER_loop_invariant(h.pos == i) | ||
{ | ||
set_len(&h, h.pos + 1); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
regression/contracts-dfcc/loop_assigns_inference-04/test.desc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CORE dfcc-only | ||
main.c | ||
--dfcc main --apply-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
^\[main.loop_invariant_base.\d+\] line \d+ Check invariant before entry for loop .*: SUCCESS$ | ||
^\[main.loop_invariant_base.\d+\] line \d+ Check invariant before entry for loop .*: SUCCESS$ | ||
^\[main.loop_invariant_step.\d+\] line \d+ Check invariant after step for loop .*: SUCCESS$ | ||
^\[main.loop_step_unwinding.\d+\] line \d+ Check step was unwound for loop .*: SUCCESS$ | ||
^\[set_len.assigns.\d+\] line \d+ Check that h\-\>pos is assignable: SUCCESS | ||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test checks assigns h->pos is inferred correctly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,13 @@ Author: Remi Delmas, [email protected] | |
#include <util/pointer_expr.h> | ||
#include <util/std_code.h> | ||
|
||
#include <goto-programs/goto_inline.h> | ||
|
||
#include <analyses/goto_rw.h> | ||
#include <goto-instrument/contracts/utils.h> | ||
#include <goto-instrument/havoc_utils.h> | ||
|
||
#include "dfcc_loop_nesting_graph.h" | ||
#include "dfcc_root_object.h" | ||
|
||
/// Builds a call expression `object_whole(expr)` | ||
|
@@ -46,10 +50,12 @@ depends_on(const exprt &expr, std::unordered_set<irep_idt> identifiers) | |
return false; | ||
} | ||
|
||
assignst dfcc_infer_loop_assigns( | ||
assignst dfcc_infer_loop_assigns_for_loop( | ||
const local_may_aliast &local_may_alias, | ||
goto_functiont &goto_function, | ||
const loopt &loop_instructions, | ||
const source_locationt &loop_head_location, | ||
message_handlert &message_handler, | ||
const namespacet &ns) | ||
{ | ||
// infer | ||
|
@@ -105,3 +111,99 @@ assignst dfcc_infer_loop_assigns( | |
|
||
return result; | ||
} | ||
|
||
/// Remove assignments to `__CPROVER_dead_object` to avoid aliasing all targets | ||
/// that are assigned to `__CPROVER_dead_object`. | ||
static void remove_dead_object_assignment(goto_functiont &goto_function) | ||
{ | ||
Forall_goto_program_instructions(i_it, goto_function.body) | ||
{ | ||
if(i_it->is_assign()) | ||
{ | ||
auto &lhs = i_it->assign_lhs(); | ||
|
||
if( | ||
lhs.id() == ID_symbol && | ||
to_symbol_expr(lhs).get_identifier() == CPROVER_PREFIX "dead_object") | ||
{ | ||
i_it->turn_into_skip(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void dfcc_infer_loop_assigns_for_function( | ||
std::map<std::size_t, assignst> &inferred_loop_assigns_map, | ||
goto_functionst &goto_functions, | ||
goto_functiont &goto_function, | ||
message_handlert &message_handler, | ||
const namespacet &ns) | ||
{ | ||
messaget log(message_handler); | ||
|
||
// We infer loop assigns based on the copy of `goto_function`. | ||
goto_functiont goto_function_copy; | ||
goto_function_copy.copy_from(goto_function); | ||
|
||
// Map from targett in `goto_function_copy` to loop number. | ||
std:: | ||
unordered_map<goto_programt::const_targett, std::size_t, const_target_hash> | ||
loop_number_map; | ||
|
||
// Build the loop id map before inlining attempt. | ||
const auto loop_nesting_graph = | ||
build_loop_nesting_graph(goto_function_copy.body); | ||
{ | ||
auto topsorted = loop_nesting_graph.topsort(); | ||
// skip function without loop. | ||
if(topsorted.empty()) | ||
return; | ||
|
||
for(const auto id : topsorted) | ||
{ | ||
loop_number_map.emplace( | ||
loop_nesting_graph[id].head, loop_nesting_graph[id].latch->loop_number); | ||
} | ||
} | ||
|
||
// We avoid inlining `malloc` and `free` whose variables should not be assigns. | ||
auto malloc_body = goto_functions.function_map.extract(irep_idt("malloc")); | ||
auto free_body = goto_functions.function_map.extract(irep_idt("free")); | ||
|
||
// Inline all function calls in goto_function_copy. | ||
goto_program_inline( | ||
goto_functions, goto_function_copy.body, ns, log.get_message_handler()); | ||
goto_function_copy.body.update(); | ||
// Build the loop graph after inlining. | ||
const auto inlined_loop_nesting_graph = | ||
build_loop_nesting_graph(goto_function_copy.body); | ||
|
||
// Alias analysis. | ||
remove_dead_object_assignment(goto_function_copy); | ||
local_may_aliast local_may_alias(goto_function_copy); | ||
|
||
auto inlined_topsorted = inlined_loop_nesting_graph.topsort(); | ||
|
||
for(const auto inlined_id : inlined_topsorted) | ||
{ | ||
// We only infer loop assigns for loops in the original function. | ||
if( | ||
loop_number_map.find(inlined_loop_nesting_graph[inlined_id].head) != | ||
loop_number_map.end()) | ||
{ | ||
const auto loop_number = | ||
loop_number_map[inlined_loop_nesting_graph[inlined_id].head]; | ||
const auto inlined_loop = inlined_loop_nesting_graph[inlined_id]; | ||
|
||
inferred_loop_assigns_map[loop_number] = dfcc_infer_loop_assigns_for_loop( | ||
local_may_alias, | ||
goto_function_copy, | ||
inlined_loop.instructions, | ||
inlined_loop.head->source_location(), | ||
message_handler, | ||
ns); | ||
} | ||
} | ||
goto_functions.function_map.insert(std::move(malloc_body)); | ||
goto_functions.function_map.insert(std::move(free_body)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,25 @@ Author: Remi Delmas, [email protected] | |
class source_locationt; | ||
class messaget; | ||
class namespacet; | ||
class message_handlert; | ||
|
||
// \brief Infer assigns clause targets for a loop from its instructions and a | ||
// may alias analysis at the function level. | ||
assignst dfcc_infer_loop_assigns( | ||
assignst dfcc_infer_loop_assigns_for_loop( | ||
const local_may_aliast &local_may_alias, | ||
goto_functiont &goto_function, | ||
const loopt &loop_instructions, | ||
const source_locationt &loop_head_location, | ||
message_handlert &message_handler, | ||
const namespacet &ns); | ||
|
||
// \brief Infer assigns clause targets for loops in `goto_function` from their | ||
// instructions and a may alias analysis at the function level (with inlining). | ||
void dfcc_infer_loop_assigns_for_function( | ||
std::map<std::size_t, assignst> &inferred_loop_assigns_map, | ||
goto_functionst &goto_functions, | ||
goto_functiont &goto_function, | ||
message_handlert &message_handler, | ||
const namespacet &ns); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters