linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,  Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	Miguel Ojeda <ojeda@kernel.org>,
	 Liam Howlett <liam.howlett@oracle.com>,
	linux-perf-users@vger.kernel.org,  linux-kernel@vger.kernel.org
Subject: [PATCH v1] perf intel-pt: pkt-decoder: Fix alignment issues
Date: Mon,  2 Oct 2023 15:19:09 -0700	[thread overview]
Message-ID: <20231002221909.2958708-1-irogers@google.com> (raw)

The byte aligned buffer is cast to large types and dereferenced
causing misaligned pointer warnings from undefined behavior sanitizer.
Fix the alignment issues with memcpy which may require the
introduction of temporaries.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 .../intel-pt-decoder/intel-pt-pkt-decoder.c   | 21 ++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c b/tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
index af9710622a1f..28659874d84e 100644
--- a/tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
+++ b/tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
@@ -83,7 +83,7 @@ static int intel_pt_get_long_tnt(const unsigned char *buf, size_t len,
 	if (len < 8)
 		return INTEL_PT_NEED_MORE_BYTES;
 
-	payload = le64_to_cpu(*(uint64_t *)buf);
+	memcpy_le64(&payload, buf, sizeof(payload));
 
 	for (count = 47; count; count--) {
 		if (payload & BIT63)
@@ -220,6 +220,8 @@ static int intel_pt_get_3byte(const unsigned char *buf, size_t len,
 static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
 				struct intel_pt_pkt *packet)
 {
+	uint32_t tmp;
+
 	packet->count = (buf[1] >> 5) & 0x3;
 	packet->type = buf[1] & BIT(7) ? INTEL_PT_PTWRITE_IP :
 					 INTEL_PT_PTWRITE;
@@ -228,12 +230,13 @@ static int intel_pt_get_ptwrite(const unsigned char *buf, size_t len,
 	case 0:
 		if (len < 6)
 			return INTEL_PT_NEED_MORE_BYTES;
-		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 2));
+		memcpy(&tmp, buf + 2, sizeof(tmp));
+		packet->payload = le32_to_cpu(tmp);
 		return 6;
 	case 1:
 		if (len < 10)
 			return INTEL_PT_NEED_MORE_BYTES;
-		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
+		memcpy_le64(&packet->payload, buf + 2, sizeof(packet->payload));
 		return 10;
 	default:
 		return INTEL_PT_BAD_PACKET;
@@ -258,7 +261,7 @@ static int intel_pt_get_mwait(const unsigned char *buf, size_t len,
 	if (len < 10)
 		return INTEL_PT_NEED_MORE_BYTES;
 	packet->type = INTEL_PT_MWAIT;
-	packet->payload = le64_to_cpu(*(uint64_t *)(buf + 2));
+	memcpy_le64(&packet->payload, buf + 2, sizeof(packet->payload));
 	return 10;
 }
 
@@ -454,6 +457,8 @@ static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
 			   struct intel_pt_pkt *packet)
 {
 	int ip_len;
+	uint16_t tmp16;
+	uint32_t tmp32;
 
 	packet->count = byte >> 5;
 
@@ -465,13 +470,15 @@ static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
 		if (len < 3)
 			return INTEL_PT_NEED_MORE_BYTES;
 		ip_len = 2;
-		packet->payload = le16_to_cpu(*(uint16_t *)(buf + 1));
+		memcpy(&tmp16, buf + 1, sizeof(tmp16));
+		packet->payload = le16_to_cpu(tmp16);
 		break;
 	case 2:
 		if (len < 5)
 			return INTEL_PT_NEED_MORE_BYTES;
 		ip_len = 4;
-		packet->payload = le32_to_cpu(*(uint32_t *)(buf + 1));
+		memcpy(&tmp32, buf + 1, sizeof(tmp32));
+		packet->payload = le32_to_cpu(tmp32);
 		break;
 	case 3:
 	case 4:
@@ -484,7 +491,7 @@ static int intel_pt_get_ip(enum intel_pt_pkt_type type, unsigned int byte,
 		if (len < 9)
 			return INTEL_PT_NEED_MORE_BYTES;
 		ip_len = 8;
-		packet->payload = le64_to_cpu(*(uint64_t *)(buf + 1));
+		memcpy_le64(&packet->payload, buf + 1, sizeof(packet->payload));
 		break;
 	default:
 		return INTEL_PT_BAD_PACKET;
-- 
2.42.0.582.g8ccd20d70d-goog


             reply	other threads:[~2023-10-02 22:19 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-02 22:19 Ian Rogers [this message]
2023-10-05 15:48 ` [PATCH v1] perf intel-pt: pkt-decoder: Fix alignment issues Ian Rogers
2023-10-05 19:04   ` Adrian Hunter
2023-10-05 21:24     ` Ian Rogers
2023-10-09  5:29       ` Namhyung Kim
2023-10-09 15:31         ` Ian Rogers
2023-10-11  5:56           ` Namhyung Kim
2023-10-11  6:50             ` Ian Rogers
2023-10-12 12:27               ` Adrian Hunter
2023-10-13  4:23                 ` 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=20231002221909.2958708-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@kernel.org \
    --cc=liam.howlett@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.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 NNTP newsgroup(s).