News for hackers. Stuff that matters.
Re:Rust and C++ Interoperability in Chrome by Paul-ish on Wednesday August 19, 2020 @03:01PM (#24214662)
Back when I was an intern at Mozilla it was a real pain (in my opinion) to call between C++ and Rust. This was before bindgen and cbindgen, and certainly CXX.

Eg if you have a heap allocated thing that is passed between Rust and C++, who frees it? I ended up hacking something together, but it didn't feel right.

Re:Rust and C++ Interoperability in Chrome by xxpor on Wednesday August 19, 2020 @03:04PM (#24214708)
>if you have a heap allocated thing that is passed between Rust and C++, who frees it?

Isn't this an issue regardless? Forget crossing the rust/c++ boundary, lets say you pass something to another class in c++, you need to have a contract of who frees what when, right? Maybe I'm not understanding the issue.

Re:Rust and C++ Interoperability in Chrome by Paul-ish on Wednesday August 19, 2020 @09:15PM (#24218852)
Inside both Rust and C++ there are very well developed tools (eg smart pointers) for dealing with memory allocations. These tools were never written to be compatible with each other, so you lose them at the language boundary.
Re:Rust and C++ Interoperability in Chrome by xxpor on Thursday August 20, 2020 @12:26PM (#24224916)
That makes sense. I do 99% C, so any time I even have to think about C++ it unfortunately turns into C with classes rather than C++, so I'm really not as familiar with the smart pointer options.
Re:Rust and C++ Interoperability in Chrome by webkike on Wednesday August 19, 2020 @03:51PM (#24215381)
There are some things that make this better, such as returning pointer types and not returning raw pointers. Returning raw pointers usually means that another function will delete them. I very rarely see pointers handed back from functions being freed caller side
Re:Rust and C++ Interoperability in Chrome by Matthias247 on Wednesday August 19, 2020 @04:13PM (#24215698)
Do you mean references instead of pointers? I'm not familiar of a difference between pointers and raw pointers in C or C++.

C++ allows to be more clear: unique_ptrs allow to move ownership, references mean this is definitely not owned, and the remaining questions are around normal pointers.

With pure C there is however zero distinction. A pointer can mean ownership is transferred, it is meant to be a reference, it could be a static reference to something in constant memory, etc.

Re:Rust and C++ Interoperability in Chrome by webkike on Wednesday August 19, 2020 @05:15PM (#24216609)
I meant pointers versus types such unique_ptr and shared_ptr
Re:Rust and C++ Interoperability in Chrome by Snelius on Wednesday August 19, 2020 @10:32PM (#24219285)
You will lost his (*_ptr) magic cuz types match is not compatible. Mem must be free on the allocation side, kind of law.
Re:Rust and C++ Interoperability in Chrome by edflsafoiewq on Wednesday August 19, 2020 @04:14PM (#24215714)
RAII usually handles all that.
Re:Rust and C++ Interoperability in Chrome by stefan_ on Wednesday August 19, 2020 @03:11PM (#24214794)
You need to answer "who frees it" in Rust, too, it just doesn't allow you to screw it up.
Re:Rust and C++ Interoperability in Chrome by rightbyte on Wednesday August 19, 2020 @03:27PM (#24215048)
Surely C++ exceptions and destructors has to be the main issue for interoperation?

I've done to many C++ wrappers with C linkage of C++ libs to call from C, that convert exceptions to error codes and free_obj() wrappers to run destructors.

Re:Rust and C++ Interoperability in Chrome by bialpio on Wednesday August 19, 2020 @06:47PM (#24217738)
Chromium does not allow using exceptions, so it may not be that important issue to solve.
Re:Rust and C++ Interoperability in Chrome by mkoubaa on Wednesday August 19, 2020 @05:19PM (#24216673)
passing a heap allocated thing from one c++ dll to another doesn't work. it's far better to define a C interface between binaries of any language and reintroduce language specific semantics in a light client library
Re:Rust and C++ Interoperability in Chrome by jcelerier on Thursday August 20, 2020 @03:11AM (#24220701)
> passing a heap allocated thing from one c++ dll to another doesn't work.

this is chromium, everything is built from source with the same compiler, there is zero issue with doing that.

This only does not work when using a DLL built with, say, visual studio 2008 and another built with 2015.

Re:Rust and C++ Interoperability in Chrome by mkoubaa on Friday August 21, 2020 @05:34PM (#24239104)
Actually a DLL built with the same version but with either a different c runtime library (e.g. MD or MT) or a different target (release or debug) will have this problem
Re:Rust and C++ Interoperability in Chrome by ncmncm on Thursday August 20, 2020 @12:09AM (#24219787)
You must be talking about Microsoft stuff not applicable here.
Re:Rust and C++ Interoperability in Chrome by mkoubaa on Friday August 21, 2020 @05:36PM (#24239121)
Not true. GCC broke compatibility with the memory layout of are string at c++11 and passing those between DLLs with versions of GCC on either side of that change will crash.

The standard doesn't specify memory layout or ABI so as a general rule you can't do this. You can get around it by depending on implementation details like what is implicitly done by chromium

Re:Rust and C++ Interoperability in Chrome by ncmncm on Friday August 21, 2020 @07:33PM (#24240002)
Now you are changing the subject. Allocation, which was the subject, happens the same way on either side of gcc's C++11 boundary. The C++11 break was required by the ISO Standard issued in 2011.

The Gcc and libstdc++ maintainers are extremely careful to maintain ABI compatibility between widely-separated releases, where permitted by the Standard. You can happily call between code compiled with gcc-6 and gcc-10, release or debug, optimized or not.

It is possible to deliberately vary ABI with compiler options, but nobody uses those for shared libraries they distribute, for obvious reasons, with the unique exception of choosing pre-C++11 vs post-C++11 ABI.

Re:Rust and C++ Interoperability in Chrome by mkoubaa on Saturday August 22, 2020 @01:50PM (#24245809)
I guess you're right. I still am wary about depending on this given the current conversation about ABI evolution with C++. And given that lots of languages can interop with C I think building C interfaces between binaries buys options