Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotty generates code that cause Exception in thread "main" java.lang.IllegalAccessError during runtime. #13897

Closed
megri opened this issue Nov 7, 2021 · 5 comments · Fixed by #21362
Assignees
Milestone

Comments

@megri
Copy link
Contributor

megri commented Nov 7, 2021

Compiler version

All versions of Dotty seem to be affected.
Java 8 and 11, possibly newer ones as well.

Minimized code

build.sbt

scalaVersion := "3.1.0"
// scalaVersion := "3.0.0"
// scalaVersion := "2.13.6" // 2.13.6 not affected
classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.Flat // recommended by sbt; does not change outcome
libraryDependencies += "com.google.cloud" % "google-cloud-datastore" % "2.1.0"

Program

package bugreport

import com.google.cloud.datastore.Query

object Main {
  def main(args: Array[String]): Unit = {
    val query = Query.newEntityQueryBuilder.setKind("Bug").setLimit(10) // One "setX"-call seems ok.
    println(s"query: $query")
  }
}

Output

Compilation succeeds. Fails at runtime with

sbt:bugreport-illegalaccesserror> run
[info] running bugreport.Main
[error] (run-main-7) java.lang.IllegalAccessError: Error running public static void bugreport.Main.main(java.lang.String[]).
[error] java.lang.IllegalAccessError: failed to access class com.google.cloud.datastore.StructuredQuery$BuilderImpl from class bugreport.Main$ (com.google.cloud.datastore.StructuredQuery$BuilderImpl and bugreport.Main$ are in unnamed module of loader sbt.internal.FlatLoader @65e7072e)
[error] If using a layered classloader, this can occur if jvm package private classes are accessed across layers. This can be fixed by changing to the Flat or ScalaInstance class loader layering strategies.
[error] java.lang.IllegalAccessError: Error running public static void bugreport.Main.main(java.lang.String[]).
[error] java.lang.IllegalAccessError: failed to access class com.google.cloud.datastore.StructuredQuery$BuilderImpl from class bugreport.Main$ (com.google.cloud.datastore.StructuredQuery$BuilderImpl and bugreport.Main$ are in unnamed module of loader sbt.internal.FlatLoader @65e7072e)
[error] If using a layered classloader, this can occur if jvm package private classes are accessed across layers. This can be fixed by changing to the Flat or ScalaInstance class loader layering strategies.
[error] stack trace is suppressed; run last Compile / bgRun for the full output
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 1 s, completed 7 Nov 2021, 11:07:28

Expectation

Should work as in 2.13.6

@megri megri added the itype:bug label Nov 7, 2021
@megri
Copy link
Contributor Author

megri commented Nov 7, 2021

This is the decompiled bytecode

2.13.6

Compiled from "Main.scala"
public final class bugreport.Main$ {
  public static final bugreport.Main$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class bugreport/Main$
       3: dup
       4: invokespecial #22                 // Method "<init>":()V
       7: putstatic     #24                 // Field MODULE$:Lbugreport/Main$;
      10: return

  public void main(java.lang.String[]);
    Code:
       0: invokestatic  #33                 // Method com/google/cloud/datastore/Query.newEntityQueryBuilder:()Lcom/google/cloud/datastore/EntityQuery$Builder;
       3: ldc           #35                 // String Bug
       5: invokevirtual #39                 // Method com/google/cloud/datastore/EntityQuery$Builder.setKind:(Ljava/lang/String;)Lcom/google/cloud/datastore/StructuredQuery$BuilderImpl;
       8: checkcast     #7                  // class com/google/cloud/datastore/EntityQuery$Builder
      11: getstatic     #44                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      14: bipush        10
      16: invokevirtual #48                 // Method scala/Predef$.int2Integer:(I)Ljava/lang/Integer;
      19: invokevirtual #52                 // Method com/google/cloud/datastore/EntityQuery$Builder.setLimit:(Ljava/lang/Integer;)Lcom/google/cloud/datastore/StructuredQuery$BuilderImpl;
      22: checkcast     #7                  // class com/google/cloud/datastore/EntityQuery$Builder
      25: astore_2
      26: getstatic     #44                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      29: new           #54                 // class java/lang/StringBuilder
      32: dup
      33: ldc           #55                 // int 7
      35: invokespecial #58                 // Method java/lang/StringBuilder."<init>":(I)V
      38: ldc           #60                 // String query:
      40: invokevirtual #64                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      43: aload_2
      44: invokevirtual #67                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      47: invokevirtual #71                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      50: invokevirtual #75                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      53: return
}

