From nobody Sat Nov 15 16:43:23 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1750787721; cv=none; d=zohomail.com; s=zohoarc; b=jMDg6dAeYC0O3c22McxGYBjeb8uKVyDYUnKyTbHYhX5nH3jbI0M3OPuv17+uV+unlcdNSIfUsFhUh4YRTcAbouKe3hFDGgZPC+Hpge8FwDLGYxlhu74gg6Z50gu7ChRLgqE6L4NeGb5OMtWwI6ukCjZFh62pL/yXC06mFePrjAk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1750787721; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Y+Cga35u/V2dpyJodTGHeWA+Et2BkhQsp9yfYqFvkQ8=; b=kUcb9LJmtU/CWyS1Q070Bpoc7ebAwBQqeBNBh/8A1ZUFq2qrMVvsvgz2iQ9u5uXzFAwVsY4UB1l+9owszxpZqKcEKS8C8aN0waki3ndm4Agpo8SUsAhTHRKKiS5Bqb/0UXFo559V1+fiTYrlbsZNV1LZ5bFGFk6uVY8RxsAQcCg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17507877219231017.3408282375703; Tue, 24 Jun 2025 10:55:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uU7qY-00032n-Oi; Tue, 24 Jun 2025 13:54:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uU7qP-0002yW-O1 for qemu-devel@nongnu.org; Tue, 24 Jun 2025 13:54:08 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uU7qL-0002mf-3P for qemu-devel@nongnu.org; Tue, 24 Jun 2025 13:54:03 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-b31c978688dso604876a12.1 for ; Tue, 24 Jun 2025 10:54:00 -0700 (PDT) Received: from shemhazi.lan ([50.46.174.34]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b31f12427b7sm9318153a12.40.2025.06.24.10.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Jun 2025 10:53:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750787639; x=1751392439; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y+Cga35u/V2dpyJodTGHeWA+Et2BkhQsp9yfYqFvkQ8=; b=hU3fy2rSlV/jXgno1ORL5+A3KMTGDAo6NuxZ1TW5cnEbPTPywgQH5+O48m3HASmoUc /Y+FhAAVAzhkEh2OGS7RY298+kEmUn2xe8ajQ/mybxX3+T6RmPomfr9GuI1lhrii3giz MkpArKCG4aeZ+1weESdGLoE9kSXZOfOjOrUJvKErbP17vmsp3yXRWQ1g62ZBJv4+BsUT sgrWxMgi6u6hEF91LoRdifE/CS4fQI3yE972AVmdBxpbywoprDrT3gVePtYBwkN+PG2E yeNibQCaSg33BR7eyTOAIb+u22pn6kyx67r0tH2y/yuq0ibdtxUI+LkNYTiSe2udbpGY jTCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750787639; x=1751392439; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y+Cga35u/V2dpyJodTGHeWA+Et2BkhQsp9yfYqFvkQ8=; b=dNOFYFXNh56Us734M8AYCyWQCa4wdAzUM10Y4lTpN2ZekeRY4R0/QsWxuKnTLOd1lh zw1nLionb1cRuryrJzui60pOlUqS1NRISEgie5aF4ADSgDSUpQyCRhoymFwj/upQNuXI rKI2FUZymo4alKWRO61LMSyVh3NfmqWwIW41d085DfUheq8Bj+BAXSIkdXxEq53Vv47M n7jHfTxxrJu6JvjoW1lWhvlm6kIwFc004UmA5naqCsDJa005y6CpAyVik7j5lJoA92yY 0bXz5SNLWWwG5M/y020AWNv1twfzxD0SE2xUMU2iMPwpcZXuRT0hVDPzlfGPV+jD8Bqx JCyQ== X-Gm-Message-State: AOJu0Yz0QQMRjgX/E9VBfYvK+zmpiBAumVp6NeH8uJkhBbDcnrJ3vkqa H/XzAMWggAjSJTg76cBajiD6g7xmsvqfJizKAKXrjnb8pWeXFHusJnKsU913eiA+7IA= X-Gm-Gg: ASbGncv3hPYxgabWwvD3SC6E3UzREeMchvYkSSbCuOp95lkcQQeiOcWT5DnSV2Rxv+4 ihRrQ9vdwqoDMbl+Mqf49WEM/NofY5Ajlxe5dJ20OrShR2GCdlaQEfY49MQKBWX6eW4b4txlsps 9rvD2vwQgzhJwjTRtHxvKlNArjS2P4pgRqHNODvcnGklIXfp48F6OJnuoqJDeUXzWLGOxNEaucF ZwX0wI+XBpcArFAmJg443GRzVZP1ubb/lPfRLhhOAPFEo1AEMMTLVEHU1//pWtki90loOv0qZLA DYfhz32j8elfurlRhhlwPpL5343G3fEEfPwrGO+b1uETLxTsG6d6mhylkG+Mbg== X-Google-Smtp-Source: AGHT+IEss7mVJII0N/FGOaEP6zOohlbhIYoV5+9r+zw7gq4nhuk8dqVOYLq/Ro7YMz02bsi2WMJsHA== X-Received: by 2002:a05:6a21:100c:b0:220:41f6:7a6c with SMTP id adf61e73a8af0-2207cd6d697mr1059584637.40.1750787638842; Tue, 24 Jun 2025 10:53:58 -0700 (PDT) From: Rowan Hart To: qemu-devel@nongnu.org Cc: Pierrick Bouvier , Richard Henderson , Mahmoud Mandour , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Paolo Bonzini , Marcel Apfelbaum , Yanan Wang , Alexandre Iooss , Eduardo Habkost , Zhao Liu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Rowan Hart Subject: [PATCH v14 7/8] plugins: Add patcher plugin and test Date: Tue, 24 Jun 2025 10:53:50 -0700 Message-ID: <20250624175351.440780-8-rowanbhart@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250624175351.440780-1-rowanbhart@gmail.com> References: <20250624175351.440780-1-rowanbhart@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=rowanbhart@gmail.com; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1750787722660116600 Content-Type: text/plain; charset="utf-8" This patch adds a plugin that exercises the virtual and hardware memory read-write API functions added in a previous patch. The plugin takes a target and patch byte sequence, and will overwrite any instruction matching the target byte sequence with the patch. Reviewed-by: Pierrick Bouvier Signed-off-by: Rowan Hart --- tests/tcg/Makefile.target | 7 +- tests/tcg/plugins/meson.build | 2 +- tests/tcg/plugins/patch.c | 241 ++++++++++++++++++++++ tests/tcg/x86_64/Makefile.softmmu-target | 7 + tests/tcg/x86_64/system/patch-target.c | 22 ++ tests/tcg/x86_64/system/validate-patch.py | 39 ++++ 6 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/plugins/patch.c create mode 100644 tests/tcg/x86_64/system/patch-target.c create mode 100755 tests/tcg/x86_64/system/validate-patch.py diff --git a/tests/tcg/Makefile.target b/tests/tcg/Makefile.target index 95ff76ea44..af68f11664 100644 --- a/tests/tcg/Makefile.target +++ b/tests/tcg/Makefile.target @@ -151,7 +151,12 @@ ifeq ($(CONFIG_PLUGIN),y) PLUGIN_SRC=3D$(SRC_PATH)/tests/tcg/plugins PLUGIN_LIB=3D../plugins VPATH+=3D$(PLUGIN_LIB) -PLUGINS=3D$(patsubst %.c, lib%.so, $(notdir $(wildcard $(PLUGIN_SRC)/*.c))) +# Some plugins need to be disabled for all tests to avoid exponential expl= osion. +# For example, libpatch.so only needs to run against the arch-specific pat= ch +# target test, so we explicitly run it in the arch-specific Makefile. +DISABLE_PLUGINS=3Dlibpatch.so +PLUGINS=3D$(filter-out $(DISABLE_PLUGINS), \ + $(patsubst %.c, lib%.so, $(notdir $(wildcard $(PLUGIN_SRC)/*.c)))) =20 # We need to ensure expand the run-plugin-TEST-with-PLUGIN # pre-requistes manually here as we can't use stems to handle it. We diff --git a/tests/tcg/plugins/meson.build b/tests/tcg/plugins/meson.build index 029342282a..61a007d9e7 100644 --- a/tests/tcg/plugins/meson.build +++ b/tests/tcg/plugins/meson.build @@ -1,6 +1,6 @@ t =3D [] if get_option('plugins') - foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall'] + foreach i : ['bb', 'empty', 'inline', 'insn', 'mem', 'reset', 'syscall',= 'patch'] if host_os =3D=3D 'windows' t +=3D shared_module(i, files(i + '.c') + '../../../contrib/plugins/= win32_linker.c', include_directories: '../../../include/qemu', diff --git a/tests/tcg/plugins/patch.c b/tests/tcg/plugins/patch.c new file mode 100644 index 0000000000..450fc51c88 --- /dev/null +++ b/tests/tcg/plugins/patch.c @@ -0,0 +1,241 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This plugin patches instructions matching a pattern to a different + * instruction as they execute + * + */ + +#include "glib.h" +#include "glibconfig.h" + +#include +#include +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +static bool use_hwaddr; +static GByteArray *target_data; +static GByteArray *patch_data; + +/** + * Parse a string of hexadecimal digits into a GByteArray. The string must= be + * even length + */ +static GByteArray *str_to_bytes(const char *str) +{ + size_t len =3D strlen(str); + + if (len =3D=3D 0 || len % 2 !=3D 0) { + return NULL; + } + + GByteArray *bytes =3D g_byte_array_new(); + char byte[3] =3D {0}; + guint8 value =3D 0; + + for (size_t i =3D 0; i < len; i +=3D 2) { + byte[0] =3D str[i]; + byte[1] =3D str[i + 1]; + value =3D (guint8)g_ascii_strtoull(byte, NULL, 16); + g_byte_array_append(bytes, &value, 1); + } + + return bytes; +} + +static void patch_hwaddr(unsigned int vcpu_index, void *userdata) +{ + uint64_t addr =3D (uint64_t)userdata; + g_autoptr(GString) str =3D g_string_new(NULL); + g_string_printf(str, "patching: @0x%" + PRIx64 "\n", + addr); + qemu_plugin_outs(str->str); + + enum qemu_plugin_hwaddr_operation_result result =3D + qemu_plugin_write_memory_hwaddr(addr, patch_data); + + + if (result !=3D QEMU_PLUGIN_HWADDR_OPERATION_OK) { + g_autoptr(GString) errmsg =3D g_string_new(NULL); + g_string_printf(errmsg, "Failed to write memory: %d\n", result); + qemu_plugin_outs(errmsg->str); + return; + } + + GByteArray *read_data =3D g_byte_array_new(); + + result =3D qemu_plugin_read_memory_hwaddr(addr, read_data, + patch_data->len); + + qemu_plugin_outs("Reading memory...\n"); + + if (result !=3D QEMU_PLUGIN_HWADDR_OPERATION_OK) { + g_autoptr(GString) errmsg =3D g_string_new(NULL); + g_string_printf(errmsg, "Failed to read memory: %d\n", result); + qemu_plugin_outs(errmsg->str); + return; + } + + if (memcmp(patch_data->data, read_data->data, patch_data->len) !=3D 0)= { + qemu_plugin_outs("Failed to read back written data\n"); + } + + qemu_plugin_outs("Success!\n"); + + return; +} + +static void patch_vaddr(unsigned int vcpu_index, void *userdata) +{ + uint64_t addr =3D (uint64_t)userdata; + uint64_t hwaddr =3D 0; + if (!qemu_plugin_translate_vaddr(addr, &hwaddr)) { + qemu_plugin_outs("Failed to translate vaddr\n"); + return; + } + g_autoptr(GString) str =3D g_string_new(NULL); + g_string_printf(str, "patching: @0x%" + PRIx64 " hw: @0x%" PRIx64 "\n", + addr, hwaddr); + qemu_plugin_outs(str->str); + + qemu_plugin_outs("Writing memory (vaddr)...\n"); + + if (!qemu_plugin_write_memory_vaddr(addr, patch_data)) { + qemu_plugin_outs("Failed to write memory\n"); + return; + } + + qemu_plugin_outs("Reading memory (vaddr)...\n"); + + g_autoptr(GByteArray) read_data =3D g_byte_array_new(); + + if (!qemu_plugin_read_memory_vaddr(addr, read_data, patch_data->len)) { + qemu_plugin_outs("Failed to read memory\n"); + return; + } + + if (memcmp(patch_data->data, read_data->data, patch_data->len) !=3D 0)= { + qemu_plugin_outs("Failed to read back written data\n"); + } + + qemu_plugin_outs("Success!\n"); + + return; +} + +/* + * Callback on translation of a translation block. + */ +static void vcpu_tb_trans_cb(qemu_plugin_id_t id, struct qemu_plugin_tb *t= b) +{ + uint64_t addr =3D 0; + g_autoptr(GByteArray) insn_data =3D g_byte_array_new(); + for (size_t i =3D 0; i < qemu_plugin_tb_n_insns(tb); i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + + if (use_hwaddr) { + uint64_t vaddr =3D qemu_plugin_insn_vaddr(insn); + if (!qemu_plugin_translate_vaddr(vaddr, &addr)) { + qemu_plugin_outs("Failed to translate vaddr\n"); + continue; + } + } else { + addr =3D qemu_plugin_insn_vaddr(insn); + } + + g_byte_array_set_size(insn_data, qemu_plugin_insn_size(insn)); + qemu_plugin_insn_data(insn, insn_data->data, insn_data->len); + + if (insn_data->len >=3D target_data->len && + !memcmp(insn_data->data, target_data->data, + MIN(target_data->len, insn_data->len))) { + if (use_hwaddr) { + qemu_plugin_register_vcpu_tb_exec_cb(tb, patch_hwaddr, + QEMU_PLUGIN_CB_NO_REG= S, + (void *)addr); + } else { + qemu_plugin_register_vcpu_tb_exec_cb(tb, patch_vaddr, + QEMU_PLUGIN_CB_NO_REG= S, + (void *)addr); + } + } + } +} + +static void usage(void) +{ + fprintf(stderr, "Usage: ,target=3D,patch=3D" + "[,use_hwaddr=3Dtrue|false]"); +} + +/* + * Called when the plugin is installed + */ +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, int ar= gc, + char **argv) +{ + + use_hwaddr =3D true; + target_data =3D NULL; + patch_data =3D NULL; + + if (argc > 4) { + usage(); + return -1; + } + + for (size_t i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + g_auto(GStrv) tokens =3D g_strsplit(opt, "=3D", 2); + if (g_strcmp0(tokens[0], "use_hwaddr") =3D=3D 0) { + if (!qemu_plugin_bool_parse(tokens[0], tokens[1], &use_hwaddr)= ) { + fprintf(stderr, + "Failed to parse boolean argument use_hwaddr\n"); + return -1; + } + } else if (g_strcmp0(tokens[0], "target") =3D=3D 0) { + target_data =3D str_to_bytes(tokens[1]); + if (!target_data) { + fprintf(stderr, + "Failed to parse target bytes.\n"); + return -1; + } + } else if (g_strcmp0(tokens[0], "patch") =3D=3D 0) { + patch_data =3D str_to_bytes(tokens[1]); + if (!patch_data) { + fprintf(stderr, "Failed to parse patch bytes.\n"); + return -1; + } + } else { + fprintf(stderr, "Unknown argument: %s\n", tokens[0]); + usage(); + return -1; + } + } + + if (!target_data) { + fprintf(stderr, "target argument is required\n"); + usage(); + return -1; + } + + if (!patch_data) { + fprintf(stderr, "patch argument is required\n"); + usage(); + return -1; + } + + if (target_data->len !=3D patch_data->len) { + fprintf(stderr, "Target and patch data must be the same length\n"); + return -1; + } + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans_cb); + + return 0; +} diff --git a/tests/tcg/x86_64/Makefile.softmmu-target b/tests/tcg/x86_64/Ma= kefile.softmmu-target index d3e09708a5..9ea24257d2 100644 --- a/tests/tcg/x86_64/Makefile.softmmu-target +++ b/tests/tcg/x86_64/Makefile.softmmu-target @@ -33,3 +33,10 @@ memory: CFLAGS+=3D-DCHECK_UNALIGNED=3D1 =20 # Running QEMU_OPTS+=3D-device isa-debugcon,chardev=3Doutput -device isa-debug-exit,= iobase=3D0xf4,iosize=3D0x4 -kernel + +run-plugin-patch-target-with-libpatch.so: \ + PLUGIN_ARGS=3D$(COMMA)target=3Dffc0$(COMMA)patch=3D9090$(COMMA)use_hwaddr= =3Dtrue +run-plugin-patch-target-with-libpatch.so: \ + CHECK_PLUGIN_OUTPUT_COMMAND=3D$(X64_SYSTEM_SRC)/validate-patch.py $@.out +run-plugin-patch-target-with-libpatch.so: patch-target libpatch.so +EXTRA_RUNS+=3Drun-plugin-patch-target-with-libpatch.so diff --git a/tests/tcg/x86_64/system/patch-target.c b/tests/tcg/x86_64/syst= em/patch-target.c new file mode 100644 index 0000000000..8c2b6f4ba7 --- /dev/null +++ b/tests/tcg/x86_64/system/patch-target.c @@ -0,0 +1,22 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This test target increments a value 100 times. The patcher converts the + * inc instruction to a nop, so it only increments the value once. + * + */ +#include + +int main(void) +{ + ml_printf("Running test...\n"); + unsigned int x =3D 0; + for (int i =3D 0; i < 100; i++) { + asm volatile ( + "inc %[x]" + : [x] "+a" (x) + ); + } + ml_printf("Value: %d\n", x); + return 0; +} diff --git a/tests/tcg/x86_64/system/validate-patch.py b/tests/tcg/x86_64/s= ystem/validate-patch.py new file mode 100755 index 0000000000..700950eae5 --- /dev/null +++ b/tests/tcg/x86_64/system/validate-patch.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# +# validate-patch.py: check the patch applies +# +# This program takes two inputs: +# - the plugin output +# - the binary output +# +# Copyright (C) 2024 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import sys +from argparse import ArgumentParser + +def main() -> None: + """ + Process the arguments, injest the program and plugin out and + verify they match up and report if they do not. + """ + parser =3D ArgumentParser(description=3D"Validate patch") + parser.add_argument('test_output', + help=3D"The output from the test itself") + parser.add_argument('plugin_output', + help=3D"The output from plugin") + args =3D parser.parse_args() + + with open(args.test_output, 'r') as f: + test_data =3D f.read() + with open(args.plugin_output, 'r') as f: + plugin_data =3D f.read() + if "Value: 1" in test_data: + sys.exit(0) + else: + sys.exit(1) + +if __name__ =3D=3D "__main__": + main() + --=20 2.49.0