All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix memory access issue due to variable block scope
@ 2024-03-25 21:05 Peter Senna Tschudin
  2024-04-10  5:40 ` [PATCH i-g-t v7] tests/intel/gem_exec_capture: Fix many-* subtests Peter Senna Tschudin
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Senna Tschudin @ 2024-03-25 21:05 UTC (permalink / raw
  To: igt-dev; +Cc: Peter Senna Tschudin, kamil.konieczny, andi.shyti

From: Peter Senna Tschudin <peter.senna@gmail.com>

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.

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-04-10 16:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-25 21:05 [PATCH] Fix memory access issue due to variable block scope Peter Senna Tschudin
2024-04-10  5:40 ` [PATCH i-g-t v7] tests/intel/gem_exec_capture: Fix many-* subtests Peter Senna Tschudin
2024-04-10 16:38   ` Kamil Konieczny

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.