Commit 0ff6e692 authored by Aiden Benner's avatar Aiden Benner Committed by Commit Bot

Enable hashing of proxy natives when !is_java_debug

If is_java_debug is set to false the JNI generator and
JNI annotation processor will hash/obfuscate native method
declarations to save binary size.

Change-Id: Ic083a7fe6564665ed0ffa0a5e508b9d75697f6b3
Reviewed-on: https://chromium-review.googlesource.com/c/1318394
Commit-Queue: Aiden Benner <abenner@google.com>
Reviewed-by: default avatarEric Stevenson <estevenson@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609366}
parent 7d9dd5f4
...@@ -80,6 +80,14 @@ group("jni_generator_tests") { ...@@ -80,6 +80,14 @@ group("jni_generator_tests") {
] ]
} }
java_cpp_template("processor_args_java") {
package_path = "org/chromium/jni_generator"
sources = [
"ProcessorArgs.template",
]
defines = [ "IS_JAVA_DEBUG_VALUE=$is_java_debug" ]
}
java_annotation_processor("jni_processor") { java_annotation_processor("jni_processor") {
java_files = [ "java/src/org/chromium/jni_generator/JniProcessor.java" ] java_files = [ "java/src/org/chromium/jni_generator/JniProcessor.java" ]
...@@ -93,4 +101,6 @@ java_annotation_processor("jni_processor") { ...@@ -93,4 +101,6 @@ java_annotation_processor("jni_processor") {
"//third_party/auto:auto_service_java", "//third_party/auto:auto_service_java",
"//third_party/guava:guava_java", "//third_party/guava:guava_java",
] ]
srcjar_deps = [ ":processor_args_java" ]
} }
// Copyright 2018 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.jni_generator;
class ProcessorArgs {
public static final boolean IS_JAVA_DEBUG = IS_JAVA_DEBUG_VALUE;
}
...@@ -75,7 +75,7 @@ public class JniProcessor extends AbstractProcessor { ...@@ -75,7 +75,7 @@ public class JniProcessor extends AbstractProcessor {
private static MessageDigest sNativeMethodHashFunction; private static MessageDigest sNativeMethodHashFunction;
// If true, native methods in GEN_JNI will be named as a hash of their descriptor. // If true, native methods in GEN_JNI will be named as a hash of their descriptor.
private static final boolean USE_HASH_FOR_METHODS = false; private static final boolean USE_HASH_FOR_METHODS = !ProcessorArgs.IS_JAVA_DEBUG;
// Limits the number characters of the Base64 encoded hash // Limits the number characters of the Base64 encoded hash
// of the method descriptor used as name of the generated // of the method descriptor used as name of the generated
......
...@@ -10,6 +10,7 @@ RegisterNonMainDexNatives(). Together, these will use manual JNI registration ...@@ -10,6 +10,7 @@ RegisterNonMainDexNatives(). Together, these will use manual JNI registration
to register all native methods that exist within an application.""" to register all native methods that exist within an application."""
import argparse import argparse
import functools
import multiprocessing import multiprocessing
import os import os
import string import string
...@@ -34,7 +35,11 @@ MERGEABLE_KEYS = [ ...@@ -34,7 +35,11 @@ MERGEABLE_KEYS = [
] ]
def _Generate(java_file_paths, srcjar_path, header_path=None, namespace=''): def _Generate(java_file_paths,
srcjar_path,
use_proxy_hash=False,
header_path=None,
namespace=''):
"""Generates files required to perform JNI registration. """Generates files required to perform JNI registration.
Generates a srcjar containing a single class, GEN_JNI, that contains all Generates a srcjar containing a single class, GEN_JNI, that contains all
...@@ -53,34 +58,39 @@ def _Generate(java_file_paths, srcjar_path, header_path=None, namespace=''): ...@@ -53,34 +58,39 @@ def _Generate(java_file_paths, srcjar_path, header_path=None, namespace=''):
# Without multiprocessing, script takes ~13 seconds for chrome_public_apk # Without multiprocessing, script takes ~13 seconds for chrome_public_apk
# on a z620. With multiprocessing, takes ~2 seconds. # on a z620. With multiprocessing, takes ~2 seconds.
pool = multiprocessing.Pool() pool = multiprocessing.Pool()
results = [d for d in pool.imap_unordered(_DictForPath, java_file_paths) if d]
results = []
for d in pool.imap_unordered(
functools.partial(_DictForPath, use_proxy_hash=use_proxy_hash),
java_file_paths):
if d:
results.append(d)
pool.close() pool.close()
# Sort to make output deterministic. # Sort to make output deterministic.
results.sort(key=lambda d: d['FULL_CLASS_NAME']) results.sort(key=lambda d: d['FULL_CLASS_NAME'])
if header_path:
combined_dict = {} combined_dict = {}
for key in MERGEABLE_KEYS: for key in MERGEABLE_KEYS:
combined_dict[key] = ''.join(d.get(key, '') for d in results) combined_dict[key] = ''.join(d.get(key, '') for d in results)
if header_path:
combined_dict['HEADER_GUARD'] = \ combined_dict['HEADER_GUARD'] = \
os.path.splitext(header_path)[0].replace('/', '_').upper() + '_' os.path.splitext(header_path)[0].replace('/', '_').upper() + '_'
combined_dict['NAMESPACE'] = namespace combined_dict['NAMESPACE'] = namespace
header_content = CreateFromDict(combined_dict) header_content = CreateFromDict(combined_dict)
with build_utils.AtomicOutput(header_path) as f: with build_utils.AtomicOutput(header_path) as f:
f.write(header_content) f.write(header_content)
with build_utils.AtomicOutput(srcjar_path) as f: with build_utils.AtomicOutput(srcjar_path) as f:
with zipfile.ZipFile(f, 'w') as srcjar: with zipfile.ZipFile(f, 'w') as srcjar:
# TODO(abenner): Write GEN_JNI.java here. build_utils.AddToZipHermetic(
# build_utils.AddToZipHermetic(srcjar, 'org/chromium/base/GEN_JNI.java', srcjar,
# data='$CONTENT') 'org/chromium/base/natives/GEN_JNI.java',
pass data=CreateProxyJavaFromDict(combined_dict))
def _DictForPath(path): def _DictForPath(path, use_proxy_hash=False):
with open(path) as f: with open(path) as f:
contents = jni_generator.RemoveComments(f.read()) contents = jni_generator.RemoveComments(f.read())
if '@JniIgnoreNatives' in contents: if '@JniIgnoreNatives' in contents:
...@@ -93,7 +103,8 @@ def _DictForPath(path): ...@@ -93,7 +103,8 @@ def _DictForPath(path):
natives += jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives( natives += jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
fully_qualified_class=fully_qualified_class, fully_qualified_class=fully_qualified_class,
contents=contents, contents=contents,
ptr_type='long') ptr_type='long',
use_hash=use_proxy_hash)
if len(natives) == 0: if len(natives) == 0:
return None return None
namespace = jni_generator.ExtractJNINamespace(contents) namespace = jni_generator.ExtractJNINamespace(contents)
...@@ -516,6 +527,11 @@ def main(argv): ...@@ -516,6 +527,11 @@ def main(argv):
default='', default='',
help='Namespace to wrap the registration functions ' help='Namespace to wrap the registration functions '
'into.') 'into.')
arg_parser.add_argument(
'--use_proxy_hash',
action='store_true',
help='Enables hashing of the native declaration '
'for methods in an @JniNatives interface')
args = arg_parser.parse_args(build_utils.ExpandFileArgs(argv[1:])) args = arg_parser.parse_args(build_utils.ExpandFileArgs(argv[1:]))
args.sources_files = build_utils.ParseGnList(args.sources_files) args.sources_files = build_utils.ParseGnList(args.sources_files)
...@@ -529,6 +545,7 @@ def main(argv): ...@@ -529,6 +545,7 @@ def main(argv):
_Generate( _Generate(
java_file_paths, java_file_paths,
args.srcjar_path, args.srcjar_path,
use_proxy_hash=args.use_proxy_hash,
header_path=args.header_path, header_path=args.header_path,
namespace=args.namespace) namespace=args.namespace)
......
...@@ -249,6 +249,10 @@ if (enable_java_templates) { ...@@ -249,6 +249,10 @@ if (enable_java_templates) {
rebase_path(_jni_generator_include, _jni_output_dir), rebase_path(_jni_generator_include, _jni_output_dir),
] ]
if (!is_java_debug) {
args += [ "--use_proxy_hash" ]
}
if (enable_profiling) { if (enable_profiling) {
args += [ "--enable_profiling" ] args += [ "--enable_profiling" ]
} }
...@@ -435,6 +439,17 @@ if (enable_java_templates) { ...@@ -435,6 +439,17 @@ if (enable_java_templates) {
"--depfile", "--depfile",
rebase_path(depfile, root_build_dir), rebase_path(depfile, root_build_dir),
] ]
if (!is_java_debug) {
args += [ "--use_proxy_hash" ]
}
if (defined(invoker.exception_files)) {
_rebase_exception_java_files =
rebase_path(invoker.exception_files, root_build_dir)
args += [ "--no_register_java=$_rebase_exception_java_files" ]
}
if (defined(invoker.header_output)) { if (defined(invoker.header_output)) {
outputs += [ invoker.header_output ] outputs += [ invoker.header_output ]
args += [ args += [
...@@ -442,11 +457,13 @@ if (enable_java_templates) { ...@@ -442,11 +457,13 @@ if (enable_java_templates) {
rebase_path(invoker.header_output, root_build_dir), rebase_path(invoker.header_output, root_build_dir),
] ]
} }
if (defined(invoker.sources_blacklist)) { if (defined(invoker.sources_blacklist)) {
_rebase_sources_blacklist = _rebase_sources_blacklist =
rebase_path(invoker.sources_blacklist, root_build_dir) rebase_path(invoker.sources_blacklist, root_build_dir)
args += [ "--sources-blacklist=$_rebase_sources_blacklist" ] args += [ "--sources-blacklist=$_rebase_sources_blacklist" ]
} }
if (defined(invoker.namespace)) { if (defined(invoker.namespace)) {
args += [ "--namespace=${invoker.namespace}" ] args += [ "--namespace=${invoker.namespace}" ]
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment