[20/22] aarch64: Add tests and docs for indirect_return attribute

Message ID 20241023110528.487830-21-yury.khrustalev@arm.com
State New
Headers
Series aarch64: Add support for Guarded Control Stack extension |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed

Commit Message

Yury Khrustalev Oct. 23, 2024, 11:05 a.m. UTC
  From: Richard Ball <richard.ball@arm.com>

This patch adds a new testcase and docs
for the indirect_return attribute.

gcc/ChangeLog:

	* doc/extend.texi: Add AArch64 docs for indirect_return
	attribute.

gcc/testsuite/ChangeLog:

	* gcc.target/aarch64/indirect_return.c: New test.
	Co-authored-by: Yury Khrustalev <yury.khrustalev@arm.com>
---
 gcc/doc/extend.texi                           |  5 ++++
 .../gcc.target/aarch64/indirect_return.c      | 25 +++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/indirect_return.c
  

Comments

Richard Sandiford Oct. 24, 2024, 5:41 p.m. UTC | #1
Yury Khrustalev <yury.khrustalev@arm.com> writes:
> From: Richard Ball <richard.ball@arm.com>
>
> This patch adds a new testcase and docs
> for the indirect_return attribute.
>
> gcc/ChangeLog:
>
> 	* doc/extend.texi: Add AArch64 docs for indirect_return
> 	attribute.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/aarch64/indirect_return.c: New test.
> 	Co-authored-by: Yury Khrustalev <yury.khrustalev@arm.com>
> ---
>  gcc/doc/extend.texi                           |  5 ++++
>  .../gcc.target/aarch64/indirect_return.c      | 25 +++++++++++++++++++
>  2 files changed, 30 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/aarch64/indirect_return.c
>
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
> index 42bd567119d..45e2b3ec569 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -4760,6 +4760,11 @@ Enable or disable calls to out-of-line helpers to implement atomic operations.
>  This corresponds to the behavior of the command-line options
>  @option{-moutline-atomics} and @option{-mno-outline-atomics}.
>  
> +@cindex @code{indirect_return} function attribute, AArch64
> +@item indirect_return
> +Used to inform the compiler that a function may return via
> +an indirect return. Adds a BTI J instruction under @option{mbranch-protection=} bti.

"return via an indirect return" doesn't really add much information,
especially since the "indirect" might sound related to "indirect
branch", and all returns are indirect in that sense.

How about going with a variation of the x86 documentation:

The @code{indirect_return} attribute can be applied to a function type
to indicate that the function may return via an indirect branch instead
of via a normal return instruction.  For example, this can be true of
functions that implement manual context switching between user space
threads, such as POSIX's @code{swapcontext} function.

>  @end table
>  
>  The above target attributes can be specified as follows:
> diff --git a/gcc/testsuite/gcc.target/aarch64/indirect_return.c b/gcc/testsuite/gcc.target/aarch64/indirect_return.c
> new file mode 100644
> index 00000000000..f1ef56d5557
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/aarch64/indirect_return.c
> @@ -0,0 +1,25 @@
> +/* { dg-do compile } */
> +/* { dg-options "-mbranch-protection=bti" } */
> +
> +int __attribute((indirect_return))
> +foo (int a)
> +{
> +  return a;
> +}
> +
> +/*
> +**func1:
> +**	hint	34 // bti c
> +**	...
> +**	bl	foo
> +**	hint	36 // bti j
> +**	...
> +**	ret
> +*/
> +int
> +func1 (int a, int b)
> +{
> +  return foo (a + b);
> +}
> +
> +/* { dg-final { check-function-bodies "**" "" "" } } */

I think we should also check the case of a sibling call from an
indirect_return function to an indirect_return function,
since patch 19/22 specifically optimises that case.

Thanks,
Richard
  

Patch

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 42bd567119d..45e2b3ec569 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -4760,6 +4760,11 @@  Enable or disable calls to out-of-line helpers to implement atomic operations.
 This corresponds to the behavior of the command-line options
 @option{-moutline-atomics} and @option{-mno-outline-atomics}.
 
+@cindex @code{indirect_return} function attribute, AArch64
+@item indirect_return
+Used to inform the compiler that a function may return via
+an indirect return. Adds a BTI J instruction under @option{mbranch-protection=} bti.
+
 @end table
 
 The above target attributes can be specified as follows:
diff --git a/gcc/testsuite/gcc.target/aarch64/indirect_return.c b/gcc/testsuite/gcc.target/aarch64/indirect_return.c
new file mode 100644
index 00000000000..f1ef56d5557
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/indirect_return.c
@@ -0,0 +1,25 @@ 
+/* { dg-do compile } */
+/* { dg-options "-mbranch-protection=bti" } */
+
+int __attribute((indirect_return))
+foo (int a)
+{
+  return a;
+}
+
+/*
+**func1:
+**	hint	34 // bti c
+**	...
+**	bl	foo
+**	hint	36 // bti j
+**	...
+**	ret
+*/
+int
+func1 (int a, int b)
+{
+  return foo (a + b);
+}
+
+/* { dg-final { check-function-bodies "**" "" "" } } */