Repository navigation
Java bytecode front-end: do not crash upon invalid class names - #8764
tautschnig wants to merge 1 commit into
Conversation
5e633f1 to
67824f0
Compare
There was a problem hiding this comment.
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_stringand add additional checks around recursive parsing results. - Add handling for
unsupported_java_class_signature_exceptiontinjava_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.
| CHECK_RETURN(array_type.has_value()); | ||
| return to_struct_tag_type(to_java_reference_type(*array_type).base_type()); |
| 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()); |
| 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()); |
| auto type_opt = java_type_from_string(f.descriptor); | ||
| CHECK_RETURN(type_opt.has_value()); | ||
| field_type = *type_opt; |
| 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. |
| CHECK_RETURN(type_from_string.has_value()); | ||
| get_dependencies_from_generic_parameters_rec(*type_from_string, refs); |
| 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); |
| CHECK_RETURN(type_opt.has_value()); | ||
| return std::move(*type_opt); |
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
`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>
67824f0 to
e58c439
Compare
Do not make unchecked use of the result of
java_type_from_string.Fixes: #728
Fixes: #849