RFR: 8302514: Misleading error generated when empty class file encountered [v2]
Julian Waters
jwaters at openjdk.org
Thu Feb 16 14:51:42 UTC 2023
On Wed, 15 Feb 2023 16:23:54 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:
>> When javac encounters an empty class file, you get the following strange error referring to `java.lang.AutoCloseable`:
>>
>> Test.java:3: error: cannot access Empty
>> new Empty();
>> ^
>> bad class file: ./Empty.class
>> class file contains wrong class: java.lang.AutoCloseable
>> Please remove or make sure it appears in the correct subdirectory of the classpath.
>> 1 error
>>
>> This is a side effect of javac blindly reading past the end of the data in a data buffer. Further investigation revealed three distinct bugs in this area of the code:
>>
>> 1. There are several unchecked corner cases in `ByteBuffer` and `ArrayUtils` that could potentially lead to `ArrayIndexOutOfBoundsException`s and/or infinite loops. Instead we should "fail fast" on any attempt to read or allocate data out of bounds.
>> 1. The method `ByteBuffer.appendStream()` assumes that the result from `InputStream.available()` is always accurate, but this is not guaranteed. As a result, class files could (in theory) be read incorrectly by the compiler, especially when they come from non-standard sources.
>> 1. There is no systematic protection against truncated class files. Instead, depending on where exactly it is truncated, a truncated class file generates one of several possible different errors:
>> * `index 25926 is not within pool size 13`
>> * `bad constant pool tag: 0 at 28317`
>> * `unexpected constant pool tag: 10 at 11`
>> * `class file is invalid for class Empty`
>> * `class file contains wrong class: java.lang.AutoCloseable`
>>
>> This patch fixes issues 1 and 2, and addresses 3 by having `ByteBuffer` throw a new unchecked `UnderflowException` if ever data is read past the end of the buffer. Then `PoolReader`, `ClassReader`, and `ModuleNameReader` are updated to catch this exception and convert it into a bad class file error. As a result, truncated class files generate a consistent `class file truncated at offset X` error regardless of where they are truncated.
>
> Archie L. Cobbs has updated the pull request incrementally with one additional commit since the last revision:
>
> Fix another ArrayUtils corner case created by previous fix.
Marked as reviewed by jwaters (Committer).
Sponsoring out of goodwill
-------------
PR: https://git.openjdk.org/jdk/pull/12574
More information about the compiler-dev
mailing list