-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HELP] FFI compatiblity issues #12960
Comments
@lupyuen could you look at this issue? |
Hi @no1wudi thanks for the interesting question! Sorry I don't strong opinions about this, but here's my gut feel: Solution 1: Reserving space in Rust side for all possible data typesThis approach says that Rust will reserve sufficient Struct Space to fit the largest possible Struct Size (of that specific struct, across all platforms). (Like this) I agree with you: Every byte is precious for Embedded Apps, so I don't think NuttX Community will accept this. Solution 2: Mapping all possible configurations in Rust sideThis approach says that every possible Data Type in C, across all platforms, will be mapped to the equivalent Data Type in Rust. (Sounds like bindgen, see below) I agree with you, this is hard to maintain. And it introduces extra overheads into the build process. Solution 3: Isolate the exported data types from NuttXThis sounds like we're creating Wrapper Types, hiding a C Data Type inside a Rust Data Type. This will be difficult to maintain, and might impact the runtime performance. Solution 4: Rust runtime is only supported for specific configurationsThis approach says that Rust will support only specific NuttX Configurations (e.g. As mentioned, it's less flexible and we might not allow the full functionality of Rust Standard Library. But it seems adequate for now, and I agree with this approach. I'm curious about this potential solution... Solution 5: Use bindgen to generate Rust Types at Compile TimeWe could run bindgen on all the NuttX Header Files (including specific configs like I think we can proceed with Solution 4, and maybe later evolve to Solution 5. Hopefully better tools will emerge. Here's an example: LVGL Graphics Library has a similar problem of supporting multiple embedded platforms with different type sizes. They solve it by running bindgen to generate Custom Rust Bindings for each embedded platform: https://github.com/lvgl/lv_binding_rust Further ResearchThis problem might be common to any Embedded OS with POSIX? Perhaps we could do some research and understand how they handle it:
Hi @acassis wonder if you have anything to add? |
@lupyuen I also don't have strong opinion too! I agree with you Solution 4 could be an initial solution until we get Solution 5 working. |
@lupyuen @acassis Thanks for your feedback! In my opinion, For the This means user must use a nighlty rust toolchain, which may be a problem for production environment. So maybe we should combine Solution 1 and Solution 4 to create a relatively stable binary interface, that allow the I can provide a patch to demonstrate the specific approach in a few days. |
@no1wudi Thanks that sounds good!
Sorry I'm curious: Today Rust Apps won't compile for QEMU RISC-V 32-bit because it needs a Custom Target for rv32gc: https://lupyuen.github.io/articles/rust4 Will this still happen when we have implemented libstd for NuttX? I'm just wondering if we eventually need to recompile libstd anyway, since the NuttX Targets so different from the typical ones. |
@lupyuen I can add it to the Rust side as rust-lang/rust#127755 does in next patch, so all these target in the support list will available out of box. |
@no1wudi That's great thanks! |
I'm not very familiar with esp-idf's Rust implemetation now, but according to the error log, it's very close to #13245 for compatility checking. |
Now I get the Rust app with std library support on my local machine, the size of the full library is around 240K, for a hello world example with println! is about 70K. It's comparable to libcxx. |
[1] https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281600 |
@cederom Thanks for your information !! Currently, the support for Rust is mainly for application development rather than NuttX kernel development, so there might be fewer issues, mainly focusing on ABI compatibility. Regarding ABI compatibility issues, they are not unique to Rust. Currently, we are troubled by this problem. In a large project, we have many teams with different permissions, so some code exists as static libraries. Therefore, any changes involving configurations or public data structures require synchronization between different teams and recompilation of these static libraries. The structures required by the Rust standard library are relatively few and are all defined by POSIX (we should not make too many additional modifications to POSIX definitions), so there is a chance to achieve ABI compatibility under specific configurations. Overall, Rust has its issues, but it should be suitable as a starting point for exploration as a language for application development. |
I am unsure if this is the same issue as the one I had with NuttX: when using NuttX in kernel mode, it is always a pain when app ELFs built with one kernel config doesn't work with another. The reason is that the Whether this is an issue depends on how we want NuttX to be:
The issue isn't specific to the app programming language as it happens to C as well. |
I guess for kernel build on a more powerful platform, maybe many apps runs at same time so it's a challenge to rebuild all of them once any config or change occurred in kernel side. |
Yes I also believe in NuttX as alternative to bigger OS like Linux or BSD or even Android as we get more and more powerful MCU / SoC at single $ at some point we will need to have an OS where people can "just" build a package, install it, and it should "just" work :-) |
Description
Backgound
I'm porting Rust libstd to NuttX, and I've run into a problem with FFI.
Please check the early work in progress: apache/nuttx-apps#2487
For Rust, the libstd is split into two parts:
libcore
andlibstd
.The
libcore
is a minimal runtime that can be used on bare metal systems,and it doesn't depend on any OS-specific functionality. The
libstd
,on the other hand, depends on the OS-specific functionality, such as
threading, networking, etc.
libstd
useslibc
for FFI, and them usually shipped as static libraray.And I've run into a problem with that.
Problem
NuttX is highly configurable, the good part of it is that you can disable
anything you don't need, but the bad part is that you can't assume anything
about the system. For example, the size of complex types like
struct timespec
can be different:
nuttx/include/time.h
Lines 113 to 117 in f7adb52
The
time_t
type can be 32 or 64 bits, and thestruct timespec
can be8 or 16 bytes. This is a problem for Rust, because it's hard to catch this at compile time.
And for libc, there are many complex types will be used in both Rust side and C side,
for current implmentation of
libc
crate, they defiend all of these types again with#[repr(C)]
,which means that they have the same layout as in C:
https://github.com/no1wudi/libc/blob/b395ec66982ae160dba1d7061c51311f32052633/src/unix/nuttx/mod.rs#L32-L56
Maybe for other OS, the data definitons are stable enough, but for NuttX, it's not.
For example, these data types are used in
libc
crate:nuttx/include/pthread.h
Lines 221 to 243 in f7adb52
It's hard to map
CONFIG_SCHED_SPORADIC
andCONFIG_SMP
and mores one by one in Rust side, espcially in a cross-compiling environment.And full list of data types used in
libc
crate can be found here:https://github.com/no1wudi/libc/blob/libc-0.2/src/unix/nuttx/mod.rs
Possible Solutions
1. Reserving space in Rust side for all possible data types
This is the most simple solution, but it's not very elegant. Many strucutres can be used as opaue data blobs in Rust side,
they don't need to be defined excatly same as in C side, just keepping the sizse of them is enough, for example:
https://github.com/no1wudi/libc/blob/b395ec66982ae160dba1d7061c51311f32052633/src/unix/nuttx/mod.rs#L79-L81
The drawback of this solution:
2. Mapping all possible conigurations in Rust side
The drawback of this solution:
libstd
andlibcore
must be build from source code each time.3. Isolate the exported data types from NuttX
Do not use the libc data typs directly in NuttX intenral if any funtionality is conigurable, instead, define a new data type for it.
The drwa back of this solution:
4. Let Rust runtime only avaliable with specific configurations
For example, add a special config like
CONFIG_RUST_RUNTIME
in NuttX, and let it depenends on all the configurations that Rust runtime needs and match the data types withlibc
crate:Discussion
I prefer the solution 4, but it's less flexible if you do not want to use full feaure of Rust libstd.
Any sugestions are welcome!
Verification
The text was updated successfully, but these errors were encountered: