diff --git a/compiler-rt/lib/crt/crtbegin.c b/compiler-rt/lib/crt/crtbegin.c index 7b041ff00b6b6d9e4b98165485176c0585d4c418..d9853a98c60a845289aab41fbca261b9c1f45d6c 100644 --- a/compiler-rt/lib/crt/crtbegin.c +++ b/compiler-rt/lib/crt/crtbegin.c @@ -28,6 +28,14 @@ extern fp __CTOR_LIST_END__[]; extern void __cxa_finalize(void *) __attribute__((weak)); +// OHOS_LOCAL begin + +#if defined(__OHOS__) +extern void __at_fini() __attribute__((weak)); +#endif + +// OHOS_LOCAL end + static void __attribute__((used)) __do_init(void) { static _Bool __initialized; if (__builtin_expect(__initialized, 0)) @@ -85,6 +93,15 @@ static void __attribute__((used)) __do_fini(void) { return; __finalized = 1; +// OHOS_LOCAL begin + +#if defined(__OHOS__) + if (__at_fini) + __at_fini(); +#endif + +// OHOS_LOCAL end + if (__cxa_finalize) __cxa_finalize(__dso_handle); diff --git a/compiler-rt/lib/profile/GCDAProfiling.c b/compiler-rt/lib/profile/GCDAProfiling.c index 4f46fd2839b9093c24c13bdb6befc4050962fabc..40189fa964710b2f70571358be1f9249088ec0a2 100644 --- a/compiler-rt/lib/profile/GCDAProfiling.c +++ b/compiler-rt/lib/profile/GCDAProfiling.c @@ -550,20 +550,6 @@ void llvm_writeout_files(void) { } } -#ifndef _WIN32 -// __attribute__((destructor)) and destructors whose priorities are greater than -// 100 run before this function and can thus be tracked. The priority is -// compatible with GCC 7 onwards. -#if __GNUC__ >= 9 -#pragma GCC diagnostic ignored "-Wprio-ctor-dtor" -#endif -__attribute__((destructor(100))) -#endif -static void llvm_writeout_and_clear(void) { - llvm_writeout_files(); - fn_list_remove(&writeout_fn_list); -} - COMPILER_RT_VISIBILITY void llvm_register_reset_function(fn_ptr fn) { fn_list_insert(&reset_fn_list, fn); @@ -584,6 +570,29 @@ void llvm_reset_counters(void) { } } +#ifndef _WIN32 +// __attribute__((destructor)) and destructors whose priorities are greater than +// 100 run before this function and can thus be tracked. The priority is +// compatible with GCC 7 onwards. +#if __GNUC__ >= 9 +#pragma GCC diagnostic ignored "-Wprio-ctor-dtor" +#endif +__attribute__((destructor(100))) +#endif +static void llvm_writeout_and_clear(void) { + llvm_writeout_files(); + fn_list_remove(&writeout_fn_list); + +// OHOS_LOCAL begin + +#if defined(__OHOS__) + /* Make sure we write out the data and delete the data structures. */ + llvm_delete_reset_function_list(); +#endif + +// OHOS_LOCAL end +} + #if !defined(_WIN32) COMPILER_RT_VISIBILITY pid_t __gcov_fork() { @@ -616,8 +625,15 @@ void llvm_gcov_init(fn_ptr wfn, fn_ptr rfn) { if (atexit_ran == 0) { atexit_ran = 1; +// OHOS_LOCAL begin + +#if !defined(__OHOS__) /* Make sure we write out the data and delete the data structures. */ atexit(llvm_delete_reset_function_list); +#endif + +// OHOS_LOCAL end + #ifdef _WIN32 atexit(llvm_writeout_and_clear); #endif diff --git a/compiler-rt/lib/profile/InstrProfilingFile.c b/compiler-rt/lib/profile/InstrProfilingFile.c index 31cd7766ae79bba06660a784c41950b79b796d33..87aa915376d2e3febd56d0b6c86af073065a39ae 100644 --- a/compiler-rt/lib/profile/InstrProfilingFile.c +++ b/compiler-rt/lib/profile/InstrProfilingFile.c @@ -1127,8 +1127,15 @@ int __llvm_orderfile_dump(void) { return rc; } +// OHOS_LOCAL begin + +#if defined(__OHOS__) +__attribute__((destructor(0))) +#endif static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); } +// OHOS_LOCAL end + COMPILER_RT_VISIBILITY int __llvm_profile_register_write_file_atexit(void) { static int HasBeenRegistered = 0; @@ -1139,6 +1146,15 @@ int __llvm_profile_register_write_file_atexit(void) { lprofSetupValueProfiler(); HasBeenRegistered = 1; + +// OHOS_LOCAL begin + +#if defined(__OHOS__) + return 0; +#endif + +// OHOS_LOCAL end + return atexit(writeFileWithoutReturn); } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp index 956b48e0b434b2a5fa48adc3c29f722fe48d067f..b245e501c8a6b207099f6a881680c5d3a0805545 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep_new.cpp @@ -210,7 +210,15 @@ void InitializeCoverage(bool enabled, const char *dir) { if (coverage_enabled) return; // May happen if two sanitizer enable coverage in the same process. coverage_enabled = enabled; + +// OHOS_LOCAL begin + +#if !SANITIZER_OHOS Atexit(__sanitizer_cov_dump); +#endif // !SANITIZER_OHOS + +// OHOS_LOCAL end + AddDieCallback(__sanitizer_cov_dump); } } // namespace __sanitizer @@ -242,6 +250,17 @@ SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_reset() { __sancov::pc_guard_controller.Reset(); } + +// OHOS_LOCAL begin + +#if SANITIZER_OHOS +SANITIZER_INTERFACE_ATTRIBUTE void __at_fini() { + __sanitizer_cov_dump(); +} +#endif // SANITIZER_OHOS + +// OHOS_LOCAL end + // Default implementations (weak). // Either empty or very simple. // Most users should redefine them. diff --git a/compiler-rt/test/crt/dlclose_ubsan_standalone.cpp b/compiler-rt/test/crt/dlclose_ubsan_standalone.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65046cbe5893bdab0843fe3701ceef5f44f19478 --- /dev/null +++ b/compiler-rt/test/crt/dlclose_ubsan_standalone.cpp @@ -0,0 +1,29 @@ +// OHOS_LOCAL begin + +// Test that we do not segfault on dlclose of ubsan-standalone. +// +// RUN: %clangxx %s -DRT=\"%device_rundir/libclang_rt.ubsan_standalone.so\" -o %t -ldl +// RUN: %push_to_device %shared_ubsan_standalone %device_rundir/libclang_rt.ubsan_standalone.so +// RUN: %run %t 2>&1 | FileCheck %s + +#include +#include + +int main(int argc, char **argv) { + void *handle = dlopen(RT, RTLD_LAZY); + if (!handle) { + fprintf(stderr, "Failed to dlopen %s\n", RT); + return -1; + } + if (!dlclose(handle)) { + fprintf(stderr, "DONE\n"); + } + return 0; +} + +// CHECK-NOT: Failed to dlopen +// CHECK-NOT: Segmentation fault +// CHECK-NOT: Aborted (core dumped) +// CHECK: DONE + +// OHOS_LOCAL end diff --git a/compiler-rt/test/crt/lit.cfg.py b/compiler-rt/test/crt/lit.cfg.py index d5a6aa9862d9d1cc67b69e4d470d9451c7e506fd..c8d59edf5eba93b228507159decdb992cd74f371 100644 --- a/compiler-rt/test/crt/lit.cfg.py +++ b/compiler-rt/test/crt/lit.cfg.py @@ -55,7 +55,8 @@ def get_libgcc_file_name(): def build_invocation(compile_flags): - return ' ' + ' '.join([config.clang] + compile_flags) + ' ' + # OHOS_LOCAL + return ' ' + ' '.join([config.compile_wrapper, config.clang] + compile_flags) + ' ' # Setup substitutions. @@ -88,8 +89,16 @@ config.substitutions.append( config.substitutions.append( ('%libstdcxx', '-l' + config.sanitizer_cxx_lib.lstrip('lib'))) +# OHOS_LOCAL begin + +config.substitutions.append( + ('%shared_ubsan_standalone', + get_library_path('libclang_rt.ubsan_standalone.so'))) + +# OHOS_LOCAL end + # Default test suffixes. config.suffixes = ['.c', '.cpp'] -if config.host_os not in ['Linux']: +if config.host_os not in ['Linux', 'OHOS']: # OHOS_LOCAL config.unsupported = True diff --git a/llvm-build/build.py b/llvm-build/build.py index 7aeb4c69e8db8ae19513bc25bba457b663d8b0ec..868fd43254f8b63410e42ef2d86afaa3ceae895c 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1022,6 +1022,7 @@ class LlvmLibs(BuildUtils): crt_defines['COMPILER_RT_TEST_COMPILER_CFLAGS'] = ' '.join(cflags) crt_defines['OHOS'] = '1' crt_defines['COMPILER_RT_TEST_TARGET_TRIPLE'] = llvm_triple + crt_defines['COMPILER_RT_TEST_STANDALONE_BUILD_LIBS'] = 'OFF' crt_defines['COMPILER_RT_INCLUDE_TESTS'] = 'ON' crt_defines['CMAKE_INSTALL_PREFIX'] = crt_install crt_defines['LLVM_TARGET_MULTILIB_SUFFIX'] = multilib_suffix