Skip to contentSkip to conversation

plugins/stoptrigger: TCG plugin to stop execution under conditions

PATCH Superseded Simon Hamelin 3 participants 9 messages Jul 10, 2024 – Jul 16, 2024

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 plugins/stoptrigger: TCG plugin to stop execution under conditions

This new plugin allows to stop emulation using conditions on the emulation state. By setting this plugin arguments, it is possible to set an instruction count limit and/or trigger address(es) to stop at. The code returned at emulation exit can be customized.

This plugin demonstrates how someone could stop QEMU execution. It could be used for research purposes to launch some code and deterministically stop it and understand where its execution flow went.

---
v2:
- use a scoreboard for counting instructions
- no longer hook each instruction to exit at given address
- add exit_emulation function for future use case such as stopping the VM or triggering a gdbstub exception

 contrib/plugins/Makefile      |   1 +
 contrib/plugins/stoptrigger.c | 150 ++++++++++++++++++++++++++++++++++
 docs/devel/tcg-plugins.rst    |  22 +++++
 3 files changed, 173 insertions(+)
 create mode 100644 contrib/plugins/stoptrigger.c
contrib/plugins/Makefile +1 -0
index 449ead1130..98a89d5c40 100644
@@ -28,6 +28,7 @@ NAMES += hwprofile
 NAMES += cache
 NAMES += drcov
 NAMES += ips
+NAMES += stoptrigger
 
 ifeq ($(CONFIG_WIN32),y)
 SO_SUFFIX := .dll
contrib/plugins/stoptrigger.c new file +150 -0

150 lines not on this page · raw message

docs/devel/tcg-plugins.rst +22 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of them are optional:
   configuration arguments implies ``l2=on``.
   (default: N = 2097152 (2MB), B = 64, A = 16)
 
+- contrib/plugins/stoptrigger.c
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and precisely stop it
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions to stop at,
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return code can be
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
 Plugin API
 ==========
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 42 lines · Simon Hamelin

This new plugin allows to stop emulation using conditions on the emulation state. By setting this plugin arguments, it is possible to set an instruction count limit and/or trigger address(es) to stop at. The code returned at emulation exit can be customized.

This plugin demonstrates how someone could stop QEMU execution. It could be used for research purposes to launch some code and deterministically stop it and understand where its execution flow went.

---
v2:
- use a scoreboard for counting instructions
- no longer hook each instruction to exit at given address
- add exit_emulation function for future use case such as stopping the VM or triggering a gdbstub exception

 contrib/plugins/Makefile      |   1 +
 contrib/plugins/stoptrigger.c | 150 ++++++++++++++++++++++++++++++++++
 docs/devel/tcg-plugins.rst    |  22 +++++
 3 files changed, 173 insertions(+)
 create mode 100644 contrib/plugins/stoptrigger.c
contrib/plugins/Makefile +1 -0
index 449ead1130..98a89d5c40 100644
@@ -28,6 +28,7 @@ NAMES += hwprofile
 NAMES += cache
 NAMES += drcov
 NAMES += ips
+NAMES += stoptrigger
 
 ifeq ($(CONFIG_WIN32),y)
 SO_SUFFIX := .dll
contrib/plugins/stoptrigger.c new file +0 -0
new file mode 100644
index 0000000000..9c23f5854e

contrib/plugins/stoptrigger.c line 10

Quoted 10 lines
contrib/plugins/stoptrigger.c new file +10 -0
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2024, Simon Hamelin <[email protected]>
+ *
+ * Stop execution once a given address is reached or if the
+ * count of executed instructions reached a specified limit
+ *
+ * License: GNU GPL, version 2 or later.
+ *   See the COPYING file in the top-level directory.
+ */
+

you are missing a glib.h include here

