Linux-Toolchains Archive mirror
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-perf-users@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Stephane Eranian <eranian@google.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	linux-toolchains@vger.kernel.org,
	linux-trace-devel@vger.kernel.org
Subject: [PATCH 14/14] perf annotate-data: Add stack canary type
Date: Fri, 16 Feb 2024 15:54:23 -0800	[thread overview]
Message-ID: <20240216235423.2343167-15-namhyung@kernel.org> (raw)
In-Reply-To: <20240216235423.2343167-1-namhyung@kernel.org>

When the stack protector is enabled, compiler would generate code to
check stack overflow with a special value called 'stack carary' at
runtime.  On x86_64, GCC hard-codes the stack canary as %gs:40.

While there's a definition of fixed_percpu_data in asm/processor.h,
it seems that the header is not included everywhere and many places
it cannot find the type info.  As it's in the well-known location (at
%gs:40), let's add a pseudo stack canary type to handle it specially.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/annotate-data.h |  1 +
 tools/perf/util/annotate.c      | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/tools/perf/util/annotate-data.h b/tools/perf/util/annotate-data.h
index 0bfef29fa52c..e293980eb11b 100644
--- a/tools/perf/util/annotate-data.h
+++ b/tools/perf/util/annotate-data.h
@@ -77,6 +77,7 @@ struct annotated_data_type {
 
 extern struct annotated_data_type unknown_type;
 extern struct annotated_data_type stackop_type;
+extern struct annotated_data_type canary_type;
 
 /**
  * struct data_loc_info - Data location information
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 5f3136f57c62..f2683dadf3cf 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -116,6 +116,13 @@ struct annotated_data_type stackop_type = {
 	},
 };
 
+struct annotated_data_type canary_type = {
+	.self = {
+		.type_name = (char *)"(stack canary)",
+		.children = LIST_HEAD_INIT(canary_type.self.children),
+	},
+};
+
 static int arch__grow_instructions(struct arch *arch)
 {
 	struct ins *new_instructions;
@@ -3764,6 +3771,17 @@ static bool is_stack_operation(struct arch *arch, struct disasm_line *dl)
 	return false;
 }
 
+static bool is_stack_canary(struct arch *arch, struct annotated_op_loc *loc)
+{
+	/* On x86_64, %gs:40 is used for stack canary */
+	if (arch__is(arch, "x86")) {
+		if (loc->segment == INSN_SEG_X86_GS && loc->offset == 40)
+			return true;
+	}
+
+	return false;
+}
+
 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset,
 			struct disasm_line *dl)
 {
@@ -3938,6 +3956,12 @@ struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he)
 		}
 
 		mem_type = find_data_type(&dloc);
+
+		if (mem_type == NULL && is_stack_canary(arch, op_loc)) {
+			mem_type = &canary_type;
+			dloc.type_offset = 0;
+		}
+
 		if (mem_type)
 			istat->good++;
 		else
-- 
2.44.0.rc0.258.g7320e95886-goog


  parent reply	other threads:[~2024-02-16 23:54 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-16 23:54 [PATCHSET 00/14] perf tools: Remaining bits of data type profiling (v6) Namhyung Kim
2024-02-16 23:54 ` [PATCH 01/14] perf dwarf-aux: Add die_collect_vars() Namhyung Kim
2024-02-16 23:54 ` [PATCH 02/14] perf dwarf-aux: Handle type transfer for memory access Namhyung Kim
2024-02-16 23:54 ` [PATCH 03/14] perf annotate-data: Introduce struct data_loc_info Namhyung Kim
2024-02-16 23:54 ` [PATCH 04/14] perf map: Add map__objdump_2rip() Namhyung Kim
2024-02-16 23:54 ` [PATCH 05/14] perf annotate: Add annotate_get_basic_blocks() Namhyung Kim
2024-02-16 23:54 ` [PATCH 06/14] perf annotate-data: Maintain variable type info Namhyung Kim
2024-02-16 23:54 ` [PATCH 07/14] perf annotate-data: Add update_insn_state() Namhyung Kim
2024-02-16 23:54 ` [PATCH 08/14] perf annotate-data: Handle global variable access Namhyung Kim
2024-02-16 23:54 ` [PATCH 09/14] perf annotate-data: Handle call instructions Namhyung Kim
2024-02-16 23:54 ` [PATCH 10/14] perf annotate-data: Implement instruction tracking Namhyung Kim
2024-02-16 23:54 ` [PATCH 11/14] perf annotate: Parse x86 segment register location Namhyung Kim
2024-02-16 23:54 ` [PATCH 12/14] perf annotate-data: Handle this-cpu variables in kernel Namhyung Kim
2024-02-16 23:54 ` [PATCH 13/14] perf annotate-data: Track instructions with a this-cpu variable Namhyung Kim
2024-02-16 23:54 ` Namhyung Kim [this message]
  -- strict thread matches above, loose matches on Subject: below --
2024-02-02 22:04 [PATCHSET 00/14] perf tools: Remaining bits of data type profiling (v5) Namhyung Kim
2024-02-02 22:04 ` [PATCH 14/14] perf annotate-data: Add stack canary type Namhyung Kim
2024-02-03  3:21   ` Ian Rogers
2024-02-06 23:18     ` Namhyung Kim
2024-02-06 23:40       ` Ian Rogers
2024-02-07 19:08         ` Namhyung Kim

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=20240216235423.2343167-15-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-toolchains@vger.kernel.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).