1 /* 2 * Copyright (c) 2013, 2018, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.lang.invoke; 26 27 import sun.util.logging.PlatformLogger; 28 29 import java.io.FilePermission; 30 import java.nio.file.Files; 31 import java.nio.file.InvalidPathException; 32 import java.nio.file.Path; 33 import java.security.AccessController; 34 import java.security.PrivilegedAction; 35 import java.util.Objects; 36 import java.util.concurrent.atomic.AtomicBoolean; 37 38 /** 39 * Helper class used by InnerClassLambdaMetafactory to log generated classes 40 * 41 * @implNote 42 * <p> Because this class is called by LambdaMetafactory, make use 43 * of lambda lead to recursive calls cause stack overflow. 44 */ 45 final class ProxyClassesDumper { 46 private static final char[] HEX = { 47 '0', '1', '2', '3', '4', '5', '6', '7', 48 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 49 }; 50 private static final char[] BAD_CHARS = { 51 '\\', ':', '*', '?', '"', '<', '>', '|' 52 }; 53 private static final String[] REPLACEMENT = { 54 "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C" 55 }; 56 57 private final Path dumpDir; 58 59 public static ProxyClassesDumper getInstance(String path) { 60 if (null == path) { 61 return null; 62 } 63 try { 64 path = path.trim(); 65 final Path dir = Path.of(path.isEmpty() ? "." : path); 66 AccessController.doPrivileged(new PrivilegedAction<>() { 67 @Override 68 public Void run() { 69 validateDumpDir(dir); 70 return null; 71 } 72 }, null, new FilePermission("<<ALL FILES>>", "read, write")); 73 return new ProxyClassesDumper(dir); 74 } catch (InvalidPathException ex) { 75 PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) 76 .warning("Path " + path + " is not valid - dumping disabled", ex); 77 } catch (IllegalArgumentException iae) { 78 PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) 79 .warning(iae.getMessage() + " - dumping disabled"); 80 } 81 return null; 82 } 83 84 private ProxyClassesDumper(Path path) { 85 dumpDir = Objects.requireNonNull(path); 86 } 87 88 private static void validateDumpDir(Path path) { 89 if (!Files.exists(path)) { 90 throw new IllegalArgumentException("Directory " + path + " does not exist"); 91 } else if (!Files.isDirectory(path)) { 92 throw new IllegalArgumentException("Path " + path + " is not a directory"); 93 } else if (!Files.isWritable(path)) { 94 throw new IllegalArgumentException("Directory " + path + " is not writable"); 95 } 96 } 97 98 public static String encodeForFilename(String className) { 99 final int len = className.length(); 100 StringBuilder sb = new StringBuilder(len); 101 102 for (int i = 0; i < len; i++) { 103 char c = className.charAt(i); 104 // control characters 105 if (c <= 31) { 106 sb.append('%'); 107 sb.append(HEX[c >> 4 & 0x0F]); 108 sb.append(HEX[c & 0x0F]); 109 } else { 110 int j = 0; 111 for (; j < BAD_CHARS.length; j++) { 112 if (c == BAD_CHARS[j]) { 113 sb.append(REPLACEMENT[j]); 114 break; 115 } 116 } 117 if (j >= BAD_CHARS.length) { 118 sb.append(c); 119 } 120 } 121 } 122 123 return sb.toString(); 124 } 125 126 public void dumpClass(String className, final byte[] classBytes) { 127 Path file; 128 try { 129 file = dumpDir.resolve(encodeForFilename(className) + ".class"); 130 } catch (InvalidPathException ex) { 131 PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) 132 .warning("Invalid path for class " + className); 133 return; 134 } 135 136 try { 137 Path dir = file.getParent(); 138 Files.createDirectories(dir); 139 Files.write(file, classBytes); 140 } catch (Exception ignore) { 141 PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) 142 .warning("Exception writing to path at " + file.toString()); 143 // simply don't care if this operation failed 144 } 145 } 146 }