| 1 |
/* PR target/94515. Check .cfi_negate_ra_state with multiple return paths. */ |
| 2 |
/* { dg-do run } */ |
| 3 |
/* { dg-require-effective-target lp64 } */ |
| 4 |
/* { dg-additional-options "-O2 -mbranch-protection=pac-ret" } */ |
| 5 |
|
| 6 |
volatile int zero = 0; |
| 7 |
int global = 0; |
| 8 |
|
| 9 |
/* This is a leaf function, so no .cfi_negate_ra_state directive is expected. */ |
| 10 |
__attribute__((noinline)) |
| 11 |
int bar(void) |
| 12 |
{ |
| 13 |
if (zero == 0) return 3; |
| 14 |
return 0; |
| 15 |
} |
| 16 |
|
| 17 |
/* This function does not return normally, so the address is signed but no |
| 18 |
* authentication code is emitted. It means that only one CFI directive is |
| 19 |
* supposed to be emitted at signing time. */ |
| 20 |
__attribute__((noinline, noreturn)) |
| 21 |
void unwind (void) |
| 22 |
{ |
| 23 |
throw 42; |
| 24 |
} |
| 25 |
|
| 26 |
/* This function has several return instructions, and alternates different RA |
| 27 |
* states. 4 .cfi_negate_ra_state and a .cfi_remember_state/.cfi_restore_state |
| 28 |
* should be emitted. |
| 29 |
* |
| 30 |
* Expected layout: |
| 31 |
* A: path to return 0 without assignment to global |
| 32 |
* B: global=y + branch back into A |
| 33 |
* C: return 2 |
| 34 |
* D: unwind |
| 35 |
* Which gives with return pointer authentication: |
| 36 |
* A: sign -> authenticate [2 negate_ra_states + remember_state for B] |
| 37 |
* B: signed [restore_state] |
| 38 |
* C: unsigned [negate_ra_state] |
| 39 |
* D: signed [negate_ra_state] |
| 40 |
*/ |
| 41 |
__attribute__((noinline, noipa)) |
| 42 |
int test(int x) |
| 43 |
{ |
| 44 |
// This return path may not use the stack. This means that the return address |
| 45 |
// won't be signed. |
| 46 |
if (x==1) return 2; |
| 47 |
|
| 48 |
// All the return paths of the code below must have RA mangle state set, and |
| 49 |
// the return address must be signed. |
| 50 |
int y = bar(); |
| 51 |
if (y > global) global=y; |
| 52 |
if (y==3) unwind(); // authentication of the return address is not required. |
| 53 |
return 0; // authentication of the return address is required. |
| 54 |
} |
| 55 |
|
| 56 |
/* This function requires only 2 .cfi_negate_ra_state. */ |
| 57 |
int main () |
| 58 |
{ |
| 59 |
// paciasp -> cfi_negate_ra_state: RA_no_signing -> RA_signing_SP |
| 60 |
try { |
| 61 |
test (zero); |
| 62 |
__builtin_abort (); // authentication of the return address is not required. |
| 63 |
} catch (...) { |
| 64 |
// autiasp -> cfi_negate_ra_state: RA_signing_SP -> RA_no_signing |
| 65 |
return 0; |
| 66 |
} |
| 67 |
__builtin_abort (); // authentication of the return address is not required. |
| 68 |
} |