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 int16 min/max for _mm_set_epi16() calls #395

Merged
merged 2 commits into from
Nov 13, 2024
Merged

Conversation

mholt
Copy link
Contributor

@mholt mholt commented Feb 23, 2023

This fixes a bug that causes g0 overflows when compiled on Intel processors and run on AMD Ryzen processors.

According to Intel, the _mm_set_epi16() function takes signed shorts (int16): https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=mm_set_epi16&ig_expand=6355,6355

But 0-65535 is the range of uint16, not int16; causing an overflow to -1. This is warned of by the compiler: implict conversion from int to short changes value from 65535 to -1

For whatever reason, compiling and running on AMD Ryzen chip succeeded fine. Compiling and runing on an Intel chip also worked fine. But compiling on Intel and running on AMD caused an exception.

Using the int16 range fixes the problem. However, this is not my skill set and I am not sure if this is the right fix. Please verify. Thank you!

This fixes a bug that causes g0 overflows when compiled on Intel processors and run on AMD Ryzen processors.

According to Intel, the `_mm_set_epi16()` function takes signed shorts (int16): https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=mm_set_epi16&ig_expand=6355,6355

But 0-65535 is the range of uint16, not int16; causing an overflow to -1. This is warned of by the compiler: `implict conversion from int to short changes value from 65535 to -1`

For whatever reason, compiling and running on AMD Ryzen chip succeeded fine. Compiling and runing on an Intel chip also worked fine. But compiling on Intel and running on AMD caused an exception.

Using the int16 range fixes the problem. However, this is not my skill set and I am not sure if this is the right fix. Please verify. Thank you!
mholt added a commit to mholt/goheif that referenced this pull request Feb 28, 2023
Hopefully. Still need to test and verify it.

See upstream PR: strukturag/libde265#395
@mholt
Copy link
Contributor Author

mholt commented Nov 13, 2024

@farindk Just wondering what you think of this patch. Is it on the right track? It does resolve the crashes for me.

@@ -3527,9 +3527,9 @@ void ff_hevc_put_hevc_qpel_h_1_v_1_sse(int16_t *dst, ptrdiff_t dststride,
r0 = _mm_srli_epi32(r0, 6);

r1 = _mm_and_si128(r1,
_mm_set_epi16(0, 65535, 0, 65535, 0, 65535, 0, 65535));
_mm_set_epi16(-32766, 32767, -32766, 32767, -32766, 32767, -32766, 32767));
Copy link
Contributor

Choose a reason for hiding this comment

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

@mholt I'm wondering why you have chosen these int16 values. The equivalent to (0, 65535, 0, 65535, ...) would be (0, -1, 0, -1, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a best guess to preserve the range of ~65535, but if I recall correctly it also required some tinkering to prevent the exception.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is used as a simple bit-mask that is ANDed with the input pixel vector.

Do you want to change it to (0, -1) so that I can take your PR or should I just make the type fix on my side?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll change it 👍 One sec.

@farindk
Copy link
Contributor

farindk commented Nov 13, 2024

I can understand that the compiler may issue a warning, but I don't see how that can lead to a crash.

@mholt
Copy link
Contributor Author

mholt commented Nov 13, 2024

Thanks for the review!

I don't see how that can lead to a crash.

I don't either. I wish I had preserved the logs until this day, but I think by now I've lost them. I do recall the crash happened when compiling on Intel and running on AMD (both amd64 architectures). (I don't have easy access to an Intel CPU anymore to replicate though.)

That said, do you think this line should be fixed? I know for sure it resolved a warning and an exception in that edge case. But I admit I don't totally understand what it does. 😅

@mholt
Copy link
Contributor Author

mholt commented Nov 13, 2024

Okay, should be updated now. I appreciate the second opinion.

I believe the crash may have to do with implementation differences between Intel and AMD for undefined behavior that the compiler warning was calling out.

@farindk farindk merged commit 62a8ae5 into strukturag:master Nov 13, 2024
22 of 25 checks passed
@farindk
Copy link
Contributor

farindk commented Nov 13, 2024

Thanks and sorry that the PR was unnoticed for so long.

I'm still sceptical that this can lead to any crash. The worst case should be wrong decoded pixel values, but even that is unlikely.

However, if you find any file that gives a crash, please send it to me and I'll have a look.

@mholt
Copy link
Contributor Author

mholt commented Nov 13, 2024

Thanks, I'll keep an eye out and update if I have more information! Appreciate the merge.

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.

2 participants