Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
@ 2026-07-08  6:03 Thiago Jung Bauermann
  2026-07-08  7:50 ` Aktemur, Baris
  2026-07-19  8:57 ` Luis
  0 siblings, 2 replies; 8+ messages in thread
From: Thiago Jung Bauermann @ 2026-07-08  6:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: Luis

On AArch64, even if the Guarded Control Stack feature is present on the
system the inferior may not have enabled it.

There's a bug in GDB's GCS support in that if the system supports GCS,
GDB will always push a GCS entry when doing an inferior function call
even if the inferior doesn't have it enabled.  This causes inferior
function calls to fail. E.g.:

  (gdb) p foo ()
  Can't write to Guarded Control Stack.

The error message is because the GCSPR doesn't point to a valid memory
address.

Fix by checking whether GCS is enabled in the inferior before trying to
push a GCS entry.

Regression tested on an aarch64-linux-gnu QEMU VM with GCS present.
---

GCS support first appeared in GDB 17, so if this patch is approved for
trunk is it ok to also commit it to the branch?

 gdb/aarch64-tdep.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index 886e7ce1e7b6..b4437dcccc8a 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -1934,7 +1934,11 @@ static void
 aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
 			   regcache *regcache)
 {
-  aarch64_push_gcs_entry (regcache, new_addr);
+  bool gcs_is_enabled;
+
+  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
+  if (gcs_is_enabled)
+    aarch64_push_gcs_entry (regcache, new_addr);
 }
 
 /* Implement the "push_dummy_call" gdbarch method.  */

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-08  6:03 [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled Thiago Jung Bauermann
@ 2026-07-08  7:50 ` Aktemur, Baris
  2026-07-08 19:43   ` Thiago Jung Bauermann
  2026-07-21 16:38   ` Tom Tromey
  2026-07-19  8:57 ` Luis
  1 sibling, 2 replies; 8+ messages in thread
From: Aktemur, Baris @ 2026-07-08  7:50 UTC (permalink / raw)
  To: Thiago Jung Bauermann, gdb-patches; +Cc: Luis

AMD General

Hi Thiago,

> On AArch64, even if the Guarded Control Stack feature is present on the
> system the inferior may not have enabled it.
>
> There's a bug in GDB's GCS support in that if the system supports GCS,
> GDB will always push a GCS entry when doing an inferior function call
> even if the inferior doesn't have it enabled.  This causes inferior
> function calls to fail. E.g.:
>
>   (gdb) p foo ()
>   Can't write to Guarded Control Stack.
>
> The error message is because the GCSPR doesn't point to a valid memory
> address.
>
> Fix by checking whether GCS is enabled in the inferior before trying to
> push a GCS entry.
>
> Regression tested on an aarch64-linux-gnu QEMU VM with GCS present.
> ---
>
> GCS support first appeared in GDB 17, so if this patch is approved for
> trunk is it ok to also commit it to the branch?
>
>  gdb/aarch64-tdep.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
> index 886e7ce1e7b6..b4437dcccc8a 100644
> --- a/gdb/aarch64-tdep.c
> +++ b/gdb/aarch64-tdep.c
> @@ -1934,7 +1934,11 @@ static void
>  aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
>                            regcache *regcache)
>  {
> -  aarch64_push_gcs_entry (regcache, new_addr);
> +  bool gcs_is_enabled;
> +
> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);

Some analyzer tools may flag this because the return value is not being used.
Maybe put a `(void)` in front to indicate the return value is being ignored
deliberately.

Regards,
-Baris



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-08  7:50 ` Aktemur, Baris
@ 2026-07-08 19:43   ` Thiago Jung Bauermann
  2026-07-21 16:38   ` Tom Tromey
  1 sibling, 0 replies; 8+ messages in thread
From: Thiago Jung Bauermann @ 2026-07-08 19:43 UTC (permalink / raw)
  To: Aktemur, Baris; +Cc: gdb-patches, Luis

Hello Baris,

Thank you for your review!

"Aktemur, Baris" <TankutBaris.Aktemur@amd.com> writes:

>> On AArch64, even if the Guarded Control Stack feature is present on the
>> system the inferior may not have enabled it.
>>
>> There's a bug in GDB's GCS support in that if the system supports GCS,
>> GDB will always push a GCS entry when doing an inferior function call
>> even if the inferior doesn't have it enabled.  This causes inferior
>> function calls to fail. E.g.:
>>
>>   (gdb) p foo ()
>>   Can't write to Guarded Control Stack.
>>
>> The error message is because the GCSPR doesn't point to a valid memory
>> address.
>>
>> Fix by checking whether GCS is enabled in the inferior before trying to
>> push a GCS entry.
>>
>> Regression tested on an aarch64-linux-gnu QEMU VM with GCS present.
>> ---
>>
>> GCS support first appeared in GDB 17, so if this patch is approved for
>> trunk is it ok to also commit it to the branch?
>>
>>  gdb/aarch64-tdep.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
>> index 886e7ce1e7b6..b4437dcccc8a 100644
>> --- a/gdb/aarch64-tdep.c
>> +++ b/gdb/aarch64-tdep.c
>> @@ -1934,7 +1934,11 @@ static void
>>  aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
>>                            regcache *regcache)
>>  {
>> -  aarch64_push_gcs_entry (regcache, new_addr);
>> +  bool gcs_is_enabled;
>> +
>> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
>
> Some analyzer tools may flag this because the return value is not being used.
> Maybe put a `(void)` in front to indicate the return value is being ignored
> deliberately.

Makes sense. I made this change locally:

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index b4437dcccc8a..5b38ff74260c 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -1936,7 +1936,7 @@ aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
 {
   bool gcs_is_enabled;
 
-  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
+  (void) gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
   if (gcs_is_enabled)
     aarch64_push_gcs_entry (regcache, new_addr);
 }

-- 
Thiago
(he/him)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-08  6:03 [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled Thiago Jung Bauermann
  2026-07-08  7:50 ` Aktemur, Baris
@ 2026-07-19  8:57 ` Luis
  2026-07-22 19:59   ` Thiago Jung Bauermann
  1 sibling, 1 reply; 8+ messages in thread
From: Luis @ 2026-07-19  8:57 UTC (permalink / raw)
  To: Thiago Jung Bauermann, gdb-patches

Hi Thiago. Thanks for the patch.

On 08/07/2026 07:03, Thiago Jung Bauermann wrote:
> On AArch64, even if the Guarded Control Stack feature is present on the
> system the inferior may not have enabled it.
> 
> There's a bug in GDB's GCS support in that if the system supports GCS,
> GDB will always push a GCS entry when doing an inferior function call
> even if the inferior doesn't have it enabled.  This causes inferior
> function calls to fail. E.g.:
> 
>    (gdb) p foo ()
>    Can't write to Guarded Control Stack.
> 
> The error message is because the GCSPR doesn't point to a valid memory
> address.
> 
> Fix by checking whether GCS is enabled in the inferior before trying to
> push a GCS entry.
> 
> Regression tested on an aarch64-linux-gnu QEMU VM with GCS present.
> ---
> 
> GCS support first appeared in GDB 17, so if this patch is approved for
> trunk is it ok to also commit it to the branch?

Sounds good to me.

> 
>   gdb/aarch64-tdep.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
> index 886e7ce1e7b6..b4437dcccc8a 100644
> --- a/gdb/aarch64-tdep.c
> +++ b/gdb/aarch64-tdep.c
> @@ -1934,7 +1934,11 @@ static void
>   aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
>   			   regcache *regcache)
>   {
> -  aarch64_push_gcs_entry (regcache, new_addr);
> +  bool gcs_is_enabled;
> +
> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
> +  if (gcs_is_enabled)
> +    aarch64_push_gcs_entry (regcache, new_addr);
>   }
>   
>   /* Implement the "push_dummy_call" gdbarch method.  */

LGTM, with Baris' suggestion.

Approved-By: Luis Machado <luis.machado.foss@gmail.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-08  7:50 ` Aktemur, Baris
  2026-07-08 19:43   ` Thiago Jung Bauermann
@ 2026-07-21 16:38   ` Tom Tromey
  2026-07-22 20:23     ` Thiago Jung Bauermann
  1 sibling, 1 reply; 8+ messages in thread
From: Tom Tromey @ 2026-07-21 16:38 UTC (permalink / raw)
  To: Aktemur, Baris; +Cc: Thiago Jung Bauermann, gdb-patches, Luis

>>>>> Aktemur, Baris <TankutBaris.Aktemur@amd.com> writes:

>> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);

> Some analyzer tools may flag this because the return value is not being used.
> Maybe put a `(void)` in front to indicate the return value is being ignored
> deliberately.

FWIW gdb does not normally do this unless the function is specially
marked.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-19  8:57 ` Luis
@ 2026-07-22 19:59   ` Thiago Jung Bauermann
  0 siblings, 0 replies; 8+ messages in thread
From: Thiago Jung Bauermann @ 2026-07-22 19:59 UTC (permalink / raw)
  To: Luis; +Cc: gdb-patches

