Hey folks,

I'm working on a project that involves an ioctl API on OpenBSD. The idea is 
that you store a memory address in a union within another structure, and 
then invoke the ioctl. When it returns, both the structure itself and the 
memory pointed at by the address in the union are filled with data by the 
kernel.

I originally wrote a pure Go version of this code that seems to work, but 
after a conversation in #darkarts on Gophers Slack (starting at 
https://gophers.slack.com/archives/C1C1YSQBT/p1557956939402700), several of 
us were unsure if this version was actually safe. See the body of this 
function:

https://github.com/WireGuard/wgctrl-go/blob/7e04c64d5b80f1991b52c6025d6f59e6aa3a3939/internal/wgopenbsd/client_openbsd.go#L59

The concern was as follows:

> i think the usage of `ifgrs` is unsafe...
> maybe it's alright because it'll always be heap allocated due to the 
dynamic size of it, but afaict, the compiler could deduce that it doesn't 
escape and then the call to ioctl could move it during a stack realloc..

After thinking it over and having a further discussion, I decided to try 
passing a pointer to memory allocated with C.malloc instead, and this also 
seems to work.

https://github.com/WireGuard/wgctrl-go/pull/49/commits/05d446d3d7b2e376424e57a5167205a325d61781

My question is: is this necessary, and if so, have I implemented it 
correctly? I've typically just passed pointers to structures with no other 
pointers directly with Linux APIs; never a pointer to a structure that also 
contains other pointers to other memory. I'm a little shaky on Cgo since 
I've mostly gotten by without it during my time writing Go.

Thanks for your time.
- Matt

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/8d41db4b-1da6-49fb-b1e7-c44e3cae9d8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to