This is an archive of the discontinued LLVM Phabricator instance.

Add an assertion in SmallVector::push_back()
ClosedPublic

Authored by mehdi_amini on Jul 21 2020, 9:43 PM.

Details

Summary

This assertion ensures the input value isn't part of the vector when
growing is required. In such cases the vector will grow and the input
value is invalidated before being read from.

This found 14 failed Tests.

Diff Detail

Event Timeline

mehdi_amini created this revision.Jul 21 2020, 9:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 21 2020, 9:43 PM
bkramer accepted this revision.Jul 22 2020, 1:57 AM

Thanks!

This revision is now accepted and ready to land.Jul 22 2020, 1:57 AM
mehdi_amini marked an inline comment as done.Jul 22 2020, 10:08 AM
mehdi_amini added inline comments.
llvm/include/llvm/MC/MCInst.h
187

Actually this is working around the assertion, but not fixing it.
The reference passed as input is invalidated by the reserve I think.

silvas added a subscriber: silvas.Nov 13 2020, 2:22 PM
silvas added inline comments.
llvm/include/llvm/MC/MCInst.h
187

Could we switch this to take MCOperand by value? A copy is going to be made in the push_back anyway.

FYI, there's also now https://reviews.llvm.org/D87326. I'd be in favour of landing this patch (with Sean's fix) while we wait for that one.

This assert should probably also be used (albeit some in a modified way) for:

  • resize(size_type, const T&)
  • append(size_type, const T&)
  • insert(iterator, const T&)
  • insert(iterator, T &&)
  • insert(iterator, size_type, const T&)

As all these functions can grow and invalidate the reference.

mehdi_amini marked an inline comment as not done.

Update with Sean's suggestion

aartbik added inline comments.
llvm/include/llvm/ADT/SmallVector.h
143

nit: s/enough/sufficient

mehdi_amini added inline comments.Nov 13 2020, 3:30 PM
llvm/include/llvm/MC/MCInst.h
187

Ah good point, MCOperand is cheap anyway.

Fix one clang instance failing this assertion

Herald added a project: Restricted Project. · View Herald TranscriptNov 13 2020, 4:01 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 added inline comments.Nov 13 2020, 4:35 PM
clang/lib/CodeGen/CGBuiltin.cpp
10811–10813 ↗(On Diff #305279)

Probably a little off topic, but shouldn't this be refactored as a rotate. Has the advantage that it will never allocate.

This revision was landed with ongoing or failed builds.Nov 13 2020, 4:56 PM
This revision was automatically updated to reflect the committed changes.
mehdi_amini added inline comments.Nov 13 2020, 4:57 PM
clang/lib/CodeGen/CGBuiltin.cpp
10811–10813 ↗(On Diff #305279)

Well spotted! I didn't even think about what the code was doing and fixed it mechanically...

I pushed this separately: https://github.com/llvm/llvm-project/commit/42e88bd6b185