Skip to content

Fix segfault in k-induction step case with nested loops#8803

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-5357-k-induction
Open

Fix segfault in k-induction step case with nested loops#8803
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-5357-k-induction

Conversation

@tautschnig
Copy link
Collaborator

The original code assumed loop_head->condition() always contained the loop guard, but in nested loop scenarios, the loop structure can vary. The backedge might contain the condition instead, or the loop might be unconditional.

Also, when processing nested loops, modifying the outer loop's goto-program could invalidate iterators pointing to the inner loop, causing memory access violations.

Co-authored-by: Kiro autonomous agent

Fixes: #5357

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@tautschnig tautschnig self-assigned this Feb 24, 2026
@tautschnig tautschnig force-pushed the fix-5357-k-induction branch from 7884d7e to 41e310e Compare March 10, 2026 17:00
@tautschnig tautschnig force-pushed the fix-5357-k-induction branch from 41e310e to 76d8afc Compare March 10, 2026 20:28
@tautschnig tautschnig marked this pull request as ready for review March 10, 2026 21:34
Copilot AI review requested due to automatic review settings March 10, 2026 21:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a segmentation fault in k-induction instrumentation when handling nested loops by making loop-guard detection more robust and avoiding iterator invalidation during loop processing.

Changes:

  • Added find_loop_guard to derive the loop guard from either the backedge or the loop-head exit goto (and handle unconditional loops).
  • Changed loop traversal to only instrument outermost loops to avoid invalidating nested-loop iterators.
  • Added a regression test covering nested loops.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/goto-instrument/k_induction.cpp Robustly locate loop guards and avoid processing nested loops directly to prevent segfaults
regression/k-induction/nested-loops/test.desc New regression test expectations for nested-loop k-induction instrumentation
regression/k-induction/nested-loops/main.c New nested-loop reproducer for the prior segfault

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +203 to +218
for(const auto &[loop_head, loop] : natural_loops.loop_map)
{
bool is_nested = false;

for(const auto &[other_head, other_loop] : natural_loops.loop_map)
{
if(other_head != loop_head && other_loop.contains(loop_head))
{
is_nested = true;
break;
}
}

if(!is_nested)
process_loop(loop_head, loop);
}
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you only process outermost loops, calling process_loop() mutates the underlying goto-program and can invalidate goto_programt::targett iterators stored inside natural_loops.loop_map. This loop continues iterating over natural_loops.loop_map after the first mutation, which can still lead to use-after-invalidation/segfaults when accessing subsequent loop_head / other_head entries. A safer approach is to first compute a stable worklist (e.g., store location_numbers of outermost loop heads), and then, for each item, re-find the current targett in the mutated program (or recompute natural loops after each transformation) before calling process_loop.

Copilot uses AI. Check for mistakes.
Comment on lines +203 to +214
for(const auto &[loop_head, loop] : natural_loops.loop_map)
{
bool is_nested = false;

for(const auto &[other_head, other_loop] : natural_loops.loop_map)
{
if(other_head != loop_head && other_loop.contains(loop_head))
{
is_nested = true;
break;
}
}
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested-loop detection is O(N²) over natural_loops.loop_map. On functions with many loops this can become noticeably expensive. Consider building a single pass parent/containment relation (e.g., sort loops by size and assign parents, or precompute a map from loop-head to smallest enclosing loop) to avoid repeatedly scanning the full loop map.

Copilot uses AI. Check for mistakes.
Comment on lines +80 to +90
goto_programt::targett backedge = loop_exit;
for(const auto &t : loop)
{
if(t->is_backwards_goto() && t->get_target() == loop_head)
{
backedge = t;
break;
}
}

if(backedge != loop_exit && !backedge->condition().is_true())
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop_exit is being used as a sentinel value for 'no backedge found'. This makes the control flow harder to reason about (and couples two unrelated concepts). Consider using an explicit bool found_backedge or an optional-like pattern to represent 'found vs not found', then only read backedge->condition() when a backedge was actually found.

Suggested change
goto_programt::targett backedge = loop_exit;
for(const auto &t : loop)
{
if(t->is_backwards_goto() && t->get_target() == loop_head)
{
backedge = t;
break;
}
}
if(backedge != loop_exit && !backedge->condition().is_true())
goto_programt::targett backedge;
bool found_backedge = false;
for(const auto &t : loop)
{
if(t->is_backwards_goto() && t->get_target() == loop_head)
{
backedge = t;
found_backedge = true;
break;
}
}
if(found_backedge && !backedge->condition().is_true())

Copilot uses AI. Check for mistakes.
The original code assumed loop_head->condition() always contained the
loop guard, but the loop head may not be a conditional goto instruction
(e.g., in nested loop scenarios). Fix by searching for the loop guard
in either the backedge or the forward goto at the loop head.

Also, processing a loop modifies the goto program (inserting
instructions and removing skips via remove_skip), which invalidates
iterators for nested loops. Only process outermost loops; inner loops
are handled as part of the outer loop body during unwinding.

Fixes: diffblue#5357

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.01%. Comparing base (b191cc1) to head (576cba3).

Files with missing lines Patch % Lines
src/goto-instrument/k_induction.cpp 90.47% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8803   +/-   ##
========================================
  Coverage    80.01%   80.01%           
========================================
  Files         1700     1700           
  Lines       188345   188360   +15     
  Branches        73       73           
========================================
+ Hits        150696   150711   +15     
  Misses       37649    37649           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation fault crash on simple case of k-induction (goto-instrument)

3 participants