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

Commit

Permalink
[Android] Support for ASAN (#2862)
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine95s authored and YorkShen committed Aug 30, 2019
1 parent e4740f5 commit 59f1ee0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
46 changes: 43 additions & 3 deletions android/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ android {
'-DANDROID_PROJECT_DIR=' + "${android_project_dir}",
'-DENABLE_ASAN=false',
'-DBUILD_RUNTIME_API='+"${buildRuntimeApi}"
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
cppFlags "-fsanitize=address -fno-omit-frame-pointer"
}
}
}
}
Expand Down Expand Up @@ -264,11 +267,10 @@ if(file('../license/LICENSE').exists()){
}
preBuild.dependsOn licenseFormat
}

def ndkDir = ''
task checkNdkVersion() {
def rootDir = project.rootDir
def localProperties = new File(rootDir, "local.properties")
def ndkDir = ''
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
Expand Down Expand Up @@ -360,7 +362,45 @@ artifactory {
}
}
}

def asanAbi = project.hasProperty('asanAbi') ? project.getProperty('asanAbi') : 'arm64-v8a'
task clearASanLibs(type: Delete){
delete project.android.sourceSets.main.resources.srcDirs
delete fileTree(project.android.sourceSets.main.jniLibs.srcDirs[-1]) {
include '**/libclang_rt.asan-*-android.so'
}
}
task copyWrapScript(type: Copy,dependsOn: clearASanLibs) {
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
from '../../scripts/wrap.sh'
into new File(project.android.sourceSets.main.resources.srcDirs[-1], "lib")
eachFile {
it.path = "${asanAbi}/${it.name}"
}
}
}
task copyASanLib(type: Copy,dependsOn: copyWrapScript){
if(project.hasProperty('enableASan') && "true" == project.getProperty('enableASan')) {
def ndkPath = ndkDir == '' ? System.getenv("ANDROID_NDK_HOME"):ndkDir
def dir = ndkPath + '/toolchains/llvm/prebuilt/'
def renamedAbi = asanAbi
if (asanAbi == "armeabi-v7a" || asanAbi == "armeabi")
renamedAbi = "arm"
if (asanAbi == "arm64-v8a")
renamedAbi = "aarch64"
if (asanAbi == "x86")
renamedAbi = "i686"
new File(dir).eachFileRecurse { file ->
if (file.name == 'libclang_rt.asan-' + renamedAbi + '-android.so')
from file.absolutePath
into project.android.sourceSets.main.jniLibs.srcDirs[-1]
eachFile {
it.path = "${asanAbi}/${it.name}"
}
includeEmptyDirs = false
}
}
}
preBuild.dependsOn copyASanLib

afterEvaluate { project ->
transformNativeLibsWithStripDebugSymbolForRelease << {
Expand Down
11 changes: 11 additions & 0 deletions scripts/wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/system/bin/sh
HERE="$(cd "$(dirname "$0")" && pwd)"
export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1,use_sigaltstack=0
ASAN_LIB=$(ls $HERE/libclang_rt.asan-*-android.so)
if [ -f "$HERE/libc++_shared.so" ]; then
# Workaround for https://github.com/android-ndk/ndk/issues/988.
export LD_PRELOAD="$ASAN_LIB $HERE/libc++_shared.so"
else
export LD_PRELOAD="$ASAN_LIB"
fi
"$@"

0 comments on commit 59f1ee0

Please sign in to comment.