Bug 43367 - MethodGen.setMaxLocals() calculates too small max locals, which results in a ClassFormatError when loading the modified class
Summary: MethodGen.setMaxLocals() calculates too small max locals, which results in a ...
Status: NEW
Alias: None
Product: BCEL - Now in Jira
Classification: Unclassified
Component: Main (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Target Milestone: ---
Assignee: issues@commons.apache.org
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-09-12 08:21 UTC by Erich Hochmuth
Modified: 2011-03-15 02:08 UTC (History)
1 user (show)



Attachments
Example how to reproduce the issue. (1.23 KB, text/x-java)
2007-09-13 03:38 UTC, Erich Hochmuth
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Erich Hochmuth 2007-09-12 08:21:06 UTC
Revision 574984 (trunk)

When a local variable is added to a method, the max_locals are incremented in
addLocalVariable (set to slot + size of type).
However, when no other transformations are made, and setMaxLocals() is called,
the bytecode is searched for the max variable index, and max_locals set to that
index + size of that entry. The old max_locals which was set by adding the field
is overwritten by this number.

If the LocalVariableTable for that method exists, the Java VM won't load the
class, because the max_locals determined by bcel is smaller than the max locals
determined from the localvariabletable => ClassFormatException.
Comment 1 Erich Hochmuth 2007-09-13 03:38:59 UTC
Created attachment 20810 [details]
Example how to reproduce the issue.

Start the Reproducer with java -cp bcel.jar:. Reproducer - the example
instrument the constructor of it's own class, adds a field, and tries to load
the modified class.
Comment 2 Dave Brosius 2011-03-15 01:52:26 UTC
It's a bug certainly, but because local variable is added to method without actually adding an statements in the method that use the variable, and the setMaxLocals only looks at opcodes. It should probably also look at the localvariable table if it exists, and use that.
Comment 3 Dave Brosius 2011-03-15 02:08:57 UTC
Actually javac doesn't add entries to the local variable table that you don't use, so perhaps the real fix is to strip unused local variables from the local variable table.