Cracking the FFI Code: From Rust to C Madness

BYMark Howell 1 years ago4 MINS READ
Cracking the FFI Code: From Rust to C Madness

Today in Edworking News we want to talk about Crossing the Impossible FFI Boundary, and My Gradual Descent Into Madness
Anyone trying to make a new mainstream language is completely insane, unless they're backed by a huge corporation. There are only two exceptions in the last 25 years that come close: Scala and Kotlin. They did this by seamlessly building on an existing ecosystem, specifically Java's. But what if you’re a low-level, memory-safe language like Vale, Austral, or Ante? There's no existing ecosystem of fast, memory-safe code to use. And unfortunately, seamlessly building on Rust's ecosystem is impossible... ...or so we thought!

Copy link The Impossible Task

Anyone who has tried to make Java call C, or Python call Javascript, or C call Rust, can tell you that it's really difficult to call functions from other languages. Heroes throughout the ages have created tools like SWIG, CXX, etc., which at least make it possible for a user to reach across the Foreign Function Interface (FFI) boundary, if they can figure out the correct incantations. But even with these tools, it's so difficult that we often just give up and call a microservice instead.
Calling into Rust is even harder; you can't just slap an `extern` onto your function, because Rust doesn't have a stable ABI; rustc-compiled functions don't have a predictable signature that the linker can recognize. Moreover, we can't send normal Rust objects to C. We instead have to make new structs with `#[repr(C)]`, which have restrictive rules about what they can contain. There are even more challenges here, making it an almost uncrossable chasm.

Copy link Overcoming Rust Challenges

Our ultimate goal is to write this Vale code and successfully make a Rust Vec and call capacity on it. The proof-of-concept tool demonstrates how C can treat Rust objects as opaque types, basically raw blobs of bytes that cannot be read directly.
Under the hood, the tool automatically generates some Rust code using `cbindgen` to generate an equivalent C header into "rust_deps/rust_deps.h". However, it mysteriously left out the hardest part: How does the tool get the correct information from Rust to generate the required Rust code?

Gathering Type Information
The tool gathers necessary information by running a small Rust program and reading its output. This program uncovers three pieces of information about `std::vec::Vec`: its size, alignment, and field types. Although this method is somewhat weird, it is straightforward. To gather function information, the tool attempts to generate the Rust code corresponding to a given function and its properties.

Edworking
All your work in one place
All-in-one platform for your team and your work. Register now for Free.
Get Started Now

Copy link A Horrifying Idea

Initially, various strategies were considered, such as using macros or the syn crate, but these did not have the required information. A thought turned to rustc's MIR (Mid-level Intermediate Representation). Fortunately, experts suggested using rustdoc's JSON output instead, which was a much better solution. The tool invokes rustdoc and reads the resulting JSON using `rustdoc_types`, organizing various relationships in HashMaps.

Copy link Implementing Overload Resolution and Generics

When encountering overloaded functions, the tool examines all implementations for a given Rust crate and identifies which ones match user specifications. This was a complex task due to Rust's generic types and varied overloads. After exploring various ideas, a better approach was discovered by leveraging rustdoc's JSON outputs and refining the tool's methodologies.

Ultimately, the Solution
Eventually, by persisting and experimenting, the team devised a surprisingly succinct and effective solution for resolving function overloads. The new Rust-based solution replaced overly complex code, making it simpler without sacrificing functionality. Removing thousands of lines of redundant code felt incredibly liberating.

Edworking
All your work in one place
All-in-one platform for your team and your work. Register now for Free.
Get Started Now

Copy link Insights and Future Directions

The final approach simplifies crossing the FFI boundary between Rust and C, opening doors for seamless integration. The tool's potential in code generation and other applications is immense, encouraging future innovations and explorations.
Remember these 3 key ideas for your startup:

  1. Innovation Through Persistence:
    Success often requires innovative solutions and persistent efforts. Overcoming technical challenges, no matter how daunting, is crucial for startups working with new technologies.
  2. Leveraging Community Knowledge:
    Engage with the community and experts to find better approaches and solutions. Collaboration can reveal simpler, more effective methods than those initially considered.
  3. Simultaneous Simplicity and Complexity:
    Strive for solutions that balance simplicity and functionality. Eliminating unnecessary complexity can lead to more streamlined and robust implementations.
    Edworking is the best and smartest decision for SMEs and startups to be more productive. Edworking is a FREE superapp of productivity that includes all you need for work powered by AI in the same superapp, connecting Task Management, Docs, Chat, Videocall, and File Management. Save money today by not paying for Slack, Trello, Dropbox, Zoom, and Notion.
    For more details, see the original source.
    Onward to new horizons!
Mark Howell

About the Author: Mark Howell

LinkedIn

Mark Howell is a talented content writer for Edworking's blog, consistently producing high-quality articles on a daily basis. As a Sales Representative, he brings a unique perspective to his writing, providing valuable insights and actionable advice for readers in the education industry. With a keen eye for detail and a passion for sharing knowledge, Mark is an indispensable member of the Edworking team. His expertise in task management ensures that he is always on top of his assignments and meets strict deadlines. Furthermore, Mark's skills in project management enable him to collaborate effectively with colleagues, contributing to the team's overall success and growth. As a reliable and diligent professional, Mark Howell continues to elevate Edworking's blog and brand with his well-researched and engaging content.

Startups

Try Edworking Background

A new way to work from anywhere, for everyone for Free!

Get Started Now