-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Implement type/address discrimination of type_info vtable. #99726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ahmedbougacha
merged 2 commits into
llvm:main
from
ahmedbougacha:users/ahmedbougacha/ptrauth-type-info-vtable-discrimination
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// RUN: %clang_cc1 -DENABLE_TID=0 -I%S -std=c++11 -triple=arm64e-apple-darwin \ | ||
// RUN: -fptrauth-calls -fptrauth-intrinsics \ | ||
// RUN: -fptrauth-vtable-pointer-type-discrimination \ | ||
// RUN: -fptrauth-vtable-pointer-address-discrimination \ | ||
// RUN: %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,NODISC | ||
|
||
// RUN: %clang_cc1 -DENABLE_TID=1 -I%S -std=c++11 -triple=arm64e-apple-darwin \ | ||
// RUN: -fptrauth-calls -fptrauth-intrinsics \ | ||
// RUN: -fptrauth-vtable-pointer-type-discrimination \ | ||
// RUN: -fptrauth-vtable-pointer-address-discrimination \ | ||
// RUN: -fptrauth-type-info-vtable-pointer-discrimination \ | ||
// RUN: %s -emit-llvm -o - | FileCheck %s --check-prefixes=CHECK,DISC | ||
|
||
// copied from typeinfo | ||
namespace std { | ||
|
||
#if __has_cpp_attribute(clang::ptrauth_vtable_pointer) | ||
# if __has_feature(ptrauth_type_info_vtable_pointer_discrimination) | ||
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \ | ||
[[clang::ptrauth_vtable_pointer(process_independent, address_discrimination, type_discrimination)]] | ||
# else | ||
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \ | ||
[[clang::ptrauth_vtable_pointer(process_independent, no_address_discrimination, no_extra_discrimination)]] | ||
# endif | ||
#else | ||
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH | ||
#endif | ||
|
||
class _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH type_info | ||
{ | ||
type_info& operator=(const type_info&); | ||
type_info(const type_info&); | ||
|
||
protected: | ||
explicit type_info(const char* __n); | ||
|
||
public: | ||
virtual ~type_info(); | ||
|
||
virtual void test_method(); | ||
}; | ||
} // namespace std | ||
|
||
static_assert(__has_feature(ptrauth_type_info_vtable_pointer_discrimination) == ENABLE_TID, "incorrect feature state"); | ||
|
||
// CHECK: @disc_std_type_info = global i32 [[STDTYPEINFO_DISC:45546]] | ||
extern "C" int disc_std_type_info = __builtin_ptrauth_string_discriminator("_ZTVSt9type_info"); | ||
|
||
// CHECK: @_ZTV10TestStruct = unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr @_ZTI10TestStruct, ptr ptrauth (ptr @_ZN10TestStructD1Ev, i32 0, i64 52216, ptr getelementptr inbounds ({ [4 x ptr] }, ptr @_ZTV10TestStruct, i32 0, i32 0, i32 2)), ptr ptrauth (ptr @_ZN10TestStructD0Ev, i32 0, i64 39671, ptr getelementptr inbounds ({ [4 x ptr] }, ptr @_ZTV10TestStruct, i32 0, i32 0, i32 3))] }, align 8 | ||
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr] | ||
// CHECK: @_ZTS10TestStruct = constant [13 x i8] c"10TestStruct\00", align 1 | ||
|
||
// NODISC: @_ZTI10TestStruct = constant { ptr, ptr } { ptr ptrauth (ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), i32 2), ptr @_ZTS10TestStruct }, align 8 | ||
|
||
// DISC: @_ZTI10TestStruct = constant { ptr, ptr } { ptr ptrauth (ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), i32 2, i64 [[STDTYPEINFO_DISC]]), ptr @_ZTS10TestStruct }, align 8 | ||
|
||
struct TestStruct { | ||
virtual ~TestStruct(); | ||
int a; | ||
}; | ||
|
||
TestStruct::~TestStruct(){} | ||
|
||
extern "C" void test_vtable(std::type_info* t) { | ||
t->test_method(); | ||
} | ||
// NODISC: define void @test_vtable(ptr noundef %t) | ||
// NODISC: [[T_ADDR:%.*]] = alloca ptr, align 8 | ||
// NODISC: store ptr %t, ptr [[T_ADDR]], align 8 | ||
// NODISC: [[T:%.*]] = load ptr, ptr [[T_ADDR]], align 8 | ||
// NODISC: [[VPTR:%.*]] = load ptr, ptr [[T]], align 8 | ||
// NODISC: [[CAST_VPTR:%.*]] = ptrtoint ptr [[VPTR]] to i64 | ||
// NODISC: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[CAST_VPTR]], i32 2, i64 0) | ||
|
||
// DISC: define void @test_vtable(ptr noundef %t) | ||
// DISC: [[T_ADDR:%.*]] = alloca ptr, align 8 | ||
// DISC: store ptr %t, ptr [[T_ADDR]], align 8 | ||
// DISC: [[T:%.*]] = load ptr, ptr [[T_ADDR]], align 8 | ||
// DISC: [[VPTR:%.*]] = load ptr, ptr [[T]], align 8 | ||
// DISC: [[ADDR:%.*]] = ptrtoint ptr [[T]] to i64 | ||
// DISC: [[DISCRIMINATOR:%.*]] = call i64 @llvm.ptrauth.blend(i64 [[ADDR]], i64 [[STDTYPEINFO_DISC]]) | ||
// DISC: [[VPTRI:%.*]] = ptrtoint ptr [[VPTR]] to i64 | ||
// DISC: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[VPTRI]], i32 2, i64 [[DISCRIMINATOR]]) | ||
|
||
extern "C" const void *ensure_typeinfo() { | ||
return new TestStruct; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahmedbougacha Here, we have
This has constant discriminator, but does not have address discrimination, while it should be enabled with
-fptrauth-type-info-vtable-pointer-discrimination
. The correct output should be smth like (if we use a placeholder valueptr inttoptr (i64 1 to ptr)
as storage address)The root cause is described in my previous comment #99726.