Skip to content

Runtime initialize non-constant static finals - #474

Merged
Godin merged 2 commits into
jacoco:masterfrom
bjkail:fix-non-constant-static-finals
Dec 14, 2016
Merged

Godin merged 2 commits into
jacoco:masterfrom
bjkail:fix-non-constant-static-finals

Conversation

@bjkail

@bjkail bjkail commented Dec 12, 2016

Copy link
Copy Markdown
Contributor

In my case, ExecutionDataWriter.FORMAT_VERSION is the problematic constant. We workaround by using reflection to access the field so that javac won't inline the value from the version of the library we use to compile.

I reviewed the org.jacoco.doc/target/apidocs/constant-values.html, and JaCoCo.ASM_API_VERSION seemed like the only other constant that was likely to cause similar problems for anyone.

@Godin

Godin commented Dec 12, 2016 •

Copy link
Copy Markdown
Member

@bjkail to confirm my understanding of

calling code must be recompiled when upgrading to a new version of the
library even if the caller would otherwise not require any changes

you are doing something like

if (ExecutionDataWriter.FORMAT_VERSION != 0x1007) {
  throw new RuntimeException();
}

this condition is always false since javac inlines FORMAT_VERSION. And from here comes the need of recompilation?

@bjkail

bjkail commented Dec 12, 2016

Copy link
Copy Markdown
Contributor Author

We have a client library that uses JaCoCo, but clients need to be able to choose their own JaCoCo version. We store execution data on a per-format folder basis, and the client library uses ExecutionDataWriter.FORMAT_VERSION so it can store new execution data files in the right place or so it only attempts to load execution data files using ExecutionDataReader that it will be able to read.

@Godin

Godin commented Dec 12, 2016

Copy link
Copy Markdown
Member

Okay, so very similar to my example: due to inlining client library can claim that uses older format, while runtime dependency changed and it actually uses new format.

@Godin Godin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also could you please fix your git email settings prior to other changes
email
and after the fix amend this commit first:

git commit --amend --reuse-message=HEAD --reset-author
git push --force-with-lease

?


