coreylowman / cudarc

Safe rust wrapper around CUDA toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unsound lifetimes of kernel params

coreylowman opened this issue · comments

There is potential undefined behavior if any of the params for cuLaunchKernel are drop'd before the kernel actually executes.

A simple case is just a local parameter variable, which gets turned into a kernel parameter which a reference. That means the pointer is to local stack frame maybe?

What would need to happen to fix this is ensuring that these values are dropped after the kernel executes. One potential way to do this is use cuLaunchHostFunc, where the host function does nothing but has ownership of all the params.

Pin also seems very useful for this case.

This may not actually be an issue. I ran some simple tests where a stack host variable is dropped before the kernel executes, and this doesn't seem to be an issue.

Since values are passed by value, and they are copied into const memory, this should all happen before the launch_async returns.

See more: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#functions

Notable excerpts:

When lowering a global function launch from host code, the compiler generates stub functions that copy the parameters one or more times by value, before eventually using memcpy to copy the arguments to the global function’s parameter memory on the device. This occurs even if an argument was non-trivially-copyable, and therefore may break programs where the copy constructor has side effects.

Kernel launches are asynchronous with host execution. As a result, if a global function argument has a non-trivial destructor, the destructor may execute in host code even before the global function has finished execution. This may break programs where the destructor has side effects.