From ae18b9410297963a379e03b94eae7433ff69a62b Mon Sep 17 00:00:00 2001 From: Akira Hatanaka Date: Thu, 11 Jul 2024 09:09:20 -0700 Subject: [PAC] Implement function pointer type discrimination (#96992) Give users an option (-fptrauth-function-pointer-type-discrimination) to sign a function pointer using a non-zero discriminator based on the function type. The discriminator is computed by first translating the function type to a string and then computing the hash value of the string. Two function types that are compatible in C must be translated to the same string with the exception of function types that use typedefs of anonymous structs in their return type or parameter types. This patch doesn't have the code to resign function pointers, which is needed when a function pointer is converted to a different function type. That will be implemented in another patch. Co-authored-by: John McCall --------- Co-authored-by: John McCall --- clang/lib/Frontend/CompilerInvocation.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 0082c15..bf57add 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1479,8 +1479,10 @@ void CompilerInvocation::setDefaultPointerAuthOptions( using Key = PointerAuthSchema::ARM8_3Key; using Discrimination = PointerAuthSchema::Discrimination; // If you change anything here, be sure to update . - Opts.FunctionPointers = - PointerAuthSchema(Key::ASIA, false, Discrimination::None); + Opts.FunctionPointers = PointerAuthSchema( + Key::ASIA, false, + LangOpts.PointerAuthFunctionTypeDiscrimination ? Discrimination::Type + : Discrimination::None); Opts.CXXVTablePointers = PointerAuthSchema( Key::ASDA, LangOpts.PointerAuthVTPtrAddressDiscrimination, @@ -3411,6 +3413,8 @@ static void GeneratePointerAuthArgs(const LangOptions &Opts, GenerateArg(Consumer, OPT_fptrauth_vtable_pointer_type_discrimination); if (Opts.PointerAuthInitFini) GenerateArg(Consumer, OPT_fptrauth_init_fini); + if (Opts.PointerAuthFunctionTypeDiscrimination) + GenerateArg(Consumer, OPT_fptrauth_function_pointer_type_discrimination); } static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args, @@ -3424,6 +3428,8 @@ static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args, Opts.PointerAuthVTPtrTypeDiscrimination = Args.hasArg(OPT_fptrauth_vtable_pointer_type_discrimination); Opts.PointerAuthInitFini = Args.hasArg(OPT_fptrauth_init_fini); + Opts.PointerAuthFunctionTypeDiscrimination = + Args.hasArg(OPT_fptrauth_function_pointer_type_discrimination); } /// Check if input file kind and language standard are compatible. -- cgit v1.1