static {
FORMAT_VERSION = 0x1007;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this by myself, but may I ask you to change back executable bit for this file?
GitHub tells me that old mode was 100644 and new one is 100755:
mode


/** ASM API version */
public static final int ASM_API_VERSION = Opcodes.ASM5;
public static final int ASM_API_VERSION;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a comment to prevent accidental change back.


/** File format version, will be incremented for each incompatible change. */
public static final char FORMAT_VERSION = 0x1007;
public static final char FORMAT_VERSION;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And comment here too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comment here and in JaCoCo. I wrote a test that uses TargetLoader to load JaCoCo and a test class that accesses JaCoCo.ASM_API_VERSION, and it uses ASM to rewrite JaCoCo to assign a different value. It seemed like overkill, but I can do it if you prefer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - this seems like an overkill. Just comment looks fine and enough for me.

buffer.write(0xC0);
buffer.write(0xC0);
final char version = ExecutionDataWriter.FORMAT_VERSION - 1;
final char version = (char) (ExecutionDataWriter.FORMAT_VERSION - 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that explicit cast not needed in case of inlined value. Learned something new today 😆

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also learned this today :-). I guess constant fields are treated like literal constants in all regards.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are implicit casts: char -> int -> subtraction , in first case value after subtraction is known at compile time and so one more safe implicit cast int -> char. In second case value not known and so javac says "possible lossy conversion from int to char".

@bjkail
bjkail force-pushed the fix-non-constant-static-finals branch from 2f9a1b6 to b8da5e6 Compare December 12, 2016 22:11
@bjkail

bjkail commented Dec 12, 2016

Copy link
Copy Markdown
Contributor Author

I believe I fixed the issues, thanks. Sorry for the sloppiness with the author/committer email and file modes.

@Godin Godin added the type: bug 🐛 Something isn't working label Dec 13, 2016
@Godin Godin added this to the 0.7.9 milestone Dec 13, 2016
@Godin Godin self-assigned this Dec 13, 2016
@marchof

marchof commented Dec 13, 2016

Copy link
Copy Markdown
Member

To make it more explicit that this is not a constant value I would prefer a static method instead:

public static char ExecutionDataWriter.getFormatVersion()

This breaks the API but people using it currently ended up with broken code anyways.

public static final String RUNTIMEPACKAGE;

/** ASM API version */
public static final int ASM_API_VERSION = Opcodes.ASM5;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only get compiled into JaCoCo classes and not be used externally. For internal classes inlining is perfectly ok because the compiled version of JaCoCo has specifically been tested for this ASM version.

Instead of changing the initialization scheme here I would rather remove it from the API and move it to some internal classes (e.g. InstrSupport).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a separate commit to move ASM_API_VERSION to InstrSupport.

@bjkail
bjkail force-pushed the fix-non-constant-static-finals branch from b8da5e6 to c4eb7e3 Compare December 13, 2016 15:02
@bjkail

bjkail commented Dec 13, 2016

Copy link
Copy Markdown
Contributor Author

@marchof My code uses FORMAT_VERSION via reflection and is not broken, and other code that uses FORMAT_VERSION and uses the same JaCoCo version at compilation and runtime is not broken. Replacing FORMAT_VERSION with getFormatVersion() means more work for me to support multiple JaCoCo versions, so I would rather drop the pull request than do that.

@marchof

marchof commented Dec 13, 2016

Copy link
Copy Markdown
Member

@bjkail Many thanks for moving ASM_API_VERSION!

I see your point about compatibility: Another option would be to leave FORMAT_VERSION as is and deprecate it. In addition we add a new method getFormatVersion(). If I understand your case correctly you need to stick with reflection anyways to support old JaCoCo versions.

But I can also live with your PR as is.

@bjkail

bjkail commented Dec 13, 2016

Copy link
Copy Markdown
Contributor Author

@marchof As soon as a JaCoCo is released with non-constant FORMAT_VERSION, I can updated my code to compile against that version, which means javac won't inline the value, so my .class file will contain a reference to FORMAT_VERSION, which will resolve the value at runtime regardless of whether the field is actually constant, so I can remove the reflection. The test I suggested to Evgeny did basically the same: compile a test class that uses FORMAT_VERSION, then use TargetLoader to load it alongside different versions of ExecutionDataWriter transformed via ASM to have FORMAT_VERSION with value 0 as either constant (simulating current JaCoCo) or non-constant (simulating future JaCoCo).

@Godin
Godin force-pushed the fix-non-constant-static-finals branch from c4eb7e3 to be6ba14 Compare December 14, 2016 00:45
If a constant expression is assigned to a static final variable, javac
will inline the constant value in the caller, which means constants
should only be used for values that will never change.  Otherwise,
calling code must be recompiled when upgrading to a new version of the
library even if the caller would otherwise not require any changes.
@Godin
Godin force-pushed the fix-non-constant-static-finals branch from be6ba14 to ba80c4e Compare December 14, 2016 00:49
@Godin

Godin commented Dec 14, 2016

Copy link
Copy Markdown
Member

@bjkail Not sure I fully understand why "more work". But in any case let's keep this as a field. @marchof other options seems overkill to me. I updated changelog, will merge after pass of builds.

@bjkail A kind of off topic: since you support multiple versions, may I ask out of curiosity - down to which one? Especially that we talk about version of format that last time was changed in 0.7.5 (2015/05/24)?

@bjkail

bjkail commented Dec 14, 2016

Copy link
Copy Markdown
Contributor Author

@Godin By "more work" I meant that I would have to update the current reflection to look for both getFormatVersion() and FORMAT_VERSION (assuming the former replaced the latter). It's not hard or complex work, it's just more :-).

I know 0.7.4 is still heavily used (the most recent for 0x1006), but I've seen older versions in use too. I have not encountered 0.4.x (0x1005) or older. Older versions are used both due to risk aversion and because upgrading creates a "flag day" for components that want to merge/intersect their execution data (if one component moves then they all need to move).

@Godin
Godin merged commit 055e8e4 into jacoco:master Dec 14, 2016
@jacoco jacoco locked and limited conversation to collaborators Jan 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

component: core type: bug 🐛 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants