Skip to contentSkip to conversation

x86: Do not assign values to unaligned pointer to 128 bits

PATCH Superseded Jacob Xu 3 participants 4 messages May 6, 2021 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jacob Xu

author
#0 lore raw

PATCH 1/1 x86: Do not assign values to unaligned pointer to 128 bits

When compiled with clang, the following statement gets converted into a movaps instructions.
mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;

Since mem is an unaligned pointer to a union of an sse, we get a GP when running.

All we want is to make the values between mem and v different for this testcase, so let's just memset the pointer at mem, and convert to uint8_t pointer. Then the compiler will not assume the pointer is aligned to 128 bits.

Fixes e5e76263b5 ("x86: add additional test cases for sse exceptions to

emulator.c")

---
 x86/emulator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/emulator.c +3 -3
index 9705073..672bfda 100644
@@ -716,12 +716,12 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
 
 	// test unaligned access for movups, movupd and movaps
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);
 	asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movups unaligned");
 
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);
 	asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movupd unaligned");
 	exceptions = 0;
@@ -734,7 +734,7 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
 	// setup memory for cross page access
 	mem = (sse_union *)(&bytes[4096-8]);
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);
 
 	asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movups unaligned crosspage");
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#1 lore raw

Please use [kvm-unit-tests PATCH ...] for the subject, it took me a depressingly long time to figure out which code base this applied to (though admittedly there was a non-zero amount of PEBKAC going on).

Quoted 25 lines · Jacob Xu

When compiled with clang, the following statement gets converted into a movaps instructions.
mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;

Since mem is an unaligned pointer to a union of an sse, we get a GP when running.

All we want is to make the values between mem and v different for this testcase, so let's just memset the pointer at mem, and convert to uint8_t pointer. Then the compiler will not assume the pointer is aligned to 128 bits.

Fixes e5e76263b5 ("x86: add additional test cases for sse exceptions to

emulator.c")

---
 x86/emulator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/emulator.c +0 -0
index 9705073..672bfda 100644

x86/emulator.c static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) · line 719

Quoted 5 lines
x86/emulator.c +1 -1
@@ -716,12 +716,12 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
 
 	// test unaligned access for movups, movupd and movaps
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);

Shouldn't this be '16', as in 16 bytes / 128 bits? And would it makes sense to use a pattern other than '0', if only for giggles?

Quoted 21 lines
x86/emulator.c +2 -2
 	asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movups unaligned");
 
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);
 	asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movupd unaligned");
 	exceptions = 0;
@@ -734,7 +734,7 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
 	// setup memory for cross page access
 	mem = (sse_union *)(&bytes[4096-8]);
 	v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-	mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+	memset((uint8_t *)mem, 0, 128);
 
 	asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse));
 	report(sseeq(&v, mem), "movups unaligned crosspage");
Signature · 1 line
2.31.1.527.g47e6f16901-goog
#2 lore raw
Quoted 32 lines · Sean Christopherson

Please use [kvm-unit-tests PATCH ...] for the subject, it took me a depressingly long time to figure out which code base this applied to (though admittedly there was a non-zero amount of PEBKAC going on).

Quoted 26 lines · Jacob Xu

When compiled with clang, the following statement gets converted into a movaps instructions.
mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;

Since mem is an unaligned pointer to a union of an sse, we get a GP when running.

All we want is to make the values between mem and v different for this testcase, so let's just memset the pointer at mem, and convert to uint8_t pointer. Then the compiler will not assume the pointer is aligned to 128 bits.

Fixes e5e76263b5 ("x86: add additional test cases for sse exceptions to

emulator.c")

---
 x86/emulator.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/emulator.c +0 -0
index 9705073..672bfda 100644
@@ -716,12 +716,12 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem)
 
Quoted 7 lines

x86/emulator.c static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) · line 719

Quoted 4 lines
x86/emulator.c +1 -1
      // test unaligned access for movups, movupd and movaps
      v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4;
-     mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8;
+     memset((uint8_t *)mem, 0, 128);

Shouldn't this be '16', as in 16 bytes / 128 bits? And would it makes sense to use a pattern other than '0', if only for giggles?

Or possibly sizeof(*mem)?

author
#3 lore raw
Quoted 1 line

Please use [kvm-unit-tests PATCH ...] for the subject

Oops, I'll resend v2 with the correct prefix.

Quoted 2 lines

Shouldn't this be '16', as in 16 bytes / 128 bits? Or possibly sizeof(*mem)?

Replaced with sizeof below.

Quoted 1 line

use a pattern other than '0', if only for giggles?

replaced uint8_t with uint32_t for more giggles and selected 0xdecafbad from the wikipedia page for Hexspeak.

Quoted 4 lines

And would it makes sense to use a pattern other than '0', if only for giggles?

Or possibly sizeof(*mem)?