Skip to content

Fix virtual method resolution for Java Object parameters#8759

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-930-virtual
Open

Fix virtual method resolution for Java Object parameters#8759
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-930-virtual

Conversation

@tautschnig
Copy link
Collaborator

The skip condition for avoiding re-processing of already-visited classes had inverted logic.

Fixes: #930

  • 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
The skip condition in get_child_functions_rec for avoiding
re-processing of already-visited classes had inverted logic. The
original code skipped children with no resolved symbol (unresolved
entries), preventing them from being re-visited. The fix only skips
children that have a concrete non-Object implementation, allowing
unresolved entries and Object-resolved entries to be re-visited.

The regression test uses a custom class with an overridden toString()
method called through an Object parameter, verifying that the virtual
dispatch correctly resolves to the subclass implementation.

The original test from issue diffblue#930 (String.equals) cannot be used as a
regression test because JBMC's String model does not support proving
equality of String objects through the equals() method.

Fixes: diffblue#930

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig marked this pull request as ready for review March 18, 2026 15:21
Copilot AI review requested due to automatic review settings March 18, 2026 15:21
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 JBMC’s Java virtual-call callee discovery so classes previously visited with an unresolved/java.lang.Object target aren’t prematurely skipped, which could prevent resolving the correct override when dispatching through Object-typed receivers/parameters (issue #930).

Changes:

  • Correct the “already visited” skip condition in get_child_functions_rec to only skip when a prior visit resolved to a non-java.lang.Object implementation.
  • Add a JBMC regression test for virtual dispatch through an Object-typed parameter, including the corresponding .desc and compiled .class artifacts.

Reviewed changes

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

Show a summary per file
File Description
src/goto-programs/remove_virtual_functions.cpp Fixes inverted skip logic in virtual callee traversal to enable correct resolution beyond java.lang.Object.
jbmc/regression/jbmc/virtual_equals_object_param/simple_test.desc Adds a new regression driver for the Java test.
jbmc/regression/jbmc/virtual_equals_object_param/SimpleEquals.java Adds a Java regression program exercising virtual dispatch via an Object-typed parameter.
jbmc/regression/jbmc/virtual_equals_object_param/SimpleEquals.class Compiled bytecode for the new regression.
jbmc/regression/jbmc/virtual_equals_object_param/SimpleEquals$MyClass.class Compiled bytecode for the nested class in the new regression.

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

Comment on lines 520 to +526
// Skip if we have already visited this and we found a function call that
// did not resolve to non java.lang.Object.
// resolved to a non-Object implementation (i.e., we don't need to revisit).
auto it = entry_map.find(child);
if(
it != entry_map.end() &&
(!it->second.symbol_expr.has_value() ||
!it->second.symbol_expr->get_identifier().starts_with(
"java::java.lang.Object")))
it != entry_map.end() && it->second.symbol_expr.has_value() &&
!it->second.symbol_expr->get_identifier().starts_with(
"java::java.lang.Object"))
Comment on lines +6 to +20
public String toString() {
counter++;
return "MyClass";
}
}

public static void testDirect() {
MyClass m = new MyClass();
m.toString();
assert(counter == 1);
}

public static void testThroughObject(Object o) {
// Virtual call through Object parameter should resolve to MyClass.toString
o.toString();
@codecov
Copy link

codecov bot commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.41%. Comparing base (85c204f) to head (4006e98).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8759   +/-   ##
========================================
  Coverage    80.41%   80.41%           
========================================
  Files         1703     1703           
  Lines       188398   188397    -1     
  Branches        73       73           
========================================
  Hits        151498   151498           
+ Misses       36900    36899    -1     

☔ 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.

Unable to resolve correct virtual method

2 participants