Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Senna Tschudin <me@petersenna.com>
To: igt-dev@lists.freedesktop.org
Cc: kamil.konieczny@linux.intel.com, andi.shyti@linux.intel.com,
	Peter Senna Tschudin <peter.senna@gmail.com>
Subject: [PATCH i-g-t v2] Fix memory access issue due to variable block scope
Date: Mon, 25 Mar 2024 22:35:48 +0100	[thread overview]
Message-ID: <20240325213548.2881-1-me@petersenna.com> (raw)

This patch fixes the tests gem_exec_capture@many-4k-incremental and
gem_exec_capture@many-4k-zero that are currently failing with an invalid file
descriptor error.

 struct intel_execution_engine2 *
 intel_get_current_engine(struct intel_engine_data *ed)

When intel_get_current_engine is called from the macro
for_each_ctx_cfg_engine(), the variable *ed is defined within a for loop. The
scope of *ed is limited to that loop, leading to access violations when
attempting to access its contents outside the loop.

Before to this patch, intel_get_current_engine() would return an element of *ed
and attempting to use it after the loop ended resulted in undefined behavior.

This patch introduces a memcpy() to copy the contents of ed->current_engine to
a memory area not confined by the loop's scope, ensuring safe access to the
data.

v2: Added 'i-g-t' to the Subject.

Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
---
 lib/i915/gem_engine_topology.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lib/i915/gem_engine_topology.c b/lib/i915/gem_engine_topology.c
index afb576afb..b3b809482 100644
--- a/lib/i915/gem_engine_topology.c
+++ b/lib/i915/gem_engine_topology.c
@@ -189,12 +189,24 @@ static int __query_engine_list(int fd, struct intel_engine_data *ed)
 struct intel_execution_engine2 *
 intel_get_current_engine(struct intel_engine_data *ed)
 {
+	struct intel_execution_engine2 *ret = NULL;
+
 	if (ed->n >= ed->nengines)
 		ed->current_engine = NULL;
 	else if (!ed->n)
 		ed->current_engine = &ed->engines[0];
 
-	return ed->current_engine;
+	// When called from the macro for_each_ctx_cfg_engine(), *ed is defined
+	// inside a for loop. In that case, not memcping ed->current_engine
+	// will lead to a memory access violation when trying to access the
+	// contents of ed->current_engine after the end of the for loop
+	if (ed->current_engine) {
+		ret = malloc(sizeof(*ret));
+		if (ret)
+			memcpy(ret, ed->current_engine, sizeof(*ret));
+	}
+
+	return ret;
 }
 
 void intel_next_engine(struct intel_engine_data *ed)
-- 
2.34.1


             reply	other threads:[~2024-03-25 21:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-25 21:35 Peter Senna Tschudin [this message]
2024-03-25 23:22 ` ✓ Fi.CI.BAT: success for Fix memory access issue due to variable block scope (rev2) Patchwork
2024-03-25 23:28 ` ✓ CI.xeBAT: " Patchwork
2024-03-26  4:52 ` ✗ Fi.CI.IGT: failure " Patchwork
2024-03-26  7:18   ` Peter Senna Tschudin
2024-03-26 12:05 ` [PATCH i-g-t v2] Fix memory access issue due to variable block scope Andi Shyti
2024-03-26 13:18   ` Peter Senna Tschudin
2024-03-26 18:20     ` Kamil Konieczny
2024-03-26 21:41       ` Peter Senna Tschudin
2024-03-27 16:59         ` Kamil Konieczny
2024-03-27 17:34           ` Peter Senna Tschudin
2024-03-26 13:29   ` Peter Senna Tschudin

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=20240325213548.2881-1-me@petersenna.com \
    --to=me@petersenna.com \
    --cc=andi.shyti@linux.intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=kamil.konieczny@linux.intel.com \
    --cc=peter.senna@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox