|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8314263 |
| 27 | + * @summary Signed jars triggering Logger finder recursion and StackOverflowError |
| 28 | + * @library /test/lib |
| 29 | + * @build jdk.test.lib.compiler.CompilerUtils |
| 30 | + * jdk.test.lib.process.* |
| 31 | + * jdk.test.lib.util.JarUtils |
| 32 | + * jdk.test.lib.JDKToolLauncher |
| 33 | + * @compile SignedLoggerFinderTest.java SimpleLoggerFinder.java |
| 34 | + * @run main SignedLoggerFinderTest init |
| 35 | + * @run main SignedLoggerFinderTest init sign |
| 36 | + */ |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.nio.file.*; |
| 40 | +import java.security.*; |
| 41 | +import java.util.*; |
| 42 | +import java.util.function.*; |
| 43 | +import java.util.jar.*; |
| 44 | + |
| 45 | +import jdk.test.lib.JDKToolFinder; |
| 46 | +import jdk.test.lib.JDKToolLauncher; |
| 47 | +import jdk.test.lib.Utils; |
| 48 | +import jdk.test.lib.process.OutputAnalyzer; |
| 49 | +import jdk.test.lib.process.ProcessTools; |
| 50 | +import jdk.test.lib.util.JarUtils; |
| 51 | + |
| 52 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 53 | +import static java.util.Arrays.asList; |
| 54 | + |
| 55 | +public class SignedLoggerFinderTest { |
| 56 | + |
| 57 | + /** |
| 58 | + * This test triggers recursion in the broken JDK. The error can |
| 59 | + * manifest in a few different ways. |
| 60 | + * One error seen is "java.lang.NoClassDefFoundError: |
| 61 | + * Could not initialize class jdk.internal.logger.LoggerFinderLoader$ErrorPolicy" |
| 62 | + * |
| 63 | + * The original reported error was a StackOverflow (also seen in different iterations |
| 64 | + * of this run). Running test in signed and unsigned jar mode for sanity coverage. |
| 65 | + * The current bug only manifests when jars are signed. |
| 66 | + */ |
| 67 | + |
| 68 | + private static boolean init = false; |
| 69 | + private static boolean signJars = false; |
| 70 | + private static boolean mutliThreadLoad = false; |
| 71 | + private static volatile boolean testComplete = false; |
| 72 | + |
| 73 | + private static final String KEYSTORE = "8314263.keystore"; |
| 74 | + private static final String ALIAS = "JavaTest"; |
| 75 | + private static final String STOREPASS = "changeit"; |
| 76 | + private static final String KEYPASS = "changeit"; |
| 77 | + private static final String DNAME = "CN=sample"; |
| 78 | + private static final String CUSTOM_LOGGER_FINDER_NAME = |
| 79 | + "loggerfinder.SimpleLoggerFinder"; |
| 80 | + private static final String CUSTOM_LOGGER_NAME = |
| 81 | + "loggerfinder.SimpleLoggerFinder$SimpleLogger"; |
| 82 | + private static final String INTERNAL_LOGGER_FINDER_NAME = |
| 83 | + "sun.util.logging.internal.LoggingProviderImpl"; |
| 84 | + private static final String INTERNAL_LOGGER_NAME = |
| 85 | + "sun.util.logging.internal.LoggingProviderImpl$JULWrapper"; |
| 86 | + private static final Path jarPath1 = |
| 87 | + Path.of(System.getProperty("test.classes", "."), "SimpleLoggerFinder.jar"); |
| 88 | + private static final Path jarPath2 = |
| 89 | + Path.of(System.getProperty("test.classes", "."), "SimpleLoggerFinder2.jar"); |
| 90 | + |
| 91 | + public static void main(String[] args) throws Throwable { |
| 92 | + init = args.length >=1 && args[0].equals("init"); |
| 93 | + signJars = args.length >=2 && args[1].equals("sign"); |
| 94 | + |
| 95 | + // init only passed in by jtreg test run, initialize the environment |
| 96 | + // for the subsequent test run |
| 97 | + if (init) { |
| 98 | + initialize(); |
| 99 | + launchTest(false, false); |
| 100 | + launchTest(false, true); |
| 101 | + launchTest(true, false); |
| 102 | + launchTest(true, true); |
| 103 | + |
| 104 | + } else { |
| 105 | + // set up complete. Run the code to trigger the recursion |
| 106 | + // We're in the JVM launched by ProcessTools.executeCommand |
| 107 | + boolean multiThreadLoad = Boolean.getBoolean("multiThreadLoad"); |
| 108 | + boolean withCustomLoggerFinder = Boolean.getBoolean("withCustomLoggerFinder"); |
| 109 | + |
| 110 | + if (multiThreadLoad) { |
| 111 | + long sleep = new Random().nextLong(100L) + 1L; |
| 112 | + System.out.println("multi thread load sleep value: " + sleep); |
| 113 | + new Thread(runnableWithSleep( |
| 114 | + () -> System.getLogger("logger" + System.currentTimeMillis()), |
| 115 | + sleep, "System.getLogger type: ")).start(); |
| 116 | + new Thread(runnableWithSleep( |
| 117 | + () -> System.LoggerFinder.getLoggerFinder(), |
| 118 | + sleep, "System.getLoggerFinder type: ")).start(); |
| 119 | + } |
| 120 | + |
| 121 | + if (withCustomLoggerFinder) { |
| 122 | + JarFile jf = new JarFile(jarPath1.toString(), true); |
| 123 | + jf.getInputStream(jf.getJarEntry("loggerfinder/SimpleLoggerFinder.class")); |
| 124 | + JarFile jf2 = new JarFile(jarPath2.toString(), true); |
| 125 | + jf2.getInputStream(jf.getJarEntry("loggerfinder/SimpleLoggerFinder.class")); |
| 126 | + } else { |
| 127 | + // some other call to prod LoggerFinder loading |
| 128 | + System.getLogger("random" + System.currentTimeMillis()); |
| 129 | + System.LoggerFinder.getLoggerFinder(); |
| 130 | + } |
| 131 | + Security.setProperty("test_1", "test"); |
| 132 | + |
| 133 | + // some extra sanity checks |
| 134 | + if (withCustomLoggerFinder) { |
| 135 | + assertEquals(System.LoggerFinder.getLoggerFinder().getClass().getName(), |
| 136 | + CUSTOM_LOGGER_FINDER_NAME); |
| 137 | + System.Logger testLogger = System.getLogger("jdk.event.security"); |
| 138 | + assertEquals(testLogger.getClass().getName(), CUSTOM_LOGGER_NAME); |
| 139 | + } else { |
| 140 | + assertEquals(System.LoggerFinder.getLoggerFinder().getClass().getName(), |
| 141 | + INTERNAL_LOGGER_FINDER_NAME); |
| 142 | + System.Logger testLogger = System.getLogger("jdk.event.security"); |
| 143 | + assertEquals(testLogger.getClass().getName(), INTERNAL_LOGGER_NAME); |
| 144 | + } |
| 145 | + testComplete = true; |
| 146 | + |
| 147 | + // LoggerFinder should be initialized, trigger a simple log call |
| 148 | + Security.setProperty("test_2", "test"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // helper to create the inner test. Run config variations with the LoggerFinder jars |
| 153 | + // on the classpath and with other threads running System.Logger calls during load |
| 154 | + private static void launchTest(boolean multiThreadLoad, boolean withCustomLoggerFinder) { |
| 155 | + List<String> cmds = new ArrayList<>(); |
| 156 | + cmds.add(JDKToolFinder.getJDKTool("java")); |
| 157 | + cmds.addAll(asList(Utils.getTestJavaOpts())); |
| 158 | + if (withCustomLoggerFinder) { |
| 159 | + cmds.addAll(List.of("-classpath", |
| 160 | + System.getProperty("test.classes") + File.pathSeparator + |
| 161 | + jarPath1.toString() + File.pathSeparator + jarPath2.toString(), |
| 162 | + "-Dtest.classes=" + System.getProperty("test.classes"))); |
| 163 | + } else { |
| 164 | + cmds.addAll(List.of("-classpath", |
| 165 | + System.getProperty("test.classes"))); |
| 166 | + } |
| 167 | + cmds.addAll(List.of( |
| 168 | + // following debug property seems useful to tickle the issue |
| 169 | + "-Dsun.misc.URLClassPath.debug=true", |
| 170 | + // console logger level to capture event output |
| 171 | + "-Djdk.system.logger.level=DEBUG", |
| 172 | + // useful for debug purposes |
| 173 | + "-Djdk.logger.finder.error=DEBUG", |
| 174 | + // enable logging to verify correct output |
| 175 | + "-Djava.util.logging.config.file=" + |
| 176 | + Path.of(System.getProperty("test.src", "."), "logging.properties"))); |
| 177 | + if (multiThreadLoad) { |
| 178 | + cmds.add("-DmultiThreadLoad=true"); |
| 179 | + } |
| 180 | + if (withCustomLoggerFinder) { |
| 181 | + cmds.add("-DwithCustomLoggerFinder=true"); |
| 182 | + } |
| 183 | + cmds.addAll(List.of( |
| 184 | + "SignedLoggerFinderTest", |
| 185 | + "no-init")); |
| 186 | + |
| 187 | + try { |
| 188 | + OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(cmds.stream() |
| 189 | + .filter(t -> !t.isEmpty()) |
| 190 | + .toArray(String[]::new)) |
| 191 | + .shouldHaveExitValue(0); |
| 192 | + if (withCustomLoggerFinder) { |
| 193 | + outputAnalyzer |
| 194 | + .shouldContain("TEST LOGGER: [test_1, test]") |
| 195 | + .shouldContain("TEST LOGGER: [test_2, test]"); |
| 196 | + } else { |
| 197 | + outputAnalyzer |
| 198 | + .shouldContain("SecurityPropertyModification: key:test_1") |
| 199 | + .shouldContain("SecurityPropertyModification: key:test_2"); |
| 200 | + } |
| 201 | + if (withCustomLoggerFinder && signJars) { |
| 202 | + // X509 cert generated during verification of signed jar file |
| 203 | + outputAnalyzer |
| 204 | + .shouldContain(DNAME); |
| 205 | + } |
| 206 | + |
| 207 | + } catch (Throwable t) { |
| 208 | + throw new RuntimeException("Unexpected fail.", t); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + private static Runnable runnableWithSleep(Supplier s, long sleep, String desc) { |
| 213 | + return () -> { |
| 214 | + while(!testComplete) { |
| 215 | + System.out.println(desc + s.get().getClass().getName()); |
| 216 | + try { |
| 217 | + Thread.sleep(sleep); |
| 218 | + } catch (InterruptedException e) { |
| 219 | + throw new RuntimeException(e); |
| 220 | + } |
| 221 | + } |
| 222 | + }; |
| 223 | + } |
| 224 | + |
| 225 | + private static void initialize() throws Throwable { |
| 226 | + if (signJars) { |
| 227 | + genKey(); |
| 228 | + } |
| 229 | + |
| 230 | + Path classes = Paths.get(System.getProperty("test.classes", "")); |
| 231 | + JarUtils.createJarFile(jarPath1, |
| 232 | + classes, |
| 233 | + classes.resolve("loggerfinder/SimpleLoggerFinder.class"), |
| 234 | + classes.resolve("loggerfinder/SimpleLoggerFinder$SimpleLogger.class")); |
| 235 | + |
| 236 | + JarUtils.updateJarFile(jarPath1, Path.of(System.getProperty("test.src")), |
| 237 | + Path.of("META-INF", "services", "java.lang.System$LoggerFinder")); |
| 238 | + if (signJars) { |
| 239 | + signJar(jarPath1.toString()); |
| 240 | + } |
| 241 | + // multiple signed jars with services to have ServiceLoader iteration |
| 242 | + Files.copy(jarPath1, jarPath2, REPLACE_EXISTING); |
| 243 | + } |
| 244 | + |
| 245 | + private static void genKey() throws Throwable { |
| 246 | + String keytool = JDKToolFinder.getJDKTool("keytool"); |
| 247 | + Files.deleteIfExists(Paths.get(KEYSTORE)); |
| 248 | + ProcessTools.executeCommand(keytool, |
| 249 | + "-J-Duser.language=en", |
| 250 | + "-J-Duser.country=US", |
| 251 | + "-genkey", |
| 252 | + "-keyalg", "rsa", |
| 253 | + "-alias", ALIAS, |
| 254 | + "-keystore", KEYSTORE, |
| 255 | + "-keypass", KEYPASS, |
| 256 | + "-dname", DNAME, |
| 257 | + "-storepass", STOREPASS |
| 258 | + ).shouldHaveExitValue(0); |
| 259 | + } |
| 260 | + |
| 261 | + |
| 262 | + private static OutputAnalyzer signJar(String jarName) throws Throwable { |
| 263 | + List<String> args = new ArrayList<>(); |
| 264 | + args.add("-verbose"); |
| 265 | + args.add(jarName); |
| 266 | + args.add(ALIAS); |
| 267 | + |
| 268 | + return jarsigner(args); |
| 269 | + } |
| 270 | + |
| 271 | + private static OutputAnalyzer jarsigner(List<String> extra) |
| 272 | + throws Throwable { |
| 273 | + JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jarsigner") |
| 274 | + .addVMArg("-Duser.language=en") |
| 275 | + .addVMArg("-Duser.country=US") |
| 276 | + .addToolArg("-keystore") |
| 277 | + .addToolArg(KEYSTORE) |
| 278 | + .addToolArg("-storepass") |
| 279 | + .addToolArg(STOREPASS) |
| 280 | + .addToolArg("-keypass") |
| 281 | + .addToolArg(KEYPASS); |
| 282 | + for (String s : extra) { |
| 283 | + if (s.startsWith("-J")) { |
| 284 | + launcher.addVMArg(s.substring(2)); |
| 285 | + } else { |
| 286 | + launcher.addToolArg(s); |
| 287 | + } |
| 288 | + } |
| 289 | + return ProcessTools.executeCommand(launcher.getCommand()); |
| 290 | + } |
| 291 | + |
| 292 | + private static void assertEquals(String received, String expected) { |
| 293 | + if (!expected.equals(received)) { |
| 294 | + throw new RuntimeException("Received: " + received); |
| 295 | + } |
| 296 | + } |
| 297 | +} |
0 commit comments