Skip to content

GOTO conversion: Declaration hops must not invalidate incomplete gotos #8349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/ansi-c/goto-conversion/goto_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@
node_indext end_scope_index = 0;
};

void goto_convertt::build_declaration_hops(
goto_convertt::declaration_hop_instrumentationt

Check warning on line 106 in src/ansi-c/goto-conversion/goto_convert.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_convert.cpp#L106

Added line #L106 was not covered by tests
goto_convertt::build_declaration_hops(
goto_programt &program,
std::unordered_map<irep_idt, symbolt, irep_id_hash> &label_flags,
const build_declaration_hops_inputst &inputs)
{
// In the case of a goto jumping into a scope, the declarations (but not the
// initialisations) need to be executed. This function performs a
// initialisations) need to be executed. This function prepares a
// transformation from code that looks like -
// {
// statement_block_a();
Expand Down Expand Up @@ -150,9 +151,14 @@
// __CPROVER_going_to::user_label = false;
// statement_block_e();
// }
//

Check warning on line 154 in src/ansi-c/goto-conversion/goto_convert.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_convert.cpp#L154

Added line #L154 was not covered by tests
// The actual insertion of instructions needs to be performed by the caller,
// which needs to use the result of this method.

Check warning on line 156 in src/ansi-c/goto-conversion/goto_convert.cpp

View check run for this annotation

Codecov / codecov/patch

src/ansi-c/goto-conversion/goto_convert.cpp#L156

Added line #L156 was not covered by tests

PRECONDITION(inputs.label_scope_index != inputs.end_scope_index);

declaration_hop_instrumentationt instructions_to_add;

const auto flag = [&]() -> symbolt {
const auto existing_flag = label_flags.find(inputs.label);
if(existing_flag != label_flags.end())
Expand All @@ -170,19 +176,20 @@
label_flags.emplace(inputs.label, new_flag);

// Create and initialise flag.
goto_programt flag_creation;
flag_creation.instructions.push_back(
instructions_to_add.emplace_back(
program.instructions.begin(),
goto_programt::make_decl(new_flag.symbol_expr(), label_location));
const auto make_clear_flag = [&]() -> goto_programt::instructiont {
return goto_programt::make_assignment(
new_flag.symbol_expr(), false_exprt{}, label_location);
};
flag_creation.instructions.push_back(make_clear_flag());
program.destructive_insert(program.instructions.begin(), flag_creation);
instructions_to_add.emplace_back(
program.instructions.begin(), make_clear_flag());

// Clear flag on arrival at label.
auto clear_on_arrival = make_clear_flag();
program.insert_before_swap(inputs.label_instruction, clear_on_arrival);
instructions_to_add.emplace_back(
inputs.label_instruction, clear_on_arrival);
return new_flag;
}();

Expand All @@ -193,9 +200,7 @@
goto_location.set_hide();
auto set_flag = goto_programt::make_assignment(
flag.symbol_expr(), true_exprt{}, goto_location);
program.insert_before_swap(goto_instruction, set_flag);
// Keep this iterator referring to the goto instruction, not the assignment.
++goto_instruction;
instructions_to_add.emplace_back(goto_instruction, set_flag);
}

auto target = inputs.label_instruction;
Expand All @@ -216,6 +221,8 @@
declaration_location.set_hide();
auto if_goto = goto_programt::make_goto(
target, flag.symbol_expr(), declaration_location);
// this isn't changing any previously existing instruction so we insert
// directly rather than going through instructions_to_add
program.instructions.insert(
std::next(declaration->instruction), std::move(if_goto));
declaration->accounted_flags.insert(flag.name);
Expand All @@ -226,6 +233,8 @@
// Update the goto so that it goes to the first declaration rather than its
// original/final destination.
goto_instruction->set_target(target);

return instructions_to_add;
}

/******************************************************************* \
Expand All @@ -243,6 +252,7 @@
void goto_convertt::finish_gotos(goto_programt &dest, const irep_idt &mode)
{
std::unordered_map<irep_idt, symbolt, irep_id_hash> label_flags;
declaration_hop_instrumentationt instructions_to_insert;

for(const auto &g_it : targets.gotos)
{
Expand Down Expand Up @@ -331,7 +341,9 @@
inputs.label_instruction = l_it->second.first;
inputs.label_scope_index = label_target;
inputs.end_scope_index = intersection_result.common_ancestor;
build_declaration_hops(dest, label_flags, inputs);
instructions_to_insert.splice(
instructions_to_insert.end(),
build_declaration_hops(dest, label_flags, inputs));
}
}
else
Expand All @@ -340,6 +352,13 @@
}
}

for(auto r_it = instructions_to_insert.rbegin();
r_it != instructions_to_insert.rend();
++r_it)
{
dest.insert_before_swap(r_it->first, r_it->second);
}

targets.gotos.clear();
}

Expand Down
5 changes: 4 additions & 1 deletion src/ansi-c/goto-conversion/goto_convert_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ class goto_convertt : public messaget
std::optional<node_indext> destructor_end_point = {},
std::optional<node_indext> destructor_start_point = {});

void build_declaration_hops(
typedef std::list<
std::pair<goto_programt::targett, goto_programt::instructiont>>
declaration_hop_instrumentationt;
[[nodiscard]] declaration_hop_instrumentationt build_declaration_hops(
goto_programt &dest,
std::unordered_map<irep_idt, symbolt, irep_id_hash> &label_flags,
const build_declaration_hops_inputst &inputs);
Expand Down
Loading