Skip to content

Java bytecode front-end: do not crash upon invalid class names - #8764

Open
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:fix-728-jbmc-core-dump
Open

tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:fix-728-jbmc-core-dump

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

Do not make unchecked use of the result of java_type_from_string.

Fixes: #728
Fixes: #849

  • 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-728-jbmc-core-dump branch from 5e633f1 to 67824f0 Compare March 18, 2026 22:36
@tautschnig
tautschnig marked this pull request as ready for review March 18, 2026 22:36
Copilot AI review requested due to automatic review settings March 18, 2026 22:36

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

This PR aims to prevent JBMC’s Java bytecode front-end from crashing on malformed/invalid class/type names by avoiding unchecked dereferences of java_type_from_string results and by adding an explicit error-handling path in the parser.

Changes:

  • Add exception throwing for some malformed signatures in java_type_from_string and add additional checks around recursive parsing results.
  • Add handling for unsupported_java_class_signature_exceptiont in java_bytecode_parsert::parse().
  • Replace several *java_type_from_string(...) dereferences with intermediate optionals + validation in parser/converter code.

Reviewed changes

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

File Description
jbmc/src/java_bytecode/java_types.cpp Adds additional validation/exception behavior when parsing descriptors/signatures and refactors some call sites to avoid direct dereference.
jbmc/src/java_bytecode/java_entry_point.cpp Adds a return-value check when constructing the String[] type used to recognize main.
jbmc/src/java_bytecode/java_bytecode_parser.cpp Replaces direct dereferences of parsed types with optionals + checks, and adds a catch for unsupported signature exceptions.
jbmc/src/java_bytecode/java_bytecode_convert_class.cpp Avoids direct dereference when converting field descriptors by introducing an optional + check.

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

Comment thread jbmc/src/java_bytecode/java_types.cpp Outdated
Comment on lines +822 to +823
CHECK_RETURN(array_type.has_value());
return to_struct_tag_type(to_java_reference_type(*array_type).base_type());
Comment on lines 581 to 584
auto type_opt = java_type_from_string(method.descriptor);
CHECK_RETURN(type_opt);
get_class_refs_rec(*type_opt);
}
*java_type_from_string(id2string(pool_entry(c.ref2).s)));
{
auto type_opt = java_type_from_string(id2string(pool_entry(c.ref2).s));
CHECK_RETURN(type_opt.has_value());
Comment on lines 557 to 560
auto type_opt = java_type_from_string(field.descriptor);
CHECK_RETURN(type_opt);
get_class_refs_rec(*type_opt);
}
const irep_idt &value_id = symbol_expr->get_identifier();
get_class_refs_rec(*java_type_from_string(id2string(value_id)));
auto type_opt = java_type_from_string(id2string(value_id));
CHECK_RETURN(type_opt.has_value());
Comment on lines +681 to +683
auto type_opt = java_type_from_string(f.descriptor);
CHECK_RETURN(type_opt.has_value());
field_type = *type_opt;
Comment thread jbmc/src/java_bytecode/java_types.cpp Outdated
Comment on lines 660 to 663
auto subtype = java_type_from_string(
src.substr(1, std::string::npos), class_name_prefix);
CHECK_RETURN(subtype.has_value());
if(subtype_letter=='L' || // [L denotes a reference array of some sort.
Comment thread jbmc/src/java_bytecode/java_types.cpp Outdated
Comment on lines 1028 to 1029
CHECK_RETURN(type_from_string.has_value());
get_dependencies_from_generic_parameters_rec(*type_from_string, refs);
Comment thread jbmc/src/java_bytecode/java_types.cpp Outdated
Comment on lines 1067 to 1070
const auto base_type = java_type_from_string(base_ref, class_name_prefix);
CHECK_RETURN(base_type.has_value());
PRECONDITION(is_java_generic_type(*base_type));
const java_generic_typet &gen_base_type = to_java_generic_type(*base_type);
Comment on lines +89 to +90
CHECK_RETURN(type_opt.has_value());
return std::move(*type_opt);
@codecov

codecov Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.34%. Comparing base (3e1f386) to head (e58c439).
⚠️ Report is 36 commits behind head on develop.

Files with missing lines Patch % Lines
jbmc/src/java_bytecode/java_types.cpp 88.67% 6 Missing ⚠️
...src/java_bytecode/java_bytecode_convert_method.cpp 80.00% 2 Missing ⚠️
jbmc/src/java_bytecode/java_bytecode_parser.cpp 96.96% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8764      +/-   ##
===========================================
+ Coverage    81.29%   81.34%   +0.05%     
===========================================
  Files         1725     1727       +2     
  Lines       191680   191764      +84     
  Branches        73       73              
===========================================
+ Hits        155835   156000     +165     
+ Misses       35845    35764      -81     

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

`java_type_from_string` reported some malformed type descriptors by
returning an empty optional, but no caller handled that case: all of
them dereferenced the result or asserted that it was set. A class file
declaring the class name "[" (diffblue#728) thus made JBMC dereference an empty
optional and crash. Other malformed descriptors, such as "[V", "(L)V"
or a type variable without a generic context, failed invariants or,
for "(TX)V", looped until memory was exhausted.

`java_type_from_string` now throws
`unsupported_java_class_signature_exceptiont` for every malformed
descriptor or signature, and returns a `typet` rather than an optional.
The same holds for the helpers it uses to split parameter and type
argument lists. This function is the parser for the descriptor and
signature grammar, so it is the place that can tell whether a string is
well formed; checking earlier would require a second parser for the same
grammar. Signatures must not make JBMC reject a class: the JVM does not
check Signature attributes (JVMS 4.7.9.1), and callers that parse them
already catch this exception to fall back to the descriptor.

Whether a well-formed descriptor has the right kind depends on where
the class file uses it, so the class file parser checks that: field,
local-variable, Fieldref and annotation descriptors must denote field
types, and method, Methodref, InterfaceMethodref and InvokeDynamic
descriptors must denote method types. It checks each descriptor when
reading it, including those of fields and methods that also have a
Signature attribute, and reports violations as parse errors. Later
stages thus only see descriptors that the parser has accepted, and keep
their invariants. A method signature that does not denote a method type
makes JBMC fall back to the descriptor, like any other signature that
cannot be used.

The new regression tests use class files with an invalid class name,
with a field descriptor in place of a method descriptor, and with a
field descriptor in a Methodref; the JVM rejects all of them with a
`ClassFormatError`. The unit test covers a list of malformed
descriptors, and a random-mutation fuzzer run over 4 million
descriptors and signatures found no further invariant failures.

Fixes: diffblue#728
Fixes: diffblue#849

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig
tautschnig force-pushed the fix-728-jbmc-core-dump branch from 67824f0 to e58c439 Compare December 1, 2026 18:47
@tautschnig
tautschnig requested a review from kroening as a code owner December 1, 2026 18:47

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.

Invalid Java byte-code errors not handled Core dump with attached class file (from openjdk regression suite)

2 participants