seL4 / microkit

Microkit - A simple operating system framework for the seL4 microkernel
Other
70 stars 37 forks source link

Difficulty calling `static inline` function in `sel4cp.h` from Rust #8

Closed sledgehammervampire closed 9 months ago

sledgehammervampire commented 2 years ago

The Core Platform headers contain static inline functions. Calling a static inline function from Rust (or any non C language) is difficult, because I believe that static inline functions are not visible outside of their translation unit, and placing such functions in the same translation unit as the Rust code would practically amount to including a C compiler. Is the Core Platform intended to be used with languages other than C?

axel-h commented 2 years ago

Is this what https://github.com/rust-lang/rust-bindgen/issues/399 was about?

sledgehammervampire commented 2 years ago

I believe that makes bindgen just ignore the function? Wouldn't that mean the Rust has to reimplement the functions, which is tedious and error-prone?

axel-h commented 2 years ago

But isn't "static inline" commonly use to especially to allow inlining? Otherwise a simple hack could be using a define STATIC_INLINE and then have

#if ...this-is-c-or-cpp...
#define STATIC_INLINE static inline
#else /* create functions for non-C language binding generator */
#define STATIC_INLINE 
#endif
sledgehammervampire commented 2 years ago

Yes, I believe static inline is used to aid inlining, but as a consequence, makes it difficult to call from other languages. Wouldn't the hack require rewriting a lot of code and also potentially penalize performance?

axel-h commented 2 years ago

This is just about sel4cp.h, so the change seem minimal to provide a solution for the time being. For the performance topic - well, there is no free lunch ;)

sledgehammervampire commented 2 years ago

Where would the definitions previously in the header file go?

axel-h commented 2 years ago

I don't understand the question.

sledgehammervampire commented 2 years ago

Is your proposed solution to make the functions in the header not static inline, and move their definition to some .c file?

axel-h commented 2 years ago

No, it would handle this via a macro, that allows turning this off when building for the non-C language binding generator:

#if BUILDING_STUB_GENERATOR
#define STATIC_INLINE /* nothing */
#else /* normal C/C++ header file */
#define STATIC_INLINE static inline
#endif

STATIC_INLINE void some_function(int some_value) 
{
    ....
}
sledgehammervampire commented 2 years ago

Just omitting the static inline doesn't make things work, unfortunately. I run into what appear to be multiple definition errors, which I presume the static inline avoids.

error: linking with `aarch64-none-elf-ld` failed: exit status: 1
  |
  = note: "aarch64-none-elf-ld" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/goodbye-5704e92d02db27e8.4nbwxkyp3u8nf3jt.rcgu.o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/goodbye-5704e92d02db27e8.552i9atz4kqbrllu.rcgu.o" "--as-needed" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/debug/deps" "-L" "/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/lib" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-Bstatic" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/librustc_std_workspace_core-e63e66292e1f5a9d.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcore-5fed435e2ddaaa95.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcompiler_builtins-40e00e2bb47f3b75.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/goodbye-5704e92d02db27e8" "--gc-sections" "-Tsel4cp.ld"
  = note: aarch64-none-elf-ld: /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib(dbg.o): in function `arm_sys_send_null':
          /home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/include/sel4/sel4_arch/syscalls.h:105: multiple definition of `sel4cp_notify'; /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib(main.o):/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/include/sel4/sel4_arch/syscalls.h:105: first defined here

error: linking with `aarch64-none-elf-ld` failed: exit status: 1
  |
  = note: "aarch64-none-elf-ld" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.3l4j6szzc8b8t3cr.rcgu.o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.irihpas4707d41l.rcgu.o" "--as-needed" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/debug/deps" "-L" "/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/lib" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-Bstatic" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/librustc_std_workspace_core-e63e66292e1f5a9d.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcore-5fed435e2ddaaa95.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcompiler_builtins-40e00e2bb47f3b75.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1" "--gc-sections" "-Tsel4cp.ld"
  = note: aarch64-none-elf-ld: /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib(dbg.o): in function `arm_sys_send_null':
          /home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/include/sel4/sel4_arch/syscalls.h:105: multiple definition of `sel4cp_notify'; /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib(main.o):/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/include/sel4/sel4_arch/syscalls.h:105: first defined here
