From d1574b1b24251aa95dccfede147978853fd1d5df Mon Sep 17 00:00:00 2001 From: yanghao Date: Sat, 3 Aug 2024 10:16:49 +0800 Subject: [PATCH] add asan env Signed-off-by: yanghao Change-Id: Iaaac97b4961b03e6cbbcf1c29893ef280b6506fb --- interfaces/innerkits/include/appspawn.h | 2 ++ modules/asan/asan_detector.c | 28 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/interfaces/innerkits/include/appspawn.h b/interfaces/innerkits/include/appspawn.h index 8265cb95..265babca 100644 --- a/interfaces/innerkits/include/appspawn.h +++ b/interfaces/innerkits/include/appspawn.h @@ -173,6 +173,8 @@ typedef enum { APP_FLAGS_BEGETCTL_BOOT, // Start an app from begetctl. APP_FLAGS_ATOMIC_SERVICE, APP_FLAGS_CHILDPROCESS, + APP_FLAGS_UBSAN_ENABLED, + APP_FLAGS_HWASAN_ENABLED, MAX_FLAGS_INDEX = 63, } AppFlagsIndex; diff --git a/modules/asan/asan_detector.c b/modules/asan/asan_detector.c index 9f79db94..878ff66b 100644 --- a/modules/asan/asan_detector.c +++ b/modules/asan/asan_detector.c @@ -18,7 +18,7 @@ #include "appspawn_utils.h" #include "parameter.h" #include "securec.h" - +#define UBSAN_OPTION "log_path=/dev/ubsan/ubsan.log:print_stacktrace=1:print_module_map=2:log_exe_name=1" // for stub extern bool may_init_gwp_asan(bool forceInit); @@ -27,22 +27,27 @@ extern bool may_init_gwp_asan(bool forceInit); static int SetAsanEnabledEnv(const AppSpawnMgr *content, const AppSpawningCtx *property) { const char *bundleName = GetBundleName(property); - if (CheckAppMsgFlagsSet(property, APP_FLAGS_ASANENABLED)) { + if (CheckAppMsgFlagsSet(property, APP_FLAGS_ASANENABLED) || + CheckAppMsgFlagsSet(property, APP_FLAGS_HWASAN_ENABLED)) { char *devPath = "/dev/asanlog"; - char logPath[PATH_MAX] = {0}; - int ret = snprintf_s(logPath, sizeof(logPath), sizeof(logPath) - 1, - "/data/app/el1/100/base/%s/log", bundleName); - APPSPAWN_CHECK(ret > 0, return -1, "Invalid snprintf_s"); char asanOptions[PATH_MAX] = {0}; - ret = snprintf_s(asanOptions, sizeof(asanOptions), sizeof(asanOptions) - 1, + int ret = snprintf_s(asanOptions, sizeof(asanOptions), sizeof(asanOptions) - 1, "log_path=%s/asan.log:include=/system/etc/asan.options", devPath); APPSPAWN_CHECK(ret > 0, return -1, "Invalid snprintf_s"); + char asanSoPath[PATH_MAX] = {0}; #if defined(__aarch64__) || defined(__x86_64__) - setenv("LD_PRELOAD", "/system/lib64/libclang_rt.asan.so", 1); + ret = snprintf_s(asanSoPath, sizeof(asanSoPath), sizeof(asanSoPath) - 1, + "/system/lib64/libclang_rt.%s.so", + CheckAppMsgFlagsSet(property, APP_FLAGS_ASANENABLED) ? "asan" : "hwasan"); #else - setenv("LD_PRELOAD", "/system/lib/libclang_rt.asan.so", 1); + ret = snprintf_s(asanSoPath, sizeof(asanSoPath), sizeof(asanSoPath) - 1, + "/system/lib/libclang_rt.%s.so", + CheckAppMsgFlagsSet(property, APP_FLAGS_ASANENABLED) ? "asan" : "hwasan"); #endif + APPSPAWN_CHECK(ret > 0, return -1, "Invalid snprintf_s asan so path"); + ret = setenv("LD_PRELOAD", asanSoPath, 1); + APPSPAWN_LOGV("set LD_PRELOAD env %{public}s, %{public}d", asanSoPath, ret); unsetenv("UBSAN_OPTIONS"); setenv("ASAN_OPTIONS", asanOptions, 1); return 0; @@ -53,6 +58,11 @@ static int SetAsanEnabledEnv(const AppSpawnMgr *content, const AppSpawningCtx *p setenv("TSAN_OPTIONS", "include=/system/etc/tsan.options", 1); return 0; } + if (CheckAppMsgFlagsSet(property, APP_FLAGS_UBSAN_ENABLED)) { + int ret = setenv("UBSAN_OPTIONS", UBSAN_OPTION, 1); + APPSPAWN_LOGV("set UBSAN_OPTIONS env %{public}d, %{public}d", errno, ret); + return 0; + } return -1; } #endif -- Gitee