-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unaligned access UB in BLIT_GLYPH routines #324
Comments
If unaligned access are illegal on sparc arch, |
No, that's wrong. Please don't do this. The behavior is undefined because it commits a strict-aliasing violation. This is wrong on all platforms, not just sparc. |
I havent tried in detail but i dont see the strict aliasing violation here. Line 880 in 303c280
It usually happens when you keep and use at the same time two pointers of different types but same adress. Which doesnt seem the case here. Then your gcc error is actually misalignment: "Load of misaligned address 0x0001272a9817 for type 'const Uint64'" |
It's because you're indexing into an array of Line 893 in 303c280
u64 . You can't access this as a u64 at a 5-byte offset.
|
Yes so this is indeed an un aligned load. Not a strict aliasing violation. And this is allowed on most platform. And this is done on purpose to blit faster. glyph 8 by 8. Apparently this isnt allowed on sparc, so we should desactivate it for this platform. |
No, that's not right. Conversion to a misaligned pointer is itself invoking undefined behavior, technically even before it is accessed. See this answer and this answer.
That UBSAN output that I provided is from a non-sparc platform. It's telling you that it is undefined behavior, which is not allowed by the C standard, even if it is allowed by the underlying hardware platform. |
@matoro is correct here, we need to fix this. We shouldn't rely on unaligned load behavior, we should split it into unaligned and aligned accesses. |
What about the assembly unaligned load? Anyway. Those routines need un-aligned load to run. |
Yeah, assembly ones are fine. Maybe we can just have the 32-bit versions and assembly? I haven't looked at the code, but 32-bit accesses also need to be aligned. |
You can instead just replace it with a
|
I'd expect the memcpy to be efficient with modern compilers and generate identical code where it's legal. Some projects make that the default with an opt-in to raw unaligned access as the project currently does (or the opposite but I'd not prefer that). |
Here some PR (from smartphone...) not tested... |
I gave a try and could not trigger the error on my x86_64 machine. I used the flags: any idea ? If I simulate a fake invalid read: but I didn't see the unaligned access, any idea ? |
Did you download the relevant sample font? See the first |
yes! |
@1bsyl Here are the exact commands to reproduce the problem:
Pasting this exact block into a terminal reproduces the problem from scratch for me. Do you also see it with this sequence of commands? |
@matoro |
Though, I still cannot run this on SDL3 |
use memcpy so we can expect the compiler to optimize when possible
here's a PR: |
@matoro please give a try the pr on sparc. btw, I also have ubsan reports like that:
|
PR works, see my comment there.
Yes, those are indeed also undefined behavior! |
use memcpy so we can expect the compiler to optimize when possible
use memcpy so we can expect the compiler to optimize when possible (cherry picked from commit d2e6ded)
here's some fix for the left shift |
Hi, I identified this bug while testing pygame: https://bugs.gentoo.org/920956
In the blit glyph routines
BG_64
andBG_32
, the loops invoke undefined behavior by casting some offset into a larger integer tou8
. GCC UBSAN at least catches and warns about this. These problems are easy to catch on sparc, because unaligned access is completely illegal there and crashes the program with a SIGBUS, but this is not a platform-specific problem: unaligned access is undefined behavior on all platforms.Minimized reproducer:
And this is the complete backtrace at the point of the crash on sparc:
Tested against
release-2.20.2
. Let me know if any further information is helpful. I also offer free shell access to the hardware I used to reproduce this issue on, if it's useful. Thank you!The text was updated successfully, but these errors were encountered: