This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Bail on memrchr calls with an excessive size.
AbandonedPublic

Authored by msebor on Apr 12 2022, 1:33 PM.

Details

Summary

This change implements folding of memrchr calls with constant bounds in excess of the size of the source array to null. This is done in preparation for a subsequent change that assumes the bound is valid.

Depends on D123626.

Diff Detail

Event Timeline

msebor created this revision.Apr 12 2022, 1:33 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2022, 1:33 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
msebor requested review of this revision.Apr 12 2022, 1:33 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2022, 1:33 PM
msebor edited the summary of this revision. (Show Details)Apr 12 2022, 1:49 PM
msebor updated this revision to Diff 422356.Apr 12 2022, 4:07 PM

Add context.

nikic added inline comments.Apr 13 2022, 2:56 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
903

While this is correct, I do wonder whether we should be going out of our way to fold this to null, rather than simply bailing out of the transform. In particular, I wonder whether this will end up suppressing an asan/msan warning. It's okay to do that, as these are "best effort", but it's probably better to avoid if it doesn't cost us any additional effort?

msebor added inline comments.Apr 13 2022, 9:09 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
903

I don't yet know what the LLVM policy is for dealing with provably undefined code. I chose this because it seems like the lesser of the two evils (e.g., when passing in a negative/very large size). But I'm aware that some other handlers punt invalid calls to the library and I'm fine with that if it helps sanitizers detect the problem.

xbolva00 added inline comments.Apr 13 2022, 9:11 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
903

Or insert UnreachableInst?

xbolva00 added inline comments.Apr 13 2022, 9:26 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
903

But generally not worth to catch all UBs and fold them, so I agree with @nikic

msebor updated this revision to Diff 422631.Apr 13 2022, 1:22 PM
msebor retitled this revision from [InstCombine] Fold memrchr calls with an excessive size to null. to [InstCombine] Bail on memrchr calls with an excessive size..

Avoid folding memrchr calls with an excessive size to null.

I'm open to treating such calls with unreachable if there is broader support for it. In my experience, some users/projects are in favor of it, others prefer the current approach of making the library call, and others still would like to see it replaced with a trap. Providing a compiler option would let each group choose what works best for them.

xbolva00 added inline comments.Apr 13 2022, 1:44 PM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
906

But currently this patch does not make sense a bit as we would reach this nullptr (with your patch we reach it sooner).

So in the current form, I dont think we need this patch. This “bail out” you can add in the patch which uses this patch as a base.

nikic added inline comments.Apr 14 2022, 9:23 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
906

Yeah, this makes more sense as part of the next patch in the stack now.

msebor added inline comments.Apr 14 2022, 1:49 PM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
906

The follow-on patch depends on this hunk so now that they've both been reviewed and agreed on, whether they're applied separately or together doesn't seem important. But they're easy enough to merge before committing.

nikic accepted this revision.Apr 14 2022, 1:52 PM
nikic added inline comments.
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
906

Right. The code change itself is fine, so I'm marking this as approved.

This revision is now accepted and ready to land.Apr 14 2022, 1:52 PM
msebor abandoned this revision.May 18 2022, 1:47 PM

This patch was committed as part of the next one in the series so I'm abandoning it to keep it from popping up as ready to land.