From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
James Clark <james.clark@linaro.org>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Clark Williams <williams@redhat.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH v10 07/16] perf tests: Exercise the x86 operand parser
Date: Sat, 21 Nov 2026 00:20:43 -0300 [thread overview]
Message-ID: <20261121032052.1392234-8-acme@kernel.org> (raw)
In-Reply-To: <20261121032052.1392234-1-acme@kernel.org>
From: Arnaldo Carvalho de Melo <acme@redhat.com>
Add a unit test for the x86 operand parser covering the absolute
addressing and movabs forms the data type resolver handles. The cases
are the instructions from the allmodconfig clang build quoted in the two
fixes.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/annotate-ops.c | 127 ++++++++++++++++++++++++++++++++
tools/perf/tests/builtin-test.c | 1 +
tools/perf/tests/tests.h | 1 +
4 files changed, 130 insertions(+)
create mode 100644 tools/perf/tests/annotate-ops.c
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 5eebb2ec5ddefcfd..c7edd8569bbccb06 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -2,6 +2,7 @@
perf-test-y += builtin-test.o
perf-test-y += tests-scripts.o
+perf-test-y += annotate-ops.o
perf-test-y += parse-events.o
perf-test-y += uncore-event-sorting.o
perf-test-y += dso-data.o
diff --git a/tools/perf/tests/annotate-ops.c b/tools/perf/tests/annotate-ops.c
new file mode 100644
index 0000000000000000..1c73433fbac67d85
--- /dev/null
+++ b/tools/perf/tests/annotate-ops.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * The x86 operand parser: what is a memory reference and where it points.
+ *
+ * The absolute addressing forms are the ones perf annotate's data type
+ * resolver has to follow, and the ones it must not mistake for a data
+ * access: a direct branch target and an immediate.
+ */
+#include <elf.h>
+#include <linux/compiler.h>
+#include <stdlib.h>
+#include <string.h>
+#include "annotate.h"
+#include "debug.h"
+#include "disasm.h"
+#include "tests.h"
+
+struct op_case {
+ const char *insn;
+ const struct ins_ops *ops;
+ const char *operand;
+ enum annotated_insn_ops which;
+ bool mem_ref;
+ int offset;
+};
+
+static struct op_case op_cases[] = {
+ /* 32-bit absolute, and the 64-bit sign extended disp32 both ways */
+ { "mov", &mov_ops, "0x8c73448", INSN_OP_SOURCE, true, 0x8c73448 },
+ { "mov", &mov_ops, "0xffffffffff5fc300", INSN_OP_SOURCE, true, -0xa03d00 },
+ { "mov", &mov_ops, "-0xa03d00", INSN_OP_SOURCE, true, -0xa03d00 },
+ /* An immediate and a direct branch target, with the same 0x */
+ { "mov", &mov_ops, "$0x10", INSN_OP_SOURCE, false, 0 },
+ { "call", &call_ops, "0x401020", INSN_OP_TARGET, false, 0 },
+ { "jmp", &jump_ops, "0x401020", INSN_OP_TARGET, false, 0 },
+ { "loop", NULL, "0x401020", INSN_OP_TARGET, false, 0 },
+};
+
+static int check_operand(const struct arch *arch, const struct op_case *c)
+{
+ struct disasm_line dl = {
+ .ins = { .name = c->insn, .ops = c->ops },
+ };
+ struct annotated_insn_loc loc;
+ struct annotated_op_loc *op;
+ int ret = 0;
+
+ if (c->which == INSN_OP_TARGET)
+ dl.ops.target.raw = (char *)c->operand;
+ else
+ dl.ops.source.raw = (char *)c->operand;
+
+ if (annotate_get_insn_location(arch, &dl, &loc) < 0)
+ return TEST_FAIL;
+
+ op = &loc.ops[c->which];
+ if (op->mem_ref != c->mem_ref) {
+ pr_debug("'%s %s': mem_ref %d, expected %d\n", c->insn,
+ c->operand, op->mem_ref, c->mem_ref);
+ ret = TEST_FAIL;
+ }
+ if (op->mem_ref && op->offset != c->offset) {
+ pr_debug("'%s %s': offset %#x, expected %#x\n", c->insn,
+ c->operand, op->offset, c->offset);
+ ret = TEST_FAIL;
+ }
+
+ return ret;
+}
+
+/* Real disassembly of a movabs, through the instruction's own parser */
+static const char *movabs_lines[][2] = {
+ { "movabsq", "$-0x2000040000000000, %r15 # imm = 0xDFFFFC0000000000" },
+ { "movabs", "$0xdfffffc000000000,%r15" },
+};
+
+static int check_movabs(const struct arch *arch, const char *name, const char *ops)
+{
+ struct disasm_line dl = { .ins = { .name = name } };
+ struct annotated_insn_loc loc;
+ int ret = TEST_FAIL;
+
+ dl.ops.raw = strdup(ops);
+ if (dl.ops.raw == NULL)
+ return TEST_FAIL;
+
+ dl.ins.ops = ins__find(arch, name, &dl);
+ if (dl.ins.ops != &mov_ops || dl.ins.ops->parse(arch, &dl.ops, NULL, &dl) < 0) {
+ pr_debug("'%s %s': not parsed as a mov\n", name, ops);
+ goto out;
+ }
+
+ if (annotate_get_insn_location(arch, &dl, &loc) == 0 &&
+ !loc.ops[INSN_OP_SOURCE].mem_ref && loc.ops[INSN_OP_SOURCE].imm &&
+ loc.ops[INSN_OP_TARGET].reg1 != -1)
+ ret = TEST_OK;
+ else
+ pr_debug("'%s %s': not an immediate to a register\n", name, ops);
+out:
+ free(dl.ops.source.raw);
+ free(dl.ops.target.raw);
+ free(dl.ops.raw);
+ return ret;
+}
+
+static int test__annotate_ops(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ struct e_machine_and_e_flags id = { .e_machine = EM_X86_64, };
+ const struct arch *arch = arch__new_x86(&id, NULL);
+ size_t i;
+ int ret = 0;
+
+ if (arch == NULL)
+ return TEST_FAIL;
+
+ for (i = 0; i < ARRAY_SIZE(op_cases); i++)
+ ret |= check_operand(arch, &op_cases[i]);
+
+ for (i = 0; i < ARRAY_SIZE(movabs_lines); i++)
+ ret |= check_movabs(arch, movabs_lines[i][0], movabs_lines[i][1]);
+
+ free((void *)arch);
+ return ret;
+}
+
+DEFINE_SUITE("Annotate operand parsing", annotate_ops);
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 01c78ef20b97dec9..a27baf3a819ccf36 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -117,6 +117,7 @@ static struct test_suite *generic_tests[] = {
&suite__openat_syscall_event_on_all_cpus,
&suite__basic_mmap,
&suite__mem,
+ &suite__annotate_ops,
&suite__parse_events,
&suite__uncore_event_sorting,
&suite__expr,
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index af822885890dda73..963cb34acd345153 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -153,6 +153,7 @@ DECLARE_SUITE(sample_parsing);
DECLARE_SUITE(keep_tracking);
DECLARE_SUITE(parse_no_sample_id_all);
DECLARE_SUITE(dwarf_unwind);
+DECLARE_SUITE(annotate_ops);
DECLARE_SUITE(expr);
DECLARE_SUITE(hists_filter);
DECLARE_SUITE(mmap_thread_lookup);
--
2.56.0
next prev parent reply other threads:[~2026-11-21 3:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 3:20 [PATCH v10 00/16] perf coverage: A harness for perf, and the fixes it found Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 01/16] perf build: Link the GTK UI with EXTRA_CFLAGS Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 02/16] perf annotate: Handle absolute addressing in data type profiling Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 03/16] perf annotate: Print the stats after the annotations Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 04/16] perf build: Record the features checked outside FEATURE_TESTS Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 05/16] perf annotate-x86: Parse movabs like a mov Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 06/16] perf annotate-data: Count flexible array accesses for the whole struct Arnaldo Carvalho de Melo
2026-11-21 3:20 ` Arnaldo Carvalho de Melo [this message]
2026-11-21 3:20 ` [PATCH v10 08/16] perf tests: Exercise DW_OP_addrx global variables Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 09/16] perf tests: Exercise the flexible array member path Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 10/16] perf tests: Exercise data type resolution from a register Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 11/16] perf tests: Exercise the annotate stats output Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 12/16] perf tests: Fix the Intel PT virtual LBR check Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 13/16] perf test: Drop samples without an IP in the dlfilter test Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 14/16] perf test: Skip the LBR inline unwinding check when unsupported Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 15/16] perf scripts: Add a coverage harness for perf Arnaldo Carvalho de Melo
2026-11-21 3:20 ` [PATCH v10 16/16] perf scripts: Add a coverage table Arnaldo Carvalho de Melo
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=20261121032052.1392234-8-acme@kernel.org \
--to=acme@kernel.org \
--cc=acme@redhat.com \
--cc=adrian.hunter@intel.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=namhyung@kernel.org \
--cc=tglx@linutronix.de \
--cc=williams@redhat.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.