From: Luis Machado <luis.machado.foss@gmail.com>
To: gdb-patches@sourceware.org
Cc: thiago.bauermann@linaro.org
Subject: [PATCH 2/2] gdb, aarch64: add selftest for the pauth mask cache
Date: Sat, 12 Sep 2026 23:20:11 +0100 [thread overview]
Message-ID: <20260912222011.2395686-3-luis.machado.foss@gmail.com> (raw)
In-Reply-To: <20260912222011.2395686-1-luis.machado.foss@gmail.com>
Add aarch64_remove_non_address_bits_test, a selftest that builds an
aarch64 gdbarch with the pauth feature, installs a mock target that
counts fetch_registers calls, and calls
aarch64_remove_non_address_bits repeatedly while invalidating the
regcache between calls to simulate a resume/stop cycle. It checks
that the target is only ever asked for the dmask/cmask registers
once each, rather than once per stop, verifying the per-inferior
cache added in the previous commit.
---
gdb/aarch64-tdep.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index c7003687eab..fb046d12c8e 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -42,6 +42,9 @@
#include "user-regs.h"
#include "ax-gdb.h"
#include "gdbsupport/selftest.h"
+#include "gdbthread.h"
+#include "test-target.h"
+#include "scoped-mock-context.h"
#include "aarch64-tdep.h"
#include "aarch64-ravenscar-thread.h"
@@ -4470,6 +4473,71 @@ aarch64_remove_non_address_bits (struct gdbarch *gdbarch, CORE_ADDR pointer)
return aarch64_remove_top_bits (pointer, mask);
}
+#if GDB_SELF_TEST
+namespace selftests {
+
+/* A mock target that supplies zeroed pauth mask registers and counts
+ how many times fetch_registers is called, so tests can tell how many
+ times a mask value was actually fetched from the target as opposed to
+ reused from a cache. */
+
+class pac_mask_counting_target : public test_target_ops
+{
+public:
+ void fetch_registers (regcache *regs, int regno) override
+ {
+ fetch_registers_called++;
+ regs->raw_supply_zeroed (regno);
+ }
+
+ unsigned int fetch_registers_called = 0;
+};
+
+/* Verify that aarch64_remove_non_address_bits does not re-fetch the
+ pauth mask registers from the target on every call, since those masks
+ are constant for the lifetime of the inferior (see the comment in
+ aarch64_remove_non_address_bits). We simulate one call per stop by
+ invalidating the regcache (via registers_changed) between calls, the
+ same way a resume/stop cycle (e.g. stepi) would. */
+
+static void
+aarch64_remove_non_address_bits_test ()
+{
+ aarch64_features features;
+ features.pauth = true;
+
+ struct gdbarch_info info;
+ info.bfd_arch_info = bfd_scan_arch ("aarch64");
+ info.target_desc = aarch64_read_description (features);
+
+ struct gdbarch *gdbarch = gdbarch_find_by_info (info);
+ SELF_CHECK (gdbarch != nullptr);
+
+ aarch64_gdbarch_tdep *tdep = gdbarch_tdep<aarch64_gdbarch_tdep> (gdbarch);
+ SELF_CHECK (tdep->has_pauth ());
+
+ scoped_mock_context<pac_mask_counting_target> ctx (gdbarch);
+
+ const int n_calls = 20;
+ for (int i = 0; i < n_calls; i++)
+ {
+ aarch64_remove_non_address_bits (gdbarch, 0x1234);
+
+ /* Invalidate the regcache, as would happen after a resume/stop
+ cycle, so that a naive implementation is forced to re-fetch the
+ mask registers on the next call. */
+ registers_changed ();
+ }
+
+ /* The masks never change for the life of the inferior. Each of the two
+ mask registers (dmask, cmask) should only ever need to be fetched
+ from the target once, not once per stop. */
+ SELF_CHECK (ctx.mock_target.fetch_registers_called == 2);
+}
+
+} /* namespace selftests */
+#endif /* GDB_SELF_TEST */
+
/* Given NAMES, a vector of strings, initialize it with all the SME
pseudo-register names for the current streaming vector length. */
@@ -5160,6 +5228,8 @@ When on, AArch64 specific debugging is enabled."),
selftests::aarch64_analyze_prologue_test);
selftests::register_test ("aarch64-process-record",
selftests::aarch64_process_record_test);
+ selftests::register_test ("aarch64-remove-non-address-bits",
+ selftests::aarch64_remove_non_address_bits_test);
#endif
}
--
2.43.0
prev parent reply other threads:[~2026-09-12 22:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-12 22:20 [PATCH 0/2] gdb, aarch64: cache pointer authentication masks per inferior Luis Machado
2026-09-12 22:20 ` [PATCH 1/2] " Luis Machado
2026-09-14 15:24 ` Simon Marchi
2026-09-12 22:20 ` Luis Machado [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260912222011.2395686-3-luis.machado.foss@gmail.com \
--to=luis.machado.foss@gmail.com \
--cc=gdb-patches@sourceware.org \
--cc=thiago.bauermann@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox