CO-RE binding generation for Rust, based on BTF

Overview

rust-struct-bindgen

Here the repo of rust-struct-bindgen, a rust source code generator to read & write native structs with BTF.

There are three crates:

  • rust-struct-bindgen-impl: The core implementation, which accepts a btf::types::Btf and yields a TokenStream represented rust source code.
  • rust-struct-bindgen-proc-macro: A wrapper for the impl, provides convenience for using rust-struct-bindgen in rust sources.
  • rust-struct-bindgen-cli: Another wrapper. Which accepts btf file path from command line argument and prints the output source code to stdout or writes to file

For detailed docs, refer to the doc in lib.rs of rust-struct-bindgen-impl

Build & Usage

Only the CLI crate can be used seperately. To use that, simply run cargo run or cargo build.

If you prefer, you can also install the CLI with running cargo install --path . in the rust-struct-bindgen-cli directory.

Usage: rust-struct-bindgen-cli [OPTIONS] <FILE_PATH>

Arguments:
  <FILE_PATH>  The ELF file path. If with `use_btf`, should be the btf archive path

Options:
  -b, --btf          The provided file is a plain btf archive
  -f, --format       Formatted the generated code. Requires the installation of `rustfmt`
  -o <OUT_FILE>      Out file. If not given, print to stdout
  -h, --help         Print help
  -V, --version      Print version

For example, you can invoke the CLI in the following syntax:

rust-struct-bindgen-cli simple_prog.bpf.o -f -o dump.rs

Will generate bindings for simple_prog.bpf.o (which is an ELF file) , format the generated sources, and write the result to dump.rs.

You might also like...
A dynamically typed, interpreted, stack-based language.

Stacc A dynamically typed, interpreted, stack-based language. How does it work? Each call-frame/scope has its own variables and stack, so you can get/

A faster experimental wasm-based tar implementation for browsers.

@bytedance/tar-wasm A faster* experimental wasm-based tar implementation for browsers. *50-160x faster, see benchmarks below. Usage Install npm instal

Lunatic based webserver embedding WASM. Supports scaling down to zero and up to infinity.

Frenezulo A WASM-embedding webserver build on top of submillisecond and lunatic. Build to serve as an entry point for microservices compiled to WASM.

An attempt to build full-featured WebAssembly-based monolith charts

Graphima Graphima (Greek: γράφημα) is an attempt to build full-featured WebAssembly-based monolith charts. See "Can I Use" WebAssembly for browser sup

An serverless framework based on wasm runtime.

wasm_serverless An distributed serverless framework based on wasm runtime. Feishu doc https://fvd360f8oos.feishu.cn/docx/XSxcdONk2oVJD5xtZuicxftqn3f?f

Slitter is a C- and Rust-callable slab allocator implemented primarily in Rust, with some C for performance or to avoid unstable Rust features.

Slitter is a less footgunny slab allocator Slitter is a classically structured thread-caching slab allocator that's meant to help write reliable long-

A Rust crate for automatically generating C header files from Rust source file.

Please be aware that this crate is no longer actively maintained, please look into the much more feature rich cbindgen instead. rusty-cheddar rusty-ch

Rust-ffi-guide - A guide for doing FFI using Rust

Using unsafe for Fun and Profit A guide to traversing the FFI boundary between Rust and other languages. A rendered version is available here. This gu

Rust library for build scripts to compile C/C++ code into a Rust library

A library to compile C/C++/assembly into a Rust library/application.

