habanero-rice / hclib

A C/C++ task-based programming model for shared memory and distributed parallel computing.
http://habanero-rice.github.io/hclib/
BSD 3-Clause "New" or "Revised" License
71 stars 35 forks source link

memcpy non-trivially-copyable function objects #12

Closed DaoWen closed 8 years ago

DaoWen commented 8 years ago

Using memcpy to clone C++ objects that are not trivially copyable can cause serious problems.

We're currently using memcpy to clone lambdas for async (see inc/hclib-async.h:124).

Here's a simple program demonstrating the problem:

#include <memory>
#include <iostream>
#include <unistd.h>
#include <cassert>

#include "hclib_cpp.h"

struct SimpleObject {
    int value;
    SimpleObject(): value(1) { }
    ~SimpleObject() { value = 0; }
};

int main (int argc, char ** argv) {
    hclib::launch([]() {
        hclib::finish([]() {{
            auto p = std::make_shared<SimpleObject>();
            std::cout << "Value starts as " << p->value << std::endl;
            hclib::async([=](){
                usleep(100);
                std::cout << "Value in async is " << p->value << std::endl;
                assert(p->value == 1);
            });
            // p is dead
        }});
    });
    std::cout << "Exiting..." << std::endl;
    return 0;
}

Expected output:

Value starts as 1
Value in async is 1
Exiting...

Current output:

Value starts as 1
Value in async is 189792480
Assertion failed: (p->value == 1), function operator(), file capture0.cpp, line 56.
Abort trap: 6
DaoWen commented 8 years ago

This issue is fixed for the "core" async code in f8240f0b5070051939e04142bb0df8cbb0f5b25b, but there are probably still issues with the "extension" async support (CUDA and UPC++).

I also added the above program as a new C++ test in dc58a3ab9e1f291e153e8d4c11c5d8c170336718.

This issue should be automatically closed when those commits are merged into master. We can open a separate issue for the CUDA / UPC++ asyncs we haven't resolved the issue before merging the above changes.