From 73ac91764e5d4d5df06d262b8e2f43c2f9962237 Mon Sep 17 00:00:00 2001 From: yolandyan Date: Wed, 16 Nov 2016 22:54:34 -0800 Subject: [PATCH] Revert of Add GN build rules to allow java_assertion_enabler to enable Java asserts. (patchset #2 id:140001 of https://codereview.chromium.org/2506013004/ ) Reason for revert: This CL cause WebView to crash BUG=666193 Original issue's description: > Reland of Add GN build rules to allow java_assertion_enabler to enable Java asserts. > > Reverted in: > https://codereview.chromium.org/2507153002/ > > Reason for reland: > Now explicitly lists default toolchain > > Also modify java_assertion_enabler to resolve cycle dependency issue > and empty jar issue. > > TBR=jbudorick@chromium.org,agrieve@chromium.org,toyoshim@chromium.org,qinmin@chromium.org > BUG=462676,665157,665478 > > Committed: https://crrev.com/95785a8601992a057837bc189e9f7eba61701f1e > Cr-Commit-Position: refs/heads/master@{#432607} TBR=jbudorick@chromium.org,agrieve@chromium.org,toyoshim@chromium.org,qinmin@chromium.org,zpeng@chromium.org # Skipping CQ checks because original CL landed less than 1 days ago. NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true BUG=462676,665157,665478 Review-Url: https://codereview.chromium.org/2506263003 Cr-Original-Commit-Position: refs/heads/master@{#432789} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 5e5387276fc07873f2a2c730b7b1adbd0d0a70ca --- android/java_assertion_enabler/BUILD.gn | 5 +- .../AssertionEnabler.java | 81 ++++------- config/android/internal_rules.gni | 132 ++++++------------ 3 files changed, 75 insertions(+), 143 deletions(-) diff --git a/android/java_assertion_enabler/BUILD.gn b/android/java_assertion_enabler/BUILD.gn index 7d819c054..e5c896b41 100644 --- a/android/java_assertion_enabler/BUILD.gn +++ b/android/java_assertion_enabler/BUILD.gn @@ -4,14 +4,13 @@ import("//build/config/android/rules.gni") -assert(current_toolchain == default_toolchain) - java_binary("java_assertion_enabler") { + testonly = true java_files = [ "java/org/chromium/javaassertionenabler/AssertionEnabler.java" ] main_class = "org.chromium.javaassertionenabler.AssertionEnabler" deps = [ + "//third_party/guava:guava_java", "//third_party/ow2_asm:asm_java", ] - wrapper_script_name = "helper/java_assertion_enabler" } diff --git a/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java b/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java index beb26facd..e800a3941 100644 --- a/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java +++ b/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java @@ -4,6 +4,8 @@ package org.chromium.javaassertionenabler; +import com.google.common.io.ByteStreams; + import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.ClassWriter; @@ -12,30 +14,21 @@ import org.objectweb.asm.Opcodes; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; +import java.util.Collections; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; /** * An application that enables Java ASSERT statements by modifying Java bytecode. It takes in a JAR * file, modifies bytecode of classes that use ASSERT, and outputs the bytecode to a new JAR file. */ class AssertionEnabler { - static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled"; static final String CLASS_FILE_SUFFIX = ".class"; static final String STATIC_INITIALIZER_NAME = ""; - static final String TEMPORARY_FILE_SUFFIX = ".temp"; - - static final int BUFFER_SIZE = 16384; + static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled"; static class AssertionEnablerVisitor extends ClassVisitor { AssertionEnablerVisitor(ClassWriter writer) { @@ -75,53 +68,33 @@ class AssertionEnabler { } } - 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(); - } - static void enableAssertionInJar(String inputJarPath, String outputJarPath) { - String tempJarPath = outputJarPath + TEMPORARY_FILE_SUFFIX; - try (ZipInputStream inputStream = new ZipInputStream( - new BufferedInputStream(new FileInputStream(inputJarPath))); - ZipOutputStream tempStream = new ZipOutputStream( - new BufferedOutputStream(new FileOutputStream(tempJarPath)))) { - ZipEntry entry = null; + try (JarOutputStream outputStream = new JarOutputStream( + new BufferedOutputStream(new FileOutputStream(outputJarPath)))) { + JarFile jarFile = new JarFile(inputJarPath); + for (JarEntry entry : Collections.list(jarFile.entries())) { + try (BufferedInputStream inputStream = new BufferedInputStream( + jarFile.getInputStream(entry))) { + byte[] byteCode = ByteStreams.toByteArray(inputStream); - while ((entry = inputStream.getNextEntry()) != null) { - byte[] byteCode = readAllBytes(inputStream); - - if (entry.isDirectory() || !entry.getName().endsWith(CLASS_FILE_SUFFIX)) { - tempStream.putNextEntry(entry); - tempStream.write(byteCode); - tempStream.closeEntry(); - continue; + if (entry.isDirectory() || !entry.getName().endsWith(CLASS_FILE_SUFFIX)) { + outputStream.putNextEntry(entry); + outputStream.write(byteCode); + outputStream.closeEntry(); + continue; + } + ClassReader reader = new ClassReader(byteCode); + ClassWriter writer = new ClassWriter(reader, 0); + reader.accept(new AssertionEnablerVisitor(writer), 0); + byte[] patchedByteCode = writer.toByteArray(); + outputStream.putNextEntry(new JarEntry(entry.getName())); + outputStream.write(patchedByteCode); + outputStream.closeEntry(); } - ClassReader reader = new ClassReader(byteCode); - ClassWriter writer = new ClassWriter(reader, 0); - reader.accept(new AssertionEnablerVisitor(writer), 0); - byte[] patchedByteCode = writer.toByteArray(); - tempStream.putNextEntry(new ZipEntry(entry.getName())); - tempStream.write(patchedByteCode); - tempStream.closeEntry(); } } catch (IOException e) { throw new RuntimeException(e); } - - try { - Path src = Paths.get(tempJarPath); - Path dest = Paths.get(outputJarPath); - Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING); - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } } public static void main(String[] args) { diff --git a/config/android/internal_rules.gni b/config/android/internal_rules.gni index 1171e2a0a..585b44768 100644 --- a/config/android/internal_rules.gni +++ b/config/android/internal_rules.gni @@ -6,7 +6,6 @@ # Some projects (e.g. V8) do not have non-build directories DEPS'ed in. import("//build_overrides/build.gni") import("//build/config/android/config.gni") -import("//build/config/dcheck_always_on.gni") import("//build/config/sanitizers/sanitizers.gni") assert(is_android) @@ -1015,11 +1014,10 @@ if (enable_java_templates) { _rebased_build_config = rebase_path(_build_config, root_build_dir) assert(_rebased_build_config != "" || true) # Mark used. - _input_jar_path = invoker.input_jar_path - _output_jar_path = invoker.output_jar_path - _proguard_preprocess = defined(invoker.proguard_preprocess) && invoker.proguard_preprocess + _input_jar_path = invoker.input_jar_path + _output_jar_path = invoker.output_jar_path _jar_excluded_patterns = [] if (defined(invoker.jar_excluded_patterns)) { @@ -1029,15 +1027,24 @@ if (enable_java_templates) { invoker.strip_resource_classes _filter_jar = _jar_excluded_patterns != [] || _strip_resource_classes - _enable_assert = - defined(invoker.supports_android) && invoker.supports_android && - (is_java_debug || dcheck_always_on) - assert(_enable_assert || true) # Mark used. - if (_filter_jar) { _filter_target = "${target_name}__filter" + _output_jar_target = _filter_target + } + if (_proguard_preprocess) { + _proguard_target = "${target_name}__proguard_process" + _output_jar_target = _proguard_target + } + if (!_filter_jar && !_proguard_preprocess) { + _copy_target = "${target_name}__copy" + _output_jar_target = _copy_target + } - _filter_jar_path = "$target_out_dir/$target_name-filtered.jar" + if (_filter_jar) { + _filtered_jar_path = _output_jar_path + if (_proguard_preprocess) { + _filtered_jar_path = "$target_out_dir/$target_name-filtered.jar" + } action(_filter_target) { script = "//build/android/gyp/jar.py" forward_variables_from(invoker, @@ -1050,13 +1057,13 @@ if (enable_java_templates) { _input_jar_path, ] outputs = [ - _filter_jar_path, + _filtered_jar_path, ] args = [ "--input-jar", rebase_path(_input_jar_path, root_build_dir), "--jar-path", - rebase_path(_filter_jar_path, root_build_dir), + rebase_path(_filtered_jar_path, root_build_dir), "--excluded-classes=$_jar_excluded_patterns", ] if (_strip_resource_classes) { @@ -1066,83 +1073,15 @@ if (enable_java_templates) { } if (_proguard_preprocess) { - _output_jar_target = "${target_name}__proguard_process" - _proguard_output_jar = _output_jar_path _proguard_config_path = invoker.proguard_preprocess_config - proguard(_output_jar_target) { + proguard(_proguard_target) { if (_filter_jar) { - _proguard_input_jar = _filter_jar_path - deps = [ - ":$_filter_target", - ] - } else { - _proguard_input_jar = _input_jar_path - deps = [] - } - if (defined(invoker.deps)) { - deps += invoker.deps - } - if (defined(invoker.public_deps)) { - public_deps = invoker.public_deps - } - inputs = [ - _build_config, - _proguard_config_path, - _proguard_input_jar, - ] - output_jar_path = _proguard_output_jar - - _rebased_input_paths = - [ rebase_path(_proguard_input_jar, root_build_dir) ] - _rebased_proguard_configs = - [ rebase_path(_proguard_config_path, root_build_dir) ] - args = [ - "--input-paths=$_rebased_input_paths", - "--proguard-configs=$_rebased_proguard_configs", - "--classpath=@FileArg($_rebased_build_config:javac:classpath)", - ] - } - } else if (_enable_assert) { - _output_jar_target = "${target_name}__assert" - _assert_output_jar = _output_jar_path - action(_output_jar_target) { - script = "$root_out_dir/bin/helper/java_assertion_enabler" - deps = [ - "//build/android/java_assertion_enabler($default_toolchain)", - ] - if (_filter_jar) { - _assert_input_jar = _filter_jar_path - deps += [ ":$_filter_target" ] - } else { - _assert_input_jar = _input_jar_path - } - if (defined(invoker.deps)) { - deps += invoker.deps - } - if (defined(invoker.public_deps)) { - public_deps = invoker.public_deps - } - inputs = [ - _assert_input_jar, - ] - outputs = [ - _assert_output_jar, - ] - args = [ - rebase_path(_assert_input_jar, root_build_dir), - rebase_path(_assert_output_jar, root_build_dir), - ] - } - } else { - _output_jar_target = "${target_name}__copy" - copy(_output_jar_target) { - if (_filter_jar) { - _copy_input_jar = _filter_jar_path + _proguard_input_jar = _filtered_jar_path public_deps = [ ":$_filter_target", ] } else { - _copy_input_jar = _input_jar_path + _proguard_input_jar = _input_jar_path public_deps = [] } if (defined(invoker.deps)) { @@ -1151,8 +1090,31 @@ if (enable_java_templates) { if (defined(invoker.public_deps)) { public_deps += invoker.public_deps } + inputs = [ + _build_config, + _proguard_config_path, + _proguard_input_jar, + ] + output_jar_path = _output_jar_path + + _rebased_input_paths = [ rebase_path(_input_jar_path, root_build_dir) ] + _rebased_proguard_configs = + [ rebase_path(_proguard_config_path, root_build_dir) ] + args = [ + "--input-paths=$_rebased_input_paths", + "--proguard-configs=$_rebased_proguard_configs", + "--classpath=@FileArg($_rebased_build_config:javac:classpath)", + ] + } + } else if (!_filter_jar) { + copy(_copy_target) { + forward_variables_from(invoker, + [ + "deps", + "public_deps", + ]) sources = [ - _copy_input_jar, + _input_jar_path, ] outputs = [ _output_jar_path, @@ -1848,7 +1810,6 @@ if (enable_java_templates) { visibility += [ ":$_dex_target_name" ] } - supports_android = _supports_android build_config = _build_config input_jar_path = invoker.jar_path output_jar_path = _jar_path @@ -2140,7 +2101,6 @@ if (enable_java_templates) { "proguard_preprocess", "proguard_preprocess_config", ]) - supports_android = _supports_android build_config = _build_config input_jar_path = _javac_jar_path output_jar_path = _process_prebuilt_jar_path