Skip to content

UBSAN "downcast of address" runtime error on createSentinal for ilists #6446

Open

Description

ubsan-compiled dxc will fail with errors like:

/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.

We should back-port those fixes into DXC.

Activity

  1. added
    bug
    needs-triage
    on Mar 22, 2024
  2. damyanp commented on Mar 22, 2024

    @damyanp
    Member

    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.

  3. amaiorano commented on Mar 22, 2024

    @amaiorano
    CollaboratorAuthor

    Let's keep this open, as my change was only a workaround for ubsan reporting.

  4. dexonsmith commented on Mar 22, 2024

    @dexonsmith
    Contributor

    ubsan-compiled dxc will fail with errors like:

    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 :

    1. the code miscompiled before, now asserted / miscompiled depending on LLVM_ASSERT
    2. the code was "fine" before (UB, but no problem in practice), now asserted / miscompiled depending on LLVM_ASSERT
    3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions