起因
android启用asan编译后,碰到在jni里面crash的问题,任何一个jni api都会crash,然后根据错误搜索后,发现下面这篇讲了编译jdk的办法,然后就尝试编译openjdk10,最后编译成功的是openjdk11-1,简单记录下过程,说不定也会有人碰到类似问题。
How to use AddressSanitizer with Java
[ https://blog.gypsyengineer.com/en/security/running-java-with-addresssanitizer.html]
From now OpenJDK can be built with enabled AddressSanitizer. Support
for AddressSanitizer has been recently added to OpenJDK configure/make
files: http://hg.openjdk.java.net/jdk10/master/rev/77a5f2ef1807
openjdk10 build
下载代码
$ git clone https://github.com/openjdk/jdk.git
$ git checkout jdk-10+10
warning: refname 'jdk-10+10' is ambiguous.
Checking out files: 100% (13089/13089), done.
Switched to branch 'jdk-10+10'
下载openjdk-9.0.4作为boot-jdk
wget https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz
tar xzvf openjdk-9.0.4_linux-x64_bin.tar.gz
编译
configure
configure时报错,发现不支持–enable-asan,然后就切换到jdk-11+1,git tag就可以查看都有那些版本。
$ bash configure --with-boot-jdk=./jdk-9.0.4 -with-debug-level=slowdebug --enable-asan
Running generated-configure.sh
configure: error: unrecognized options: --enable-asan
configure exiting with result code 1
jdk11支持enable-asan
git tag可以看到不同的tag版本,选择jdk-11+1试试,发现支持ASAN
git tag
jdk-10+8
jdk-10+9
jdk-11+0
jdk-11+1
选择jdk-11+1
git checkout jdk-11+1
git checkout -b jdk-11+1
bash configure --with-boot-jdk=./jdk-9.0.4 --with-debug-level=slowdebug --enable-asan
make images
直接编译,报错,需要ffmpeg,随之编译ffmpeg
=== Output from failing command(s) repeated here ===
* For target buildtools_langtools_tools_classes__the.BUILD_TOOLS_LANGTOOLS_batch:
warning: [path] bad path element "/usr/lib/jvm/java-1.11.0-openjdk-amd64/jre/lib": no such file or directory
warning: [path] bad path element "/usr/local/ffmpeg/lib": no such file or directory
warning: [path] bad path element "/usr/local/ffmpeg/bin": no such file or directory
error: warnings found and -Werror specified
1 error
3 warnings
编译ffmpeg
./configure --prefix=/usr/local/ffmpeg --enable-shared --enable-static \
--enable-gpl --enable-sdl --enable-ffplay
继续编译报错
=== Output from failing command(s) repeated here ===
* For target buildtools_langtools_tools_classes__the.BUILD_TOOLS_LANGTOOLS_batch:
warning: [path] bad path element "/usr/lib/jvm/java-1.11.0-openjdk-amd64/jre/lib":\
no such file or directory
error: warnings found and -Werror specified
1 error
1 warning
创建目录后OK
$ sudo mkdir -p /usr/lib/jvm/java-1.11.0-openjdk-amd64/jre/lib
再次make images
Building target 'images' in configuration 'linux-x86_64-normal-server-slowdebug'
Warning: No mercurial configuration present and no .src-rev
Compiling 8 files for BUILD_TOOLS_LANGTOOLS
Updating support/modules_libs/java.base/libjsig.so due to 1 file(s)
Updating hotspot/variant-server/tools/adlc/adlc due to 13 file(s)
Compiling 2 files for BUILD_JVMTI_TOOLS
Compiling 16 properties into resource bundles for jdk.compiler
Compiling 13 properties into resource bundles for jdk.javadoc
Parsing 1 properties into enum-like class for jdk.compiler
Compiling 12 properties into resource bundles for jdk.jdeps
Compiling 7 properties into resource bundles for jdk.jshell
Compiling 117 files for BUILD_java.compiler.interim
Compiling 385 files for BUILD_jdk.compiler.interim
Creating support/modules_libs/java.base/server/libjvm.so from 728 file(s)
Creating hotspot/variant-server/libjvm/gtest/libjvm.so from 78 file(s)
Creating hotspot/variant-server/libjvm/gtest/gtestLauncher from 1 file(s)
Compiling 298 files for BUILD_jdk.javadoc.interim
Compiling 188 files for BUILD_jdk.rmic.interim
Compiling 160 files for BUILD_TOOLS_JDK
Compiling 2 files for COMPILE_DEPEND
Compiling 31 files for BUILD_JRTFS
Compiling 957 files for BUILD_VM_COMPILER_MATCH_PROCESSOR
Compiling 9 files for BUILD_VM_COMPILER_NODEINFO_PROCESSOR
Creating support/modules_libs/java.base/jrt-fs.jar
Compiling 24 files for BUILD_VM_COMPILER_OPTIONS_PROCESSOR
Compiling 333 files for BUILD_VM_COMPILER_REPLACEMENTS_VERIFIER
Compiling 8 files for BUILD_VM_COMPILER_SERVICEPROVIDER_PROCESSOR
Creating buildtools/jdk.vm.compiler.serviceprovider.processor.jar
Creating buildtools/jdk.vm.compiler.nodeinfo.processor.jar
Creating buildtools/jdk.vm.compiler.options.processor.jar
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Compiling 11 properties into resource bundles for java.logging
Compiling 2 files for BUILD_BREAKITERATOR_BASE
Compiling 2 files for BUILD_BREAKITERATOR_LD
Creating buildtools/jdk.vm.compiler.replacements.verifier.jar
Compiling 11 properties into resource bundles for jdk.jartool
Compiling 11 properties into resource bundles for jdk.management.agent
Compiling 3 properties into resource bundles for jdk.jdi
Creating buildtools/jdk.vm.compiler.match.processor.jar
Compiling 4 properties into resource bundles for jdk.jlink
Compiling 3 properties into resource bundles for jdk.jlink
Compiling 1 properties into resource bundles for jdk.jlink
Compiling 224 properties into resource bundles for jdk.localedata
Compiling 11 properties into resource bundles for java.base
Compiling 6 properties into resource bundles for java.base
Compiling 2921 files for java.base
Compiling 89 properties into resource bundles for java.desktop
configure选项
configure项碰到编译错误的时候不断地添加参数才完成编译的。
bash configure --with-boot-jdk=./jdk-9.0.4 -with-debug-level=slowdebug \
--enable-asan --with-extra-cflags=-Werror=return-type
bash configure --with-boot-jdk=./jdk-9.0.4 -with-debug-level=slowdebug \
--enable-asan --with-extra-cflags=-Wno-error=return-type
bash configure --with-boot-jdk=./jdk-9.0.4 -with-debug-level=slowdebug \
--enable-asan --with-extra-cflags=-Wno-error=return-type \
--with-extra-cflags=-Wno-error=deprecated-declarations
特殊错误
ERROR: Invalid value for bool option: '0"'
ASAN_OPTIONS多了引号报错,去掉引号就好了,参考 (https://bugs.openjdk.java.net/browse/JDK-8223667)
--- a/make/autoconf/spec.gmk.in Wed May 08 15:36:10 2019 -0700
+++ b/make/autoconf/spec.gmk.in Fri May 10 17:47:42 2019 -0700
@@ -395,7 +395,7 @@
export ASAN_ENABLED:=@ASAN_ENABLED@
export DEVKIT_LIB_DIR:=@DEVKIT_LIB_DIR@
ifeq ($(ASAN_ENABLED), yes)
- export ASAN_OPTIONS="handle_segv=0 detect_leaks=0"
+ export ASAN_OPTIONS=handle_segv=0 detect_leaks=0
ifneq ($(DEVKIT_LIB_DIR),)
export LD_LIBRARY_PATH:=$(LD_LIBRARY_PATH):$(DEVKIT_LIB_DIR)
endif
最后make images就可以编译成功了。