Re: [Question] Could the max code size check be optional?

"ebruneton" (via asm Mailing List) <[email protected]> Thu, 18 Mar 2021 18:31:36 +0100
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
This is a multi-part message in MIME format...

------------=_1616088699-28311-17
Content-Type: text/plain; charset=UTF-8;
 format=flowed
Content-Transfer-Encoding: 8bit

Hi,

https://asm.ow2.io/javadoc/org/objectweb/asm/tree/analysis/Analyzer.html#analyzeAndComputeMaxs(java.lang.String,org.objectweb.asm.tree.MethodNode) 
can be used to compute the max stack and locals, as well as the stack 
map frames. However, it is much slower than using ClassWriter.

You can also use 
https://asm.ow2.io/javadoc/org/objectweb/asm/commons/CodeSizeEvaluator.html 
to estimate the bytecode size of a method. So you can know if your 
method is too big without using ClassWriter and checking whether you get 
an exception because the code size is too large.

Eric

>> In the sense, that the frame computation and max local/stack is
> coupled to it. Currently, those features can't be used without a
> MethodWriter (please correct me otherwise!).
> 
> I would say they are more a utility added onto the MethodWriter in
> case you don't update the maxLocals and maxStack yourself.
> 
>> Would it be better that those analyses/transformations are available
> and required to be executed before actually calling the MethodWriter?
> In this way, the MethodWriter just writes the legal and valid method
> structure of the class file. In addition, in case you need to compute
> the max stack/local and frames for a method node you can use that
> functionality without calling a MethodWriter.
> 
> I think it would certainly be useful to have it available before
> calling the MethodWriter (though not a requirement, calculating it
> within the MethodWriter would be faster than first passing it through
> a separate calculator).
> 
> It would be duplicating the functionality of the MethodWriter, however
> I don't think that is much of a problem, as the actual stack and local
> size calculation performed in MethodWriter is a very small part of the
> large code existing for frame generation. Removing it from the
> MethodWriter would not have much purpose - the code that performs all
> the calculations for the stack and local size calculations would still
> need to remain to support the frame calculations.
> 
>> In the current design, is it possible to calculate these things
> without calling the MethodWriter? I'm assuming it is not possible.
> 
> It is possible, you would just have to write it yourself - or it could
> perhaps be included in asm-commons. A similar thing already exists
> (AnalyzerAdapter) but I think it only works with bytecode that has
> updated stackframes - I assume if you are not updating the maxLocals
> and maxStack you are also not updating frames.
> 
> x4e
> 
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> 
> On Wednesday, March 17th, 2021 at 13:45, Manuel Carrasco
> <[email protected]> wrote:
> 
>> Thanks for your answer!
>> 
>>> In my opinion the MethodWriter should be designed to fit one
>>> purpose - writing the (legal and valid) method structure of the
>>> classfile.
>> 
>> Yes, I do agree, but I think the MethodWriter is not actually doing
>> that. In the sense, that the frame computation and max local/stack
>> is coupled to it. Currently, those features can't be used without a
>> MethodWriter (please correct me otherwise!). Would it be better that
>> those analyses/transformations are available and required to be
>> executed before actually calling the MethodWriter? In this way, the
>> MethodWriter just writes the legal and valid method structure of the
>> class file. In addition, in case you need to compute the max
>> stack/local and frames for a method node you can use that
>> functionality without calling a MethodWriter.
>> 
>>> Perhaps a better way to do this would be to write a custom method
>>> visitor that is able to calculate this for you?
>> 
>> That can work but the problem that I see is that we would duplicate
>> the functionality that is already in the MethodWriter.
>> 
>> In the current design, is it possible to calculate these things
>> without calling the MethodWriter? I'm assuming it is not possible.
>> 
>> Thanks again!
>> 
>> Cheers,
>> 
>> Manuel.
>> 
>> El mié, 17 mar 2021 a las 12:25, x4e_x4e (<[email protected]>)
>> escribió:
>> 
>> Hi Manuel,
>> 
>> In my opinion the MethodWriter should be designed to fit one purpose
>> - writing the (legal and valid) method structure of the classfile.
>> It would seem strange for it to also be used as a way of calculating
>> the max stack/locals in a method for the user. Especially because
>> the intended output of the methodvisitor would be not just illegal
>> but physically invalid, and no longer conform to the classfile’s
>> structure from the specification (not just the verifier/formatting
>> rules).
>> 
>> Perhaps a better way to do this would be to write a custom method
>> visitor that is able to calculate this for you?
>> 
>> x4e
>> 
>> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
>> 
>> On Wednesday, March 17th, 2021 at 11:09, Manuel Carrasco
>> <[email protected]> wrote:
>> 
>> Hi,
>> 
>> How are you?
>> 
>> I noticed that the MethodWriter currently
>> (184aaa9f0ec4c233872087a3c4d2d96a3c204ac6) throws an exception when
>> the method size exceeds the maximum allowed by the JVM [1]. The
>> intent is clear to me, the library wants to emit Java bytecode that
>> is legal in terms of the specification. My question is, could we
>> make this check optional and true by default?
>> 
>> In case that you want to perform an optimization to shrink your
>> code, you may require to have the max stack and local as well as the
>> stack map frames available. So, you execute the ClassWriter to
>> compute them, but it will crash as soon the MethodWriter verifies
>> the maximum code size.
>> 
>> The current implementation is limiting user optimizations on their
>> code in case it has already exceeded the maximum limit. At least, it
>> is not allowing them to compute the max stack and local as well as
>> the map stack frames.
>> 
>> I tested commenting the check and emitting the code. It didn't
>> crash, and I could optimize it, so it can be executed by the JVM.
>> 
>> Considering all this, I'd like to know if you consider reasonable to
>> make the max code size check optional. Perhaps, I'm missing some
>> other technical reason that could make this impossible.
>> 
>> Thanks in advance!
>> 
>> Cheers,
>> 
>> Manuel.
> 
> 
> 
> Links:
> ------
> [1]
> https://gitlab.ow2.org/asm/asm/-/blob/master/asm/src/main/java/org/objectweb/asm/MethodWriter.java#L2086

------------=_1616088699-28311-17
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws

------------=_1616088699-28311-17--