You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UBSAN "downcast of address" runtime error on createSentinal for ilists #6446
/home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/SparseBitVector.h:256:44: runtime error: downcast of address 0x7ffc07f29348 with insufficient space for an object of type 'llvm::ilist_traits<llvm::SparseBitVectorElement<128>>::Element' (aka 'SparseBitVectorElement<128U>')
0x7ffc07f29348: note: pointer points here
00 00 00 00 00 00 00 00 00 00 00 00 18 ab 02 00 10 61 00 00 38 ab 02 00 10 61 00 00 68 ab 02 00
^
#0 0x5572402d0e1d in createSentinel /home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/SparseBitVector.h:256:44
#1 0x5572402d0e1d in provideInitialHead /home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/SparseBitVector.h:260:48
#2 0x5572402d0e1d in iplist /home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/ilist.h:350:25
#3 0x5572402d0e1d in ilist /home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/ilist.h:603:3
#4 0x5572402d0e1d in SparseBitVector /home/amaiorano/src/external/DirectXShaderCompiler/include/llvm/ADT/SparseBitVector.h:437:3
#5 0x5572402d0e1d in (anonymous namespace)::SparseBitVectorTest_TrivialOperation_Test::TestBody() /home/amaiorano/src/external/DirectXShaderCompiler/unittests/ADT/SparseBitVectorTest.cpp:18:21
<snip>
The reason this fails is that most implementations of ilist_traits<T>::createSentinal return a down-casted instance of a ilist_half_node<T> or ilist_node<T>, which although can be cast-down to T (since T derives from ilist_node), means we're returning a pointer to a sliced object of type T. And although we never dereference the pointer, this is still UB, which is why UBSAN flags it.
This issue was already flagged and addressed in LLVM by Duncan P. N. Exon Smith (@dexonsmith), as can be seen by most of his changes here, where Duncan ended up rewriting the ilist system to be clearer, potentially more efficient, and non-UB. See Duncan's original llvm-dev post where he describes the issues in even more detail.
Triage notes: thanks for the report. I think this falls into the bucket of things that we'd happily consider a PR for but would not likely find time to work out ourselves.
This issue was already flagged and addressed in LLVM by Duncan P. N. Exon Smith (@dexonsmith), as can be seen by most of his changes here, where Duncan ended up rewriting the ilist system to be clearer, potentially more efficient, and non-UB. See Duncan's original llvm-dev post where he describes the issues in even more detail.
We should back-port those fixes into DXC.
FYI, just so you plan any work accordingly, this was a lot of work to land and there was more work after as well:
A very long tail of compile errors to fix because of losing the implicit cast from ilist_iterator<N> to N*.
Many cases of rewriting doSomethingWithPointerToN(I) as doSomethingWithPointerToN(&*I) (all landed ahead of the main change).
This was a critical part of the feature, because ilist_node::operator*() has the new bug-catching assertion. You really want this assertion.
A long tail of fixes caught by asserts builds after this change (mainly in LLVM (both IR and in the backends proper), but also in downstream clients like Clang and Swift).
I saw the new assertion firing in these three categories :
the code miscompiled before, now asserted / miscompiled depending on LLVM_ASSERT
the code was "fine" before (UB, but no problem in practice), now asserted / miscompiled depending on LLVM_ASSERT
the code was "fine" before (UB, but no problem in practice), now asserted / "fine" depending on LLVM_ASSERT
(I remember no examples of: miscompiled before, now asserted / "fine" depending on LLVM_ASSERT.)
Category 2 was the risky one. Mostly this was due to stack slots being assigned more efficiently: ilist was a smaller object because the sentinel didn't include unused space for the ilist_node. Previously, a client that dereferenced the sentinel read from the not-constructed ilist_node data and would often enough find a 0, which would happen to be "fine". With the change, a client that dereferences the sentinel reads from the next object on the stack after the ilist, which is usually not 0.
The more holes in your asserts-enabled testing coverage, the more risky.
Reacted by Antonio Maiorano, Chris B and David Neto
Activity
damyanp commentedon Mar 22, 2024
Triage notes: thanks for the report. I think this falls into the bucket of things that we'd happily consider a PR for but would not likely find time to work out ourselves.
Disable "downcast of address" UBSAN runtime error
Disable "downcast of address" UBSAN runtime error
amaiorano commentedon Mar 22, 2024
Let's keep this open, as my change was only a workaround for ubsan reporting.
Disable "downcast of address" UBSAN runtime error (microsoft#6448)
dexonsmith commentedon Mar 22, 2024
FYI, just so you plan any work accordingly, this was a lot of work to land and there was more work after as well:
ilist_iterator<N>toN*.doSomethingWithPointerToN(I)asdoSomethingWithPointerToN(&*I)(all landed ahead of the main change).ilist_node::operator*()has the new bug-catching assertion. You really want this assertion.I saw the new assertion firing in these three categories :
LLVM_ASSERTLLVM_ASSERTLLVM_ASSERT(I remember no examples of: miscompiled before, now asserted / "fine" depending on
LLVM_ASSERT.)Category 2 was the risky one. Mostly this was due to stack slots being assigned more efficiently:
ilistwas a smaller object because the sentinel didn't include unused space for theilist_node. Previously, a client that dereferenced the sentinel read from the not-constructedilist_nodedata and would often enough find a 0, which would happen to be "fine". With the change, a client that dereferences the sentinel reads from the next object on the stack after theilist, which is usually not 0.The more holes in your asserts-enabled testing coverage, the more risky.
Use a real instruction for the sentinel in the Instruction intrusive …
Use a real instruction for the sentinel in the Instruction intrusive … (
Use a real instruction for the sentinel in the Instruction intrusive … (