Quoted 45 lines
contrib/plugins/stoptrigger.c new file +45 -0
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <qemu-plugin.h>
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
+
+/* Scoreboard to track executed instructions count */
+typedef struct {
+    uint64_t insn_count;
+} InstructionsCount;
+static struct qemu_plugin_scoreboard *insn_count_sb;
+static qemu_plugin_u64 insn_count;
+
+static uint64_t icount;
+static int icount_exit_code;
+
+static bool exit_on_icount;
+static bool exit_on_address;
+
+/* Map trigger addresses to exit code */
+static GHashTable *addrs_ht;
+static GMutex addrs_ht_lock;
+
+static void exit_emulation(int return_code)
+{
+    exit(return_code);
+}
+
+static void exit_icount_reached(unsigned int cpu_index, void *udata)
+{
+    qemu_plugin_outs("icount reached, exiting\n");
+    exit_emulation(icount_exit_code);
+}
+
+static void exit_address_reached(unsigned int cpu_index, void *udata)
+{
+    uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
+    g_mutex_lock(&addrs_ht_lock);
+    int exit_code = GPOINTER_TO_INT(
+        g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
+    g_mutex_unlock(&addrs_ht_lock);
+    char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n",
Quoted 1 line

insn_vaddr);

Dont intermix variable declarations, put them at the top of the block.

contrib/plugins/stoptrigger.c line 58

Quoted 3 lines
contrib/plugins/stoptrigger.c new file +3 -0
+    qemu_plugin_outs(msg);
+    exit_emulation(exit_code);
+}

How about something like:

  static void exit_emulation(int return_code, char *message)
  {
      qemu_plugin_outs(message);
      g_free(message);
      exit(return_code);
  }

  static void exit_icount_reached(unsigned int cpu_index, void *udata)
  {
      uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
      char *msg = g_strdup_printf("icount reached at 0x%" PRIx64 ", exiting\n", insn_vaddr);
      exit_emulation(icount_exit_code, msg);

}

  static void exit_address_reached(unsigned int cpu_index, void *udata)
  {
      uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
      char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n", insn_vaddr);
      int exit_code;
      g_mutex_lock(&addrs_ht_lock);
      exit_code = GPOINTER_TO_INT(
          g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
      g_mutex_unlock(&addrs_ht_lock);

      exit_emulation(exit_code, msg);

}

contrib/plugins/stoptrigger.c line 65

Quoted 7 lines
contrib/plugins/stoptrigger.c new file +7 -0
+
+static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb)
+{
+    size_t tb_n = qemu_plugin_tb_n_insns(tb);
+    for (size_t i = 0; i < tb_n; i++) {
+        struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i);
+        uint64_t insn_vaddr = qemu_plugin_insn_vaddr(insn);

might as well just cast it once:

        gpointer insn_vaddr = GUINT_TO_POINTER(qemu_plugin_insn_vaddr(insn));

contrib/plugins/stoptrigger.c line 73

Quoted 8 lines
contrib/plugins/stoptrigger.c new file +8 -0
+
+        if (exit_on_icount) {
+            /* Increment and check scoreboard for each instruction */
+            qemu_plugin_register_vcpu_insn_exec_inline_per_vcpu(
+                insn, QEMU_PLUGIN_INLINE_ADD_U64, insn_count, 1);
+            qemu_plugin_register_vcpu_insn_exec_cond_cb(
+                insn, exit_icount_reached, QEMU_PLUGIN_CB_NO_REGS,
+                QEMU_PLUGIN_COND_EQ, insn_count, icount + 1, NULL);

Might be useful to report the PC at which you reach the icount boundary

Quoted 41 lines
contrib/plugins/stoptrigger.c new file +38 -0
+        }
+
+        if (exit_on_address) {
+            g_mutex_lock(&addrs_ht_lock);
+            if (g_hash_table_contains(addrs_ht, GUINT_TO_POINTER(insn_vaddr))) {
+                /* Exit triggered by address */
+                qemu_plugin_register_vcpu_insn_exec_cb(
+                    insn, exit_address_reached, QEMU_PLUGIN_CB_NO_REGS,
+                    GUINT_TO_POINTER(insn_vaddr));
+            }
+            g_mutex_unlock(&addrs_ht_lock);
+        }
+    }
+}
+
+static void plugin_exit(qemu_plugin_id_t id, void *p)
+{
+    g_hash_table_destroy(addrs_ht);
+    qemu_plugin_scoreboard_free(insn_count_sb);
+}
+
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+                                           const qemu_info_t *info, int argc,
+                                           char **argv)
+{
+    addrs_ht = g_hash_table_new(NULL, g_direct_equal);
+
+    insn_count_sb = qemu_plugin_scoreboard_new(sizeof(InstructionsCount));
+    insn_count = qemu_plugin_scoreboard_u64_in_struct(
+        insn_count_sb, InstructionsCount, insn_count);
+
+    for (int i = 0; i < argc; i++) {
+        char *opt = argv[i];
+        g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
+        if (g_strcmp0(tokens[0], "icount") == 0) {
+            g_auto(GStrv) icount_tokens = g_strsplit(tokens[1], ":", 2);
+            icount = g_ascii_strtoull(icount_tokens[0], NULL, 0);
 
 
 
+            if (icount < 1 || g_strrstr(icount_tokens[0], "-") !=
Quoted 1 line

NULL) {

I don't think strstoull would even parse something with - in it so I would just do:

  if (icount == 0) {
     /* fail */
  }
Quoted 61 lines
contrib/plugins/stoptrigger.c new file +39 -0
+                fprintf(stderr,
+                        "icount parsing failed: '%s' must be a positive "
+                        "integer\n",
+                        icount_tokens[0]);
+                return -1;
+            }
+            if (icount_tokens[1]) {
+                icount_exit_code = g_ascii_strtoull(icount_tokens[1], NULL, 0);
+            }
+            exit_on_icount = true;
+        } else if (g_strcmp0(tokens[0], "addr") == 0) {
+            g_auto(GStrv) addr_tokens = g_strsplit(tokens[1], ":", 2);
+            uint64_t exit_addr = g_ascii_strtoull(addr_tokens[0], NULL, 0);
+            int exit_code = 0;
+            if (addr_tokens[1]) {
+                exit_code = g_ascii_strtoull(addr_tokens[1], NULL, 0);
+            }
+            g_mutex_lock(&addrs_ht_lock);
+            g_hash_table_insert(addrs_ht, GUINT_TO_POINTER(exit_addr),
+                                GINT_TO_POINTER(exit_code));
+            g_mutex_unlock(&addrs_ht_lock);
+            exit_on_address = true;
+        } else {
+            fprintf(stderr, "option parsing failed: %s\n", opt);
+            return -1;
+        }
+    }
+
+    if (!exit_on_icount && !exit_on_address) {
+        fprintf(stderr, "'icount' or 'addr' argument missing\n");
+        return -1;
+    }
+
+    /* Register translation block and exit callbacks */
+    qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
+    qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
+
+    return 0;
+}
docs/devel/tcg-plugins.rst +14 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of them are optional:
   configuration arguments implies ``l2=on``.
   (default: N = 2097152 (2MB), B = 64, A = 16)
 