axel-h commented 2 years ago

Maybe __attribute__((externally_visible)) can be used then. I wonder if this can be used for a static inline function, so any usage gets inline, but also a function symbol will "survive".

sledgehammervampire commented 2 years ago

Same error still, I think

error: linking with `aarch64-none-elf-ld` failed: exit status: 1
  |
  = note: "aarch64-none-elf-ld" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.3l4j6szzc8b8t3cr.rcgu.o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.irihpas4707d41l.rcgu.o" "--as-needed" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/debug/deps" "-L" "/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/lib" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-Bstatic" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/librustc_std_workspace_core-e63e66292e1f5a9d.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcore-5fed435e2ddaaa95.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcompiler_builtins-40e00e2bb47f3b75.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1" "--gc-sections" "-Tsel4cp.ld"
  = note: aarch64-none-elf-ld: /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.irihpas4707d41l.rcgu.o: in function `init':
          /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/src/bin/hello.rs:13: undefined reference to `sel4cp_notify'

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
sledgehammervampire commented 2 years ago

Pretty sure that's the same error as a bare static inline.

error: linking with `aarch64-none-elf-ld` failed: exit status: 1
  |
  = note: "aarch64-none-elf-ld" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.3l4j6szzc8b8t3cr.rcgu.o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.irihpas4707d41l.rcgu.o" "--as-needed" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps" "-L" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/debug/deps" "-L" "/home/rdp/src/sel4cp/release/sel4cp-sdk-1.2.6/board/zcu102/debug/lib" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-Bstatic" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libhello_goodbye-35d8553e62f20864.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/librustc_std_workspace_core-e63e66292e1f5a9d.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcore-5fed435e2ddaaa95.rlib" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/libcompiler_builtins-40e00e2bb47f3b75.rlib" "-Bdynamic" "--eh-frame-hdr" "-znoexecstack" "-L" "/nix/store/91iaw69imbi56wdp7lgc78h0mhvac4qy-rust-default-1.61.0-nightly-2022-03-05/lib/rustlib/zcu102/lib" "-o" "/home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1" "--gc-sections" "-Tsel4cp.ld"
  = note: aarch64-none-elf-ld: /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/target/zcu102/debug/deps/hello-a605fa3c4adfc5c1.irihpas4707d41l.rcgu.o: in function `init':
          /home/rdp/src/sel4cp/example/zcu102/hello-goodbye-rs/src/bin/hello.rs:13: undefined reference to `sel4cp_notify'

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
axel-h commented 2 years ago

ok, seems __attribute__((externally_visible)) can't convince the compiler/linker to to inline it when possible, but preserve is also. It's a pity actually. But maybe there is another attribute?

sledgehammervampire commented 2 years ago

What about removing static inline and using LTO to do cross-TU inlining?

axel-h commented 2 years ago

Well, 'LTO' would be the answer to a lot of such things actually...you can give it a try, question is just how does this interfere with the rest. It might be a solution when it is just used in your case where you want to build a wrapper for the binding.

bennoleslie commented 1 year ago

Calling these functions from a language other than C is not a high priority currently.

A number of considerations would need to go into doing so effectively. My expectation is that it most likely makes sense to have language specific bindings written directly in the language rather than going through a C based translation unit.. The sel4cp functions are mostly very light wrappers around seL4 kernel invocations.

On Wed, 9 Mar 2022 at 01:06, Kevin Tran @.***> wrote:

Calling a static inline function from Rust (or any non C language) is difficult, because I believe that static inline functions are not visible outside of their translation unit, and placing such functions in the same translation unit as the Rust code would practically amount to including a C compiler. Is the Core Platform intended to be called from languages other than C?

— Reply to this email directly, view it on GitHub https://github.com/BreakawayConsulting/sel4cp/issues/8, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACLKJ6OAHSVVQVMMKARDN3U65NHBANCNFSM5QGOHSXQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

Ivan-Velickovic commented 9 months ago

For Rust support in Microkit, the plan is to use Nick Spinale's work here https://github.com/coliasgroup/rust-seL4/. The details will become clearer in the future, https://github.com/seL4/microkit/issues/28 will be updated with progress on integrating everything.