[PATCH v2 01/24] New target methods for memory tagging support

Luis Machado via Gdb-patches
2020-10-22T20:00:33Z

Updates on v2:

- Added type parameter to fetch_memtags/store_memtags hooks.

--

This patch starts adding some of the generic pieces to accomodate memory
tagging.

We have three new target methods:

- supports_memory_tagging: Checks if the target supports memory tagging. This
defaults to false for targets that don't support memory tagging.

- fetch_memtags: Fetches the allocation tags associated with a particular
memory range [address, address + length).

The default is to return 0 without returning any tags. This should only
be called if memory tagging is supported.

- store_memtags: Stores a set of allocation tags for a particular memory
range [address, address + length).

The default is to return 0. This should only
be called if memory tagging is supported.

It also adds a control option for enabling/disabling memory tagging
manually: set memory-tagging on/off.

The default is "on", with GDB making its use conditional to the
architecture supporting memory tagging.

gdb/ChangeLog:

YYYY-MM-DD Luis Machado <luis.machado@linaro.org>

* printcmd.c (memtag): New static global.
(show_memtag): New function.
(_initialize_printcmd): Add set/show memory-tagging command.
* remote.c (remote_target) <supports_memory_tagging>: New method
override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(remote_target::supports_memory_tagging): New method.
(remote_target::fetch_memtags): New method.
(remote_target::store_memtags): New method.
* target-delegates.c
(dummy_target) <supports_memory_tagging>: New method override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(debug_target) <supports_memory_tagging>: New method override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(target_ops::supports_memory_tagging): New method.
(target_ops::fetch_memtags): New method.
(target_ops::store_memtags): New method.
(dummy_target::supports_memory_tagging): New method.
(dummy_target::fetch_memtags): New method.
(dummy_target::store_memtags): New method.
(debug_target::supports_memory_tagging): New method.
(debug_target::fetch_memtags): New method.
(debug_target::store_memtags): New method.
* target.h (struct target_ops) <supports_memory_tagging>: New virtual
method.
<fetch_memtags: New virtual method.
<store_memtags>: New virtual method.
(target_supports_memory_tagging): Define.
(target_fetch_memtags): Define.
(target_store_memtags): Define.
---
gdb/printcmd.c | 28 ++++++++++++++
gdb/remote.c | 36 ++++++++++++++++++
gdb/target-delegates.c | 84 ++++++++++++++++++++++++++++++++++++++++++
gdb/target.h | 25 +++++++++++++
4 files changed, 173 insertions(+)

diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index f9b42cfda7..c083d8973c 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -84,6 +84,24 @@ static CORE_ADDR last_examine_address;

static value_ref_ptr last_examine_value;

+/* If TRUE (default), and the architecture supports it, GDB will attempt to use
+ the memory tagging infrastructure to validate certain memory accesses. It
+ will also report memory tag violations alongside a SIGSEGV signal.
+
+ If FALSE, GDB will not use memory tagging in any way, and debugging will work
+ in the standard way. */
+static bool memtag = true;
+
+static void
+show_memtag (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ fprintf_filtered (file,
+ _("Use of memory tagging infrastructure is \"%s\".\n"),
+ value);
+}
+
/* Largest offset between a symbolic value and an address, that will be
printed as `0x1234 <symbol+offset>'. */

@@ -2893,4 +2911,14 @@ Construct a GDB command and then evaluate it.\n\
Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
Convert the arguments to a string as \"printf\" would, but then\n\
treat this string as a command line, and evaluate it."));
+
+ add_setshow_boolean_cmd ("memory-tagging", class_support,
+ &memtag, _("\
+Set whether the debugger should use memory tagging infrastructure."), _("\
+Show whether the debugger should use memory tagging infrastructure."), _("\
+If on, gdb will attempt to validate memory tags and will warn the user if\n\
+certain operations have illegal tags."),
+ NULL,
+ show_memtag,
+ &setlist, &showlist);
}
diff --git a/gdb/remote.c b/gdb/remote.c
index 9ee1e8cbcf..6e37e5f4e6 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -686,6 +686,16 @@ class remote_target : public process_stratum_target
int remove_exec_catchpoint (int) override;
enum exec_direction_kind execution_direction () override;

+ bool supports_memory_tagging () override;
+
+ /* Read memory tags via the qMemTags packet */
+ int fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type) override;
+
+ /* Write allocation tags via the QMemTags packet. */
+ int store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type) override;
+
public: /* Remote specific methods. */

void remote_download_command_source (int num, ULONGEST addr,
@@ -14387,6 +14397,32 @@ set_range_stepping (const char *ignore_args, int from_tty,
}
}

+/* Implement the "supports_memory_tagging" target_ops method. */
+
+bool
+remote_target::supports_memory_tagging ()
+{
+ return false;
+}
+
+/* Implement the "fetch_memtags" target_ops method. */
+
+int
+remote_target::fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type)
+{
+ return 0;
+}
+
+/* Implement the "store_memtags" target_ops method. */
+
+int
+remote_target::store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type)
+{
+ return 0;
+}
+
void _initialize_remote ();
void
_initialize_remote ()
diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c
index 437b19b858..fbebc8b4ce 100644
--- a/gdb/target-delegates.c
+++ b/gdb/target-delegates.c
@@ -173,6 +173,11 @@ struct dummy_target : public target_ops
const struct frame_unwind *get_tailcall_unwinder () override;
void prepare_to_generate_core () override;
void done_generating_core () override;
+ bool supports_memory_tagging () override;
+ int fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type) override;
+ int store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type) override;
};

struct debug_target : public target_ops
@@ -344,6 +349,11 @@ struct debug_target : public target_ops
const struct frame_unwind *get_tailcall_unwinder () override;
void prepare_to_generate_core () override;
void done_generating_core () override;
+ bool supports_memory_tagging () override;
+ int fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type) override;
+ int store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type) override;
};

void
@@ -4413,3 +4423,77 @@ debug_target::done_generating_core ()
fputs_unfiltered (")\n", gdb_stdlog);
}

+bool
+target_ops::supports_memory_tagging ()
+{
+ return this->beneath ()->supports_memory_tagging ();
+}
+
+int
+target_ops::fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type)
+{
+ return this->beneath ()->fetch_memtags (address, len, tags, type);
+}
+
+int
+target_ops::store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type)
+{
+ return this->beneath ()->store_memtags (address, len, tags, type);
+}
+
+bool
+dummy_target::supports_memory_tagging ()
+{
+ return false;
+}
+
+int
+dummy_target::fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type)
+{
+ return 0;
+}
+
+int
+dummy_target::store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type)
+{
+ return 0;
+}
+
+bool
+debug_target::supports_memory_tagging ()
+{
+ bool result;
+ fprintf_unfiltered (gdb_stdlog, "-> %s->supports_memory_tagging (...)\n", this->beneath ()->shortname ());
+ result = this->beneath ()->supports_memory_tagging ();
+ fprintf_unfiltered (gdb_stdlog, "<- %s->supports_memory_tagging (", this->beneath ()->shortname ());
+ fputs_unfiltered (") = ", gdb_stdlog);
+ target_debug_print_bool (result);
+ fputs_unfiltered ("\n", gdb_stdlog);
+ return result;
+}
+
+int
+debug_target::fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type)
+{
+ fprintf_unfiltered (gdb_stdlog, "-> %s->fetch_memtags (...)\n", this->beneath ()->shortname ());
+ int result = this->beneath ()->fetch_memtags (address, len, tags, type);
+ fprintf_unfiltered (gdb_stdlog, "<- %s->fetch_memtags (", this->beneath ()->shortname ());
+ fputs_unfiltered (")\n", gdb_stdlog);
+ return result;
+}
+
+int
+debug_target::store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type)
+{
+ fprintf_unfiltered (gdb_stdlog, "-> %s->store_memtags (...)\n", this->beneath ()->shortname ());
+ int result = this->beneath ()->store_memtags (address, len, tags, type);
+ fprintf_unfiltered (gdb_stdlog, "<- %s->store_memtags (", this->beneath ()->shortname ());
+ fputs_unfiltered (")\n", gdb_stdlog);
+ return result;
+}
diff --git a/gdb/target.h b/gdb/target.h
index 039194c239..80f9b1ee92 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -1260,6 +1260,22 @@ struct target_ops
/* Cleanup after generating a core file. */
virtual void done_generating_core ()
TARGET_DEFAULT_IGNORE ();
+
+ /* Returns true if the target supports memory tagging. */
+ virtual bool supports_memory_tagging ()
+ TARGET_DEFAULT_RETURN (false);
+
+ /* Return the allocated memory tags of type TYPE associated with
+ [ADDRESS, ADDRESS + LEN) in TAGS. */
+ virtual int fetch_memtags (CORE_ADDR address, size_t len,
+ gdb::byte_vector &tags, int type)
+ TARGET_DEFAULT_IGNORE ();
+
+ /* Write the allocation tags of type TYPE contained in TAGS to the memory
+ range [ADDRESS, ADDRESS + LEN). */
+ virtual int store_memtags (CORE_ADDR address, size_t len,
+ const gdb::byte_vector &tags, int type)
+ TARGET_DEFAULT_IGNORE ();
};

/* Deleter for std::unique_ptr. See comments in
@@ -2312,6 +2328,15 @@ extern gdb::unique_xmalloc_ptr<char> target_fileio_read_stralloc
#define target_augmented_libraries_svr4_read() \
(current_top_target ()->augmented_libraries_svr4_read) ()

+#define target_supports_memory_tagging() \
+ ((current_top_target ()->supports_memory_tagging) ())
+
+#define target_fetch_memtags(address, len, tags, type) \
+ (current_top_target ()->fetch_memtags) ((address), (len), (tags), (type))
+
+#define target_store_memtags(address, len, tags, type) \
+ (current_top_target ()->store_memtags) ((address), (len), (tags), (type))
+
/* Command logging facility. */

#define target_log_command(p) \
--
2.17.1