leonardchan created this revision. leonardchan added reviewers: mcgrathr, eugenis, phosek, vitalybuka. leonardchan added a project: clang. Herald added subscribers: abrachet, kristof.beyls. Herald added a project: All. leonardchan requested review of this revision.
Full context in https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=107017. Instrumenting hwasan with globals results in a linker error under the relative vtables abi: ld.lld: error: libunwind.cpp:(.rodata..L_ZTVN9libunwind12UnwindCursorINS_17LocalAddressSpaceENS_15Registers_arm64EEE.hwasan+0x8): relocation R_AARCH64_PLT32 out of range: 6845471433603167792 is not in [-2147483648, 2147483647]; references libunwind::AbstractUnwindCursor::~AbstractUnwindCursor() >>> defined in libunwind/src/CMakeFiles/unwind_shared.dir/libunwind.cpp.obj This is because the tag is included in the vtable address when calculating the offset between the vtable and virtual function. A temporary solution until we can resolve this is to just disable hwasan instrumentation on relative vtables specifically, which can be done in the frontend. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D132425 Files: clang/lib/CodeGen/CGVTables.cpp clang/lib/CodeGen/CGVTables.h clang/lib/CodeGen/ItaniumCXXABI.cpp clang/test/CodeGenCXX/RelativeVTablesABI/relative-vtables-hwasan.cpp
Index: clang/test/CodeGenCXX/RelativeVTablesABI/relative-vtables-hwasan.cpp =================================================================== --- /dev/null +++ clang/test/CodeGenCXX/RelativeVTablesABI/relative-vtables-hwasan.cpp @@ -0,0 +1,34 @@ +// RUN: %clang_cc1 %s -triple=aarch64-unknown-fuchsia -S -o - -emit-llvm -fsanitize=hwaddress | FileCheck %s + +/// The usual vtable will have default visibility. In this case, the actual +/// vtable is hidden and the alias is made public. With hwasan enabled, we want +/// to ensure the local one self-referenced in the hidden vtable is not +/// hwasan-instrumented. +// CHECK-DAG: @_ZTV1A.local = private unnamed_addr constant { [3 x i32] } { [3 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZTI1A.rtti_proxy to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @_ZN1A3fooEv to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32)] }, no_sanitize_hwaddress, align 4 +// CHECK-DAG: @_ZTV1A = unnamed_addr alias { [3 x i32] }, ptr @_ZTV1A.local + +class A { +public: + virtual void foo(); +}; + +void A::foo() {} + +void A_foo(A *a) { + a->foo(); +} + +/// If the vtable happens to be hidden, then the alias is not needed. In this +/// case, the original vtable struct itself should be unsanitized. +// CHECK-DAG: @_ZTV1B = hidden unnamed_addr constant { [3 x i32] } { [3 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZTI1B.rtti_proxy to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1B, i32 0, i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @_ZN1B3fooEv to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1B, i32 0, i32 0, i32 2) to i64)) to i32)] }, no_sanitize_hwaddress, align 4 + +class __attribute__((visibility("hidden"))) B { +public: + virtual void foo(); +}; + +void B::foo() {} + +void B_foo(B *b) { + b->foo(); +} Index: clang/lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- clang/lib/CodeGen/ItaniumCXXABI.cpp +++ clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -1769,8 +1769,11 @@ } } - if (VTContext.isRelativeLayout() && !VTable->isDSOLocal()) - CGVT.GenerateRelativeVTableAlias(VTable, VTable->getName()); + if (VTContext.isRelativeLayout()) { + CGVT.RemoveHwasanMetadata(VTable); + if (!VTable->isDSOLocal()) + CGVT.GenerateRelativeVTableAlias(VTable, VTable->getName()); + } } bool ItaniumCXXABI::isVirtualOffsetNeededForVTableField( Index: clang/lib/CodeGen/CGVTables.h =================================================================== --- clang/lib/CodeGen/CGVTables.h +++ clang/lib/CodeGen/CGVTables.h @@ -154,6 +154,9 @@ /// when a vtable may not be dso_local. void GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable, llvm::StringRef AliasNameRef); + + /// Specify a vtable should not be instrumented with hwasan. + void RemoveHwasanMetadata(llvm::GlobalValue *VTable); }; } // end namespace CodeGen Index: clang/lib/CodeGen/CGVTables.cpp =================================================================== --- clang/lib/CodeGen/CGVTables.cpp +++ clang/lib/CodeGen/CGVTables.cpp @@ -921,12 +921,25 @@ CGM.EmitVTableTypeMetadata(RD, VTable, *VTLayout.get()); - if (UsingRelativeLayout && !VTable->isDSOLocal()) - GenerateRelativeVTableAlias(VTable, OutName); + if (UsingRelativeLayout) { + RemoveHwasanMetadata(VTable); + if (!VTable->isDSOLocal()) + GenerateRelativeVTableAlias(VTable, OutName); + } return VTable; } +void CodeGenVTables::RemoveHwasanMetadata(llvm::GlobalValue *VTable) { + if (CGM.getLangOpts().Sanitize.has(SanitizerKind::HWAddress)) { + llvm::GlobalValue::SanitizerMetadata Meta; + if (VTable->hasSanitizerMetadata()) + Meta = VTable->getSanitizerMetadata(); + Meta.NoHWAddress = true; + VTable->setSanitizerMetadata(Meta); + } +} + // If the VTable is not dso_local, then we will not be able to indicate that // the VTable does not need a relocation and move into rodata. A frequent // time this can occur is for classes that should be made public from a DSO
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits