Repository navigation
Description
Is this a duplicate?
- I confirmed there appear to be no duplicate issues for this bug and that I agree to the Code of Conduct
Type of Bug
Something else
Component
Thrust
Describe the bug
I'm porting thrust tests to our internal build at work.
One of the things we have enabled in our build by default is a subset of UB sanitizer, and I've noticed that thrust tests appear to have a lot of nupp pointer dereference failures. Some of the UB-enabled builds result in a crash. Enabling UB sometimes preserves the offending code, which would otherwise be removed by compiler, because it's allowed to treat UB in whatever way it wants.
Probing few of the failures deeper suggests that that the issues are real. E.g. test_async_copy_after test in async_copy.cu apparently attempts to dereference a null pointer.
How to Reproduce
Add the following lines to thrust/CMakeLists.txt:
# Force enable minimal UB sanitizer.
add_compile_options(-fsanitize-trap=all -fsanitize=null -fno-sanitize-trap=undefined)
add_link_options(-fsanitize=undefined)
Run thrust tests with:
PATH=$HOME/local/cuda-12.3.1/bin:$PATH bash -x ci/test_thrust.sh -cxx clang++-17 -std 20 -arch 80-real -cuda clang++-17
Adding -O0 will likely make even more tests fail as with high optimizations a lot of code gets eliminated before it gets a chance to be instrumented.
Observe the test failures:
- with non-fatal ubsan:
79% tests passed, 77 tests failed out of 362. Test output: https://gist.github.com/Artem-B/9d59658f4c64940c2da4d59fd14096f4 - with fatal ubsan:
23% tests passed, 280 tests failed out of 362
Note that non-failing tests also report UB violations, but they are hidden by the test framework.
If you want to force all of them to turn into test failures (alas, it's just a crash with no useful diagnostics attached), use the following flags, and skip add_link_options():
add_compile_options(-fsanitize-trap=all -fsanitize=null)
Expected behavior
Thrust should not be relying on UB in general, and in particular when it comes to null pointers. Compiler can and does optimize code on the assumption that UB never happens. Sooner or later that will become a problem. It's possible that it already is, we just didn't notice it, yet.
Most of the ubsan reports are associated with the same few locations, so the root cause is probably fairly localized.
Reproduction link
No response
Operating System
Debian/testing
nvidia-smi output
not applicable.
NVCC version
- release build of clang++-17
- CUDA-12.3.1, though CUDA version is not particularly relevant here, as it's about host-side compilation w/ clang.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
jrhemstad commentedon Apr 17, 2024
Hey @Artem-B, thanks for the heads up.
Good timing! We're just starting the process of setting up nightly CI jobs to do things like build/run our tests with sanitizers enabled so we can find and resolve this kind of stuff.
I just added a task to #1619 to include setting up jobs that build with these sanitizer options enabled. Once we get that infrastructure setup, we can go chip away at fixing the issues that come up.
bernhardmgruber commentedon Nov 7, 2024
I just stumbled over this as well when trying UBSan to hunt a bug. We should get that fixed.
Similarly, I see issues reported by MSan as well.
Artem-B commentedon Nov 13, 2024
This also appears to affect cub tests if they are built without optimizations.
That
ud1instruction is a tell-tale sign that we've got to the point we should not have.Artem-B commentedon Nov 13, 2024
Ugh. What can possibly go wrong here...
cccl/thrust/thrust/detail/reference.h
Line 182 in 18043cb
... because, of course we jump straight to this:
cccl/thrust/thrust/detail/reference.h
Line 330 in 18043cb
Artem-B commentedon Nov 13, 2024
@brycelelbach Looks like it was introduced by 4fd1b54 a while back.
Any suggestions on how we can guarantee that
systemis not dereferenced, or implement the value type check some other way that avoids undefined behavior?jrhemstad commentedon Nov 14, 2024
@miscco could you scope what it would take to fix this? It feels like there must be a better way to do that dispatch than passing around nullptrs.
Artem-B commentedon Nov 14, 2024
As a temporary workaround, the
convert_to_value_typefunction can be annotated with__attribute__((no_sanitize("null"))):cccl/thrust/thrust/detail/reference.h
Line 327 in 18043cb
That unbreaks about half of the tests () for me (
-fsanitize=nullis enabled by default in my builds). Looks like there are other places where this would need to be applied.Try to avoid UB in `thrust::reference`
Try to avoid UB in `thrust::reference`
bernhardmgruber commentedon Sep 23, 2025
@Artem-B we just merged #4226 which fixes the null reference access. Please let us know whether that fixes this issue. Thx!
Artem-B commentedon Sep 23, 2025
Nice! It will be a while till we import/use CCCL v3, but I think this bug can be closed now.
bernhardmgruber commentedon Sep 23, 2025
Awesome! Please reopen if the issue persists!