Skip to content
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

Use explicit memcpy() to avoid unaligned memory accesses #3225

Merged
merged 2 commits into from Jul 1, 2019
Merged

Use explicit memcpy() to avoid unaligned memory accesses #3225

merged 2 commits into from Jul 1, 2019

Conversation

DerDakon
Copy link
Contributor

@DerDakon DerDakon commented Jul 2, 2018

Fixes #3213.

This replaces casts of incoming memory with explicit memcpy(). Some platforms, especially Sparc, but also ARM and Itanium, do not like unaligned memory accesses and may lack from slowdowns or crashes as result of those if the incoming memory is not aligned to the expected access type. Using memcpy() will avoid this: the compiler will optimize this to direct unaligned accesses on platforms that support those operations, and will do whatever copy operation is needed on platforms that do not.

@homm
Copy link
Member

homm commented Jul 3, 2018

Personally, I don't like it because, while compiler will likely optimize memcpy calls to just one move operation, it doesn't have to optimize it.

Do you have examples of such fixes in other widespread libraries?

@DerDakon
Copy link
Contributor Author

DerDakon commented Jul 3, 2018

Is python common enough? For fixed size copies you must try very hard for the compiler not to optimize this on e.g. x86, this is one of the most common intrinsics that are in use.

@DerDakon
Copy link
Contributor Author

What is the way forward? I bet this needs updating, but I'm only going to do this if it would be accepted afterwards.

@hugovk
Copy link
Member

hugovk commented Dec 29, 2018

@homm Any comments?

@DerDakon
Copy link
Contributor Author

I would rebase it if there is a chance that this get's merged in time then, otherwise it is too much work for nothing.

@hugovk
Copy link
Member

hugovk commented Jun 29, 2019

There's been no objections from @homm or anyone else in a year, so let's merge this for Monday's release, if possible. Thank you!

This replaces trivial instances where a copy from one pointer to the other
involves no further calculations or casts. The compiler will optimize this to
whatever the platform offers.
@@ -480,86 +479,78 @@ void
ImagingUnpackRGB(UINT8* _out, const UINT8* in, int pixels)
{
int i = 0;
#ifdef __sparc
/* SPARC CPUs cannot read integers from nonaligned addresses. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the removals of these exceptions for SPARC intentional?

They were only added recently, in #3858.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, by using memcpy the compiler will care for the correct access pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks.

cc @kulikjak fyi.

@DerDakon
Copy link
Contributor Author

DerDakon commented Jun 30, 2019

I have tested this on my sparc machine and I see some test errors and I suspect them being endianess issues. I guess this is still better than crashes. I'll probably need a few days to look into this.

@DerDakon
Copy link
Contributor Author

Ok, I was even faster than expected. When I compare the test results of my patch on sparc with current git master on hppa I have only these additional failures:

Tests/test_imagefont.py::TestImageFont::test_variation_get FAILED
Tests/test_imagefont.py::TestImageFont::test_variation_set_by_axes FAILED
Tests/test_imagefont.py::TestImageFont::test_variation_set_by_name FAILED

Please decide if they are likely caused by this patches or not, I have no clue.

@DerDakon
Copy link
Contributor Author

I have tested it on my hppa machine (also big endian) with and without this patch, and the font errors are also present there in both runs. So I don't see any regressions with that patch.

@hugovk
Copy link
Member

hugovk commented Jul 1, 2019

Thanks for testing. As no regressions found, let's merge it.

@hugovk hugovk merged commit 555e305 into python-pillow:master Jul 1, 2019
@DerDakon DerDakon deleted the unaligned-access branch July 1, 2019 07:25
@hugovk hugovk mentioned this pull request Jul 1, 2019
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unaligned access crash
4 participants