1 /* 2 * Copyright (c) 2012, 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 package org.graalvm.compiler.printer; 24 25 import java.io.Closeable; 26 import java.io.IOException; 27 import java.lang.reflect.Array; 28 import java.util.Arrays; 29 import java.util.List; 30 import java.util.Map; 31 32 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 33 import org.graalvm.compiler.debug.DebugContext; 34 import org.graalvm.compiler.debug.DebugContext.Scope; 35 import org.graalvm.compiler.graph.Graph; 36 import org.graalvm.compiler.nodes.ConstantNode; 37 import org.graalvm.compiler.nodes.StructuredGraph; 38 import org.graalvm.compiler.nodes.util.JavaConstantFormatter; 39 import org.graalvm.compiler.phases.schedule.SchedulePhase; 40 import org.graalvm.compiler.serviceprovider.JDK9Method; 41 42 import jdk.vm.ci.meta.JavaConstant; 43 import jdk.vm.ci.meta.JavaKind; 44 import jdk.vm.ci.meta.JavaType; 45 import jdk.vm.ci.meta.MetaUtil; 46 import jdk.vm.ci.meta.ResolvedJavaMethod; 47 import jdk.vm.ci.runtime.JVMCI; 48 import jdk.vm.ci.services.Services; 49 50 interface GraphPrinter extends Closeable, JavaConstantFormatter { 51 52 /** 53 * Starts a new group of graphs with the given name, short name and method byte code index (BCI) 54 * as properties. 55 */ 56 void beginGroup(DebugContext debug, String name, String shortName, ResolvedJavaMethod method, int bci, Map<Object, Object> properties) throws IOException; 57 58 /** 59 * Prints an entire {@link Graph} with the specified title, optionally using short names for 60 * nodes. 61 */ 62 void print(DebugContext debug, Graph graph, Map<Object, Object> properties, int id, String format, Object... args) throws IOException; 63 64 SnippetReflectionProvider getSnippetReflectionProvider(); 65 66 /** 67 * Ends the current group. 68 */ 69 void endGroup() throws IOException; 70 71 @Override 72 void close(); 73 74 /** 75 * A JVMCI package dynamically exported to trusted modules. 76 */ 77 String JVMCI_RUNTIME_PACKAGE = JVMCI.class.getPackage().getName(); 78 79 /** 80 * {@code jdk.vm.ci} module. 81 */ 82 Object JVMCI_MODULE = JDK9Method.JAVA_SPECIFICATION_VERSION < 9 ? null : JDK9Method.getModule.invoke(Services.class); 83 84 /** 85 * Classes whose {@link #toString()} method does not run any untrusted code. 86 */ 87 List<Class<?>> TRUSTED_CLASSES = Arrays.asList( 88 String.class, 89 Class.class, 90 Boolean.class, 91 Byte.class, 92 Character.class, 93 Short.class, 94 Integer.class, 95 Float.class, 96 Long.class, 97 Double.class); 98 int MAX_CONSTANT_TO_STRING_LENGTH = 50; 99 100 /** 101 * Determines if invoking {@link Object#toString()} on an instance of {@code c} will only run 102 * trusted code. 103 */ 104 static boolean isToStringTrusted(Class<?> c) { 105 if (TRUSTED_CLASSES.contains(c)) { 106 return true; 107 } 108 if (JDK9Method.JAVA_SPECIFICATION_VERSION < 9) { 109 if (c.getClassLoader() == Services.class.getClassLoader()) { 110 // Loaded by the JVMCI class loader 111 return true; 112 } 113 } else { 114 Object module = JDK9Method.getModule.invoke(c); 115 if (JVMCI_MODULE == module || (Boolean) JDK9Method.isOpenTo.invoke(JVMCI_MODULE, JVMCI_RUNTIME_PACKAGE, module)) { 116 // Can access non-statically-exported package in JVMCI 117 return true; 118 } 119 } 120 if (c.getClassLoader() == GraphPrinter.class.getClassLoader()) { 121 return true; 122 } 123 return false; 124 } 125 126 /** 127 * Use the real {@link Object#toString()} method for {@link JavaConstant JavaConstants} that are 128 * wrapping trusted types, other just return the results of {@link JavaConstant#toString()}. 129 */ 130 @Override 131 default String format(JavaConstant constant) { 132 SnippetReflectionProvider snippetReflection = getSnippetReflectionProvider(); 133 if (snippetReflection != null) { 134 if (constant.getJavaKind() == JavaKind.Object) { 135 Object obj = snippetReflection.asObject(Object.class, constant); 136 if (obj != null) { 137 return GraphPrinter.constantToString(obj); 138 } 139 } 140 } 141 return constant.toString(); 142 } 143 144 /** 145 * Sets or updates the {@code "rawvalue"} and {@code "toString"} properties in {@code props} for 146 * {@code cn} if it's a boxed Object value and {@code snippetReflection} can access the raw 147 * value. 148 */ 149 default void updateStringPropertiesForConstant(Map<Object, Object> props, ConstantNode cn) { 150 if (cn.isJavaConstant() && cn.getStackKind().isObject()) { 151 String toString = format(cn.asJavaConstant()); 152 String rawvalue = GraphPrinter.truncate(toString); 153 // Overwrite the value inserted by 154 // ConstantNode.getDebugProperties() 155 props.put("rawvalue", rawvalue); 156 if (!rawvalue.equals(toString)) { 157 props.put("toString", toString); 158 } 159 } 160 } 161 162 /** 163 * Replaces all {@link JavaType} elements in {@code args} with the result of 164 * {@link JavaType#getUnqualifiedName()}. 165 * 166 * @return a copy of {@code args} with the above mentioned substitutions or {@code args} if no 167 * substitutions were performed 168 */ 169 default Object[] simplifyClassArgs(Object... args) { 170 Object[] res = args; 171 for (int i = 0; i < args.length; i++) { 172 Object arg = args[i]; 173 if (arg instanceof JavaType) { 174 if (args == res) { 175 res = new Object[args.length]; 176 for (int a = 0; a < i; a++) { 177 res[a] = args[a]; 178 } 179 } 180 res[i] = ((JavaType) arg).getUnqualifiedName(); 181 } else { 182 res[i] = arg; 183 } 184 } 185 return res; 186 } 187 188 static String truncate(String s) { 189 if (s.length() > MAX_CONSTANT_TO_STRING_LENGTH) { 190 return s.substring(0, MAX_CONSTANT_TO_STRING_LENGTH - 3) + "..."; 191 } 192 return s; 193 } 194 195 static String constantToString(Object value) { 196 Class<?> c = value.getClass(); 197 if (c.isArray()) { 198 return constantArrayToString(value); 199 } else if (value instanceof Enum) { 200 return ((Enum<?>) value).name(); 201 } else if (isToStringTrusted(c)) { 202 return value.toString(); 203 } 204 return MetaUtil.getSimpleName(c, true) + "@" + Integer.toHexString(System.identityHashCode(value)); 205 206 } 207 208 static String constantArrayToString(Object array) { 209 Class<?> componentType = array.getClass().getComponentType(); 210 assert componentType != null; 211 int arrayLength = Array.getLength(array); 212 StringBuilder buf = new StringBuilder(MetaUtil.getSimpleName(componentType, true)).append('[').append(arrayLength).append("]{"); 213 int length = arrayLength; 214 boolean primitive = componentType.isPrimitive(); 215 for (int i = 0; i < length; i++) { 216 if (primitive) { 217 buf.append(Array.get(array, i)); 218 } else { 219 Object o = ((Object[]) array)[i]; 220 buf.append(o == null ? "null" : constantToString(o)); 221 } 222 if (i != length - 1) { 223 buf.append(", "); 224 } 225 } 226 return buf.append('}').toString(); 227 } 228 229 @SuppressWarnings("try") 230 static StructuredGraph.ScheduleResult getScheduleOrNull(Graph graph) { 231 if (graph instanceof StructuredGraph) { 232 StructuredGraph sgraph = (StructuredGraph) graph; 233 StructuredGraph.ScheduleResult scheduleResult = sgraph.getLastSchedule(); 234 if (scheduleResult == null) { 235 DebugContext debug = graph.getDebug(); 236 try (Scope scope = debug.disable()) { 237 SchedulePhase schedule = new SchedulePhase(graph.getOptions()); 238 schedule.apply(sgraph); 239 scheduleResult = sgraph.getLastSchedule(); 240 } catch (Throwable t) { 241 } 242 } 243 return scheduleResult; 244 } 245 return null; 246 } 247 }