This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix crash in __builtin_strncmp and other related builtin functions
ClosedPublic

Authored by shafik on Aug 22 2023, 2:30 PM.

Details

Summary

The implementation of __builtin_strncmp and other related builtins function use getExtValue() to evaluate the size argument. This can cause a crash when the value does not fit into an int64_t value, which is can be expected since the type of the argument is size_t.

The fix is to switch to using getZExtValue().

This fixes:

https://github.com/llvm/llvm-project/issues/64876

Diff Detail

Event Timeline

shafik created this revision.Aug 22 2023, 2:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2023, 2:30 PM
shafik requested review of this revision.Aug 22 2023, 2:30 PM

Are there any Sema tests we can add to show that we warn/diagnose/SOMETHING on these? If someone passes a negative size, we should probably at least do the warning that it was converted/truncated.

clang/lib/AST/ExprConstant.cpp
9357

Context missing throughout

shafik updated this revision to Diff 552516.Aug 22 2023, 3:21 PM
  • Diff w/ context
shafik updated this revision to Diff 552518.Aug 22 2023, 3:25 PM

-Updated values used in test

Are there any Sema tests we can add to show that we warn/diagnose/SOMETHING on these? If someone passes a negative size, we should probably at least do the warning that it was converted/truncated.

The arguments have type size_t so using -Wsign-conversion would warn for the previous value but that is not really the point of the fix, which is why I updated the value to clarify.

shafik marked an inline comment as done.Aug 22 2023, 3:28 PM

Precommit CI found issues that should be addressed, but otherwise the changes LGTM. You should add a release note for the fix, though!

I think there is value to ensuring we diagnose the negative-to-size_t conversion here, but I also think there is value to a CodeGen test to ensure we emit the correct value.

shafik updated this revision to Diff 553195.Aug 24 2023, 10:51 AM
  • Silence -Wconstant-conversion warning in test

Still think a codegen test for this example is VERY valuable here.

clang/test/SemaCXX/constexpr-string.cpp
681

rather than suppress these, it makes sense to me to just mark them expected-warning.

shafik added inline comments.Aug 24 2023, 11:17 AM
clang/test/SemaCXX/constexpr-string.cpp
681

It depends on the size of size_t whether I get this diagnostic or not.

aaron.ballman added inline comments.Aug 24 2023, 12:22 PM
clang/test/SemaCXX/constexpr-string.cpp
681

Given that the point to this test isn't to test constant conversion behavior but is testing boundary cases where you'll get those diagnostics, I think suppressing the warnings is fine.

shafik updated this revision to Diff 553270.Aug 24 2023, 2:48 PM
shafik marked 2 inline comments as done.
  • Add codegen test
aaron.ballman accepted this revision.Aug 25 2023, 5:12 AM

LGTM but the changes need to be landed with a release note.

This revision is now accepted and ready to land.Aug 25 2023, 5:12 AM

I suggest doing the -511LL value from the original bug report in the tests as well, else, fine to me.

erichkeane requested changes to this revision.Aug 25 2023, 7:23 AM

Ah, wait, still no release note!

This revision now requires changes to proceed.Aug 25 2023, 7:23 AM
erichkeane accepted this revision.Aug 25 2023, 7:39 AM

I see aaron mentioned the release note and is ok with it happening on commit, so I'll undo my 'needs revision'.

This revision is now accepted and ready to land.Aug 25 2023, 7:39 AM
shafik updated this revision to Diff 553601.Aug 25 2023, 1:39 PM
  • Extended constexpr-string.cpp test
  • Added release notes
erichkeane accepted this revision.Aug 25 2023, 1:41 PM

Still LGTM.

This revision was landed with ongoing or failed builds.Aug 25 2023, 1:55 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2023, 1:55 PM