Hello Luis,

Thank you for the review!

Luis <luis.machado.foss@gmail.com> writes:

> On 08/07/2026 07:03, Thiago Jung Bauermann wrote:
>> On AArch64, even if the Guarded Control Stack feature is present on the
>> system the inferior may not have enabled it.
>> There's a bug in GDB's GCS support in that if the system supports GCS,
>> GDB will always push a GCS entry when doing an inferior function call
>> even if the inferior doesn't have it enabled.  This causes inferior
>> function calls to fail. E.g.:
>>    (gdb) p foo ()
>>    Can't write to Guarded Control Stack.
>> The error message is because the GCSPR doesn't point to a valid memory
>> address.
>> Fix by checking whether GCS is enabled in the inferior before trying to
>> push a GCS entry.
>> Regression tested on an aarch64-linux-gnu QEMU VM with GCS present.
>> ---
>> GCS support first appeared in GDB 17, so if this patch is approved for
>> trunk is it ok to also commit it to the branch?
>
> Sounds good to me.
>
>>   gdb/aarch64-tdep.c | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
>> index 886e7ce1e7b6..b4437dcccc8a 100644
>> --- a/gdb/aarch64-tdep.c
>> +++ b/gdb/aarch64-tdep.c
>> @@ -1934,7 +1934,11 @@ static void
>>   aarch64_shadow_stack_push (gdbarch *gdbarch, CORE_ADDR new_addr,
>>   			   regcache *regcache)
>>   {
>> -  aarch64_push_gcs_entry (regcache, new_addr);
>> +  bool gcs_is_enabled;
>> +
>> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
>> +  if (gcs_is_enabled)
>> +    aarch64_push_gcs_entry (regcache, new_addr);
>>   }
>>     /* Implement the "push_dummy_call" gdbarch method.  */
>
> LGTM, with Baris' suggestion.
>
> Approved-By: Luis Machado <luis.machado.foss@gmail.com>

Pushed as commit 4410648da673, with Baris' suggestion.

Also pushed as commit cbdcbf506376 to gdb-17-branch.

-- 
Thiago
(he/him)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-21 16:38   ` Tom Tromey
@ 2026-07-22 20:23     ` Thiago Jung Bauermann
  2026-07-22 22:27       ` Tom Tromey
  0 siblings, 1 reply; 8+ messages in thread
From: Thiago Jung Bauermann @ 2026-07-22 20:23 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Aktemur, Baris, gdb-patches, Luis

Tom Tromey <tom@tromey.com> writes:

>>>>>> Aktemur, Baris <TankutBaris.Aktemur@amd.com> writes:
>
>>> +  gdbarch_get_shadow_stack_pointer (gdbarch, regcache, gcs_is_enabled);
>
>> Some analyzer tools may flag this because the return value is not being used.
>> Maybe put a `(void)` in front to indicate the return value is being ignored
>> deliberately.
>
> FWIW gdb does not normally do this unless the function is specially
> marked.

I found some places in GDB where we do this even though the called
function isn't specially marked (e.g., calls to record_stop in
gdb/record.c, and call to PyGILState_Ensure in gdb/python/python.c)
so ahead with the `(void)`.

I can push a patch removing it if anyone wants.

-- 
Thiago
(he/him)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled
  2026-07-22 20:23     ` Thiago Jung Bauermann
@ 2026-07-22 22:27       ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-07-22 22:27 UTC (permalink / raw)
  To: Thiago Jung Bauermann; +Cc: Tom Tromey, Aktemur, Baris, gdb-patches, Luis

Thiago> I found some places in GDB where we do this even though the called
Thiago> function isn't specially marked (e.g., calls to record_stop in
Thiago> gdb/record.c, and call to PyGILState_Ensure in gdb/python/python.c)
Thiago> so ahead with the `(void)`.

Yeah, like with most rules in gdb, there's also violations.

Thiago> I can push a patch removing it if anyone wants.

It's not really that important IMO, just something to note for the
future.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-07-22 22:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-08  6:03 [PATCH] GDB: aarch64: Fix inferior function call if GCS is present but disabled Thiago Jung Bauermann
2026-07-08  7:50 ` Aktemur, Baris
2026-07-08 19:43   ` Thiago Jung Bauermann
2026-07-21 16:38   ` Tom Tromey
2026-07-22 20:23     ` Thiago Jung Bauermann
2026-07-22 22:27       ` Tom Tromey
2026-07-19  8:57 ` Luis
2026-07-22 19:59   ` Thiago Jung Bauermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox