Skip to content

Java bytecode front-end: fix main method detection#8763

Open
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-759-ambiguous-main
Open

Java bytecode front-end: fix main method detection#8763
tautschnig wants to merge 1 commit intodiffblue:developfrom
tautschnig:fix-759-ambiguous-main

Conversation

@tautschnig
Copy link
Collaborator

When parsing Java classes with just a class name (without method signature), JBMC was incorrectly using resolve_friendly_method_name which could report 'main method is ambiguous' errors. This fix checks if config.main contains a method signature (has a colon) and handles simple class names separately by looking for the standard main method signature directly.

This aligns the Java front-end with Java's specification, where only public static void main(String[] args) is recognized as a valid program entry point.

Fixes: #759

  • 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
When a Java class has multiple methods named 'main' with different
signatures, resolve_friendly_method_name reports ambiguity. Fix this by
first trying resolve_friendly_method_name, and when it fails for a name
without a colon (i.e., potentially a class name rather than a method
specification), fall back to looking for the standard
main(String[]) entry point directly.

This preserves the existing --function ClassName.methodName behavior
while fixing the case where config.main is just a class name.

Fixes: diffblue#759

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig force-pushed the fix-759-ambiguous-main branch from 6afa932 to 073532d Compare March 18, 2026 23:03
@tautschnig tautschnig marked this pull request as ready for review March 18, 2026 23:03
Copilot AI review requested due to automatic review settings March 18, 2026 23:03
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

Updates JBMC’s Java entry-point selection to avoid spurious “ambiguous main” conversion errors when the user provides a class name (rather than a fully-qualified method signature), aligning entry-point selection with the Java public static void main(String[] args) convention.

Changes:

  • Adjust get_main_symbol to return a resolved symbol immediately when resolve_friendly_method_name succeeds, and otherwise fall back to searching for the standard Java main signature when the input looks like a class name.
  • Add a regression test based on issue #759 with multiple main overloads to ensure JBMC does not fail conversion due to ambiguity.

Reviewed changes

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

File Description
jbmc/src/java_bytecode/java_entry_point.cpp Changes main symbol resolution logic; adds class-name fallback to locate the standard Java main signature.
jbmc/regression/jbmc/main-method-ambiguity/Test.java New Java source exhibiting multiple main overloads.
jbmc/regression/jbmc/main-method-ambiguity/Test.class Precompiled class file for the new regression test.
jbmc/regression/jbmc/main-method-ambiguity/test.desc New regression expectations for the ambiguity scenario.

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

Comment on lines +573 to 580
// resolve_friendly_method_name failed; if config.main has no colon
// it may be a class name rather than a method name, so fall back to
// looking for the standard main method in that class.
if(config.main.value().find(':') != std::string::npos)
{
message.error() << "main symbol resolution failed: " << error_message
<< messaget::eom;
return main_function_resultt::Error;
Comment on lines +573 to 595
// resolve_friendly_method_name failed; if config.main has no colon
// it may be a class name rather than a method name, so fall back to
// looking for the standard main method in that class.
if(config.main.value().find(':') != std::string::npos)
{
message.error() << "main symbol resolution failed: " << error_message
<< messaget::eom;
return main_function_resultt::Error;
}

const symbolt *symbol = symbol_table.lookup(main_symbol_id);
INVARIANT(
symbol != nullptr,
"resolve_friendly_method_name should return a symbol-table identifier");
std::string class_name = config.main.value();
if(has_suffix(class_name, ".class"))
class_name.resize(class_name.size() - 6);

std::string entry_method = "java::" + class_name + "." + JAVA_MAIN_METHOD;
const symbolt *symbol = symbol_table.lookup(entry_method);

if(symbol && is_java_main(*symbol))
return *symbol;

return *symbol; // Return found function
// allow this situation to output symbol table, goto functions, etc
return main_function_resultt::NotFound;
}
CORE
Test.class

^EXIT=(0|10)$
@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 (1690ff0) to head (073532d).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8763      +/-   ##
===========================================
- Coverage    80.41%   80.41%   -0.01%     
===========================================
  Files         1703     1703              
  Lines       188398   188407       +9     
  Branches        73       73              
===========================================
- Hits        151509   151505       -4     
- Misses       36889    36902      +13     

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

Conversion Error with PKCS11Test.java from openjdk regression suite

2 participants