Skip to content

Commit be6614b

Browse files
committed
Add basic DW_TAG_LLVM_ptrauth_type handling in lldb user expressions
1 parent 0e3c7fa commit be6614b

File tree

10 files changed

+104
-1
lines changed

10 files changed

+104
-1
lines changed

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ class CompilerType {
279279
/// Return a new CompilerType that is a pointer to this type
280280
CompilerType GetPointerType() const;
281281

282+
283+
282284
/// Return a new CompilerType that is a L value reference to this type if this
283285
/// type is valid and the type system supports L value references, else return
284286
/// an invalid type.
@@ -322,6 +324,12 @@ class CompilerType {
322324

323325
/// Create related types using the current type's AST
324326
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const;
327+
328+
/// Return a new CompilerType adds a ptrauth modifier with given parameters to this type if this type
329+
/// is valid and the type system supports ptrauth modifiers, else return an
330+
/// invalid type. Note that this does not check if this type is a pointer.
331+
CompilerType AddPtrAuthModifier(unsigned key, bool isAddressDiscriminated,
332+
unsigned extraDiscriminator) const;
325333
/// \}
326334

327335
/// Exploring the type.

lldb/include/lldb/Symbol/Type.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class Type : public std::enable_shared_from_this<Type>, public UserID {
9292
/// This type is the type whose UID is m_encoding_uid as an atomic type.
9393
eEncodingIsAtomicUID,
9494
/// This type is the synthetic type whose UID is m_encoding_uid.
95-
eEncodingIsSyntheticUID
95+
eEncodingIsSyntheticUID,
96+
/// This type is a signed pointer.
97+
eEncodingIsLLVMPtrAuthUID
9698
};
9799

98100
enum class ResolveState : unsigned char {

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ class TypeSystem : public PluginInterface,
275275

276276
virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
277277

278+
// TODO: are we allowed to insert virtual functions in the middle of the class interface and break ABI?
279+
virtual CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type,
280+
unsigned key,
281+
bool isAddressDiscriminated,
282+
unsigned extraDiscriminator);
283+
278284
virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
279285

280286
virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,15 @@ ClangExpressionParser::ClangExpressionParser(
442442
m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) {
443443
m_compiler->getTargetOpts().ABI = "apcs-gnu";
444444
}
445+
445446
// Supported subsets of x86
446447
if (target_machine == llvm::Triple::x86 ||
447448
target_machine == llvm::Triple::x86_64) {
449+
// FIXME: shouldn't this be placed after
450+
// `auto target_info = TargetInfo::CreateTargetInfo(...)`
451+
// (see `if (target_machine == llvm::Triple::aarch64)`)?
452+
// It computes `Features` from `FeatureMap` and `FeaturesAsWritten` and
453+
// erases initial `Features` vector.
448454
m_compiler->getTargetOpts().Features.push_back("+sse");
449455
m_compiler->getTargetOpts().Features.push_back("+sse2");
450456
}
@@ -467,6 +473,10 @@ ClangExpressionParser::ClangExpressionParser(
467473

468474
auto target_info = TargetInfo::CreateTargetInfo(
469475
m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts);
476+
if (target_machine == llvm::Triple::aarch64) {
477+
// TODO: enable this depending on corresponding tag section in ELF
478+
target_info->getTargetOpts().Features.push_back("+pauth");
479+
}
470480
if (log) {
471481
LLDB_LOGF(log, "Target datalayout string: '%s'",
472482
target_info->getDataLayoutString());
@@ -612,6 +622,12 @@ ClangExpressionParser::ClangExpressionParser(
612622
// additionally enabling them as expandable builtins is breaking Clang.
613623
lang_opts.NoBuiltin = true;
614624

625+
lang_opts.PointerAuthCalls = true;
626+
lang_opts.PointerAuthReturns = true;
627+
lang_opts.PointerAuthVTPtrAddressDiscrimination = true;
628+
lang_opts.PointerAuthVTPtrTypeDiscrimination = true;
629+
lang_opts.PointerAuthInitFini = true;
630+
615631
// Set CodeGen options
616632
m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
617633
m_compiler->getCodeGenOpts().InstrumentFunctions = false;
@@ -622,6 +638,10 @@ ClangExpressionParser::ClangExpressionParser(
622638
else
623639
m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo);
624640

641+
CompilerInvocation::setDefaultPointerAuthOptions(
642+
m_compiler->getCodeGenOpts().PointerAuth, lang_opts,
643+
target_arch.GetTriple());
644+
625645
// Disable some warnings.
626646
SetupDefaultClangDiagnostics(*m_compiler);
627647

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
473473
case DW_TAG_restrict_type:
474474
case DW_TAG_volatile_type:
475475
case DW_TAG_atomic_type:
476+
case DW_TAG_LLVM_ptrauth_type:
476477
case DW_TAG_unspecified_type: {
477478
type_sp = ParseTypeModifier(sc, die, attrs);
478479
break;
@@ -629,6 +630,9 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
629630
case DW_TAG_atomic_type:
630631
encoding_data_type = Type::eEncodingIsAtomicUID;
631632
break;
633+
case DW_TAG_LLVM_ptrauth_type:
634+
encoding_data_type = Type::eEncodingIsLLVMPtrAuthUID;
635+
break;
632636
}
633637

634638
if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
@@ -3399,6 +3403,7 @@ clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
33993403
decl = m_ast.CreateVariableDeclaration(
34003404
decl_context, GetOwningClangModule(die), name,
34013405
ClangUtil::GetQualType(type->GetForwardCompilerType()));
3406+
// TODO: handled signed member function pointers and stuff
34023407
}
34033408
break;
34043409
}

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,6 +4622,24 @@ TypeSystemClang::AddConstModifier(lldb::opaque_compiler_type_t type) {
46224622
return CompilerType();
46234623
}
46244624

4625+
CompilerType
4626+
TypeSystemClang::AddPtrAuthModifier(lldb::opaque_compiler_type_t type,
4627+
unsigned key, bool isAddressDiscriminated,
4628+
unsigned extraDiscriminator) {
4629+
if (type) {
4630+
clang::ASTContext &clang_ast = getASTContext();
4631+
auto pauth = PointerAuthQualifier::Create(
4632+
key, isAddressDiscriminated, extraDiscriminator,
4633+
PointerAuthenticationMode::SignAndAuth,
4634+
/* isIsaPointer */ false,
4635+
/* authenticatesNullValues */ false);
4636+
clang::QualType result =
4637+
clang_ast.getPointerAuthType(GetQualType(type), pauth);
4638+
return GetType(result);
4639+
}
4640+
return CompilerType();
4641+
}
4642+
46254643
CompilerType
46264644
TypeSystemClang::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
46274645
if (type) {

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ class TypeSystemClang : public TypeSystem {
782782

783783
CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override;
784784

785+
CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, unsigned key,
786+
bool isAddressDiscriminated,
787+
unsigned extraDiscriminator) override;
788+
785789
CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override;
786790

787791
CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override;

lldb/source/Symbol/CompilerType.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,17 @@ CompilerType CompilerType::GetPointerType() const {
485485
return CompilerType();
486486
}
487487

488+
CompilerType CompilerType::AddPtrAuthModifier(unsigned key,
489+
bool isAddressDiscriminated,
490+
unsigned extraDiscriminator) const {
491+
if (IsValid()) {
492+
if (auto type_system_sp = GetTypeSystem())
493+
return type_system_sp->AddPtrAuthModifier(
494+
m_type, key, isAddressDiscriminated, extraDiscriminator);
495+
}
496+
return CompilerType();
497+
}
498+
488499
CompilerType CompilerType::GetLValueReferenceType() const {
489500
if (IsValid())
490501
if (auto type_system_sp = GetTypeSystem())

lldb/source/Symbol/Type.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ void Type::GetDescription(Stream *s, lldb::DescriptionLevel level,
230230
case eEncodingIsSyntheticUID:
231231
s->PutCString(" (synthetic type)");
232232
break;
233+
case eEncodingIsLLVMPtrAuthUID:
234+
s->PutCString(" (ptrauth type)");
235+
break;
233236
}
234237
}
235238
}
@@ -291,6 +294,8 @@ void Type::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
291294
case eEncodingIsSyntheticUID:
292295
s->PutCString(" (synthetic type)");
293296
break;
297+
case eEncodingIsLLVMPtrAuthUID:
298+
s->PutCString(" (ptrauth type)");
294299
}
295300
}
296301

@@ -383,6 +388,9 @@ std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) {
383388
return static_cast<uint64_t>(m_byte_size);
384389
}
385390
} break;
391+
case eEncodingIsLLVMPtrAuthUID:
392+
// TODO: compute byte size properly
393+
return 8;
386394
}
387395
return {};
388396
}
@@ -538,6 +546,13 @@ bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) {
538546
encoding_type->GetForwardCompilerType().GetRValueReferenceType();
539547
break;
540548

549+
case eEncodingIsLLVMPtrAuthUID:
550+
// TODO: proper signing schema
551+
m_compiler_type = encoding_type->GetForwardCompilerType().AddPtrAuthModifier(
552+
/*key*/ 0, /*isAddressDiscriminated*/ false,
553+
/*extraDiscriminator*/ 0);
554+
break;
555+
541556
default:
542557
llvm_unreachable("Unhandled encoding_data_type.");
543558
}
@@ -593,6 +608,13 @@ bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) {
593608
m_compiler_type = void_compiler_type.GetRValueReferenceType();
594609
break;
595610

611+
case eEncodingIsLLVMPtrAuthUID:
612+
// TODO: proper signing schema
613+
m_compiler_type = void_compiler_type.AddPtrAuthModifier(
614+
/*key*/ 0, /*isAddressDiscriminated*/ false,
615+
/*extraDiscriminator*/ 0);
616+
break;
617+
596618
default:
597619
llvm_unreachable("Unhandled encoding_data_type.");
598620
}

lldb/source/Symbol/TypeSystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ CompilerType TypeSystem::AddConstModifier(lldb::opaque_compiler_type_t type) {
9393
return CompilerType();
9494
}
9595

96+
CompilerType TypeSystem::AddPtrAuthModifier(lldb::opaque_compiler_type_t type,
97+
unsigned key,
98+
bool isAddressDiscriminated,
99+
unsigned extraDiscriminator) {
100+
return CompilerType();
101+
}
102+
96103
CompilerType
97104
TypeSystem::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
98105
return CompilerType();

0 commit comments

Comments
 (0)