[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-10 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Thanks @smithp35 for a detailed description!

> Use the environment part of the triple

I suppose we should use this this option since it's harder for end users to use 
that incorrectly - it looks like that many real-world code and corresponding 
build environments rely on triple and do not encounter other options (for 
example, as you said, hard/soft float are encoded in triple or normalized to 
triple if passed separately). 

I'll implement initial support for that and update current PR.

> I think we should require the `+pauth` feature (default in armv8.3-A) for 
> `-mabi=pauth` to be accepted.

We actually have a similar issue already opened, see #97332, so yes, it's a 
known feature request.

> If we are being as careful to avoid prematurly fixing a signing schema for 
> the linux target we could use a of test and no `-mabi=pauth` default. For 
> example `-mabi=pauthtest`.

I like this idea. Of course, we can mark functionality as "alpha" or 
"experimental", but just using a different option value makes this more 
explicit and end users will not have a chance to mix "alpha" stuff with 
"released" stuff when we have one.

> I think that this has to be under control of the platform. My intuition is 
> that we trust the user, perhaps with a warning, and
> record the high-level signing schema.

In general, I agree with that point, but for the first "adoption" phase (while 
we have `-mabi=pauthtest` or `-mabi=pauth` marked as "alpha") it might be 
useful to encode the low-level schema - we let users compile with `-fptrauth-*` 
flags if they want (probably with a huge warning that they are using this at 
their own risk), but we do not let them link incompatible binaries. At least, I 
suggest to limit scope of the PR to implementing `-mabi=pauth` (normalizing to 
triple), parsing the triple and enabling proper features correspondingly, while 
leaving low-level encoding of signing schema in pauth core info (inside elf's 
gnu property) already implemented untouched as for now.

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@ahmedbougacha I've updated my review: I've misinterpreted logic a bit 
previously, and thought that a couple of codepaths are not covered by tests. 
It's not true, everything is OK, but please address @efriedma-quic 's concern 
described above so this can be approved and merged.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 deleted 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 deleted 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -1789,6 +1789,9 @@ void Clang::AddAArch64TargetArgs(const ArgList ,
   options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
   Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
 options::OPT_fno_ptrauth_init_fini);
+

kovdan01 wrote:

Nit: there is no empty line between previous `Args.addOptInFlag(...)` 
invocations, so probably this new line should also be deleted.

Feel free to ignore

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,106 @@
+; RUN: llc -mtriple arm64e-apple-darwin \
+; RUN:   -asm-verbose=false -aarch64-enable-collect-loh=false \
+; RUN:   -o - %s | FileCheck %s
+
+; RUN: llc -mtriple arm64e-apple-darwin \
+; RUN:   -global-isel -global-isel-abort=1 -verify-machineinstrs \
+; RUN:   -asm-verbose=false -aarch64-enable-collect-loh=false \
+; RUN:   -o - %s | FileCheck %s
+
+; The discriminator is the same for all blockaddresses in the function.

kovdan01 wrote:

Nit: it looks like that comments which are actually comments and not run/check 
lines use `;;` as prefix instead of `;` in newly added tests. Same applies 
below.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -3461,6 +3470,23 @@ bool AArch64InstructionSelector::select(MachineInstr ) 
{
 return true;
   }
   case TargetOpcode::G_BLOCK_ADDR: {
+Function *BAFn = I.getOperand(1).getBlockAddress()->getFunction();
+if (std::optional BADisc =
+STI.getPtrAuthBlockAddressDiscriminator(*BAFn)) {
+  MIB.buildInstr(TargetOpcode::IMPLICIT_DEF, {AArch64::X16}, {});

kovdan01 wrote:

Is there a test which covers this code path?

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -2516,6 +2516,10 @@ bool AArch64FastISel::selectIndirectBr(const Instruction 
*I) {
   if (AddrReg == 0)
 return false;
 
+  // Authenticated indirectbr is not implemented yet.

kovdan01 wrote:

It looks like that a test with FastISel ensuring that we fall back to 
SelectionDAG ISel is missing. I'm OK with adding missing tests later as a 
separate patch.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -1866,6 +1876,20 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const 
MachineInstr ) {
   assert(STI->getInstrInfo()->getInstSizeInBytes(MI) >= InstsEmitted * 4);
 }
 
+const MCExpr *
+AArch64AsmPrinter::lowerBlockAddressConstant(const BlockAddress ) {
+  const MCExpr *BAE = AsmPrinter::lowerBlockAddressConstant(BA);
+  const Function  = *BA.getFunction();
+
+  if (std::optional BADisc =
+  STI->getPtrAuthBlockAddressDiscriminator(Fn))
+return AArch64AuthMCExpr::create(BAE, *BADisc, AArch64PACKey::IA,
+ /* HasAddressDiversity= */ false,

kovdan01 wrote:

Nit: it looks like that across llvm it's more common not to leave spaces inside 
comment in such case 
https://llvm.org/docs/CodingStandards.html#comment-formatting

```suggestion
 /*HasAddressDiversity=*/ false,
```

Feel free to ignore

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -10704,15 +10728,36 @@ SDValue 
AArch64TargetLowering::LowerConstantPool(SDValue Op,
 
 SDValue AArch64TargetLowering::LowerBlockAddress(SDValue Op,
SelectionDAG ) const {
-  BlockAddressSDNode *BA = cast(Op);
+  BlockAddressSDNode *BAN = cast(Op);
+  const BlockAddress *BA = BAN->getBlockAddress();
+
+  if (std::optional BADisc =
+  Subtarget->getPtrAuthBlockAddressDiscriminator(*BA->getFunction())) {
+SDLoc DL(Op);

kovdan01 wrote:

Is there a test which covers this code path?

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -412,6 +412,15 @@ class AArch64Subtarget final : public 
AArch64GenSubtargetInfo {
   /// Choose a method of checking LR before performing a tail call.
   AArch64PAuth::AuthCheckMethod getAuthenticatedLRCheckMethod() const;
 
+  /// Compute the integer discriminator for a given BlockAddress constant, if
+  /// blockaddress signing is enabled (using function attribute
+  /// "ptrauth-indirect-gotos").

kovdan01 wrote:

Nit: it might be worth explicitly saying that `std::nullopt` return value 
stands for disabled indirect gotos signing: someone might mistakenly think that 
`std::nullopt` might indicate absent discriminator (say, zero discr by default) 
with signing enabled.

Alternatively, you might consider adding a separate function for determining 
presence of "ptrauth-indirect-gotos" attribute and use just `uint16_t` as a 
return value for `getPtrAuthBlockAddressDiscriminator` (with an assertion 
inserted against presence of "ptrauth-indirect-gotos"). A drawback of such 
approach is that we might occasionally forget to check if indirect gotos are 
signed before calling get discr function, but a benefit is that function names 
will be more expressive IMHO - `getPtrAuthBlockAddressDiscriminator` will only 
get the discriminator and it'll not have a responsibility to check if the 
signing is enabled.

Feel free to ignore.

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 commented:

The changes mostly look reasonable, but there are several comments to be 
answered before the PR can be merged.

Regarding previous @efriedma-quic 's comment about computing the difference 
between two blockaddresses - I suppose it might be OK just to explicitly do not 
support this as for now and implement+test a proper error message (if it's too 
time-consuming to have full support right now).

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits


@@ -10685,6 +10689,26 @@ SDValue AArch64TargetLowering::LowerBR_JT(SDValue Op,
   return DAG.getNode(ISD::BRIND, DL, MVT::Other, JTInfo, SDValue(Dest, 0));
 }
 
+SDValue AArch64TargetLowering::LowerBRIND(SDValue Op, SelectionDAG ) const 
{
+  MachineFunction  = DAG.getMachineFunction();

kovdan01 wrote:

Nit
```suggestion
  const MachineFunction  = DAG.getMachineFunction();
```

https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [AArch64][PAC] Sign block addresses used in indirectbr. (PR #97647)

2024-07-08 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97647
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Thanks @tmatheson-arm! I'll merge this as soon as #96478 gets merged - there, 
we introduce `AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINIADDRDISC = 7`, 
so signed GOT should be bit 8. We can't use bit 8 for signed GOT right now 
since the implementation relies on contiguous set of flags - so, I'll wait for 
#96478 and change signed GOT bit position to 8.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Ping: would be glad to see feedback on the changes from those who are 
interested.

https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-05 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Ping: would be glad to see feedback on the changes from those who are 
interested.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/5] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain , const ArgList ,
 ArgStringList , bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"
+// PAUTHABI2-NOT: 

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits


@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)

kovdan01 wrote:

@DanielKristofKiss Thanks! Yes, I now see that authenticating branch 
instructions also set the `PSTATE.BTYPE`. So, I agree that we can allow 
pauthabi+bti. The codegen when `pauthabi+bti` is passed as branch protection 
looks OK on downstream:
- for functions which do not sign LR (w/o `pacibsp` in the beginning, e.g. leaf 
functions), a proper `bti` instruction is emitted in the beginning of the 
function;
- for function which sign LR (with `pacibsp` in the beginning), no additional 
`bti` instruction is emitted since `pacibsp` also has `bti` built-in, see 
`SetBTypeCompatible` in 
https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/PACIB--PACIB1716--PACIBSP--PACIBZ--PACIZB--Pointer-Authentication-Code-for-Instruction-address--using-key-B-

In mainline, codegen with `-fptrauth-returns` is not implemented yet, so I 
can't provide tests verifying generated assembly correctness right now. I'll 
add them as soon as codegen support for this flag is merged.

I've implemented a check which only allows `bti` with `pauthabi` and does not 
allow `pac-ret[+leaf,+pc,+b-key]` and `gcs`. See 
3067c934a957eed21cb3ae73404675242425e5cb

Tagging @smithp35



https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-05 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/4] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain , const ArgList ,
 ArgStringList , bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"
+// PAUTHABI2-NOT: 

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits


@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"

kovdan01 wrote:

Changed, thanks, see 1d81b91d88f4f93dc6cf9bbec0c5f7fb851f89ab

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/3] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816ed..4ed1ece22b7aac 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain , const ArgList ,
 ArgStringList , bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9a..dc63545a47a866 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"
+// PAUTHABI2-NOT: 

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-04 Thread Daniil Kovalev via cfe-commits


@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)

kovdan01 wrote:

I suggest not to support `pauthabi` in combination with other branch protection 
options as for now. Here are the reasons why.

1. `pac-ret`: this and `-fptrauth-returns` (which is enabled by 
`-mbranch-protection=pauthabi`) are intended to do similar stuff, but the 
implementation differs.

   For `-mbranch-protection=pac-ret`:
   - `sign-return-address` llvm module flag is set (and optionally 
`sign-return-address-all` with `+leaf`);
   - `PAUTH_PROLOGUE` and `PAUTH_EPILOGUE` pseudo-instructions are emitted in 
`AArch64FrameLowering::emitPrologue` and `AArch64FrameLowering::emitEpilogue`; 
these pseudos are later expanded by `AArch64PointerAuth` pass;
   - both A and B keys can be used (depending on `+b-key` and corresponding 
function attribute `sign-return-address-key` or module flag 
`sign-return-address-with-bkey`);
   - using `pc` register as additional modifier is supported with `+pc` 
(corresponding module flag is `branch-protection-pauth-lr`).

   For `-fptrauth-returns` (I'll talk about downstream Apple implementation 
since many parts are not upstreamed yet, see, for example, 
https://github.com/apple/llvm-project/commit/13f9944a4c8993f9af32dc634e5d7a08cf0394e7):
   - `ptrauth-returns` attribute is set on functions we want this to be enabled;
   - actual codegen logic is implemented in 
`AArch64FrameLowering::emitPrologue` and `AArch64FrameLowering::emitEpilogue` - 
we emit actual instructions like `pacibsp` directly there;
   - B key is always used;
   - using `pc` register as additional modifier is **not** supported.

   If we try to enable both by `-mbranch-protection=pauthabi+pac-ret`, it'll 
result in incorrect code with duplicating sign/auth instructions. For example, 
for this:

   ```
   int a() {
 return b() + c();
   }
   ```

   We get this:

   ```
   a:
 paciasp
 pacibsp
 stp x29, x30, [sp, #-32]!   // 16-byte Folded Spill
 str x19, [sp, #16]  // 8-byte Folded Spill
 mov x29, sp
 bl  b
 mov w19, w0
 bl  c
 add w0, w0, w19
 ldr x19, [sp, #16]  // 8-byte Folded Reload
 ldp x29, x30, [sp], #32 // 16-byte Folded Reload
 autiasp
 retab
   ```

   A corresponding issue was already previously opened (mistakenly in mainline 
llvm repo while it was and actually still is an issue specific for Apple 
downstream). Links:
   - the issue in mainline repo 
https://github.com/llvm/llvm-project/issues/60239;
   - thread on Apple forum regarding the issue 
https://forums.developer.apple.com/forums/thread/724568;
   - the issue on Apple feedback portal (I was unable to open that actually but 
the link should be correct) 
https://feedbackassistant.apple.com/feedback/1196543.
 
   I'll probably re-open the issue in mainline repo when codegen support for 
`ptrauth-returns` is upstreamed. Alternatively, the Apple's downstream 
implementation for return address signing might be dropped since `pac-ret` 
seems to be more complete, and we can use `-fptrauth-returns` for setting the 
same return address signing options as `pac-ret+b-key`. Tagging @ahmedbougacha.

2. `+leaf` and `+pc`: these are only allowed with `pac-ret`, and while it's not 
clear how we'll resolve collisions between `pac-ret` and `ptrauth-returns` 
(which is part of `pauthabi`), it's probably better to just disallow 
`pauthabi+leaf` and `pauthabi+pc`.

3. `+b-key`: `ptrauth-returns` (which is part of `pauthabi`) already uses B key 
by default (but the codegen support is still present only in Apple downstream, 
see 
https://github.com/apple/llvm-project/commit/13f9944a4c8993f9af32dc634e5d7a08cf0394e7)

4. `gcs`: as far as I understand, guarded control stack is smth like what is 
usually called shadow stack. I'm not sure how it's supposed to work with return 
address signing - probably, these shouldn't be used together, so, since 
`pauthabi` implies return address signing, disallow `pauthabi+gcs`.

5. `bti`: depending on operand value (`c`, `j` or `jc`), the `bti` instruction 
inserted at beginning of valid call/jump targets checks that `PSTATE.BTYPE` 
matches the value set by `blr` and/or `br` instructions (see 
https://developer.arm.com/documentation/100076/0100/A64-Instruction-Set-Reference/A64-General-Instructions/BTI).
 

[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits


@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,

kovdan01 wrote:

> See addOptInFlag.

Do you mean that we need to replace

```
  if (!DriverArgs.hasArg(options::OPT_fptrauth_xxx,
 options::OPT_fno_ptrauth_xxx))
CC1Args.push_back("-fptrauth-xxx");
```

with

```
  DriverArgs.addOptInFlag(CC1Args, options::OPT_fptrauth_xxx,
  options::OPT_fno_ptrauth_xxx);
```

...?

If so, this does not look correct - `addOptInFlag` would add the flag present 
(if any) in `DriverArgs` to `CC1Args`, but we want to append a list of ptrauth 
flags to cc1 args unconditionally if `pauthabi` is passed as branch protection.

Do I miss smth? I suppose I might have misunderstood you point.

> But the implementation seems quite different from the title/description.

Hmm, it actually looks consistent to me, the implementation seems matching the 
description from my point of view - we want to add a bunch of arguments with 
`-mbranch-protection=pauthabi` used as a shortcut, we do that. Could you please 
describe what is inconsistent between description and implementation in a bit 
more detail?

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits


@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,

kovdan01 wrote:

I've added the comment, thanks! See fcd090caac9ede6b915db991819298bed4a5d44e

https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-03 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/97237

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH 1/2] [PAC][Driver] Implement `-mbranch-protection=pauthabi`
 option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816e..4ed1ece22b7aa 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain , const ArgList ,
 ArgStringList , bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9..dc63545a47a86 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   -fno-ptrauth-init-fini %s 2>&1 | FileCheck %s 
--check-prefix=PAUTHABI2
+// PAUTHABI2-NOT: "-fptrauth-intrinsics"
+// PAUTHABI2-NOT: "-fptrauth-calls"
+// PAUTHABI2-NOT: "-fptrauth-returns"
+// PAUTHABI2-NOT: "-fptrauth-auth-traps"
+// PAUTHABI2-NOT: 

[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay @ahmedbougacha @asl I suggest to move the discussion on 
comma-separated flags/current flags to an issue #97320. This PR is intended to 
introduce the signed GOT flag, and we already have a bunch of similar ptrauth 
flags, so, if we want to change the flags to a single comma-separated value, we 
need to do that as a separate refactoring PR but not here. As for this PR, I 
suggest to stick with old convention not to make this a blocker.

@MaskRay please let me know if you have other issues with proposed changes 
except suggestion to use comma-separated flags.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-07-01 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@ahmedbougacha 

> I should mention that in our world we generally don't expect these to be 
> common (other than in cc1 invocations), and they're generally used for 
> overriding default ABI behavior inferred from triples and deployment targets 
> and whatnot.

We also don't expect usage of these options to be common - instead, we propose 
`-mbranch-protection=pauthabi` option which enables a pre-defined set of 
ptrauth flags, see #97237.

I think that the main rationale for using comma-separated flags instead of a 
bunch of different flags is that it'll reduce unneeded `-fptrauth-` duplication 
- such duplication is probably undesirable even in cc1 invocations.

> and it should be doable to support the long spellings on top of these.

Regarding that: I'm not sure if it's a good idea to support both `-fptrauth-xxx 
-fptrauth-yyy` and `-fptrauth=xxx,yyy`. It'll result in additional logic for 
handling conflicts if the same flags are defined in different ways. It's 
probably not too complex, but it'll make things more messy, and I don't think 
it's what we are trying to achive.

https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Would be glad to see your feedback on the changes

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Would be glad to see feedback on the changes from those who are interested.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-07-01 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

Would be glad to see feedback on the changes from those who are interested.

https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-06-30 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 milestoned 
https://github.com/llvm/llvm-project/pull/97237
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option (PR #97237)

2024-06-30 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/97237

Enable the following ptrauth flags when `pauthabi` is passed as branch 
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 

>From 3b4b1b1739b810d758e68f30c48b648963cff740 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Mon, 1 Jul 2024 00:50:21 +0300
Subject: [PATCH] [PAC][Driver] Implement `-mbranch-protection=pauthabi` option

Enable the following ptrauth flags when `pauthabi` is passed as branch
protection:

- `intrinsics`;
- `calls`;
- `returns`;
- `auth-traps`;
- `vtable-pointer-address-discrimination`;
- `vtable-pointer-type-discrimination`;
- `init-fini`.

Co-authored-by: Anatoly Trosinenko 
---
 clang/lib/Driver/ToolChains/Clang.cpp | 38 +++
 clang/test/Driver/aarch64-ptrauth.c   | 36 ++
 .../llvm/TargetParser/ARMTargetParserCommon.h |  1 +
 .../TargetParser/ARMTargetParserCommon.cpp|  6 ++-
 4 files changed, 72 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 1b7cc82ea816e..4ed1ece22b7aa 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1484,6 +1484,39 @@ void AddUnalignedAccessWarning(ArgStringList ) {
 }
 }
 
+static void handlePAuthABIOption(const ArgList ,
+ ArgStringList , const Driver ) {
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_intrinsics,
+ options::OPT_fno_ptrauth_intrinsics))
+CC1Args.push_back("-fptrauth-intrinsics");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_calls,
+ options::OPT_fno_ptrauth_calls))
+CC1Args.push_back("-fptrauth-calls");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_returns,
+ options::OPT_fno_ptrauth_returns))
+CC1Args.push_back("-fptrauth-returns");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_auth_traps,
+ options::OPT_fno_ptrauth_auth_traps))
+CC1Args.push_back("-fptrauth-auth-traps");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_address_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_address_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-address-discrimination");
+
+  if (!DriverArgs.hasArg(
+  options::OPT_fptrauth_vtable_pointer_type_discrimination,
+  options::OPT_fno_ptrauth_vtable_pointer_type_discrimination))
+CC1Args.push_back("-fptrauth-vtable-pointer-type-discrimination");
+
+  if (!DriverArgs.hasArg(options::OPT_fptrauth_init_fini,
+ options::OPT_fno_ptrauth_init_fini))
+CC1Args.push_back("-fptrauth-init-fini");
+}
+
 static void CollectARMPACBTIOptions(const ToolChain , const ArgList ,
 ArgStringList , bool isAArch64) {
   const Arg *A = isAArch64
@@ -1537,11 +1570,16 @@ static void CollectARMPACBTIOptions(const ToolChain 
, const ArgList ,
 if (!isAArch64 && PBP.Key == "b_key")
   D.Diag(diag::warn_unsupported_branch_protection)
   << "b-key" << A->getAsString(Args);
+if (!isAArch64 && PBP.HasPauthABI)
+  D.Diag(diag::warn_unsupported_branch_protection)
+  << "pauthabi" << A->getAsString(Args);
 Scope = PBP.Scope;
 Key = PBP.Key;
 BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
 IndirectBranches = PBP.BranchTargetEnforcement;
 GuardedControlStack = PBP.GuardedControlStack;
+if (isAArch64 && PBP.HasPauthABI)
+  handlePAuthABIOption(Args, CmdArgs, D);
   }
 
   CmdArgs.push_back(
diff --git a/clang/test/Driver/aarch64-ptrauth.c 
b/clang/test/Driver/aarch64-ptrauth.c
index fa0125f4b22a9..dc63545a47a86 100644
--- a/clang/test/Driver/aarch64-ptrauth.c
+++ b/clang/test/Driver/aarch64-ptrauth.c
@@ -13,13 +13,33 @@
 // RUN:   %s 2>&1 | FileCheck %s --check-prefix=ALL
 // ALL: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
 
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi %s 2>&1 | 
\
+// RUN:   FileCheck %s --check-prefix=PAUTHABI1
+// PAUTHABI1: "-cc1"{{.*}} "-fptrauth-intrinsics" "-fptrauth-calls" 
"-fptrauth-returns" "-fptrauth-auth-traps" 
"-fptrauth-vtable-pointer-address-discrimination" 
"-fptrauth-vtable-pointer-type-discrimination" "-fptrauth-init-fini"
+
+// RUN: %clang -### -c --target=aarch64 -mbranch-protection=pauthabi 
-fno-ptrauth-intrinsics \
+// RUN:   -fno-ptrauth-calls -fno-ptrauth-returns -fno-ptrauth-auth-traps \
+// RUN:   -fno-ptrauth-vtable-pointer-address-discrimination 
-fno-ptrauth-vtable-pointer-type-discrimination \
+// RUN:   

[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,

kovdan01 wrote:

This would be nice, thanks

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,
+ QualType QT) {
+  // FIXME: Consider address space qualifiers.
+  const Type *T = QT.getCanonicalType().getTypePtr();
+
+  // FIXME: Consider using the C++ type mangling when we encounter a construct
+  // that is incompatible with C.
+
+  switch (T->getTypeClass()) {
+  case Type::Atomic:
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getValueType());
+
+  case Type::LValueReference:
+OS << "R";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+  case Type::RValueReference:
+OS << "O";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+
+  case Type::Pointer:
+// C11 6.7.6.1p2:
+//   For two pointer types to be compatible, both shall be identically
+//   qualified and both shall be pointers to compatible types.
+// FIXME: we should also consider pointee types.
+OS << "P";
+return;
+
+  case Type::ObjCObjectPointer:
+  case Type::BlockPointer:
+OS << "P";
+return;
+
+  case Type::Complex:
+OS << "C";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::VariableArray:
+  case Type::ConstantArray:
+  case Type::IncompleteArray:
+  case Type::ArrayParameter:
+// C11 6.7.6.2p6:
+//   For two array types to be compatible, both shall have compatible
+//   element types, and if both size specifiers are present, and are 
integer
+//   constant expressions, then both size specifiers shall have the same
+//   constant value [...]
+//
+// So since ElemType[N] has to be compatible ElemType[], we can't encode 
the
+// width of the array.
+OS << "A";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::ObjCInterface:
+  case Type::ObjCObject:
+OS << "";
+return;
+
+  case Type::Enum:
+// C11 6.7.2.2p4:
+//   Each enumerated type shall be compatible with char, a signed integer
+//   type, or an unsigned integer type.
+//
+// So we have to treat enum types as integers.
+OS << "i";
+return;
+
+  case Type::FunctionNoProto:
+  case Type::FunctionProto: {
+// C11 6.7.6.3p15:
+//   For two function types to be compatible, both shall specify compatible
+//   return types. Moreover, the parameter type lists, if both are present,
+//   shall agree in the number of parameters and in the use of the ellipsis
+//   terminator; corresponding parameters shall have compatible types.
+//
+// That paragraph goes on to describe how unprototyped functions are to be
+// handled, which we ignore here. Unprototyped function pointers are hashed
+// as though they were prototyped nullary functions since thats probably
+// what the user meant. This behavior is non-conforming.
+// FIXME: If we add a "custom discriminator" function type attribute we
+// should encode functions as their discriminators.
+OS << "F";
+auto *FuncType = cast(T);
+encodeTypeForFunctionPointerAuth(Ctx, OS, FuncType->getReturnType());
+if (auto *FPT = dyn_cast(FuncType)) {
+  for (QualType Param : FPT->param_types()) {
+Param = Ctx.getSignatureParameterType(Param);
+encodeTypeForFunctionPointerAuth(Ctx, OS, Param);
+  }
+  if (FPT->isVariadic())
+OS << "z";
+}
+OS << "E";
+return;
+  }
+
+  case Type::MemberPointer: {
+OS << "M";
+auto *MPT = T->getAs();
+encodeTypeForFunctionPointerAuth(Ctx, OS, QualType(MPT->getClass(), 0));
+

[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -470,6 +470,9 @@ ENUM_LANGOPT(StrictFlexArraysLevel, 
StrictFlexArraysLevelKind, 2,
 
 COMPATIBLE_VALUE_LANGOPT(MaxTokens, 32, 0, "Max number of tokens per TU or 0")
 
+BENIGN_LANGOPT(PointerAuthFunctionTypeDiscrimination, 1, 0,

kovdan01 wrote:

Can we use `LANGOPT` just as we use for similar `PointerAuth*` options above? 
Also, moving this definition to similar ones above would probably make sense so 
they are grouped together visually.


https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -15,6 +15,7 @@
 #include "CodeGenModule.h"
 #include "clang/CodeGen/CodeGenABITypes.h"
 #include "clang/CodeGen/ConstantInitBuilder.h"
+#include "llvm/Analysis/ValueTracking.h"

kovdan01 wrote:

Is this include needed?

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -4779,6 +4783,9 @@ bool CompilerInvocation::CreateFromArgsImpl(
   if (Res.getFrontendOpts().ProgramAction == frontend::RewriteObjC)
 LangOpts.ObjCExceptions = 1;
 
+  LangOpts.FunctionPointerTypeDiscrimination =

kovdan01 wrote:

Can we move this to `ParsePointerAuthArgs`?

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,

kovdan01 wrote:

```suggestion
static void encodeTypeForFunctionPointerAuth(const ASTContext , raw_ostream 
,
```

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -2220,6 +2220,11 @@ llvm::Constant 
*ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
 
   // The assertions here are all checked by Sema.
   assert(Result.Val.isLValue());
+  auto *Base = Result.Val.getLValueBase().get();
+  if (auto *Decl = dyn_cast_or_null(Base)) {

kovdan01 wrote:

nit
```suggestion
  if (const auto *Decl = dyn_cast_or_null(Base)) {
```

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,
+ QualType QT) {
+  // FIXME: Consider address space qualifiers.
+  const Type *T = QT.getCanonicalType().getTypePtr();
+
+  // FIXME: Consider using the C++ type mangling when we encounter a construct
+  // that is incompatible with C.
+
+  switch (T->getTypeClass()) {
+  case Type::Atomic:
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getValueType());
+
+  case Type::LValueReference:
+OS << "R";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+  case Type::RValueReference:
+OS << "O";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+
+  case Type::Pointer:
+// C11 6.7.6.1p2:
+//   For two pointer types to be compatible, both shall be identically
+//   qualified and both shall be pointers to compatible types.
+// FIXME: we should also consider pointee types.
+OS << "P";
+return;
+
+  case Type::ObjCObjectPointer:
+  case Type::BlockPointer:
+OS << "P";
+return;
+
+  case Type::Complex:
+OS << "C";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::VariableArray:
+  case Type::ConstantArray:
+  case Type::IncompleteArray:
+  case Type::ArrayParameter:
+// C11 6.7.6.2p6:
+//   For two array types to be compatible, both shall have compatible
+//   element types, and if both size specifiers are present, and are 
integer
+//   constant expressions, then both size specifiers shall have the same
+//   constant value [...]
+//
+// So since ElemType[N] has to be compatible ElemType[], we can't encode 
the
+// width of the array.
+OS << "A";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::ObjCInterface:
+  case Type::ObjCObject:
+OS << "";
+return;
+
+  case Type::Enum:
+// C11 6.7.2.2p4:
+//   Each enumerated type shall be compatible with char, a signed integer
+//   type, or an unsigned integer type.
+//
+// So we have to treat enum types as integers.
+OS << "i";
+return;
+
+  case Type::FunctionNoProto:
+  case Type::FunctionProto: {
+// C11 6.7.6.3p15:
+//   For two function types to be compatible, both shall specify compatible
+//   return types. Moreover, the parameter type lists, if both are present,
+//   shall agree in the number of parameters and in the use of the ellipsis
+//   terminator; corresponding parameters shall have compatible types.
+//
+// That paragraph goes on to describe how unprototyped functions are to be
+// handled, which we ignore here. Unprototyped function pointers are hashed
+// as though they were prototyped nullary functions since thats probably
+// what the user meant. This behavior is non-conforming.
+// FIXME: If we add a "custom discriminator" function type attribute we
+// should encode functions as their discriminators.
+OS << "F";
+auto *FuncType = cast(T);
+encodeTypeForFunctionPointerAuth(Ctx, OS, FuncType->getReturnType());
+if (auto *FPT = dyn_cast(FuncType)) {
+  for (QualType Param : FPT->param_types()) {
+Param = Ctx.getSignatureParameterType(Param);
+encodeTypeForFunctionPointerAuth(Ctx, OS, Param);
+  }
+  if (FPT->isVariadic())
+OS << "z";
+}
+OS << "E";
+return;
+  }
+
+  case Type::MemberPointer: {
+OS << "M";
+auto *MPT = T->getAs();

kovdan01 wrote:

nit
```suggestion
const auto *MPT = T->getAs();
```


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -5,55 +5,65 @@
 // RUN:   -fptrauth-vtable-pointer-address-discrimination \
 // RUN:   -fptrauth-vtable-pointer-type-discrimination \
 // RUN:   -fptrauth-init-fini | \
-// RUN:   FileCheck %s 
--check-prefixes=INTRIN,CALLS,RETS,VPTR_ADDR_DISCR,VPTR_TYPE_DISCR,INITFINI
+// RUN:   FileCheck %s 
--check-prefixes=INTRIN,CALLS,RETS,VPTR_ADDR_DISCR,VPTR_TYPE_DISCR,INITFINI,NOFUNC

kovdan01 wrote:

Nit: we have test cases for everything enabled and for 
everything-except-one-feature enabled, so, to keep things consistent, it's 
probably better to have `-fptrauth-function-pointer-type-discrimination` passed 
for all test cases except one. Alternatively, we can test 
nothing-enabled-except-one-feature, it'll result in more compact RUN lines.

Current variant is also OK in terms of testing that things work properly, so I 
don't mind leaving this "as is".

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,
+ QualType QT) {
+  // FIXME: Consider address space qualifiers.
+  const Type *T = QT.getCanonicalType().getTypePtr();
+
+  // FIXME: Consider using the C++ type mangling when we encounter a construct
+  // that is incompatible with C.
+
+  switch (T->getTypeClass()) {
+  case Type::Atomic:
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getValueType());
+
+  case Type::LValueReference:
+OS << "R";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+  case Type::RValueReference:
+OS << "O";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+
+  case Type::Pointer:
+// C11 6.7.6.1p2:
+//   For two pointer types to be compatible, both shall be identically
+//   qualified and both shall be pointers to compatible types.
+// FIXME: we should also consider pointee types.
+OS << "P";
+return;
+
+  case Type::ObjCObjectPointer:
+  case Type::BlockPointer:
+OS << "P";
+return;
+
+  case Type::Complex:
+OS << "C";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::VariableArray:
+  case Type::ConstantArray:
+  case Type::IncompleteArray:
+  case Type::ArrayParameter:
+// C11 6.7.6.2p6:
+//   For two array types to be compatible, both shall have compatible
+//   element types, and if both size specifiers are present, and are 
integer
+//   constant expressions, then both size specifiers shall have the same
+//   constant value [...]
+//
+// So since ElemType[N] has to be compatible ElemType[], we can't encode 
the
+// width of the array.
+OS << "A";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::ObjCInterface:
+  case Type::ObjCObject:
+OS << "";
+return;
+
+  case Type::Enum:
+// C11 6.7.2.2p4:
+//   Each enumerated type shall be compatible with char, a signed integer
+//   type, or an unsigned integer type.
+//
+// So we have to treat enum types as integers.
+OS << "i";
+return;
+
+  case Type::FunctionNoProto:
+  case Type::FunctionProto: {
+// C11 6.7.6.3p15:
+//   For two function types to be compatible, both shall specify compatible
+//   return types. Moreover, the parameter type lists, if both are present,
+//   shall agree in the number of parameters and in the use of the ellipsis
+//   terminator; corresponding parameters shall have compatible types.
+//
+// That paragraph goes on to describe how unprototyped functions are to be
+// handled, which we ignore here. Unprototyped function pointers are hashed
+// as though they were prototyped nullary functions since thats probably
+// what the user meant. This behavior is non-conforming.
+// FIXME: If we add a "custom discriminator" function type attribute we
+// should encode functions as their discriminators.
+OS << "F";
+auto *FuncType = cast(T);
+encodeTypeForFunctionPointerAuth(Ctx, OS, FuncType->getReturnType());
+if (auto *FPT = dyn_cast(FuncType)) {
+  for (QualType Param : FPT->param_types()) {
+Param = Ctx.getSignatureParameterType(Param);
+encodeTypeForFunctionPointerAuth(Ctx, OS, Param);
+  }
+  if (FPT->isVariadic())
+OS << "z";
+}
+OS << "E";
+return;
+  }
+
+  case Type::MemberPointer: {
+OS << "M";
+auto *MPT = T->getAs();
+encodeTypeForFunctionPointerAuth(Ctx, OS, QualType(MPT->getClass(), 0));
+

[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -470,6 +470,9 @@ ENUM_LANGOPT(StrictFlexArraysLevel, 
StrictFlexArraysLevelKind, 2,
 
 COMPATIBLE_VALUE_LANGOPT(MaxTokens, 32, 0, "Max number of tokens per TU or 0")
 
+BENIGN_LANGOPT(FunctionPointerTypeDiscrimination, 1, 0,

kovdan01 wrote:

Can we use `LANGOPT`, just as we use for other similar `PointerAuth*` options 
above? Also, moving this definition to similar ones would probably make sense.

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -3140,6 +3140,269 @@ 
ASTContext::getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD) {
   return llvm::getPointerAuthStableSipHash(Str);
 }
 
+/// Encode a function type for use in the discriminator of a function pointer
+/// type. We can't use the itanium scheme for this since C has quite permissive
+/// rules for type compatibility that we need to be compatible with.
+///
+/// Formally, this function associates every function pointer type T with an
+/// encoded string E(T). Let the equivalence relation T1 ~ T2 be defined as
+/// E(T1) == E(T2). E(T) is part of the ABI of values of type T. C type
+/// compatibility requires equivalent treatment under the ABI, so
+/// CCompatible(T1, T2) must imply E(T1) == E(T2), that is, CCompatible must be
+/// a subset of ~. Crucially, however, it must be a proper subset because
+/// CCompatible is not an equivalence relation: for example, int[] is 
compatible
+/// with both int[1] and int[2], but the latter are not compatible with each
+/// other. Therefore this encoding function must be careful to only distinguish
+/// types if there is no third type with which they are both required to be
+/// compatible.
+static void encodeTypeForFunctionPointerAuth(ASTContext , raw_ostream ,
+ QualType QT) {
+  // FIXME: Consider address space qualifiers.
+  const Type *T = QT.getCanonicalType().getTypePtr();
+
+  // FIXME: Consider using the C++ type mangling when we encounter a construct
+  // that is incompatible with C.
+
+  switch (T->getTypeClass()) {
+  case Type::Atomic:
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getValueType());
+
+  case Type::LValueReference:
+OS << "R";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+  case Type::RValueReference:
+OS << "O";
+encodeTypeForFunctionPointerAuth(Ctx, OS,
+ cast(T)->getPointeeType());
+return;
+
+  case Type::Pointer:
+// C11 6.7.6.1p2:
+//   For two pointer types to be compatible, both shall be identically
+//   qualified and both shall be pointers to compatible types.
+// FIXME: we should also consider pointee types.
+OS << "P";
+return;
+
+  case Type::ObjCObjectPointer:
+  case Type::BlockPointer:
+OS << "P";
+return;
+
+  case Type::Complex:
+OS << "C";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::VariableArray:
+  case Type::ConstantArray:
+  case Type::IncompleteArray:
+  case Type::ArrayParameter:
+// C11 6.7.6.2p6:
+//   For two array types to be compatible, both shall have compatible
+//   element types, and if both size specifiers are present, and are 
integer
+//   constant expressions, then both size specifiers shall have the same
+//   constant value [...]
+//
+// So since ElemType[N] has to be compatible ElemType[], we can't encode 
the
+// width of the array.
+OS << "A";
+return encodeTypeForFunctionPointerAuth(
+Ctx, OS, cast(T)->getElementType());
+
+  case Type::ObjCInterface:
+  case Type::ObjCObject:
+OS << "";
+return;
+
+  case Type::Enum:
+// C11 6.7.2.2p4:
+//   Each enumerated type shall be compatible with char, a signed integer
+//   type, or an unsigned integer type.
+//
+// So we have to treat enum types as integers.
+OS << "i";
+return;
+
+  case Type::FunctionNoProto:
+  case Type::FunctionProto: {
+// C11 6.7.6.3p15:
+//   For two function types to be compatible, both shall specify compatible
+//   return types. Moreover, the parameter type lists, if both are present,
+//   shall agree in the number of parameters and in the use of the ellipsis
+//   terminator; corresponding parameters shall have compatible types.
+//
+// That paragraph goes on to describe how unprototyped functions are to be
+// handled, which we ignore here. Unprototyped function pointers are hashed
+// as though they were prototyped nullary functions since thats probably
+// what the user meant. This behavior is non-conforming.
+// FIXME: If we add a "custom discriminator" function type attribute we
+// should encode functions as their discriminators.
+OS << "F";
+auto *FuncType = cast(T);

kovdan01 wrote:

nit
```suggestion
const auto *FuncType = cast(T);
```

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -2220,6 +2220,11 @@ llvm::Constant 
*ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
 
   // The assertions here are all checked by Sema.
   assert(Result.Val.isLValue());
+  auto *Base = Result.Val.getLValueBase().get();

kovdan01 wrote:

nit
```suggestion
  const auto *Base = Result.Val.getLValueBase().get();
```

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -1283,6 +1283,9 @@ class ASTContext : public RefCountedBase {
   uint16_t
   getPointerAuthVTablePointerDiscriminator(const CXXRecordDecl *RD);
 
+  /// Return the "other" type-specific discriminator for the given type.
+  uint16_t getPointerAuthTypeDiscriminator(QualType T);

kovdan01 wrote:

nit
```suggestion
  uint16_t getPointerAuthTypeDiscriminator(QualType T) const;
```

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 approved this pull request.

LGTM with minor mostly cosmetic comments. I'm OK with fixing them in a separate 
PR if it's too time-consuming now.

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement function pointer type discrimination (PR #96992)

2024-06-28 Thread Daniil Kovalev via cfe-commits


@@ -4228,6 +4228,8 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   OptInCC1FFlag<"ptrauth-vtable-pointer-type-discrimination", "Enable type 
discrimination of vtable pointers">;
 defm ptrauth_init_fini : OptInCC1FFlag<"ptrauth-init-fini", "Enable signing of 
function pointers in init/fini arrays">;
+defm ptrauth_function_pointer_type_discrimination : 
OptInCC1FFlag<"ptrauth-function-pointer-type-discrimination",
+  "Enabling type discrimination on C function pointers">;

kovdan01 wrote:

Nit: above, "Enable" is used instead of "Enabling". Consider using "Enable" 
here as well to keep things consistent. Feel free to ignore though.

https://github.com/llvm/llvm-project/pull/96992
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96478

>From 5399237a71c0ccd872821034d83ea2c3a04bed3f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH 1/2] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 ++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 +--
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 ++-
 clang/test/CodeGen/ptrauth-init-fini.c| 39 ++
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 +
 compiler-rt/lib/builtins/crtbegin.c   | 16 
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../test/CodeGen/AArch64/ptrauth-init-fini.ll | 77 +++
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 ++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 18 files changed, 268 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c
 create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index c529cc9506667..5b8d4139d975b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4228,6 +4228,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   

[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96478

>From 5399237a71c0ccd872821034d83ea2c3a04bed3f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH 1/3] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 ++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 +--
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 ++-
 clang/test/CodeGen/ptrauth-init-fini.c| 39 ++
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 +
 compiler-rt/lib/builtins/crtbegin.c   | 16 
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../test/CodeGen/AArch64/ptrauth-init-fini.ll | 77 +++
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 ++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 18 files changed, 268 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c
 create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index c529cc9506667..5b8d4139d975b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4228,6 +4228,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   

[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

The PR no longer depends on #96477. For signed ctors/dtors we now have special 
address discrimination handling so we don't need to use self-references to 
`llvm.global_{c|d}tors` with giant `getelementptr` expressions - such 
references are disallowed and, as discussed in #96477, allowing them just for 
ptrauth purposes has significant drawbacks.

Instead, just use `ptr inttoptr (i64 1 to ptr)`, like this:

```
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr 
} { i32 65535, ptr ptrauth (ptr @foo, i32 0, i64 55764, ptr inttoptr (i64 1 to 
ptr)), ptr null }]
```

This is the same approach which is used in Apple's downstream for coroutines, 
and we share some code with that - see 
https://github.com/ahmedbougacha/llvm-project/commit/2a5528ea2cb223fca0b47f0a4833bbb938cf0748.
 Also tagging @ahmedbougacha.



https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-28 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96478

>From 5399237a71c0ccd872821034d83ea2c3a04bed3f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH 1/2] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 ++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 +--
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 ++-
 clang/test/CodeGen/ptrauth-init-fini.c| 39 ++
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 +
 compiler-rt/lib/builtins/crtbegin.c   | 16 
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../test/CodeGen/AArch64/ptrauth-init-fini.ll | 77 +++
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 ++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 18 files changed, 268 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c
 create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index c529cc9506667..5b8d4139d975b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4228,6 +4228,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   

[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-27 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96159

>From 4eeb1b4e82941681b6cafda8579d136e3e7cb09f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 18 Jun 2024 15:37:18 +0300
Subject: [PATCH 1/2] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core
 info

Treat 7th bit of version value for llvm_linux platform as signed GOT flag.

- clang: define `PointerAuthELFGOT` LangOption and set 7th bit of
  `aarch64-elf-pauthabi-version` LLVM module flag correspondingly;

- llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version
  description of llvm_linux platform depending on whether the flag is set.
---
 clang/include/clang/Basic/LangOptions.def  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp|  6 --
 llvm/include/llvm/BinaryFormat/ELF.h   |  3 ++-
 .../AArch64/note-gnu-property-elf-pauthabi.ll  |  2 +-
 .../ELF/AArch64/aarch64-feature-pauth.s| 18 +-
 llvm/tools/llvm-readobj/ELFDumper.cpp  |  3 ++-
 6 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..bc99dad5cd55e 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthELFGOT, 1, 0, "authenticate pointers from GOT")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index dd4a665ebc78b..feac291e01b50 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1210,8 +1210,10 @@ void CodeGenModule::Release() {
   (LangOpts.PointerAuthVTPtrTypeDiscrimination
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
   (LangOpts.PointerAuthInitFini
-   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI);
-  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI ==
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI) |
+  (LangOpts.PointerAuthELFGOT
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT);
+  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT ==
 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
 "Update when new enum items are defined");
   if (PAuthABIVersion != 0) {
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index dfba180149916..2aa37bbed6656 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1774,8 +1774,9 @@ enum : unsigned {
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR = 4,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR = 5,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI = 6,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT = 7,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST =
-  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT,
 };
 
 // x86 processor feature bits.
diff --git a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll 
b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
index 728cffeba02a2..fb69a12b2f906 100644
--- a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
+++ b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
@@ -27,7 +27,7 @@
 ; OBJ: Displaying notes found in: .note.gnu.property
 ; OBJ-NEXT:   Owner Data size  Description
 ; OBJ-NEXT:   GNU   0x0018 NT_GNU_PROPERTY_TYPE_0 
(property note)
-; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 (PointerAuthIntrinsics, !PointerAuthCalls, PointerAuthReturns, 
!PointerAuthAuthTraps, PointerAuthVTPtrAddressDiscrimination, 
!PointerAuthVTPtrTypeDiscrimination, PointerAuthInitFini)
+; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 (PointerAuthIntrinsics, !PointerAuthCalls, PointerAuthReturns, 
!PointerAuthAuthTraps, PointerAuthVTPtrAddressDiscrimination, 
!PointerAuthVTPtrTypeDiscrimination, PointerAuthInitFini, !PointerAuthELFGOT)
 
 ; ERR: either both or no 'aarch64-elf-pauthabi-platform' and 
'aarch64-elf-pauthabi-version' module flags must be present
 
diff --git 

[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-27 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

> I was referring to this line from the description:
> 
> > llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version 
> > description of llvm_linux platform depending on whether the flag is set.
> 
> In my opinion, if you don't test the first of those two cases, you might as 
> well not have implemented behaviour for it. I'd always test "all flags set" 
> and "no flags set" cases (or some variant that effectively tests this, e.g. 
> 0xff and ~0xff). Of course, if it's not practical, that's fine.
> 
> To be clear, I'm not suggesting testing every possible combination of flags, 
> just each flag individually set/not set.

@jh7370 Thanks for explanation! It's a reasonable point, I'll add corresponding 
test cases, thanks.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-27 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

> I'm not at all familiar with this PAuth stuff, but don't you need a test case 
> for where the new value is set (currently they all seem to be unset, if I'm 
> interpreting things correctly)?

I'm not sure if I understood your question correctly - particularly, I'm not 
sure what does the phrase "the new value is set" mean. Could you please add a 
bit more details in your question?

If you are talking about 
llvm/test/tools/llvm-readobj/ELF/AArch64/aarch64-feature-pauth.s and 
llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll tests checking 
version value 0x55 which does not imply signed GOT enabled, we just can't test 
2^8=256 combinations of flags, so we test values which look like 0b10101... But 
I can add a test for version value 0xAA which would set opposite flags compared 
to 0x55.

https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 milestoned 
https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

> Can we have LLVM IR test as well?

I've added llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll, thanks for 
suggestion.

https://github.com/llvm/llvm-project/pull/96478
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96478

>From 5399237a71c0ccd872821034d83ea2c3a04bed3f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 ++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 +--
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 ++-
 clang/test/CodeGen/ptrauth-init-fini.c| 39 ++
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 +
 compiler-rt/lib/builtins/crtbegin.c   | 16 
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../test/CodeGen/AArch64/ptrauth-init-fini.ll | 77 +++
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 ++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 18 files changed, 268 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c
 create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index c529cc9506667..5b8d4139d975b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4228,6 +4228,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   

[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/96478

>From e5eeab35a842453d97bd2bf8763efa426aead3e8 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 ++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 +--
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 ++-
 clang/test/CodeGen/ptrauth-init-fini.c| 45 +++
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 +
 compiler-rt/lib/builtins/crtbegin.c   | 16 
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../test/CodeGen/AArch64/ptrauth-init-fini.ll | 77 +++
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 ++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 18 files changed, 274 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c
 create mode 100644 llvm/test/CodeGen/AArch64/ptrauth-init-fini.ll

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index c529cc9506667..5b8d4139d975b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4228,6 +4228,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   

[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 milestoned 
https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 milestoned 
https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] [PAC][AArch64] Support init/fini array signing (PR #96478)

2024-06-24 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/96478

Depends on #96477

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign function 
pointers in `llvm.global_ctors` and `llvm.global_dtors` with constant 
discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`). 
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed, 
address discrimination is used for signing (otherwise, just constant 
discriminator is used).

>From aeece525a037c227018013267ecb30c83e420c57 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Fri, 21 Jun 2024 12:32:51 +0300
Subject: [PATCH] [PAC][AArch64] Support init/fini array signing

If both `-fptrauth-init-fini` and `-fptrauth-calls` are passed, sign
function pointers in `llvm.global_ctors` and `llvm.global_dtors` with
constant discriminator 0xD9D4 (`ptrauth_string_discriminator("init_fini")`).
Additionally, if `-fptrauth-init-fini-address-discrimination` is passed,
address discrimination is used for signing (otherwise, just constant
discriminator is used).
---
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Basic/LangOptions.def |  1 +
 .../include/clang/Basic/PointerAuthOptions.h  |  7 +++
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 63 ---
 clang/lib/Driver/ToolChains/Clang.cpp |  3 +
 clang/lib/Frontend/CompilerInvocation.cpp |  9 +++
 clang/lib/Headers/ptrauth.h   |  8 +++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 12 +++-
 clang/test/CodeGen/ptrauth-init-fini.c| 45 +
 clang/test/Driver/aarch64-ptrauth.c   |  6 +-
 clang/test/Preprocessor/ptrauth_feature.c | 52 ++-
 compiler-rt/lib/builtins/crtbegin.c   | 16 +
 llvm/include/llvm/BinaryFormat/ELF.h  |  3 +-
 .../AArch64/note-gnu-property-elf-pauthabi.ll |  2 +-
 .../ELF/AArch64/aarch64-feature-pauth.s   | 18 +++---
 llvm/tools/llvm-readobj/ELFDumper.cpp |  4 +-
 17 files changed, 197 insertions(+), 54 deletions(-)
 create mode 100644 clang/test/CodeGen/ptrauth-init-fini.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..5dca40b261655 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_init_fini_address_discrimination, 
LangOpts.PointerAuthInitFiniAddressDiscrimination)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..2de854731 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated function pointers in init/fini arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/include/clang/Basic/PointerAuthOptions.h 
b/clang/include/clang/Basic/PointerAuthOptions.h
index aaad4a2b2b5ae..9e2b64111e461 100644
--- a/clang/include/clang/Basic/PointerAuthOptions.h
+++ b/clang/include/clang/Basic/PointerAuthOptions.h
@@ -23,6 +23,10 @@
 
 namespace clang {
 
+/// Constant discriminator to be used with function pointers in .init_array and
+/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
+constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
+
 constexpr unsigned PointerAuthKeyNone = -1;
 
 class PointerAuthSchema {
@@ -150,6 +154,9 @@ class PointerAuthSchema {
 struct PointerAuthOptions {
   /// The ABI for C function pointers.
   PointerAuthSchema FunctionPointers;
+
+  /// The ABI for function addresses in .init_array and .fini_array
+  PointerAuthSchema InitFiniPointers;
 };
 
 } // end namespace clang
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 

[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/96160
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/96159
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [PAC][clang][Driver] Add signed GOT flag (PR #96160)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/96160

Depends on #96159

Add `-fptrauth-elf-got` clang driver flag and set `ptrauth_elf_got` 
preprocessor feature and `PointerAuthELFGOT` LangOption correspondingly. For 
non-ELF triples, the driver flag is ignored and a warning is emitted.

>From f891f791dfe882389d83d3c4c4fb57d67a845c04 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 18 Jun 2024 15:38:18 +0300
Subject: [PATCH] [PAC][clang][Driver] Add signed GOT flag

Add `-fptrauth-elf-got` clang driver flag and set `ptrauth_elf_got`
preprocessor feature and `PointerAuthELFGOT` LangOption correspondingly.
For non-ELF triples, the driver flag is ignored and a warning is emitted.
---
 .../clang/Basic/DiagnosticDriverKinds.td  |  4 ++
 clang/include/clang/Basic/Features.def|  1 +
 clang/include/clang/Driver/Options.td |  1 +
 clang/lib/Driver/ToolChains/Clang.cpp |  7 +++
 clang/lib/Frontend/CompilerInvocation.cpp |  4 ++
 clang/test/CodeGen/aarch64-elf-pauthabi.c | 11 +++-
 clang/test/Driver/aarch64-ptrauth.c   |  9 +++-
 clang/test/Preprocessor/ptrauth_feature.c | 52 ++-
 8 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 1ca2cb85565a1..28667b1eb239e 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -742,6 +742,10 @@ def warn_drv_fjmc_for_elf_only : Warning<
   "-fjmc works only for ELF; option ignored">,
   InGroup;
 
+def warn_drv_ptrauth_elf_got_for_elf_only : Warning<
+  "-fptrauth-elf-got works only for ELF; option ignored">,
+  InGroup;
+
 def warn_target_override_arm64ec : Warning<
   "/arm64EC has been overridden by specified target: %0; option ignored">,
   InGroup;
diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index 53f410d3cb4bd..569f4e1715af5 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -110,6 +110,7 @@ FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtr
 FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
 FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
 FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
+FEATURE(ptrauth_elf_got, LangOpts.PointerAuthELFGOT)
 EXTENSION(swiftcc,
   PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 112eb286eb075..e16c1a0d06a1b 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4222,6 +4222,7 @@ defm ptrauth_vtable_pointer_address_discrimination :
 defm ptrauth_vtable_pointer_type_discrimination :
   OptInCC1FFlag<"ptrauth-vtable-pointer-type-discrimination", "Enable type 
discrimination of vtable pointers">;
 defm ptrauth_init_fini : OptInCC1FFlag<"ptrauth-init-fini", "Enable signing of 
function pointers in init/fini arrays">;
+defm ptrauth_elf_got : OptInCC1FFlag<"ptrauth-elf-got", "Enable authentication 
of pointers from GOT (ELF only)">;
 }
 
 def fenable_matrix : Flag<["-"], "fenable-matrix">, Group,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 331cf6e713d89..5f55e79ec206b 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1788,6 +1788,13 @@ void Clang::AddAArch64TargetArgs(const ArgList ,
   options::OPT_fno_ptrauth_vtable_pointer_type_discrimination);
   Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_init_fini,
 options::OPT_fno_ptrauth_init_fini);
+
+  Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_elf_got,
+options::OPT_fno_ptrauth_elf_got);
+
+  if (Args.hasArg(options::OPT_fptrauth_elf_got))
+getToolChain().getDriver().Diag(
+diag::warn_drv_ptrauth_elf_got_for_elf_only);
 }
 
 void Clang::AddLoongArchTargetArgs(const ArgList ,
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 58694e5399d58..97a5408a4c1e0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3361,6 +3361,8 @@ static void GeneratePointerAuthArgs(const LangOptions 
,
 GenerateArg(Consumer, OPT_fptrauth_vtable_pointer_type_discrimination);
   if (Opts.PointerAuthInitFini)
 GenerateArg(Consumer, OPT_fptrauth_init_fini);
+  if (Opts.PointerAuthELFGOT)
+GenerateArg(Consumer, OPT_fptrauth_elf_got);
 }
 
 static void ParsePointerAuthArgs(LangOptions , ArgList ,
@@ -3374,6 +3376,7 @@ static void ParsePointerAuthArgs(LangOptions , 
ArgList ,
   Opts.PointerAuthVTPtrTypeDiscrimination =
   

[clang] [llvm] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info (PR #96159)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/96159

Depends on #96158

Treat 7th bit of version value for llvm_linux platform as signed GOT flag.

- clang: define `PointerAuthELFGOT` LangOption and set 7th bit of 
`aarch64-elf-pauthabi-version` LLVM module flag correspondingly;

- llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version 
description of llvm_linux platform depending on whether the flag is set.

>From 4eeb1b4e82941681b6cafda8579d136e3e7cb09f Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 18 Jun 2024 15:37:18 +0300
Subject: [PATCH] [PAC][ELF][AArch64] Encode signed GOT flag in PAuth core info

Treat 7th bit of version value for llvm_linux platform as signed GOT flag.

- clang: define `PointerAuthELFGOT` LangOption and set 7th bit of
  `aarch64-elf-pauthabi-version` LLVM module flag correspondingly;

- llvm-readobj: print `PointerAuthELFGOT` or `!PointerAuthELFGOT` in version
  description of llvm_linux platform depending on whether the flag is set.
---
 clang/include/clang/Basic/LangOptions.def  |  1 +
 clang/lib/CodeGen/CodeGenModule.cpp|  6 --
 llvm/include/llvm/BinaryFormat/ELF.h   |  3 ++-
 .../AArch64/note-gnu-property-elf-pauthabi.ll  |  2 +-
 .../ELF/AArch64/aarch64-feature-pauth.s| 18 +-
 llvm/tools/llvm-readobj/ELFDumper.cpp  |  3 ++-
 6 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 6dd6b5614f44c..bc99dad5cd55e 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -168,6 +168,7 @@ LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication 
failure traps")
 LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
 LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
+LANGOPT(PointerAuthELFGOT, 1, 0, "authenticate pointers from GOT")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 LANGOPT(ExperimentalLateParseAttributes, 1, 0, "experimental late parsing of 
attributes")
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index dd4a665ebc78b..feac291e01b50 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1210,8 +1210,10 @@ void CodeGenModule::Release() {
   (LangOpts.PointerAuthVTPtrTypeDiscrimination
<< AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
   (LangOpts.PointerAuthInitFini
-   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI);
-  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI ==
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI) |
+  (LangOpts.PointerAuthELFGOT
+   << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT);
+  static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT ==
 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
 "Update when new enum items are defined");
   if (PAuthABIVersion != 0) {
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index dfba180149916..2aa37bbed6656 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1774,8 +1774,9 @@ enum : unsigned {
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR = 4,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR = 5,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI = 6,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT = 7,
   AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST =
-  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI,
+  AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_GOT,
 };
 
 // x86 processor feature bits.
diff --git a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll 
b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
index 728cffeba02a2..fb69a12b2f906 100644
--- a/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
+++ b/llvm/test/CodeGen/AArch64/note-gnu-property-elf-pauthabi.ll
@@ -27,7 +27,7 @@
 ; OBJ: Displaying notes found in: .note.gnu.property
 ; OBJ-NEXT:   Owner Data size  Description
 ; OBJ-NEXT:   GNU   0x0018 NT_GNU_PROPERTY_TYPE_0 
(property note)
-; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 (PointerAuthIntrinsics, !PointerAuthCalls, PointerAuthReturns, 
!PointerAuthAuthTraps, PointerAuthVTPtrAddressDiscrimination, 
!PointerAuthVTPtrTypeDiscrimination, PointerAuthInitFini)
+; OBJ-NEXT:   AArch64 PAuth ABI core info: platform 0x1002 (llvm_linux), 
version 0x55 

[clang] [clang] Define ptrauth_string_discriminator builtin. (PR #93903)

2024-06-20 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 approved this pull request.


https://github.com/llvm/llvm-project/pull/93903
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,174 @@
+//===--- StableHash.cpp - An ABI-stable string hash 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file implements an ABI-stable string hash based on SipHash, used to
+//  compute ptrauth discriminators.
+//
+//===--===//
+
+#include "llvm/Support/SipHash.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Debug.h"
+#include 
+#include 

kovdan01 wrote:

Is `cstring` include needed? I do not see anything used from that.

https://github.com/llvm/llvm-project/pull/93902
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,174 @@
+//===--- StableHash.cpp - An ABI-stable string hash 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file implements an ABI-stable string hash based on SipHash, used to
+//  compute ptrauth discriminators.
+//
+//===--===//
+
+#include "llvm/Support/SipHash.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Debug.h"
+#include 
+#include 
+
+using namespace llvm;
+
+#define DEBUG_TYPE "llvm-siphash"
+
+//  Lightly adapted from the SipHash reference C implementation by
+//  Jean-Philippe Aumasson and Daniel J. Bernstein.
+
+#define SIPHASH_ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b
+
+#define SIPHASH_U8TO64_LE(p)   
\
+  (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) |  
\
+   ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) |   
\
+   ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) |   
\
+   ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
+
+#define SIPHASH_SIPROUND   
\
+  do { 
\
+v0 += v1;  
\
+v1 = SIPHASH_ROTL(v1, 13); 
\
+v1 ^= v0;  
\
+v0 = SIPHASH_ROTL(v0, 32); 
\
+v2 += v3;  
\
+v3 = SIPHASH_ROTL(v3, 16); 
\
+v3 ^= v2;  
\
+v0 += v3;  
\
+v3 = SIPHASH_ROTL(v3, 21); 
\
+v3 ^= v0;  
\
+v2 += v1;  
\
+v1 = SIPHASH_ROTL(v1, 17); 
\
+v1 ^= v2;  
\
+v2 = SIPHASH_ROTL(v2, 32); 
\
+  } while (0)
+
+template 
+static inline ResultTy siphash(const uint8_t *in, uint64_t inlen,
+   const uint8_t ()[16]) {
+  static_assert(sizeof(ResultTy) == 8 || sizeof(ResultTy) == 16,
+"result type should be uint64_t or uint128_t");
+  uint64_t v0 = 0x736f6d6570736575ULL;
+  uint64_t v1 = 0x646f72616e646f6dULL;
+  uint64_t v2 = 0x6c7967656e657261ULL;
+  uint64_t v3 = 0x7465646279746573ULL;
+  uint64_t b;
+  uint64_t k0 = SIPHASH_U8TO64_LE(k);
+  uint64_t k1 = SIPHASH_U8TO64_LE(k + 8);
+  uint64_t m;
+  int i;
+  const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
+  const int left = inlen & 7;
+  b = ((uint64_t)inlen) << 56;
+  v3 ^= k1;
+  v2 ^= k0;
+  v1 ^= k1;
+  v0 ^= k0;
+
+  if (sizeof(ResultTy) == 16) {
+v1 ^= 0xee;
+  }
+
+  for (; in != end; in += 8) {
+m = SIPHASH_U8TO64_LE(in);
+v3 ^= m;
+
+for (i = 0; i < cROUNDS; ++i)
+  SIPHASH_SIPROUND;
+
+v0 ^= m;
+  }
+
+  switch (left) {
+  case 7:
+b |= ((uint64_t)in[6]) << 48;
+LLVM_FALLTHROUGH;
+  case 6:
+b |= ((uint64_t)in[5]) << 40;
+LLVM_FALLTHROUGH;
+  case 5:
+b |= ((uint64_t)in[4]) << 32;
+LLVM_FALLTHROUGH;
+  case 4:
+b |= ((uint64_t)in[3]) << 24;
+LLVM_FALLTHROUGH;
+  case 3:
+b |= ((uint64_t)in[2]) << 16;
+LLVM_FALLTHROUGH;
+  case 2:
+b |= ((uint64_t)in[1]) << 8;
+LLVM_FALLTHROUGH;
+  case 1:
+b |= ((uint64_t)in[0]);
+break;
+  case 0:
+break;
+  }
+
+  v3 ^= b;
+
+  for (i = 0; i < cROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  v0 ^= b;
+
+  if (sizeof(ResultTy) == 8) {
+v2 ^= 0xff;
+  } else {
+v2 ^= 0xee;
+  }
+
+  for (i = 0; i < dROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  b = v0 ^ v1 ^ v2 ^ v3;
+
+  // This mess with the result type would be easier with 'if constexpr'.
+
+  uint64_t firstHalf = b;
+  if (sizeof(ResultTy) == 8)
+return firstHalf;
+
+  v1 ^= 0xdd;
+
+  for (i = 0; i < dROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  b = v0 ^ v1 ^ v2 ^ v3;
+  uint64_t secondHalf = b;
+
+  return firstHalf | (ResultTy(secondHalf) << (sizeof(ResultTy) == 8 ? 0 : 
64));
+}
+
+//===--- LLVM-specific wrappers around siphash.
+
+/// Compute an ABI-stable 64-bit hash of the given string.

[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,174 @@
+//===--- StableHash.cpp - An ABI-stable string hash 
---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file implements an ABI-stable string hash based on SipHash, used to
+//  compute ptrauth discriminators.
+//
+//===--===//
+
+#include "llvm/Support/SipHash.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Debug.h"
+#include 
+#include 
+
+using namespace llvm;
+
+#define DEBUG_TYPE "llvm-siphash"
+
+//  Lightly adapted from the SipHash reference C implementation by
+//  Jean-Philippe Aumasson and Daniel J. Bernstein.
+
+#define SIPHASH_ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b
+
+#define SIPHASH_U8TO64_LE(p)   
\
+  (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) |  
\
+   ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) |   
\
+   ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) |   
\
+   ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
+
+#define SIPHASH_SIPROUND   
\
+  do { 
\
+v0 += v1;  
\
+v1 = SIPHASH_ROTL(v1, 13); 
\
+v1 ^= v0;  
\
+v0 = SIPHASH_ROTL(v0, 32); 
\
+v2 += v3;  
\
+v3 = SIPHASH_ROTL(v3, 16); 
\
+v3 ^= v2;  
\
+v0 += v3;  
\
+v3 = SIPHASH_ROTL(v3, 21); 
\
+v3 ^= v0;  
\
+v2 += v1;  
\
+v1 = SIPHASH_ROTL(v1, 17); 
\
+v1 ^= v2;  
\
+v2 = SIPHASH_ROTL(v2, 32); 
\
+  } while (0)
+
+template 
+static inline ResultTy siphash(const uint8_t *in, uint64_t inlen,
+   const uint8_t ()[16]) {
+  static_assert(sizeof(ResultTy) == 8 || sizeof(ResultTy) == 16,
+"result type should be uint64_t or uint128_t");
+  uint64_t v0 = 0x736f6d6570736575ULL;
+  uint64_t v1 = 0x646f72616e646f6dULL;
+  uint64_t v2 = 0x6c7967656e657261ULL;
+  uint64_t v3 = 0x7465646279746573ULL;
+  uint64_t b;
+  uint64_t k0 = SIPHASH_U8TO64_LE(k);
+  uint64_t k1 = SIPHASH_U8TO64_LE(k + 8);
+  uint64_t m;
+  int i;
+  const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
+  const int left = inlen & 7;
+  b = ((uint64_t)inlen) << 56;
+  v3 ^= k1;
+  v2 ^= k0;
+  v1 ^= k1;
+  v0 ^= k0;
+
+  if (sizeof(ResultTy) == 16) {
+v1 ^= 0xee;
+  }
+
+  for (; in != end; in += 8) {
+m = SIPHASH_U8TO64_LE(in);
+v3 ^= m;
+
+for (i = 0; i < cROUNDS; ++i)
+  SIPHASH_SIPROUND;
+
+v0 ^= m;
+  }
+
+  switch (left) {
+  case 7:
+b |= ((uint64_t)in[6]) << 48;
+LLVM_FALLTHROUGH;
+  case 6:
+b |= ((uint64_t)in[5]) << 40;
+LLVM_FALLTHROUGH;
+  case 5:
+b |= ((uint64_t)in[4]) << 32;
+LLVM_FALLTHROUGH;
+  case 4:
+b |= ((uint64_t)in[3]) << 24;
+LLVM_FALLTHROUGH;
+  case 3:
+b |= ((uint64_t)in[2]) << 16;
+LLVM_FALLTHROUGH;
+  case 2:
+b |= ((uint64_t)in[1]) << 8;
+LLVM_FALLTHROUGH;
+  case 1:
+b |= ((uint64_t)in[0]);
+break;
+  case 0:
+break;
+  }
+
+  v3 ^= b;
+
+  for (i = 0; i < cROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  v0 ^= b;
+
+  if (sizeof(ResultTy) == 8) {
+v2 ^= 0xff;
+  } else {
+v2 ^= 0xee;
+  }
+
+  for (i = 0; i < dROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  b = v0 ^ v1 ^ v2 ^ v3;
+
+  // This mess with the result type would be easier with 'if constexpr'.
+
+  uint64_t firstHalf = b;
+  if (sizeof(ResultTy) == 8)
+return firstHalf;
+
+  v1 ^= 0xdd;
+
+  for (i = 0; i < dROUNDS; ++i)
+SIPHASH_SIPROUND;
+
+  b = v0 ^ v1 ^ v2 ^ v3;
+  uint64_t secondHalf = b;
+
+  return firstHalf | (ResultTy(secondHalf) << (sizeof(ResultTy) == 8 ? 0 : 
64));
+}
+
+//===--- LLVM-specific wrappers around siphash.
+
+/// Compute an ABI-stable 64-bit hash of the given string.

[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,47 @@
+//===--- SipHash.h - An ABI-stable string SipHash ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// A family of ABI-stable string hash algorithms based on SipHash, currently
+// used to compute ptrauth discriminators.
+//
+//===--===//
+
+#ifndef LLVM_SUPPORT_SIPHASH_H
+#define LLVM_SUPPORT_SIPHASH_H
+
+#include 
+
+namespace llvm {
+class StringRef;
+
+/// Compute a stable 64-bit hash of the given string.
+///
+/// The exact algorithm is the little-endian interpretation of the
+/// non-doubled (i.e. 64-bit) result of applying a SipHash-2-4 using
+/// a specific key value which can be found in the source.
+///
+/// By "stable" we mean that the result of this hash algorithm will
+/// the same across different compiler versions and target platforms.
+uint64_t getPointerAuthStableSipHash64(StringRef S);
+
+/// Compute a stable non-zero 16-bit hash of the given string.
+///
+/// This computes the full getPointerAuthStableSipHash64, but additionally
+/// truncates it down to a non-zero 16-bit value.
+///
+/// We use a 16-bit discriminator because ARM64 can efficiently load
+/// a 16-bit immediate into the high bits of a register without disturbing
+/// the remainder of the value, which serves as a nice blend operation.
+/// 16 bits is also sufficiently compact to not inflate a loader relocation.
+/// We disallow zero to guarantee a different discriminator from the places
+/// in the ABI that use a constant zero.
+uint64_t getPointerAuthStableSipHash16(StringRef S);

kovdan01 wrote:

Is having `uint64_t` as return value type intentional even for a function which 
by design always returns a 16-bit value?

https://github.com/llvm/llvm-project/pull/93902
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,43 @@
+//===- llvm/unittest/Support/SipHashTest.cpp 
--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/SipHash.h"
+#include "llvm/ADT/StringRef.h"

kovdan01 wrote:

It looks like that llvm/Support/SipHash.h and gtest/gtest.h includes are 
enough, other ones should probably be omitted.

https://github.com/llvm/llvm-project/pull/93902
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 commented:

As far as I understand, we want to take reference SipHash implementation with 
minimal changes. I personally have no issues with taking cryptographic C code 
"as is" with minimal changes. If it is the intention, LGTM with several minor 
comments.

Anyway, I want someone else with deeper understanding of the context to take a 
look before this gets merged.

https://github.com/llvm/llvm-project/pull/93902
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Support] Add SipHash-based 16/64-bit ptrauth stable hash. (PR #93902)

2024-06-03 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/93902
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-09 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 closed 
https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-09 Thread Daniil Kovalev via cfe-commits


@@ -0,0 +1,59 @@
+// RUN: %clang_cc1 -triple aarch64-linux -S -emit-llvm -o - \

kovdan01 wrote:

Fixed, thanks, see 37a4d9d08fefd8c42c21b92ad2e3c1fa07fac126

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-09 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 edited 
https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-09 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 919af72c09216838bfe586c3da503f5d74104a7d Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 19 Mar 2024 23:57:06 +0300
Subject: [PATCH 01/10] [PAC][clang] Define ptrauth driver flags and
 preprocessor features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   6 ++
 clang/include/clang/Basic/LangOptions.def |   6 ++
 clang/include/clang/Driver/Options.td |  18 
 clang/lib/Driver/ToolChains/Clang.cpp |  27 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  20 
 clang/test/Driver/ptrauth.c   |  32 +++
 clang/test/Preprocessor/ptrauth_feature.c | 107 +-
 7 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index eeed5f4751f2f..1c6236aa4f974 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -102,6 +102,12 @@ FEATURE(thread_sanitizer, 
LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
 FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 8ef6700ecdc78..4b99e70298462 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,12 @@ LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed 
matching of template t
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
 LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 29c226f4bd8da..e624eed2a1531 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4110,8 +4110,26 @@ let Group = f_Group in {
   let Visibility = [ClangOption,CC1Option] in {
 def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
   HelpText<"Enable pointer authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"Enable signing and authentication of all indirect calls">;
+def fptrauth_returns : Flag<["-"], "fptrauth-returns">,
+  HelpText<"Enable signing and authentication of return addresses">;
+def fptrauth_auth_traps : Flag<["-"], "fptrauth-auth-traps">,
+  

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-09 Thread Daniil Kovalev via cfe-commits


@@ -1190,6 +1191,36 @@ void CodeGenModule::Release() {
 if (!LangOpts.isSignReturnAddressWithAKey())
   getModule().addModuleFlag(llvm::Module::Min,
 "sign-return-address-with-bkey", 1);
+
+if (getTriple().isOSLinux() && getTriple().isOSBinFormatELF()) {

kovdan01 wrote:

OK, but I'll leave an assertion against ELF here. Theoretically, we might have 
non-ELF binary format on Linux, while the Linux itself seems to have deprecated 
COFF and a.out support and llvm does not seem to support them as well. This 
assertion makes sense to me since (a) it makes things clear that we want to 
check both Linux and ELF (b) this will notify us if in future we have binary 
formats other than ELF supported on Linux.


https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-08 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay Please let me know if you have some additional feedback after the 
latest updates b0f9a1985846448d01af9bacdb6e3ddb23d4588f or if this can be 
merged.

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-06 Thread Daniil Kovalev via cfe-commits


@@ -1796,6 +1796,26 @@ void Clang::AddAArch64TargetArgs(const ArgList ,
 
   Args.addOptInFlag(CmdArgs, options::OPT_fptrauth_intrinsics,
 options::OPT_fno_ptrauth_intrinsics);
+

kovdan01 wrote:

Deleted, see b0f9a1985846448d01af9bacdb6e3ddb23d4588f

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-06 Thread Daniil Kovalev via cfe-commits


@@ -1,5 +1,37 @@
+// RUN: %clang -### -c --target=aarch64 %s 2>&1 | FileCheck %s --check-prefix 
NONE
+// NONE: "-cc1"
+// NONE-NOT: "-fptrauth-
+
 // RUN: %clang -### -c --target=aarch64 -fno-ptrauth-intrinsics 
-fptrauth-intrinsics %s 2>&1 | FileCheck %s --check-prefix=INTRIN
 // INTRIN: "-cc1"{{.*}} "-fptrauth-intrinsics"
 
-// RUN: not %clang -### -c --target=x86_64 -fptrauth-intrinsics %s 2>&1 | 
FileCheck %s --check-prefix=ERR
-// ERR: error: unsupported option '-fptrauth-intrinsics' for target '{{.*}}'
+// RUN: %clang -### -c --target=aarch64 -fno-ptrauth-calls -fptrauth-calls %s 
2>&1 | FileCheck %s --check-prefix=CALL

kovdan01 wrote:

OK, this makes sense. Given that, I've combined 7 RUN lines for individual 
options into a single RUN line (so the file contains 3 RUN lines total now). 
See b0f9a1985846448d01af9bacdb6e3ddb23d4588f.

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-06 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 updated 
https://github.com/llvm/llvm-project/pull/85235

>From 919af72c09216838bfe586c3da503f5d74104a7d Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Tue, 19 Mar 2024 23:57:06 +0300
Subject: [PATCH 1/9] [PAC][clang] Define ptrauth driver flags and preprocessor
 features

Define the following clang driver flags:

- `-fptrauth-intrinsics`: `PointerAuth.intrinsics()` in `LangOptions`,
  `ptrauth_intrinsics` preprocessor feature;

- `-fptrauth-calls`: `PointerAuth.calls()` in `LangOptions`, `ptrauth_calls` and
  and `ptrauth_member_function_pointer_type_discrimination` preprocessor
  features;

- `-fptrauth-returns`: `PointerAuth.returns()` in `LangOptions`,
  `ptrauth_returns` preprocessor feature;

- `-fptrauth-auth-traps`: `PointerAuth.authTraps()` in `LangOptions`;

- `-fptrauth-vtable-pointer-address-discrimination`:
  `PointerAuth.vtptrAddressDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_address_discrimination` preprocessor feature;

- `-fptrauth-vtable-pointer-type-discrimination`:
  `PointerAuth.vtptrTypeDiscrimination()` in `LangOptions`,
  `ptrauth_vtable_pointer_type_discrimination` preprocessor feature;

- `-fptrauth-init-fini`: `PointerAuth.initFini()` in `LangOptions`,
  `ptrauth_init_fini` preprocessor feature.

The patch only defines the flags and having corresponding `LangOptions`
set does not affect codegen yet.

Co-authored-by: Ahmed Bougacha 
---
 clang/include/clang/Basic/Features.def|   6 ++
 clang/include/clang/Basic/LangOptions.def |   6 ++
 clang/include/clang/Driver/Options.td |  18 
 clang/lib/Driver/ToolChains/Clang.cpp |  27 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  20 
 clang/test/Driver/ptrauth.c   |  32 +++
 clang/test/Preprocessor/ptrauth_feature.c | 107 +-
 7 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/ptrauth.c

diff --git a/clang/include/clang/Basic/Features.def 
b/clang/include/clang/Basic/Features.def
index eeed5f4751f2f4..1c6236aa4f9748 100644
--- a/clang/include/clang/Basic/Features.def
+++ b/clang/include/clang/Basic/Features.def
@@ -102,6 +102,12 @@ FEATURE(thread_sanitizer, 
LangOpts.Sanitize.has(SanitizerKind::Thread))
 FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
 FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
 FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
+FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
+FEATURE(ptrauth_vtable_pointer_address_discrimination, 
LangOpts.PointerAuthVTPtrAddressDiscrimination)
+FEATURE(ptrauth_vtable_pointer_type_discrimination, 
LangOpts.PointerAuthVTPtrTypeDiscrimination)
+FEATURE(ptrauth_member_function_pointer_type_discrimination, 
LangOpts.PointerAuthCalls)
+FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
 FEATURE(swiftasynccc,
   PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
   clang::TargetInfo::CCCR_OK)
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index 8ef6700ecdc78e..4b99e70298462f 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -162,6 +162,12 @@ LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed 
matching of template t
 LANGOPT(ExperimentalLibrary, 1, 0, "enable unstable and experimental library 
features")
 
 LANGOPT(PointerAuthIntrinsics, 1, 0, "pointer authentication intrinsics")
+LANGOPT(PointerAuthCalls  , 1, 0, "function pointer authentication")
+LANGOPT(PointerAuthReturns, 1, 0, "return pointer authentication")
+LANGOPT(PointerAuthAuthTraps, 1, 0, "pointer authentication failure traps")
+LANGOPT(PointerAuthVTPtrAddressDiscrimination, 1, 0, "incorporate address 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthVTPtrTypeDiscrimination, 1, 0, "incorporate type 
discrimination in authenticated vtable pointers")
+LANGOPT(PointerAuthInitFini, 1, 0, "sign function pointers in init/fini 
arrays")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for 
all language standard modes")
 
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 29c226f4bd8da7..e624eed2a15316 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4110,8 +4110,26 @@ let Group = f_Group in {
   let Visibility = [ClangOption,CC1Option] in {
 def fptrauth_intrinsics : Flag<["-"], "fptrauth-intrinsics">,
   HelpText<"Enable pointer authentication intrinsics">;
+def fptrauth_calls : Flag<["-"], "fptrauth-calls">,
+  HelpText<"Enable signing and authentication of all indirect calls">;
+def fptrauth_returns : Flag<["-"], "fptrauth-returns">,
+  HelpText<"Enable signing and authentication of return addresses">;
+def fptrauth_auth_traps : Flag<["-"], "fptrauth-auth-traps">,
+  

[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-05-06 Thread Daniil Kovalev via cfe-commits


@@ -1,5 +1,37 @@
+// RUN: %clang -### -c --target=aarch64 %s 2>&1 | FileCheck %s --check-prefix 
NONE
+// NONE: "-cc1"
+// NONE-NOT: "-fptrauth-
+
 // RUN: %clang -### -c --target=aarch64 -fno-ptrauth-intrinsics 
-fptrauth-intrinsics %s 2>&1 | FileCheck %s --check-prefix=INTRIN
 // INTRIN: "-cc1"{{.*}} "-fptrauth-intrinsics"
 
-// RUN: not %clang -### -c --target=x86_64 -fptrauth-intrinsics %s 2>&1 | 
FileCheck %s --check-prefix=ERR
-// ERR: error: unsupported option '-fptrauth-intrinsics' for target '{{.*}}'
+// RUN: %clang -### -c --target=aarch64 -fno-ptrauth-calls -fptrauth-calls %s 
2>&1 | FileCheck %s --check-prefix=CALL

kovdan01 wrote:

I've measured timings on my machine, and we can potentially save about 0.1s if 
combine some of the RUN lines. It's noticeable, but there are tests like 
aarch64-fixed-x-register.c which contain a lot more similar RUN lines which can 
also be combined and we could save even more if enhance them.

I suggest to merge this PR "as is" and probably submitting a subsequent PR 
which makes this test and some similar ones "lighter" if it's a big concern. As 
for me, keeping all the RUN lines can protect us from dummy typos and they 
probably are worth the time spent on them - but I don't mind combining the RUN 
lines if the extra overhead bothers many people.

Please let me know if this approach is OK for you and I can merge the PR. If 
the clang driver test timings look too bad, I can also submit a corresponding 
issue so the context of the discussion is not lost.



https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [AArch64][PAC][clang][ELF] Support PAuth ABI core info (PR #85235)

2024-04-30 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@MaskRay I've addressed your latest comments in 
fdb26a14024f5f8297480a74982a37ee988cd30f - would be glad to see your feedback 
on the changes

https://github.com/llvm/llvm-project/pull/85235
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit `Qualifiers::Mask` (PR #90329)

2024-04-29 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 closed 
https://github.com/llvm/llvm-project/pull/90329
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit `Qualifiers::Mask` (PR #90329)

2024-04-29 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@cor3ntin Thanks for feedback! I'll merge it myself

https://github.com/llvm/llvm-project/pull/90329
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit `Qualifiers::Mask` (PR #90329)

2024-04-29 Thread Daniil Kovalev via cfe-commits

kovdan01 wrote:

@AaronBallman Please let me know if you have some comments on this or if can be 
merged

https://github.com/llvm/llvm-project/pull/90329
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit `Qualifiers::Mask` (PR #90329)

2024-04-27 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 ready_for_review 
https://github.com/llvm/llvm-project/pull/90329
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit `Qualifiers::Mask` (PR #90329)

2024-04-27 Thread Daniil Kovalev via cfe-commits

https://github.com/kovdan01 created 
https://github.com/llvm/llvm-project/pull/90329

After #84384, `Qualifiers::Mask` becomes 64-bit. So, operations like `Mask &= 
~U32` where `U32` is `unsigned` produce undesirable results since higher 32 
bits of `Mask` become zeroed while they should be preserved. Fix that by 
explicitly casting `unsigned` values to `uint64_t` in such operations. 
Signatures of fixed functions are intentionally left intact instead of changing 
the argument itself to `uint64_t` to keep things consistent with other 
functions working with the same qualifiers and to emphasize that 64-bit masks 
should not be used for these types of qualifiers.

>From dd3ace8376caec6bd2488a730990cf2e691a21e6 Mon Sep 17 00:00:00 2001
From: Daniil Kovalev 
Date: Sat, 27 Apr 2024 10:52:43 +0300
Subject: [PATCH] [clang] Fix `remove{CVR|Fast}Qualifiers` with 64-bit
 `Qualifiers::Mask`

After #84384, `Qualifiers::Mask` becomes 64-bit. So, operations like
`Mask &= ~U32` where `U32` is `unsigned` produce undesirable results
since higher 32 bits of `Mask` become zeroed while they should be
preserved. Fix that by explicitly casting `unsigned` values to `uint64_t`
in such operations. Signatures of fixed functions are intentionally left
intact instead of changing the argument itself to `uint64_t` to keep things
consistent with other functions working with the same qualifiers and to
emphasize that 64-bit masks should not be used for these types of
qualifiers.
---
 clang/include/clang/AST/Type.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index dff02d4861b3db..5c7d5396658b57 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -480,7 +480,7 @@ class Qualifiers {
   }
   void removeCVRQualifiers(unsigned mask) {
 assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
-Mask &= ~mask;
+Mask &= ~static_cast(mask);
   }
   void removeCVRQualifiers() {
 removeCVRQualifiers(CVRMask);
@@ -609,7 +609,7 @@ class Qualifiers {
   }
   void removeFastQualifiers(unsigned mask) {
 assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
-Mask &= ~mask;
+Mask &= ~static_cast(mask);
   }
   void removeFastQualifiers() {
 removeFastQualifiers(FastMask);

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >