Skip to contentSkip to conversation

Fix memory access issue due to variable block scope

PATCH Superseded Peter Senna Tschudin 2 participants 3 messages Mar 25, 2024 – Apr 10, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory access issue due to variable block scope

From: Peter Senna Tschudin <[email protected]>

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.

---
 lib/i915/gem_engine_topology.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
lib/i915/gem_engine_topology.c +13 -1
index afb576afb..b3b809482 100644
@@ -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)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v7 1/1 tests/intel/gem_exec_capture: Fix many-* subtests

Currently trying to run gem_exec_capture --run-subtest many-4K-incremental or gem_exec_capture --run-subtest many-4K-zero will fail with:

(gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Test
assertion failure function gem_engine_properties_configure, file ../lib/i915/gem_engine_topology.c:577:
(gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Failed assertion: ret == 1 (gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Last errno: 9, Bad file descriptor (gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: error: -1 != 1

This problem happens inside the macro find_first_available_engine() when:
1. for_each_ctx_engine() allocates an struct intel_engine_data 'ed' inside a for loop. The core of the issue is that ed only exists inside the for loop. As soon as the for loop ends, ed is out of scope and after it's lifetime.
2. intel_get_current_engine() sets '*e' to an address of ed. This is ok while inside the for loop, and is undefined behavior after the for loop ends.
3. configure_hangs() uses '*e' after the lifetime of 'ed' has ended leading to undefined behavior
4. After the call to find_first_available_engine() __captureN() will fail as it expects '*e' to be valid. This is also undefined behavior.

This patch fixes the issue by:
1. Creating a 'tmpe' variable for code clarity by hinting that this is a temporal variable.
1. Moving the call to configure_hangs() to inside the for loop, where there are no issues with scope and lifetime of 'ed'.
2. Assigning 'e' to a value of 'saved' with 'e = &saved.engine;'. The reason why this works is twofold: First 'saved' has the same content as 'e' had when there were no variable scope and lifetime issues. Second both 'e' and 'saved' are defined in many() meaning that they share the same scope and lifetime.

Additionally, this patch:
- moves the 'igt_assert(tmpe);' to between the assignment and first use of 'tmpe'.
- sets 'e = NULL;' and checks if this changes to decide to skip the test.

v7: unifies the two patches from v6; creates 'tmpe' for code clarity;
sets 'e = NULL;' to decide to skip the the test if still NULL by the end of 'find_first_available_engine()'; places 'igt_assert(tmpe);' between assignment and first use of 'tmpe';

---
 tests/intel/gem_exec_capture.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tests/intel/gem_exec_capture.c +10 -4
index 57b178f3e..b79268443 100644
@@ -662,13 +662,19 @@ static bool needs_recoverable_ctx(int fd)
 
 #define find_first_available_engine(fd, ctx, e, saved) \
 	do { \
+		struct intel_execution_engine2 *tmpe = NULL; \
+		e = NULL; \
 		ctx = intel_ctx_create_all_physical(fd); \
 		igt_assert(ctx); \
-		for_each_ctx_engine(fd, ctx, e) \
-			for_each_if(gem_class_can_store_dword(fd, e->class)) \
+		for_each_ctx_engine(fd, ctx, tmpe) { \
+			igt_assert(tmpe); \
+			if(gem_class_can_store_dword(fd, tmpe->class)) { \
+				saved = configure_hangs(fd, tmpe, ctx->id); \
+				e = &saved.engine; \
 				break; \
-		igt_assert(e); \
-		saved = configure_hangs(fd, e, ctx->id); \
+			} \
+		} \
+		igt_skip_on_f(e == NULL, "no capable engine found\n"); \
 	} while(0)
 
 static void many(int fd, int dir, uint64_t size, unsigned int flags)
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH i-g-t v7] tests/intel/gem_exec_capture: Fix many-* subtests

Hi Peter,

Quoted 11 lines · Peter Senna Tschudin

Currently trying to run gem_exec_capture --run-subtest many-4K-incremental or gem_exec_capture --run-subtest many-4K-zero will fail with:

(gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Test
assertion failure function gem_engine_properties_configure, file ../lib/i915/gem_engine_topology.c:577:
(gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Failed assertion: ret == 1 (gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: Last errno: 9, Bad file descriptor (gem_exec_capture:81999) i915/gem_engine_topology-CRITICAL: error: -1 != 1

Quoted 14 lines

This problem happens inside the macro find_first_available_engine() when:
1. for_each_ctx_engine() allocates an struct intel_engine_data 'ed' inside a for loop. The core of the issue is that ed only exists inside the for loop. As soon as the for loop ends, ed is out of scope and after it's lifetime.
2. intel_get_current_engine() sets '*e' to an address of ed. This is ok while inside the for loop, and is undefined behavior after the for loop ends.
3. configure_hangs() uses '*e' after the lifetime of 'ed' has ended leading to undefined behavior
4. After the call to find_first_available_engine() __captureN() will fail as it expects '*e' to be valid. This is also undefined behavior.

That is good explanation.

Quoted 11 lines

This patch fixes the issue by:
1. Creating a 'tmpe' variable for code clarity by hinting that this is a temporal variable.
1. Moving the call to configure_hangs() to inside the for loop, where there are no issues with scope and lifetime of 'ed'.
2. Assigning 'e' to a value of 'saved' with 'e = &saved.engine;'. The reason why this works is twofold: First 'saved' has the same content as 'e' had when there were no variable scope and lifetime issues. Second both 'e' and 'saved' are defined in many() meaning that they share the same scope and lifetime.

Skip this second explanation points starting from 'Creating tmpe' as it can be readed from patch, just write that using 'e' outside for_each_ctx_engine() is incorrect and this patch fixes it.

Quoted 6 lines

Additionally, this patch:
- moves the 'igt_assert(tmpe);' to between the assignment and first use of 'tmpe'.
- sets 'e = NULL;' and checks if this changes to decide to skip the test.

And here just write that you changes assert into skip_on and why.

Quoted 16 lines

v7: unifies the two patches from v6; creates 'tmpe' for code clarity;
sets 'e = NULL;' to decide to skip the the test if still NULL by the end of 'find_first_available_engine()'; places 'igt_assert(tmpe);' between assignment and first use of 'tmpe';

---
 tests/intel/gem_exec_capture.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
tests/intel/gem_exec_capture.c +0 -0
index 57b178f3e..b79268443 100644

tests/intel/gem_exec_capture.c static bool needs_recoverable_ctx(int fd) · line 666

Quoted 5 lines
tests/intel/gem_exec_capture.c +2 -0
@@ -662,13 +662,19 @@ static bool needs_recoverable_ctx(int fd)
 
 #define find_first_available_engine(fd, ctx, e, saved) \
 	do { \
+		struct intel_execution_engine2 *tmpe = NULL; \
+		e = NULL; \

Add here newline to improve readability:

        \

With that added feel free to add my r-b.
I do not see your patch on patchwork, maybe resend as a new one?

Regards,
Kamil

Quoted 21 lines
tests/intel/gem_exec_capture.c +8 -4
 		ctx = intel_ctx_create_all_physical(fd); \
 		igt_assert(ctx); \
-		for_each_ctx_engine(fd, ctx, e) \
-			for_each_if(gem_class_can_store_dword(fd, e->class)) \
+		for_each_ctx_engine(fd, ctx, tmpe) { \
+			igt_assert(tmpe); \
+			if(gem_class_can_store_dword(fd, tmpe->class)) { \
+				saved = configure_hangs(fd, tmpe, ctx->id); \
+				e = &saved.engine; \
 				break; \
-		igt_assert(e); \
-		saved = configure_hangs(fd, e, ctx->id); \
+			} \
+		} \
+		igt_skip_on_f(e == NULL, "no capable engine found\n"); \
 	} while(0)
 
 static void many(int fd, int dir, uint64_t size, unsigned int flags)
Signature · 1 line
2.44.0