+- contrib/plugins/stoptrigger.c
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and precisely stop it
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions to stop at,
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return code can be
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::

docs/devel/tcg-plugins.rst The plugin has a number of arguments, all of them are optional: · line 668

Quoted 10 lines
docs/devel/tcg-plugins.rst +8 -0
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
 Plugin API
 ==========

Otherwise it looks good to me. Unless you want to tackle additional exit modes?

What is your current use case for this?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#2 lore raw
Quoted 50 lines · Alex Bennée
Quoted 19 lines
contrib/plugins/stoptrigger.c new file +19 -0
+static void exit_emulation(int return_code)
+{
+    exit(return_code);
+}
+
+static void exit_icount_reached(unsigned int cpu_index, void *udata)
+{
+    qemu_plugin_outs("icount reached, exiting\n");
+    exit_emulation(icount_exit_code);
+}
+
+static void exit_address_reached(unsigned int cpu_index, void *udata)
+{
+    uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
+    g_mutex_lock(&addrs_ht_lock);
+    int exit_code = GPOINTER_TO_INT(
+        g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
+    g_mutex_unlock(&addrs_ht_lock);
+    char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n",
Quoted 1 line

insn_vaddr);

Dont intermix variable declarations, put them at the top of the block.

contrib/plugins/stoptrigger.c line 58

Quoted 3 lines
contrib/plugins/stoptrigger.c new file +3 -0
+    qemu_plugin_outs(msg);
+    exit_emulation(exit_code);
+}

