8307168: Inconsistent validation and handling of --system flag arguments

Reviewed-by: jjg
This commit is contained in:
Christian Stein 2023-10-27 08:37:19 +00:00
parent 5b5fd3694a
commit 957703b1f9
2 changed files with 44 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, 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
@ -1906,9 +1906,12 @@ public class Locations {
}
private void update(Path p) {
if (!isCurrentPlatform(p) && !Files.exists(p.resolve("lib").resolve("jrt-fs.jar")) &&
!Files.exists(systemJavaHome.resolve("modules")))
if (!isCurrentPlatform(p)) {
var noJavaRuntimeFS = Files.notExists(resolveInJavaHomeLib(p, "jrt-fs.jar"));
var noModulesFile = Files.notExists(resolveInJavaHomeLib(p, "modules"));
if (noJavaRuntimeFS || noModulesFile)
throw new IllegalArgumentException(p.toString());
}
systemJavaHome = p;
modules = null;
}
@ -1921,6 +1924,10 @@ public class Locations {
}
}
private static Path resolveInJavaHomeLib(Path javaHomePath, String name) {
return javaHomePath.resolve("lib").resolve(name);
}
@Override
Location getLocationForModule(String name) throws IOException {
initSystemModules();
@ -1967,10 +1974,10 @@ public class Locations {
Collections.singletonMap("java.home", systemJavaHome.toString());
jrtfs = FileSystems.newFileSystem(jrtURI, attrMap);
} catch (ProviderNotFoundException ex) {
URL javaHomeURL = systemJavaHome.resolve("jrt-fs.jar").toUri().toURL();
URL jfsJar = resolveInJavaHomeLib(systemJavaHome, "jrt-fs.jar").toUri().toURL();
ClassLoader currentLoader = Locations.class.getClassLoader();
URLClassLoader fsLoader =
new URLClassLoader(new URL[] {javaHomeURL}, currentLoader);
new URLClassLoader(new URL[] {jfsJar}, currentLoader);
jrtfs = FileSystems.newFileSystem(jrtURI, Collections.emptyMap(), fsLoader);
@ -1982,7 +1989,7 @@ public class Locations {
modules = jrtfs.getPath("/modules");
} catch (FileSystemNotFoundException | ProviderNotFoundException e) {
modules = systemJavaHome.resolve("modules");
modules = resolveInJavaHomeLib(systemJavaHome, "modules");
if (!Files.exists(modules))
throw new IOException("can't find system classes", e);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, 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
@ -23,7 +23,7 @@
/**
* @test
* @bug 8196433
* @bug 8196433 8307168
* @summary use the new error diagnostic approach at javac.Main
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -255,6 +255,34 @@ public class OptionSmokeTest extends TestRunner {
String.format("--release %s --upgrade-module-path any", Source.DEFAULT.name));
}
@Test
public void consistentSystemOptionHandlingWithAnEmptyDirectory(Path base) throws Exception {
tb.createDirectories(base);
doTestNoSource(base, "error: illegal argument for --system: %s".formatted(base), String.format("--system %s", base));
}
@Test
public void consistentSystemOptionHandlingWithLibJrtFsJar(Path base) throws Exception {
tb.createDirectories(base);
tb.writeFile(base.resolve("lib").resolve("jrt-fs.jar"), "this is not a JAR file");
doTestNoSource(base, "error: illegal argument for --system: %s".formatted(base), String.format("--system %s", base));
}
@Test
public void consistentSystemOptionHandlingWithLibModules(Path base) throws Exception {
tb.createDirectories(base);
tb.writeFile(base.resolve("lib").resolve("modules"), "this is not a modules file");
doTestNoSource(base, "error: illegal argument for --system: %s".formatted(base), String.format("--system %s", base));
}
@Test
public void consistentSystemOptionHandlingWithAlmostValidLibEntries(Path base) throws Exception {
tb.createDirectories(base);
tb.writeFile(base.resolve("lib").resolve("jrt-fs.jar"), "this is not a JAR file");
tb.writeFile(base.resolve("lib").resolve("modules"), "this is not a modules file");
doTestNoSource(base, "error: no source files", String.format("--system %s", base));
}
void doTest(Path base, String output, String options) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src, "class Dummy { }");