Skip to content

JBMC: test virtual dispatch of equals through Object - #8759

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

tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:fix-930-virtual

Conversation

@tautschnig

@tautschnig tautschnig commented Nov 30, 2025 •

Copy link
Copy Markdown
Collaborator

Issue #930 reported that calling equals through a parameter of type Object did not resolve to String.equals. The example from the issue passes on develop when the Java models library is on the class path (without it, JBMC has no model of String, and both assertions fail alike). This PR adds that example as a regression test.

An earlier version of this PR changed the skip condition in get_child_functions_rec (remove_virtual_functions.cpp). That change had no observable effect: the tests, the example from #930, and a differential fuzzing run over 286 generated Java dispatch programs gave the same results with and without it. The current condition keeps the behaviour that the code had before 6b2b950.

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
@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

Copilot AI left a comment

Copy link
Copy Markdown

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

codecov Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.34%. Comparing base (3e1f386) to head (93a847d).
⚠️ Report is 36 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8759      +/-   ##
===========================================
+ Coverage    81.29%   81.34%   +0.04%     
===========================================
  Files         1725     1727       +2     
  Lines       191680   191715      +35     
  Branches        73       73              
===========================================
+ Hits        155835   155948     +113     
+ Misses       35845    35767      -78     

☔ View full report in Codecov by Harness.
📢 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.

Issue diffblue#930 reported that, in

  private static boolean myEquals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
  }

the call of equals did not resolve to String.equals, so that
assert(myEquals(a, b)) failed for equal Strings a and b, while the same
comparison written in main succeeded. Both assertions hold on develop
when the Java models library is on the class path; without it, JBMC
has no model of String and both assertions fail alike. Add the example
from the issue as a regression test.

Fixes: diffblue#930

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig changed the title Fix virtual method resolution for Java Object parameters JBMC: test virtual dispatch of equals through Object Dec 1, 2026

This branch has not been deployed

No deployments
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