Comments
  • Implementation

    Implementation

    This PR implements the rust-struct-bindgen.

    It contains three crates:

    • rust-struct-bindgen-impl: The core implementation, which accepts a btf::types::Btf and yields a TokenStream represented rust source code.
    • rust-struct-bindgen-proc-macro: A wrapper for the impl, provides convenience for using rust-struct-bindgen in rust sources.
    • rust-struct-bindgen-cli: Another wrapper. Which accepts btf file path from command line argument and prints the output source code to stdout or writes to file

    The generating stragety:

    • For every struct, enum, int, float, array, ptr, rust-struct-bindgen will generate a represented type named btf_type_XX, where XX is the btf type id of the corresponding type for them. And the corresponding serializing and deserializing function will also be generated.
    • Serializing functions always have signature like fn (&T) -> Result<Vec<u8>, String>where T is the generated rust type. The function name will always be deserialize_btf_type_XX, where XX is the type id. This function will be in the inner_impl module.
    • Deserializing functions have signature like fn (&[u8]) -> Result<T, String>, T is also the generated rust type. The function name will always be serialize_btf_type_XX, where XX is the type id. This function will be in the inner_impl module.
    • struct and enums will have a type alias pointing to the btf_type_XX type. The alias will be its original name. And deserializing function and serializing function will also be associated functions of the alias type.
    • The alias of struct and enums will be put at the top level module. All other type definitions and (de)serializing functions will be put under a module named inner_impl; All things are pub.

    e.g, for a btf int with sz 64bits and unsigned encoding and typeid 1, rust-struct-bindgen will generate the following code:

        #[allow(unused)]
        #[allow(non_camel_case_types)]
        pub type btf_type_1 = u64;
        #[allow(unused)]
        pub fn deserialize_btf_type_1(b: &[u8]) -> Result<btf_type_1, String> {
            if b.len() != 8 {
                return Err("Expected a slice with 8 bytes".to_string());
            }
            Ok(u64::from_ne_bytes(b.try_into().unwrap()))
        }
        #[allow(unused)]
        pub fn serialize_btf_type_1(v: &btf_type_1) -> Result<Vec<u8>, String> {
            Ok(v.to_ne_bytes().to_vec())
        }
    

    Note: the code above has been formatted by rust-fmt. The directly generated code is terribly ugly, with no line-breaks, and only spaces.

    size/XXL 
    opened by Officeyutong 0
  • [FEATURE] test it with wasm-bpf rust program s

    [FEATURE] test it with wasm-bpf rust program s

    Is your feature request related to a problem? Please describe.

    Use this tool for generating rust binding for wasm-bpf program.

    Describe the solution you'd like

    Modify the rust example and document in wasm-bpf to use this tool.

    enhancement 
    opened by yunwei37 0
Owner
eunomia-bpf
Simplify and enhance eBPF programming with Webassembly, and GPT!
eunomia-bpf
A safe Rust FFI binding for the NVIDIA® Tools Extension SDK (NVTX).

NVIDIA® Tools Extension SDK (NVTX) is a C-based Application Programming Interface (API) for annotating events, code ranges, and resources in your applications. Official documentation for NVIDIA®'s NVTX can be found here.

Spencer Imbleau 78 Jan 2, 2023
Benchmark over Node.js binding frameworks in Rust

Benchmark over Node.js binding frameworks in Rust

LongYinan 7 Dec 28, 2022
High-level memory-safe binding generator for Flutter/Dart <-> Rust

flutter_rust_bridge: High-level memory-safe binding generator for Flutter/Dart <-> Rust Want to combine the best between Flutter, a cross-platform hot

fzyzcjy 2.1k Dec 31, 2022
lzma-rs binding to Node.js via napi-rs.

@napi-rs/lzma lzma-rs binding to Node.js via napi-rs. ?? Help me to become a full-time open-source developer by sponsoring me on Github Install yarn a

LongYinan 8 Aug 16, 2022
Whitewash is python binding for Ammonia.

Whitewash Whitewash is python binding for Ammonia. Ammonia is a whitelist-based HTML sanitization library. It is designed to prevent cross-site script

Vivek Kushwaha 1 Nov 23, 2021
Ypy - a Python binding for Y-CRDT

Ypy is a Python binding for Y-CRDT. It provides distributed data types that enable real-time collaboration between devices. Ypy can sync data with any other platform that has a Y-CRDT binding, allowing for seamless cross-domain communication. The library is a thin wrapper around Yrs, taking advantage of the safety and performance of Rust.

null 51 Dec 20, 2022
TSS of GG18 by WASM, for Confidential Transaction Generation and Signing

TSS WASM portable lightweight client application for threshold ECDSA (based on GG18), built on&for multi-party-ecdsa : Wasm HW friendly Dev yarn build

Eigen Labs 24 Dec 28, 2022
Rust based WASM/JS bindings for ur-rust

ur-wasm-js WASM/JS bindings for the ur-rust rust library Getting started Installation Either build the library yourself with wasm-pack or install for

Lightning Digital Entertainment 5 Feb 28, 2024
Modern JavaScript runtime for Sony PSP, based on rust-psp and QuickJS.

PSP.js Modern JavaScript runtime for Sony PSP, based on rust-psp and QuickJS. ⚠️ Currently in PoC state, unusable for developing JavaScript apps yet.

Yifeng Wang 15 Nov 28, 2022
⚡ Fast Web Security Scanner written in Rust based on Lua Scripts 🌖 🦀

⚡ Fast Web Security Scanner written in Rust based on Lua Scripts ?? ??

Rusty Sec 14 Nov 28, 2022