Skip to content

Commit 1fa1aab

Browse files
committed
[UBSan] Implementation NonUniqueARMRTTIBit __type_name has higher bits set on arm64
Differential Revision: https://reviews.llvm.org/D137518
1 parent 2d678d8 commit 1fa1aab

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "sanitizer_common/sanitizer_common.h"
1919
#include "sanitizer_common/sanitizer_ptrauth.h"
20+
#include <stdint.h>
2021

2122
// The following are intended to be binary compatible with the definitions
2223
// given in the Itanium ABI. We make no attempt to be ODR-compatible with
@@ -25,9 +26,20 @@
2526
namespace std {
2627
class type_info {
2728
public:
29+
typedef const char *__type_name_t;
2830
virtual ~type_info();
2931

3032
const char *__type_name;
33+
34+
__type_name_t name() const {
35+
#if defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
36+
uintptr_t __non_unique_rtti_bit =
37+
(1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1));
38+
return (__type_name_t)((uintptr_t)__type_name & ~__non_unique_rtti_bit);
39+
#else
40+
return __type_name;
41+
#endif
42+
}
3143
};
3244
}
3345

@@ -117,7 +129,7 @@ static __ubsan::HashValue *getTypeCacheHashTableBucket(__ubsan::HashValue V) {
117129
static bool isDerivedFromAtOffset(const abi::__class_type_info *Derived,
118130
const abi::__class_type_info *Base,
119131
sptr Offset) {
120-
if (Derived->__type_name == Base->__type_name ||
132+
if (Derived->name() == Base->name() ||
121133
__ubsan::checkTypeInfoEquality(Derived, Base))
122134
return Offset == 0;
123135

@@ -254,17 +266,16 @@ __ubsan::getDynamicTypeInfoFromVtable(void *VtablePtr) {
254266
const abi::__class_type_info *ObjectType = findBaseAtOffset(
255267
static_cast<const abi::__class_type_info*>(Vtable->TypeInfo),
256268
-Vtable->Offset);
257-
return DynamicTypeInfo(Vtable->TypeInfo->__type_name, -Vtable->Offset,
258-
ObjectType ? ObjectType->__type_name : "<unknown>");
269+
return DynamicTypeInfo(Vtable->TypeInfo->name(), -Vtable->Offset,
270+
ObjectType ? ObjectType->name() : "<unknown>");
259271
}
260272

261273
bool __ubsan::checkTypeInfoEquality(const void *TypeInfo1,
262274
const void *TypeInfo2) {
263275
auto TI1 = static_cast<const std::type_info *>(TypeInfo1);
264276
auto TI2 = static_cast<const std::type_info *>(TypeInfo2);
265-
return SANITIZER_NON_UNIQUE_TYPEINFO && TI1->__type_name[0] != '*' &&
266-
TI2->__type_name[0] != '*' &&
267-
!internal_strcmp(TI1->__type_name, TI2->__type_name);
277+
return SANITIZER_NON_UNIQUE_TYPEINFO && TI1->name()[0] != '*' &&
278+
TI2->name()[0] != '*' && !internal_strcmp(TI1->name(), TI2->name());
268279
}
269280

270281
#endif // CAN_SANITIZE_UB && !SANITIZER_WINDOWS

0 commit comments

Comments
 (0)