Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8277165: jdeps --multi-release --print-module-deps fails if module-in…
…fo.class in different versioned directories

8277166: Data race in jdeps VersionHelper
8277123: jdeps does not report some exceptions correctly

Reviewed-by: jvernee, alanb
  • Loading branch information
Mandy Chung committed Nov 26, 2021
1 parent eb4d886 commit 7e54d06
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 13 deletions.
Expand Up @@ -356,7 +356,8 @@ public ClassFile getClassFile(String name) throws IOException {
protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
try (InputStream is = jarfile.getInputStream(e)) {
ClassFile cf = ClassFile.read(is);
if (jarfile.isMultiRelease()) {
// exclude module-info.class since this jarFile is on classpath
if (jarfile.isMultiRelease() && !cf.getName().equals("module-info")) {
VersionHelper.add(jarfile, e, cf);
}
return cf;
Expand Down Expand Up @@ -437,5 +438,4 @@ public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
private static final String MODULE_INFO = "module-info.class";
}
Expand Up @@ -268,7 +268,14 @@ private Set<Location> waitForTasksCompleted() {
}
return targets;
} catch (InterruptedException|ExecutionException e) {
throw new Error(e);
Throwable cause = e.getCause();
if (cause instanceof RuntimeException x) {
throw x;
} else if (cause instanceof Error x) {
throw x;
} else {
throw new Error(e);
}
}
}

Expand Down
Expand Up @@ -47,7 +47,7 @@ class MultiReleaseException extends RuntimeException {
* The detail message array
*/
public MultiReleaseException(String key, Object... params) {
super();
super(JdepsTask.getMessage(key, params));
this.key = key;
this.params = params;
}
Expand Down
Expand Up @@ -55,15 +55,10 @@ public static void add(JarFile jarfile, JarEntry e, ClassFile cf)
String version = realName.substring(len, n);
assert (Integer.parseInt(version) > 8);
String name = cf.getName().replace('/', '.');
if (nameToVersion.containsKey(name)) {
if (!version.equals(nameToVersion.get(name))) {
throw new MultiReleaseException(
"err.multirelease.version.associated",
name, nameToVersion.get(name), version
);
}
} else {
nameToVersion.put(name, version);
String v = nameToVersion.computeIfAbsent(name, _n -> version);
if (!version.equals(v)) {
throw new MultiReleaseException("err.multirelease.version.associated",
name, nameToVersion.get(name), version);
}
} else {
throw new MultiReleaseException("err.multirelease.jar.malformed",
Expand Down
107 changes: 107 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/MultiVersionError.java
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8277165
* @library ../lib
* @build CompilerUtils
* @run testng MultiVersionError
* @summary Tests multiple versions of the same class file
*/

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.spi.ToolProvider;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

public class MultiVersionError {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");

private static final Path MODS_DIR = Paths.get("mods");

private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").orElseThrow();
private static final Set<String> modules = Set.of("m1", "m2");

/**
* Compiles classes used by the test
*/
@BeforeTest
public void compileAll() throws Exception {
CompilerUtils.cleanDir(MODS_DIR);
modules.forEach(mn ->
assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));

// create a modular multi-release m1.jar
Path m1 = MODS_DIR.resolve("m1");
Path m2 = MODS_DIR.resolve("m2");
jar("cf", "m1.jar", "-C", m1.toString(), "p/Test.class",
"--release", "9", "-C", m1.toString(), "module-info.class",
"--release", "11", "-C", m1.toString(), "p/internal/P.class");
jar("cf", "m2.jar", "-C", m2.toString(), "q/Q.class",
"--release", "10", "-C", m2.toString(), "module-info.class");

// package private p/internal/P.class in m1 instead
jar("cf", "m3.jar", "-C", m2.toString(), "q/Q.class",
"--release", "12", "-C", m2.toString(), "module-info.class",
"-C", m1.toString(), "p/internal/P.class");
}

/*
* multiple module-info.class from different versions should be excluded
* from multiple version check.
*/
@Test
public void noMultiVersionClass() {
// skip parsing p.internal.P to workaround JDK-8277681
JdepsRunner jdepsRunner = new JdepsRunner("--print-module-deps", "--multi-release", "10",
"--ignore-missing-deps",
"--module-path", "m1.jar", "m2.jar");
int rc = jdepsRunner.run(true);
assertTrue(rc == 0);
assertTrue(jdepsRunner.outputContains("java.base,m1"));
}

/*
* Detect multiple versions of p.internal.P class
*/
@Test
public void classInMultiVersions() {
JdepsRunner jdepsRunner = new JdepsRunner("--print-module-deps", "--multi-release", "13",
"--module-path", "m1.jar", "m3.jar");
int rc = jdepsRunner.run(true);
assertTrue(rc != 0);
assertTrue(jdepsRunner.outputContains("class p.internal.P already associated with version"));
}

private static void jar(String... options) {
int rc = JAR_TOOL.run(System.out, System.err, options);
assertTrue(rc == 0);
}
}
27 changes: 27 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m1/module-info.java
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

module m1 {
requires java.management;
exports p;
}
27 changes: 27 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m1/p/Test.java
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package p;

public class Test {
}
30 changes: 30 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m1/p/internal/P.java
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package p.internal;

import java.lang.management.*;

class P {
private static RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
}
27 changes: 27 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m2/module-info.java
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

module m2 {
requires m1;
requires java.logging;
}
30 changes: 30 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m2/p/internal/P.java
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package p.internal;

import java.util.logging.Logger;

public class P {
private static final Logger LOGGER = Logger.getLogger("p");
}
32 changes: 32 additions & 0 deletions test/langtools/tools/jdeps/multiVersion/src/m2/q/Q.java
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package q;

public class Q {
static p.Test t = new p.Test();

public Q() {
Object o = new p.internal.P();
}
}

7 comments on commit 7e54d06

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@mlchung
Copy link
Member

Choose a reason for hiding this comment

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

/backport jdk17u

@openjdk
Copy link

@openjdk openjdk bot commented on 7e54d06 Nov 30, 2021

Choose a reason for hiding this comment

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

@mlchung the backport was successfully created on the branch mlchung-backport-7e54d065 in my personal fork of openjdk/jdk17u. To create a pull request with this backport targeting openjdk/jdk17u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 7e54d065 from the openjdk/jdk repository.

The commit being backported was authored by Mandy Chung on 26 Nov 2021 and was reviewed by Jorn Vernee and Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u:

$ git fetch https://github.com/openjdk-bots/jdk17u mlchung-backport-7e54d065:mlchung-backport-7e54d065
$ git checkout mlchung-backport-7e54d065
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u mlchung-backport-7e54d065

@openjdk
Copy link

@openjdk openjdk bot commented on 7e54d06 Jan 28, 2022

Choose a reason for hiding this comment

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

@lucapino-kobalt Only OpenJDK contributors can use the /backport command

@openjdk
Copy link

@openjdk openjdk bot commented on 7e54d06 Feb 25, 2022

Choose a reason for hiding this comment

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

@lucapinouk Only OpenJDK contributors can use the /backport command

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 7e54d06 Mar 15, 2022

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 7e54d06 Mar 15, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-7e54d065 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 7e54d065 from the openjdk/jdk repository.

The commit being backported was authored by Mandy Chung on 26 Nov 2021 and was reviewed by Jorn Vernee and Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-7e54d065:GoeLin-backport-7e54d065
$ git checkout GoeLin-backport-7e54d065
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-7e54d065

Please sign in to comment.