Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit 59f1ee0

Browse files
katherine95sYorkShen
authored andcommitted
[Android] Support for ASAN (#2862)
1 parent e4740f5 commit 59f1ee0

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

android/sdk/build.gradle

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ android {
151151
'-DANDROID_PROJECT_DIR=' + "${android_project_dir}",
152152
'-DENABLE_ASAN=false',
153153
'-DBUILD_RUNTIME_API='+"${buildRuntimeApi}"
154+
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
155+
cppFlags "-fsanitize=address -fno-omit-frame-pointer"
156+
}
154157
}
155158
}
156159
}
@@ -264,11 +267,10 @@ if(file('../license/LICENSE').exists()){
264267
}
265268
preBuild.dependsOn licenseFormat
266269
}
267-
270+
def ndkDir = ''
268271
task checkNdkVersion() {
269272
def rootDir = project.rootDir
270273
def localProperties = new File(rootDir, "local.properties")
271-
def ndkDir = ''
272274
if (localProperties.exists()) {
273275
Properties properties = new Properties()
274276
localProperties.withInputStream { instr ->
@@ -360,7 +362,45 @@ artifactory {
360362
}
361363
}
362364
}
363-
365+
def asanAbi = project.hasProperty('asanAbi') ? project.getProperty('asanAbi') : 'arm64-v8a'
366+
task clearASanLibs(type: Delete){
367+
delete project.android.sourceSets.main.resources.srcDirs
368+
delete fileTree(project.android.sourceSets.main.jniLibs.srcDirs[-1]) {
369+
include '**/libclang_rt.asan-*-android.so'
370+
}
371+
}
372+
task copyWrapScript(type: Copy,dependsOn: clearASanLibs) {
373+
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
374+
from '../../scripts/wrap.sh'
375+
into new File(project.android.sourceSets.main.resources.srcDirs[-1], "lib")
376+
eachFile {
377+
it.path = "${asanAbi}/${it.name}"
378+
}
379+
}
380+
}
381+
task copyASanLib(type: Copy,dependsOn: copyWrapScript){
382+
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
383+
def ndkPath = ndkDir == '' ? System.getenv("ANDROID_NDK_HOME"):ndkDir
384+
def dir = ndkPath + '/toolchains/llvm/prebuilt/'
385+
def renamedAbi = asanAbi
386+
if (asanAbi == "armeabi-v7a" || asanAbi == "armeabi")
387+
renamedAbi = "arm"
388+
if (asanAbi == "arm64-v8a")
389+
renamedAbi = "aarch64"
390+
if (asanAbi == "x86")
391+
renamedAbi = "i686"
392+
new File(dir).eachFileRecurse { file ->
393+
if (file.name == 'libclang_rt.asan-' + renamedAbi + '-android.so')
394+
from file.absolutePath
395+
into project.android.sourceSets.main.jniLibs.srcDirs[-1]
396+
eachFile {
397+
it.path = "${asanAbi}/${it.name}"
398+
}
399+
includeEmptyDirs = false
400+
}
401+
}
402+
}
403+
preBuild.dependsOn copyASanLib
364404

365405
afterEvaluate { project ->
366406
transformNativeLibsWithStripDebugSymbolForRelease << {

scripts/wrap.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/system/bin/sh
2+
HERE="$(cd "$(dirname "$0")" && pwd)"
3+
export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1,use_sigaltstack=0
4+
ASAN_LIB=$(ls $HERE/libclang_rt.asan-*-android.so)
5+
if [ -f "$HERE/libc++_shared.so" ]; then
6+
# Workaround for https://github.com/android-ndk/ndk/issues/988.
7+
export LD_PRELOAD="$ASAN_LIB $HERE/libc++_shared.so"
8+
else
9+
export LD_PRELOAD="$ASAN_LIB"
10+
fi
11+
"$@"

0 commit comments

Comments
 (0)