How about something like:

   static void exit_emulation(int return_code, char *message)
   {
       qemu_plugin_outs(message);
       g_free(message);
       exit(return_code);
   }

   static void exit_icount_reached(unsigned int cpu_index, void *udata)
   {
       uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
       char *msg = g_strdup_printf("icount reached at 0x%" PRIx64 ", exiting\n", insn_vaddr);
       exit_emulation(icount_exit_code, msg);

}

   static void exit_address_reached(unsigned int cpu_index, void *udata)
   {
       uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
       char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n", insn_vaddr);
       int exit_code;
Quoted 9 lines
       g_mutex_lock(&addrs_ht_lock);
       exit_code = GPOINTER_TO_INT(
           g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
       g_mutex_unlock(&addrs_ht_lock);

       exit_emulation(exit_code, msg);

}

Looks good to me, will definitly put that in the next patch !

Quoted 22 lines
Quoted 20 lines
contrib/plugins/stoptrigger.c new file +17 -0
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+                                           const qemu_info_t *info, int argc,
+                                           char **argv)
+{
+    addrs_ht = g_hash_table_new(NULL, g_direct_equal);
+
+    insn_count_sb = qemu_plugin_scoreboard_new(sizeof(InstructionsCount));
+    insn_count = qemu_plugin_scoreboard_u64_in_struct(
+        insn_count_sb, InstructionsCount, insn_count);
+
+    for (int i = 0; i < argc; i++) {
+        char *opt = argv[i];
+        g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
+        if (g_strcmp0(tokens[0], "icount") == 0) {
+            g_auto(GStrv) icount_tokens = g_strsplit(tokens[1], ":", 2);
+            icount = g_ascii_strtoull(icount_tokens[0], NULL, 0);
 
 
 
+            if (icount < 1 || g_strrstr(icount_tokens[0], "-") !=
Quoted 1 line

NULL) {

Quoted 7 lines

I don't think strstoull would even parse something with - in it so I would just do:

   if (icount == 0) {
      /* fail */
   }

According to the GLib documentation: "Note that input with a leading minus sign (-) is accepted, and will return the negation of the parsed number, unless that would overflow a guint64". So i guess we need to keep this check.

Quoted 36 lines
Quoted 22 lines
docs/devel/tcg-plugins.rst +14 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of them are optional:
    configuration arguments implies ``l2=on``.
    (default: N = 2097152 (2MB), B = 64, A = 16)
 
+- contrib/plugins/stoptrigger.c
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and precisely stop it
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions to stop at,
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return code can be
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::

docs/devel/tcg-plugins.rst The plugin has a number of arguments, all of them are optional: · line 668

Quoted 10 lines
docs/devel/tcg-plugins.rst +8 -0
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
  Plugin API
  ==========

Otherwise it looks good to me. Unless you want to tackle additional exit modes?

Quoted 2 lines

What is your current use case for this?

I'm currently using this plugin to determine where my programm stop after a given number of instructions executed.

Signature · 1 line
Simon Hamelin
#3 lore raw

Hello Simon,

Quoted 136 lines · Simon Hamelin
Quoted 50 lines · Alex Bennée
Quoted 19 lines
contrib/plugins/stoptrigger.c new file +19 -0
+static void exit_emulation(int return_code)
+{
+    exit(return_code);
+}
+
+static void exit_icount_reached(unsigned int cpu_index, void *udata)
+{
+    qemu_plugin_outs("icount reached, exiting\n");
+    exit_emulation(icount_exit_code);
+}
+
+static void exit_address_reached(unsigned int cpu_index, void *udata)
+{
+    uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
+    g_mutex_lock(&addrs_ht_lock);
+    int exit_code = GPOINTER_TO_INT(
+        g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
+    g_mutex_unlock(&addrs_ht_lock);
+    char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n",
Quoted 1 line

insn_vaddr);

Dont intermix variable declarations, put them at the top of the block.

contrib/plugins/stoptrigger.c line 58

Quoted 3 lines
contrib/plugins/stoptrigger.c new file +3 -0
+    qemu_plugin_outs(msg);
+    exit_emulation(exit_code);
+}

How about something like:

    static void exit_emulation(int return_code, char *message)
    {
        qemu_plugin_outs(message);
        g_free(message);
        exit(return_code);
    }

    static void exit_icount_reached(unsigned int cpu_index, void *udata)
    {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("icount reached at 0x%" PRIx64 ", exiting\n", insn_vaddr);

        exit_emulation(icount_exit_code, msg);
    }

    static void exit_address_reached(unsigned int cpu_index, void *udata)
    {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n", insn_vaddr);
        int exit_code;
Quoted 9 lines
        g_mutex_lock(&addrs_ht_lock);
        exit_code = GPOINTER_TO_INT(
            g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
        g_mutex_unlock(&addrs_ht_lock);

        exit_emulation(exit_code, msg);
    }

Looks good to me, will definitly put that in the next patch !

Quoted 22 lines
Quoted 20 lines
contrib/plugins/stoptrigger.c new file +17 -0
+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
+                                           const qemu_info_t *info, int argc,
+                                           char **argv)
+{
+    addrs_ht = g_hash_table_new(NULL, g_direct_equal);
+
+    insn_count_sb = qemu_plugin_scoreboard_new(sizeof(InstructionsCount));
+    insn_count = qemu_plugin_scoreboard_u64_in_struct(
+        insn_count_sb, InstructionsCount, insn_count);
+
+    for (int i = 0; i < argc; i++) {
+        char *opt = argv[i];
+        g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
+        if (g_strcmp0(tokens[0], "icount") == 0) {
+            g_auto(GStrv) icount_tokens = g_strsplit(tokens[1], ":", 2);
+            icount = g_ascii_strtoull(icount_tokens[0], NULL, 0);
 
 
 
+            if (icount < 1 || g_strrstr(icount_tokens[0], "-") !=
Quoted 1 line

NULL) {

Quoted 7 lines

I don't think strstoull would even parse something with - in it so I would just do:

    if (icount == 0) {
       /* fail */
    }

According to the GLib documentation: "Note that input with a leading minus sign (-) is accepted, and will return the negation of the parsed number, unless that would overflow a guint64". So i guess we need to keep this check.

Quoted 36 lines
Quoted 22 lines
docs/devel/tcg-plugins.rst +14 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of them are optional:
     configuration arguments implies ``l2=on``.
     (default: N = 2097152 (2MB), B = 64, A = 16)
 
+- contrib/plugins/stoptrigger.c
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and precisely stop it
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions to stop at,
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return code can be
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::

docs/devel/tcg-plugins.rst The plugin has a number of arguments, all of them are optional: · line 668

Quoted 10 lines
docs/devel/tcg-plugins.rst +8 -0
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
   Plugin API
   ==========

Otherwise it looks good to me. Unless you want to tackle additional exit modes?

Quoted 6 lines
Quoted 2 lines

What is your current use case for this?

I'm currently using this plugin to determine where my programm stop after a given number of instructions executed.

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

#4 lore raw

Hello Pierrick,

Quoted 150 lines · Pierrick Bouvier

Hello Simon,

Quoted 147 lines · Simon Hamelin
Quoted 50 lines · Alex Bennée
Quoted 19 lines
contrib/plugins/stoptrigger.c new file +19 -0
+static void exit_emulation(int return_code)
+{
+    exit(return_code);
+}
+
+static void exit_icount_reached(unsigned int cpu_index, void *udata)
+{
+    qemu_plugin_outs("icount reached, exiting\n");
+    exit_emulation(icount_exit_code);
+}
+
+static void exit_address_reached(unsigned int cpu_index, void *udata)
+{
+    uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
+    g_mutex_lock(&addrs_ht_lock);
+    int exit_code = GPOINTER_TO_INT(
+        g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
+    g_mutex_unlock(&addrs_ht_lock);
+    char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n",
Quoted 1 line

insn_vaddr);

Dont intermix variable declarations, put them at the top of the block.

Quoted 3 lines

+    qemu_plugin_outs(msg);
+    exit_emulation(exit_code);
+}

How about something like:

    static void exit_emulation(int return_code, char *message) {
        qemu_plugin_outs(message);
        g_free(message);
        exit(return_code);
    }

    static void exit_icount_reached(unsigned int cpu_index, void *udata)
    {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("icount reached at 0x%" PRIx64 ", exiting\n", insn_vaddr);

        exit_emulation(icount_exit_code, msg);
    }

    static void exit_address_reached(unsigned int cpu_index, void *udata) {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n", insn_vaddr); int exit_code;

Quoted 9 lines

        g_mutex_lock(&addrs_ht_lock);
        exit_code = GPOINTER_TO_INT( g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr))); g_mutex_unlock(&addrs_ht_lock);

        exit_emulation(exit_code, msg);
    }

Looks good to me, will definitly put that in the next patch !

Quoted 25 lines
Quoted 22 lines

+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, +                                           const qemu_info_t *info, int argc,
+                                           char **argv)

contrib/plugins/stoptrigger.c new file +4 -0
+{
+    addrs_ht = g_hash_table_new(NULL, g_direct_equal);
+
+    insn_count_sb = 

qemu_plugin_scoreboard_new(sizeof(InstructionsCount)); +    insn_count = qemu_plugin_scoreboard_u64_in_struct( +        insn_count_sb, InstructionsCount, insn_count); +
+    for (int i = 0; i < argc; i++) {
+        char *opt = argv[i];
+        g_auto(GStrv) tokens = g_strsplit(opt, "=", 2); +        if (g_strcmp0(tokens[0], "icount") == 0) {
+            g_auto(GStrv) icount_tokens = g_strsplit(tokens[1], ":", 2);
+            icount = g_ascii_strtoull(icount_tokens[0], NULL, 0);

Quoted 2 lines

+            if (icount < 1 || g_strrstr(icount_tokens[0], "-") != NULL) {

Quoted 7 lines

I don't think strstoull would even parse something with - in it so I would just do:

    if (icount == 0) {
       /* fail */
    }

According to the GLib documentation: "Note that input with a leading minus sign (-) is accepted, and will return the negation of the parsed number, unless that would overflow a guint64". So i guess we need to keep this check.

Quoted 43 lines
Quoted 35 lines
docs/devel/tcg-plugins.rst +0 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of 

them are optional:
     configuration arguments implies ``l2=on``.
     (default: N = 2097152 (2MB), B = 64, A = 16)
+- contrib/plugins/stoptrigger.c

docs/devel/tcg-plugins.rst +3 -0
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and 

precisely stop it

docs/devel/tcg-plugins.rst +3 -0
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions to 

stop at,

docs/devel/tcg-plugins.rst +3 -0
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return code 

can be

docs/devel/tcg-plugins.rst +3 -0
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, 

at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin

docs/devel/tcg-plugins.rst +5 -0
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
Quoted 2 lines

   Plugin API
   ==========

Otherwise it looks good to me. Unless you want to tackle additional exit modes?

What is your current use case for this?

Quoted 6 lines
Quoted 3 lines

I'm currently using this plugin to determine where my programm stop after a given number of instructions executed.

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

I'm currently using this plugin for security analysis purposes. Basically my goal is to simulate fault injection using QEMU. To do so I'm using this plugin along with another plugin that skips an instruction at a given address. With this plugin I'm able to see how the program reacts to the fault and stop it with a custom return code. Basically there are 4 cases:
- The fault does not disrupt the program and it reaches the expected
address.
- The fault disrupt the program and it reaches an unexpected address
known as the "target" address.
- The fault disrupt the program in such a way that it executes a lot
of instructions without reaching the expected address, this situation is known as a "timeout".
- The fault crashes the guest, causing it to reach an error handler
address.

In short, here's a command line that illustrates how I use the plugin:

$ qemu-system-aarch64 $(QEMU_ARGS) -plugin ./contrib/plugins/libstoptrigger.so,icount=timeout_count,addr=addr=expected_addr:0,error_handler_addr:1,target_addr:2 -d plugin

--
Simon Hamelin

#5 lore raw
Quoted 170 lines · Simon Hamelin

Hello Pierrick,

Quoted 150 lines · Pierrick Bouvier

Hello Simon,

Quoted 147 lines · Simon Hamelin
Quoted 50 lines · Alex Bennée
Quoted 19 lines
contrib/plugins/stoptrigger.c new file +19 -0
+static void exit_emulation(int return_code)
+{
+    exit(return_code);
+}
+
+static void exit_icount_reached(unsigned int cpu_index, void *udata)
+{
+    qemu_plugin_outs("icount reached, exiting\n");
+    exit_emulation(icount_exit_code);
+}
+
+static void exit_address_reached(unsigned int cpu_index, void *udata)
+{
+    uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
+    g_mutex_lock(&addrs_ht_lock);
+    int exit_code = GPOINTER_TO_INT(
+        g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr)));
+    g_mutex_unlock(&addrs_ht_lock);
+    char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n",
Quoted 1 line

insn_vaddr);

Dont intermix variable declarations, put them at the top of the block.

Quoted 3 lines

+    qemu_plugin_outs(msg);
+    exit_emulation(exit_code);
+}

How about something like:

    static void exit_emulation(int return_code, char *message) {
        qemu_plugin_outs(message);
        g_free(message);
        exit(return_code);
    }

    static void exit_icount_reached(unsigned int cpu_index, void *udata)
    {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("icount reached at 0x%" PRIx64 ", exiting\n", insn_vaddr);

        exit_emulation(icount_exit_code, msg);
    }

    static void exit_address_reached(unsigned int cpu_index, void *udata) {
        uint64_t insn_vaddr = GPOINTER_TO_UINT(udata);
        char *msg = g_strdup_printf("0x%" PRIx64 " reached, exiting\n", insn_vaddr); int exit_code;

Quoted 9 lines

        g_mutex_lock(&addrs_ht_lock);
        exit_code = GPOINTER_TO_INT( g_hash_table_lookup(addrs_ht, GUINT_TO_POINTER(insn_vaddr))); g_mutex_unlock(&addrs_ht_lock);

        exit_emulation(exit_code, msg);
    }

Looks good to me, will definitly put that in the next patch !

Quoted 25 lines
Quoted 22 lines

+QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, +                                           const qemu_info_t *info, int argc,
+                                           char **argv)

contrib/plugins/stoptrigger.c new file +4 -0
+{
+    addrs_ht = g_hash_table_new(NULL, g_direct_equal);
+
+    insn_count_sb = 

qemu_plugin_scoreboard_new(sizeof(InstructionsCount)); +    insn_count = qemu_plugin_scoreboard_u64_in_struct( +        insn_count_sb, InstructionsCount, insn_count); +
+    for (int i = 0; i < argc; i++) {
+        char *opt = argv[i];
+        g_auto(GStrv) tokens = g_strsplit(opt, "=", 2); +        if (g_strcmp0(tokens[0], "icount") == 0) {
+            g_auto(GStrv) icount_tokens = g_strsplit(tokens[1], ":", 2);
+            icount = g_ascii_strtoull(icount_tokens[0], NULL, 0);

Quoted 2 lines

+            if (icount < 1 || g_strrstr(icount_tokens[0], "-") != NULL) {

Quoted 7 lines

I don't think strstoull would even parse something with - in it so I would just do:

    if (icount == 0) {
       /* fail */
    }

According to the GLib documentation: "Note that input with a leading minus sign (-) is accepted, and will return the negation of the parsed number, unless that would overflow a guint64". So i guess we need to keep this check.

Quoted 43 lines
Quoted 35 lines
docs/devel/tcg-plugins.rst +0 -0
index f7d7b9e3a4..954623f9bf 100644
@@ -642,6 +642,28 @@ The plugin has a number of arguments, all of 

them are optional:
     configuration arguments implies ``l2=on``.
     (default: N = 2097152 (2MB), B = 64, A = 16)
+- contrib/plugins/stoptrigger.c

docs/devel/tcg-plugins.rst +3 -0
+
+The stoptrigger plugin allows to setup triggers to stop emulation.
+It can be used for research purposes to launch some code and 

precisely stop it

docs/devel/tcg-plugins.rst +3 -0
+and understand where its execution flow went.
+
+Two types of triggers can be configured: a count of instructions 

to stop at,

docs/devel/tcg-plugins.rst +3 -0
+or an address to stop at. Multiple triggers can be set at once.
+
+By default, QEMU will exit with return code 0. A custom return 

code can be

docs/devel/tcg-plugins.rst +3 -0
+configured for each trigger using ``:CODE`` syntax.
+
+For example, to stop at the 20-th instruction with return code 41, 

at address
+0xd4 with return code 0 or at address 0xd8 with return code 42::
+
+  $ qemu-system-aarch64 $(QEMU_ARGS) \
+    -plugin ./contrib/plugins/libstoptrigger.so,icount=20:41,addr=0xd4,addr=0xd8:42 -d plugin

docs/devel/tcg-plugins.rst +5 -0
+
+The plugin will log the reason of exit, for example::
+
+  0xd4 reached, exiting
+
Quoted 2 lines

   Plugin API
   ==========

Otherwise it looks good to me. Unless you want to tackle additional exit modes?

What is your current use case for this?

Quoted 6 lines
Quoted 3 lines

I'm currently using this plugin to determine where my programm stop after a given number of instructions executed.

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

I'm currently using this plugin for security analysis purposes. Basically my goal is to simulate fault injection using QEMU. To do so I'm using this plugin along with another plugin that skips an instruction at a given address. With this plugin I'm able to see how the program reacts to the fault and stop it with a custom return code. Basically there are 4 cases:
   - The fault does not disrupt the program and it reaches the expected
address.
   - The fault disrupt the program and it reaches an unexpected address
known as the "target" address.
   - The fault disrupt the program in such a way that it executes a lot
of instructions without reaching the expected address, this situation is known as a "timeout".
   - The fault crashes the guest, causing it to reach an error handler
address.

Quoted 6 lines

In short, here's a command line that illustrates how I use the plugin:

$ qemu-system-aarch64 $(QEMU_ARGS) -plugin ./contrib/plugins/libstoptrigger.so,icount=timeout_count,addr=addr=expected_addr:0,error_handler_addr:1,target_addr:2 -d plugin
Signature · 1 line
Simon Hamelin

Look like I made a little mistake in the command line example, here is the correct one:

qemu-system-aarch64 $(QEMU_ARGS) -plugin ./contrib/plugins/libstoptrigger.so,icount=timeout_count,addr=expected_addr:0,addr=error_handler_addr:1,addr=target_addr:2 -d plugin

--
Simon Hamelin

#6 lore raw
In reply to Simon Hamelin
Quoted 5 lines · Simon Hamelin

Hello Pierrick,

Quoted 2 lines

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

Quoted 16 lines

I'm currently using this plugin for security analysis purposes. Basically my goal is to simulate fault injection using QEMU. To do so I'm using this plugin along with another plugin that skips an instruction at a given address. With this plugin I'm able to see how the program reacts to the fault and stop it with a custom return code. Basically there are 4 cases:
- The fault does not disrupt the program and it reaches the expected
address.
- The fault disrupt the program and it reaches an unexpected address
known as the "target" address.
- The fault disrupt the program in such a way that it executes a lot
of instructions without reaching the expected address, this situation is known as a "timeout".
- The fault crashes the guest, causing it to reach an error handler
address.

Thanks for sharing this, it's an interesting use case.

How did you implement instruction skipping?
Is that based on current API, or do you have a QEMU fork?

Quoted 8 lines

In short, here's a command line that illustrates how I use the plugin:

$ qemu-system-aarch64 $(QEMU_ARGS) -plugin
./contrib/plugins/libstoptrigger.so,icount=timeout_count,addr=addr=expected_addr:0,error_handler_addr:1,target_addr:2
-d plugin

--
Simon Hamelin

#7 lore raw
Quoted 23 lines · Pierrick Bouvier
Quoted 5 lines · Simon Hamelin

Hello Pierrick,

Quoted 2 lines

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

Quoted 16 lines

I'm currently using this plugin for security analysis purposes. Basically my goal is to simulate fault injection using QEMU. To do so I'm using this plugin along with another plugin that skips an instruction at a given address. With this plugin I'm able to see how the program reacts to the fault and stop it with a custom return code. Basically there are 4 cases:
     - The fault does not disrupt the program and it reaches the expected address.
     - The fault disrupt the program and it reaches an unexpected address known as the "target" address.
     - The fault disrupt the program in such a way that it executes a lot of instructions without reaching the expected address, this situation is known as a "timeout".
     - The fault crashes the guest, causing it to reach an error handler address.

Quoted 5 lines

Thanks for sharing this, it's an interesting use case.

How did you implement instruction skipping?
Is that based on current API, or do you have a QEMU fork?

I had to extend the current API to support writing guest registers, specifically I'm skipping instructions by writing to the PC register. To do this I use the set_pc function of the CPUClass struct. This requires a bit of a hack to make the TCG react to the PC change as it's normal behavior is to execute the entire TB and ignore the PC change. To make the TCG react to this modification the only way I've found is to leave it midway it's execution using the cpu_loop_exit function.

#8 lore raw
Quoted 32 lines · Simon Hamelin
Quoted 23 lines · Pierrick Bouvier
Quoted 5 lines · Simon Hamelin

Hello Pierrick,

Quoted 2 lines

Could you share a bit more information on the final goal, if possible? Is that used for fuzzing binaries, security analysis, or other things?

Quoted 16 lines

I'm currently using this plugin for security analysis purposes. Basically my goal is to simulate fault injection using QEMU. To do so I'm using this plugin along with another plugin that skips an instruction at a given address. With this plugin I'm able to see how the program reacts to the fault and stop it with a custom return code. Basically there are 4 cases:
     - The fault does not disrupt the program and it reaches the expected
address.
     - The fault disrupt the program and it reaches an unexpected address
known as the "target" address.
     - The fault disrupt the program in such a way that it executes a lot
of instructions without reaching the expected address, this situation is known as a "timeout".
     - The fault crashes the guest, causing it to reach an error handler
address.

Quoted 5 lines

Thanks for sharing this, it's an interesting use case.

How did you implement instruction skipping?
Is that based on current API, or do you have a QEMU fork?

Quoted 8 lines

I had to extend the current API to support writing guest registers, specifically I'm skipping instructions by writing to the PC register. To do this I use the set_pc function of the CPUClass struct. This requires a bit of a hack to make the TCG react to the PC change as it's normal behavior is to execute the entire TB and ignore the PC change. To make the TCG react to this modification the only way I've found is to leave it midway it's execution using the cpu_loop_exit function.

Ok.
So far, our API is designed to not allow modification on guest side (code or data wise). It's definitely a discussion we should start at some point, as it may be needed for specific use cases.

Thanks!