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
Placement new should be used instead of assigning to uninitialized memory in CUB algorithms #734
thrust::copy_if and probably other functions fail to copy structs having a non-trivial assignment operator. Functions in thrust/system/cuda/detail/copy_if.h and in cub/block/block_exchange.cuh invoke the assignment operator to copy to temporary uninitialized memory, but the destination item has never been constructed and the bits are random uninitialized memory. A non-trivial assignment operator expecting a valid this object will behave incorrectly.
I will submit a couple of PRs real soon.
#include<thrust/copy.h>
#include<thrust/device_vector.h>
#include<iostream>// This struct will only properly assign if its magic field is// this certain number.constintMAGIC = 923390;
structX
{
int field;
int magic;
__host__ __device__ X()
{
magic = MAGIC;
field = 0;
}
__host__ __device__ X(int f)
{
magic = MAGIC;
field = f;
}
// Omit this assignment operator to make the copy_if succeed.
#if1
X& __host__ __device__ operator=(const X& x)
{
// To really copy over x's field value, require we have magic value set.// Since copy_if copies to uninitialized bits, the field will never be 923390.if (magic == MAGIC){
field = x.field;
}
return *this;
}
#endif
};
structalways_true {
__host__ __device__ booloperator() (const X&) {
returntrue;
};
};
intmain(void)
{
thrust::device_vector<X> a(10, X(99));
thrust::device_vector<X> b(10);
thrust::copy_if(a.begin(), a.end(), b.begin(), always_true());
int result = 0;
for (int i =0; i < 10; i++){
X ha(a[i]);
X hb(b[i]);
int ia = ha.field;
int ib = hb.field;
// The ia are all 99.// The ib should be too.if (ib != ia) {
std::cout << "bad element in output "
<< " expected: " << ia
<< " got: " << ib
<< std::endl;
result = 1;
}
}
return result;
}
changed the title [-]copy_if fails if copied objects have non-trivial constructors.[/-][+]copy_if fails if copied objects have non-trivial assignment operators.[/+]on May 18, 2020
I have confirmed at least one more similar issue, usage of the assignment operator on uninitialized bits, in CUB. I'm commenting here because there are no issues on the Thrust fork of CUB Github page, and because I found all these by calling Thrust copy_if. And because the Thrust team are committers on NVLabs/cub anyway, so I'll ask your advice how to proceed
I could probably work up a repro case and PR for this additional error I've found. However, there are potentially 10 or more remaining cases almost exactly the same in cub/block/block_load.cuh, and my own codebase doesn't seem to exercise them.
The cases, or most of them, are wrong by inspection. Given the contract of BlockLoadItems and friends, they expect the storage.load_items bits to be opaque, so uninitialized. C++ overloaded assignment operators presume the left-hand side of the assignment to be valid instances. We must replace all of these assignments by placement new.
The conservative thing to do is for me to submit a PR just for the single case I can prove is faulty.
I think the CUB team would probably want to make a task to fix all of these cases.
Running this regex over block_load.cuh will find several candidates:
items\s*\[\s*\w+\s*\]\s*=
I believe you could probably do similarly in block_exchange.cuh.
Here's what I propose to do:
I'll submit a PR to thrust/cub for the one new CUB fix I can prove, referencing this issue.
I'll submit a new issue to NVLabs/cub repeating the above.
A Thrust or CUB developer should do the review of that code to identify assignments needing placement new. You don't want me doing it.
Feel free to adjust my plan to work best with your work flow.
@hwinkler Thanks for looking into this. Your proposal sounds good to me -- please file the general CUB issue (at nvlabs/cub) and any PRs (at thrust/cub) that you need for your projects.
We plan to unify the two CUB repos Real Soon Now so this won't be ambiguous in the future.
changed the title [-]copy_if fails if copied objects have non-trivial assignment operators.[/-][+]Placement new should be used instead of assigning to uninitialized memory in CUB algorithms[/+]on May 27, 2020
Activity
Use placement new to construct item on uninitialized memory.
[-]copy_if fails if copied objects have non-trivial constructors.[/-][+]copy_if fails if copied objects have non-trivial assignment operators.[/+]alliepiper commentedon May 18, 2020
Thanks for the report and PRs! I'll take a look.
Add regression test for #1153
Use placement new to construct item on uninitialized memory.
Use placement new to construct item on uninitialized memory.
hwinkler commentedon May 26, 2020
I have confirmed at least one more similar issue, usage of the assignment operator on uninitialized bits, in CUB. I'm commenting here because there are no issues on the Thrust fork of CUB Github page, and because I found all these by calling Thrust
copy_if. And because the Thrust team are committers onNVLabs/cubanyway, so I'll ask your advice how to proceedI could probably work up a repro case and PR for this additional error I've found. However, there are potentially 10 or more remaining cases almost exactly the same in
cub/block/block_load.cuh, and my own codebase doesn't seem to exercise them.The cases, or most of them, are wrong by inspection. Given the contract of
BlockLoadItemsand friends, they expect thestorage.load_itemsbits to be opaque, so uninitialized. C++ overloaded assignment operators presume the left-hand side of the assignment to be valid instances. We must replace all of these assignments by placementnew.The conservative thing to do is for me to submit a PR just for the single case I can prove is faulty.
I think the CUB team would probably want to make a task to fix all of these cases.
Running this regex over
block_load.cuhwill find several candidates:items\s*\[\s*\w+\s*\]\s*=I believe you could probably do similarly in
block_exchange.cuh.Here's what I propose to do:
thrust/cubfor the one new CUB fix I can prove, referencing this issue.NVLabs/cubrepeating the above.A Thrust or CUB developer should do the review of that code to identify assignments needing placement
new. You don't want me doing it.Feel free to adjust my plan to work best with your work flow.
alliepiper commentedon May 26, 2020
@hwinkler Thanks for looking into this. Your proposal sounds good to me -- please file the general CUB issue (at nvlabs/cub) and any PRs (at thrust/cub) that you need for your projects.
We plan to unify the two CUB repos Real Soon Now so this won't be ambiguous in the future.
Use placement new to construct item on uninitialized memory.
Use placement new to construct item on uninitialized memory.
Use placement new to construct item on uninitialized memory.
[-]copy_if fails if copied objects have non-trivial assignment operators.[/-][+]Placement new should be used instead of assigning to uninitialized memory in CUB algorithms[/+]alliepiper commentedon May 27, 2020
I've updated the title to reflect that there are more instances of this issue that need to be fixed.
hwinkler commentedon Jun 9, 2020
This bug looks like a duplicate of NVIDIA/thrust#1106
jrhemstad commentedon Aug 15, 2025
@bernhardmgruber could you take a look to see if this is still an issue?
miscco commentedon Aug 15, 2025
@jrhemstad this is not a bug in thrust but a relatively clear user bug.
We have
thrust::uninitialized_copyandthrust::uninitialized_fillfor exactly these circumstances.Using "ordinary" algorithms with uninitialized memory is always UB.
jrhemstad commentedon Aug 15, 2025
As far as I can tell, the repro passes fine: https://godbolt.org/z/nWfdGxoY7
That said, there very well could still be issues elsewhere. I'm going to close this issue in favor of #860 as it is more general.