4 ms·
Communicating via the C ABI between safe languages isn't inherently unsafe, as long as you drop pointers and other unsafe things from the passed structures. It
by fulafel 11y ago
Communicating via the C ABI between safe languages isn't inherently unsafe, as long as you drop pointers and other unsafe things from the passed structures. It becomes just another protocol that you handle in your safe language.
- masklinn 11y agoCommunicating via the C ABI is as safe as coding in C in the first place. Possibly less as there's a pretty high chance to get the ownership rules wrong on the callee side in a way which'll fault (because the callee will have freed the structure) rather than "just" leak memory (because the caller will not send the structure back for deallocation). Example: https://blog.filippo.io/building-python-modules-with-go-1-5/ https://blog.filippo.io/building-python-modules-with-go-1-5/ in the third snippet (AddDot) the author returns a Go string which may (will?) be freed next time the GC runs[0] regardless of the caller still using it. [0] https://www.reddit.com/r/golang/comments/3ieiiu/building_python_modules_in_go_thanks_to_15/cufrz55 https://www.reddit.com/r/golang/comments/3ieiiu/building_pyt...
- fulafel 11y agoOwnership rules sound a lot like pointers which I excluded. Think pass-by-value structs with only fixed size fields. Or JSON, protobufs etc. (Ok, you need the one pointer for the JSON/PB data, but it's not kept around after null check and decode)
- masklinn 11y ago> Think pass-by-value structs with only fixed size fields. There are (at least) three issues with that: 1. not all FFIs can handle structs, according to the Rust FFI omnibus GHC's doesn't[0] 2. the callee side must be able to express on-stack by-value structs, many (most?) managed languages don't, either not supporting them or having them only as optimisation (based on escape analysis) 3. and finally the layout (and size) of the struct must be deterministic, which further restricts (2) > Or JSON, protobufs etc. (Ok, you need the one pointer for the JSON/PB data, but it's not kept around after null check and decode) Depending on the callee's implementation that may be too late already, there is absolutely no guarantee the data will even go through the FFI without being released, so you'd be relying on garbage data not being zeroed/reused/moved in the interval, which would be an implementation detail. [0] http://jakegoulding.com/rust-ffi-omnibus/tuples/#haskell http://jakegoulding.com/rust-ffi-omnibus/tuples/#haskell
- fulafel 11y ago> 2. the callee side must be able to express on-stack by-value structs, many (most?) managed languages don't I think the FFI could well express this. Just generate a C thunk that converts the value from the stack to a suitable representation. > 3. and finally the layout (and size) of the struct must be deterministic The layout and padding of structs is deterministic in all C environments I know of since they end up as part of ABIs. > Depending on the callee's implementation that may be too late already, there is absolutely no guarantee the data will even go through the FFI without being released So you make a C FFI call from a safe language to libfoo.so function void foo(char *jsondata) and before the callee returns your safe runtime yanks the data from under it in another thread? Yeah, you would also need the caller's FFI to guarantee against this to get safety. In absence of such guarantees you should stick to only passing pass-by-value structures!
- masklinn 11y ago> I think the FFI could well express this. Just generate a C thunk that converts the value from the stack to a suitable representation. Oh definitely, I'm just saying not all of them do (if you follow the link, the Ruby/MRI, CPython and Node FFIs all provide for that at least on the caller side) > So you make a C FFI call from a safe language to libfoo.so function void foo(char *jsondata) and before the callee returns your safe runtime yanks the data from under it in another thread? Yup, could happen with a concurrent GC kicking it at an inconvenient time, or with a refcounting GC. > Yeah, you would also need the FFI to guarantee against this to get safety. I'm not sure it could (without caller cooperation), best I can see is offer side-channels to "leak" memory (allocate non-managed memory and copy stuff into it) or recommend that you keep an internal strong reference of some sort (e.g. a global list/set).