diff options
Diffstat (limited to 'chromium/build/android/bytecode/java/org/chromium')
3 files changed, 0 insertions, 328 deletions
diff --git a/chromium/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java b/chromium/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java index 56b84845c72..4a56332c061 100644 --- a/chromium/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java +++ b/chromium/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java @@ -63,8 +63,6 @@ class ByteCodeProcessor { private static ClassLoader sDirectClassPathClassLoader; private static ClassLoader sFullClassPathClassLoader; private static Set<String> sFullClassPathJarPaths; - private static String sGenerateClassDepsPath; - private static Set<String> sSplitCompatClassNames; private static ClassPathValidator sValidator; private static class EntryDataPair { @@ -138,10 +136,6 @@ class ByteCodeProcessor { chain = new CustomResourcesClassAdapter( chain, reader.getClassName(), reader.getSuperName(), sFullClassPathClassLoader); } - if (!sSplitCompatClassNames.isEmpty()) { - chain = new SplitCompatClassAdapter( - chain, sSplitCompatClassNames, sFullClassPathClassLoader); - } reader.accept(chain, 0); byte[] patchedByteCode = writer.toByteArray(); return EntryDataPair.create(entry.getName(), patchedByteCode); @@ -262,7 +256,6 @@ class ByteCodeProcessor { sShouldUseCustomResources = args[currIndex++].equals("--enable-custom-resources"); sShouldUseThreadAnnotations = args[currIndex++].equals("--enable-thread-annotations"); sShouldCheckClassPath = args[currIndex++].equals("--enable-check-class-path"); - sGenerateClassDepsPath = args[currIndex++]; int sdkJarsLength = Integer.parseInt(args[currIndex++]); List<String> sdkJarPaths = Arrays.asList(Arrays.copyOfRange(args, currIndex, currIndex + sdkJarsLength)); @@ -277,13 +270,6 @@ class ByteCodeProcessor { currIndex += directJarsLength; sDirectClassPathClassLoader = loadJars(directClassPathJarPaths); - // Load list of class names that need to be fixed. - int splitCompatClassNamesLength = Integer.parseInt(args[currIndex++]); - sSplitCompatClassNames = new HashSet<>(); - sSplitCompatClassNames.addAll(Arrays.asList( - Arrays.copyOfRange(args, currIndex, currIndex + splitCompatClassNamesLength))); - currIndex += splitCompatClassNamesLength; - // Load all jars that are on the classpath for the input jar for analyzing class hierarchy. sFullClassPathJarPaths = new HashSet<>(); sFullClassPathJarPaths.clear(); @@ -292,13 +278,6 @@ class ByteCodeProcessor { sFullClassPathJarPaths.addAll( Arrays.asList(Arrays.copyOfRange(args, currIndex, args.length))); - // Write list of references from Java class constant pools to specified output file - // sGenerateClassDepsPath. This is needed for keep rule generation for async DFMs. - if (!sGenerateClassDepsPath.isEmpty()) { - ConstantPoolReferenceReader.writeConstantPoolRefsToFile( - sFullClassPathJarPaths, sGenerateClassDepsPath); - } - sFullClassPathClassLoader = loadJars(sFullClassPathJarPaths); sFullClassPathJarPaths.removeAll(directClassPathJarPaths); diff --git a/chromium/build/android/bytecode/java/org/chromium/bytecode/ConstantPoolReferenceReader.java b/chromium/build/android/bytecode/java/org/chromium/bytecode/ConstantPoolReferenceReader.java deleted file mode 100644 index e2cbd39785d..00000000000 --- a/chromium/build/android/bytecode/java/org/chromium/bytecode/ConstantPoolReferenceReader.java +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright 2019 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.bytecode; - -import org.objectweb.asm.ClassReader; - -import java.io.BufferedInputStream; -import java.io.BufferedWriter; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -/** - * Compiles list of references from all Java .class files in given jar paths by - * reading from the constant pool, and writes this list to an output file. - * This list is used for keep rule generation for maintaining compatibility between - * async DFMs and synchronous modules. - */ -public class ConstantPoolReferenceReader { - private static final String CLASS_FILE_SUFFIX = ".class"; - private static final int BUFFER_SIZE = 16384; - - // Constants representing Java constant pool tags - // See https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4 - private static final int FIELD_REF_TAG = 9; - private static final int METHOD_REF_TAG = 10; - private static final int INTERFACE_METHOD_REF_TAG = 11; - - private static byte[] readAllBytes(InputStream inputStream) throws IOException { - ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - int numRead = 0; - byte[] data = new byte[BUFFER_SIZE]; - while ((numRead = inputStream.read(data, 0, data.length)) != -1) { - buffer.write(data, 0, numRead); - } - return buffer.toByteArray(); - } - - /** - * Given a set of paths, generates references used to produce Proguard keep rules - * necessary for asynchronous DFMs. - * It reads all references stored in constant pools of Java classes from - * the specified jar paths and writes them to an output file. - * References written to the specified file can be converted to a - * corresponding set of Proguard keep rules using the - * constant_pool_refs_to_keep_rules.py script. - * - * @param jarPaths Set of paths specifying Java files to read constant pool - * references from. - * @param outputFilePath File path to write output to. - */ - public static void writeConstantPoolRefsToFile(Set<String> jarPaths, String outputFilePath) { - HashSet<String> classReferences = new HashSet<>(); - - for (String jarPath : jarPaths) { - try (ZipInputStream inputStream = new ZipInputStream( - new BufferedInputStream(new FileInputStream(jarPath)))) { - ZipEntry entry; - while ((entry = inputStream.getNextEntry()) != null) { - if (entry.isDirectory() || !entry.getName().endsWith(CLASS_FILE_SUFFIX)) { - continue; - } - byte[] data = readAllBytes(inputStream); - ClassReader reader = new ClassReader(data); - classReferences.addAll(collectConstantPoolClassReferences(reader)); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - try { - BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath)); - for (String ref : classReferences) { - writer.append(ref); - writer.append("\n"); - } - writer.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** - * Given a ClassReader, return a set of all super classes, implemented interfaces and - * members by reading from the associated class's constant pool. - * - * @param classReader .class file interface for reading the constant pool. - */ - private static Set<String> collectConstantPoolClassReferences(ClassReader classReader) { - char[] charBuffer = new char[classReader.getMaxStringLength()]; - HashSet<String> classReferences = new HashSet<>(); - - classReferences.add(classReader.getSuperName()); - classReferences.addAll(Arrays.asList(classReader.getInterfaces())); - - // According to the Java spec, the constant pool is indexed from 1 to constant_pool_count - - // 1. See https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4 - StringBuilder refInfoString = new StringBuilder(); - for (int i = 1; i < classReader.getItemCount(); i++) { - int offset = classReader.getItem(i); - if (offset <= 0) { - continue; - } - int constantType = classReader.readByte(offset - 1); - if (offset > 0 - && (constantType == METHOD_REF_TAG || constantType == FIELD_REF_TAG - || constantType == INTERFACE_METHOD_REF_TAG)) { - // Read the corresponding class ref and member info from the constant pool. - int classIndex = classReader.readUnsignedShort(offset); - int classStartIndex = classReader.getItem(classIndex); - // Class index is a 2-byte quantity, nameAndTypeIndex is stored sequentially after. - int nameAndTypeIndex = classReader.readUnsignedShort(offset + 2); - int nameAndTypeStartIndex = classReader.getItem(nameAndTypeIndex); - - // Get member's containing class's name, member's name, and member's details (type, - // return type, and argument types). - refInfoString.append(classReader.readUTF8(classStartIndex, charBuffer)); - refInfoString.append(","); - refInfoString.append(classReader.readUTF8(nameAndTypeStartIndex, charBuffer)); - refInfoString.append(","); - refInfoString.append(classReader.readUTF8(nameAndTypeStartIndex + 2, charBuffer)); - - classReferences.add(refInfoString.toString()); - refInfoString.setLength(0); - } - } - - return classReferences; - } -}
\ No newline at end of file diff --git a/chromium/build/android/bytecode/java/org/chromium/bytecode/SplitCompatClassAdapter.java b/chromium/build/android/bytecode/java/org/chromium/bytecode/SplitCompatClassAdapter.java deleted file mode 100644 index 17c271642c0..00000000000 --- a/chromium/build/android/bytecode/java/org/chromium/bytecode/SplitCompatClassAdapter.java +++ /dev/null @@ -1,167 +0,0 @@ -// Copyright 2019 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.bytecode; - -import static org.objectweb.asm.Opcodes.ACC_PROTECTED; -import static org.objectweb.asm.Opcodes.ALOAD; -import static org.objectweb.asm.Opcodes.INVOKEINTERFACE; -import static org.objectweb.asm.Opcodes.INVOKESPECIAL; -import static org.objectweb.asm.Opcodes.INVOKESTATIC; -import static org.objectweb.asm.Opcodes.RETURN; - -import static org.chromium.bytecode.TypeUtils.CONTEXT; -import static org.chromium.bytecode.TypeUtils.VOID; - -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; - -import java.util.Set; - -/** - * A ClassVisitor for injecting ModuleInstaller.initActivity(activity) method call - * into Activity's attachBaseContext() method. The goal is to eventually invoke - * SplitCompat.install() method if running with the binary that has bundle support - * enabled. This needs to happen for activities that were not built with SplitCompat - * support. - */ -class SplitCompatClassAdapter extends ClassVisitor { - private static final String ANDROID_APP_ACTIVITY_CLASS_NAME = "android/app/Activity"; - private static final String ATTACH_BASE_CONTEXT_METHOD_NAME = "attachBaseContext"; - private static final String ATTACH_BASE_CONTEXT_DESCRIPTOR = - TypeUtils.getMethodDescriptor(VOID, CONTEXT); - - private static final String MODULE_INSTALLER_CLASS_NAME = - "org/chromium/components/module_installer/ModuleInstaller"; - private static final String GET_INSTANCE_METHOD_NAME = "getInstance"; - private static final String GET_INSTANCE_DESCRIPTOR = - TypeUtils.getMethodDescriptor(MODULE_INSTALLER_CLASS_NAME); - private static final String INIT_ACTIVITY_METHOD_NAME = "initActivity"; - private static final String INIT_ACTIVITY_DESCRIPTOR = - TypeUtils.getMethodDescriptor(VOID, CONTEXT); - - private boolean mShouldTransform; - - private Set<String> mClassNames; - - private ClassLoader mClassLoader; - - /** - * Creates instance of SplitCompatClassAdapter. - * - * @param visitor - * @param classNames Names of classes into which the attachBaseContext method will be - * injected. Currently, we'll only consider classes for bytecode rewriting only if - * they inherit directly from android.app.Activity & not already contain - * attachBaseContext method. - * @param classLoader - */ - SplitCompatClassAdapter(ClassVisitor visitor, Set<String> classNames, ClassLoader classLoader) { - super(Opcodes.ASM5, visitor); - - mShouldTransform = false; - mClassNames = classNames; - mClassLoader = classLoader; - } - - @Override - public void visit(int version, int access, String name, String signature, String superName, - String[] interfaces) { - super.visit(version, access, name, signature, superName, interfaces); - - if (mClassNames.contains(name)) { - if (!isSubclassOfActivity(name)) { - throw new RuntimeException(name - + " should be transformed but does not inherit from android.app.Activity"); - } - - mShouldTransform = true; - } - } - - @Override - public MethodVisitor visitMethod( - int access, String name, String descriptor, String signature, String[] exceptions) { - // Check if current method matches attachBaseContext & we're supposed to emit code - if so, - // fail. - if (mShouldTransform && name.equals(ATTACH_BASE_CONTEXT_METHOD_NAME)) { - throw new RuntimeException(ATTACH_BASE_CONTEXT_METHOD_NAME + " method already exists"); - } - - return super.visitMethod(access, name, descriptor, signature, exceptions); - } - - @Override - public void visitEnd() { - if (mShouldTransform) { - // If we reached this place, it means we're rewriting a class that inherits from - // Activity and there was no exception thrown due to existence of attachBaseContext - // method - emit code. - emitAttachBaseContext(); - } - - super.visitEnd(); - } - - /** - * Generates: - * - * <pre> - * protected void attachBaseContext(Context base) { - * super.attachBaseContext(base); - * ModuleInstaller.getInstance().initActivity(this); - * } - * </pre> - */ - private void emitAttachBaseContext() { - MethodVisitor mv = super.visitMethod(ACC_PROTECTED, ATTACH_BASE_CONTEXT_METHOD_NAME, - ATTACH_BASE_CONTEXT_DESCRIPTOR, null, null); - mv.visitCode(); - // Push "this" on stack. - mv.visitVarInsn(ALOAD, 0); - // Push first method parameter on stack (Context). - mv.visitVarInsn(ALOAD, 1); - // Pop argument from stack (Context). - // Pop target object from stack ("this"). - // Calls attachBaseContext. - mv.visitMethodInsn(INVOKESPECIAL, ANDROID_APP_ACTIVITY_CLASS_NAME, - ATTACH_BASE_CONTEXT_METHOD_NAME, ATTACH_BASE_CONTEXT_DESCRIPTOR, false); - // Push return value on stack (ModuleInstaller). - // Calls getInstance. - mv.visitMethodInsn(INVOKESTATIC, MODULE_INSTALLER_CLASS_NAME, GET_INSTANCE_METHOD_NAME, - GET_INSTANCE_DESCRIPTOR, true); - // Push "this" on stack. - mv.visitVarInsn(ALOAD, 0); - // Pop argument from stack ("this"). - // Pop target object from stack (ModuleInstaller). - // Calls initActivity. - mv.visitMethodInsn(INVOKEINTERFACE, MODULE_INSTALLER_CLASS_NAME, INIT_ACTIVITY_METHOD_NAME, - INIT_ACTIVITY_DESCRIPTOR, true); - mv.visitInsn(RETURN); - // Max stack size = 2 (Only push at most 2 before popping). - // Max locals = 2 ("this" and 1 parameter). - mv.visitMaxs(2, 2); - mv.visitEnd(); - } - - /** - * Checks whether passed in class inherits from android.app.Activity. - * @param name Name of the class to be checked. - * @return true if class inherits from android.app.Activity, false otherwise. - */ - private boolean isSubclassOfActivity(String name) { - Class<?> activityClass = loadClass(ANDROID_APP_ACTIVITY_CLASS_NAME); - Class<?> candidateClass = loadClass(name); - return activityClass.isAssignableFrom(candidateClass); - } - - private Class<?> loadClass(String className) { - try { - return mClassLoader.loadClass(className.replace('/', '.')); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } -} |