From 28c727169c6480053cda25fff50072b674da3418 Mon Sep 17 00:00:00 2001 From: Nikolai Kholiavin Date: Wed, 20 Dec 2023 16:19:03 +0000 Subject: [PATCH 01/44] [cfi][CodeGen] Call SetLLVMFunctionAttributes{,ForDefinition} on __cfi_check This causes __cfi_check, just as __cfi_check_fail, to get the proper target-specific attributes, in particular uwtable for unwind table generation. Previously, nounwind attribute could be inferred for __cfi_check, which caused it to lose its unwind table even with -funwind-table option. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8QWWL Signed-off-by: Nikolai Kholiavin --- clang/lib/CodeGen/CGExpr.cpp | 22 ++++++++++++++++++++++ clang/test/CodeGen/cfi-check-attrs.c | 5 +++++ clang/test/CodeGen/cfi-check-fail.c | 4 +++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGen/cfi-check-attrs.c diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index bf3dd812b9e8..9df9776504bc 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3446,10 +3446,32 @@ void CodeGenFunction::EmitCfiSlowPathCheck( void CodeGenFunction::EmitCfiCheckStub() { llvm::Module *M = &CGM.getModule(); auto &Ctx = M->getContext(); + // OHOS_LOCAL begin + auto &C = getContext(); + QualType QInt8Ty = C.getIntTypeForBitwidth(8, false); + QualType QInt64Ty = C.getIntTypeForBitwidth(64, false); + QualType QInt8PtrTy = C.getPointerType(QInt8Ty); + + FunctionArgList Args; + ImplicitParamDecl ArgCallsiteTypeId(C, QInt64Ty, ImplicitParamDecl::Other); + ImplicitParamDecl ArgAddr(C, QInt8PtrTy, ImplicitParamDecl::Other); + ImplicitParamDecl ArgCFICheckFailData(C, QInt8PtrTy, + ImplicitParamDecl::Other); + Args.push_back(&ArgCallsiteTypeId); + Args.push_back(&ArgAddr); + Args.push_back(&ArgCFICheckFailData); + + const CGFunctionInfo &FI = + CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args); + llvm::Function *F = llvm::Function::Create( llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy}, false), llvm::GlobalValue::WeakAnyLinkage, "__cfi_check", M); + CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false); + CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F); + // OHOS_LOCAL end CGM.setDSOLocal(F); + llvm::BasicBlock *BB = llvm::BasicBlock::Create(Ctx, "entry", F); // FIXME: consider emitting an intrinsic call like // call void @llvm.cfi_check(i64 %0, i8* %1, i8* %2) diff --git a/clang/test/CodeGen/cfi-check-attrs.c b/clang/test/CodeGen/cfi-check-attrs.c new file mode 100644 index 000000000000..375aa30074d8 --- /dev/null +++ b/clang/test/CodeGen/cfi-check-attrs.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -triple arm-unknown-linux -funwind-tables=1 -fsanitize-cfi-cross-dso -emit-llvm -o - %s | FileCheck %s + +// CHECK: define weak {{.*}}void @__cfi_check({{.*}} [[ATTR:#[0-9]*]] + +// CHECK: attributes [[ATTR]] = {{.*}} uwtable(sync) diff --git a/clang/test/CodeGen/cfi-check-fail.c b/clang/test/CodeGen/cfi-check-fail.c index 09c40d43fef2..89d5bac4c174 100644 --- a/clang/test/CodeGen/cfi-check-fail.c +++ b/clang/test/CodeGen/cfi-check-fail.c @@ -73,7 +73,9 @@ void caller(void (*f)(void)) { // CHECK: [[CONT5]]: // CHECK: ret void -// CHECK: define weak void @__cfi_check(i64 %0, i8* %1, i8* %2) +// OHOS_LOCAL begin +// CHECK: define weak void @__cfi_check(i64 noundef %0, i8* noundef %1, i8* noundef %2) +// OHOS_LOCAL end // CHECK-NOT: } // CHECK: call void @llvm.trap() // CHECK-NEXT: ret void -- Gitee From 90b58b78b1d8a317798ebf3e79801887ec1c7afc Mon Sep 17 00:00:00 2001 From: lwx1312777 Date: Mon, 8 Jan 2024 12:30:07 +0800 Subject: [PATCH 02/44] =?UTF-8?q?=E6=9B=B4=E6=96=B0musl=E4=BB=93=E5=90=88?= =?UTF-8?q?=E5=85=A5=E8=A7=84=E8=8C=83=E8=A6=81=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lwx1312777 --- .gitee/PULL_REQUEST_TEMPLATE.en.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitee/PULL_REQUEST_TEMPLATE.en.md b/.gitee/PULL_REQUEST_TEMPLATE.en.md index e7d9a15062a7..c15c8992b6f4 100644 --- a/.gitee/PULL_REQUEST_TEMPLATE.en.md +++ b/.gitee/PULL_REQUEST_TEMPLATE.en.md @@ -20,3 +20,4 @@ Development Guidelines: https://gitee.com/openharmony/third_party_llvm-project/b why: +### Compatibility impact assessment, please indicate (Y/N) \ No newline at end of file -- Gitee From 1eb3737aa2eccb63abbbecf521fbf4d64e3dc6b3 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Mon, 8 Jan 2024 14:32:52 +0800 Subject: [PATCH 03/44] Switch gwp lock to normal lock Issue:I8STHA Signed-off-by: yinchuang --- compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp | 7 ------- compiler-rt/lib/gwp_asan/guarded_pool_allocator.h | 1 - compiler-rt/lib/gwp_asan/mutex.h | 1 - .../guarded_pool_allocator_posix.cpp | 6 ------ .../lib/gwp_asan/platform_specific/mutex_posix.cpp | 12 ------------ .../lib/gwp_asan/platform_specific/mutex_posix.h | 6 ------ 6 files changed, 33 deletions(-) diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp index ef3923eb788d..5bf1284e1f89 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.cpp @@ -120,13 +120,6 @@ void GuardedPoolAllocator::enable() { BacktraceMutex.unlock(); } -// OHOS_LOCAL begin -void GuardedPoolAllocator::enableAtFork() { - PoolMutex.unlockAtFork(); - BacktraceMutex.unlockAtFork(); -} -// OHOS_LOCAL end - void GuardedPoolAllocator::iterate(void *Base, size_t Size, iterate_callback Cb, void *Arg) { uintptr_t Start = reinterpret_cast(Base); diff --git a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h index f1086651395f..152028acd822 100644 --- a/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h +++ b/compiler-rt/lib/gwp_asan/guarded_pool_allocator.h @@ -63,7 +63,6 @@ public: // to allocate memory, until enable() is called. void disable(); void enable(); - void enableAtFork(); // OHOS_LOCAL typedef void (*iterate_callback)(uintptr_t base, size_t size, void *arg); // Execute the callback Cb for every allocation the lies in [Base, Base + diff --git a/compiler-rt/lib/gwp_asan/mutex.h b/compiler-rt/lib/gwp_asan/mutex.h index 299eae6719ea..34b91a2880dd 100644 --- a/compiler-rt/lib/gwp_asan/mutex.h +++ b/compiler-rt/lib/gwp_asan/mutex.h @@ -25,7 +25,6 @@ public: bool tryLock(); // Unlock the mutex. void unlock(); - void unlockAtFork(); // OHOS_LOCAL }; class ScopedLock { diff --git a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp index f2db40aa3cb9..149b74238f0d 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp +++ b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_posix.cpp @@ -106,13 +106,7 @@ void GuardedPoolAllocator::installAtFork() { }; auto Enable = []() { if (auto *S = getSingleton()) -// OHOS_LOCAL begin -#if defined(__OHOS__) - S->enableAtFork(); -#else S->enable(); -#endif -// OHOS_LOCAL end }; pthread_atfork(Disable, Enable, Enable); } diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp index a3ee11c0e78a..8bd405e1074c 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.cpp @@ -27,16 +27,4 @@ void Mutex::unlock() { // Remove warning for non-debug builds. (void)Status; } - -// OHOS_LOCAL begin -// 1、OHOS uses recursive locks to avoid deadlock, such as this call chain: -// `gwp_asan malloc -> find double free -> get lock -> trigger segv -> -// segv handler -> malloc -> gwp_asan malloc` -// 2、It will be failed to unlock recursive lock after fork because tid changes, -// so we just create a new recursive lock here. -void Mutex::unlockAtFork() { - Mu = {{{PTHREAD_MUTEX_RECURSIVE}}}; -} -// OHOS_LOCAL end - } // namespace gwp_asan diff --git a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h index 57b981c6e1b0..7f0239198f56 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h +++ b/compiler-rt/lib/gwp_asan/platform_specific/mutex_posix.h @@ -15,13 +15,7 @@ namespace gwp_asan { class PlatformMutex { protected: -// OHOS_LOCAL begin -#if defined(__OHOS__) - pthread_mutex_t Mu = {{{PTHREAD_MUTEX_RECURSIVE}}}; -#else pthread_mutex_t Mu = PTHREAD_MUTEX_INITIALIZER; -#endif -// OHOS_LOCAL end }; } // namespace gwp_asan -- Gitee From 0ba028411f505925b4ea31947cd67f2b0a502471 Mon Sep 17 00:00:00 2001 From: Peshkov Ivan Date: Wed, 6 Dec 2023 12:55:10 +0300 Subject: [PATCH 04/44] [RISCV] Fix linking with floating-point ABI Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8K781 Signed-off-by: Lyupa Anastasia --- llvm/include/llvm/ObjCopy/CommonConfig.h | 3 +++ llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 4 ++++ llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 8 ++++++++ llvm/tools/llvm-objcopy/ObjcopyOpts.td | 2 ++ 4 files changed, 17 insertions(+) diff --git a/llvm/include/llvm/ObjCopy/CommonConfig.h b/llvm/include/llvm/ObjCopy/CommonConfig.h index 4921f5281ca6..cd20eac444e9 100644 --- a/llvm/include/llvm/ObjCopy/CommonConfig.h +++ b/llvm/include/llvm/ObjCopy/CommonConfig.h @@ -218,6 +218,9 @@ struct CommonConfig { StringRef AllocSectionsPrefix; DiscardType DiscardMode = DiscardType::None; + // Float abi + Optional FabiValue; + // Repeated options std::vector AddSection; std::vector DumpSection; diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp index 781be3d8aeb1..2126324bbad9 100644 --- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp @@ -604,6 +604,10 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Obj.OSABI = Config.OutputArch.value().OSABI; } + if (Config.FabiValue) { + Obj.Flags |= Config.FabiValue.value(); + } + if (!Config.SplitDWO.empty() && Config.ExtractDWO) { return Obj.removeSections( ELFConfig.AllowBrokenLinks, diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp index 7db1e79f3e49..5baca1062a66 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -662,6 +662,14 @@ objcopy::parseObjcopyOptions(ArrayRef RawArgsArr, VisibilityStr.str().c_str()); } + if (InputArgs.hasArg(OBJCOPY_fabi_riscv)) { + StringRef FabiValue = InputArgs.getLastArgValue(OBJCOPY_fabi_riscv); + // TODO: check if this value is correct + uint32_t result; + FabiValue.getAsInteger(10, result); + Config.FabiValue = result; + } + for (const auto *Arg : InputArgs.filtered(OBJCOPY_subsystem)) { StringRef Subsystem, Version; std::tie(Subsystem, Version) = StringRef(Arg->getValue()).split(':'); diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td index d3713b5ec3c3..a7e1f6b09c15 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td +++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td @@ -29,6 +29,8 @@ defm new_symbol_visibility : Eq<"new-symbol-visibility", "Visibility of " " with --add-symbol unless otherwise" " specified. The default value is 'default'.">; +defm fabi_riscv : Eq<"fabi-riscv", "Sets float ABI value for riscv arch">; + def compress_debug_sections : Joined<["--"], "compress-debug-sections=">, MetaVarName<"format">, -- Gitee From a510ba9728dc6f4f61ff8b6839fba5be59d2841d Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Tue, 9 Jan 2024 16:18:19 +0300 Subject: [PATCH 05/44] [llvm-objcopy] Refactor fabi-riscv to elf-flags Signed-off-by: Lyupa Anastasia --- llvm/include/llvm/ObjCopy/CommonConfig.h | 6 ++++-- llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp | 6 ++++-- llvm/tools/llvm-objcopy/ObjcopyOptions.cpp | 14 +++++++++----- llvm/tools/llvm-objcopy/ObjcopyOpts.td | 3 ++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/ObjCopy/CommonConfig.h b/llvm/include/llvm/ObjCopy/CommonConfig.h index cd20eac444e9..38132b725557 100644 --- a/llvm/include/llvm/ObjCopy/CommonConfig.h +++ b/llvm/include/llvm/ObjCopy/CommonConfig.h @@ -218,8 +218,10 @@ struct CommonConfig { StringRef AllocSectionsPrefix; DiscardType DiscardMode = DiscardType::None; - // Float abi - Optional FabiValue; + // OHOS_LOCAL begin + // Additional ELF object flags + Optional ELFObjFlags; + // OHOS_LOCAL end // Repeated options std::vector AddSection; diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp index 2126324bbad9..5c55abb72aed 100644 --- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp @@ -604,9 +604,11 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig, Obj.OSABI = Config.OutputArch.value().OSABI; } - if (Config.FabiValue) { - Obj.Flags |= Config.FabiValue.value(); + // OHOS_LOCAL begin + if (Config.ELFObjFlags) { + Obj.Flags |= Config.ELFObjFlags.value(); } + // OHOS_LOCAL end if (!Config.SplitDWO.empty() && Config.ExtractDWO) { return Obj.removeSections( diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp index 5baca1062a66..cfacaa0aa030 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -662,13 +662,17 @@ objcopy::parseObjcopyOptions(ArrayRef RawArgsArr, VisibilityStr.str().c_str()); } - if (InputArgs.hasArg(OBJCOPY_fabi_riscv)) { - StringRef FabiValue = InputArgs.getLastArgValue(OBJCOPY_fabi_riscv); - // TODO: check if this value is correct + // OHOS_LOCAL begin + if (InputArgs.hasArg(OBJCOPY_elf_flags)) { + StringRef ELFObjFlags = InputArgs.getLastArgValue(OBJCOPY_elf_flags); uint32_t result; - FabiValue.getAsInteger(10, result); - Config.FabiValue = result; + if (ELFObjFlags.getAsInteger(10, result)) + return createStringError(errc::invalid_argument, + "'%s' is not a valid ELF object flags value", + ELFObjFlags.str().c_str()); + Config.ELFObjFlags = result; } + // OHOS_LOCAL end for (const auto *Arg : InputArgs.filtered(OBJCOPY_subsystem)) { StringRef Subsystem, Version; diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td index a7e1f6b09c15..4c454d7ec59a 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td +++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td @@ -29,7 +29,8 @@ defm new_symbol_visibility : Eq<"new-symbol-visibility", "Visibility of " " with --add-symbol unless otherwise" " specified. The default value is 'default'.">; -defm fabi_riscv : Eq<"fabi-riscv", "Sets float ABI value for riscv arch">; +/// OHOS_LOCAL +defm elf_flags : Eq<"elf-flags", "Additional ELF flags of unsigned integer type">; def compress_debug_sections : Joined<["--"], "compress-debug-sections=">, -- Gitee From 7593893945553702fd002a6ffc87bab1f025594b Mon Sep 17 00:00:00 2001 From: liuyaning Date: Thu, 18 Jan 2024 12:03:55 +0800 Subject: [PATCH 06/44] Update gn version Update gn version to 20240115 Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I8X469 Test:prebuilts_download Signed-off-by: liuyaning --- llvm-build/env_prepare.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm-build/env_prepare.sh b/llvm-build/env_prepare.sh index c9996de3b34a..2557ece0be32 100755 --- a/llvm-build/env_prepare.sh +++ b/llvm-build/env_prepare.sh @@ -67,7 +67,7 @@ copy_config_linux_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},https://mirrors.huaweicloud.com/openharmony/compiler/clang/${CLANG_PACKAGE_VERSION}/linux/${CLANG_LINUX_BUILD}.tar.bz2 prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz -prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/2024/linux/gn-linux-x86-20230426.tar.gz +prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/20240115/linux/gn-linux-x86-20240115.tar.gz prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/ninja/1.11.0/linux/ninja-linux-x86-1.11.0.tar.gz """ @@ -77,7 +77,7 @@ copy_config_darwin_x86_64=""" prebuilts/cmake,https://mirrors.huaweicloud.com/harmonyos/compiler/cmake/3.16.5/${host_platform}/cmake-${host_platform}-x86-3.16.5.tar.gz prebuilts/clang/ohos/${host_platform}-${host_cpu},http://mirrors.huaweicloud.com/harmonyos/compiler/clang/15.0.4-8e906c/darwin/${CLANG_DARWIN_BUILD}.tar.bz2 prebuilts/python3,https://mirrors.huaweicloud.com/harmonyos/compiler/python/3.10.2/${host_platform}/python-${host_platform}-x86-3.10.2_20230604.tar.gz -prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/2024/darwin/gn-darwin-x86-20230425.tar.gz +prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/gn/20240115/darwin/gn-darwin-x86-20240115.tar.gz prebuilts/build-tools/${host_platform}-x86/bin,https://repo.huaweicloud.com/openharmony/compiler/ninja/1.11.0/darwin/ninja-darwin-x86-1.11.0.tar.gz """ -- Gitee From 379016e89b860de4524c46143c04ac390ed08476 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Tue, 23 Jan 2024 10:52:07 +0800 Subject: [PATCH 07/44] [Gwp-Asan] Fix gwp_asan using tls Issue:I8Y6MQ Signed-off-by: yinchuang --- .../platform_specific/guarded_pool_allocator_tls.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_tls.h b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_tls.h index 3e2055db3dc5..475b176ccd47 100644 --- a/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_tls.h +++ b/compiler-rt/lib/gwp_asan/platform_specific/guarded_pool_allocator_tls.h @@ -44,12 +44,26 @@ static_assert(sizeof(ThreadLocalPackedVariables) == sizeof(uint64_t), #ifdef GWP_ASAN_PLATFORM_TLS_HEADER #include GWP_ASAN_PLATFORM_TLS_HEADER #else +// OHOS_LOCAL begin +#if defined(__OHOS__) +// Musl doesn't support libc using TLS variables now, +// so musl puts gwp_asan tls on the pthread, this interface can return the corresponding address. +extern "C" void* get_platform_gwp_asan_tls_slot(); +namespace gwp_asan { +inline ThreadLocalPackedVariables *getThreadLocals() { + return reinterpret_cast(get_platform_gwp_asan_tls_slot()); +} +} // namespace gwp_asan +#else namespace gwp_asan { inline ThreadLocalPackedVariables *getThreadLocals() { alignas(8) static GWP_ASAN_TLS_INITIAL_EXEC ThreadLocalPackedVariables Locals; return &Locals; } } // namespace gwp_asan +#endif // __OHOS__ +// OHOS_LOCAL end + #endif // GWP_ASAN_PLATFORM_TLS_HEADER #endif // GWP_ASAN_GUARDED_POOL_ALLOCATOR_TLS_H_ -- Gitee From b5e6a34c40a411b5f68e87986320a60a6a3e00e3 Mon Sep 17 00:00:00 2001 From: guzhihao4 Date: Tue, 23 Jan 2024 11:10:23 +0000 Subject: [PATCH 08/44] [Sanitizer]Disable asan intecept dlopen Avoid asan dlopen an app library Issue: #I8Y3EU Signed-off-by: guzhihao4 Change-Id: I72c9297cf0e98e228af02fc1c5f385b173cc7462 --- .../lib/sanitizer_common/sanitizer_platform_interceptors.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index b78534e8fc42..de78cca581f0 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -443,8 +443,9 @@ #define SANITIZER_INTERCEPT_FCLOSE SI_POSIX #ifndef SANITIZER_INTERCEPT_DLOPEN_DLCLOSE +// OHOS_LOCAL #define SANITIZER_INTERCEPT_DLOPEN_DLCLOSE \ - (SI_FREEBSD || SI_NETBSD || SI_LINUX_NOT_ANDROID || SI_MAC || SI_SOLARIS) + (SI_FREEBSD || SI_NETBSD || (SI_LINUX_NOT_ANDROID && !SI_OHOS) || SI_MAC || SI_SOLARIS) #endif #define SANITIZER_INTERCEPT_GETPASS \ -- Gitee From 58d837aea1e6fc90df74fa2d51122254b6b23eb9 Mon Sep 17 00:00:00 2001 From: zhao-baiyi Date: Mon, 29 Jan 2024 09:11:06 +0000 Subject: [PATCH 09/44] Modify README.md Signed-off-by: zhao-baiyi --- llvm-build/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/llvm-build/README.md b/llvm-build/README.md index 1a256676fd34..bd90dd8017e2 100644 --- a/llvm-build/README.md +++ b/llvm-build/README.md @@ -16,6 +16,19 @@ MacOS X >= 10.15.4
+### Environmental preparation + +ubuntu +``` +sudo apt-get install build-essential swig python3-dev libedit-dev libncurses5-dev binutils-dev gcc-multilib +``` +mac +``` +brew install swig git-lfs java coreutils wget +``` + +
+ ### Get Code ``` repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-toolchain.xml -- Gitee From 6dcc1c556692389602c3c1ad26b20047ee2402e9 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Tue, 30 Jan 2024 21:10:50 +0800 Subject: [PATCH 10/44] [Sanitizer] Enable Ohos hilog Issue:I900ZT Signed-off-by: yinchuang --- compiler-rt/lib/asan/asan_rtl.cpp | 2 ++ compiler-rt/lib/hwasan/hwasan.cpp | 1 + .../lib/sanitizer_common/sanitizer_common.h | 2 ++ .../sanitizer_linux_libcdep.cpp | 28 +++++++++++++++++++ .../lib/sanitizer_common/sanitizer_win.cpp | 2 ++ compiler-rt/lib/ubsan/ubsan_init.cpp | 1 + 6 files changed, 36 insertions(+) diff --git a/compiler-rt/lib/asan/asan_rtl.cpp b/compiler-rt/lib/asan/asan_rtl.cpp index 2bbf0ac5240a..686834063149 100644 --- a/compiler-rt/lib/asan/asan_rtl.cpp +++ b/compiler-rt/lib/asan/asan_rtl.cpp @@ -433,6 +433,8 @@ static void AsanInitInternal() { // AsanInitInternal -> android_log_write -> __interceptor_strcmp AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL + ReplaceSystemMalloc(); DisableCoreDumperIfNecessary(); diff --git a/compiler-rt/lib/hwasan/hwasan.cpp b/compiler-rt/lib/hwasan/hwasan.cpp index 235aa73740af..41c0c8b51fdf 100644 --- a/compiler-rt/lib/hwasan/hwasan.cpp +++ b/compiler-rt/lib/hwasan/hwasan.cpp @@ -351,6 +351,7 @@ __attribute__((constructor(0))) void __hwasan_init() { SetPrintfAndReportCallback(AppendToErrorMessageBuffer); // This may call libc -> needs initialized shadow. AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL InitializeInterceptors(); InstallDeadlySignalHandlers(HwasanOnDeadlySignal); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 08c6062ba067..c4153201a1a0 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -925,9 +925,11 @@ inline void LogMessageOnPrintf(const char *str) {} #if SANITIZER_LINUX || SANITIZER_WIN_TRACE // Initialize Android logging. Any writes before this are silently lost. void AndroidLogInit(); +void OhosLogInit(); // OHOS_LOCAL void SetAbortMessage(const char *); #else inline void AndroidLogInit() {} +inline void OhosLogInit() {} // OHOS_LOCAL // FIXME: MacOS implementation could use CRSetCrashLogMessage. inline void SetAbortMessage(const char *) {} #endif diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp index 182155687d08..785c399d0238 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp @@ -896,7 +896,34 @@ void SetAbortMessage(const char *str) { if (&android_set_abort_message) android_set_abort_message(str); } +// OHOS_LOCAL begin +#elif SANITIZER_OHOS + +void AndroidLogInit() {} + +static atomic_uint8_t ohos_log_initialized; + +void OhosLogInit() { + atomic_store(&ohos_log_initialized, 1, memory_order_release); +} + +static bool ShouldLogAfterPrintf() { + return atomic_load(&ohos_log_initialized, memory_order_acquire); +} + +extern "C" SANITIZER_WEAK_ATTRIBUTE int musl_log(const char *fmt, ...); +void WriteOneLineToSyslog(const char *s) { + if (&musl_log) { + musl_log(s); + } else { + syslog(LOG_INFO, "%s", s); + } +} +void SetAbortMessage(const char *str) {} + #else +void OhosLogInit() {} + void AndroidLogInit() {} static bool ShouldLogAfterPrintf() { return true; } @@ -910,6 +937,7 @@ void LogMessageOnPrintf(const char *str) { if (common_flags()->log_to_syslog && ShouldLogAfterPrintf()) WriteToSyslog(str); } +// OHOS_LOCAL end #endif // SANITIZER_LINUX diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp index e0568c9b62d5..e7b7d6f31f0f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp @@ -1182,6 +1182,8 @@ void LogFullErrorReport(const char *buffer) { TraceLoggingValue(buffer, "AsanReportContents")); } } + +void OhosLogInit(void) {} // OHOS_LOCAL #endif // SANITIZER_WIN_TRACE void InitializePlatformCommonFlags(CommonFlags *cf) {} diff --git a/compiler-rt/lib/ubsan/ubsan_init.cpp b/compiler-rt/lib/ubsan/ubsan_init.cpp index 5802d58896f0..01080567331e 100644 --- a/compiler-rt/lib/ubsan/ubsan_init.cpp +++ b/compiler-rt/lib/ubsan/ubsan_init.cpp @@ -46,6 +46,7 @@ static void CommonStandaloneInit() { __sanitizer::InitializePlatformEarly(); __sanitizer_set_report_path(common_flags()->log_path); AndroidLogInit(); + OhosLogInit(); // OHOS_LOCAL InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); CommonInit(); -- Gitee From 1d363f4427de0f26db3fa661157b199d9a4a08cb Mon Sep 17 00:00:00 2001 From: Nikolai Kholiavin Date: Tue, 5 Dec 2023 12:42:31 +0000 Subject: [PATCH 11/44] [lldb][OHOS] Support remote HDC server Adds support for connecting to an HDC server on a remote machine. The address in the platform connection command is used as that remote machine address, meaning that device id can now be only set with HDC_UTID environment variable. The remote HDC server should be listening not on loopback to accept the lldb connection, and the HDC forwarded ports should be accessible. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8O7Q9 Signed-off-by: Nikolai Kholiavin --- .../Plugins/Platform/OHOS/HdcClient.cpp | 520 +++++++++++++++++- lldb/source/Plugins/Platform/OHOS/HdcClient.h | 34 +- .../Plugins/Platform/OHOS/PlatformOHOS.cpp | 31 +- .../Plugins/Platform/OHOS/PlatformOHOS.h | 3 + .../OHOS/PlatformOHOSRemoteGDBServer.cpp | 64 ++- .../OHOS/PlatformOHOSRemoteGDBServer.h | 3 + 6 files changed, 612 insertions(+), 43 deletions(-) diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp index eef720eaf78b..679b821f2d08 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp @@ -8,14 +8,17 @@ #include "HdcClient.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" #include "lldb/Host/ConnectionFileDescriptor.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/PosixApi.h" +#include "lldb/Utility/LLDBLog.h" #include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataEncoder.h" @@ -34,9 +37,13 @@ #include #include +#include #include #include +#include +#include #include +#include // On Windows, transitive dependencies pull in , which defines a // macro that clashes with a method name. @@ -121,11 +128,13 @@ Status HdcClient::CreateByDeviceID(const std::string &device_id, return error; } -HdcClient::HdcClient() {} +HdcClient::HdcClient(const std::string &connect_addr, + const std::string &device_id) + : m_connect_addr(connect_addr), m_device_id(device_id) {} -HdcClient::HdcClient(const std::string &device_id) : m_device_id(device_id) {} +HdcClient::HdcClient(HdcClient &&) = default; -HdcClient::~HdcClient() {} +HdcClient::~HdcClient() = default; void HdcClient::SetDeviceID(const std::string &device_id) { m_device_id = device_id; @@ -133,6 +142,10 @@ void HdcClient::SetDeviceID(const std::string &device_id) { const std::string &HdcClient::GetDeviceID() const { return m_device_id; } +bool HdcClient::IsServerLocal() { + return m_connect_addr == "localhost"; +} + namespace { typedef unsigned msg_len_t; struct ChannelHandShake { @@ -163,8 +176,9 @@ Status HdcClient::Connect() { return Status("invalid port specification: %s. $OHOS_HDC_SERVER_PORT must be a positive number in (0,65535]", env_port); } } - - std::string uri = "connect://localhost:" + port; + + // Support remote HDC server by providing connection address explicitly + std::string uri = "connect://" + m_connect_addr + ":" + port; m_conn->Connect(uri.c_str(), &error); ConnectionStatus status = eConnectionStatusError; if (error.Success()) { @@ -265,8 +279,8 @@ Status HdcClient::DeletePortForwarding(const uint16_t local_port, return ReadResponseStatus("Remove forward ruler success"); } -Status HdcClient::TransferFile(const char *direction, const FileSpec &src, - const FileSpec &dst) { +Status HdcClient::LocalTransferFile(const char *direction, const FileSpec &src, + const FileSpec &dst) { LLDB_MODULE_TIMER(LLDBPerformanceTagName::TAG_HDC); // OHOS_LOCAL llvm::SmallVector cwd; std::error_code ec = llvm::sys::fs::current_path(cwd); @@ -284,12 +298,459 @@ Status HdcClient::TransferFile(const char *direction, const FileSpec &src, return ReadResponseStatus("FileTransfer finish"); } +Status HdcClient::ExpectCommandMessagePrefix(uint16_t expected_command, + std::vector &message, + size_t prefix_size) { + uint16_t command; + Status error = ReadCommandMessagePrefix(command, message, prefix_size); + if (error.Fail()) + return error; + if (command != expected_command) + return Status("Unexpected HDC server command: %d, expected %d", command, + expected_command); + + return error; +} + +Status HdcClient::ExpectCommandMessage(uint16_t expected_command, + std::vector &message) { + return ExpectCommandMessagePrefix(expected_command, message, + std::numeric_limits::max()); +} + +template struct HdcIO; + +struct HdcTagIO { + enum class WireType : uint32_t { VARINT = 0, LENGTH_DELIMETED = 2 }; + + template + static llvm::Optional> ParseTag(InItT &InBegin, + InItT InEnd) { + llvm::Optional Tag = HdcIO::Parse(InBegin, InEnd); + return Tag.map([](U Val) { + return std::make_pair(static_cast(Val >> 3), + static_cast(Val & 0x7)); + }); + } + + template + static void SerializeTag(size_t Idx, WireType Type, OutItT &OutIt) { + HdcIO::Serialize( + static_cast(Type) | (static_cast(Idx) << 3), OutIt); + } +}; + +template +struct HdcIOBase { + using ValueT = T; + + static HdcTagIO::WireType GetWireType() { return TheType; } + + template + static llvm::Optional ParseTagged(size_t Idx, InItT &InBegin, + InItT InEnd) { + if (!HdcTagIO::ParseTag(InBegin, InEnd) + .map([Idx](auto P) { + return P.first == Idx && P.second == TheType; + }) + .value_or(false)) + return {}; + return DerivedT::Parse(InBegin, InEnd); + } + + template + static void SerializeTagged(size_t Idx, T Value, OutItT &OutIt) { + HdcTagIO::SerializeTag(Idx, TheType, OutIt); + DerivedT::Serialize(Value, OutIt); + } +}; + +template +struct HdcIO : HdcIOBase> { + static_assert(std::is_integral::value, "Don't know how to parse T"); + + template + static llvm::Optional Parse(InItT &InBegin, InItT InEnd) { + constexpr size_t NBytes = (sizeof(T) * 8 + 6) / 7; + T value = 0; + for (size_t c = 0; c < NBytes; ++c) { + uint8_t x; + if (InBegin == InEnd) + return {}; + x = *InBegin++; + value |= static_cast(x & 0x7Fu) << 7 * c; + if (!(x & 0x80)) + return value; + } + return {}; + } + + template static void Serialize(T value, OutItT &OutIt) { + constexpr size_t NBytes = (sizeof(T) * 8 + 6) / 7; + uint8_t b[NBytes]; + for (size_t i = 0; i < NBytes; ++i) { + b[i] = value & 0b0111'1111; + value >>= 7; + if (value) { + b[i] |= 0b1000'0000; + } else { + OutIt = std::copy_n(std::begin(b), i + 1, OutIt); + return; + } + } + } +}; + +template <> +struct HdcIO : HdcIOBase> { + template + static llvm::Optional Parse(InItT &InBegin, InItT InEnd) { + auto MaybeLen = HdcIO::Parse(InBegin, InEnd); + if (!MaybeLen) + return llvm::None; + size_t Len = *MaybeLen; + std::string Res; + Res.reserve(Len); + while (Res.size() < Len && InBegin != InEnd) + Res.push_back(*InBegin++); + if (Res.size() < Len) + return {}; + return Res; + } + + template + static void Serialize(const std::string &value, OutItT &OutIt) { + HdcIO::Serialize(value.size(), OutIt); + OutIt = std::copy_n(value.begin(), value.size(), OutIt); + } +}; + +template struct HdcTaggedIOHelper { + template + static llvm::Optional> Parse(InItT &InBegin, InItT InEnd) { + return std::tuple<>{}; + } + + template static void Serialize(OutItT &OutIt) {} +}; + +template +struct HdcTaggedIOHelper { + using ResT = llvm::Optional>; + + template static ResT Parse(InItT &InBegin, InItT InEnd) { + return HdcIO::ParseTagged(StartIdx, InBegin, InEnd) + .map([&](auto &&LHS) { + return HdcTaggedIOHelper::Parse(InBegin, InEnd) + .map([LHS = std::move(LHS)](auto &&RHS) { + return std::tuple_cat(std::make_tuple(std::move(LHS)), + std::move(RHS)); + }); + }) + .value_or(ResT(llvm::None)); + } + + template static ResT ParseOnce(InItT InBegin, InItT InEnd) { + return Parse(InBegin, InEnd); + } + + template + static void Serialize(const T &Arg, const Ts &...Args, OutItT &OutIt) { + HdcIO::SerializeTagged(StartIdx, std::move(Arg), OutIt); + HdcTaggedIOHelper::Serialize(std::move(Args)..., + OutIt); + } + + template + static void SerializeOnce(const T &Arg, const Ts &...Args, OutItT OutIt) { + return Serialize(Arg, Args..., OutIt); + } +}; + +template struct HdcTaggedIO : HdcTaggedIOHelper<1, Ts...> {}; + +template struct HdcTaggedIOTuple; + +template +struct HdcTaggedIOTuple> : HdcTaggedIO {}; + +using HdcTransferConfig = std::tuple; + +using HdcTransferPayload = std::tuple; + +using HdcFileMode = std::tuple; // fullName + +enum HdcCommand : uint16_t { + CMD_KERNEL_WAKEUP_SLAVETASK = 12, + CMD_FILE_INIT = 3000, + CMD_FILE_CHECK, + CMD_FILE_BEGIN, + CMD_FILE_DATA, + CMD_FILE_FINISH, + CMD_FILE_MODE = 3006 +}; + +static constexpr size_t HdcTransferPayloadPrefixReserve = 64; +static constexpr size_t HdcTransferPayloadMaxChunkSize = 49152; + +Status HdcClient::FileCheck(int FD, size_t &file_size) { + std::vector msg; + Status error = ExpectCommandMessage(HdcCommand::CMD_FILE_MODE, msg); + if (error.Fail()) + return error; + + auto maybe_file_mode = HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); + if (!maybe_file_mode) + return Status("Could not parse HDC server FileMode"); + + auto &file_mode = *maybe_file_mode; + + uint32_t perms = static_cast(std::get<0>(file_mode)); + auto EC = llvm::sys::fs::setPermissions( + FD, static_cast(perms) & llvm::sys::fs::all_perms); + if (EC) + return Status(EC); + + error = SendCommandMessage(HdcCommand::CMD_FILE_MODE, {}); + if (error.Fail()) + return error; + + error = ExpectCommandMessage(HdcCommand::CMD_FILE_CHECK, msg); + if (error.Fail()) + return error; + + auto transfer_config = + HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); + if (!transfer_config.has_value()) + return Status("Could not parse HDC server TransferConfig"); + + if (auto compress_type = std::get<7>(*transfer_config)) + return Status("Compression is not supported"); + + file_size = std::get<0>(*transfer_config); + + return SendCommandMessage(HdcCommand::CMD_FILE_BEGIN, {}); +} + +Status HdcClient::PullFileChunk(std::vector &buffer) { + buffer.clear(); + + std::vector msg; + Status error = ExpectCommandMessagePrefix(HdcCommand::CMD_FILE_DATA, msg, + HdcTransferPayloadPrefixReserve); + if (error.Fail()) + return error; + + auto transfer_payload = + HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); + if (!transfer_payload.has_value()) + return Status("Could not parse HDC server TransferPayload"); + + if (auto compress_type = std::get<1>(*transfer_payload)) + return Status("Compression is not supported"); + + uint32_t read_bytes = std::get<3>(*transfer_payload); + buffer.resize(read_bytes, 0); + error = ReadAllBytes(buffer.data(), buffer.size()); + if (error.Fail()) + buffer.clear(); + + return error; +} + Status HdcClient::RecvFile(const FileSpec &src, const FileSpec &dst) { - return TransferFile("recv", src, dst); + if (IsServerLocal()) + return LocalTransferFile("recv", src, dst); + + const auto local_file_path = dst.GetPath(); + llvm::FileRemover local_file_remover(local_file_path); + + int dst_file_fd; + auto EC = llvm::sys::fs::openFileForWrite(local_file_path, dst_file_fd); + if (EC) + return Status("Unable to open local file %s", local_file_path.c_str()); + + std::stringstream cmd; + cmd << "file recv remote -m"; + cmd << " " << src.GetPath() << " " << dst.GetPath(); + Status error = SendMessage(cmd.str()); + if (error.Fail()) + return error; + + size_t cur_size = 0, all_size = 0; + error = FileCheck(dst_file_fd, all_size); + if (error.Fail()) + return error; + + llvm::raw_fd_ostream dst_file(dst_file_fd, true); + + std::vector buf; + while (cur_size < all_size) { + error = PullFileChunk(buf); + if (error.Fail()) + return error; + dst_file.write(buf.data(), buf.size()); + cur_size += buf.size(); + } + + error = SendCommandMessage(HdcCommand::CMD_FILE_FINISH, {}); + if (error.Fail()) + return error; + error = ReadResponseStatus("FileTransfer finish"); + if (error.Fail()) + return error; + + dst_file.close(); + if (dst_file.has_error()) + return Status("Failed to write file %s", local_file_path.c_str()); + + local_file_remover.releaseFile(); + return error; +} + +Status HdcClient::FileInit(size_t file_size, uint32_t perm, uint32_t u_id, + uint32_t g_id, const std::string &remote_path) { + std::vector msg; + Status error = ExpectCommandMessage(HdcCommand::CMD_FILE_INIT, msg); + if (error.Fail()) + return error; + + error = SendCommandMessage(HdcCommand::CMD_KERNEL_WAKEUP_SLAVETASK, {}); + if (error.Fail()) + return error; + + constexpr uint64_t IFREG_MASK = 0100000; + msg.clear(); + HdcTaggedIOTuple::SerializeOnce(perm | IFREG_MASK, // perm + u_id, // u_id + g_id, // g_id + "", // context + "", // fullName + std::back_inserter(msg)); + error = SendCommandMessage(HdcCommand::CMD_FILE_MODE, msg); + if (error.Fail()) + return error; + + error = ExpectCommandMessage(HdcCommand::CMD_FILE_MODE, msg); + if (error.Fail()) + return error; + + msg.clear(); + HdcTaggedIOTuple::SerializeOnce(file_size, // fileSize + 0, // atime + 0, // mtime + "", // options + remote_path, // path + "", // optionalName + false, // updateIfNew + 0, // compressType + false, // holdTimestamp + "", // funcName + "", // clientCwd + "", // reserve1 + "", // reserve2 + std::back_inserter(msg)); + error = SendCommandMessage(HdcCommand::CMD_FILE_CHECK, msg); + if (error.Fail()) + return error; + + return ExpectCommandMessage(HdcCommand::CMD_FILE_BEGIN, msg); +} + +Status HdcClient::PushFileChunk(std::vector &buffer, size_t chunk_size, + size_t index) { + std::fill_n(buffer.begin(), HdcTransferPayloadPrefixReserve, 0); + HdcTaggedIOTuple::SerializeOnce( + index, // index + 0, // compressType + chunk_size, // compressSize + chunk_size, // uncompressSize + buffer.begin()); + return SendCommandMessage(CMD_FILE_DATA, buffer); } Status HdcClient::SendFile(const FileSpec &src, const FileSpec &dst) { - return TransferFile("send", src, dst); + if (IsServerLocal()) + return LocalTransferFile("send", src, dst); + + const auto local_file_path = src.GetPath(); + std::ifstream src_file(local_file_path.c_str(), std::ios::in | std::ios::binary); + if (!src_file.is_open()) + return Status("Unable to open local file %s", local_file_path.c_str()); + + std::stringstream cmd; + cmd << "file send remote -m " << src.GetPath() << " " << dst.GetPath(); + Status error = SendMessage(cmd.str()); + if (error.Fail()) + return error; + + llvm::sys::fs::file_status status; + auto EC = llvm::sys::fs::status(local_file_path, status); + if (EC) + return Status(EC); + + error = FileInit(status.getSize(), status.permissions(), status.getUser(), + status.getGroup(), dst.GetPath()); + if (error.Fail()) + return error; + + std::vector buffer; + size_t sent_bytes = 0; + while (!src_file.eof()) { + buffer.resize(HdcTransferPayloadPrefixReserve + + HdcTransferPayloadMaxChunkSize); + if (src_file + .read(buffer.data() + HdcTransferPayloadPrefixReserve, + HdcTransferPayloadMaxChunkSize) + .bad()) + break; + size_t chunk_size = src_file.gcount(); + buffer.resize(HdcTransferPayloadPrefixReserve + chunk_size); + error = PushFileChunk(buffer, chunk_size, sent_bytes); + if (error.Fail()) + return error; + sent_bytes += chunk_size; + } + + if (src_file.bad()) + return Status("Failed read on %s", local_file_path.c_str()); + if (sent_bytes < status.getSize()) { + m_conn.reset(); + return Status("Failed to read all of the bytes from %s: read %zu/%zu", + local_file_path.c_str(), sent_bytes, status.getSize()); + } + + error = ExpectCommandMessage(HdcCommand::CMD_FILE_FINISH, buffer); + if (error.Fail()) + return error; + + error = SendCommandMessage(HdcCommand::CMD_FILE_FINISH, {}); + if (error.Fail()) + return error; + + return ReadResponseStatus("FileTransfer finish"); } Status HdcClient::SendMessage(llvm::StringRef packet, const bool reconnect) { @@ -314,6 +775,13 @@ Status HdcClient::SendMessage(llvm::StringRef packet, const bool reconnect) { return error; } +Status HdcClient::SendCommandMessage(uint16_t command, llvm::ArrayRef packet) { + llvm::SmallVector buf(sizeof(command) + packet.size()); + std::copy_n(reinterpret_cast(&command), sizeof(command), buf.begin()); + std::copy_n(packet.begin(), packet.size(), buf.begin() + sizeof(command)); + return SendMessage(llvm::StringRef(buf.data(), buf.size()), false); +} + Status HdcClient::ReadMessage(std::vector &message) { message.clear(); @@ -331,6 +799,40 @@ Status HdcClient::ReadMessage(std::vector &message) { return error; } +Status HdcClient::ReadCommandMessagePrefix(uint16_t &command, std::vector &message, size_t prefix_size) { + message.clear(); + + msg_len_t packet_len; + auto error = ReadAllBytes(&packet_len, sizeof(packet_len)); + if (error.Fail()) + return error; + + packet_len = htonl(packet_len); + if (packet_len < sizeof(command)) + return Status("Message too small to contain a command"); + + error = ReadAllBytes(&command, sizeof(command)); + if (error.Fail()) { + command = 0; + return error; + } + + message.resize(std::min(packet_len - sizeof(command), prefix_size), 0); + error = ReadAllBytes(&message[0], message.size()); + if (error.Fail()) { + command = 0; + message.clear(); + } + + return error; +} + +Status HdcClient::ReadCommandMessage(uint16_t &command, + std::vector &message) { + return ReadCommandMessagePrefix(command, message, + std::numeric_limits::max()); +} + Status HdcClient::ReadMessageStream(std::vector &message, milliseconds timeout) { auto start = steady_clock::now(); diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.h b/lldb/source/Plugins/Platform/OHOS/HdcClient.h index 39b6569b447a..5e17b0d8f8ef 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.h +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.h @@ -34,8 +34,10 @@ public: static Status CreateByDeviceID(const std::string &device_id, HdcClient &hdc); - HdcClient(); - explicit HdcClient(const std::string &device_id); + explicit HdcClient(const std::string &connect_addr, + const std::string &device_id = ""); + + HdcClient(HdcClient &&); ~HdcClient(); @@ -63,19 +65,38 @@ public: std::string *output); private: + bool IsServerLocal(); Status Connect(); - Status TransferFile(const char *direction, const FileSpec &src, - const FileSpec &dst); + Status LocalTransferFile(const char *direction, const FileSpec &src, + const FileSpec &dst); + + Status FileCheck(int FD, size_t &file_size); + Status PullFileChunk(std::vector &buffer); + + Status FileInit(size_t file_size, uint32_t perm, uint32_t u_id, uint32_t g_id, + const std::string &remote_path); + Status PushFileChunk(std::vector &buffer, size_t chunk_size, + size_t index); void SetDeviceID(const std::string &device_id); Status SendMessage(llvm::StringRef packet, const bool reconnect = true); - Status SendDeviceMessage(const std::string &packet); - Status ReadMessage(std::vector &message); + Status SendCommandMessage(uint16_t command, llvm::ArrayRef packet); + + Status ReadCommandMessagePrefix(uint16_t &command, std::vector &message, + size_t prefix_size); + Status ReadCommandMessage(uint16_t &command, std::vector &message); + + Status ExpectCommandMessage(uint16_t expected_command, + std::vector &message); + Status ExpectCommandMessagePrefix(uint16_t expected_command, + std::vector &message, + size_t prefix_size); + Status ReadMessageStream(std::vector &message, std::chrono::milliseconds timeout); @@ -83,6 +104,7 @@ private: Status ReadAllBytes(void *buffer, size_t size); + std::string m_connect_addr; std::string m_device_id; std::unique_ptr m_conn; }; diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp index 5193e17edbd1..6a1bb9044646 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp @@ -129,8 +129,13 @@ llvm::StringRef PlatformOHOS::GetPluginName() { return GetPluginNameStatic(IsHost()); } +HdcClient PlatformOHOS::CreateHdcClient() { + return HdcClient(m_connect_addr, m_device_id); +} + Status PlatformOHOS::ConnectRemote(Args &args) { m_device_id.clear(); + m_connect_addr = "localhost"; if (IsHost()) { return Status("can't connect to the host platform '%s', always connected", @@ -146,17 +151,26 @@ Status PlatformOHOS::ConnectRemote(Args &args) { llvm::Optional uri = URI::Parse(url); if (!uri) return Status("Invalid URL: %s", url); - if (uri->hostname != "localhost") - m_device_id = static_cast(uri->hostname); + + Log *log = GetLog(LLDBLog::Platform); + if (PlatformOHOSRemoteGDBServer::IsHostnameDeviceID( + uri->hostname)) { // accepts no (empty) hostname too + m_device_id = uri->hostname.str(); + LLDB_LOG(log, "Treating hostname as device id: \"{0}\"", m_device_id); + } else { + m_connect_addr = uri->hostname.str(); + LLDB_LOG(log, "Treating hostname as remote HDC server address: \"{0}\"", + m_connect_addr); + } auto error = PlatformLinux::ConnectRemote(args); if (error.Success()) { - HdcClient adb; - error = HdcClient::CreateByDeviceID(m_device_id, adb); + HdcClient hdc(m_connect_addr); + error = HdcClient::CreateByDeviceID(m_device_id, hdc); if (error.Fail()) return error; - m_device_id = adb.GetDeviceID(); + m_device_id = hdc.GetDeviceID(); } return error; } @@ -171,7 +185,7 @@ Status PlatformOHOS::GetFile(const FileSpec &source, source_spec = GetRemoteWorkingDirectory().CopyByAppendingPathComponent( source_spec.GetCString(false)); - HdcClient hdc(m_device_id); + HdcClient hdc = CreateHdcClient(); Status error = hdc.RecvFile(source_spec, destination); return error; } @@ -188,7 +202,7 @@ Status PlatformOHOS::PutFile(const FileSpec &source, destination_spec.GetCString(false)); // TODO: Set correct uid and gid on remote file. - HdcClient hdc(m_device_id); + HdcClient hdc = CreateHdcClient(); Status error = hdc.SendFile(source, destination_spec); return error; } @@ -209,6 +223,7 @@ Status PlatformOHOS::DisconnectRemote() { Status error = PlatformLinux::DisconnectRemote(); if (error.Success()) { m_device_id.clear(); + m_connect_addr.clear(); m_sdk_version = 0; m_remote_platform_sp.reset(); } @@ -227,7 +242,7 @@ uint32_t PlatformOHOS::GetSdkVersion() { return m_sdk_version; std::string version_string; - HdcClient hdc(m_device_id); + HdcClient hdc = CreateHdcClient(); Status error = hdc.Shell("param get const.ohos.apiversion", seconds(5), &version_string); version_string = llvm::StringRef(version_string).trim().str(); diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h index 83645ff209a5..fdbd1d42535a 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h @@ -66,7 +66,10 @@ protected: llvm::StringRef GetLibdlFunctionDeclarations(lldb_private::Process *process) override; + HdcClient CreateHdcClient(); + private: + std::string m_connect_addr; std::string m_device_id; uint32_t m_sdk_version; diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp index 28f996a6cdb7..0a6a7b208f40 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp @@ -26,13 +26,13 @@ static const lldb::pid_t g_remote_platform_pid = static uint16_t g_hdc_forward_port_offset = 0; static Status ForwardPortWithHdc( - const uint16_t local_port, const uint16_t remote_port, - llvm::StringRef remote_socket_name, + const std::string &connect_addr, const uint16_t local_port, + const uint16_t remote_port, llvm::StringRef remote_socket_name, const llvm::Optional &socket_namespace, std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); - HdcClient hdc; + HdcClient hdc(connect_addr); auto error = HdcClient::CreateByDeviceID(device_id, hdc); if (error.Fail()) return error; @@ -56,19 +56,22 @@ static Status ForwardPortWithHdc( *socket_namespace); } -static Status DeleteForwardPortWithHdc(std::pair ports, +static Status DeleteForwardPortWithHdc(const std::string &connect_addr, + std::pair ports, const std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); LLDB_LOGF(log, "Delete port forwarding %d -> %d, device=%s", ports.first, ports.second, device_id.c_str()); - HdcClient hdc(device_id); + HdcClient hdc(connect_addr, device_id); return hdc.DeletePortForwarding(ports); } -static Status DeleteForwardPortWithHdc(std::pair remote_socket, - const llvm::Optional &socket_namespace, - const std::string &device_id) { +static Status DeleteForwardPortWithHdc( + const std::string &connect_addr, + std::pair remote_socket, + const llvm::Optional &socket_namespace, + const std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); uint16_t local_port = remote_socket.first; @@ -78,7 +81,7 @@ static Status DeleteForwardPortWithHdc(std::pair remote_s if (!socket_namespace) return Status("Invalid socket namespace"); - HdcClient hdc(device_id); + HdcClient hdc(connect_addr, device_id); return hdc.DeletePortForwarding(local_port, remote_socket_name, *socket_namespace); } @@ -102,14 +105,14 @@ static Status FindUnusedPort(uint16_t &port) { return error; } -PlatformOHOSRemoteGDBServer::PlatformOHOSRemoteGDBServer() {} +PlatformOHOSRemoteGDBServer::PlatformOHOSRemoteGDBServer() = default; PlatformOHOSRemoteGDBServer::~PlatformOHOSRemoteGDBServer() { for (const auto &it : m_port_forwards) { - DeleteForwardPortWithHdc(it.second, m_device_id); + DeleteForwardPortWithHdc(m_connect_addr, it.second, m_device_id); } for (const auto &it_socket : m_remote_socket_name) { - DeleteForwardPortWithHdc(it_socket.second, m_socket_namespace, m_device_id); + DeleteForwardPortWithHdc(m_connect_addr, it_socket.second, m_socket_namespace, m_device_id); } } @@ -135,8 +138,14 @@ bool PlatformOHOSRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) { return m_gdb_client_up->KillSpawnedProcess(pid); } +bool PlatformOHOSRemoteGDBServer::IsHostnameDeviceID(llvm::StringRef hostname) { + return hostname != "localhost" && !hostname.contains(':') && + !hostname.contains('.'); +} + Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { m_device_id.clear(); + m_connect_addr = "localhost"; if (args.GetArgumentCount() != 1) return Status( @@ -149,8 +158,16 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { uri = URI::Parse(url); if (!uri) return Status("Invalid URL: %s", url); - if (uri->hostname != "localhost") - m_device_id = static_cast(uri->hostname); + + Log *log = GetLog(LLDBLog::Platform); + if (IsHostnameDeviceID(uri->hostname)) { // accepts no (empty) hostname too + m_device_id = uri->hostname.str(); + LLDB_LOG(log, "Treating hostname as device id: \"{0}\"", m_device_id); + } else { + m_connect_addr = uri->hostname.str(); + LLDB_LOG(log, "Treating hostname as remote HDC server address: \"{0}\"", + m_connect_addr); + } m_socket_namespace.reset(); if (uri->scheme == "unix-connect") @@ -168,7 +185,6 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { args.ReplaceArgumentAtIndex(0, connect_url); - Log *log = GetLog(LLDBLog::Platform); LLDB_LOGF(log, "Rewritten platform connect URL: %s", connect_url.c_str()); error = PlatformRemoteGDBServer::ConnectRemote(args); if (error.Fail()) @@ -180,6 +196,7 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { Status PlatformOHOSRemoteGDBServer::DisconnectRemote() { DeleteForwardPort(g_remote_platform_pid); g_hdc_forward_port_offset = 0; + m_connect_addr.clear(); return PlatformRemoteGDBServer::DisconnectRemote(); } @@ -189,7 +206,8 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { auto it = m_port_forwards.find(pid); auto it_socket = m_remote_socket_name.find(pid); if (it != m_port_forwards.end() && it->second.second != 0) { - const auto error = DeleteForwardPortWithHdc(it->second, m_device_id); + const auto error = + DeleteForwardPortWithHdc(m_connect_addr, it->second, m_device_id); if (error.Fail()) { LLDB_LOGF(log, "Failed to delete port forwarding (pid=%" PRIu64 ", fwd=(%d -> %d), device=%s): %s", @@ -200,7 +218,8 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { } if(it_socket != m_remote_socket_name.end()) { - const auto error_Socket = DeleteForwardPortWithHdc(it_socket->second, m_socket_namespace, m_device_id); + const auto error_Socket = DeleteForwardPortWithHdc( + m_connect_addr, it_socket->second, m_socket_namespace, m_device_id); if (error_Socket.Fail()) { LLDB_LOGF(log, "Failed to delete port forwarding (pid=%" PRIu64 ", fwd=(%d->%s)device=%s): %s", pid, it_socket->second.first, it_socket->second.second.c_str(), m_device_id.c_str(),error_Socket.AsCString()); @@ -226,8 +245,9 @@ Status PlatformOHOSRemoteGDBServer::MakeConnectURL( if (error.Fail()) return error; - error = ForwardPortWithHdc(local_port, remote_port, remote_socket_name, - m_socket_namespace, m_device_id); + error = + ForwardPortWithHdc(m_connect_addr, local_port, remote_port, + remote_socket_name, m_socket_namespace, m_device_id); if (error.Success()) { if (remote_port != 0){ m_port_forwards[pid] = {local_port, remote_port}; @@ -235,8 +255,10 @@ Status PlatformOHOSRemoteGDBServer::MakeConnectURL( else{ m_remote_socket_name[pid] ={local_port, remote_socket_name.str()}; } + // Connect to local_port on a potentially remote machine with running HDC + // server std::ostringstream url_str; - url_str << "connect://localhost:" << local_port; + url_str << "connect://" << m_connect_addr << ":" << local_port; connect_url = url_str.str(); break; } @@ -262,6 +284,8 @@ lldb::ProcessSP PlatformOHOSRemoteGDBServer::ConnectProcess( return nullptr; } + // If m_connect_addr is remote, this connects to a remote HDC server, assuming + // that all of the needed ports are open std::string new_connect_url; error = MakeConnectURL(s_remote_gdbserver_fake_pid--, (*uri->port) ? (*uri->port) : 0, uri->path, diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h index 680ddc1e733c..cb130574d3f6 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h @@ -38,7 +38,10 @@ public: lldb_private::Target *target, lldb_private::Status &error) override; + static bool IsHostnameDeviceID(llvm::StringRef hostname); + protected: + std::string m_connect_addr; std::string m_device_id; std::map> m_port_forwards; std::map> m_remote_socket_name; -- Gitee From 77cce2a12da41d3c500eee70e0f2b4103b0cc1a8 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Thu, 1 Feb 2024 11:06:15 +0000 Subject: [PATCH 12/44] !373 [Build] Using gzip compression instead of bzip2 compression * [Build] fix packaging * [Build] Add option to choose compression format, I8QMD2 --- llvm-build/build.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index dfc057a62cfe..f35164454c3b 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -66,6 +66,7 @@ class BuildConfig(): self.enable_monitoring = args.enable_monitoring self.build_libs = args.build_libs self.build_libs_flags = args.build_libs_flags + self.compression_format = args.compression_format self.discover_paths() @@ -81,6 +82,9 @@ class BuildConfig(): self.CLANG_VERSION = prebuilts_clang_version self.MINGW_TRIPLE = 'x86_64-windows-gnu' self.build_libs_with_hb = self.build_libs_flags == 'OH' or self.build_libs_flags == 'BOTH' + + self.ARCHIVE_EXTENSION = '.tar.' + self.compression_format + self.ARCHIVE_OPTION = '-c' + ('j' if self.compression_format == "bz2" else 'z') logging.basicConfig(level=logging.INFO) def discover_paths(self): @@ -230,6 +234,15 @@ class BuildConfig(): action='store_true', default=False, help='Enable lldb performance monitoring') + + compression_formats = ['bz2', 'gz'] + + parser.add_argument( + '--compression-format', + choices=compression_formats, + default='bz2', + help='Choose compression output format (bz2 or gz)' + ) def parse_args(self): @@ -1828,9 +1841,9 @@ class LlvmPackage(BuildUtils): #Package libcxx-ndk for host in hosts_list: tarball_name = 'libcxx-ndk-%s-%s' % (self.build_config.build_name, host) - package_path = '%s%s' % (self.merge_packages_path(tarball_name), '.tar.bz2') + package_path = '%s%s' % (self.merge_packages_path(tarball_name), self.build_config.ARCHIVE_EXTENSION) self.logger().info('Packaging %s', package_path) - args = ['tar', '-chjC', self.build_config.OUT_PATH, '-f', package_path, 'libcxx-ndk'] + args = ['tar', self.build_config.ARCHIVE_OPTION, '-h', '-C', self.build_config.OUT_PATH, '-f', package_path, 'libcxx-ndk'] self.check_create_dir(self.build_config.PACKAGES_PATH) self.check_call(args) @@ -2070,9 +2083,9 @@ class LlvmPackage(BuildUtils): def package_up_resulting(self, package_name, host, install_host_dir): # Package up the resulting trimmed install/ directory. tarball_name = '%s-%s' % (package_name, host) - package_path = '%s%s' % (self.merge_packages_path(tarball_name), '.tar.bz2') + package_path = '%s%s' % (self.merge_packages_path(tarball_name), self.build_config.ARCHIVE_EXTENSION) self.logger().info('Packaging %s', package_path) - args = ['tar', '-cjC', install_host_dir, '-f', package_path, package_name] + args = ['tar', self.build_config.ARCHIVE_OPTION, '-C', install_host_dir, '-f', package_path, package_name] if host.startswith('windows'): # windows do not support symlinks, # replace them with file copies @@ -2084,9 +2097,9 @@ class LlvmPackage(BuildUtils): # Package ohos NDK if os.path.exists(self.merge_out_path('sysroot')): tarball_ndk_name = 'ohos-sysroot-%s' % self.build_config.build_name - package_ndk_path = '%s%s' % (self.merge_packages_path(tarball_ndk_name), '.tar.bz2') + package_ndk_path = '%s%s' % (self.merge_packages_path(tarball_ndk_name), self.build_config.ARCHIVE_EXTENSION) self.logger().info('Packaging %s', package_ndk_path) - args = ['tar', '-chjC', self.build_config.OUT_PATH, '-f', package_ndk_path, 'sysroot'] + args = ['tar', self.build_config.ARCHIVE_OPTION, '-h', '-C', self.build_config.OUT_PATH, '-f', package_ndk_path, 'sysroot'] self.check_call(args) def get_dependency_list(self, install_dir, lib): -- Gitee From 05be96a0332402f1213de4c1dba7e57d5398df59 Mon Sep 17 00:00:00 2001 From: yinchuang Date: Sat, 3 Feb 2024 19:52:13 +0800 Subject: [PATCH 13/44] [Asan] Fix bug of sendmsg hook Issue:I90UZR Signed-off-by: yinchuang --- .../lib/sanitizer_common/sanitizer_common_interceptors.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index 006d5a9f5d38..cd60952a2605 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -3250,7 +3250,7 @@ static void read_msghdr_control(void *ctx, void *control, uptr controllen) { __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p; COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len)); - if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break; + if (control_end - p < RoundUpTo(cmsg->cmsg_len, sizeof(uptr))) break; // OHOS_LOCAL COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level, sizeof(cmsg->cmsg_level)); -- Gitee From 1c54fcedbbcceb7fad7270f46d5d9144ecbf431e Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Mon, 5 Feb 2024 14:01:20 +0300 Subject: [PATCH 14/44] [Build] Add clang, lld for aarch64, copy runtimes Enable clang, lld, clang-tools-extra projects in build script. Copy runtimes from main toolchain build to ohos-aarch64-install. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I9083P Signed-off-by: Lyupa Anastasia --- llvm-build/README.md | 4 ++-- llvm-build/build-ohos-aarch64.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/llvm-build/README.md b/llvm-build/README.md index 1a256676fd34..c04c18af0012 100644 --- a/llvm-build/README.md +++ b/llvm-build/README.md @@ -114,7 +114,7 @@ OHOS fetch prebuilts from: https://mirrors.huaweicloud.com/openharmony/compiler/ License: Apache License v2.0 with LLVM Exceptions ``` -### Build process of AArch64 LLVM components for Ark Runtime +### Build process of AArch64 toolchain First build toolchain on Linux. Here is an example of starting build process on Linux: @@ -134,7 +134,7 @@ build-ohos-aarch64.py options: ```
-When build successfully completed, artifacts will be available in `out/ohos-aarch64-install` directory, including LLVM tools and libLLVM.so for aarch64. +When build successfully completed, artifacts will be available in `out/ohos-aarch64-install` directory, including clang, lld, runtimes, LLVM tools and libLLVM.so for aarch64. ## Function Introduction diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index f197e3ea75cb..35bceab1a12b 100644 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -64,7 +64,7 @@ def main(): llvm_defines['LLVM_INCLUDE_TESTS'] = 'OFF' llvm_defines['LLVM_BUILD_TOOLS'] = 'ON' llvm_defines['LLVM_ENABLE_ZLIB'] = 'OFF' - llvm_defines['LLVM_DISTRIBUTION_COMPONENTS'] = 'cmake-exports;llvm-headers;LLVM' + llvm_defines['LLVM_ENABLE_PROJECTS'] = 'clang;lld;clang-tools-extra' llvm_defines['LLVM_CONFIG_PATH'] = os.path.join(llvm_root, 'bin', 'llvm-config') llvm_defines['LLVM_TABLEGEN'] = os.path.join(llvm_root, 'bin', 'llvm-tblgen') llvm_defines['CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN'] = llvm_root @@ -111,6 +111,11 @@ def main(): build_utils.invoke_ninja(out_path=llvm_path, env=env, target=None, install=True) + # Copy required aarch64-linux-ohos libs from main toolchain build. + build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', llvm_triple), + os.path.join(llvm_install, 'lib', llvm_triple)) + build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', 'clang', '15.0.4', 'lib', llvm_triple), + os.path.join(llvm_install, 'lib', 'clang', '15.0.4', 'lib', llvm_triple)) if __name__ == '__main__': main() -- Gitee From c55e53758a80769bc005a0da78661bed96f15aae Mon Sep 17 00:00:00 2001 From: Denis Zavedeev Date: Mon, 5 Feb 2024 16:47:34 +0300 Subject: [PATCH 15/44] [ImplicitNullChecks] Improve ImplicitNullChecks support for aarch64 Signed-off-by: Denis Zavedeev --- llvm/lib/Target/AArch64/AArch64InstrInfo.cpp | 8 + llvm/lib/Target/AArch64/AArch64InstrInfo.h | 6 + .../CodeGen/AArch64/implicit-null-check.ll | 70 +++-- .../AArch64/ptr32-implicit-null-checks.ll | 276 ++++++++++++++++++ 4 files changed, 343 insertions(+), 17 deletions(-) create mode 100644 llvm/test/CodeGen/AArch64/ptr32-implicit-null-checks.ll diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp index 835a7b6cc81d..98c4f1ada15b 100644 --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -8141,6 +8141,14 @@ AArch64InstrInfo::getTailDuplicateSize(CodeGenOpt::Level OptLevel) const { return OptLevel >= CodeGenOpt::Aggressive ? 6 : 2; } +// OHOS_LOCAL begin +bool AArch64InstrInfo::preservesZeroValueInReg( + const MachineInstr *MI, const Register NullValueReg, + const TargetRegisterInfo *TRI) const { + return !MI->modifiesRegister(NullValueReg, TRI); +} +// OHOS_LOCAL end + unsigned llvm::getBLRCallOpcode(const MachineFunction &MF) { if (MF.getSubtarget().hardenSlsBlr()) return AArch64::BLRNoIP; diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.h b/llvm/lib/Target/AArch64/AArch64InstrInfo.h index b7a6ac301cdc..7059de6b272c 100644 --- a/llvm/lib/Target/AArch64/AArch64InstrInfo.h +++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.h @@ -194,6 +194,12 @@ public: // with subreg operands to foldMemoryOperandImpl. bool isSubregFoldable() const override { return true; } + // OHOS_LOCAL begin + bool preservesZeroValueInReg(const MachineInstr *MI, + const Register NullValueReg, + const TargetRegisterInfo *TRI) const override; + // OHOS_LOCAL end + using TargetInstrInfo::foldMemoryOperandImpl; MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, diff --git a/llvm/test/CodeGen/AArch64/implicit-null-check.ll b/llvm/test/CodeGen/AArch64/implicit-null-check.ll index ed236e0010fb..59bbfda757c8 100644 --- a/llvm/test/CodeGen/AArch64/implicit-null-check.ll +++ b/llvm/test/CodeGen/AArch64/implicit-null-check.ll @@ -224,11 +224,13 @@ define i32 @imp_null_check_add_result(i32* %x, i32 %p) { define i32 @imp_null_check_hoist_over_udiv(i32* %x, i32 %a, i32 %b) { ; CHECK-LABEL: imp_null_check_hoist_over_udiv: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: cbz x0, .LBB9_2 +;; OHOS_LOCAL begin +; CHECK-NEXT: .Ltmp6: +; CHECK-NEXT: ldr w9, [x0] // on-fault: .LBB9_2 ; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: udiv w8, w1, w2 -; CHECK-NEXT: ldr w9, [x0] ; CHECK-NEXT: add w0, w9, w8 +;; OHOS_LOCAL end ; CHECK-NEXT: ret ; CHECK-NEXT: .LBB9_2: ; CHECK-NEXT: mov w0, #42 @@ -247,21 +249,20 @@ define i32 @imp_null_check_hoist_over_udiv(i32* %x, i32 %a, i32 %b) { ret i32 %res } - -; TODO: We should be able to hoist this - we can on x86, why isn't this -; working for aarch64? Aliasing? define i32 @imp_null_check_hoist_over_unrelated_load(i32* %x, i32* %y, i32* %z) { +;; OHOS_LOCAL begin ; CHECK-LABEL: imp_null_check_hoist_over_unrelated_load: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: cbz x0, .LBB10_2 +; CHECK-NEXT: .Ltmp7: +; CHECK-NEXT: ldr w0, [x0] // on-fault: .LBB10_2 ; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: ldr w8, [x1] -; CHECK-NEXT: ldr w0, [x0] ; CHECK-NEXT: str w8, [x2] ; CHECK-NEXT: ret ; CHECK-NEXT: .LBB10_2: ; CHECK-NEXT: mov w0, #42 ; CHECK-NEXT: ret +;; OHOS_LOCAL end entry: %c = icmp eq i32* %x, null br i1 %c, label %is_null, label %not_null, !make.implicit !0 @@ -279,7 +280,9 @@ define i32 @imp_null_check_hoist_over_unrelated_load(i32* %x, i32* %y, i32* %z) define i32 @imp_null_check_gep_load_with_use_dep(i32* %x, i32 %a) { ; CHECK-LABEL: imp_null_check_gep_load_with_use_dep: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: .Ltmp6: +;; OHOS_LOCAL begin +; CHECK-NEXT: .Ltmp8: +;; OHOS_LOCAL end ; CHECK-NEXT: ldr w8, [x0] // on-fault: .LBB11_2 ; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: add w9, w0, w1 @@ -357,14 +360,15 @@ not_null: ret i32 %t } -; TODO: We can fold to implicit null here, not sure why this isn't working define void @imp_null_check_store(i32* %x) { +;; OHOS_LOCAL begin ; CHECK-LABEL: imp_null_check_store: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: cbz x0, .LBB14_2 -; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: mov w8, #1 -; CHECK-NEXT: str w8, [x0] +; CHECK-NEXT: .Ltmp9: +; CHECK-NEXT: str w8, [x0] // on-fault: .LBB14_2 +; CHECK-NEXT: // %bb.1: // %not_null +;; OHOS_LOCAL end ; CHECK-NEXT: .LBB14_2: // %common.ret ; CHECK-NEXT: ret entry: @@ -379,14 +383,15 @@ define void @imp_null_check_store(i32* %x) { ret void } -;; TODO: can be implicit define void @imp_null_check_unordered_store(i32* %x) { +;; OHOS_LOCAL begin ; CHECK-LABEL: imp_null_check_unordered_store: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: cbz x0, .LBB15_2 -; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: mov w8, #1 -; CHECK-NEXT: str w8, [x0] +; CHECK-NEXT: .Ltmp10: +; CHECK-NEXT: str w8, [x0] // on-fault: .LBB15_2 +; CHECK-NEXT: // %bb.1: // %not_null +;; OHOS_LOCAL end ; CHECK-NEXT: .LBB15_2: // %common.ret ; CHECK-NEXT: ret entry: @@ -404,7 +409,9 @@ define void @imp_null_check_unordered_store(i32* %x) { define i32 @imp_null_check_neg_gep_load(i32* %x) { ; CHECK-LABEL: imp_null_check_neg_gep_load: ; CHECK: // %bb.0: // %entry -; CHECK-NEXT: .Ltmp7: +;; OHOS_LOCAL begin +; CHECK-NEXT: .Ltmp11: +;; OHOS_LOCAL end ; CHECK-NEXT: ldur w0, [x0, #-128] // on-fault: .LBB16_2 ; CHECK-NEXT: // %bb.1: // %not_null ; CHECK-NEXT: ret @@ -424,4 +431,33 @@ define i32 @imp_null_check_neg_gep_load(i32* %x) { ret i32 %t } +;; OHOS_LOCAL begin +;; LLVM does not support implicit null checks for ldp and stp +;; The test must be fixed, when such is introduced +define i64 @imp_null_check_load_pair(i64* %array) { +; CHECK-LABEL: imp_null_check_load_pair: +; CHECK: // %bb.0: // %entry +; CHECK-NEXT: cbz x0, .LBB17_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldp x8, x9, [x0] +; CHECK-NEXT: add x0, x9, x8 +; CHECK-NEXT: .LBB17_2: // %return +; CHECK-NEXT: ret +entry: + %cmp = icmp eq i64* %array, null + br i1 %cmp, label %return, label %if.end, !make.implicit !0 + +if.end: ; preds = %entry + %0 = load i64, i64* %array, align 8 + %arrayidx1 = getelementptr inbounds i64, i64* %array, i64 1 + %1 = load i64, i64* %arrayidx1, align 8 + %add = add nsw i64 %1, %0 + br label %return + +return: ; preds = %entry, %if.end + %retval.0 = phi i64 [ %add, %if.end ], [ 0, %entry ] + ret i64 %retval.0 +} +;; OHOS_LOCAL end + !0 = !{} diff --git a/llvm/test/CodeGen/AArch64/ptr32-implicit-null-checks.ll b/llvm/test/CodeGen/AArch64/ptr32-implicit-null-checks.ll new file mode 100644 index 000000000000..dcecc92d5b70 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/ptr32-implicit-null-checks.ll @@ -0,0 +1,276 @@ +; RUN: llc --enable-implicit-null-checks --aarch64-enable-ptr32 -verify-machineinstrs -O2 < %s | FileCheck %s + +target triple = "aarch64-unknown-linux-gnu" + +; CHECK-LABEL: LoadI32FromPtr32: +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: +; CHECK-NEXT: str x30, [sp, #-16]! +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp0: +; CHECK-NEXT: ldr w0, [x0, #16] +; CHECK-NEXT: // %bb.1: +; CHECK-NEXT: ldr x30, [sp], #16 +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB0_2: +; CHECK-NEXT: bl ThrowNullPointerException +define i32 @LoadI32FromPtr32(ptr addrspace(271) %object) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: + tail call void @ThrowNullPointerException() + unreachable + +if.end: + %add.ptr = getelementptr inbounds i32, ptr addrspace(271) %object, i32 4 + %1 = load i32, ptr addrspace(271) %add.ptr, align 4 + ret i32 %1 +} + +; CHECK-LABEL: LoadFloatFromPtr32: +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: +; CHECK-NEXT: str x30, [sp, #-16]! +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp1: +; CHECK-NEXT: ldr s0, [x0, #16] +; CHECK-NEXT: // %bb.1: +; CHECK-NEXT: ldr x30, [sp], #16 +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB1_2: +; CHECK-NEXT: bl ThrowNullPointerException +define float @LoadFloatFromPtr32(ptr addrspace(271) %object) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() #2 + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds float, ptr addrspace(271) %object, i32 4 + %1 = load float, ptr addrspace(271) %add.ptr, align 4 + ret float %1 +} + +; CHECK-LABEL: LoadDoubleFromPtr32: +; CHECK: .cfi_startproc +; CHECK: // %bb.0: // %entry +; CHECK: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK: .cfi_def_cfa_offset 16 +; CHECK: .cfi_offset w30, -16 +; CHECK: .Ltmp2: +; CHECK: ldr d0, [x0, #64] // on-fault: .LBB2_2 +; CHECK: // %bb.1: // %if.end +; CHECK: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK: ret +; CHECK: .LBB2_2: // %if.then +; CHECK: bl ThrowNullPointerException +define double @LoadDoubleFromPtr32(ptr addrspace(271) %object) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: + tail call void @ThrowNullPointerException() + unreachable + +if.end: + %add.ptr = getelementptr inbounds double, ptr addrspace(271) %object, i32 8 + %1 = load double, ptr addrspace(271) %add.ptr, align 8 + ret double %1 +} + +; CHECK-LABEL: LoadPtr32FromPtr32: // @LoadPtr32FromPtr32 +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: // %entry +; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp3: +; CHECK-NEXT: ldr w0, [x0, #16] // on-fault: .LBB3_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB3_2: // %if.then +; CHECK-NEXT: bl ThrowNullPointerException +define ptr addrspace(271) @LoadPtr32FromPtr32(ptr addrspace(271) %object) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 4 + %1 = load ptr addrspace(271), ptr addrspace(271) %add.ptr, align 4 + ret ptr addrspace(271) %1 +} + +; CHECK-LABEL: StoreI32ToPtr32: // @StoreI32ToPtr32 +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: // %entry +; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp4: +; CHECK-NEXT: str w1, [x0, #32] // on-fault: .LBB4_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB4_2: // %if.then +; CHECK-NEXT: bl ThrowNullPointerException +define void @StoreI32ToPtr32(ptr addrspace(271) %object, i32 %value) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 8 + store i32 %value, ptr addrspace(271) %add.ptr, align 4 + ret void +} + +; CHECK-LABEL: StoreFloatToPtr32: // @StoreFloatToPtr32 +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: // %entry +; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp5: +; CHECK-NEXT: str s0, [x0, #32] // on-fault: .LBB5_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB5_2: // %if.then +; CHECK-NEXT: bl ThrowNullPointerException +define void @StoreFloatToPtr32(ptr addrspace(271) %object, float %value) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 8 + store float %value, ptr addrspace(271) %add.ptr, align 4 + ret void +} + +; CHECK-LABEL: StoreDoubleToPtr32: // @StoreDoubleToPtr32 +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: // %entry +; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp6: +; CHECK-NEXT: str d0, [x0, #32] // on-fault: .LBB6_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB6_2: // %if.then +; CHECK-NEXT: bl ThrowNullPointerException +define void @StoreDoubleToPtr32(ptr addrspace(271) %object, double %value) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 8 + store double %value, ptr addrspace(271) %add.ptr, align 8 + ret void +} + +; Note LLVM does not support this case because ImplicitNullChecks does not hoist +; str x8, [x0, #32] above the moves to x8 because of the dependency on x8 +; The test should be enabled when such support is introduced +; StoreConstantDoubleToPtr32: // @StoreConstantDoubleToPtr32 +; // %bb.0: // %entry +; str x30, [sp, #-16]! // 8-byte Folded Spill +; cbz x0, .LBB7_2 +; // %bb.1: // %if.end +; mov x8, #55370 +; movk x8, #19730, lsl #16 +; movk x8, #8699, lsl #32 +; movk x8, #16393, lsl #48 +; str x8, [x0, #32] +; ldr x30, [sp], #16 // 8-byte Folded Reload +; ret +; .LBB7_2: // %if.then +; bl ThrowNullPointerException + +; COM: CHECK-LABEL: StoreConstantDoubleToPtr32: // @StoreConstantDoubleToPtr32 +; COM: CHECK-NOT: cbz x0, {{.*}} +; COM: CHECK: ret +define void @StoreConstantDoubleToPtr32(ptr addrspace(271) %object) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 8 + store double 0x400921FB4D12D84A, ptr addrspace(271) %add.ptr, align 8 + ret void +} + +; CHECK-LABEL: StorePtr32ToPtr32: // @StorePtr32ToPtr32 +; CHECK-NEXT: .cfi_startproc +; CHECK-NEXT: // %bb.0: // %entry +; CHECK-NEXT: str x30, [sp, #-16]! // 8-byte Folded Spill +; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset w30, -16 +; CHECK-NEXT: .Ltmp7: +; CHECK-NEXT: str w1, [x0, #32] // on-fault: .LBB8_2 +; CHECK-NEXT: // %bb.1: // %if.end +; CHECK-NEXT: ldr x30, [sp], #16 // 8-byte Folded Reload +; CHECK-NEXT: ret +; CHECK-NEXT: .LBB8_2: // %if.then +; CHECK-NEXT: bl ThrowNullPointerException +define void @StorePtr32ToPtr32(ptr addrspace(271) %object, ptr addrspace(271) %value) { +entry: + %0 = addrspacecast ptr addrspace(271) %object to ptr + %cmp = icmp eq ptr %0, null + br i1 %cmp, label %if.then, label %if.end, !make.implicit !0 + +if.then: ; preds = %entry + tail call void @ThrowNullPointerException() + unreachable + +if.end: ; preds = %entry + %add.ptr = getelementptr inbounds ptr addrspace(271), ptr addrspace(271) %object, i32 8 + store ptr addrspace(271) %value, ptr addrspace(271) %add.ptr, align 4 + ret void +} + +declare void @ThrowNullPointerException() + +!0 = !{} -- Gitee From 77029408d06a5b7a87cd97c713049992eff50798 Mon Sep 17 00:00:00 2001 From: Nikolai Kholiavin Date: Tue, 6 Feb 2024 00:57:23 +0000 Subject: [PATCH 16/44] [TSan][OHOS] Support TSan for OHOS This enables TSan in clang driver for OHOS targets. Testing support is to be added in a subsequent PR. This also disables interceptors on OHOS targets for some of the functions, which are not exported from our musl. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I90RVC Signed-off-by: Nikolai Kholiavin --- clang/lib/Driver/ToolChains/OHOS.cpp | 4 ++++ compiler-rt/lib/sanitizer_common/CMakeLists.txt | 6 ++++++ .../lib/sanitizer_common/sanitizer_platform_interceptors.h | 3 ++- compiler-rt/lib/tsan/CMakeLists.txt | 6 ++++++ compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp | 4 ++-- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/clang/lib/Driver/ToolChains/OHOS.cpp b/clang/lib/Driver/ToolChains/OHOS.cpp index 7afdecefb614..1bbe09030f3c 100644 --- a/clang/lib/Driver/ToolChains/OHOS.cpp +++ b/clang/lib/Driver/ToolChains/OHOS.cpp @@ -403,6 +403,8 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const { } SanitizerMask OHOS::getSupportedSanitizers() const { + const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64; + const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64; SanitizerMask Res = ToolChain::getSupportedSanitizers(); Res |= SanitizerKind::Address; Res |= SanitizerKind::PointerCompare; @@ -418,6 +420,8 @@ SanitizerMask OHOS::getSupportedSanitizers() const { // OHOS_LOCAL Res |= SanitizerKind::HWAddress; // TODO: Support TSAN and HWASAN and update mask. + if (IsAArch64 || IsX86_64) + Res |= SanitizerKind::Thread; return Res; } diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt index 9bc93abc553a..c7012e08a9de 100644 --- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt @@ -219,6 +219,12 @@ append_list_if(SANITIZER_LIMIT_FRAME_SIZE -Wframe-larger-than=570 append_list_if(COMPILER_RT_HAS_WGLOBAL_CONSTRUCTORS_FLAG -Wglobal-constructors SANITIZER_CFLAGS) +# OHOS_LOCAL begin +if(OHOS) + list(APPEND SANITIZER_CFLAGS -fno-emulated-tls) +endif() +# OHOS_LOCAL end + if(APPLE) set(OS_OPTION OS ${SANITIZER_COMMON_SUPPORTED_OS}) endif() diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index de78cca581f0..d2858ab538f3 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -456,7 +456,8 @@ #define SANITIZER_INTERCEPT_FOPENCOOKIE SI_LINUX_NOT_ANDROID #define SANITIZER_INTERCEPT_SEM \ (SI_LINUX || SI_FREEBSD || SI_NETBSD || SI_SOLARIS) -#define SANITIZER_INTERCEPT_PTHREAD_SETCANCEL SI_POSIX +// OHOS_LOCAL +#define SANITIZER_INTERCEPT_PTHREAD_SETCANCEL (SI_POSIX && !SI_OHOS) #define SANITIZER_INTERCEPT_MINCORE \ (SI_LINUX || SI_NETBSD || SI_FREEBSD || SI_SOLARIS) #define SANITIZER_INTERCEPT_PROCESS_VM_READV SI_LINUX diff --git a/compiler-rt/lib/tsan/CMakeLists.txt b/compiler-rt/lib/tsan/CMakeLists.txt index a327fb14de9a..23f1bc15207a 100644 --- a/compiler-rt/lib/tsan/CMakeLists.txt +++ b/compiler-rt/lib/tsan/CMakeLists.txt @@ -15,6 +15,12 @@ if(COMPILER_RT_TSAN_DEBUG_OUTPUT) list(APPEND TSAN_CFLAGS -DTSAN_DEBUG_OUTPUT=2) endif() +# OHOS_LOCAL begin +if(OHOS) + list(APPEND TSAN_CFLAGS -fno-emulated-tls) +endif() +# OHOS_LOCAL end + # Add the actual runtime library. option(TSAN_USE_OLD_RUNTIME "Use the old tsan runtime (temporal option for emergencies)." OFF) if (TSAN_USE_OLD_RUNTIME) diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp index 17f6b1f472d8..825814f54d82 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp @@ -1109,7 +1109,7 @@ TSAN_INTERCEPTOR(void, pthread_exit, void *retval) { REAL(pthread_exit)(retval); } -#if SANITIZER_LINUX +#if SANITIZER_LINUX && !SANITIZER_OHOS // OHOS_LOCAL TSAN_INTERCEPTOR(int, pthread_tryjoin_np, void *th, void **ret) { SCOPED_INTERCEPTOR_RAW(pthread_tryjoin_np, th, ret); Tid tid = ThreadConsumeTid(thr, pc, (uptr)th); @@ -2835,7 +2835,7 @@ void InitializeInterceptors() { TSAN_INTERCEPT(pthread_join); TSAN_INTERCEPT(pthread_detach); TSAN_INTERCEPT(pthread_exit); - #if SANITIZER_LINUX + #if SANITIZER_LINUX && !SANITIZER_OHOS // OHOS_LOCAL TSAN_INTERCEPT(pthread_tryjoin_np); TSAN_INTERCEPT(pthread_timedjoin_np); #endif -- Gitee From e44d6c450d77cc44a7dd8d7de56fce46280edfc5 Mon Sep 17 00:00:00 2001 From: liuyaning Date: Wed, 7 Feb 2024 16:28:10 +0800 Subject: [PATCH 17/44] Add cmake file in release packages Add cmake file in release packages Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I91GIJ Test:no Signed-off-by: liuyaning --- llvm-build/build.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index f35164454c3b..8bf26fe65dec 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2310,8 +2310,6 @@ class LlvmPackage(BuildUtils): print('Did not find %s in %s' % (necessary_bin_file, bin_dir)) raise RuntimeError('Did not find %s in %s' % (necessary_bin_file, bin_dir)) - cmake_dir = os.path.join(lib_dir, 'cmake') - self.check_rm_tree(cmake_dir) self.notice_prebuilts_file(host, self.package_license_project_tuple(), install_dir) -- Gitee From ca307f0d1e5fc29d5bf7a36b45ff1e5a77fb0db6 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Tue, 6 Feb 2024 15:25:59 +0300 Subject: [PATCH 18/44] [R&D][AArch64 Host] Enable build for all main llvm-projects Fix openmp build - musl provides XSI version of strerror_r, not GNU one Issue https://gitee.com/openharmony/third_party_llvm-project/issues/I905ZL Signed-off-by: Sukhikh Alexander --- llvm-build/build-ohos-aarch64.py | 3 ++- openmp/runtime/src/kmp_i18n.cpp | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index 35bceab1a12b..67974140b0b6 100644 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -64,7 +64,8 @@ def main(): llvm_defines['LLVM_INCLUDE_TESTS'] = 'OFF' llvm_defines['LLVM_BUILD_TOOLS'] = 'ON' llvm_defines['LLVM_ENABLE_ZLIB'] = 'OFF' - llvm_defines['LLVM_ENABLE_PROJECTS'] = 'clang;lld;clang-tools-extra' + llvm_defines['LLVM_ENABLE_PROJECTS'] = 'clang;clang-tools-extra;lld;lldb;openmp' + # We do not build runtimes, since they will be copied from main toolchain build llvm_defines['LLVM_CONFIG_PATH'] = os.path.join(llvm_root, 'bin', 'llvm-config') llvm_defines['LLVM_TABLEGEN'] = os.path.join(llvm_root, 'bin', 'llvm-tblgen') llvm_defines['CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN'] = llvm_root diff --git a/openmp/runtime/src/kmp_i18n.cpp b/openmp/runtime/src/kmp_i18n.cpp index 2474810ce6bf..a164aa180dd4 100644 --- a/openmp/runtime/src/kmp_i18n.cpp +++ b/openmp/runtime/src/kmp_i18n.cpp @@ -710,8 +710,7 @@ static char *sys_error(int err) { #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || \ (defined(__BIONIC__) && defined(_GNU_SOURCE) && \ - __ANDROID_API__ >= __ANDROID_API_M__) || \ - (defined(__OHOS__) && defined(_GNU_SOURCE)) + __ANDROID_API__ >= __ANDROID_API_M__) // GNU version of strerror_r. char buffer[2048]; -- Gitee From 9b4b0ccdd3f4e2dffb896d30f43c636cc1721c85 Mon Sep 17 00:00:00 2001 From: peshkovivan Date: Fri, 19 Jan 2024 16:17:32 +0300 Subject: [PATCH 19/44] Implement DumpAllRegisters for linux aarch64 Signed-off-by: peshkovivan --- .../lib/sanitizer_common/sanitizer_linux.cpp | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp index f03ef2562033..630def6c0a57 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp @@ -2090,7 +2090,41 @@ bool SignalContext::IsTrueFaultingAddress() const { } void SignalContext::DumpAllRegisters(void *context) { - // FIXME: Implement this. + // OHOS_LOCAL begin + Report("Register values:\n"); + + ucontext_t *ucontext = (ucontext_t*)context; + # define DUMPREG64(r) \ + Printf("x%s = 0x%016llx ", #r, ucontext->uc_mcontext.regs[r]); + # define DUMPREGA64(r) \ + Printf("%s = 0x%016llx ", #r, ucontext->uc_mcontext. r); + # define DUMPREG32(r) \ + Printf("%s = 0x%08x ", #r, ucontext->uc_mcontext->__ss.__ ## r); + # define DUMPREG_(r) Printf(" "); DUMPREG(r); + # define DUMPREG__(r) Printf(" "); DUMPREG(r); + # define DUMPREG___(r) Printf(" "); DUMPREG(r); + + #if defined(__aarch64__) + #define DUMPREG(r) DUMPREG64(r) + DUMPREG_(0); DUMPREG_(1); DUMPREG_(2); DUMPREG_(3); Printf("\n"); + DUMPREG_(4); DUMPREG_(5); DUMPREG_(6); DUMPREG_(7); Printf("\n"); + DUMPREG_(8); DUMPREG_(9); DUMPREG(10); DUMPREG(11); Printf("\n"); + DUMPREG(12); DUMPREG(13); DUMPREG(14); DUMPREG(15); Printf("\n"); + DUMPREG(16); DUMPREG(17); DUMPREG(18); DUMPREG(19); Printf("\n"); + DUMPREG(20); DUMPREG(21); DUMPREG(22); DUMPREG(23); Printf("\n"); + DUMPREG(24); DUMPREG(25); DUMPREG(26); DUMPREG(27); Printf("\n"); + DUMPREG(28); DUMPREG(29); DUMPREG(30); DUMPREGA64(sp); Printf("\n"); + + #endif + + # undef DUMPREG64 + # undef DUMPREGA64 + # undef DUMPREG32 + # undef DUMPREG_ + # undef DUMPREG__ + # undef DUMPREG___ + # undef DUMPREG + // OHOS_LOCAL end } static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { -- Gitee From 1a0d2fbf9d60ff975b55f30c65f6d71768b3066c Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Fri, 9 Feb 2024 12:25:15 +0300 Subject: [PATCH 20/44] [Build][AArch64Host] Package toolchain Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I91JQN Signed-off-by: Lyupa Anastasia --- llvm-build/build-ohos-aarch64.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index 67974140b0b6..c760a6ed6816 100644 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -118,5 +118,15 @@ def main(): build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', 'clang', '15.0.4', 'lib', llvm_triple), os.path.join(llvm_install, 'lib', 'clang', '15.0.4', 'lib', llvm_triple)) + # Package ohos-aarch64 toolchain. + if build_config.do_package: + tarball_name = 'clang-%s-ohos-aarch64' % (build_config.build_name) + package_path = '%s%s' % (build_utils.merge_packages_path(tarball_name), build_config.ARCHIVE_EXTENSION) + build_utils.logger().info('Packaging %s', package_path) + args = ['tar', build_config.ARCHIVE_OPTION, '-h', '-C', build_config.OUT_PATH, '-f', package_path, 'ohos-aarch64-install'] + build_utils.check_create_dir(build_config.PACKAGES_PATH) + build_utils.check_call(args) + + if __name__ == '__main__': main() -- Gitee From d25cff72021df3d9c0e394c2327906d56c58d2c3 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Fri, 9 Feb 2024 16:06:01 +0300 Subject: [PATCH 21/44] [compiler-rt] testing support for ohos with hdc Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8PGWS Change-Id: Ide7c3150a930ce197ebd0c1840f43b9cfa6ae67d Signed-off-by: Sukhikh Alexander --- compiler-rt/test/gwp_asan/lit.cfg.py | 4 +-- .../hwasan/TestCases/Linux/lit.local.cfg.py | 2 +- .../TestCases/abort-message-android.cpp | 3 +- .../hwasan/TestCases/libc_thread_freeres.c | 3 +- .../test/hwasan/TestCases/preinit_array.c | 4 +++ .../TestCases/print-memory-usage-android.c | 3 +- compiler-rt/test/hwasan/TestCases/sizes.cpp | 4 +++ compiler-rt/test/hwasan/TestCases/stack-uas.c | 4 +++ compiler-rt/test/hwasan/lit.cfg.py | 3 +- compiler-rt/test/lit.common.cfg.py | 29 ++++++++------- .../ohos_family_commands/hdc_constants.py | 18 ++++++++++ .../ohos_family_commands/ohos_common.py | 33 +++++++++++------ .../ohos_family_commands/ohos_compile.py | 19 +++------- .../ohos_family_commands/ohos_run.py | 35 ++++++++++++++----- compiler-rt/test/scudo/lit.cfg.py | 6 ++-- compiler-rt/test/scudo/malloc.cpp | 6 ++++ compiler-rt/test/scudo/overflow.c | 6 ++++ 17 files changed, 125 insertions(+), 57 deletions(-) create mode 100644 compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py diff --git a/compiler-rt/test/gwp_asan/lit.cfg.py b/compiler-rt/test/gwp_asan/lit.cfg.py index c806ae2d4727..58658a4551dc 100644 --- a/compiler-rt/test/gwp_asan/lit.cfg.py +++ b/compiler-rt/test/gwp_asan/lit.cfg.py @@ -51,6 +51,6 @@ default_gwp_asan_options += ':' config.substitutions.append(('%env_scudo_options=', 'env SCUDO_OPTIONS=' + default_gwp_asan_options)) -# GWP-ASan tests are currently supported on Linux only. -if config.host_os not in ['Linux']: +# GWP-ASan tests are currently supported on Linux and OHOS only. +if config.host_os not in ['Linux', 'OHOS']: # OHOS_LOCAL config.unsupported = True diff --git a/compiler-rt/test/hwasan/TestCases/Linux/lit.local.cfg.py b/compiler-rt/test/hwasan/TestCases/Linux/lit.local.cfg.py index 57271b8078a4..ce7ce5ddde93 100644 --- a/compiler-rt/test/hwasan/TestCases/Linux/lit.local.cfg.py +++ b/compiler-rt/test/hwasan/TestCases/Linux/lit.local.cfg.py @@ -5,5 +5,5 @@ def getRoot(config): root = getRoot(config) -if root.host_os not in ['Linux']: +if root.host_os not in ['Linux', 'OHOS']: # OHOS_LOCAL config.unsupported = True diff --git a/compiler-rt/test/hwasan/TestCases/abort-message-android.cpp b/compiler-rt/test/hwasan/TestCases/abort-message-android.cpp index f89b929d454d..c038d74a546b 100644 --- a/compiler-rt/test/hwasan/TestCases/abort-message-android.cpp +++ b/compiler-rt/test/hwasan/TestCases/abort-message-android.cpp @@ -1,6 +1,7 @@ // RUN: %clangxx_hwasan -DERR=1 %s -o %t && not %run %t 2>&1 | FileCheck %s // RUN: %clangxx_hwasan -DERR=2 %s -o %t && not %run %t 2>&1 | FileCheck %s -// REQUIRES: android +// OHOS_LOCAL +// REQUIRES: android && !ohos_family #include #include diff --git a/compiler-rt/test/hwasan/TestCases/libc_thread_freeres.c b/compiler-rt/test/hwasan/TestCases/libc_thread_freeres.c index 83451f5bdf40..105213972112 100644 --- a/compiler-rt/test/hwasan/TestCases/libc_thread_freeres.c +++ b/compiler-rt/test/hwasan/TestCases/libc_thread_freeres.c @@ -1,5 +1,6 @@ // RUN: %clang_hwasan %s -o %t && %env_hwasan_opts=random_tags=1 %run %t -// REQUIRES: stable-runtime +// OHOS_LOCAL +// REQUIRES: stable-runtime && !ohos_family #include #include diff --git a/compiler-rt/test/hwasan/TestCases/preinit_array.c b/compiler-rt/test/hwasan/TestCases/preinit_array.c index 54d3ee7eea15..7e9c807fa34c 100644 --- a/compiler-rt/test/hwasan/TestCases/preinit_array.c +++ b/compiler-rt/test/hwasan/TestCases/preinit_array.c @@ -3,6 +3,10 @@ // RUN: %clang_hwasan %s -o %t // RUN: %run %t +// OHOS_LOCAL +// musl doesn't have .preinit_array section +// REQUIRES: !ohos_family + volatile int Global; void StoreToGlobal() { Global = 42; } diff --git a/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c b/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c index 5a057928ba75..3dc8a33bc914 100644 --- a/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c +++ b/compiler-rt/test/hwasan/TestCases/print-memory-usage-android.c @@ -1,6 +1,7 @@ // Tests __hwasan_print_memory_usage through /proc/$PID/maps. // RUN: %clang_hwasan %s -o %t && %env_hwasan_opts=export_memory_stats=1 %run %t 2>&1 | FileCheck %s -// REQUIRES: android +// OHOS_LOCAL +// REQUIRES: android && !ohos_family #include #include diff --git a/compiler-rt/test/hwasan/TestCases/sizes.cpp b/compiler-rt/test/hwasan/TestCases/sizes.cpp index 2cb70f52147a..32c2cfc27e86 100644 --- a/compiler-rt/test/hwasan/TestCases/sizes.cpp +++ b/compiler-rt/test/hwasan/TestCases/sizes.cpp @@ -23,6 +23,10 @@ // allocator can allocate. Tests that an integer overflow in the parameters of // calloc is caught. +// OHOS_LOCAL +// we can't link with libstdc++ +// REQUIRES: !ohos_family + #include #include #include diff --git a/compiler-rt/test/hwasan/TestCases/stack-uas.c b/compiler-rt/test/hwasan/TestCases/stack-uas.c index 7f5a6f26d067..ca81663e00e6 100644 --- a/compiler-rt/test/hwasan/TestCases/stack-uas.c +++ b/compiler-rt/test/hwasan/TestCases/stack-uas.c @@ -17,6 +17,10 @@ // Stack histories currently are not recorded on x86. // XFAIL: x86_64 +// OHOS_LOCAL +// Return value is undefined on OHOS +// XFAIL: ohos_family + void USE(void *x) { // pretend_to_do_something(void *x) __asm__ __volatile__("" : diff --git a/compiler-rt/test/hwasan/lit.cfg.py b/compiler-rt/test/hwasan/lit.cfg.py index 85fda0c70324..4b3313a374ba 100644 --- a/compiler-rt/test/hwasan/lit.cfg.py +++ b/compiler-rt/test/hwasan/lit.cfg.py @@ -53,6 +53,5 @@ config.substitutions.append(('%env_hwasan_opts=', # Default test suffixes. config.suffixes = ['.c', '.cpp'] - -if config.host_os not in ['Linux', 'Android'] or not config.has_lld: +if config.host_os not in ['Linux', 'Android', 'OHOS'] or not config.has_lld: # OHOS_LOCAL config.unsupported = True diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py index 4c43a0b0143e..04093624454c 100644 --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -7,6 +7,7 @@ import os import platform import re import subprocess +import sys # OHOS_LOCAL import json import lit.formats @@ -273,10 +274,6 @@ elif is_ohos_family_mobile(): config.compile_wrapper = compile_wrapper config.substitutions.append( ('%run', "") ) config.substitutions.append( ('%env ', "env ") ) - # TODO: Implement `%device_rm` to perform removal of files on a device. For - # now just make it a no-op. - lit_config.warning('%device_rm is not implemented') - config.substitutions.append( ('%device_rm', 'echo ') ) elif config.host_os == 'Darwin' and config.apple_platform != "osx": # Darwin tests can be targetting macOS, a device or a simulator. All devices # are declared as "ios", even for iOS derivatives (tvOS, watchOS). Similarly, @@ -521,14 +518,18 @@ if config.android: # OHOS_LOCAL begin elif config.host_os == 'OHOS': + hdc_imp = os.path.join(os.path.dirname(__file__), 'sanitizer_common', 'ohos_family_commands') + sys.path.append(hdc_imp) + import hdc_constants env = os.environ.copy() - adb = os.environ.get('ADB', 'adb') - device_tmpdir = '/data/local/tmp/Output/' - config.substitutions.append( ('%device_rundir/', device_tmpdir) ) - config.substitutions.append( ('%push_to_device', "%s push " % adb) ) - config.substitutions.append( ('%adb_shell ', "%s shell " % adb) ) - config.substitutions.append( ('%device_rm', "%s shell 'rm ' " % adb) ) - subprocess.check_call([adb, "shell", "mkdir", "-p", device_tmpdir], env=env) + config.substitutions.append( ('%device_rundir/', hdc_constants.TMPDIR) ) + prefix = hdc_constants.get_hdc_cmd_prefix() + prefix_str = ' '.join(prefix) + config.substitutions.append(('%push_to_device', "%s file send " % prefix_str) ) + config.substitutions.append(('%adb_shell ', "%s shell " % prefix_str) ) + config.substitutions.append(('%device_rm', "%s shell 'rm ' " % prefix_str) ) + subprocess.check_call(prefix + ['tconn'], env=env) + subprocess.check_call(prefix + ['shell', 'mkdir', '-p', hdc_constants.TMPDIR], env=env) # OHOS_LOCAL end else: @@ -710,7 +711,11 @@ elif config.android or is_ohos_family_mobile(): # Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL # because the test hangs or fails on one configuration and not the other. -if config.android or (config.target_arch not in ['arm', 'armhf', 'aarch64']): +# OHOS_LOCAL begin +if config.android or config.host_os == 'OHOS' or ( + config.target_arch not in ['arm', 'armhf', 'aarch64'] + ): +# OHOS_LOCAL end config.available_features.add('stable-runtime') if config.asan_shadow_scale: diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py new file mode 100644 index 000000000000..af6853ed8b1d --- /dev/null +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py @@ -0,0 +1,18 @@ + +import os + +# TODO: move this to the cmake ? +HDC = os.environ.get('HDC', 'hdc') +HDC_SERVER_IP_PORT = os.environ.get('HDC_SERVER_IP_PORT') +HDC_UTID = os.environ.get('HDC_UTID') +TMPDIR = os.environ.get('OHOS_REMOTE_TMP_DIR', '/data/local/tmp/Output') + +# emit warning on import if some required constants are not set +if not HDC_SERVER_IP_PORT or not HDC_UTID: + print('Please set "HDC_SERVER_IP_PORT" and "HDC_UTID" environment variables ' + 'to be able to debug on remote device') + +def get_hdc_cmd_prefix(): + server = ['-s', HDC_SERVER_IP_PORT] if HDC_SERVER_IP_PORT else [] + device = ['-t', HDC_UTID] if HDC_UTID else [] + return [HDC, *server, *device] diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py index d4a94c03a654..de2aabe8107e 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py @@ -1,9 +1,7 @@ #!/usr/bin/env python3 import os, subprocess, tempfile - -HOS_TMPDIR = '/data/local/tmp/Output' -ADB = os.environ.get('ADB', 'adb') +import hdc_constants verbose = False if os.environ.get('HOS_RUN_VERBOSE') == '1': @@ -11,10 +9,14 @@ if os.environ.get('HOS_RUN_VERBOSE') == '1': def host_to_device_path(path): rel = os.path.relpath(path, "/") - dev = os.path.join(HOS_TMPDIR, rel) + dev = os.path.join(hdc_constants.TMPDIR, rel) return dev -def adb(args, attempts = 1): +def hdc_output(args): + command = hdc_constants.get_hdc_cmd_prefix() + args + return subprocess.check_output(command, stderr=subprocess.STDOUT) + +def hdc(args, attempts=1, check_stdout=''): if verbose: print (args) tmpname = tempfile.mktemp() @@ -22,11 +24,13 @@ def adb(args, attempts = 1): ret = 255 while attempts > 0 and ret != 0: attempts -= 1 - ret = subprocess.call([ADB] + args, stdout=out, stderr=subprocess.STDOUT) - if attempts != 0: - ret = 5 + ret = 0 + output = hdc_output(args) + # hdc exit code is always zero + if check_stdout not in output.decode(): + ret = 255 if ret != 0: - print ("adb command failed", args) + print ("hdc command failed", args) print (tmpname) out.close() out = open(tmpname, 'r') @@ -36,12 +40,19 @@ def adb(args, attempts = 1): return ret def pull_from_device(path): + # hdc can't download empty files + file_sz = hdc_output(['shell', 'du', path]).split() + if file_sz and file_sz[0] == b'0': + return '' + tmp = tempfile.mktemp() - adb(['pull', path, tmp], 5) + hdc(['file', 'recv', path, tmp], attempts=5, check_stdout='FileTransfer finish') text = open(tmp, 'r').read() os.unlink(tmp) return text def push_to_device(path): dst_path = host_to_device_path(path) - adb(['push', path, dst_path], 5) + # hdc do not auto create directories on device + hdc(['shell', 'mkdir', '-p', os.path.dirname(dst_path)]) + hdc(['file', 'send', path, dst_path], attempts=5, check_stdout='FileTransfer finish') diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py index 265a4a9f8f09..9006dac3d3b9 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py @@ -21,31 +21,20 @@ while args: output_type = 'object' elif arg == '-o': output = args.pop(0) - elif arg == '-target': - check_trgt = True - elif check_trgt or arg.startswith('--target='): - check_trgt = False - if arg.endswith('-linux-ohos'): - arg = arg.split('=')[-1] - dyld = 'unknown_hos_dyld' - # FIXME: Handle -mfloat-abi=hard for arm - # TODO: Support x86_64 - if arg.startswith('arm'): - dyld = 'ld-musl-arm.so.1' - elif arg.startswith('aarch64'): - dyld = 'ld-musl-aarch64.so.1' - append_args += ['-Wl,--dynamic-linker=' + os.path.join(HOS_TMPDIR, dyld)] if output == None: print ("No output file name!") sys.exit(1) -ret = subprocess.call(sys.argv[1:] + append_args) +with open(f'{output}.stderr', 'w') as f: + ret = subprocess.call(sys.argv[1:] + append_args, stderr=f) + if ret != 0: sys.exit(ret) if output_type in ['executable', 'shared']: push_to_device(output) + hdc(['shell', 'chmod', '+x', host_to_device_path(output)]) if output_type == 'executable': os.rename(output, output + '.real') diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py index 84a8d9fb709c..c53c94c46ce9 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import os, signal, sys, subprocess import re @@ -8,10 +8,28 @@ device_binary = host_to_device_path(sys.argv[0]) def build_env(): args = [] + sanitizers = ( + 'HWASAN', 'ASAN', 'LSAN', 'MEMPROF', 'MSAN', 'TSAN', 'UBSAN', 'SCUDO' + ) + for san in sanitizers: + # for all sanitizers we need 'abort_on_error=0', + # so prepare key for them, to set value later + opt_str = '%s_OPTIONS' % san + if opt_str not in os.environ: + os.environ[opt_str] = '' + + # All sanitizers need external symbolizers for some tests + # set them by default to llvm-symbolizer + symb_name = '%s_SYMBOLIZER_PATH' % san + args.append('%s=%s' % (symb_name, os.environ.get('LLVM_SYMBOLIZER_PATH', + os.path.join(hdc_constants.TMPDIR,'llvm-symbolizer-aarch64')))) # HOS linker ignores RPATH. Set LD_LIBRARY_PATH to Output dir. - args.append('LD_LIBRARY_PATH=%s' % (HOS_TMPDIR,)) + args.append('LD_LIBRARY_PATH=%s' % ( hdc_constants.TMPDIR,)) for (key, value) in os.environ.items(): - if key in ['ASAN_ACTIVATION_OPTIONS', 'SCUDO_OPTIONS'] or key.endswith('SAN_OPTIONS'): + san_opt = key.endswith('SAN_OPTIONS') + if san_opt: + value += ':abort_on_error=0' + if key in ['ASAN_ACTIVATION_OPTIONS', 'SCUDO_OPTIONS'] or san_opt: args.append('%s="%s"' % (key, value)) return ' '.join(args) @@ -22,11 +40,12 @@ device_stderr = device_binary + '.stderr' device_exitcode = device_binary + '.exitcode' device_linker = '' -ret = adb(['shell', 'cd %s && %s %s %s %s >%s 2>%s ; echo $? >%s' % - (HOS_TMPDIR, device_env, device_linker, device_binary, device_args, - device_stdout, device_stderr, device_exitcode)]) -if ret != 0: - sys.exit(ret) +# Currently OHOS set log_path in UBSAN_OPTIONS +# Tests expects to see output in stdout/stderr and fails when it is not there +# So unset UBSAN_OPTIONS before run tests. +hdc(['shell', 'unset UBSAN_OPTIONS && cd %s && %s %s %s %s >%s 2>%s ; echo $? >%s' % + (hdc_constants.TMPDIR, device_env, device_linker, device_binary, device_args, + device_stdout, device_stderr, device_exitcode)]) sys.stdout.write(pull_from_device(device_stdout)) sys.stderr.write(pull_from_device(device_stderr)) diff --git a/compiler-rt/test/scudo/lit.cfg.py b/compiler-rt/test/scudo/lit.cfg.py index 236d645f3b94..60cdeb65bd50 100644 --- a/compiler-rt/test/scudo/lit.cfg.py +++ b/compiler-rt/test/scudo/lit.cfg.py @@ -45,7 +45,7 @@ config.substitutions.append(("%shared_minlibscudo", shared_minlibscudo)) # Platform-specific default SCUDO_OPTIONS for lit tests. default_scudo_opts = '' -if config.android: +if config.android or config.host_os == 'OHOS': # OHOS_LOCAL # Android defaults to abort_on_error=1, which doesn't work for us. default_scudo_opts = 'abort_on_error=0' @@ -59,6 +59,6 @@ if default_scudo_opts: config.substitutions.append(('%env_scudo_opts=', 'env SCUDO_OPTIONS=' + default_scudo_opts)) -# Hardened Allocator tests are currently supported on Linux only. -if config.host_os not in ['Linux']: +# Hardened Allocator tests are currently supported on Linux and OHOS only. +if config.host_os not in ['Linux', 'OHOS']: # OHOS_LOCAL config.unsupported = True diff --git a/compiler-rt/test/scudo/malloc.cpp b/compiler-rt/test/scudo/malloc.cpp index afa7ee47c03b..e0ba5deace3e 100644 --- a/compiler-rt/test/scudo/malloc.cpp +++ b/compiler-rt/test/scudo/malloc.cpp @@ -11,6 +11,12 @@ #include +// OHOS_LOCAL begin +#ifdef __OHOS__ +#include +#endif +// OHOS_LOCAL end + int main(int argc, char **argv) { void *p; std::vector sizes{1, 8, 16, 32, 1024, 32768, diff --git a/compiler-rt/test/scudo/overflow.c b/compiler-rt/test/scudo/overflow.c index 7df9f8ba3534..8c0d797cc086 100644 --- a/compiler-rt/test/scudo/overflow.c +++ b/compiler-rt/test/scudo/overflow.c @@ -8,6 +8,12 @@ #include #include +// OHOS_LOCAL begin +#ifdef __OHOS__ +#include +#endif +// OHOS_LOCAL end + int main(int argc, char **argv) { ssize_t offset = sizeof(void *) == 8 ? 8 : 0; -- Gitee From 1302f071d4be91f9a31544d460d7d741b28a9c80 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Fri, 9 Feb 2024 16:06:57 +0300 Subject: [PATCH 22/44] [compiler-rt] TMP: disable not working tests and runtimes Disable scudo-standalone build and its dependecies until https://gitee.com/openharmony/third_party_musl/pulls/1094 is merged Also disable scudo and gwp-asan for arm and mips More info here: https://gitee.com/openharmony/third_party_llvm-project/issues/I91GO7 Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8PGWS Change-Id: Ibb90c86e38ab14d5226e088d344564661f9db715 Signed-off-by: Sukhikh Alexander --- compiler-rt/cmake/config-ix.cmake | 1 + compiler-rt/lib/CMakeLists.txt | 18 +++++++++++++++--- compiler-rt/test/CMakeLists.txt | 18 ++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index a549732afea0..ac88d54ccfd2 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -814,6 +814,7 @@ else() endif() #TODO(kostyak): add back Android & Fuchsia when the code settles a bit. +# TODO: Add OHOS when https://gitee.com/openharmony/third_party_musl/pulls/1235 is merged if (SCUDO_STANDALONE_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux" AND COMPILER_RT_HAS_AUXV) set(COMPILER_RT_HAS_SCUDO_STANDALONE TRUE) diff --git a/compiler-rt/lib/CMakeLists.txt b/compiler-rt/lib/CMakeLists.txt index 18eed2446dc6..678bc03a8df2 100644 --- a/compiler-rt/lib/CMakeLists.txt +++ b/compiler-rt/lib/CMakeLists.txt @@ -28,9 +28,11 @@ function(compiler_rt_build_runtime runtime) if(${runtime} STREQUAL tsan) add_subdirectory(tsan/dd) endif() - if(${runtime} STREQUAL scudo) - add_subdirectory(scudo/standalone) - endif() + # Disable standalone scudo until this PR merged + # https://gitee.com/openharmony/third_party_musl/pulls/1235/files + # if(${runtime} STREQUAL scudo) + # add_subdirectory(scudo/standalone) + # endif() endif() endfunction() @@ -38,6 +40,16 @@ if(COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_MEMPROF) compiler_rt_build_runtime(interception) endif() +# OHOS_LOCAL begin +if(COMPILER_RT_DEFAULT_TARGET_TRIPLE MATCHES "arm-linux-ohos|mipsel-linux-ohos") + # temporary disable scudo and gwp_asan for arm + # arm scudo build failed due to the abcsence of the `_aeabi_unwind_cpp_pr1` symbol + # on mipsel -latomic is not found. + # gwp_asan depends on scudo + list(REMOVE_ITEM COMPILER_RT_SANITIZERS_TO_BUILD "scudo" "gwp_asan") +endif() +# OHOS_LOCAL end + if(COMPILER_RT_BUILD_SANITIZERS) if(COMPILER_RT_HAS_SANITIZER_COMMON) add_subdirectory(stats) diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt index 3cbfc4b0e544..e23d48147834 100644 --- a/compiler-rt/test/CMakeLists.txt +++ b/compiler-rt/test/CMakeLists.txt @@ -58,6 +58,16 @@ function(compiler_rt_test_runtime runtime) endif() endfunction() +# OHOS_LOCAL begin +if(COMPILER_RT_DEFAULT_TARGET_TRIPLE MATCHES "arm-linux-ohos|mipsel-linux-ohos") + # temporary disable scudo and gwp_asan for arm + # arm scudo build failed due to the abcsence of the `_aeabi_unwind_cpp_pr1` symbol + # on mipsel -latomic is not found. + # gwp_asan depends on scudo + list(REMOVE_ITEM COMPILER_RT_SANITIZERS_TO_BUILD "scudo" "gwp_asan") +endif() +# OHOS_LOCAL end + # Run sanitizer tests only if we're sure that clang would produce # working binaries. if(COMPILER_RT_CAN_EXECUTE_TESTS) @@ -79,9 +89,13 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS) foreach(sanitizer ${COMPILER_RT_SANITIZERS_TO_BUILD}) # cfi testing is gated on ubsan - if(NOT ${sanitizer} STREQUAL cfi) - compiler_rt_test_runtime(${sanitizer}) + # OHOS_LOCAL begin + # can't test gwp-asan, because it is depends on scudo-standalone + if(NOT ${sanitizer} STREQUAL cfi AND + NOT ${sanitizer} STREQUAL gwp_asan) + compiler_rt_test_runtime(${sanitizer}) endif() + # OHOS_LOCAL end endforeach() endif() if(COMPILER_RT_BUILD_PROFILE AND COMPILER_RT_HAS_PROFILE) -- Gitee From 44ede63c214f479b6d5b4d5916a602556d5bb69f Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Fri, 9 Feb 2024 18:23:47 +0300 Subject: [PATCH 23/44] [Build][AArch64Host] Make build script executable Change script mode, add copyright Signed-off-by: Lyupa Anastasia --- llvm-build/build-ohos-aarch64.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) mode change 100644 => 100755 llvm-build/build-ohos-aarch64.py diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py old mode 100644 new mode 100755 index c760a6ed6816..0a57f7e5542d --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -1,3 +1,18 @@ +#!/usr/bin/env python3 +# Copyright (C) 2023 Huawei Device Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import os from build import BuildConfig, BuildUtils -- Gitee From aee8229a3280cc6930c36383dc04d93f2dbf2b90 Mon Sep 17 00:00:00 2001 From: Yuanfang Chen Date: Mon, 22 Aug 2022 16:01:09 -0700 Subject: [PATCH 24/44] [compiler-rt][lit] initialize LIT LLVMConfig instance to handle lit tools searching. Otherwise compiler-rt depends on system environment variable PATH for lit tools which diverge from the other LLVM projects. This reverts D83486 which really should be implemented in LIT itself when the PATH is constructed. Reviewed By: vitalybuka Differential Revision: https://reviews.llvm.org/D122837 Signed-off-by: Nikolai Kholiavin --- compiler-rt/test/lit.common.cfg.py | 2 -- compiler-rt/test/lit.common.configured.in | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py index 04093624454c..30e47066d3cd 100644 --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -176,8 +176,6 @@ if config.asan_shadow_scale != '': if config.memprof_shadow_scale != '': config.target_cflags += " -mllvm -memprof-mapping-scale=" + config.memprof_shadow_scale -config.environment = dict(os.environ) - # Clear some environment variables that might affect Clang. possibly_dangerous_env_vars = ['ASAN_OPTIONS', 'DFSAN_OPTIONS', 'LSAN_OPTIONS', 'MSAN_OPTIONS', 'UBSAN_OPTIONS', diff --git a/compiler-rt/test/lit.common.configured.in b/compiler-rt/test/lit.common.configured.in index a06ede7e7331..1a20461cbf7d 100644 --- a/compiler-rt/test/lit.common.configured.in +++ b/compiler-rt/test/lit.common.configured.in @@ -18,6 +18,7 @@ set_default("compiler_rt_src_root", "@COMPILER_RT_SOURCE_DIR@") set_default("compiler_rt_obj_root", "@COMPILER_RT_BINARY_DIR@") set_default("enable_per_target_runtime_dir", @LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_PYBOOL@) set_default("llvm_tools_dir", "@LLVM_TOOLS_DIR@") +set_default("lit_tools_dir", "@LLVM_LIT_TOOLS_DIR@") set_default("llvm_shlib_dir", "@LLVM_LIBRARY_OUTPUT_INTDIR@") set_default("gnu_ld_executable", "@GNU_LD_EXECUTABLE@") set_default("gold_executable", "@GOLD_EXECUTABLE@") @@ -77,5 +78,8 @@ config.compiler_rt_libdir = lit_config.substitute(config.compiler_rt_libdir) if not os.path.exists(config.clang): lit_config.fatal("Can't find compiler on path %r" % config.clang) +import lit.llvm +lit.llvm.initialize(lit_config, config) + # Setup attributes common for all compiler-rt projects. lit_config.load_config(config, "@COMPILER_RT_SOURCE_DIR@/test/lit.common.cfg.py") -- Gitee From 2c3738c12991809e306219491c0e3a02234cc7f3 Mon Sep 17 00:00:00 2001 From: Nikolai Kholiavin Date: Thu, 15 Feb 2024 12:18:02 +0000 Subject: [PATCH 25/44] [OHOS][compiler-rt][test] Enable TSan testing remotely on OHOS devices This enables TSan tests for OHOS; disables some tests that use features absent from upstream or OHOS musl; turns on pthread_setname_np and pthread_getname_np interceptors, because these functions are available on OHOS musl. Also, modifies test suite to be able to run TSan tests, in particular the handling of LD_LIBRARY_PATH and local suppressions files to work on the remote device as well; replaces pthread_yield with sched_yield in tests. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I91O7W Signed-off-by: Nikolai Kholiavin --- .../sanitizer_platform_interceptors.h | 6 +++-- compiler-rt/lib/tsan/tests/CMakeLists.txt | 6 +++++ .../TestCases/Posix/halt_on_error-torture.cpp | 6 +++-- compiler-rt/test/lit.common.cfg.py | 22 ++++++++++++++++ .../ohos_family_commands/hdc_constants.py | 1 + .../ohos_family_commands/ohos_common.py | 2 +- .../ohos_family_commands/ohos_compile.py | 6 ++--- .../ohos_family_commands/ohos_run.py | 26 ++++++++++++++++++- .../ohos_family_commands/ohos_tool.py | 11 ++++++++ compiler-rt/test/tsan/CMakeLists.txt | 12 ++++++--- compiler-rt/test/tsan/Linux/lit.local.cfg.py | 2 +- compiler-rt/test/tsan/Linux/mutex_robust.cpp | 3 +++ compiler-rt/test/tsan/Linux/mutex_robust2.cpp | 3 +++ .../test/tsan/Linux/thread_timedjoin.c | 3 +++ compiler-rt/test/tsan/Linux/thread_tryjoin.c | 5 +++- compiler-rt/test/tsan/cond_cancel.c | 3 +++ compiler-rt/test/tsan/debugging.cpp | 6 +++-- compiler-rt/test/tsan/dtls.c | 3 +++ compiler-rt/test/tsan/fiber_from_thread.cpp | 3 +++ compiler-rt/test/tsan/fiber_longjmp.cpp | 3 +++ compiler-rt/test/tsan/fiber_race.cpp | 3 +++ compiler-rt/test/tsan/fiber_simple.cpp | 3 +++ compiler-rt/test/tsan/fiber_two_threads.cpp | 3 +++ compiler-rt/test/tsan/ignore_lib1.cpp | 4 +++ compiler-rt/test/tsan/ignore_lib2.cpp | 4 +++ compiler-rt/test/tsan/ignore_lib3.cpp | 4 +++ compiler-rt/test/tsan/ignore_lib4.cpp | 4 +++ compiler-rt/test/tsan/ignore_lib5.cpp | 4 +++ .../test/tsan/ignored-interceptors-mmap.cpp | 7 +++-- compiler-rt/test/tsan/java_finalizer2.cpp | 6 ++--- compiler-rt/test/tsan/lit.cfg.py | 23 ++++++++-------- .../test/tsan/mutex_lock_destroyed.cpp | 4 +++ compiler-rt/test/tsan/on_exit.cpp | 4 +++ compiler-rt/test/tsan/signal_sync2.cpp | 12 ++++++++- compiler-rt/test/tsan/strerror_r.cpp | 3 ++- compiler-rt/test/tsan/thread_name.cpp | 8 +++++- compiler-rt/test/tsan/tls_race.cpp | 2 ++ compiler-rt/test/tsan/tls_race2.cpp | 2 ++ llvm/utils/lit/lit/llvm/subst.py | 12 +++++++++ 39 files changed, 208 insertions(+), 36 deletions(-) create mode 100755 compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_tool.py diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index d2858ab538f3..8131bb915337 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -405,10 +405,12 @@ #define SANITIZER_INTERCEPT_PTHREAD_MUTEX SI_POSIX #define SANITIZER_INTERCEPT___PTHREAD_MUTEX SI_GLIBC #define SANITIZER_INTERCEPT___LIBC_MUTEX SI_NETBSD +// OHOS_LOCAL begin #define SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP \ - (SI_FREEBSD || SI_NETBSD || SI_GLIBC || SI_SOLARIS) + (SI_FREEBSD || SI_NETBSD || SI_GLIBC || SI_SOLARIS || SI_OHOS) #define SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP \ - (SI_FREEBSD || SI_NETBSD || SI_GLIBC || SI_SOLARIS) + (SI_FREEBSD || SI_NETBSD || SI_GLIBC || SI_SOLARIS || SI_OHOS) +// OHOS_LOCAL end #define SANITIZER_INTERCEPT_TLS_GET_ADDR \ (SI_FREEBSD || SI_NETBSD || (SI_LINUX_NOT_ANDROID && !SI_OHOS) || SI_SOLARIS) diff --git a/compiler-rt/lib/tsan/tests/CMakeLists.txt b/compiler-rt/lib/tsan/tests/CMakeLists.txt index 8afd217cb8b0..d36ed16b499a 100644 --- a/compiler-rt/lib/tsan/tests/CMakeLists.txt +++ b/compiler-rt/lib/tsan/tests/CMakeLists.txt @@ -15,6 +15,12 @@ set(TSAN_UNITTEST_CFLAGS -fno-rtti ) +# OHOS_LOCAL begin +if(OHOS) + list(APPEND TSAN_UNITTEST_CFLAGS -fno-emulated-tls) +endif() +# OHOS_LOCAL end + if(COMPILER_RT_TSAN_DEBUG_OUTPUT) # Need to match these flags with the runtime. list(APPEND TSAN_UNITTEST_CFLAGS -DTSAN_COLLECT_STATS=1 diff --git a/compiler-rt/test/asan/TestCases/Posix/halt_on_error-torture.cpp b/compiler-rt/test/asan/TestCases/Posix/halt_on_error-torture.cpp index 559f9434e392..f0f36ec310c7 100644 --- a/compiler-rt/test/asan/TestCases/Posix/halt_on_error-torture.cpp +++ b/compiler-rt/test/asan/TestCases/Posix/halt_on_error-torture.cpp @@ -38,7 +38,8 @@ void *run(void *arg) { for (size_t i = 0; i < niter; ++i) { random_delay(&seed); - // CHECK: ERROR: AddressSanitizer: use-after-poison + // OHOS_LOCAL + // CHECK-DAG: ERROR: AddressSanitizer: use-after-poison volatile int idx = 0; tmp[idx] = 0; } @@ -71,7 +72,8 @@ int main(int argc, char **argv) { } } - // CHECK: All threads terminated + // OHOS_LOCAL + // CHECK-DAG: All threads terminated printf("All threads terminated\n"); delete [] tids; diff --git a/compiler-rt/test/lit.common.cfg.py b/compiler-rt/test/lit.common.cfg.py index 30e47066d3cd..869d0e233ef3 100644 --- a/compiler-rt/test/lit.common.cfg.py +++ b/compiler-rt/test/lit.common.cfg.py @@ -12,6 +12,10 @@ import json import lit.formats import lit.util +# OHOS_LOCAL begin +from lit.llvm import llvm_config +from lit.llvm.subst import ToolSubst, WrapTool, FindTool +# OHOS_LOCAL end # Get shlex.quote if available (added in 3.3), and fall back to pipes.quote if # it's not available. @@ -255,6 +259,7 @@ def get_ios_commands_dir(): return os.path.join(config.compiler_rt_src_root, "test", "sanitizer_common", "ios_commands") # Allow tests to be executed on a simulator or remotely. +tools = [] # OHOS_LOCAL if emulator: config.substitutions.append(('%run', emulator)) config.substitutions.append(('%env ', "env ")) @@ -263,6 +268,7 @@ if emulator: lit_config.warning('%device_rm is not implemented') config.substitutions.append(('%device_rm', 'echo ')) config.compile_wrapper = "" +# OHOS_LOCAL begin elif is_ohos_family_mobile(): config.available_features.add('ohos_family') # FIXME: some tests for hos also need this now, @@ -272,6 +278,9 @@ elif is_ohos_family_mobile(): config.compile_wrapper = compile_wrapper config.substitutions.append( ('%run', "") ) config.substitutions.append( ('%env ', "env ") ) + tool_wrapper = os.path.join(config.compiler_rt_src_root, "test", "sanitizer_common", "ohos_family_commands", "ohos_tool.py") + " " + tools.append(ToolSubst('llvm-objdump', command=WrapTool('llvm-objdump', tool_wrapper), unresolved='fatal')) +# OHOS_LOCAL end elif config.host_os == 'Darwin' and config.apple_platform != "osx": # Darwin tests can be targetting macOS, a device or a simulator. All devices # are declared as "ios", even for iOS derivatives (tvOS, watchOS). Similarly, @@ -341,6 +350,9 @@ else: config.substitutions.append(('%device_rm', 'echo ')) config.compile_wrapper = "" +# OHOS_LOCAL +llvm_config.add_tool_substitutions(tools, [config.llvm_tools_dir] + config.environment['PATH'].split(os.path.pathsep)) + # Define CHECK-%os to check for OS-dependent output. config.substitutions.append(('CHECK-%os', ("CHECK-" + config.host_os))) @@ -516,6 +528,16 @@ if config.android: # OHOS_LOCAL begin elif config.host_os == 'OHOS': + for var in [ + 'HDC', + 'HDC_SERVER_IP_PORT', + 'HDC_UTID', + 'OHOS_REMOTE_TMP_DIR', + 'OHOS_REMOTE_DYN_LINKER', + ]: + if var in os.environ: + config.environment[var] = os.environ[var] + hdc_imp = os.path.join(os.path.dirname(__file__), 'sanitizer_common', 'ohos_family_commands') sys.path.append(hdc_imp) import hdc_constants diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py index af6853ed8b1d..5fcfb514f4b3 100644 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/hdc_constants.py @@ -6,6 +6,7 @@ HDC = os.environ.get('HDC', 'hdc') HDC_SERVER_IP_PORT = os.environ.get('HDC_SERVER_IP_PORT') HDC_UTID = os.environ.get('HDC_UTID') TMPDIR = os.environ.get('OHOS_REMOTE_TMP_DIR', '/data/local/tmp/Output') +DYN_LINKER = os.environ.get('OHOS_REMOTE_DYN_LINKER') # emit warning on import if some required constants are not set if not HDC_SERVER_IP_PORT or not HDC_UTID: diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py index de2aabe8107e..b84161f8b99c 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_common.py @@ -55,4 +55,4 @@ def push_to_device(path): dst_path = host_to_device_path(path) # hdc do not auto create directories on device hdc(['shell', 'mkdir', '-p', os.path.dirname(dst_path)]) - hdc(['file', 'send', path, dst_path], attempts=5, check_stdout='FileTransfer finish') + hdc(['file', 'send', '-m', path, dst_path], attempts=5, check_stdout='FileTransfer finish') diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py index 9006dac3d3b9..b3f48571588e 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_compile.py @@ -21,20 +21,20 @@ while args: output_type = 'object' elif arg == '-o': output = args.pop(0) +if hdc_constants.DYN_LINKER: + append_args.append('-Wl,--dynamic-linker=' + hdc_constants.DYN_LINKER) if output == None: print ("No output file name!") sys.exit(1) -with open(f'{output}.stderr', 'w') as f: - ret = subprocess.call(sys.argv[1:] + append_args, stderr=f) +ret = subprocess.call(sys.argv[1:] + append_args) if ret != 0: sys.exit(ret) if output_type in ['executable', 'shared']: push_to_device(output) - hdc(['shell', 'chmod', '+x', host_to_device_path(output)]) if output_type == 'executable': os.rename(output, output + '.real') diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py index c53c94c46ce9..675e064fa8ff 100755 --- a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_run.py @@ -6,6 +6,21 @@ from ohos_common import * device_binary = host_to_device_path(sys.argv[0]) +def map_path(path, do_push): + if os.path.exists(path): + if do_push: + push_to_device(path) + return host_to_device_path(path) + return path + +def map_list(value, sep, regex, get_path_and_do_push): + def repl(m): + path, do_push = get_path_and_do_push(m) + return map_path(path, do_push) + + opts = value.split(sep) + return sep.join(re.sub(regex, repl, opt) for opt in opts) + def build_env(): args = [] sanitizers = ( @@ -29,7 +44,14 @@ def build_env(): san_opt = key.endswith('SAN_OPTIONS') if san_opt: value += ':abort_on_error=0' - if key in ['ASAN_ACTIVATION_OPTIONS', 'SCUDO_OPTIONS'] or san_opt: + if key in ['ASAN_ACTIVATION_OPTIONS', 'SCUDO_OPTIONS'] or san_opt or key == 'LD_LIBRARY_PATH': + if key == 'TSAN_OPTIONS': + # Map TSan suppressions file to device + value = map_list(value, ':', r'(?<=suppressions=)(.+)', lambda m: (m.group(1), True)) + elif key == 'LD_LIBRARY_PATH': + # Map LD_LIBRARY_PATH to device + value = map_list(value, ':', r'(.+)', lambda m: (m.group(1), False)) + args.append('%s="%s"' % (key, value)) return ' '.join(args) @@ -49,6 +71,8 @@ hdc(['shell', 'unset UBSAN_OPTIONS && cd %s && %s %s %s %s >%s 2>%s ; echo $? >% sys.stdout.write(pull_from_device(device_stdout)) sys.stderr.write(pull_from_device(device_stderr)) +sys.stdout.flush() +sys.stderr.flush() retcode = int(pull_from_device(device_exitcode)) # If the device process died with a signal, do abort(). # Not exactly the same, but good enough to fool "not --crash". diff --git a/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_tool.py b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_tool.py new file mode 100755 index 000000000000..cb3eb0fdabd6 --- /dev/null +++ b/compiler-rt/test/sanitizer_common/ohos_family_commands/ohos_tool.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import os, sys, subprocess + +def map_arg(arg): + if not arg.startswith('-') and os.path.exists(arg) and os.path.exists(arg + '.real'): + return arg + '.real' + return arg + +args = [map_arg(arg) for arg in sys.argv[2:]] +sys.exit(subprocess.call([sys.argv[1]] + args)) diff --git a/compiler-rt/test/tsan/CMakeLists.txt b/compiler-rt/test/tsan/CMakeLists.txt index 25e95aa98cf9..45039aecff7b 100644 --- a/compiler-rt/test/tsan/CMakeLists.txt +++ b/compiler-rt/test/tsan/CMakeLists.txt @@ -122,11 +122,15 @@ if(COMPILER_RT_INCLUDE_TESTS) ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic/lit.site.cfg.py) endif() - list(APPEND TSAN_TEST_DEPS TsanUnitTests) - list(APPEND TSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit) - if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) - list(APPEND TSAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic) + # OHOS_LOCAL begin + if (NOT OHOS) + list(APPEND TSAN_TEST_DEPS TsanUnitTests) + list(APPEND TSAN_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit) + if(COMPILER_RT_TSAN_HAS_STATIC_RUNTIME) + list(APPEND TSAN_DYNAMIC_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/Unit/dynamic) + endif() endif() + # OHOS_LOCAL end endif() add_lit_testsuite(check-tsan "Running ThreadSanitizer tests" diff --git a/compiler-rt/test/tsan/Linux/lit.local.cfg.py b/compiler-rt/test/tsan/Linux/lit.local.cfg.py index 57271b8078a4..ce7ce5ddde93 100644 --- a/compiler-rt/test/tsan/Linux/lit.local.cfg.py +++ b/compiler-rt/test/tsan/Linux/lit.local.cfg.py @@ -5,5 +5,5 @@ def getRoot(config): root = getRoot(config) -if root.host_os not in ['Linux']: +if root.host_os not in ['Linux', 'OHOS']: # OHOS_LOCAL config.unsupported = True diff --git a/compiler-rt/test/tsan/Linux/mutex_robust.cpp b/compiler-rt/test/tsan/Linux/mutex_robust.cpp index 5ca5e70d49a7..bddf57e96539 100644 --- a/compiler-rt/test/tsan/Linux/mutex_robust.cpp +++ b/compiler-rt/test/tsan/Linux/mutex_robust.cpp @@ -1,4 +1,7 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s +// OHOS_LOCAL +// pthread_mutexattr_setrobust not available on OHOS musl +// UNSUPPORTED: ohos_family #include #include #include diff --git a/compiler-rt/test/tsan/Linux/mutex_robust2.cpp b/compiler-rt/test/tsan/Linux/mutex_robust2.cpp index 0914c1763604..d6cae7a7c497 100644 --- a/compiler-rt/test/tsan/Linux/mutex_robust2.cpp +++ b/compiler-rt/test/tsan/Linux/mutex_robust2.cpp @@ -1,4 +1,7 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s +// OHOS_LOCAL +// pthread_mutexattr_setrobust not available on OHOS musl +// UNSUPPORTED: ohos_family #include #include #include diff --git a/compiler-rt/test/tsan/Linux/thread_timedjoin.c b/compiler-rt/test/tsan/Linux/thread_timedjoin.c index 1d3f1098ece7..a7cdb335bcbe 100644 --- a/compiler-rt/test/tsan/Linux/thread_timedjoin.c +++ b/compiler-rt/test/tsan/Linux/thread_timedjoin.c @@ -1,4 +1,7 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s +// OHOS_LOCAL +// pthread_timedjoin_np not available on OHOS musl +// UNSUPPORTED: ohos_family #define _GNU_SOURCE #include "../test.h" #include diff --git a/compiler-rt/test/tsan/Linux/thread_tryjoin.c b/compiler-rt/test/tsan/Linux/thread_tryjoin.c index 675e1595528a..789842e0ed1b 100644 --- a/compiler-rt/test/tsan/Linux/thread_tryjoin.c +++ b/compiler-rt/test/tsan/Linux/thread_tryjoin.c @@ -1,4 +1,7 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s +// OHOS_LOCAL +// pthread_tryjoin_np not available on OHOS musl +// UNSUPPORTED: ohos_family #define _GNU_SOURCE #include "../test.h" #include @@ -29,7 +32,7 @@ int main() { if (!res) break; check(res); - pthread_yield(); + sched_yield(); // OHOS_LOCAL } var = 2; fprintf(stderr, "PASS\n"); diff --git a/compiler-rt/test/tsan/cond_cancel.c b/compiler-rt/test/tsan/cond_cancel.c index 7f20b8f8add4..0c741f7106a0 100644 --- a/compiler-rt/test/tsan/cond_cancel.c +++ b/compiler-rt/test/tsan/cond_cancel.c @@ -9,6 +9,9 @@ // however the same version GLIBC-2.17 will not make fail the test on // powerpc64 BE (VMA=46) // UNSUPPORTED: powerpc64-unknown-linux-gnu +// OHOS_LOCAL +// pthread_cancel not available on OHOS musl +// UNSUPPORTED: ohos_family #include "test.h" diff --git a/compiler-rt/test/tsan/debugging.cpp b/compiler-rt/test/tsan/debugging.cpp index be0a0c7b0824..ec4dd59625c3 100644 --- a/compiler-rt/test/tsan/debugging.cpp +++ b/compiler-rt/test/tsan/debugging.cpp @@ -79,7 +79,8 @@ __tsan_on_report(void *report) { tid, addr, size, write, atomic); // CHECK: tid = 1, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0 fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]); - // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}} + // OHOS_LOCAL + // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)|0}} __tsan_get_report_mop(report, 1, &tid, &addr, &size, &write, &atomic, trace, 16); @@ -87,7 +88,8 @@ __tsan_on_report(void *report) { tid, addr, size, write, atomic); // CHECK: tid = 0, addr = [[GLOBAL]], size = 8, write = 1, atomic = 0 fprintf(stderr, "trace[0] = %p, trace[1] = %p\n", trace[0], trace[1]); - // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)}} + // OHOS_LOCAL + // CHECK: trace[0] = 0x{{[0-9a-f]+}}, trace[1] = {{0x0|\(nil\)|\(null\)|0}} fprintf(stderr, "thread_count = %d\n", thread_count); // CHECK: thread_count = 2 diff --git a/compiler-rt/test/tsan/dtls.c b/compiler-rt/test/tsan/dtls.c index 57c9da83a374..9b4febb0c019 100644 --- a/compiler-rt/test/tsan/dtls.c +++ b/compiler-rt/test/tsan/dtls.c @@ -32,6 +32,9 @@ void *Thread2(void *arg) { } int main(int argc, char *argv[]) { + // OHOS_LOCAL + dlerror(); // Clear any previous errors + char path[4096]; snprintf(path, sizeof(path), "%s-so.so", argv[0]); diff --git a/compiler-rt/test/tsan/fiber_from_thread.cpp b/compiler-rt/test/tsan/fiber_from_thread.cpp index d27379e7acf4..7f859a0c55e8 100644 --- a/compiler-rt/test/tsan/fiber_from_thread.cpp +++ b/compiler-rt/test/tsan/fiber_from_thread.cpp @@ -1,6 +1,9 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s // UNSUPPORTED: tvos, watchos // XFAIL: ios && !iossim +// OHOS_LOCAL +// ucontext.h not available on musl +// UNSUPPORTED: ohos_family #include "sanitizer_common/sanitizer_ucontext.h" #include "test.h" diff --git a/compiler-rt/test/tsan/fiber_longjmp.cpp b/compiler-rt/test/tsan/fiber_longjmp.cpp index e56fd21befbb..189726e4a042 100644 --- a/compiler-rt/test/tsan/fiber_longjmp.cpp +++ b/compiler-rt/test/tsan/fiber_longjmp.cpp @@ -1,6 +1,9 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s // UNSUPPORTED: tvos, watchos // XFAIL: ios && !iossim +// OHOS_LOCAL +// ucontext.h not available on musl +// UNSUPPORTED: ohos_family #include "sanitizer_common/sanitizer_ucontext.h" #include "test.h" #include diff --git a/compiler-rt/test/tsan/fiber_race.cpp b/compiler-rt/test/tsan/fiber_race.cpp index add66940ffd1..eef17cf42c5c 100644 --- a/compiler-rt/test/tsan/fiber_race.cpp +++ b/compiler-rt/test/tsan/fiber_race.cpp @@ -1,6 +1,9 @@ // RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s // UNSUPPORTED: tvos, watchos // XFAIL: ios && !iossim +// OHOS_LOCAL +// ucontext.h not available on musl +// UNSUPPORTED: ohos_family #include "sanitizer_common/sanitizer_ucontext.h" #include "test.h" diff --git a/compiler-rt/test/tsan/fiber_simple.cpp b/compiler-rt/test/tsan/fiber_simple.cpp index 8123eb8eafc8..633f8bd408f5 100644 --- a/compiler-rt/test/tsan/fiber_simple.cpp +++ b/compiler-rt/test/tsan/fiber_simple.cpp @@ -1,6 +1,9 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s // UNSUPPORTED: tvos, watchos // XFAIL: ios && !iossim +// OHOS_LOCAL +// ucontext.h not available on musl +// UNSUPPORTED: ohos_family #include "sanitizer_common/sanitizer_ucontext.h" #include "test.h" diff --git a/compiler-rt/test/tsan/fiber_two_threads.cpp b/compiler-rt/test/tsan/fiber_two_threads.cpp index d2961b94dde1..b4d6604c7ef1 100644 --- a/compiler-rt/test/tsan/fiber_two_threads.cpp +++ b/compiler-rt/test/tsan/fiber_two_threads.cpp @@ -1,6 +1,9 @@ // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s // UNSUPPORTED: tvos, watchos // XFAIL: ios && !iossim +// OHOS_LOCAL +// ucontext.h not available on musl +// UNSUPPORTED: ohos_family #include "sanitizer_common/sanitizer_ucontext.h" #include "test.h" diff --git a/compiler-rt/test/tsan/ignore_lib1.cpp b/compiler-rt/test/tsan/ignore_lib1.cpp index 01139519e491..2b17dee50141 100644 --- a/compiler-rt/test/tsan/ignore_lib1.cpp +++ b/compiler-rt/test/tsan/ignore_lib1.cpp @@ -16,6 +16,10 @@ // FIXME: This test regularly fails on powerpc64 LE possibly starting with // r279664. Re-enable the test once the problem(s) have been fixed. +// OHOS_LOCAL +// dlopen not intercepted on OHOS +// UNSUPPORTED: ohos_family + #ifndef LIB #include diff --git a/compiler-rt/test/tsan/ignore_lib2.cpp b/compiler-rt/test/tsan/ignore_lib2.cpp index 05b7c2ed915a..24b7f5e66a3e 100644 --- a/compiler-rt/test/tsan/ignore_lib2.cpp +++ b/compiler-rt/test/tsan/ignore_lib2.cpp @@ -6,6 +6,10 @@ // RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t-dir/executable // RUN: %env_tsan_opts=suppressions='%s.supp' %deflake %run %t-dir/executable | FileCheck %s +// OHOS_LOCAL +// dlopen not intercepted on OHOS +// UNSUPPORTED: ohos_family + // Tests that called_from_lib suppression matched against 2 libraries // causes program crash (this is not supported). diff --git a/compiler-rt/test/tsan/ignore_lib3.cpp b/compiler-rt/test/tsan/ignore_lib3.cpp index b1a3940d03b6..b13229184141 100644 --- a/compiler-rt/test/tsan/ignore_lib3.cpp +++ b/compiler-rt/test/tsan/ignore_lib3.cpp @@ -11,6 +11,10 @@ // Some aarch64 kernels do not support non executable write pages // REQUIRES: stable-runtime +// OHOS_LOCAL +// dlopen not intercepted on OHOS +// UNSUPPORTED: ohos_family + #ifndef LIB #include diff --git a/compiler-rt/test/tsan/ignore_lib4.cpp b/compiler-rt/test/tsan/ignore_lib4.cpp index 06241c7b89f3..00bde34639d0 100644 --- a/compiler-rt/test/tsan/ignore_lib4.cpp +++ b/compiler-rt/test/tsan/ignore_lib4.cpp @@ -13,6 +13,10 @@ // is matched against 2 libraries". // UNSUPPORTED: aarch64 +// OHOS_LOCAL +// dlopen not intercepted on OHOS +// UNSUPPORTED: ohos_family + // Test longjmp in ignored lib. // It used to crash since we jumped out of ScopedInterceptor scope. diff --git a/compiler-rt/test/tsan/ignore_lib5.cpp b/compiler-rt/test/tsan/ignore_lib5.cpp index a71d560ab913..9ae3f7bcbb95 100644 --- a/compiler-rt/test/tsan/ignore_lib5.cpp +++ b/compiler-rt/test/tsan/ignore_lib5.cpp @@ -21,6 +21,10 @@ // ReadProcMaps() on NetBSD does not handle >=1MB of memory layout information // UNSUPPORTED: netbsd +// OHOS_LOCAL +// dlopen not intercepted on OHOS +// UNSUPPORTED: ohos_family + #ifndef LIB #include diff --git a/compiler-rt/test/tsan/ignored-interceptors-mmap.cpp b/compiler-rt/test/tsan/ignored-interceptors-mmap.cpp index 2b35627229df..bbf43be5b605 100644 --- a/compiler-rt/test/tsan/ignored-interceptors-mmap.cpp +++ b/compiler-rt/test/tsan/ignored-interceptors-mmap.cpp @@ -49,7 +49,10 @@ int main(int argc, const char *argv[]) { return 0; } -// CHECK-RACE: WARNING: ThreadSanitizer: data race -// CHECK-RACE: OK +// OHOS_LOCAL begin +// CHECK-RACE-DAG: WARNING: ThreadSanitizer: data race +// CHECK-RACE-DAG: OK // CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race // CHECK-IGNORE: OK +// CHECK-IGNORE-NOT: WARNING: ThreadSanitizer: data race +// OHOS_LOCAL end diff --git a/compiler-rt/test/tsan/java_finalizer2.cpp b/compiler-rt/test/tsan/java_finalizer2.cpp index 87528900541a..77e222766bd8 100644 --- a/compiler-rt/test/tsan/java_finalizer2.cpp +++ b/compiler-rt/test/tsan/java_finalizer2.cpp @@ -37,9 +37,9 @@ void *Thread3(void *p) { Heap* heap = (Heap*)p; pthread_barrier_wait(&heap->barrier_finalizer); while (__atomic_load_n(&heap->ready, __ATOMIC_ACQUIRE) != 1) - pthread_yield(); + sched_yield(); // OHOS_LOCAL while (__atomic_load_n(&heap->finalized, __ATOMIC_RELAXED) != 1) - pthread_yield(); + sched_yield(); // OHOS_LOCAL __atomic_fetch_add(&heap->wg, 1, __ATOMIC_RELEASE); return 0; } @@ -70,7 +70,7 @@ int main() { pthread_join(ballast[i], 0); pthread_barrier_wait(&heap->barrier_finalizer); while (__atomic_load_n(&heap->wg, __ATOMIC_ACQUIRE) != 2) - pthread_yield(); + sched_yield(); // OHOS_LOCAL if (heap->data != 1) exit(printf("no data\n")); for (int i = 0; i < 3; i++) diff --git a/compiler-rt/test/tsan/lit.cfg.py b/compiler-rt/test/tsan/lit.cfg.py index 4295514f0a5e..ddd4d21642fa 100644 --- a/compiler-rt/test/tsan/lit.cfg.py +++ b/compiler-rt/test/tsan/lit.cfg.py @@ -18,24 +18,25 @@ config.name = 'ThreadSanitizer' + config.name_suffix config.test_source_root = os.path.dirname(__file__) # Setup environment variables for running ThreadSanitizer. -default_tsan_opts = "atexit_sleep_ms=0" +# OHOS_LOCAL begin +default_tsan_opts = list(config.default_sanitizer_opts) +default_tsan_opts += ["atexit_sleep_ms=0"] if config.host_os == 'Darwin': - # On Darwin, we default to `abort_on_error=1`, which would make tests run - # much slower. Let's override this and run lit tests with 'abort_on_error=0'. - default_tsan_opts += ':abort_on_error=0' # On Darwin, we default to ignore_noninstrumented_modules=1, which also # suppresses some races the tests are supposed to find. Let's run without this # setting, but turn it back on for Darwin tests (see Darwin/lit.local.cfg.py). - default_tsan_opts += ':ignore_noninstrumented_modules=0' - default_tsan_opts += ':ignore_interceptors_accesses=0' + default_tsan_opts += ['ignore_noninstrumented_modules=0'] + default_tsan_opts += ['ignore_interceptors_accesses=0'] # Platform-specific default TSAN_OPTIONS for lit tests. -if default_tsan_opts: - config.environment['TSAN_OPTIONS'] = default_tsan_opts - default_tsan_opts += ':' +default_tsan_opts_str = ':'.join(default_tsan_opts) +if default_tsan_opts_str: + config.environment['TSAN_OPTIONS'] = default_tsan_opts_str + default_tsan_opts_str += ':' config.substitutions.append(('%env_tsan_opts=', - 'env TSAN_OPTIONS=' + default_tsan_opts)) + 'env TSAN_OPTIONS=' + default_tsan_opts_str)) +# OHOS_LOCAL end # GCC driver doesn't add necessary compile/link flags with -fsanitize=thread. if config.compiler_id == 'GNU': @@ -81,7 +82,7 @@ config.substitutions.append( ("%deflake ", os.path.join(os.path.dirname(__file__ # Default test suffixes. config.suffixes = ['.c', '.cpp', '.m', '.mm'] -if config.host_os not in ['FreeBSD', 'Linux', 'Darwin', 'NetBSD']: +if config.host_os not in ['FreeBSD', 'Linux', 'Darwin', 'NetBSD', 'OHOS']: # OHOS_LOCAL config.unsupported = True if config.android: diff --git a/compiler-rt/test/tsan/mutex_lock_destroyed.cpp b/compiler-rt/test/tsan/mutex_lock_destroyed.cpp index 892d44400541..1a4135008410 100644 --- a/compiler-rt/test/tsan/mutex_lock_destroyed.cpp +++ b/compiler-rt/test/tsan/mutex_lock_destroyed.cpp @@ -5,6 +5,10 @@ // The pthread_mutex_lock interceptor assumes incompatible internals w/ NetBSD // XFAIL: netbsd +// OHOS_LOCAL +// musl seems to not return error when calling pthread_mutex_lock with a destroyed mutex +// UNSUPPORTED: ohos_family + #include #include #include diff --git a/compiler-rt/test/tsan/on_exit.cpp b/compiler-rt/test/tsan/on_exit.cpp index 2dadd68c144a..567fd0a8a73b 100644 --- a/compiler-rt/test/tsan/on_exit.cpp +++ b/compiler-rt/test/tsan/on_exit.cpp @@ -3,6 +3,10 @@ // on_exit() is not available on Darwin. // UNSUPPORTED: darwin +// OHOS_LOCAL +// on_exit() is not available on musl. +// UNSUPPORTED: ohos_family + #include "test.h" volatile long global; diff --git a/compiler-rt/test/tsan/signal_sync2.cpp b/compiler-rt/test/tsan/signal_sync2.cpp index a1c31dcdc1ee..7f61dae6a9a6 100644 --- a/compiler-rt/test/tsan/signal_sync2.cpp +++ b/compiler-rt/test/tsan/signal_sync2.cpp @@ -13,13 +13,21 @@ const int kSignalCount = 500; -__thread int process_signals; +// OHOS_LOCAL begin +pthread_t main_tid; +int process_signals; +// OHOS_LOCAL end int signals_handled; int done; int ready[kSignalCount]; long long data[kSignalCount]; static void handler(int sig) { + // OHOS_LOCAL begin + // avoid using possibly emulated TLS in signal handlers + if (pthread_self() == main_tid) + return; + // OHOS_LOCAL end if (!__atomic_load_n(&process_signals, __ATOMIC_RELAXED)) return; int pos = signals_handled++; @@ -39,6 +47,8 @@ static void* thr(void *p) { } int main() { + main_tid = pthread_self(); // OHOS_LOCAL + struct sigaction act = {}; act.sa_handler = handler; if (sigaction(SIGPROF, &act, 0)) { diff --git a/compiler-rt/test/tsan/strerror_r.cpp b/compiler-rt/test/tsan/strerror_r.cpp index 438f54914d6c..3b0497ed97f3 100644 --- a/compiler-rt/test/tsan/strerror_r.cpp +++ b/compiler-rt/test/tsan/strerror_r.cpp @@ -1,7 +1,8 @@ // RUN: %clangxx_tsan -O1 -DTEST_ERROR=ERANGE %s -o %t && %run %t 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-SYS %s // RUN: %clangxx_tsan -O1 -DTEST_ERROR=-1 %s -o %t && not %run %t 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-USER %s // This test is for GNU specific version of strerror_r() -// UNSUPPORTED: darwin, netbsd, freebsd +// OHOS_LOCAL +// UNSUPPORTED: darwin, netbsd, freebsd, ohos_family #include "test.h" diff --git a/compiler-rt/test/tsan/thread_name.cpp b/compiler-rt/test/tsan/thread_name.cpp index bc65421b36d0..f6396ed29c79 100644 --- a/compiler-rt/test/tsan/thread_name.cpp +++ b/compiler-rt/test/tsan/thread_name.cpp @@ -1,7 +1,8 @@ // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s #include "test.h" -#if defined(__linux__) +// OHOS_LOCAL +#if defined(__linux__) && !defined(__OHOS__) #define USE_PTHREAD_SETNAME_NP __GLIBC_PREREQ(2, 12) #define tsan_pthread_setname_np pthread_setname_np #elif defined(__FreeBSD__) @@ -11,6 +12,11 @@ #elif defined(__NetBSD__) #define USE_PTHREAD_SETNAME_NP 1 #define tsan_pthread_setname_np(a, b) pthread_setname_np((a), "%s", (void *)(b)) +// OHOS_LOCAL begin +#elif defined(__OHOS__) +#define USE_PTHREAD_SETNAME_NP 1 +#define tsan_pthread_setname_np pthread_setname_np +// OHOS_LOCAL end #else #define USE_PTHREAD_SETNAME_NP 0 #endif diff --git a/compiler-rt/test/tsan/tls_race.cpp b/compiler-rt/test/tsan/tls_race.cpp index 5f5b6aae4f58..0821680d0aae 100644 --- a/compiler-rt/test/tsan/tls_race.cpp +++ b/compiler-rt/test/tsan/tls_race.cpp @@ -24,3 +24,5 @@ int main() { // CHECK-FreeBSD: Location is TLS of main thread. // CHECK-NetBSD: Location is TLS of main thread. // CHECK-Darwin: Location is heap block of size 4 +// OHOS_LOCAL +// CHECK-OHOS: Location is heap block of size diff --git a/compiler-rt/test/tsan/tls_race2.cpp b/compiler-rt/test/tsan/tls_race2.cpp index 0e008313518a..b05547e6643f 100644 --- a/compiler-rt/test/tsan/tls_race2.cpp +++ b/compiler-rt/test/tsan/tls_race2.cpp @@ -33,4 +33,6 @@ int main() { // CHECK-FreeBSD: Location is TLS of thread T1. // CHECK-NetBSD: Location is TLS of thread T1. // CHECK-Darwin: Location is heap block of size 4 +// OHOS_LOCAL +// CHECK-OHOS: Location is heap block of size // CHECK: DONE diff --git a/llvm/utils/lit/lit/llvm/subst.py b/llvm/utils/lit/lit/llvm/subst.py index 4275b8a56a3b..c51a257386bb 100644 --- a/llvm/utils/lit/lit/llvm/subst.py +++ b/llvm/utils/lit/lit/llvm/subst.py @@ -29,6 +29,18 @@ class FindTool(object): command += ' go=' + exe return command +# OHOS_LOCAL begin +class WrapTool(FindTool): + def __init__(self, name, wrapper): + super().__init__(name) + self.wrapper = wrapper + + def resolve(self, config, dirs): + command = super().resolve(config, dirs) + if not command: + return None + return self.wrapper + " " + command +# OHOS_LOCAL end class ToolSubst(object): """String-like class used to build regex substitution patterns for llvm -- Gitee From c448a8b5219ac8782d8fe8101d1a2db588382168 Mon Sep 17 00:00:00 2001 From: lubinglun Date: Thu, 8 Feb 2024 05:38:44 +0000 Subject: [PATCH 26/44] [Build][AArch64 Host]Add needed libs for cross-compiling;Add needed headerfiles for compiling cpp files Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I91X8G Signed-off-by: lubinglun Change-Id: I8fc570e4638ade0fc773a2fc1e2fb42d2c25a37d --- llvm-build/build-ohos-aarch64.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index 0a57f7e5542d..71aa0a0f6aa7 100755 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -68,7 +68,7 @@ def main(): llvm_defines['CMAKE_INSTALL_PREFIX'] = llvm_install llvm_defines['CMAKE_SYSROOT'] = sysroot llvm_defines['CMAKE_LIBRARY_ARCHITECTURE'] = llvm_triple - llvm_defines['LLVM_TARGETS_TO_BUILD'] = 'AArch64' + llvm_defines['LLVM_TARGETS_TO_BUILD'] = build_config.TARGETS llvm_defines['LLVM_TARGET_ARCH'] = 'AArch64' llvm_defines['LLVM_DEFAULT_TARGET_TRIPLE'] = llvm_triple llvm_defines['LLVM_BUILD_LLVM_DYLIB'] = 'ON' @@ -128,10 +128,18 @@ def main(): build_utils.invoke_ninja(out_path=llvm_path, env=env, target=None, install=True) # Copy required aarch64-linux-ohos libs from main toolchain build. - build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', llvm_triple), - os.path.join(llvm_install, 'lib', llvm_triple)) - build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', 'clang', '15.0.4', 'lib', llvm_triple), - os.path.join(llvm_install, 'lib', 'clang', '15.0.4', 'lib', llvm_triple)) + arch_list = [build_utils.liteos_triple('arm'), build_utils.open_ohos_triple('arm'), + build_utils.open_ohos_triple('aarch64'), build_utils.open_ohos_triple('riscv64'), + build_utils.open_ohos_triple('mipsel'), build_utils.open_ohos_triple('x86_64')] + for arch in arch_list: + build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', arch), + os.path.join(llvm_install, 'lib', arch)) + build_utils.check_copy_tree(os.path.join(llvm_root, 'lib', 'clang', '15.0.4', 'lib', arch), + os.path.join(llvm_install, 'lib', 'clang', '15.0.4', 'lib', arch)) + + #Copy required c++ headerfiles from main toolchain build. + build_utils.check_copy_tree(os.path.join(llvm_root, 'include', 'c++'), os.path.join(llvm_install, 'include', 'c++')) + build_utils.check_copy_tree(os.path.join(llvm_root, 'include', 'libcxx-ohos'), os.path.join(llvm_install, 'include', 'libcxx-ohos')) # Package ohos-aarch64 toolchain. if build_config.do_package: -- Gitee From 6e862eb025247c3d8cdd964306a55a59da3db17b Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Mon, 19 Feb 2024 17:16:03 +0300 Subject: [PATCH 27/44] [Build][AArch64 Host] Install llvm utils Install count, not, yaml2obj, etc. Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I91U0C Signed-off-by: Lyupa Anastasia --- llvm-build/build-ohos-aarch64.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index 71aa0a0f6aa7..84c7e1e8feba 100755 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -78,6 +78,7 @@ def main(): llvm_defines['LLVM_INCLUDE_EXAMPLES'] = 'OFF' llvm_defines['LLVM_INCLUDE_TESTS'] = 'OFF' llvm_defines['LLVM_BUILD_TOOLS'] = 'ON' + llvm_defines['LLVM_INSTALL_UTILS'] = 'ON' llvm_defines['LLVM_ENABLE_ZLIB'] = 'OFF' llvm_defines['LLVM_ENABLE_PROJECTS'] = 'clang;clang-tools-extra;lld;lldb;openmp' # We do not build runtimes, since they will be copied from main toolchain build -- Gitee From fac380401edcb7ba0fd78b3a567cc3203f609955 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Tue, 20 Feb 2024 17:53:41 +0300 Subject: [PATCH 28/44] [Build][AArch64 Host] Add libomptarget .bc files Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I92NHW Signed-off-by: Lyupa Anastasia --- llvm-build/build-ohos-aarch64.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llvm-build/build-ohos-aarch64.py b/llvm-build/build-ohos-aarch64.py index 84c7e1e8feba..9dc42111624a 100755 --- a/llvm-build/build-ohos-aarch64.py +++ b/llvm-build/build-ohos-aarch64.py @@ -115,6 +115,8 @@ def main(): llvm_defines['CMAKE_C_FLAGS_RELEASE'] = cflags_release llvm_defines['CMAKE_CXX_FLAGS_RELEASE'] = cflags_release llvm_defines['CMAKE_ASM_FLAGS_RELEASE'] = cflags_release + llvm_defines['OPENMP_STANDALONE_BUILD'] = 'ON' + llvm_defines['LLVM_DIR'] = os.path.join(llvm_root, 'lib', 'cmake', 'llvm') if build_config.enable_assertions: llvm_defines['LLVM_ENABLE_ASSERTIONS'] = 'ON' -- Gitee From 2ad1fb7a884014e1b0089db3d5eb20a25fad13b3 Mon Sep 17 00:00:00 2001 From: liuyaning Date: Wed, 7 Feb 2024 16:51:03 +0800 Subject: [PATCH 29/44] [Build]Add a build libs debug args Add buildlibs_debug args Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I91GL8 Test:no Signed-off-by: liuyaning --- llvm-build/build.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/llvm-build/build.py b/llvm-build/build.py index f35164454c3b..50f4bc19b086 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -51,6 +51,7 @@ class BuildConfig(): self.need_libs = self.do_build and 'libs' not in args.no_build self.need_lldb_server = self.do_build and 'lldb-server' not in args.no_build self.build_python = args.build_python + self.build_with_debug_info = args.build_with_debug_info self.no_build_arm = args.skip_build or args.no_build_arm self.no_build_aarch64 = args.skip_build or args.no_build_aarch64 @@ -243,6 +244,12 @@ class BuildConfig(): default='bz2', help='Choose compression output format (bz2 or gz)' ) + + parser.add_argument( + '--build-with-debug-info', + action='store_true', + default=False, + help='Append -g to build flags in build_libs') def parse_args(self): @@ -1186,6 +1193,8 @@ class LlvmLibs(BuildUtils): '-ffunction-sections', '-fdata-sections', extra_flags, ] + if self.build_config.build_with_debug_info: + cflag.append('-g') cflags.extend(cflag) -- Gitee From 68a0ef271765e3431bee4ee05a32e24c43b8942a Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Mon, 19 Feb 2024 13:29:02 +0300 Subject: [PATCH 30/44] Revert "[compiler-rt] TMP: disable not working tests and runtimes" This reverts commit 1302f071d4be91f9a31544d460d7d741b28a9c80. Signed-off-by: Sukhikh Alexander --- compiler-rt/cmake/config-ix.cmake | 1 - compiler-rt/lib/CMakeLists.txt | 18 +++--------------- compiler-rt/test/CMakeLists.txt | 18 ++---------------- 3 files changed, 5 insertions(+), 32 deletions(-) diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index ac88d54ccfd2..a549732afea0 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -814,7 +814,6 @@ else() endif() #TODO(kostyak): add back Android & Fuchsia when the code settles a bit. -# TODO: Add OHOS when https://gitee.com/openharmony/third_party_musl/pulls/1235 is merged if (SCUDO_STANDALONE_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux" AND COMPILER_RT_HAS_AUXV) set(COMPILER_RT_HAS_SCUDO_STANDALONE TRUE) diff --git a/compiler-rt/lib/CMakeLists.txt b/compiler-rt/lib/CMakeLists.txt index 678bc03a8df2..18eed2446dc6 100644 --- a/compiler-rt/lib/CMakeLists.txt +++ b/compiler-rt/lib/CMakeLists.txt @@ -28,11 +28,9 @@ function(compiler_rt_build_runtime runtime) if(${runtime} STREQUAL tsan) add_subdirectory(tsan/dd) endif() - # Disable standalone scudo until this PR merged - # https://gitee.com/openharmony/third_party_musl/pulls/1235/files - # if(${runtime} STREQUAL scudo) - # add_subdirectory(scudo/standalone) - # endif() + if(${runtime} STREQUAL scudo) + add_subdirectory(scudo/standalone) + endif() endif() endfunction() @@ -40,16 +38,6 @@ if(COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_MEMPROF) compiler_rt_build_runtime(interception) endif() -# OHOS_LOCAL begin -if(COMPILER_RT_DEFAULT_TARGET_TRIPLE MATCHES "arm-linux-ohos|mipsel-linux-ohos") - # temporary disable scudo and gwp_asan for arm - # arm scudo build failed due to the abcsence of the `_aeabi_unwind_cpp_pr1` symbol - # on mipsel -latomic is not found. - # gwp_asan depends on scudo - list(REMOVE_ITEM COMPILER_RT_SANITIZERS_TO_BUILD "scudo" "gwp_asan") -endif() -# OHOS_LOCAL end - if(COMPILER_RT_BUILD_SANITIZERS) if(COMPILER_RT_HAS_SANITIZER_COMMON) add_subdirectory(stats) diff --git a/compiler-rt/test/CMakeLists.txt b/compiler-rt/test/CMakeLists.txt index e23d48147834..3cbfc4b0e544 100644 --- a/compiler-rt/test/CMakeLists.txt +++ b/compiler-rt/test/CMakeLists.txt @@ -58,16 +58,6 @@ function(compiler_rt_test_runtime runtime) endif() endfunction() -# OHOS_LOCAL begin -if(COMPILER_RT_DEFAULT_TARGET_TRIPLE MATCHES "arm-linux-ohos|mipsel-linux-ohos") - # temporary disable scudo and gwp_asan for arm - # arm scudo build failed due to the abcsence of the `_aeabi_unwind_cpp_pr1` symbol - # on mipsel -latomic is not found. - # gwp_asan depends on scudo - list(REMOVE_ITEM COMPILER_RT_SANITIZERS_TO_BUILD "scudo" "gwp_asan") -endif() -# OHOS_LOCAL end - # Run sanitizer tests only if we're sure that clang would produce # working binaries. if(COMPILER_RT_CAN_EXECUTE_TESTS) @@ -89,13 +79,9 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS) foreach(sanitizer ${COMPILER_RT_SANITIZERS_TO_BUILD}) # cfi testing is gated on ubsan - # OHOS_LOCAL begin - # can't test gwp-asan, because it is depends on scudo-standalone - if(NOT ${sanitizer} STREQUAL cfi AND - NOT ${sanitizer} STREQUAL gwp_asan) - compiler_rt_test_runtime(${sanitizer}) + if(NOT ${sanitizer} STREQUAL cfi) + compiler_rt_test_runtime(${sanitizer}) endif() - # OHOS_LOCAL end endforeach() endif() if(COMPILER_RT_BUILD_PROFILE AND COMPILER_RT_HAS_PROFILE) -- Gitee From 7a4806c5a6e88233a063734ed4f7423bd7f6bb15 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Mon, 12 Feb 2024 18:57:41 +0300 Subject: [PATCH 31/44] [ASan][OHOS] Not define missing interceptors Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I91IGU Signed-off-by: Lyupa Anastasia --- .../sanitizer_common_interceptors.inc | 2 +- .../sanitizer_platform_interceptors.h | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc index cd60952a2605..c489cb2979f1 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -2670,7 +2670,7 @@ INTERCEPTOR(int, wait3, int *status, int options, void *rusage) { } return res; } -#if SANITIZER_ANDROID || SANITIZER_OHOS +#if SANITIZER_ANDROID INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) { void *ctx; COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h index d2858ab538f3..816c2fe9878a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h @@ -241,8 +241,10 @@ #define SANITIZER_INTERCEPT_TIME SI_POSIX #define SANITIZER_INTERCEPT_GLOB (SI_GLIBC || SI_SOLARIS) #define SANITIZER_INTERCEPT_GLOB64 SI_GLIBC -#define SANITIZER_INTERCEPT___B64_TO SI_LINUX_NOT_ANDROID -#define SANITIZER_INTERCEPT___DN_EXPAND SI_LINUX_NOT_ANDROID +// OHOS_LOCAL begin +#define SANITIZER_INTERCEPT___B64_TO (SI_LINUX_NOT_ANDROID && !SI_OHOS) +#define SANITIZER_INTERCEPT___DN_EXPAND (SI_LINUX_NOT_ANDROID && !SI_OHOS) +// OHOS_LOCAL end #define SANITIZER_INTERCEPT_POSIX_SPAWN SI_POSIX #define SANITIZER_INTERCEPT_WAIT SI_POSIX #define SANITIZER_INTERCEPT_INET SI_POSIX @@ -360,11 +362,14 @@ #define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE SI_POSIX #define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL \ (SI_MAC || SI_NETBSD || SI_LINUX_NOT_ANDROID || SI_SOLARIS) +// OHOS_LOCAL begin #define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING \ - (SI_MAC || SI_NETBSD || SI_LINUX_NOT_ANDROID || SI_SOLARIS) + ((SI_MAC || SI_NETBSD || SI_LINUX_NOT_ANDROID || SI_SOLARIS) && !SI_OHOS) #define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST \ - (SI_LINUX_NOT_ANDROID || SI_SOLARIS) -#define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP SI_LINUX_NOT_ANDROID + ((SI_LINUX_NOT_ANDROID || SI_SOLARIS) && !SI_OHOS) +#define SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP \ + (SI_LINUX_NOT_ANDROID && !SI_OHOS) +// OHOS_LOCAL end #define SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED \ (SI_POSIX && !SI_NETBSD) #define SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP SI_GLIBC @@ -579,8 +584,9 @@ #define SANITIZER_INTERCEPT_FDEVNAME SI_FREEBSD #define SANITIZER_INTERCEPT_GETUSERSHELL (SI_POSIX && !SI_ANDROID) #define SANITIZER_INTERCEPT_SL_INIT (SI_FREEBSD || SI_NETBSD) +// OHOS_LOCAL #define SANITIZER_INTERCEPT_CRYPT (SI_POSIX && !SI_ANDROID) -#define SANITIZER_INTERCEPT_CRYPT_R (SI_LINUX && !SI_ANDROID) +#define SANITIZER_INTERCEPT_CRYPT_R (SI_LINUX && !SI_ANDROID && !SI_OHOS) #define SANITIZER_INTERCEPT_GETRANDOM \ ((SI_LINUX && __GLIBC_PREREQ(2, 25)) || SI_FREEBSD) -- Gitee From 99c036388e2848c2a030d2fcc1f81cf526b14948 Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Thu, 29 Feb 2024 14:57:07 +0800 Subject: [PATCH 32/44] * *[R&D][BUILD] Add llvm-dwarfdump to the package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue:https://gitee.com/openharmony/third_party_llvm-project/issues/I93VN4?from=project-issue Test:Toolchain compilation and packaging Signed-off-by: xwx1135370 --- llvm-build/build.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm-build/build.py b/llvm-build/build.py index 29d5d9d12a54..6782645fc3f1 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2030,6 +2030,7 @@ class LlvmPackage(BuildUtils): 'llvm-strings%s' % ext, 'llvm-strip%s' % ext, 'llvm-symbolizer%s' % ext, + 'llvm-dwarfdump%s' % ext, ] necessary_bin_files.extend(necessary_bin_file) -- Gitee From 750fb65e0920061d62631a1c64241e5416120ea3 Mon Sep 17 00:00:00 2001 From: Sukhikh Alexander Date: Wed, 21 Feb 2024 17:28:51 +0300 Subject: [PATCH 33/44] [compiler-rt] Fix sanitizer build issues for arm and mips Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I91GO7 Change-Id: Ided87d35042e9aa79e20322a15ae843147c1a01e Signed-off-by: Sukhikh Alexander --- compiler-rt/CMakeLists.txt | 8 ++++++++ compiler-rt/cmake/config-ix.cmake | 2 +- compiler-rt/lib/scudo/standalone/CMakeLists.txt | 11 +++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt index 0e3ce2ad7938..98ade77f0d76 100644 --- a/compiler-rt/CMakeLists.txt +++ b/compiler-rt/CMakeLists.txt @@ -565,6 +565,14 @@ if(COMPILER_RT_USE_LLVM_UNWINDER) endif() endif() +# OHOS_LOCAL begin +if (COMPILER_RT_HAS_SCUDO_STANDALONE AND + NOT COMPILER_RT_USE_LLVM_UNWINDER AND # unwinder not set yet + COMPILER_RT_DEFAULT_TARGET_TRIPLE MATCHES "^arm-.*") + list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "unwind") +endif() +# OHOS_LOCAL end + if (COMPILER_RT_CXX_LIBRARY STREQUAL "libcxx") # We are using the in-tree libc++ so avoid including the default one. append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ COMPILER_RT_COMMON_CFLAGS) diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index a549732afea0..3c6fe62e1aaf 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -814,7 +814,7 @@ else() endif() #TODO(kostyak): add back Android & Fuchsia when the code settles a bit. -if (SCUDO_STANDALONE_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux" AND +if (SCUDO_STANDALONE_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux|OHOS" AND COMPILER_RT_HAS_AUXV) set(COMPILER_RT_HAS_SCUDO_STANDALONE TRUE) else() diff --git a/compiler-rt/lib/scudo/standalone/CMakeLists.txt b/compiler-rt/lib/scudo/standalone/CMakeLists.txt index 4b586bc662b4..55e97ac0dadf 100644 --- a/compiler-rt/lib/scudo/standalone/CMakeLists.txt +++ b/compiler-rt/lib/scudo/standalone/CMakeLists.txt @@ -30,7 +30,13 @@ endif() set(SCUDO_LINK_FLAGS) -list(APPEND SCUDO_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro) +# OHOS_LOCAL begin +# We do not have all symbols at build time (get_platform_gwp_asan_tls_slot), +# since musl is built without them for some reason +if (NOT OHOS) + list(APPEND SCUDO_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro) +endif() +# OHOS_LOCAL end list(APPEND SCUDO_LINK_FLAGS -ffunction-sections -fdata-sections -Wl,--gc-sections) @@ -141,7 +147,8 @@ append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SCUDO_LINK_FLAGS) append_list_if(FUCHSIA zircon SCUDO_LINK_LIBS) -if(COMPILER_RT_DEFAULT_TARGET_ARCH MATCHES "mips|mips64|mipsel|mips64el") +if(COMPILER_RT_DEFAULT_TARGET_ARCH MATCHES "mips|mips64|mipsel|mips64el" AND + NOT OHOS) # OHOS_LOCAL OHOS uses compiler-rt list(APPEND SCUDO_LINK_LIBS atomic) endif() -- Gitee From 2388d10d2a8f5ae882288a33b756d57cc2a5b7e3 Mon Sep 17 00:00:00 2001 From: liujia178 Date: Fri, 1 Mar 2024 16:39:52 +0800 Subject: [PATCH 34/44] [lldb] Add support lzma in lldb Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I8PEAT Signed-off-by: liujia178 --- lldb/cmake/modules/LLDBConfig.cmake | 6 ++ lldb/include/lldb/Host/Config.h.cmake | 2 + lldb/source/Host/common/LZMA.cpp | 97 +++++++++++++++++++++++++++ llvm-build/MakeLiblzma | 65 ++++++++++++++++++ llvm-build/build.py | 86 +++++++++++++++++++++++- 5 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 llvm-build/MakeLiblzma diff --git a/lldb/cmake/modules/LLDBConfig.cmake b/lldb/cmake/modules/LLDBConfig.cmake index 55d1aa17b46b..209f8a1cd348 100644 --- a/lldb/cmake/modules/LLDBConfig.cmake +++ b/lldb/cmake/modules/LLDBConfig.cmake @@ -58,7 +58,13 @@ endmacro() add_optional_dependency(LLDB_ENABLE_LIBEDIT "Enable editline support in LLDB" LibEdit LibEdit_FOUND) add_optional_dependency(LLDB_ENABLE_CURSES "Enable curses support in LLDB" CursesAndPanel CURSESANDPANEL_FOUND) +if (NOT LLDB_ENABLE_LZMA_7ZIP) # OHOS_LOCAL add_optional_dependency(LLDB_ENABLE_LZMA "Enable LZMA compression support in LLDB" LibLZMA LIBLZMA_FOUND) +# OHOS_LOCAL begin +else() +message(STATUS "Enable LZMA-7zip compression support in LLDB: TRUE") +endif() +# OHOS_LOCAL end add_optional_dependency(LLDB_ENABLE_LUA "Enable Lua scripting support in LLDB" LuaAndSwig LUAANDSWIG_FOUND) add_optional_dependency(LLDB_ENABLE_PYTHON "Enable Python scripting support in LLDB" PythonAndSwig PYTHONANDSWIG_FOUND) add_optional_dependency(LLDB_ENABLE_LIBXML2 "Enable Libxml 2 support in LLDB" LibXml2 LIBXML2_FOUND VERSION 2.8) diff --git a/lldb/include/lldb/Host/Config.h.cmake b/lldb/include/lldb/Host/Config.h.cmake index 9ee0b9dcd04f..ad69d0bfe743 100644 --- a/lldb/include/lldb/Host/Config.h.cmake +++ b/lldb/include/lldb/Host/Config.h.cmake @@ -32,6 +32,8 @@ #cmakedefine01 LLDB_ENABLE_TERMIOS #cmakedefine01 LLDB_ENABLE_LZMA +// OHOS_LOCAL +#cmakedefine01 LLDB_ENABLE_LZMA_7ZIP #cmakedefine01 LLDB_ENABLE_CURSES diff --git a/lldb/source/Host/common/LZMA.cpp b/lldb/source/Host/common/LZMA.cpp index 5b457f07afca..c94c636b517e 100644 --- a/lldb/source/Host/common/LZMA.cpp +++ b/lldb/source/Host/common/LZMA.cpp @@ -11,7 +11,16 @@ #include "llvm/Support/Error.h" #if LLDB_ENABLE_LZMA +// OHOS_LOCAL begin +#if !LLDB_ENABLE_LZMA_7ZIP #include +#else +#include <7zCrc.h> +#include +#include +#define EXPAND_FACTOR 2 +#endif +// OHOS_LOCAL end #endif // LLDB_ENABLE_LZMA namespace lldb_private { @@ -34,6 +43,8 @@ llvm::Error uncompress(llvm::ArrayRef InputBuffer, bool isAvailable() { return true; } +// OHOS_LOCAL +#if !LLDB_ENABLE_LZMA_7ZIP static const char *convertLZMACodeToString(lzma_ret Code) { switch (Code) { case LZMA_STREAM_END: @@ -139,7 +150,93 @@ llvm::Error uncompress(llvm::ArrayRef InputBuffer, return llvm::Error::success(); } +// OHOS_LOCAL begin +#else +static const char *convertLZMACodeToString(SRes Code) { + switch (Code) { + case SZ_ERROR_DATA: + return "lzma error: SZ_ERROR_DATA"; + case SZ_ERROR_MEM: + return "lzma error: SZ_ERROR_MEM"; + case SZ_ERROR_CRC: + return "lzma error: SZ_ERROR_CRC"; + case SZ_ERROR_UNSUPPORTED: + return "lzma error: SZ_ERROR_UNSUPPORTED"; + case SZ_ERROR_PARAM: + return "lzma error: SZ_ERROR_PARAM"; + case SZ_ERROR_INPUT_EOF: + return "lzma error: SZ_ERROR_INPUT_EOF"; + case SZ_ERROR_OUTPUT_EOF: + return "lzma error: SZ_ERROR_OUTPUT_EOF"; + case SZ_ERROR_READ: + return "lzma error: SZ_ERROR_READ"; + case SZ_ERROR_WRITE: + return "lzma error: SZ_ERROR_WRITE"; + case SZ_ERROR_PROGRESS: + return "lzma error: SZ_ERROR_PROGRESS"; + case SZ_ERROR_FAIL: + return "lzma error: SZ_ERROR_FAIL"; + case SZ_ERROR_THREAD: + return "lzma error: SZ_ERROR_THREAD"; + case SZ_ERROR_ARCHIVE: + return "lzma error: SZ_ERROR_ARCHIVE"; + case SZ_ERROR_NO_ARCHIVE: + return "lzma error: SZ_ERROR_NO_ARCHIVE"; + default: + llvm_unreachable("unknown or unexpected lzma status code"); + } +} + +static void *XzAlloc(ISzAllocPtr, size_t size) { + return malloc(size); +} +static void XzFree(ISzAllocPtr, void *address) { + free(address); +} + +llvm::Error uncompress(llvm::ArrayRef InputBuffer, + llvm::SmallVectorImpl &Uncompressed) { + const uint8_t *src = InputBuffer.data(); + ISzAlloc alloc; + CXzUnpacker state; + alloc.Alloc = XzAlloc; + alloc.Free = XzFree; + XzUnpacker_Construct(&state, &alloc); + CrcGenerateTable(); + Crc64GenerateTable(); + size_t srcOff = 0; + size_t dstOff = 0; + size_t srcLen = InputBuffer.size(); + std::vector dst(srcLen, 0); + ECoderStatus status = CODER_STATUS_NOT_FINISHED; + while (status == CODER_STATUS_NOT_FINISHED) { + dst.resize(dst.size() * EXPAND_FACTOR); + size_t srcRemain = srcLen - srcOff; + size_t dstRemain = dst.size() - dstOff; + SRes res = XzUnpacker_Code(&state, + reinterpret_cast(&dst[dstOff]), &dstRemain, + reinterpret_cast(&src[srcOff]), &srcRemain, + true, CODER_FINISH_ANY, &status); + if (res != SZ_OK) { + XzUnpacker_Free(&state); + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "XzUnpacker_Code()=%s", convertLZMACodeToString(res)); + } + srcOff += srcRemain; + dstOff += dstRemain; + } + XzUnpacker_Free(&state); + if (!XzUnpacker_IsStreamWasFinished(&state)) { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "XzUnpacker_IsStreamWasFinished()=lzma error: return False"); + } + Uncompressed.resize(dstOff); + memcpy(Uncompressed.data(), dst.data(), dstOff); + return llvm::Error::success(); +} +#endif +// OHOS_LOCAL end #endif // LLDB_ENABLE_LZMA } // end of namespace lzma diff --git a/llvm-build/MakeLiblzma b/llvm-build/MakeLiblzma new file mode 100644 index 000000000000..b78b86f3496b --- /dev/null +++ b/llvm-build/MakeLiblzma @@ -0,0 +1,65 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +SYSROOT := +INSTALL_DIR := +TARGET_TRIPLE := +CC := +SRCS := 7zAlloc.c 7zArcIn.c 7zBuf2.c 7zBuf.c 7zCrc.c 7zCrcOpt.c 7zDec.c 7zFile.c 7zStream.c Aes.c AesOpt.c Alloc.c Bcj2.c Bra86.c Bra.c BraIA64.c CpuArch.c Delta.c LzFind.c Lzma2Dec.c Lzma2Enc.c Lzma86Dec.c Lzma86Enc.c LzmaDec.c LzmaEnc.c LzmaLib.c Ppmd7.c Ppmd7Dec.c Ppmd7Enc.c Sha256.c Sha256Opt.c Sort.c Xz.c XzCrc64.c XzCrc64Opt.c XzDec.c XzEnc.c XzIn.c +SRC_PREFIX := + +ifeq ($(TARGET_TRIPLE),linux-x86_64) +CFLAGS := --target=x86_64-unknown-linux-gnu -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -shared -fuse-ld=lld +TARGET := liblzma.so +else +ifeq ($(TARGET_TRIPLE),windows-x86_64) +CFLAGS := --target=x86_64-pc-windows-gnu --sysroot=$(SYSROOT) -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -shared -fuse-ld=lld --rtlib=compiler-rt -Wl,--out-implib=liblzma.dll.a +TARGET := liblzma.dll +TARGET_A := liblzma.dll.a +else +ifeq ($(findstring darwin,$(TARGET_TRIPLE)),darwin) +SDKROOT := $(shell xcrun --sdk macosx --show-sdk-path) +CFLAGS := -D_7ZIP_ST -Wall -Werror -Wno-empty-body -Wno-enum-conversion -Wno-logical-op-parentheses -Wno-self-assign -fPIC +LDFLAGS := -dynamiclib -fuse-ld=lld -Wl,-syslibroot,$(SDKROOT) -install_name @rpath/liblzma.dylib +TARGET := liblzma.dylib +else +$(warning *** warning: TARGET_TRIPLE $(TARGET_TRIPLE) has not been set in rights) +endif +endif +endif + +lzma_header_install: + @echo "begin header install" + mkdir -p $(INSTALL_DIR)/include + cp -rf $(SRC_PREFIX)* $(INSTALL_DIR)/include + +$(TARGET): lzma_header_install + $(CC) $(CFLAGS) $(LDFLAGS) -o $(TARGET) $(addprefix $(SRC_PREFIX), $(SRCS)) + +.PHONY: clean + +clean: + rm -f $(TARGET) $(SRC_PREFIX)*.o + +.PHONY:install + +install: $(TARGET) + @echo "begin install" + mkdir -p $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) + mv $(TARGET) $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) +ifeq ($(TARGET_TRIPLE),windows-x86_64) + mv $(TARGET_A) $(INSTALL_DIR)/lib/$(TARGET_TRIPLE) +endif + @echo "install success!" diff --git a/llvm-build/build.py b/llvm-build/build.py index 6782645fc3f1..07577568b845 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -65,6 +65,7 @@ class BuildConfig(): self.build_libxml2 = args.build_libxml2 self.lldb_timeout = args.lldb_timeout self.enable_monitoring = args.enable_monitoring + self.enable_lzma_7zip = args.enable_lzma_7zip self.build_libs = args.build_libs self.build_libs_flags = args.build_libs_flags self.compression_format = args.compression_format @@ -224,6 +225,12 @@ class BuildConfig(): default=False, help='Build libxml2 tool') + parser.add_argument( + '--enable-lzma-7zip', + action='store_true', + default=False, + help='Build 7zip tool and enable LZMA compression support in LLDB') + parser.add_argument( '--lldb-timeout', action='store_true', @@ -631,6 +638,9 @@ class LlvmCore(BuildUtils): llvm_defines['CURSES_LIBRARIES'] = ncurses_libs llvm_defines['PANEL_LIBRARIES'] = ncurses_libs + if self.build_config.enable_lzma_7zip: + llvm_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', self.use_platform(), 'liblzma.dylib') + if self.build_config.build_libedit: llvm_defines['LibEdit_LIBRARIES'] = os.path.join(self.get_prebuilts_dir('libedit'), 'lib', 'libedit.0.dylib') @@ -672,6 +682,9 @@ class LlvmCore(BuildUtils): llvm_defines['CURSES_LIBRARIES'] = ncurses_libs llvm_defines['PANEL_LIBRARIES'] = ncurses_libs + if self.build_config.enable_lzma_7zip: + llvm_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', 'linux-x86_64', 'liblzma.so') + if self.build_config.build_libedit: llvm_defines['LibEdit_LIBRARIES'] = os.path.join(self.get_prebuilts_dir('libedit'), 'lib', 'libedit.so.0.0.68') @@ -717,6 +730,12 @@ class LlvmCore(BuildUtils): if self.build_config.build_ncurses and self.get_ncurses_version() is not None: llvm_defines['LLDB_ENABLE_CURSES'] = 'ON' llvm_defines['CURSES_INCLUDE_DIRS'] = os.path.join(self.get_prebuilts_dir('ncurses'), 'include') + + if self.build_config.enable_lzma_7zip: + llvm_defines['LLDB_ENABLE_LZMA'] = 'ON' + llvm_defines['LLDB_ENABLE_LZMA_7ZIP'] = 'ON' + llvm_defines['LIBLZMA_INCLUDE_DIRS'] = self.merge_out_path('lzma', 'include') + if self.build_config.build_libedit: llvm_defines['LLDB_ENABLE_LIBEDIT'] = 'ON' llvm_defines['LibEdit_INCLUDE_DIRS'] = os.path.join(self.get_prebuilts_dir('libedit'), 'include') @@ -849,6 +868,12 @@ class LlvmCore(BuildUtils): if self.build_config.enable_monitoring: windows_defines['LLDB_ENABLE_PERFORMANCE'] = 'ON' + if self.build_config.enable_lzma_7zip: + windows_defines['LLDB_ENABLE_LZMA'] = 'ON' + windows_defines['LLDB_ENABLE_LZMA_7ZIP'] = 'ON' + windows_defines['LIBLZMA_INCLUDE_DIRS'] = self.merge_out_path('lzma', 'include') + windows_defines['LIBLZMA_LIBRARIES'] = self.merge_out_path('lzma', 'lib', 'windows-x86_64', 'liblzma.dll.a') + def llvm_compile_windows_flags(self, windows_defines, windowstool_path, @@ -1086,7 +1111,6 @@ class SysrootComposer(BuildUtils): gn_args += ' is_legacy=true musl_target_multilib=nanlegacy' multi_lib_dir = os.path.join(ohos_lib_dir, 'nanlegacy') sysroot_multi_lib_dir = os.path.join(sysroot_lib_dir, 'nanlegacy') - self.run_hb_build(product_name, target_cpu, target_name, gn_args) ld_musl_lib = os.path.join(sysroot_multi_lib_dir, 'ld-musl-{}.so.1'.format(ld_arch)) self.build_musl_libs(product_name, target_cpu, target_name, multi_lib_dir, sysroot_multi_lib_dir, ld_musl_lib, gn_args) @@ -1710,6 +1734,36 @@ class LlvmLibs(BuildUtils): self.llvm_package.copy_ncurses_to_llvm(llvm_make) self.llvm_package.copy_ncurses_to_llvm(llvm_install) + def build_lzma(self, llvm_install): + self.logger().info('Building lzma') + target_triple = self.use_platform() + liblzma_build_path = self.merge_out_path('lzma') + llvm_clang_prebuilts = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, + 'prebuilts', 'clang', 'ohos', self.use_platform(), + 'clang-%s' % self.build_config.CLANG_VERSION, 'bin', 'clang')) + src_dir = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, 'third_party', 'lzma', 'C')) + cmd = [ 'make', + 'install', + 'CC=%s' % llvm_clang_prebuilts, + 'SRC_PREFIX=%s/' % src_dir, + 'TARGET_TRIPLE=%s' % target_triple, + 'INSTALL_DIR=%s' % liblzma_build_path, + '-f', + 'MakeLiblzma'] + os.chdir(self.build_config.LLVM_BUILD_DIR) + self.check_call(cmd) + + self.logger().info('Copy lzma to llvm install dir is %s', llvm_install) + if self.host_is_darwin(): + shlib_ext = '.dylib' + if self.host_is_linux(): + shlib_ext = '.so' + lzma_file = os.path.join(liblzma_build_path, 'lib', target_triple, 'liblzma' + shlib_ext) + lib_dst_path = os.path.join(llvm_install, 'lib') + if not os.path.exists(lib_dst_path): + os.makedirs(lib_dst_path) + self.check_copy_file(lzma_file, lib_dst_path + '/liblzma' + shlib_ext) + def build_libedit(self, llvm_make, llvm_install): self.logger().info('Building libedit') @@ -2077,6 +2131,12 @@ class LlvmPackage(BuildUtils): if self.build_config.build_libxml2: windows_additional_bin_files += ['libxml2%s' % shlib_ext] + if self.build_config.enable_lzma_7zip: + windows_additional_bin_files += ['liblzma%s' % shlib_ext] + bin_root = os.path.join(install_dir, 'bin') + prebuild_dir = self.merge_out_path('lzma', 'lib', 'windows-x86_64', 'liblzma.dll') + shutil.copyfile(prebuild_dir, os.path.join(bin_root, 'liblzma.dll')) + new_necessary_bin_files = list(set(necessary_bin_files) - set(windows_forbidden_list_bin_files)) new_necessary_bin_files.extend(windows_additional_bin_files) del necessary_bin_files[:] @@ -2370,6 +2430,9 @@ def main(): if build_config.build_ncurses: llvm_libs.build_ncurses(llvm_make, llvm_install) + if build_config.enable_lzma_7zip: + llvm_libs.build_lzma(llvm_install) + if build_config.build_libedit: llvm_libs.build_libedit(llvm_make, llvm_install) @@ -2428,6 +2491,27 @@ def main(): windows_python_builder.build() windows_python_builder.prepare_for_package() llvm_core.set_mingw_python_dir(windows_python_builder.install_dir) + + if build_config.enable_lzma_7zip: + build_utils.logger().info('build windows lzma') + target_triple = 'windows-x86_64' + build_dir = build_utils.merge_out_path('lzma') + build_utils.check_create_dir(build_dir) + clang_path = build_utils.merge_out_path('llvm-install', 'bin', 'clang') + src_dir = os.path.abspath(os.path.join(build_config.REPOROOT_DIR, 'third_party', 'lzma', 'C')) + windows_sysroot = build_utils.merge_out_path('mingw', build_config.MINGW_TRIPLE) + cmd = [ 'make', + 'install', + 'CC=%s' % clang_path, + 'SRC_PREFIX=%s/' % src_dir, + 'SYSROOT=%s' % windows_sysroot, + 'TARGET_TRIPLE=%s' % target_triple, + 'INSTALL_DIR=%s' % build_dir, + '-f', + 'MakeLiblzma'] + os.chdir(build_config.LLVM_BUILD_DIR) + build_utils.check_call(cmd) + llvm_core.llvm_compile_for_windows(build_config.TARGETS, build_config.enable_assertions, build_config.build_name) -- Gitee From 3887fd81bf5dd7f31e5a4a3f4ef5218a4ae97a7e Mon Sep 17 00:00:00 2001 From: Roman Zhuykov Date: Fri, 1 Mar 2024 13:50:39 +0300 Subject: [PATCH 35/44] [ArkRuntime] AArch64: Adjust ArkFast calling conventions Take floating point temporary registers into account Signed-off-by: Roman Zhuykov --- llvm/lib/Target/AArch64/AArch64ArkGcCallingConvention.td | 2 +- llvm/lib/Target/AArch64/AArch64CallingConvention.td | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/AArch64/AArch64ArkGcCallingConvention.td b/llvm/lib/Target/AArch64/AArch64ArkGcCallingConvention.td index 5190ea36c3b6..5485ac4c90da 100644 --- a/llvm/lib/Target/AArch64/AArch64ArkGcCallingConvention.td +++ b/llvm/lib/Target/AArch64/AArch64ArkGcCallingConvention.td @@ -506,7 +506,7 @@ def CSR_AArch64_ArkInt : CalleeSavedRegs<(add FP)>; def CSR_AArch64_ArkFast5 : CalleeSavedRegs<(add (sub (sequence "X%u", 5, 27), X16, X17), LR, - (sequence "D%u", 0, 31))>; + (sequence "D%u", 0, 29))>; def CSR_AArch64_ArkFast4 : CalleeSavedRegs<(add CSR_AArch64_ArkFast5, X4)>; diff --git a/llvm/lib/Target/AArch64/AArch64CallingConvention.td b/llvm/lib/Target/AArch64/AArch64CallingConvention.td index dbc5b5eb2218..0094ee3e3129 100644 --- a/llvm/lib/Target/AArch64/AArch64CallingConvention.td +++ b/llvm/lib/Target/AArch64/AArch64CallingConvention.td @@ -505,7 +505,7 @@ def CSR_AArch64_ArkInt : CalleeSavedRegs<(add FP)>; def CSR_AArch64_ArkFast5 : CalleeSavedRegs<(add (sub (sequence "X%u", 5, 27), X16, X17), LR, - (sequence "D%u", 0, 31))>; + (sequence "D%u", 0, 29))>; def CSR_AArch64_ArkFast4 : CalleeSavedRegs<(add CSR_AArch64_ArkFast5, X4)>; -- Gitee From e76230ee0c648d85c68e9ed56b2ecadce6206e73 Mon Sep 17 00:00:00 2001 From: haotuo Date: Sat, 9 Mar 2024 20:36:24 +0800 Subject: [PATCH 36/44] Fix oat issue Signed-off-by: haotuo Change-Id: Id347cf0506d42f2926aa1e873d6085be35ad9610 --- OAT.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/OAT.xml b/OAT.xml index fe34bbf4d4a4..0407b602a749 100644 --- a/OAT.xml +++ b/OAT.xml @@ -47,7 +47,7 @@ - llvm/LICENSE.TXT|clang/LICENSE.TXT|libcxx/LICENSE.TXT|lldb/LICENSE.TXT|clang-tools-extra/LICENSE.TXT|llvm/utils/lit/LICENSE.TXT|llvm/tools/msbuild/license.txt|llvm/tools/msbuild/license.txt|clang-tools-extra/clang-tidy/cert/LICENSE.TXT|clang-tools-extra/clang-tidy/hicpp/LICENSE.TXT|libclc/LICENSE.TXT|llvm/include/llvm/Support/LICENSE.TXT + llvm/LICENSE.TXT @@ -205,6 +205,9 @@ + + + -- Gitee From 526b354f4256254b70343dfc32cc19173302b723 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Mon, 11 Mar 2024 15:11:35 +0300 Subject: [PATCH 37/44] [cmake][AArch64 Host] Install rpath directly https://gitee.com/openharmony/third_party_llvm-project/issues/I95LB6 Signed-off-by: Lyupa Anastasia --- llvm/cmake/modules/AddLLVM.cmake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 057431208322..9202c141af8b 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -2289,6 +2289,12 @@ function(llvm_setup_rpath name) set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ") endif() + # OHOS_LOCAL begin + if(${CMAKE_SYSTEM_NAME} MATCHES "OHOS") + # Target property INSTALL_RPATH is not supported on OHOS. + set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-rpath,\'${_install_rpath}\' ") + endif() + # OHOS_LOCAL end else() return() endif() -- Gitee From 4a95895666b068835bb48e1791f01118ef75edb7 Mon Sep 17 00:00:00 2001 From: Lyupa Anastasia Date: Tue, 12 Mar 2024 12:09:34 +0300 Subject: [PATCH 38/44] [Build] Fix linker and resource paths Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I97VC3 Signed-off-by: Lyupa Anastasia --- llvm-build/build.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 07577568b845..290acb10931e 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -791,8 +791,9 @@ class LlvmCore(BuildUtils): llvm_defines['LLVM_BUILD_INSTRUMENTED'] = 'ON' llvm_defines['LLVM_PROFDATA'] = llvm_profdata - resource_dir = "lib/clang/10.0.1/lib/linux/libclang_rt.profile-x86_64.a" - ldflags += ' %s' % os.path.join(llvm_clang_install, resource_dir) + resource_dir = os.path.join(llvm_clang_install, 'lib', 'clang', self.build_config.CLANG_VERSION, + 'lib', 'x86_64-unknown-linux-gnu', 'libclang_rt.profile.a') + ldflags += ' %s' % resource_dir cflags = '-fstack-protector-strong' if not self.host_is_darwin(): @@ -802,8 +803,7 @@ class LlvmCore(BuildUtils): self.llvm_compile_llvm_defines(llvm_defines, llvm_clang_install, cflags, ldflags) - linker_path = os.path.abspath(os.path.join(self.build_config.REPOROOT_DIR, 'prebuilts', 'clang', - 'ohos', 'linux-x86_64', 'llvm', 'bin', 'ld.lld')) + linker_path = os.path.join(llvm_clang_install, 'bin', 'ld.lld') llvm_defines['CMAKE_LINKER'] = linker_path self.build_llvm(targets=self.build_config.TARGETS, -- Gitee From a0c18fb2f6508c6d9372065854b72e6d45fe897a Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Tue, 12 Mar 2024 12:59:21 +0000 Subject: [PATCH 39/44] *[BUILD][LLDB] 'XzCrc64.h"' file not found Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I97YSA?from=project-issue Test: Toolchain compilation and packaging (python3 toolchain/llvm-project/llvm-build/build.py --enable-lzma-7zip) Signed-off-by: xwx1135370 --- lldb/source/Host/common/LZMA.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lldb/source/Host/common/LZMA.cpp b/lldb/source/Host/common/LZMA.cpp index c94c636b517e..34ce61c239a5 100644 --- a/lldb/source/Host/common/LZMA.cpp +++ b/lldb/source/Host/common/LZMA.cpp @@ -17,7 +17,7 @@ #else #include <7zCrc.h> #include -#include +#include #define EXPAND_FACTOR 2 #endif // OHOS_LOCAL end -- Gitee From f1bf3a3fc11f4d4bcbc4036b23c4fd9dec33c3be Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 12 Jan 2023 14:04:57 +0100 Subject: [PATCH 40/44] [lldb] Detach the child process when stepping over a fork Step over thread plans were claiming to explain the fork stop reasons, which prevented the default fork logic (detaching from the child process) from kicking in. This patch changes that. Differential Revision: https://reviews.llvm.org/D141605 Signed-off-by: liyiming13 --- lldb/source/Target/ThreadPlan.cpp | 3 ++ .../fork/resumes-child/Makefile | 3 ++ .../resumes-child/TestForkResumesChild.py | 22 ++++++++++++++ .../fork/resumes-child/main.cpp | 29 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 lldb/test/API/functionalities/fork/resumes-child/Makefile create mode 100644 lldb/test/API/functionalities/fork/resumes-child/TestForkResumesChild.py create mode 100644 lldb/test/API/functionalities/fork/resumes-child/main.cpp diff --git a/lldb/source/Target/ThreadPlan.cpp b/lldb/source/Target/ThreadPlan.cpp index 9913ecb591fa..7927fc314014 100644 --- a/lldb/source/Target/ThreadPlan.cpp +++ b/lldb/source/Target/ThreadPlan.cpp @@ -171,6 +171,9 @@ bool ThreadPlan::IsUsuallyUnexplainedStopReason(lldb::StopReason reason) { case eStopReasonExec: case eStopReasonThreadExiting: case eStopReasonInstrumentation: + case eStopReasonFork: + case eStopReasonVFork: + case eStopReasonVForkDone: return true; default: return false; diff --git a/lldb/test/API/functionalities/fork/resumes-child/Makefile b/lldb/test/API/functionalities/fork/resumes-child/Makefile new file mode 100644 index 000000000000..99998b20bcb0 --- /dev/null +++ b/lldb/test/API/functionalities/fork/resumes-child/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/test/API/functionalities/fork/resumes-child/TestForkResumesChild.py b/lldb/test/API/functionalities/fork/resumes-child/TestForkResumesChild.py new file mode 100644 index 000000000000..70df4f1c51a7 --- /dev/null +++ b/lldb/test/API/functionalities/fork/resumes-child/TestForkResumesChild.py @@ -0,0 +1,22 @@ +""" +Make sure that the fork child keeps running. +""" + + + +import lldb +import lldbsuite.test.lldbutil as lldbutil +from lldbsuite.test.lldbtest import * +from lldbsuite.test.decorators import * + + +class TestForkResumesChild(TestBase): + + NO_DEBUG_INFO_TESTCASE = True + + @skipIfWindows + def test_step_over_fork(self): + self.build() + lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp")) + self.runCmd("next") + self.expect("continue", substrs = ["exited with status = 0"]) diff --git a/lldb/test/API/functionalities/fork/resumes-child/main.cpp b/lldb/test/API/functionalities/fork/resumes-child/main.cpp new file mode 100644 index 000000000000..2d37f323bdf6 --- /dev/null +++ b/lldb/test/API/functionalities/fork/resumes-child/main.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +int main() { + pid_t fork_result = fork(); // break here + assert(fork_result >= 0); + if (fork_result == 0) { + // child + _exit(47); + } + // parent + // Use polling to avoid blocking if the child is not actually resumed. + auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10); + std::chrono::milliseconds poll_interval{10}; + while (std::chrono::steady_clock::now() < deadline) { + int status; + pid_t waitpid_result = waitpid(fork_result, &status, WNOHANG); + if (waitpid_result == fork_result) + return 0; + assert(waitpid_result == 0); + std::this_thread::sleep_for(poll_interval); + poll_interval *= 2; + } + abort(); +} -- Gitee From b1caa7e5b69e0e3fcdce1bf13c44ef21d563874f Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Thu, 14 Mar 2024 11:45:12 +0800 Subject: [PATCH 41/44] * [R&D][Doc]Updated the development guide MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I989NF?from=project-issue Signed-off-by: xwx1135370 --- OpenHarmonyDevelopmentGuidelines.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/OpenHarmonyDevelopmentGuidelines.md b/OpenHarmonyDevelopmentGuidelines.md index 29cfad2fa8be..13e41c5e6633 100644 --- a/OpenHarmonyDevelopmentGuidelines.md +++ b/OpenHarmonyDevelopmentGuidelines.md @@ -37,6 +37,10 @@ Some local OHOS change The presence of such marks greatly simplifies porting such code snippets to new LLVM versions. All such changes MUST be accompanied with a test case that MUST fail should the change is reverted. +If the change is from the upstream and is a mainline patch, the OHOS_LOCAL flag does not need to be added. Similar PRs, such as: +- https://gitee.com/openharmony/third_party_llvm-project/pulls/93 +- https://gitee.com/openharmony/third_party_llvm-project/pulls/409 + --------------------- ### Commit title and message -- Gitee From 904b2139262ec6dd4be42b27af194448c3fd26cc Mon Sep 17 00:00:00 2001 From: Vadim Mutilin Date: Thu, 7 Mar 2024 17:28:33 +0300 Subject: [PATCH 42/44] [build] Fix python version 2 -> 3 Issue: https://gitee.com/openharmony/third_party_llvm-project/issues/I96RV6 Change-Id: I6899b6266e4f6a0f7d8c356cb0f0969d724af4ef Signed-off-by: Vadim Mutilin --- llvm-build/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index 290acb10931e..d02b8ce2089c 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -2341,7 +2341,7 @@ class LlvmPackage(BuildUtils): repo_tool = os.path.join(self.build_config.REPOROOT_DIR, '.repo', 'repo', 'repo') if os.path.isfile(repo_tool): self.logger().info('Generating manifest.') - subprocess.run(['python2.7', repo_tool, 'manifest', '-r', '-o', manifest], shell=False, + subprocess.run(['python3', repo_tool, 'manifest', '-r', '-o', manifest], shell=False, stdout=subprocess.PIPE, cwd=self.build_config.REPOROOT_DIR) else: self.logger().error('Cannot generate manifest, repo tool not found.') -- Gitee From 45f88e5557f8acbb3685f70c8b39314eb9984de3 Mon Sep 17 00:00:00 2001 From: xwx1135370 Date: Tue, 19 Mar 2024 12:19:51 +0000 Subject: [PATCH 43/44] [R&D][LLDB]Undefined symbols exist in the llvm_make directory when the lldb is running Issue: https://gitee.com/openharmony/third_party_llvm-project/pulls/415/files Test: Test:Toolchain Compilation and LLDB Tool Running Signed-off-by: xwx1135370 --- llvm-build/build.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/llvm-build/build.py b/llvm-build/build.py index d02b8ce2089c..b665c2200fd2 100755 --- a/llvm-build/build.py +++ b/llvm-build/build.py @@ -1734,7 +1734,7 @@ class LlvmLibs(BuildUtils): self.llvm_package.copy_ncurses_to_llvm(llvm_make) self.llvm_package.copy_ncurses_to_llvm(llvm_install) - def build_lzma(self, llvm_install): + def build_lzma(self, llvm_make, llvm_install): self.logger().info('Building lzma') target_triple = self.use_platform() liblzma_build_path = self.merge_out_path('lzma') @@ -1753,15 +1753,20 @@ class LlvmLibs(BuildUtils): os.chdir(self.build_config.LLVM_BUILD_DIR) self.check_call(cmd) - self.logger().info('Copy lzma to llvm install dir is %s', llvm_install) if self.host_is_darwin(): shlib_ext = '.dylib' if self.host_is_linux(): shlib_ext = '.so' lzma_file = os.path.join(liblzma_build_path, 'lib', target_triple, 'liblzma' + shlib_ext) + + self.logger().info('Copy lzma to llvm make dir is %s', llvm_make) + lib_make_path = os.path.join(llvm_make, 'lib') + self.check_create_dir(lib_make_path) + self.check_copy_file(lzma_file, lib_make_path + '/liblzma' + shlib_ext) + + self.logger().info('Copy lzma to llvm install dir is %s', llvm_install) lib_dst_path = os.path.join(llvm_install, 'lib') - if not os.path.exists(lib_dst_path): - os.makedirs(lib_dst_path) + self.check_create_dir(lib_dst_path) self.check_copy_file(lzma_file, lib_dst_path + '/liblzma' + shlib_ext) def build_libedit(self, llvm_make, llvm_install): @@ -2431,7 +2436,7 @@ def main(): llvm_libs.build_ncurses(llvm_make, llvm_install) if build_config.enable_lzma_7zip: - llvm_libs.build_lzma(llvm_install) + llvm_libs.build_lzma(llvm_make, llvm_install) if build_config.build_libedit: llvm_libs.build_libedit(llvm_make, llvm_install) -- Gitee From 6c223ca2c81dd739a54b118c4db1669df41e0274 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Thu, 21 Mar 2024 08:11:30 +0000 Subject: [PATCH 44/44] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!3?= =?UTF-8?q?40=20:=20[R&D][OHOS][lldb]=20Support=20remote=20HDC=20server'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Plugins/Platform/OHOS/HdcClient.cpp | 520 +----------------- lldb/source/Plugins/Platform/OHOS/HdcClient.h | 34 +- .../Plugins/Platform/OHOS/PlatformOHOS.cpp | 31 +- .../Plugins/Platform/OHOS/PlatformOHOS.h | 3 - .../OHOS/PlatformOHOSRemoteGDBServer.cpp | 64 +-- .../OHOS/PlatformOHOSRemoteGDBServer.h | 3 - 6 files changed, 43 insertions(+), 612 deletions(-) diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp index 679b821f2d08..eef720eaf78b 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.cpp @@ -8,17 +8,14 @@ #include "HdcClient.h" -#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" #include "lldb/Host/ConnectionFileDescriptor.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/PosixApi.h" -#include "lldb/Utility/LLDBLog.h" #include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/DataEncoder.h" @@ -37,13 +34,9 @@ #include #include -#include #include #include -#include -#include #include -#include // On Windows, transitive dependencies pull in , which defines a // macro that clashes with a method name. @@ -128,13 +121,11 @@ Status HdcClient::CreateByDeviceID(const std::string &device_id, return error; } -HdcClient::HdcClient(const std::string &connect_addr, - const std::string &device_id) - : m_connect_addr(connect_addr), m_device_id(device_id) {} +HdcClient::HdcClient() {} -HdcClient::HdcClient(HdcClient &&) = default; +HdcClient::HdcClient(const std::string &device_id) : m_device_id(device_id) {} -HdcClient::~HdcClient() = default; +HdcClient::~HdcClient() {} void HdcClient::SetDeviceID(const std::string &device_id) { m_device_id = device_id; @@ -142,10 +133,6 @@ void HdcClient::SetDeviceID(const std::string &device_id) { const std::string &HdcClient::GetDeviceID() const { return m_device_id; } -bool HdcClient::IsServerLocal() { - return m_connect_addr == "localhost"; -} - namespace { typedef unsigned msg_len_t; struct ChannelHandShake { @@ -176,9 +163,8 @@ Status HdcClient::Connect() { return Status("invalid port specification: %s. $OHOS_HDC_SERVER_PORT must be a positive number in (0,65535]", env_port); } } - - // Support remote HDC server by providing connection address explicitly - std::string uri = "connect://" + m_connect_addr + ":" + port; + + std::string uri = "connect://localhost:" + port; m_conn->Connect(uri.c_str(), &error); ConnectionStatus status = eConnectionStatusError; if (error.Success()) { @@ -279,8 +265,8 @@ Status HdcClient::DeletePortForwarding(const uint16_t local_port, return ReadResponseStatus("Remove forward ruler success"); } -Status HdcClient::LocalTransferFile(const char *direction, const FileSpec &src, - const FileSpec &dst) { +Status HdcClient::TransferFile(const char *direction, const FileSpec &src, + const FileSpec &dst) { LLDB_MODULE_TIMER(LLDBPerformanceTagName::TAG_HDC); // OHOS_LOCAL llvm::SmallVector cwd; std::error_code ec = llvm::sys::fs::current_path(cwd); @@ -298,459 +284,12 @@ Status HdcClient::LocalTransferFile(const char *direction, const FileSpec &src, return ReadResponseStatus("FileTransfer finish"); } -Status HdcClient::ExpectCommandMessagePrefix(uint16_t expected_command, - std::vector &message, - size_t prefix_size) { - uint16_t command; - Status error = ReadCommandMessagePrefix(command, message, prefix_size); - if (error.Fail()) - return error; - if (command != expected_command) - return Status("Unexpected HDC server command: %d, expected %d", command, - expected_command); - - return error; -} - -Status HdcClient::ExpectCommandMessage(uint16_t expected_command, - std::vector &message) { - return ExpectCommandMessagePrefix(expected_command, message, - std::numeric_limits::max()); -} - -template struct HdcIO; - -struct HdcTagIO { - enum class WireType : uint32_t { VARINT = 0, LENGTH_DELIMETED = 2 }; - - template - static llvm::Optional> ParseTag(InItT &InBegin, - InItT InEnd) { - llvm::Optional Tag = HdcIO::Parse(InBegin, InEnd); - return Tag.map([](U Val) { - return std::make_pair(static_cast(Val >> 3), - static_cast(Val & 0x7)); - }); - } - - template - static void SerializeTag(size_t Idx, WireType Type, OutItT &OutIt) { - HdcIO::Serialize( - static_cast(Type) | (static_cast(Idx) << 3), OutIt); - } -}; - -template -struct HdcIOBase { - using ValueT = T; - - static HdcTagIO::WireType GetWireType() { return TheType; } - - template - static llvm::Optional ParseTagged(size_t Idx, InItT &InBegin, - InItT InEnd) { - if (!HdcTagIO::ParseTag(InBegin, InEnd) - .map([Idx](auto P) { - return P.first == Idx && P.second == TheType; - }) - .value_or(false)) - return {}; - return DerivedT::Parse(InBegin, InEnd); - } - - template - static void SerializeTagged(size_t Idx, T Value, OutItT &OutIt) { - HdcTagIO::SerializeTag(Idx, TheType, OutIt); - DerivedT::Serialize(Value, OutIt); - } -}; - -template -struct HdcIO : HdcIOBase> { - static_assert(std::is_integral::value, "Don't know how to parse T"); - - template - static llvm::Optional Parse(InItT &InBegin, InItT InEnd) { - constexpr size_t NBytes = (sizeof(T) * 8 + 6) / 7; - T value = 0; - for (size_t c = 0; c < NBytes; ++c) { - uint8_t x; - if (InBegin == InEnd) - return {}; - x = *InBegin++; - value |= static_cast(x & 0x7Fu) << 7 * c; - if (!(x & 0x80)) - return value; - } - return {}; - } - - template static void Serialize(T value, OutItT &OutIt) { - constexpr size_t NBytes = (sizeof(T) * 8 + 6) / 7; - uint8_t b[NBytes]; - for (size_t i = 0; i < NBytes; ++i) { - b[i] = value & 0b0111'1111; - value >>= 7; - if (value) { - b[i] |= 0b1000'0000; - } else { - OutIt = std::copy_n(std::begin(b), i + 1, OutIt); - return; - } - } - } -}; - -template <> -struct HdcIO : HdcIOBase> { - template - static llvm::Optional Parse(InItT &InBegin, InItT InEnd) { - auto MaybeLen = HdcIO::Parse(InBegin, InEnd); - if (!MaybeLen) - return llvm::None; - size_t Len = *MaybeLen; - std::string Res; - Res.reserve(Len); - while (Res.size() < Len && InBegin != InEnd) - Res.push_back(*InBegin++); - if (Res.size() < Len) - return {}; - return Res; - } - - template - static void Serialize(const std::string &value, OutItT &OutIt) { - HdcIO::Serialize(value.size(), OutIt); - OutIt = std::copy_n(value.begin(), value.size(), OutIt); - } -}; - -template struct HdcTaggedIOHelper { - template - static llvm::Optional> Parse(InItT &InBegin, InItT InEnd) { - return std::tuple<>{}; - } - - template static void Serialize(OutItT &OutIt) {} -}; - -template -struct HdcTaggedIOHelper { - using ResT = llvm::Optional>; - - template static ResT Parse(InItT &InBegin, InItT InEnd) { - return HdcIO::ParseTagged(StartIdx, InBegin, InEnd) - .map([&](auto &&LHS) { - return HdcTaggedIOHelper::Parse(InBegin, InEnd) - .map([LHS = std::move(LHS)](auto &&RHS) { - return std::tuple_cat(std::make_tuple(std::move(LHS)), - std::move(RHS)); - }); - }) - .value_or(ResT(llvm::None)); - } - - template static ResT ParseOnce(InItT InBegin, InItT InEnd) { - return Parse(InBegin, InEnd); - } - - template - static void Serialize(const T &Arg, const Ts &...Args, OutItT &OutIt) { - HdcIO::SerializeTagged(StartIdx, std::move(Arg), OutIt); - HdcTaggedIOHelper::Serialize(std::move(Args)..., - OutIt); - } - - template - static void SerializeOnce(const T &Arg, const Ts &...Args, OutItT OutIt) { - return Serialize(Arg, Args..., OutIt); - } -}; - -template struct HdcTaggedIO : HdcTaggedIOHelper<1, Ts...> {}; - -template struct HdcTaggedIOTuple; - -template -struct HdcTaggedIOTuple> : HdcTaggedIO {}; - -using HdcTransferConfig = std::tuple; - -using HdcTransferPayload = std::tuple; - -using HdcFileMode = std::tuple; // fullName - -enum HdcCommand : uint16_t { - CMD_KERNEL_WAKEUP_SLAVETASK = 12, - CMD_FILE_INIT = 3000, - CMD_FILE_CHECK, - CMD_FILE_BEGIN, - CMD_FILE_DATA, - CMD_FILE_FINISH, - CMD_FILE_MODE = 3006 -}; - -static constexpr size_t HdcTransferPayloadPrefixReserve = 64; -static constexpr size_t HdcTransferPayloadMaxChunkSize = 49152; - -Status HdcClient::FileCheck(int FD, size_t &file_size) { - std::vector msg; - Status error = ExpectCommandMessage(HdcCommand::CMD_FILE_MODE, msg); - if (error.Fail()) - return error; - - auto maybe_file_mode = HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); - if (!maybe_file_mode) - return Status("Could not parse HDC server FileMode"); - - auto &file_mode = *maybe_file_mode; - - uint32_t perms = static_cast(std::get<0>(file_mode)); - auto EC = llvm::sys::fs::setPermissions( - FD, static_cast(perms) & llvm::sys::fs::all_perms); - if (EC) - return Status(EC); - - error = SendCommandMessage(HdcCommand::CMD_FILE_MODE, {}); - if (error.Fail()) - return error; - - error = ExpectCommandMessage(HdcCommand::CMD_FILE_CHECK, msg); - if (error.Fail()) - return error; - - auto transfer_config = - HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); - if (!transfer_config.has_value()) - return Status("Could not parse HDC server TransferConfig"); - - if (auto compress_type = std::get<7>(*transfer_config)) - return Status("Compression is not supported"); - - file_size = std::get<0>(*transfer_config); - - return SendCommandMessage(HdcCommand::CMD_FILE_BEGIN, {}); -} - -Status HdcClient::PullFileChunk(std::vector &buffer) { - buffer.clear(); - - std::vector msg; - Status error = ExpectCommandMessagePrefix(HdcCommand::CMD_FILE_DATA, msg, - HdcTransferPayloadPrefixReserve); - if (error.Fail()) - return error; - - auto transfer_payload = - HdcTaggedIOTuple::ParseOnce(msg.cbegin(), msg.cend()); - if (!transfer_payload.has_value()) - return Status("Could not parse HDC server TransferPayload"); - - if (auto compress_type = std::get<1>(*transfer_payload)) - return Status("Compression is not supported"); - - uint32_t read_bytes = std::get<3>(*transfer_payload); - buffer.resize(read_bytes, 0); - error = ReadAllBytes(buffer.data(), buffer.size()); - if (error.Fail()) - buffer.clear(); - - return error; -} - Status HdcClient::RecvFile(const FileSpec &src, const FileSpec &dst) { - if (IsServerLocal()) - return LocalTransferFile("recv", src, dst); - - const auto local_file_path = dst.GetPath(); - llvm::FileRemover local_file_remover(local_file_path); - - int dst_file_fd; - auto EC = llvm::sys::fs::openFileForWrite(local_file_path, dst_file_fd); - if (EC) - return Status("Unable to open local file %s", local_file_path.c_str()); - - std::stringstream cmd; - cmd << "file recv remote -m"; - cmd << " " << src.GetPath() << " " << dst.GetPath(); - Status error = SendMessage(cmd.str()); - if (error.Fail()) - return error; - - size_t cur_size = 0, all_size = 0; - error = FileCheck(dst_file_fd, all_size); - if (error.Fail()) - return error; - - llvm::raw_fd_ostream dst_file(dst_file_fd, true); - - std::vector buf; - while (cur_size < all_size) { - error = PullFileChunk(buf); - if (error.Fail()) - return error; - dst_file.write(buf.data(), buf.size()); - cur_size += buf.size(); - } - - error = SendCommandMessage(HdcCommand::CMD_FILE_FINISH, {}); - if (error.Fail()) - return error; - error = ReadResponseStatus("FileTransfer finish"); - if (error.Fail()) - return error; - - dst_file.close(); - if (dst_file.has_error()) - return Status("Failed to write file %s", local_file_path.c_str()); - - local_file_remover.releaseFile(); - return error; -} - -Status HdcClient::FileInit(size_t file_size, uint32_t perm, uint32_t u_id, - uint32_t g_id, const std::string &remote_path) { - std::vector msg; - Status error = ExpectCommandMessage(HdcCommand::CMD_FILE_INIT, msg); - if (error.Fail()) - return error; - - error = SendCommandMessage(HdcCommand::CMD_KERNEL_WAKEUP_SLAVETASK, {}); - if (error.Fail()) - return error; - - constexpr uint64_t IFREG_MASK = 0100000; - msg.clear(); - HdcTaggedIOTuple::SerializeOnce(perm | IFREG_MASK, // perm - u_id, // u_id - g_id, // g_id - "", // context - "", // fullName - std::back_inserter(msg)); - error = SendCommandMessage(HdcCommand::CMD_FILE_MODE, msg); - if (error.Fail()) - return error; - - error = ExpectCommandMessage(HdcCommand::CMD_FILE_MODE, msg); - if (error.Fail()) - return error; - - msg.clear(); - HdcTaggedIOTuple::SerializeOnce(file_size, // fileSize - 0, // atime - 0, // mtime - "", // options - remote_path, // path - "", // optionalName - false, // updateIfNew - 0, // compressType - false, // holdTimestamp - "", // funcName - "", // clientCwd - "", // reserve1 - "", // reserve2 - std::back_inserter(msg)); - error = SendCommandMessage(HdcCommand::CMD_FILE_CHECK, msg); - if (error.Fail()) - return error; - - return ExpectCommandMessage(HdcCommand::CMD_FILE_BEGIN, msg); -} - -Status HdcClient::PushFileChunk(std::vector &buffer, size_t chunk_size, - size_t index) { - std::fill_n(buffer.begin(), HdcTransferPayloadPrefixReserve, 0); - HdcTaggedIOTuple::SerializeOnce( - index, // index - 0, // compressType - chunk_size, // compressSize - chunk_size, // uncompressSize - buffer.begin()); - return SendCommandMessage(CMD_FILE_DATA, buffer); + return TransferFile("recv", src, dst); } Status HdcClient::SendFile(const FileSpec &src, const FileSpec &dst) { - if (IsServerLocal()) - return LocalTransferFile("send", src, dst); - - const auto local_file_path = src.GetPath(); - std::ifstream src_file(local_file_path.c_str(), std::ios::in | std::ios::binary); - if (!src_file.is_open()) - return Status("Unable to open local file %s", local_file_path.c_str()); - - std::stringstream cmd; - cmd << "file send remote -m " << src.GetPath() << " " << dst.GetPath(); - Status error = SendMessage(cmd.str()); - if (error.Fail()) - return error; - - llvm::sys::fs::file_status status; - auto EC = llvm::sys::fs::status(local_file_path, status); - if (EC) - return Status(EC); - - error = FileInit(status.getSize(), status.permissions(), status.getUser(), - status.getGroup(), dst.GetPath()); - if (error.Fail()) - return error; - - std::vector buffer; - size_t sent_bytes = 0; - while (!src_file.eof()) { - buffer.resize(HdcTransferPayloadPrefixReserve + - HdcTransferPayloadMaxChunkSize); - if (src_file - .read(buffer.data() + HdcTransferPayloadPrefixReserve, - HdcTransferPayloadMaxChunkSize) - .bad()) - break; - size_t chunk_size = src_file.gcount(); - buffer.resize(HdcTransferPayloadPrefixReserve + chunk_size); - error = PushFileChunk(buffer, chunk_size, sent_bytes); - if (error.Fail()) - return error; - sent_bytes += chunk_size; - } - - if (src_file.bad()) - return Status("Failed read on %s", local_file_path.c_str()); - if (sent_bytes < status.getSize()) { - m_conn.reset(); - return Status("Failed to read all of the bytes from %s: read %zu/%zu", - local_file_path.c_str(), sent_bytes, status.getSize()); - } - - error = ExpectCommandMessage(HdcCommand::CMD_FILE_FINISH, buffer); - if (error.Fail()) - return error; - - error = SendCommandMessage(HdcCommand::CMD_FILE_FINISH, {}); - if (error.Fail()) - return error; - - return ReadResponseStatus("FileTransfer finish"); + return TransferFile("send", src, dst); } Status HdcClient::SendMessage(llvm::StringRef packet, const bool reconnect) { @@ -775,13 +314,6 @@ Status HdcClient::SendMessage(llvm::StringRef packet, const bool reconnect) { return error; } -Status HdcClient::SendCommandMessage(uint16_t command, llvm::ArrayRef packet) { - llvm::SmallVector buf(sizeof(command) + packet.size()); - std::copy_n(reinterpret_cast(&command), sizeof(command), buf.begin()); - std::copy_n(packet.begin(), packet.size(), buf.begin() + sizeof(command)); - return SendMessage(llvm::StringRef(buf.data(), buf.size()), false); -} - Status HdcClient::ReadMessage(std::vector &message) { message.clear(); @@ -799,40 +331,6 @@ Status HdcClient::ReadMessage(std::vector &message) { return error; } -Status HdcClient::ReadCommandMessagePrefix(uint16_t &command, std::vector &message, size_t prefix_size) { - message.clear(); - - msg_len_t packet_len; - auto error = ReadAllBytes(&packet_len, sizeof(packet_len)); - if (error.Fail()) - return error; - - packet_len = htonl(packet_len); - if (packet_len < sizeof(command)) - return Status("Message too small to contain a command"); - - error = ReadAllBytes(&command, sizeof(command)); - if (error.Fail()) { - command = 0; - return error; - } - - message.resize(std::min(packet_len - sizeof(command), prefix_size), 0); - error = ReadAllBytes(&message[0], message.size()); - if (error.Fail()) { - command = 0; - message.clear(); - } - - return error; -} - -Status HdcClient::ReadCommandMessage(uint16_t &command, - std::vector &message) { - return ReadCommandMessagePrefix(command, message, - std::numeric_limits::max()); -} - Status HdcClient::ReadMessageStream(std::vector &message, milliseconds timeout) { auto start = steady_clock::now(); diff --git a/lldb/source/Plugins/Platform/OHOS/HdcClient.h b/lldb/source/Plugins/Platform/OHOS/HdcClient.h index 5e17b0d8f8ef..39b6569b447a 100644 --- a/lldb/source/Plugins/Platform/OHOS/HdcClient.h +++ b/lldb/source/Plugins/Platform/OHOS/HdcClient.h @@ -34,10 +34,8 @@ public: static Status CreateByDeviceID(const std::string &device_id, HdcClient &hdc); - explicit HdcClient(const std::string &connect_addr, - const std::string &device_id = ""); - - HdcClient(HdcClient &&); + HdcClient(); + explicit HdcClient(const std::string &device_id); ~HdcClient(); @@ -65,37 +63,18 @@ public: std::string *output); private: - bool IsServerLocal(); Status Connect(); - Status LocalTransferFile(const char *direction, const FileSpec &src, - const FileSpec &dst); - - Status FileCheck(int FD, size_t &file_size); - Status PullFileChunk(std::vector &buffer); - - Status FileInit(size_t file_size, uint32_t perm, uint32_t u_id, uint32_t g_id, - const std::string &remote_path); - Status PushFileChunk(std::vector &buffer, size_t chunk_size, - size_t index); + Status TransferFile(const char *direction, const FileSpec &src, + const FileSpec &dst); void SetDeviceID(const std::string &device_id); Status SendMessage(llvm::StringRef packet, const bool reconnect = true); - Status ReadMessage(std::vector &message); + Status SendDeviceMessage(const std::string &packet); - Status SendCommandMessage(uint16_t command, llvm::ArrayRef packet); - - Status ReadCommandMessagePrefix(uint16_t &command, std::vector &message, - size_t prefix_size); - Status ReadCommandMessage(uint16_t &command, std::vector &message); - - Status ExpectCommandMessage(uint16_t expected_command, - std::vector &message); - Status ExpectCommandMessagePrefix(uint16_t expected_command, - std::vector &message, - size_t prefix_size); + Status ReadMessage(std::vector &message); Status ReadMessageStream(std::vector &message, std::chrono::milliseconds timeout); @@ -104,7 +83,6 @@ private: Status ReadAllBytes(void *buffer, size_t size); - std::string m_connect_addr; std::string m_device_id; std::unique_ptr m_conn; }; diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp index 6a1bb9044646..5193e17edbd1 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.cpp @@ -129,13 +129,8 @@ llvm::StringRef PlatformOHOS::GetPluginName() { return GetPluginNameStatic(IsHost()); } -HdcClient PlatformOHOS::CreateHdcClient() { - return HdcClient(m_connect_addr, m_device_id); -} - Status PlatformOHOS::ConnectRemote(Args &args) { m_device_id.clear(); - m_connect_addr = "localhost"; if (IsHost()) { return Status("can't connect to the host platform '%s', always connected", @@ -151,26 +146,17 @@ Status PlatformOHOS::ConnectRemote(Args &args) { llvm::Optional uri = URI::Parse(url); if (!uri) return Status("Invalid URL: %s", url); - - Log *log = GetLog(LLDBLog::Platform); - if (PlatformOHOSRemoteGDBServer::IsHostnameDeviceID( - uri->hostname)) { // accepts no (empty) hostname too - m_device_id = uri->hostname.str(); - LLDB_LOG(log, "Treating hostname as device id: \"{0}\"", m_device_id); - } else { - m_connect_addr = uri->hostname.str(); - LLDB_LOG(log, "Treating hostname as remote HDC server address: \"{0}\"", - m_connect_addr); - } + if (uri->hostname != "localhost") + m_device_id = static_cast(uri->hostname); auto error = PlatformLinux::ConnectRemote(args); if (error.Success()) { - HdcClient hdc(m_connect_addr); - error = HdcClient::CreateByDeviceID(m_device_id, hdc); + HdcClient adb; + error = HdcClient::CreateByDeviceID(m_device_id, adb); if (error.Fail()) return error; - m_device_id = hdc.GetDeviceID(); + m_device_id = adb.GetDeviceID(); } return error; } @@ -185,7 +171,7 @@ Status PlatformOHOS::GetFile(const FileSpec &source, source_spec = GetRemoteWorkingDirectory().CopyByAppendingPathComponent( source_spec.GetCString(false)); - HdcClient hdc = CreateHdcClient(); + HdcClient hdc(m_device_id); Status error = hdc.RecvFile(source_spec, destination); return error; } @@ -202,7 +188,7 @@ Status PlatformOHOS::PutFile(const FileSpec &source, destination_spec.GetCString(false)); // TODO: Set correct uid and gid on remote file. - HdcClient hdc = CreateHdcClient(); + HdcClient hdc(m_device_id); Status error = hdc.SendFile(source, destination_spec); return error; } @@ -223,7 +209,6 @@ Status PlatformOHOS::DisconnectRemote() { Status error = PlatformLinux::DisconnectRemote(); if (error.Success()) { m_device_id.clear(); - m_connect_addr.clear(); m_sdk_version = 0; m_remote_platform_sp.reset(); } @@ -242,7 +227,7 @@ uint32_t PlatformOHOS::GetSdkVersion() { return m_sdk_version; std::string version_string; - HdcClient hdc = CreateHdcClient(); + HdcClient hdc(m_device_id); Status error = hdc.Shell("param get const.ohos.apiversion", seconds(5), &version_string); version_string = llvm::StringRef(version_string).trim().str(); diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h index fdbd1d42535a..83645ff209a5 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOS.h @@ -66,10 +66,7 @@ protected: llvm::StringRef GetLibdlFunctionDeclarations(lldb_private::Process *process) override; - HdcClient CreateHdcClient(); - private: - std::string m_connect_addr; std::string m_device_id; uint32_t m_sdk_version; diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp index 0a6a7b208f40..28f996a6cdb7 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.cpp @@ -26,13 +26,13 @@ static const lldb::pid_t g_remote_platform_pid = static uint16_t g_hdc_forward_port_offset = 0; static Status ForwardPortWithHdc( - const std::string &connect_addr, const uint16_t local_port, - const uint16_t remote_port, llvm::StringRef remote_socket_name, + const uint16_t local_port, const uint16_t remote_port, + llvm::StringRef remote_socket_name, const llvm::Optional &socket_namespace, std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); - HdcClient hdc(connect_addr); + HdcClient hdc; auto error = HdcClient::CreateByDeviceID(device_id, hdc); if (error.Fail()) return error; @@ -56,22 +56,19 @@ static Status ForwardPortWithHdc( *socket_namespace); } -static Status DeleteForwardPortWithHdc(const std::string &connect_addr, - std::pair ports, +static Status DeleteForwardPortWithHdc(std::pair ports, const std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); LLDB_LOGF(log, "Delete port forwarding %d -> %d, device=%s", ports.first, ports.second, device_id.c_str()); - HdcClient hdc(connect_addr, device_id); + HdcClient hdc(device_id); return hdc.DeletePortForwarding(ports); } -static Status DeleteForwardPortWithHdc( - const std::string &connect_addr, - std::pair remote_socket, - const llvm::Optional &socket_namespace, - const std::string &device_id) { +static Status DeleteForwardPortWithHdc(std::pair remote_socket, + const llvm::Optional &socket_namespace, + const std::string &device_id) { Log *log = GetLog(LLDBLog::Platform); uint16_t local_port = remote_socket.first; @@ -81,7 +78,7 @@ static Status DeleteForwardPortWithHdc( if (!socket_namespace) return Status("Invalid socket namespace"); - HdcClient hdc(connect_addr, device_id); + HdcClient hdc(device_id); return hdc.DeletePortForwarding(local_port, remote_socket_name, *socket_namespace); } @@ -105,14 +102,14 @@ static Status FindUnusedPort(uint16_t &port) { return error; } -PlatformOHOSRemoteGDBServer::PlatformOHOSRemoteGDBServer() = default; +PlatformOHOSRemoteGDBServer::PlatformOHOSRemoteGDBServer() {} PlatformOHOSRemoteGDBServer::~PlatformOHOSRemoteGDBServer() { for (const auto &it : m_port_forwards) { - DeleteForwardPortWithHdc(m_connect_addr, it.second, m_device_id); + DeleteForwardPortWithHdc(it.second, m_device_id); } for (const auto &it_socket : m_remote_socket_name) { - DeleteForwardPortWithHdc(m_connect_addr, it_socket.second, m_socket_namespace, m_device_id); + DeleteForwardPortWithHdc(it_socket.second, m_socket_namespace, m_device_id); } } @@ -138,14 +135,8 @@ bool PlatformOHOSRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) { return m_gdb_client_up->KillSpawnedProcess(pid); } -bool PlatformOHOSRemoteGDBServer::IsHostnameDeviceID(llvm::StringRef hostname) { - return hostname != "localhost" && !hostname.contains(':') && - !hostname.contains('.'); -} - Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { m_device_id.clear(); - m_connect_addr = "localhost"; if (args.GetArgumentCount() != 1) return Status( @@ -158,16 +149,8 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { uri = URI::Parse(url); if (!uri) return Status("Invalid URL: %s", url); - - Log *log = GetLog(LLDBLog::Platform); - if (IsHostnameDeviceID(uri->hostname)) { // accepts no (empty) hostname too - m_device_id = uri->hostname.str(); - LLDB_LOG(log, "Treating hostname as device id: \"{0}\"", m_device_id); - } else { - m_connect_addr = uri->hostname.str(); - LLDB_LOG(log, "Treating hostname as remote HDC server address: \"{0}\"", - m_connect_addr); - } + if (uri->hostname != "localhost") + m_device_id = static_cast(uri->hostname); m_socket_namespace.reset(); if (uri->scheme == "unix-connect") @@ -185,6 +168,7 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { args.ReplaceArgumentAtIndex(0, connect_url); + Log *log = GetLog(LLDBLog::Platform); LLDB_LOGF(log, "Rewritten platform connect URL: %s", connect_url.c_str()); error = PlatformRemoteGDBServer::ConnectRemote(args); if (error.Fail()) @@ -196,7 +180,6 @@ Status PlatformOHOSRemoteGDBServer::ConnectRemote(Args &args) { Status PlatformOHOSRemoteGDBServer::DisconnectRemote() { DeleteForwardPort(g_remote_platform_pid); g_hdc_forward_port_offset = 0; - m_connect_addr.clear(); return PlatformRemoteGDBServer::DisconnectRemote(); } @@ -206,8 +189,7 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { auto it = m_port_forwards.find(pid); auto it_socket = m_remote_socket_name.find(pid); if (it != m_port_forwards.end() && it->second.second != 0) { - const auto error = - DeleteForwardPortWithHdc(m_connect_addr, it->second, m_device_id); + const auto error = DeleteForwardPortWithHdc(it->second, m_device_id); if (error.Fail()) { LLDB_LOGF(log, "Failed to delete port forwarding (pid=%" PRIu64 ", fwd=(%d -> %d), device=%s): %s", @@ -218,8 +200,7 @@ void PlatformOHOSRemoteGDBServer::DeleteForwardPort(lldb::pid_t pid) { } if(it_socket != m_remote_socket_name.end()) { - const auto error_Socket = DeleteForwardPortWithHdc( - m_connect_addr, it_socket->second, m_socket_namespace, m_device_id); + const auto error_Socket = DeleteForwardPortWithHdc(it_socket->second, m_socket_namespace, m_device_id); if (error_Socket.Fail()) { LLDB_LOGF(log, "Failed to delete port forwarding (pid=%" PRIu64 ", fwd=(%d->%s)device=%s): %s", pid, it_socket->second.first, it_socket->second.second.c_str(), m_device_id.c_str(),error_Socket.AsCString()); @@ -245,9 +226,8 @@ Status PlatformOHOSRemoteGDBServer::MakeConnectURL( if (error.Fail()) return error; - error = - ForwardPortWithHdc(m_connect_addr, local_port, remote_port, - remote_socket_name, m_socket_namespace, m_device_id); + error = ForwardPortWithHdc(local_port, remote_port, remote_socket_name, + m_socket_namespace, m_device_id); if (error.Success()) { if (remote_port != 0){ m_port_forwards[pid] = {local_port, remote_port}; @@ -255,10 +235,8 @@ Status PlatformOHOSRemoteGDBServer::MakeConnectURL( else{ m_remote_socket_name[pid] ={local_port, remote_socket_name.str()}; } - // Connect to local_port on a potentially remote machine with running HDC - // server std::ostringstream url_str; - url_str << "connect://" << m_connect_addr << ":" << local_port; + url_str << "connect://localhost:" << local_port; connect_url = url_str.str(); break; } @@ -284,8 +262,6 @@ lldb::ProcessSP PlatformOHOSRemoteGDBServer::ConnectProcess( return nullptr; } - // If m_connect_addr is remote, this connects to a remote HDC server, assuming - // that all of the needed ports are open std::string new_connect_url; error = MakeConnectURL(s_remote_gdbserver_fake_pid--, (*uri->port) ? (*uri->port) : 0, uri->path, diff --git a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h index cb130574d3f6..680ddc1e733c 100644 --- a/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/OHOS/PlatformOHOSRemoteGDBServer.h @@ -38,10 +38,7 @@ public: lldb_private::Target *target, lldb_private::Status &error) override; - static bool IsHostnameDeviceID(llvm::StringRef hostname); - protected: - std::string m_connect_addr; std::string m_device_id; std::map> m_port_forwards; std::map> m_remote_socket_name; -- Gitee