Skip to content

Fix UBSan errors #1948

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

Merged
merged 8 commits into from
Sep 12, 2021
Merged

Fix UBSan errors #1948

merged 8 commits into from
Sep 12, 2021

Conversation

kleisauke
Copy link
Member

@kleisauke kleisauke commented Dec 29, 2020

As discussed in #1727 (comment). Commit 8113231 fixes this error1:

Running: common_fuzzer_corpus/smartcrop_fuzzer-5687924892368896
mosaic_fuzzer.cc:47:56: runtime error: member access within misaligned address 0x60b0000004c3 for type 'struct mosaic_opt', which requires 2 byte alignment

https://travis-ci.org/github/kleisauke/libvips/jobs/709752013#L2599

Commit 1dd6698 fixes this error:

Running: common_fuzzer_corpus/sharpen_fuzzer-5678720198639616
scRGB2sRGB.c:114:11: runtime error: 8224 is outside the range of representable values of type 'unsigned char'

https://travis-ci.org/github/kleisauke/libvips/jobs/709752013#L2315

Commit 1a6854a fixes this error2:

Running: common_fuzzer_corpus/jpegsave_buffer_fuzzer-5658586599915520
flatten.c:190:4: runtime error: 1.64485e+07 is outside the range of representable values of type 'unsigned short'

https://travis-ci.org/github/kleisauke/libvips/jobs/709752013#L2270

And the last commit (b8f18e1) fixes this error:

Running: common_fuzzer_corpus/jpegsave_buffer_fuzzer-5658586599915520
heifload.c:282:27: runtime error: load of misaligned address 0x60f000000229 for type 'guint32' (aka 'unsigned int'), which requires 4 byte alignment

https://github.com/libvips/libvips/runs/3554490177#step:13:366

Marked this PR as draft due to these endnotes:

  • 1: but also changes the way how vips_mosaic is fuzzed (i.e. by separating the fuzzer parameters from the image), so perhaps we need to regenerate the existing corpus?
  • 2: this was copied-pasted from vips_cast, I'm not sure if there's a better way to fix this?

@kleisauke kleisauke changed the base branch from 8.10 to master September 9, 2021 09:17
@kleisauke
Copy link
Member Author

I think the above endnotes have been resolved.

  • The existing corpus (in the fuzz/common_fuzzer_corpus directory) doesn't needs to be regenerated, because it does not contain any corpora for the mosaic fuzzer.
  • I don't think there's a better way to resolve this.

This is ready for review now.

@kleisauke kleisauke marked this pull request as ready for review September 9, 2021 09:57
@jcupitt
Copy link
Member

jcupitt commented Sep 10, 2021

This looks great Kleis.

The flatten change is a shame -- the casts will be quite a bit slower.

The overflows only happen if there is a mismatch between the expected range of the alpha (0-255, or 0-65535) and the available range of the numeric type, which will be a rare case, I think. For example, if the interpretation is srgb (0-255 alpha), but it's a ushort image (pixels can have much higher values).

How about detecting mismatch, and if that happens, flattening to float and then calling vips_clip() to go back to the original type? It should fix the error, and we won't lose speed, except in these rare cases.

I could no longer reproduce this with clang 12 locally.
@kleisauke
Copy link
Member Author

Oh huh, I could no longer reproduce the flatten change locally with clang 12. But CI doesn't seem to pass when reverting this.

@jcupitt
Copy link
Member

jcupitt commented Sep 12, 2021

OK, shall we merge? I could make another PR with a possible flatten fix after this, if you like.

@kleisauke
Copy link
Member Author

Feel free to merge, another PR to fix that error in flatten sounds good to me. FWIW, I'm testing locally with clang 12.0.1. CI seems to be testing with version 12.0.0.

@jcupitt jcupitt merged commit 5ab66e1 into libvips:master Sep 12, 2021
@jcupitt
Copy link
Member

jcupitt commented Sep 12, 2021

OK, let's do it. Well done for crunching through the fuzzer results.

@jcupitt
Copy link
Member

jcupitt commented Sep 12, 2021

I made an issue with a reproducer for the flatten error. I'll have a go at a PR.

@kleisauke kleisauke deleted the fix-ubsan-errors branch September 12, 2021 14:03
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