Interesting GCC bug

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Interesting GCC bug

Post by 8infy »

Hi everyone, today i wanna share with you an interesting GCC bug i found.
I was working on FPU initialization in my kernel, and finally got to the point where i needed to use FXSAVE in order to retrieve the FPU MXCSR mask.
Since it was a throw away FPU state i decided to put it on the stack like this:

Code: Select all

alignas(16) u8 fxsave_region[512] {};
asm volatile ("fxsave %0" : "=m"(fxsave_region));
However, that code would generate GPF at all times, even though the fxsave_region address i logged to the console seemed to be aligned to 32 bytes (0xC0888DA0).
After trying different things (using attribute aligned, moving the variable around, adding * before fxsave_region in the asm statement) i ended up looking at the dissasembly,
which was relatively simple:

Code: Select all

fxsave [ebp-0x218]
Since that code would GPF i had the exact EBP value at the time of the GPF, which was 0xC0888FC4.
As you can see 0xC0888FC4 - 0x218 = 0xC0888DAC, which is 0xC bytes past the address that fxsave_region is actually located at.

For some reason GCC uses a wrong offset to load my stack array.
Not sure what this bug is about, but the workaround i found was to use alignas(32) instead of alignas(16).
I must note that i use GCC 10.1.0.
Since i don't want this to break later on, I'm just going to use kmalloc instead.

If you know anything about this bug please let me know.
Octocontrabass
Member
Member
Posts: 5567
Joined: Mon Mar 25, 2013 7:01 pm

Re: Interesting GCC bug

Post by Octocontrabass »

It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Re: Interesting GCC bug

Post by 8infy »

Octocontrabass wrote:It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.
I verified that my stack begin and end pointers are both page aligned.
8infy
Member
Member
Posts: 185
Joined: Sun Apr 05, 2020 1:01 pm

Re: Interesting GCC bug

Post by 8infy »

Octocontrabass wrote:It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.
Never mind, you're right!
value (%esp+ 4) is always a multiple of 16 (32 or 64) when control is transferred
to the function entry point.
This is what i was missing in my pre-kernel. Thanks a lot. Crazy how it didn't reveal itself earlier!
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Interesting GCC bug

Post by nullplan »

8infy wrote:This is what i was missing in my pre-kernel. Thanks a lot. Crazy how it didn't reveal itself earlier!
In x86, there is very little that depends hard on alignment, and even less that depends on stack alignment. In this case you were using fxsave on a stack variable, which is a bit weird. Normally, the fxsave area is allocated in kernel heap (along with the rest of the task descriptor), and then the alignment is for the heap allocator do determine.
Carpe diem!
Post Reply