Page 1 of 1

Crashes without -O3

Posted: Sun Oct 11, 2020 7:55 am
by nexos
Hello,
In my kernel, at one point, I would get occasional #PFs and hangs. I compiled with -O3, and it works now. Why would -O3 fix this problem?
Thanks,
nexos

Re: Crashes without -O3

Posted: Sun Oct 11, 2020 8:55 am
by nakst
Are you on x86_64, and if so, have you setup the compiler to not use the red zone? https://wiki.osdev.org/Libgcc_without_red_zone

Re: Crashes without -O3

Posted: Sun Oct 11, 2020 11:01 am
by sj95126
It's possible you have a buggy section of code somewhere that -O3 is optimizing into nothing, but that is triggering #PF when it's there.

This isn't a good example, because it's a useless function, but as an illustration:

Code: Select all

void func(int *x) {
        int y;

        y = *x;
}
That would cause #PF if x is an invalid pointer, but if you use -O3, the entire function becomes a NOP. The optimizer decides since you didn't use y, there's no reason to set it.

What'd you have to do is 1) track whether your #PFs are happening in the same place(s), and 2) analyze that section of code with and without -O3 to see if you can tell what's going wrong. (if the #PFs are in random places, obviously that makes it harder to find)

Re: Crashes without -O3

Posted: Sun Oct 11, 2020 11:26 am
by nexos
What is strange is that it only does it occasionaly. I don't think libgcc was compiled with -mno-red-zone. The code probably got interrupted and it corrupted the stack.

Re: Crashes without -O3

Posted: Mon Oct 12, 2020 1:09 pm
by bzt
nexos wrote:What is strange is that it only does it occasionaly. I don't think libgcc was compiled with -mno-red-zone. The code probably got interrupted and it corrupted the stack.
I'm certain you have an UB somewhere. Things like "it works with -O0 but not with -O3 (or vice versa)" almost all the time caused by an UB somewhere. The optimizer generating code that occasionaly doesn't work also a good indicator for an UB.

Cheers,
bzt

Re: Crashes without -O3

Posted: Mon Oct 12, 2020 3:10 pm
by nexos
I meant it does it occasionaly with -O0, amd never with -O3. I probably had to do with the red zone. I realized today I wasn't passing -mno-red-zone to the compiler :shock: .