|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | +package com.oracle.svm.agent; |
| 27 | + |
| 28 | +import static com.oracle.svm.jvmtiagentbase.Support.jniFunctions; |
| 29 | +import static com.oracle.svm.jvmtiagentbase.Support.getMethodFullNameAtFrame; |
| 30 | + |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileOutputStream; |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.ByteBuffer; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | + |
| 38 | +import com.oracle.svm.jni.nativeapi.JNIMethodId; |
| 39 | +import org.graalvm.nativeimage.c.type.CCharPointer; |
| 40 | +import org.graalvm.nativeimage.c.type.CTypeConversion; |
| 41 | +import org.graalvm.nativeimage.c.type.VoidPointer; |
| 42 | +import org.graalvm.word.WordFactory; |
| 43 | + |
| 44 | +import com.oracle.svm.jni.nativeapi.JNIEnvironment; |
| 45 | +import com.oracle.svm.jni.nativeapi.JNIObjectHandle; |
| 46 | + |
| 47 | +public abstract class AbstractDynamicClassGenerationSupport { |
| 48 | + |
| 49 | + protected JNIEnvironment jni; |
| 50 | + protected JNIObjectHandle callerClass; |
| 51 | + protected final String generatedClassName; |
| 52 | + protected TraceWriter traceWriter; |
| 53 | + protected NativeImageAgent agent; |
| 54 | + |
| 55 | + private static String dynclassDumpDir = null; |
| 56 | + private static final String DEFAULT_DUMP = "dynClass"; |
| 57 | + |
| 58 | + static { |
| 59 | + if (dynclassDumpDir == null) { |
| 60 | + System.out.println("Warning: dynamic-class-dump-dir= was not set in -agentlib:native-image-agent=, using default location dynClass"); |
| 61 | + dynclassDumpDir = DEFAULT_DUMP; |
| 62 | + } |
| 63 | + Path dumpDir = new File(dynclassDumpDir).toPath(); |
| 64 | + try { |
| 65 | + if (!Files.exists(dumpDir)) { |
| 66 | + Files.createDirectory(dumpDir); |
| 67 | + } |
| 68 | + } catch (IOException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static void setDynClassDumpDir(String dir) { |
| 74 | + dynclassDumpDir = dir; |
| 75 | + } |
| 76 | + |
| 77 | + public static AbstractDynamicClassGenerationSupport getDynamicClassGenerationSupport(JNIEnvironment jni, JNIObjectHandle callerClass, |
| 78 | + String generatedClassName, TraceWriter traceWriter, NativeImageAgent agent) { |
| 79 | + return new DynamicDefineClassSupport(jni, callerClass, generatedClassName, traceWriter, agent); |
| 80 | + } |
| 81 | + |
| 82 | + protected AbstractDynamicClassGenerationSupport(JNIEnvironment jni, JNIObjectHandle callerClass, |
| 83 | + String generatedClassName, TraceWriter traceWriter, NativeImageAgent agent) { |
| 84 | + this.jni = jni; |
| 85 | + this.callerClass = callerClass; |
| 86 | + // Make sure use qualified name for generatedClassName |
| 87 | + if (generatedClassName.indexOf('/') != -1) { |
| 88 | + this.generatedClassName = generatedClassName.replace('/', '.'); |
| 89 | + } else { |
| 90 | + this.generatedClassName = generatedClassName; |
| 91 | + } |
| 92 | + this.traceWriter = traceWriter; |
| 93 | + this.agent = agent; |
| 94 | + } |
| 95 | + |
| 96 | + public abstract boolean traceReflects(); |
| 97 | + |
| 98 | + /** |
| 99 | + * Get class definition from java.lang.ClassLoader.defineClass. |
| 100 | + * |
| 101 | + * @return JObject represents byte[] or DirectBuffer |
| 102 | + */ |
| 103 | + protected abstract JNIObjectHandle getClassDefinition(); |
| 104 | + |
| 105 | + protected abstract int getClassDefinitionBytesLength(); |
| 106 | + |
| 107 | + protected abstract byte[] getClassContents(); |
| 108 | + |
| 109 | + protected byte[] getClassContentsFromByteArray() { |
| 110 | + // bytes parameter of defineClass method |
| 111 | + JNIObjectHandle bytes = getClassDefinition(); |
| 112 | + // len parameter of defineClass method |
| 113 | + int length = getClassDefinitionBytesLength(); |
| 114 | + // Get generated class' byte array |
| 115 | + CCharPointer byteArray = jniFunctions().getGetByteArrayElements().invoke(jni, bytes, WordFactory.nullPointer()); |
| 116 | + byte[] values = new byte[length]; |
| 117 | + try { |
| 118 | + CTypeConversion.asByteBuffer(byteArray, length).get(values); |
| 119 | + } finally { |
| 120 | + jniFunctions().getReleaseByteArrayElements().invoke(jni, bytes, byteArray, 0); |
| 121 | + } |
| 122 | + return values; |
| 123 | + } |
| 124 | + |
| 125 | + protected byte[] getClassContentsFromDirectBuffer() { |
| 126 | + // DirectBuffer parameter of defineClass |
| 127 | + JNIObjectHandle directbuffer = getClassDefinition(); |
| 128 | + |
| 129 | + // Get byte array from DirectBuffer |
| 130 | + VoidPointer baseAddr = jniFunctions().getGetDirectBufferAddress().invoke(jni, directbuffer); |
| 131 | + JNIMethodId limitMId = agent.handles().getMethodId(jni, agent.handles().javaNioByteBuffer, "limit", "()I", false); |
| 132 | + int limit = jniFunctions().getCallIntMethod().invoke(jni, directbuffer, limitMId); |
| 133 | + ByteBuffer classContentsAsByteBuffer = CTypeConversion.asByteBuffer(baseAddr, limit); |
| 134 | + byte[] dest = new byte[classContentsAsByteBuffer.limit()]; |
| 135 | + classContentsAsByteBuffer.get(dest); |
| 136 | + classContentsAsByteBuffer.position(0); |
| 137 | + return dest; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Save dynamically defined class to file system. |
| 142 | + * |
| 143 | + * @return true if successfully dumped |
| 144 | + */ |
| 145 | + public boolean dumpDefinedClass() { |
| 146 | + byte[] values = getClassContents(); |
| 147 | + // Get name for generated class |
| 148 | + String internalName = generatedClassName; |
| 149 | + if (internalName.indexOf('.') != -1) { |
| 150 | + internalName = internalName.replace('.', File.separatorChar); |
| 151 | + } |
| 152 | + String dumpFileName = dynclassDumpDir + File.pathSeparator + internalName + ".class"; |
| 153 | + |
| 154 | + // Get directory from package |
| 155 | + String dumpDirs = dumpFileName.substring(0, dumpFileName.lastIndexOf('/')); |
| 156 | + try { |
| 157 | + File dirs = new File(dumpDirs); |
| 158 | + if (!dirs.exists()) { |
| 159 | + dirs.mkdirs(); |
| 160 | + } |
| 161 | + try (FileOutputStream stream = new FileOutputStream(dumpFileName);) { |
| 162 | + stream.write(values); |
| 163 | + } |
| 164 | + |
| 165 | + // Dump stack trace for debug usage |
| 166 | + String dumpTraceFile = dynclassDumpDir + File.pathSeparator + internalName + ".txt"; |
| 167 | + StringBuilder trace = new StringBuilder(); |
| 168 | + int i = 0; |
| 169 | + while (true) { |
| 170 | + String methodName = getMethodFullNameAtFrame(jni, i++); |
| 171 | + if (methodName == null) { |
| 172 | + break; |
| 173 | + } |
| 174 | + trace.append(methodName).append("\n"); |
| 175 | + } |
| 176 | + try (FileOutputStream traceStream = new FileOutputStream(dumpTraceFile);) { |
| 177 | + traceStream.write(trace.toString().getBytes()); |
| 178 | + } |
| 179 | + } catch (IOException e) { |
| 180 | + e.printStackTrace(); |
| 181 | + return false; |
| 182 | + } |
| 183 | + return true; |
| 184 | + } |
| 185 | +} |
0 commit comments