RFR: 8302514: Misleading error generated when empty class file encountered
Archie L. Cobbs
duke at openjdk.org
Wed Feb 15 16:07:47 UTC 2023
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.
-------------
Commit messages:
- Fix misleading error when empty an class file is encountered.
- Fix bogus reliance on InputStream.available() in ByteBuffer.appendStream().
- Tighten up handling of corner cases in ByteBuffer and ArrayUtils.
Changes: https://git.openjdk.org/jdk/pull/12574/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=12574&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8302514
Stats: 353 lines in 10 files changed: 308 ins; 0 del; 45 mod
Patch: https://git.openjdk.org/jdk/pull/12574.diff
Fetch: git fetch https://git.openjdk.org/jdk pull/12574/head:pull/12574
PR: https://git.openjdk.org/jdk/pull/12574
More information about the compiler-dev
mailing list