This is an archive of the discontinued LLVM Phabricator instance.

[IntervalTree] Initialize find_iterator::Point
ClosedPublic

Authored by caojoshua on May 2 2023, 3:02 PM.

Details

Summary

There was initially a msan report for use-of-uninitialized value due to
a bug in https://reviews.llvm.org/D138526. find_iterator::Point is
uninitialized for the default constructor of find_iterator, which is
used by IntervalTree::end. This change is not required, but its good
practice to make sure all class members are initialized.

Diff Detail

Event Timeline

caojoshua created this revision.May 2 2023, 3:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 3:02 PM
caojoshua removed a reviewer: clopez.

If this change looks ok, I would like to reland https://reviews.llvm.org/D138526

We can't rely on the compiler to short-circuit the conditional check in find_iterator::operator==().

return (!LHS.Node && !RHS.Node && !LHS.Index && !RHS.Index) ||
       (LHS.Point == RHS.Point && LHS.Node == RHS.Node &&
        LHS.Index == RHS.Index);

Before the unswitch patch, the compiler was short-circuiting, so we never use the uninitialized RHS.Point. After the unswitching patch, the RHS of the || gets hoisted to an earlier part of the function.

The compiler is legally allowed to switch the left hand and right hand side of the ||. I constructed an example in https://godbolt.org/z/MvdzdKovz.

caojoshua published this revision for review.May 2 2023, 3:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2023, 3:14 PM
vitalybuka requested changes to this revision.May 2 2023, 4:07 PM

We can't rely on the compiler to short-circuit the conditional check in find_iterator::operator==().

return (!LHS.Node && !RHS.Node && !LHS.Index && !RHS.Index) ||
       (LHS.Point == RHS.Point && LHS.Node == RHS.Node &&
        LHS.Index == RHS.Index);

Before the unswitch patch, the compiler was short-circuiting, so we never use the uninitialized RHS.Point. After the unswitching patch, the RHS of the || gets hoisted to an earlier part of the function.

The compiler is legally allowed to switch the left hand and right hand side of the ||. I constructed an example in https://godbolt.org/z/MvdzdKovz.

Comment in https://godbolt.org/z/MvdzdKovz is incorrect, x is zero initialized as it's global. But I am not sure what this proves.

There is c/c++ standard and the is compiler. Standard explain how such expressions are evaluated. I believe this is short circuited. Compiler can do shortcuts if results the same.

However Msan goal is to detect undefined behaviors in user code when the user violates standard.
D138526 brakes msan, making it falsely complaining when violation is not really happening, like here.

foo in your example is not a problem as select is pass-through for msan. it complains about branches.
When you replace select with branch, we have a report.

llvm/include/llvm/ADT/IntervalTree.h
466

it should be here

This revision now requires changes to proceed.May 2 2023, 4:07 PM
caojoshua updated this revision to Diff 520147.May 6 2023, 10:25 PM
caojoshua retitled this revision from [IntervalTree] Initialize find_iterator::Point with default constructor to [IntervalTree] Initialize find_iterator::Point.
caojoshua edited the summary of this revision. (Show Details)
  • zero initialize Point
  • update commit message to show this is NFC. there is no true use-of-uninitialized bug in IntervalTree, just a bug in the unswitching patch

I don't want to push this until https://reviews.llvm.org/D138526 is merged and is not causing any test issues. I fixed that patch to pass the affected test locally, but want to confirm on buildbot builds.

llvm/include/llvm/ADT/IntervalTree.h
466

LGTM

vitalybuka accepted this revision.May 8 2023, 10:23 AM

I don't see problem with the patch and prefer to initialized as much as possible.
However there is an opinion that keeping stuff uninitialized may help msan to catch holes in logic if code missed real initialization.
Seems @CarlosAlbertoEnciso is OK

This revision is now accepted and ready to land.May 8 2023, 10:23 AM
This revision was automatically updated to reflect the committed changes.