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

suppress clang undefined behavior sanitizer in Channel constructor #780

Conversation

cary-ilm
Copy link
Member

Similar to #779...

Addresses https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=24021

If an input file contains a channel with an invalid PixelType, clang will generate an Invalid-enum-value warning. The proper behavior is to carry the invalid value, so it will be preserved on file write. This could potentially happen with "legal" files if the PixelType enum were ever extended in the future, in which case old versions of the library should read the new files.

Suppressing the warning in this one case is preferable to disabling all undefined behavior by the sanitizer, because other instances of undefined behavior may be legitimate bugs.

Signed-off-by: Cary Phillips cary@ilm.com

Signed-off-by: Cary Phillips <cary@ilm.com>
@lgritz
Copy link
Contributor

lgritz commented Jul 14, 2020

If this idiom is going to pop up a lot, is it worth defining once

#ifdef __clang__
#    define OPENEXR_NO_SANITIZE(x) __attribute__((no_sanitize (x)))
#else
#    define OPENEXR_NO_SANITIZE(x)
#endif 

and then just use the macro more compactly at the individual places where it's needed? And also have a way to add other compilers as needed, with a change in only one place?

@meshula
Copy link
Contributor

meshula commented Jul 14, 2020

I agree with Larry, although I think perhaps it's clearer to not suppress the information that this is sanitizer, for clang; especially since gcc and clang kinda sorta share attributes but not completely. I think hoping for something that generalizes across msvc as well might be futile? Does anyone know for sure?

Jamming on Larry's proposal, but de-generalizing back to Cary's, I suggest:

#ifdef __clang__
#    define OPENEXR_CLANG_NO_UB __attribute__((no_sanitize ("undefined")))
#else
#    define OPENEXR_CLANG_NO_UB
#endif

@cary-ilm
Copy link
Member Author

cary-ilm commented Jul 14, 2020 via email

@cary-ilm
Copy link
Member Author

What's the consensus here, any objections to foregoing the macro until it becomes clear there's a more common need?

@peterhillman
Copy link
Contributor

I meant to cross-reference this to #785, which modifies invalid PixelType attributes to address the same issue. Files that have an invalid PixelType in any part cannot currently be opened (the sanityCheck method called from the constructor throws an exception) so there's no reason to try to preserve unknown enum values. Also, I think it is dangerous to cast 32 bit signed ints to enums, so maybe the sanitizer did catch something important.

I think we may need to review each case separately and decide if/how to fix, so maybe we don't need the macro yet.

@cary-ilm
Copy link
Member Author

It looks like there's a broader problem here. The strategy of the initialization/file reading code is to read data, assign it to the header, and then call sanityCheck(). This is nice because it conveniently groups all the validation code in one place. But it means the header passes through a state in which is has bad values before the sanityCheck() rejects it, and the sanitizer is flagging the initial assignments. In the case of overflow, the result of the assignment may not actually be the expected values, so the sanityCheck may actually be checking values that are different from the originals. A better strategy would be to validate the values before assignment, and in fact that's what we have to do, on a case by case basis.

@cary-ilm
Copy link
Member Author

Either way, this PR is not the right solution for the case at hand, so closing it.

@cary-ilm cary-ilm closed this Jul 21, 2020
@cary-ilm cary-ilm deleted the nosanitize-channellist branch May 18, 2021 03:21
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.

4 participants