3.1.0

Compiled from "Main.scala"
public final class bugreport.Main$ implements java.io.Serializable {
  public static final bugreport.Main$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class bugreport/Main$
       3: dup
       4: invokespecial #26                 // Method "<init>":()V
       7: putstatic     #28                 // Field MODULE$:Lbugreport/Main$;
      10: return

  public void main(java.lang.String[]);
    Code:
       0: invokestatic  #44                 // Method com/google/cloud/datastore/Query.newEntityQueryBuilder:()Lcom/google/cloud/datastore/EntityQuery$Builder;
       3: ldc           #46                 // String Bug
       5: invokevirtual #50                 // Method com/google/cloud/datastore/EntityQuery$Builder.setKind:(Ljava/lang/String;)Lcom/google/cloud/datastore/StructuredQuery$BuilderImpl;
       8: getstatic     #55                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      11: bipush        10
      13: invokevirtual #59                 // Method scala/Predef$.int2Integer:(I)Ljava/lang/Integer;
      16: invokevirtual #63                 // Method com/google/cloud/datastore/StructuredQuery$BuilderImpl.setLimit:(Ljava/lang/Integer;)Lcom/google/cloud/datastore/StructuredQuery$BuilderImpl;
      19: checkcast     #9                  // class com/google/cloud/datastore/EntityQuery$Builder
      22: astore_2
      23: getstatic     #55                 // Field scala/Predef$.MODULE$:Lscala/Predef$;
      26: new           #65                 // class java/lang/StringBuilder
      29: dup
      30: ldc           #66                 // int 7
      32: invokespecial #69                 // Method java/lang/StringBuilder."<init>":(I)V
      35: ldc           #71                 // String query:
      37: invokevirtual #75                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      40: aload_2
      41: invokevirtual #78                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      44: invokevirtual #82                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      47: invokevirtual #86                 // Method scala/Predef$.println:(Ljava/lang/Object;)V
      50: return
}

So 2.13.6 does an extra 8: checkcast #7 // class com/google/cloud/datastore/EntityQuery$Builder. I have no idea if this is what's making the difference.

@nicolasstucki nicolasstucki added the stat:needs minimization Needs a self contained minimization label Nov 8, 2021
@nicolasstucki
Copy link
Contributor

We need a version that does not have the dependency

@megri
Copy link
Contributor Author

megri commented Nov 10, 2021

We need a version that does not have the dependency

I managed to minimize it.

Bar.java

package com.a.b;

public class Bar<V> {
    abstract static class BuilderImpl<V, B extends BuilderImpl<V, B>> {
        public B call() {
            return (B) this;
        }
    }
}

Baz.java

package com.a.b;

public final class Baz extends Bar<String> {
    public static final class Builder extends Bar.BuilderImpl<String, Builder> {}
}

Main.scala

package bugreport

import com.a.b.Baz

object Main {
  def main(args: Array[String]): Unit = {
    val b = new Baz.Builder()
    b.call() // works
    val x = b.call(); val y = x.call() // also works
    b.call().call() // works on 2.13, fails on Scala 3
  }
}

edit: shrunk it further

@som-snytt
Copy link
Contributor

Possibly duplicates #13841 like two ships passing in the night.

@eed3si9n
Copy link
Member

Closing this as duplicate.

@eed3si9n eed3si9n closed this as not planned Won't fix, can't repro, duplicate, stale Aug 10, 2024
eed3si9n added a commit that referenced this issue Sep 12, 2024
This is a backport of Scala 2.x scala/scala#6023 by @lrytz 

Fixes #13841
Fixes #13897

## Problem
When compiling `builder.call1().call2()` where both are Java-defined
package-protected class through a public subsclass, Scala 3 does not
properly cast the receiver to the public class, and results in an
IllegalAccessError.

## Solution
This backports the casting fix from the Scala 2.x compiler.
@WojciechMazur WojciechMazur added this to the 3.6.0 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants