List of usage examples for org.objectweb.asm.tree.analysis SimpleVerifier SimpleVerifier
public SimpleVerifier(final Type currentClass, final Type currentSuperClass, final List<Type> currentClassInterfaces, final boolean isInterface)
From source file:org.ballerinalang.compiler.backend.jvm.ClassVerifier.java
License:Open Source License
/** * This method is an extension of//from w w w. j ava2 s . c o m * {@code jdk.internal.org.objectweb.asm.util.CheckClassAdapter#verify(ClassReader, boolean, java.io.PrintWriter)}. * * @param bytes Bytes stream of the class to be verified. * @return An optional error, if there are verification errors. */ private static Optional<ErrorValue> verify(byte[] bytes, ClassLoader classLoader) { ClassReader classReader = new ClassReader(bytes); ClassNode classNode = new ClassNode(); classReader.accept(new CheckClassAdapter(Opcodes.ASM7, classNode, false) { }, ClassReader.SKIP_DEBUG); Type syperType = classNode.superName == null ? null : Type.getObjectType(classNode.superName); List<MethodNode> methods = classNode.methods; List<Type> interfaces = new ArrayList<>(); for (String interfaceName : classNode.interfaces) { interfaces.add(Type.getObjectType(interfaceName)); } for (MethodNode method : methods) { SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(classNode.name), syperType, interfaces, (classNode.access & Opcodes.ACC_INTERFACE) != 0); Analyzer<BasicValue> analyzer = new Analyzer<>(verifier); if (classLoader != null) { verifier.setClassLoader(classLoader); } try { analyzer.analyze(classNode.name, method); } catch (AnalyzerException e) { return Optional.of(BallerinaErrors.createError(e.getMessage())); } } return Optional.empty(); }
From source file:org.f3.tools.bytecodeverifier.Verifier.java
License:Open Source License
private static boolean verify(String name, ClassReader reader, final PrintWriter err, boolean verbose) { if (verbose) { err.println("Verifying " + name); }/*from www .jav a 2 s . co m*/ ClassNode classNode = new ClassNode(); reader.accept(new CheckClassAdapter(classNode), ClassReader.SKIP_DEBUG); Type syperType = classNode.superName == null ? null : Type.getObjectType(classNode.superName); List<Type> interfaces = new ArrayList<Type>(); for (Object iface : classNode.interfaces) { interfaces.add(Type.getObjectType(iface.toString())); } List<MethodNode> methods = classNode.methods; for (int i = 0; i < methods.size(); i++) { MethodNode method = methods.get(i); SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(classNode.name), syperType, interfaces, false) { @Override protected boolean isAssignableFrom(Type t, Type u) { // FIXME: Assignment check in the superclass implementation uses // Class.forName to check currently loaded classes. We don't want // to use loaded Class objects in the test. We leave the reference // assignment compatibility checks for now. return true; } }; Analyzer analyzer = new Analyzer(verifier); try { analyzer.analyze(classNode.name, method); } catch (AnalyzerException exp) { err.println("Verification failed for " + name); exp.printStackTrace(err); err.flush(); return false; } } err.flush(); return true; }
From source file:org.spongepowered.asm.util.Locals.java
License:MIT License
/** * Use ASM Analyzer to generate the local variable table for the specified * method/* w ww . ja va 2s. c o m*/ * * @param classNode Containing class * @param method Method * @return generated local variable table */ public static List<LocalVariableNode> generateLocalVariableTable(ClassNode classNode, MethodNode method) { List<Type> interfaces = null; if (classNode.interfaces != null) { interfaces = new ArrayList<Type>(); for (String iface : classNode.interfaces) { interfaces.add(Type.getObjectType(iface)); } } Type objectType = null; if (classNode.superName != null) { objectType = Type.getObjectType(classNode.superName); } // Use Analyzer to generate the bytecode frames Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>( new SimpleVerifier(Type.getObjectType(classNode.name), objectType, interfaces, false)); try { analyzer.analyze(classNode.name, method); } catch (AnalyzerException ex) { ex.printStackTrace(); } // Get frames from the Analyzer Frame<BasicValue>[] frames = analyzer.getFrames(); // Record the original size of hte method int methodSize = method.instructions.size(); // List of LocalVariableNodes to return List<LocalVariableNode> localVariables = new ArrayList<LocalVariableNode>(); LocalVariableNode[] localNodes = new LocalVariableNode[method.maxLocals]; // LocalVariableNodes for current frame BasicValue[] locals = new BasicValue[method.maxLocals]; // locals in previous frame, used to work out what changes between frames LabelNode[] labels = new LabelNode[methodSize]; // Labels to add to the method, for the markers // Traverse the frames and work out when locals begin and end for (int i = 0; i < methodSize; i++) { Frame<BasicValue> f = frames[i]; if (f == null) { continue; } LabelNode label = null; for (int j = 0; j < f.getLocals(); j++) { BasicValue local = f.getLocal(j); if (local == null && locals[j] == null) { continue; } if (local != null && local.equals(locals[j])) { continue; } if (label == null) { label = new LabelNode(); labels[i] = label; } if (local == null && locals[j] != null) { localVariables.add(localNodes[j]); localNodes[j].end = label; localNodes[j] = null; } else if (local != null) { if (locals[j] != null) { localVariables.add(localNodes[j]); localNodes[j].end = label; localNodes[j] = null; } String desc = (local.getType() != null) ? local.getType().getDescriptor() : null; localNodes[j] = new LocalVariableNode("var" + j, desc, null, label, null, j); } locals[j] = local; } } // Reached the end of the method so flush all current locals and mark the end LabelNode label = null; for (int k = 0; k < localNodes.length; k++) { if (localNodes[k] != null) { if (label == null) { label = new LabelNode(); method.instructions.add(label); } localNodes[k].end = label; localVariables.add(localNodes[k]); } } // Insert generated labels into the method body for (int n = methodSize - 1; n >= 0; n--) { if (labels[n] != null) { method.instructions.insert(method.instructions.get(n), labels[n]); } } return localVariables; }