Refine search
@Override public void visitFile(Path file, String relative) throws IOException { if (file.getFileName().toString().endsWith(".class")) { ClassReader cr = new ClassReader(Files.readAllBytes(file)); ClassNode cn = new ClassNode(); cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES | ClassReader.SKIP_FRAMES); for (MethodNode method : cn.methods) { BasicVerifier verifier = new BasicVerifier(); Analyzer<BasicValue> a = new Analyzer<>(verifier); try { a.analyze(cn.name, method); } catch (Exception ex) { System.err.println("Error verify method " + cr.getClassName() + "." + method.name + " " + method.desc); if (detail) { ex.printStackTrace(System.err); printAnalyzerResult(method, a, new PrintWriter(new OutputStreamWriter(System.err, StandardCharsets.UTF_8))); } } } } } });
protected void checkTrace(String expected) throws IOException { org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(traceCode); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); CheckClassAdapter.verify(cr, false, pw); if (sw.toString().contains("AnalyzerException")) { System.err.println(sw.toString()); fail(); } }
public static void debugByteCode(ClassDefinition classDefinition, PrintWriter writer) { ClassReader reader = new ClassReader(classDefinition.getBytecode()); reader.accept(new CheckClassAdapter(new TraceClassVisitor(writer)), 0); File file = new File("/tmp/" + classDefinition.getInternalName() + ".class"); file.getParentFile().mkdirs(); writer.println(file); writer.flush(); try { ByteStreams.write(classDefinition.getBytecode(), Files.newOutputStreamSupplier(file)); } catch (IOException e) { e.printStackTrace(); } }
private ClassVisitor decorateClassVisitor(ClassVisitor visitor, final boolean check, final PrintStream dumpStream) { if (check) { visitor = new CheckClassAdapter(visitor); if (null != CHECK_DATA_FLOW) { try { // Currently CheckMethodAdapter throws error, so suppress flow checks CHECK_DATA_FLOW.set(visitor, Boolean.FALSE); } catch (final IllegalAccessException ex) { throw new RuntimeException(ex); } } } if (null != dumpStream) { visitor = new TraceClassVisitor(visitor, new PrintWriter(dumpStream)); } return visitor; }
@SuppressWarnings("rawtypes") public static void verify(final ClassReader cr, PrintWriter out) throws AnalyzerException, IllegalArgumentException, IllegalAccessException { ClassNode cn = new ClassNode(); cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG); List methods = cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); List tryCatchBlocks = method.tryCatchBlocks; for (int j = 0; j < tryCatchBlocks.size(); j++) { TryCatchBlockNode tcn = (TryCatchBlockNode) tryCatchBlocks.get(j); if (tcn.start.equals(tcn.end)) { throw new DexException("try/catch block %d in %s has same start(%s) and end(%s)", j, method.name, tcn.start.getLabel(), tcn.end.getLabel()); } } BasicVerifier verifier = new BasicVerifier(); Analyzer a = new Analyzer(verifier); try { a.analyze(cn.name, method); } catch (Exception e) { out.println(cr.getClassName() + "." + method.name + method.desc); printAnalyzerResult(method, a, out); e.printStackTrace(out); out.flush(); throw new DexException("method " + method.name + " " + method.desc, e); } } }
private void validateClass(ClassReader reader, ClassLoader loader) { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); Exception error = null; try { CheckClassAdapter.verify(reader, loader, false, printWriter); } catch (Exception e) { error = e; } String contents = writer.toString(); if (error != null || contents.length() > 0) { reader.accept(new TraceClassVisitor(printWriter), 0); throw new IllegalStateException(writer.toString(), error); } }
private boolean readClass(String clazz) throws IOException { ClassReader cr = new ClassReader(new FileInputStream(clazz)); ClassNode ca = new ClassNode() { public void visitEnd() { cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS); boolean failed = false;
/** * See {@link org.objectweb.asm.util.CheckClassAdapter#verify(ClassReader, boolean, PrintWriter)}. */ public static void verify(final ClassReader cr, final boolean dump, final PrintWriter pw) { /* * For plain verification, we don't need to restore the original access * bytes the way we do when the check adapter is used as part of a chain, so * we can just strip it and use the ASM version directly. */ final ClassWriter classWriter = new ClassWriter(0); cr.accept(new InnerClassAccessStripper(CompilationConfig.ASM_API_VERSION, classWriter), ClassReader.SKIP_DEBUG); final ClassReader strippedCr = new ClassReader(classWriter.toByteArray()); CheckClassAdapter.verify(strippedCr, dump, pw); } }
/** * Weave tracking method call if the annotations present. */ private static void weaveIfNecessary(ClassReader classReader, File dest) throws IOException { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); ModifierClassWriter mcw = new ModifierClassWriter(Opcodes.ASM5, cw); classReader.accept(new CheckClassAdapter(mcw), ClassReader.EXPAND_FRAMES); if (!mcw.isProceed()) { return; } OutputStream out = null; try { out = new FileOutputStream(dest); out.write(cw.toByteArray()); out.flush(); } finally { closeQuietly(out); } }
/** * @param cv - a listener for code-instrumentation events * @param ignoreRegexp - list of patters of method calls that should be ignored from line-coverage-measurement * @param duplicatedLinesMap - map of found duplicates in the class. You should use {@link DetectDuplicatedCodeClassVisitor} to find the duplicated lines. */ public AbstractFindTouchPointsClassInstrumenter(ClassVisitor cv, Collection<Pattern> ignoreRegexp, Map<Integer, Map<Integer, Integer>> duplicatedLinesMap) { super(Opcodes.ASM4, new CheckClassAdapter(cv, false)); this.ignoreRegexp = ignoreRegexp; this.duplicatedLinesMap = duplicatedLinesMap; }
public Class<Runnable> load() { ClassWriter cw0 = new ClassWriter(ClassWriter.COMPUTE_FRAMES); CheckClassAdapter cw = new CheckClassAdapter(cw0); GeneratorAdapter mv; cw.visit(52, ACC_PUBLIC + ACC_SUPER, "Test", null, "java/lang/Object", new String[] { "java/lang/Runnable" }); mv = new GeneratorAdapter(cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null), ACC_PUBLIC, "<init>", "()V"); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitEnd(); mv = new GeneratorAdapter(cw.visitMethod(ACC_PUBLIC, "run", "()V", null, null), ACC_PUBLIC, "run", "()V"); mv.visitCode(); Label l0 = new Label(); mv.visitEnd(); cw.visitEnd(); byte[] bytes = cw0.toByteArray();
private static byte[] generateClass(String internalClassName, Type testClassType, int iThread, List<Actor> actors, List<Object> objArgs, boolean waitsEnabled) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); CheckClassAdapter cca = new CheckClassAdapter(cw, false); cca.visit(52, ACC_PUBLIC + ACC_SUPER, internalClassName, null, TEST_THREAD_EXECUTION_TYPE.getInternalName(), null); generateConstructor(cca); generateRun(cca, testClassType, iThread, actors, objArgs, waitsEnabled); cca.visitEnd(); return cw.toByteArray(); }
/** * Checks the given class. * * @param classReader the class to be checked. * @param printResults whether to print the results of the bytecode verification. * @param printWriter where the results (or the stack trace in case of error) must be printed. */ public static void verify( final ClassReader classReader, final boolean printResults, final PrintWriter printWriter) { verify(classReader, null, printResults, printWriter); }
@SuppressWarnings("unchecked") public Class<T> generate() { ClassWriter out = new ClassWriter(ClassWriter.COMPUTE_MAXS); CheckClassAdapter writer = new CheckClassAdapter(out); int offset = 0; declareClass(writer); declareConstructor(writer); for (Method getter : inspector.getters) { offset = declareField(getter, writer, offset); } writer.visitEnd(); return (Class<T>) new GeneratedClassLoader(options).defineClass(implementationName, out); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { checkState(); checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL + Opcodes.ACC_VOLATILE + Opcodes.ACC_TRANSIENT + Opcodes.ACC_SYNTHETIC + Opcodes.ACC_ENUM + Opcodes.ACC_DEPRECATED + 0x40000); // ClassWriter.ACC_SYNTHETIC_ATTRIBUTE CheckMethodAdapter.checkUnqualifiedName(version, name, "field name"); CheckMethodAdapter.checkDesc(desc, false); if (signature != null) { checkFieldSignature(signature); } if (value != null) { CheckMethodAdapter.checkConstant(value); } FieldVisitor av = super .visitField(access, name, desc, signature, value); return new CheckFieldAdapter(av); }
@Override public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { checkState(); checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL + Opcodes.ACC_SYNCHRONIZED checkMethodSignature(signature);
checkState(); checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC checkClassSignature(signature);
if (getChar(signature, 0) == '<') { pos = checkTypeParameters(signature, pos); pos = checkChar('(', signature, pos); while ("ZCBSIFJDL[T".indexOf(getChar(signature, pos)) != -1) { pos = checkJavaTypeSignature(signature, pos); pos = checkChar(')', signature, pos); if (getChar(signature, pos) == 'V') { ++pos; } else { pos = checkJavaTypeSignature(signature, pos); while (getChar(signature, pos) == '^') { ++pos; if (getChar(signature, pos) == 'L') { pos = checkClassTypeSignature(signature, pos); } else { pos = checkTypeVariableSignature(signature, pos);
@Override public void visitInnerClass(final String name, final String outerName, final String innerName, final int access) { checkState(); CheckMethodAdapter.checkInternalName(name, "class name"); if (outerName != null) { CheckMethodAdapter.checkInternalName(outerName, "outer class name"); } if (innerName != null) { int start = 0; while (start < innerName.length() && Character.isDigit(innerName.charAt(start))) { start++; } if (start == 0 || start < innerName.length()) { CheckMethodAdapter.checkIdentifier(innerName, start, -1, "inner class name"); } } checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL + Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC + Opcodes.ACC_ANNOTATION + Opcodes.ACC_ENUM); super.visitInnerClass(name, outerName, innerName, access); }
if (getChar(signature, 0) == '<') { pos = checkFormalTypeParameters(signature, pos); pos = checkChar('(', signature, pos); while ("ZCBSIFJDL[T".indexOf(getChar(signature, pos)) != -1) { pos = checkTypeSignature(signature, pos); pos = checkChar(')', signature, pos); if (getChar(signature, pos) == 'V') { ++pos; } else { pos = checkTypeSignature(signature, pos); while (getChar(signature, pos) == '^') { ++pos; if (getChar(signature, pos) == 'L') { pos = checkClassTypeSignature(signature, pos); } else { pos = checkTypeVariableSignature(signature, pos);