Skip to content

Add basic DW_TAG_LLVM_ptrauth_type handling in lldb user expressions #67

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lldb/include/lldb/Symbol/CompilerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion lldb/include/lldb/Symbol/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class Type : public std::enable_shared_from_this<Type>, 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 {
Expand Down
7 changes: 7 additions & 0 deletions lldb/include/lldb/Symbol/TypeSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions lldb/source/Symbol/CompilerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
23 changes: 23 additions & 0 deletions lldb/source/Symbol/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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)");
}
}

Expand Down Expand Up @@ -383,6 +388,9 @@ std::optional<uint64_t> Type::GetByteSize(ExecutionContextScope *exe_scope) {
return static_cast<uint64_t>(m_byte_size);
}
} break;
case eEncodingIsLLVMPtrAuthUID:
// TODO: compute byte size properly
return 8;
}
return {};
}
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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.");
}
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Symbol/TypeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down