diff --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h index 2d7092d2c93f..10edd75aee2f 100644 --- a/lldb/include/lldb/Symbol/CompilerType.h +++ b/lldb/include/lldb/Symbol/CompilerType.h @@ -322,6 +322,13 @@ class CompilerType { /// Create related types using the current type's AST CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const; + + /// Return a new CompilerType adds a ptrauth modifier with given parameters to + /// this type if this type is valid and the type system supports ptrauth + /// modifiers, else return an invalid type. Note that this does not check if + /// this type is a pointer. + CompilerType AddPtrAuthModifier(unsigned key, bool isAddressDiscriminated, + unsigned extraDiscriminator) const; /// \} /// Exploring the type. diff --git a/lldb/include/lldb/Symbol/Type.h b/lldb/include/lldb/Symbol/Type.h index 046501931d21..5dcd98387c90 100644 --- a/lldb/include/lldb/Symbol/Type.h +++ b/lldb/include/lldb/Symbol/Type.h @@ -92,7 +92,9 @@ class Type : public std::enable_shared_from_this, public UserID { /// This type is the type whose UID is m_encoding_uid as an atomic type. eEncodingIsAtomicUID, /// This type is the synthetic type whose UID is m_encoding_uid. - eEncodingIsSyntheticUID + eEncodingIsSyntheticUID, + /// This type is a signed pointer. + eEncodingIsLLVMPtrAuthUID }; enum class ResolveState : unsigned char { diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h index eb6e453e1aec..b58f4a87de3d 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -275,6 +275,13 @@ class TypeSystem : public PluginInterface, virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type); + // TODO: are we allowed to insert virtual functions in the middle of the class + // interface and break ABI? + virtual CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, + unsigned key, + bool isAddressDiscriminated, + unsigned extraDiscriminator); + virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type); virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type); diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp index f65192a3225e..740dc44fae87 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -445,6 +445,11 @@ ClangExpressionParser::ClangExpressionParser( // Supported subsets of x86 if (target_machine == llvm::Triple::x86 || target_machine == llvm::Triple::x86_64) { + // FIXME: shouldn't this be placed after + // `auto target_info = TargetInfo::CreateTargetInfo(...)` + // (see `if (target_machine == llvm::Triple::aarch64)`)? + // It computes `Features` from `FeatureMap` and `FeaturesAsWritten` and + // erases initial `Features` vector. m_compiler->getTargetOpts().Features.push_back("+sse"); m_compiler->getTargetOpts().Features.push_back("+sse2"); } @@ -467,6 +472,10 @@ ClangExpressionParser::ClangExpressionParser( auto target_info = TargetInfo::CreateTargetInfo( m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts); + if (target_machine == llvm::Triple::aarch64) { + // TODO: enable this depending on corresponding tag section in ELF + target_info->getTargetOpts().Features.push_back("+pauth"); + } if (log) { LLDB_LOGF(log, "Target datalayout string: '%s'", target_info->getDataLayoutString()); @@ -612,6 +621,13 @@ ClangExpressionParser::ClangExpressionParser( // additionally enabling them as expandable builtins is breaking Clang. lang_opts.NoBuiltin = true; + // TODO: enable this depending on corresponding tag section in ELF + lang_opts.PointerAuthCalls = true; + lang_opts.PointerAuthReturns = true; + lang_opts.PointerAuthVTPtrAddressDiscrimination = true; + lang_opts.PointerAuthVTPtrTypeDiscrimination = true; + lang_opts.PointerAuthInitFini = true; + // Set CodeGen options m_compiler->getCodeGenOpts().EmitDeclMetadata = true; m_compiler->getCodeGenOpts().InstrumentFunctions = false; @@ -622,6 +638,10 @@ ClangExpressionParser::ClangExpressionParser( else m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo); + CompilerInvocation::setDefaultPointerAuthOptions( + m_compiler->getCodeGenOpts().PointerAuth, lang_opts, + target_arch.GetTriple()); + // Disable some warnings. SetupDefaultClangDiagnostics(*m_compiler); diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp index 37fb16d4e035..57cf341fbe4c 100644 --- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp +++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp @@ -473,6 +473,7 @@ TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc, case DW_TAG_restrict_type: case DW_TAG_volatile_type: case DW_TAG_atomic_type: + case DW_TAG_LLVM_ptrauth_type: case DW_TAG_unspecified_type: { type_sp = ParseTypeModifier(sc, die, attrs); break; @@ -629,6 +630,9 @@ DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc, case DW_TAG_atomic_type: encoding_data_type = Type::eEncodingIsAtomicUID; break; + case DW_TAG_LLVM_ptrauth_type: + encoding_data_type = Type::eEncodingIsLLVMPtrAuthUID; + break; } if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || @@ -3399,6 +3403,7 @@ clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) { decl = m_ast.CreateVariableDeclaration( decl_context, GetOwningClangModule(die), name, ClangUtil::GetQualType(type->GetForwardCompilerType())); + // TODO: handled signed member function pointers and stuff } break; } diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 69cff0f35ae4..9903220abf9d 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -4622,6 +4622,24 @@ TypeSystemClang::AddConstModifier(lldb::opaque_compiler_type_t type) { return CompilerType(); } +CompilerType +TypeSystemClang::AddPtrAuthModifier(lldb::opaque_compiler_type_t type, + unsigned key, bool isAddressDiscriminated, + unsigned extraDiscriminator) { + if (type) { + clang::ASTContext &clang_ast = getASTContext(); + auto pauth = PointerAuthQualifier::Create( + key, isAddressDiscriminated, extraDiscriminator, + PointerAuthenticationMode::SignAndAuth, + /* isIsaPointer */ false, + /* authenticatesNullValues */ false); + clang::QualType result = + clang_ast.getPointerAuthType(GetQualType(type), pauth); + return GetType(result); + } + return CompilerType(); +} + CompilerType TypeSystemClang::AddVolatileModifier(lldb::opaque_compiler_type_t type) { if (type) { diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h index 0544de3cd33b..fcc2c506e02a 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -782,6 +782,10 @@ class TypeSystemClang : public TypeSystem { CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override; + CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, + unsigned key, bool isAddressDiscriminated, + unsigned extraDiscriminator) override; + CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override; CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override; diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp index 33f7e63d9be4..58e9820e455c 100644 --- a/lldb/source/Symbol/CompilerType.cpp +++ b/lldb/source/Symbol/CompilerType.cpp @@ -485,6 +485,17 @@ CompilerType CompilerType::GetPointerType() const { return CompilerType(); } +CompilerType +CompilerType::AddPtrAuthModifier(unsigned key, bool isAddressDiscriminated, + unsigned extraDiscriminator) const { + if (IsValid()) { + if (auto type_system_sp = GetTypeSystem()) + return type_system_sp->AddPtrAuthModifier( + m_type, key, isAddressDiscriminated, extraDiscriminator); + } + return CompilerType(); +} + CompilerType CompilerType::GetLValueReferenceType() const { if (IsValid()) if (auto type_system_sp = GetTypeSystem()) diff --git a/lldb/source/Symbol/Type.cpp b/lldb/source/Symbol/Type.cpp index 66284eb73cad..e5a98b0cdaac 100644 --- a/lldb/source/Symbol/Type.cpp +++ b/lldb/source/Symbol/Type.cpp @@ -230,6 +230,9 @@ void Type::GetDescription(Stream *s, lldb::DescriptionLevel level, case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break; + case eEncodingIsLLVMPtrAuthUID: + s->PutCString(" (ptrauth type)"); + break; } } } @@ -291,6 +294,8 @@ void Type::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) { case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break; + case eEncodingIsLLVMPtrAuthUID: + s->PutCString(" (ptrauth type)"); } } @@ -383,6 +388,9 @@ std::optional Type::GetByteSize(ExecutionContextScope *exe_scope) { return static_cast(m_byte_size); } } break; + case eEncodingIsLLVMPtrAuthUID: + // TODO: compute byte size properly + return 8; } return {}; } @@ -538,6 +546,14 @@ bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) { encoding_type->GetForwardCompilerType().GetRValueReferenceType(); break; + case eEncodingIsLLVMPtrAuthUID: + // TODO: proper signing schema + m_compiler_type = + encoding_type->GetForwardCompilerType().AddPtrAuthModifier( + /*key*/ 0, /*isAddressDiscriminated*/ false, + /*extraDiscriminator*/ 0); + break; + default: llvm_unreachable("Unhandled encoding_data_type."); } @@ -593,6 +609,13 @@ bool Type::ResolveCompilerType(ResolveState compiler_type_resolve_state) { m_compiler_type = void_compiler_type.GetRValueReferenceType(); break; + case eEncodingIsLLVMPtrAuthUID: + // TODO: proper signing schema + m_compiler_type = void_compiler_type.AddPtrAuthModifier( + /*key*/ 0, /*isAddressDiscriminated*/ false, + /*extraDiscriminator*/ 0); + break; + default: llvm_unreachable("Unhandled encoding_data_type."); } diff --git a/lldb/source/Symbol/TypeSystem.cpp b/lldb/source/Symbol/TypeSystem.cpp index 24f202930565..47949518f7c6 100644 --- a/lldb/source/Symbol/TypeSystem.cpp +++ b/lldb/source/Symbol/TypeSystem.cpp @@ -93,6 +93,13 @@ CompilerType TypeSystem::AddConstModifier(lldb::opaque_compiler_type_t type) { return CompilerType(); } +CompilerType TypeSystem::AddPtrAuthModifier(lldb::opaque_compiler_type_t type, + unsigned key, + bool isAddressDiscriminated, + unsigned extraDiscriminator) { + return CompilerType(); +} + CompilerType TypeSystem::AddVolatileModifier(lldb::opaque_compiler_type_t type) { return CompilerType();