Skip to content

Placement new should be used instead of assigning to uninitialized memory in CUB algorithms #734

Closed as not planned

Description

@hwinkler

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.
const int MAGIC = 923390;

struct X
{
    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.
    
    #if 1
    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
};

struct always_true {
    __host__ __device__ bool operator() (const X&) {
        return true;
    };
};


int main(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;  
}

Activity

  1. 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
  2. alliepiper commented on May 18, 2020

    @alliepiper
    Contributor

    Thanks for the report and PRs! I'll take a look.

  3. added a commit that references this issue on May 19, 2020
  4. hwinkler commented on May 26, 2020

    @hwinkler
    Author

    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:

    1. I'll submit a PR to thrust/cub for the one new CUB fix I can prove, referencing this issue.
    2. 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.

  5. alliepiper commented on May 26, 2020

    @alliepiper
    Contributor

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

  6. 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
  7. alliepiper commented on May 27, 2020

    @alliepiper
    Contributor

    I've updated the title to reflect that there are more instances of this issue that need to be fixed.

  8. hwinkler commented on Jun 9, 2020

    @hwinkler
    Author

    This bug looks like a duplicate of NVIDIA/thrust#1106

  9. added
    thrust
    on Feb 22, 2023
  10. transferred this issue fromNVIDIA/thruston Nov 8, 2023
  11. jrhemstad commented on Aug 15, 2025

    @jrhemstad
    Collaborator

    @bernhardmgruber could you take a look to see if this is still an issue?

  12. miscco commented on Aug 15, 2025

    @miscco
    Contributor

    @jrhemstad this is not a bug in thrust but a relatively clear user bug.

    We have thrust::uninitialized_copy and thrust::uninitialized_fill for exactly these circumstances.

    Using "ordinary" algorithms with uninitialized memory is always UB.

  13. moved this from Todo to Done in CCCLon Aug 15, 2025
  14. reopened this on Aug 15, 2025
  15. moved this from Done to Needs Triage in CCCLon Aug 15, 2025
  16. jrhemstad commented on Aug 15, 2025

    @jrhemstad
    Collaborator

    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.

  17. moved this from Needs Triage to Done in CCCLon Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

thrust

Type

No type

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions