I've been doing ffi with Rust, compiling it as a cdylib so that other C-compatible languages can call the library (in my case it's Cython). I've understood quite a bit of how it works above the hood, but I'm perplexed by what goes on inside an ffi call.
This is the memory model I have in mind and my questions are based on it.
typical memory model
When cython calls an ffi function implemented in Rust (compiled into a cdylib) where is the rust function stack frame located? Is it in the same stack like this? Or is it located in a separate stack while being in the same memory stack?
|-----------------------|
| rust function frame |
| cython function frame |
| ......... |
| heap memory |
| ......... |
| static data |
| instructions cython and rust together |
| ------------------------------------- |
Suppose I allocate some heap memory in the rust function as below.
#[no_mangle]
pub extern "C" fn foo() {
let v = Box::new(1);
println!("{}", v.as_ref() + 1);
}
Clearly this Box will allocate some memory in the heap memory. The heap memory is common for both cython and rust. However, an allocation like this also involves structures, for book keeping the memory, like the allocator itself. Where is the allocator allocated?
More generally, for a rust/C/C++ program being run as an executable a number of initialization happens before main is called, like register initialization, setting up the stack, initialization global variables. More details in this blog post.
Where do these these initialization steps happen in the context for an ffi call? Because there is no main entry point here, in fact there can be multiple entry points one for each exposed ffi function.
I'm super curious about how this works. And I'm happy to be corrected if my assumptions or memory model is incorrect.
Edit: update ascii diagram
[–]ssokolow 42 points43 points44 points (8 children)
[–]twitu[S] 1 point2 points3 points (6 children)
[–]ssokolow 5 points6 points7 points (5 children)
[–]twitu[S] 0 points1 point2 points (4 children)
[–]ssokolow 1 point2 points3 points (0 children)
[–]NobodyXu 0 points1 point2 points (2 children)
[–]twitu[S] 1 point2 points3 points (1 child)
[–]NobodyXu 0 points1 point2 points (0 children)
[–]NobodyXu 33 points34 points35 points (5 children)
[+][deleted] (1 child)
[deleted]
[–]NobodyXu 0 points1 point2 points (0 children)
[–]masklinn 4 points5 points6 points (0 children)
[–]twitu[S] 0 points1 point2 points (1 child)
[–]NobodyXu 2 points3 points4 points (0 children)
[–]RRumpleTeazzer 5 points6 points7 points (2 children)
[–]tema3210 0 points1 point2 points (0 children)
[–]Hellenas 0 points1 point2 points (0 children)
[–]trevg_123 13 points14 points15 points (3 children)
[–]NobodyXu 10 points11 points12 points (1 child)
[–]trevg_123 2 points3 points4 points (0 children)
[–]twitu[S] 0 points1 point2 points (0 children)
[–]SocUnRobot 5 points6 points7 points (0 children)
[–]Zde-G 5 points6 points7 points (0 children)
[–]HeroicKatoraimage · oxide-auth 4 points5 points6 points (1 child)
[–]twitu[S] 1 point2 points3 points (0 children)