Bug 576093 - Switch on enum causes VerifyError: Bad type on operand stack
Summary: Switch on enum causes VerifyError: Bad type on operand stack
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.22   Edit
Hardware: PC All
: P3 blocker with 1 vote (vote)
Target Milestone: 4.22 M1   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords: regression
: Switch, VerifyError 576404 576534 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-09-20 03:40 EDT by Andrey Loskutov CLA
Modified: 2021-11-11 06:11 EST (History)
13 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Loskutov CLA 2021-09-20 03:40:11 EDT
The class below compiles fine, but at runtime it can't be executed and JVM throws VerifyError.

Error: Unable to initialize main class verifyerror.X
Caused by: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    verifyerror/X.crashNow()V @36: invokevirtual
  Reason:
    Type 'java/lang/Object' (current frame, stack[1]) is not assignable to 'verifyerror/Z'
  Current Frame:
    bci: @36
    flags: { }
    locals: { 'java/util/Map$Entry', 'java/util/Iterator' }
    stack: { '[I', 'java/lang/Object' }
  Bytecode:
    0000000: b200 13b9 0021 0100 b900 2701 004c a700
    0000010: 1b2b b900 2d01 00c0 0033 4bb8 0035 2ab9
    0000020: 0038 0100 b600 3b2e 572b b900 4101 009a
    0000030: ffe2 b200 4512 4bb6 004d b1            
  Stackmap Table:
    full_frame(@17,{Top,Object[#46]},{})
    same_frame(@41)

Changing the code to use temporary local variable fixes the issue.

##########

package verifyerror;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


public class X {

	private static final Map<Z, Object> map = new HashMap<>();

	public static void main(String[] args) {
		crashNow();
	}

	static void crashNow() {
		for (Entry<Z, Object> entry : map.entrySet()) {
			switch (entry.getKey()) {
			case A:
					continue;
			default:
				break;
			}
		}
		System.out.println("No crash if you see this");
	}

}

enum Z {
	A, B
}

##########

"Fix":

Z key = entry.getKey();
switch (key) {

This is a regression in 4.22.
Comment 1 Andrey Loskutov CLA 2021-09-20 04:20:14 EDT
I've bisected this crash to commit 258240a7cae9336cb26bd41d3da9533bc39eab64

So it is a regression from bug 575047. 

Gerrit: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184014 
Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=258240a7cae9336cb26bd41d3da9533bc39eab64

Jay: could you please check?
Comment 2 Eclipse Genie CLA 2021-09-21 08:41:48 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/185652
Comment 3 Andrey Loskutov CLA 2021-09-21 08:43:31 EDT
(In reply to Eclipse Genie from comment #2)
> New Gerrit change created:
> https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/185652

Only the regression test. I would be happy if someone could point to the right fix.
Comment 4 Jay Arthanareeswaran CLA 2021-09-22 02:38:58 EDT
(In reply to Andrey Loskutov from comment #1)
> Jay: could you please check?

This is due to a disappointingly bad refactoring that resulted in removal of a useful line of code. Will take this through.
Comment 5 Eclipse Genie CLA 2021-09-22 02:44:58 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/185688
Comment 8 Jay Arthanareeswaran CLA 2021-09-22 06:16:01 EDT
Thanks Andrey for the test and narrowing down to the cause.
Comment 9 Philipp Gruhn CLA 2021-09-22 06:30:53 EDT
*** Bug 576008 has been marked as a duplicate of this bug. ***
Comment 10 Andrey Loskutov CLA 2021-09-23 03:58:59 EDT
Verified with I20210922-1800

Thanks Jay!
Comment 11 Andrey Loskutov CLA 2021-10-05 05:40:50 EDT
*** Bug 576404 has been marked as a duplicate of this bug. ***
Comment 12 Lucas Vander Wal CLA 2021-10-11 08:56:18 EDT
How can I download a copy of eclipse with this fix? I downloaded the 2021-09, package and it still contains this bug.
Comment 13 Lucas Vander Wal CLA 2021-10-11 09:22:40 EDT
(In reply to Lucas Vander Wal from comment #12)
> How can I download a copy of eclipse with this fix? I downloaded the
> 2021-09, package and it still contains this bug.

I was able to download the 2021-12 developer version, and it is fixed in that version.
Comment 14 Andrey Loskutov CLA 2021-10-14 10:00:37 EDT
*** Bug 576534 has been marked as a duplicate of this bug. ***
Comment 15 Jeff Meyers CLA 2021-10-15 08:26:06 EDT
*** Bug 576534 has been marked as a duplicate of this bug. ***
Comment 16 Daniel Barciela CLA 2021-10-15 13:50:28 EDT
Is there a workaround for version 2021-09? Will it be updated?
Comment 17 Jay Arthanareeswaran CLA 2021-10-18 05:44:25 EDT
(In reply to Daniel Barciela from comment #16)
> Is there a workaround for version 2021-09? Will it be updated?

I assume you installed the Java 17 patch on top of 2021-09? If so, we can perhaps update the patch.
Comment 18 Lionel Duriez CLA 2021-10-20 04:40:28 EDT
Facing "VerifyError: Bad type on operand stack" with up-to-date eclipse-jee-2021-09-R.

Will there be a patch for 2021-09-R ?
Comment 19 Eclipse Genie CLA 2021-10-20 05:05:32 EDT
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/186174
Comment 20 Mauro Molinari CLA 2021-10-20 09:22:38 EDT
Just to add that, based on our experience and on this post on StackOverflow:

https://stackoverflow.com/questions/69197046/jdk-17-switch-statement-causes-java-lang-verifyerror-bad-type-on-operand-stack

this problem also happens with switches on Strings, not just on Enums.

We solved by uninstalling the JDT patch for Java 17 from our Eclipse 2021-09 installation.
Comment 22 Jay Arthanareeswaran CLA 2021-10-26 03:27:33 EDT
(In reply to Lionel Duriez from comment #18)
> Facing "VerifyError: Bad type on operand stack" with up-to-date
> eclipse-jee-2021-09-R.
> 
> Will there be a patch for 2021-09-R ?


(In reply to Mauro Molinari from comment #20)
> Just to add that, based on our experience and on this post on StackOverflow:
> 
> https://stackoverflow.com/questions/69197046/jdk-17-switch-statement-causes-java-lang-verifyerror-bad-type-on-operand-stack
> 
> 
> this problem also happens with switches on Strings, not just on Enums.
> 
> We solved by uninstalling the JDT patch for Java 17 from our Eclipse 2021-09
> installation.

A new patch has been released with this fix. Please try out the same from marketplace or the update site.
Comment 23 Philipp Gruhn CLA 2021-10-26 04:13:02 EDT
I can confirm this does also fix my filed bug 576008. I have also updated the stackoverflow question with an answer.

(In reply to Jay Arthanareeswaran from comment #22)
> (In reply to Lionel Duriez from comment #18)
> > Facing "VerifyError: Bad type on operand stack" with up-to-date
> > eclipse-jee-2021-09-R.
> > 
> > Will there be a patch for 2021-09-R ?
> 
> 
> (In reply to Mauro Molinari from comment #20)
> > Just to add that, based on our experience and on this post on StackOverflow:
> > 
> > https://stackoverflow.com/questions/69197046/jdk-17-switch-statement-causes-java-lang-verifyerror-bad-type-on-operand-stack
> > 
> > 
> > this problem also happens with switches on Strings, not just on Enums.
> > 
> > We solved by uninstalling the JDT patch for Java 17 from our Eclipse 2021-09
> > installation.
> 
> A new patch has been released with this fix. Please try out the same from
> marketplace or the update site.
Comment 24 Lionel Duriez CLA 2021-11-04 11:11:27 EDT
Just updated eclipse-jee-2021-09-R (Help > Check for Updates), proposed update is: 
Eclipse JDT (Java Development Tools) Patch with Java 17 support for 2021-09 development stream	1.2.200.v20210914-1442_BETA_JAVA17

Bug is not fixed in the above Eclipse JDT Patch.

Do we need to change Eclipse configuration in order to find the fix ?
Comment 25 David Berkman CLA 2021-11-11 04:56:31 EST
I'm hitting this bug through code compiled with AJC from ascpectj 1.9.8.RC1
Comment 26 Jay Arthanareeswaran CLA 2021-11-11 06:11:29 EST
(In reply to Lionel Duriez from comment #24)
> Just updated eclipse-jee-2021-09-R (Help > Check for Updates), proposed
> update is: 
> Eclipse JDT (Java Development Tools) Patch with Java 17 support for 2021-09
> development stream	1.2.200.v20210914-1442_BETA_JAVA17
> 
> Bug is not fixed in the above Eclipse JDT Patch.
> 
> Do we need to change Eclipse configuration in order to find the fix ?

I don't know why the check for updates is bringing the older version. You can update via the "Install new Software" option and using one of the following update sites:

https://download.eclipse.org/eclipse/updates/4.21-P-builds/

(or) 

https://download.eclipse.org/eclipse/updates/4.21-P-builds/P20211026-0140/