From 3dbe60a2c3e396c7ed6b005b94fb8338cf8b0bf4 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Tue, 31 Oct 2023 16:07:07 +0300 Subject: [PATCH 1/7] Tryig to implement hwasan support Signed-off-by: Sukhikh Alexander --- ldso/dynlink.c | 4 ++ porting/linux/user/ldso/dynlink.c | 4 ++ porting/linux/user/src/internal/libc.h | 3 ++ src/env/__libc_start_main.c | 60 ++++++++++++++++++++++++++ src/internal/libc.c | 2 +- src/internal/libc.h | 5 +++ 6 files changed, 77 insertions(+), 1 deletion(-) diff --git a/ldso/dynlink.c b/ldso/dynlink.c index 5b9c8be42..8910f2d25 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1191,6 +1191,10 @@ static struct dso *load_library(const char *name, struct dso *needed_by) if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); + if (libc.load_hook) { + libc.load_hook((long unsigned int)p->base, p->phdr, p->phnum); + } + return p; } diff --git a/porting/linux/user/ldso/dynlink.c b/porting/linux/user/ldso/dynlink.c index 0b6d00e49..adb8f02ce 100644 --- a/porting/linux/user/ldso/dynlink.c +++ b/porting/linux/user/ldso/dynlink.c @@ -2158,6 +2158,10 @@ struct dso *load_library( if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); + if (libc.load_hook) { + libc.load_hook((long unsigned int)p->base, p->phdr, p->phnum); + } + return p; } diff --git a/porting/linux/user/src/internal/libc.h b/porting/linux/user/src/internal/libc.h index 2f1d24795..cdf99849c 100644 --- a/porting/linux/user/src/internal/libc.h +++ b/porting/linux/user/src/internal/libc.h @@ -21,6 +21,9 @@ #include #include #include + +#include "link.h" + struct __locale_map; struct __locale_struct { diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c index c5b277bdc..3be7b037b 100644 --- a/src/env/__libc_start_main.c +++ b/src/env/__libc_start_main.c @@ -69,11 +69,71 @@ weak_alias(libc_start_init, __libc_start_init); typedef int lsm2_fn(int (*)(int,char **,char **), int, char **); static lsm2_fn libc_start_main_stage2; +// // bionic_globals.h + +// struct libc_shared_globals { +// void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = NULL; +// void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = NULL; +// }; + +// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals(); + +// //dlfcn.cpp +// libc_shared_globals* __loader_shared_globals() __LINKER_PUBLIC__; +// libc_shared_globals* __loader_shared_globals() { +// return __libc_shared_globals(); +// } + +// // ld_android.cpp +// __strong_alias(__loader_shared_globals, __internal_linker_error); + + +// // libc_init_dynamic.cpp +// libc_shared_globals* __loader_shared_globals(); + +// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() { +// return __loader_shared_globals(); +// } + +// // libc_init_static.cpp +// // This function is called in the dynamic linker before ifunc resolvers have run, so this file is +// // compiled with -ffreestanding to avoid implicit string.h function calls. (It shouldn't strictly +// // be necessary, though.) +// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() { +// static libc_shared_globals globals; +// return &globals; +// } + + +// #ifdef SHARED +#if 1 +weak void __hwasan_library_loaded(ElfW(Addr) base, + const ElfW(Phdr)* phdr, + ElfW(Half) phnum); +// weak void __hwasan_library_unloaded(ElfW(Addr) base, +// const ElfW(Phdr)* phdr, +// ElfW(Half) phnum); +#else +void __hwasan_init_static(); +#endif // SHARED + + int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv, void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)()) { char **envp = argv+argc+1; +#if 1 +// #ifdef SHARED + // Notify the HWASan runtime library whenever a library is loaded or unloaded + // so that it can update its shadow memory. + libc.load_hook = __hwasan_library_loaded; +#else + // Initialize HWASan enough to run instrumented code. This sets up TLS_SLOT_SANITIZER, among other + // things. + __hwasan_init_static(); +#endif // SHARED + /* External linkage, and explicit noinline attribute if available, * are used to prevent the stack frame used during init from * persisting for the entire process lifetime. */ diff --git a/src/internal/libc.c b/src/internal/libc.c index cb0518108..b1ecc9794 100644 --- a/src/internal/libc.c +++ b/src/internal/libc.c @@ -1,6 +1,6 @@ #include "libc.h" -struct __libc __libc; +struct __libc __libc = {.load_hook=NULL}; size_t __hwcap; char *__progname=0, *__progname_full=0; diff --git a/src/internal/libc.h b/src/internal/libc.h index 619bba861..c69c63b9d 100644 --- a/src/internal/libc.h +++ b/src/internal/libc.h @@ -5,6 +5,8 @@ #include #include +#include "link.h" + struct __locale_map; struct __locale_struct { @@ -28,6 +30,9 @@ struct __libc { size_t tls_size, tls_align, tls_cnt; size_t page_size; struct __locale_struct global_locale; + void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum); + // musl doesn't support library unloading, so this hook will never be used. + // void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum); }; #ifndef PAGE_SIZE -- Gitee From cc5014fd6937562e0094b0a66cacb55e524c3407 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Wed, 8 Nov 2023 20:19:07 +0300 Subject: [PATCH 2/7] Fix header length for .note.ohos.ident Signed-off-by: Sukhikh Alexander --- porting/linux/user/arch/generic/crtbrand.s | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/porting/linux/user/arch/generic/crtbrand.s b/porting/linux/user/arch/generic/crtbrand.s index 2974ec6aa..d66eb9da6 100644 --- a/porting/linux/user/arch/generic/crtbrand.s +++ b/porting/linux/user/arch/generic/crtbrand.s @@ -4,7 +4,7 @@ abitag: .long 2f-1f // int32_t namesz .long 3f-2f // int32_t descsz - .long 4f-3f + .long 4f-4f // should be the "type" according to the elf spec 1:.ascii "OHOS\0" // char name[] 2:.long 1 // int32_t ohos_api 3: -- Gitee From 1124ddbeeda03613d429d4e4ca20b796988b0b21 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Wed, 8 Nov 2023 20:27:50 +0300 Subject: [PATCH 3/7] Remove extra changes Signed-off-by: Sukhikh Alexander --- ldso/dynlink.c | 4 --- src/env/__libc_start_main.c | 60 ------------------------------------- src/internal/libc.h | 5 ---- 3 files changed, 69 deletions(-) diff --git a/ldso/dynlink.c b/ldso/dynlink.c index 8910f2d25..5b9c8be42 100644 --- a/ldso/dynlink.c +++ b/ldso/dynlink.c @@ -1191,10 +1191,6 @@ static struct dso *load_library(const char *name, struct dso *needed_by) if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); - if (libc.load_hook) { - libc.load_hook((long unsigned int)p->base, p->phdr, p->phnum); - } - return p; } diff --git a/src/env/__libc_start_main.c b/src/env/__libc_start_main.c index 3be7b037b..c5b277bdc 100644 --- a/src/env/__libc_start_main.c +++ b/src/env/__libc_start_main.c @@ -69,71 +69,11 @@ weak_alias(libc_start_init, __libc_start_init); typedef int lsm2_fn(int (*)(int,char **,char **), int, char **); static lsm2_fn libc_start_main_stage2; -// // bionic_globals.h - -// struct libc_shared_globals { -// void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = NULL; -// void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum) = NULL; -// }; - -// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals(); - -// //dlfcn.cpp -// libc_shared_globals* __loader_shared_globals() __LINKER_PUBLIC__; -// libc_shared_globals* __loader_shared_globals() { -// return __libc_shared_globals(); -// } - -// // ld_android.cpp -// __strong_alias(__loader_shared_globals, __internal_linker_error); - - -// // libc_init_dynamic.cpp -// libc_shared_globals* __loader_shared_globals(); - -// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() { -// return __loader_shared_globals(); -// } - -// // libc_init_static.cpp -// // This function is called in the dynamic linker before ifunc resolvers have run, so this file is -// // compiled with -ffreestanding to avoid implicit string.h function calls. (It shouldn't strictly -// // be necessary, though.) -// __LIBC_HIDDEN__ libc_shared_globals* __libc_shared_globals() { -// static libc_shared_globals globals; -// return &globals; -// } - - -// #ifdef SHARED -#if 1 -weak void __hwasan_library_loaded(ElfW(Addr) base, - const ElfW(Phdr)* phdr, - ElfW(Half) phnum); -// weak void __hwasan_library_unloaded(ElfW(Addr) base, -// const ElfW(Phdr)* phdr, -// ElfW(Half) phnum); -#else -void __hwasan_init_static(); -#endif // SHARED - - int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv, void (*init_dummy)(), void(*fini_dummy)(), void(*ldso_dummy)()) { char **envp = argv+argc+1; -#if 1 -// #ifdef SHARED - // Notify the HWASan runtime library whenever a library is loaded or unloaded - // so that it can update its shadow memory. - libc.load_hook = __hwasan_library_loaded; -#else - // Initialize HWASan enough to run instrumented code. This sets up TLS_SLOT_SANITIZER, among other - // things. - __hwasan_init_static(); -#endif // SHARED - /* External linkage, and explicit noinline attribute if available, * are used to prevent the stack frame used during init from * persisting for the entire process lifetime. */ diff --git a/src/internal/libc.h b/src/internal/libc.h index c69c63b9d..619bba861 100644 --- a/src/internal/libc.h +++ b/src/internal/libc.h @@ -5,8 +5,6 @@ #include #include -#include "link.h" - struct __locale_map; struct __locale_struct { @@ -30,9 +28,6 @@ struct __libc { size_t tls_size, tls_align, tls_cnt; size_t page_size; struct __locale_struct global_locale; - void (*load_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum); - // musl doesn't support library unloading, so this hook will never be used. - // void (*unload_hook)(ElfW(Addr) base, const ElfW(Phdr)* phdr, ElfW(Half) phnum); }; #ifndef PAGE_SIZE -- Gitee From cee683063e872f600b664e812d2a33796875faeb Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Tue, 28 Nov 2023 15:49:17 +0300 Subject: [PATCH 4/7] Fix crtbrand.s size and alignment Signed-off-by: Sukhikh Alexander --- porting/linux/user/arch/generic/crtbrand.s | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/porting/linux/user/arch/generic/crtbrand.s b/porting/linux/user/arch/generic/crtbrand.s index d66eb9da6..a59a9ffdb 100644 --- a/porting/linux/user/arch/generic/crtbrand.s +++ b/porting/linux/user/arch/generic/crtbrand.s @@ -1,12 +1,12 @@ - .section .note.ohos.ident,"a",%note - .balign 4 - .type abitag, %object -abitag: - .long 2f-1f // int32_t namesz - .long 3f-2f // int32_t descsz - .long 4f-4f // should be the "type" according to the elf spec -1:.ascii "OHOS\0" // char name[] -2:.long 1 // int32_t ohos_api -3: -4: - .size abitag, .-abitag + .section .note.ohos.ident,"a",%note + .balign 4 + .type abitag, %object +abitag: + .long 2f-1f // int32_t namesz + .long 3f-2f // int32_t descsz + .long 1 // should be the "type" according to the elf spec +1:.ascii "OHOS\0" // char name[] + .balign 4 +2:.long 1 // int32_t ohos_api +3: + .size abitag, .-abitag -- Gitee From 1e4d0b43b512b71b588ae5d77961b18ec1cc8a31 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Thu, 14 Dec 2023 15:50:46 +0300 Subject: [PATCH 5/7] Remove NULL initialization (not needed for global) Signed-off-by: Sukhikh Alexander --- src/internal/libc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/libc.c b/src/internal/libc.c index b1ecc9794..cb0518108 100644 --- a/src/internal/libc.c +++ b/src/internal/libc.c @@ -1,6 +1,6 @@ #include "libc.h" -struct __libc __libc = {.load_hook=NULL}; +struct __libc __libc; size_t __hwcap; char *__progname=0, *__progname_full=0; -- Gitee From 4deb0de18c28a83c36c00d1b3fb78e0aabe7a56e Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Thu, 14 Dec 2023 15:51:18 +0300 Subject: [PATCH 6/7] Remove extra flags which broke stack unwinding Signed-off-by: Sukhikh Alexander --- musl_template.gni | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/musl_template.gni b/musl_template.gni index f25f0d9fc..369bdac6f 100644 --- a/musl_template.gni +++ b/musl_template.gni @@ -149,17 +149,10 @@ template("musl_libs") { "-Wno-int-conversion", ] - if (is_llvm_build) { - cflags_auto += [ - "-fno-unwind-tables", - "-fno-asynchronous-unwind-tables", - ] - } else { - cflags_auto += [ - "-funwind-tables", - "-fasynchronous-unwind-tables", - ] - } + cflags_auto += [ + "-funwind-tables", + "-fasynchronous-unwind-tables", + ] if (is_asan && use_hwasan) { cflags_auto += [ "-DENABLE_HWASAN" ] @@ -325,11 +318,7 @@ template("musl_libs") { "-DOHOS_TCACHE_NSLOTS_LARGE=16", ] - if (is_llvm_build) { - cflags += [ "-fno-unwind-tables" ] - } else { - cflags += [ "-funwind-tables" ] - } + cflags += [ "-funwind-tables" ] if (is_debug || musl_secure_level > 1) { cflags += [ "-DOHOS_TCACHE_NSLOTS_RANDOM" ] -- Gitee From 23e9542442af01b818f456a3d6c5d4369cd06aca Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Thu, 14 Dec 2023 15:57:37 +0300 Subject: [PATCH 7/7] Remove changes, that already commited into musl Signed-off-by: Sukhikh Alexander --- porting/linux/user/ldso/dynlink.c | 4 ---- porting/linux/user/src/internal/libc.h | 3 --- 2 files changed, 7 deletions(-) diff --git a/porting/linux/user/ldso/dynlink.c b/porting/linux/user/ldso/dynlink.c index adb8f02ce..0b6d00e49 100644 --- a/porting/linux/user/ldso/dynlink.c +++ b/porting/linux/user/ldso/dynlink.c @@ -2158,10 +2158,6 @@ struct dso *load_library( if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); - if (libc.load_hook) { - libc.load_hook((long unsigned int)p->base, p->phdr, p->phnum); - } - return p; } diff --git a/porting/linux/user/src/internal/libc.h b/porting/linux/user/src/internal/libc.h index cdf99849c..2f1d24795 100644 --- a/porting/linux/user/src/internal/libc.h +++ b/porting/linux/user/src/internal/libc.h @@ -21,9 +21,6 @@ #include #include #include - -#include "link.h" - struct __locale_map; struct __locale_struct { -- Gitee