• Stars
    star
    1,286
  • Rank 35,202 (Top 0.8 %)
  • Language
    Rust
  • License
    Other
  • Created over 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Safe Rust bindings to POSIX-ish APIs

rustix

Safe Rust bindings to POSIX/Unix/Linux/Winsock2 syscalls

A Bytecode Alliance project

Github Actions CI Status zulip chat crates.io page docs.rs docs

rustix provides efficient memory-safe and I/O-safe wrappers to POSIX-like, Unix-like, Linux, and Winsock2 syscall-like APIs, with configurable backends. It uses Rust references, slices, and return values instead of raw pointers, and I/O safety types instead of raw file descriptors, providing memory safety, I/O safety, and provenance. It uses Results for reporting errors, bitflags instead of bare integer flags, an Arg trait with optimizations to efficiently accept any Rust string type, and several other efficient conveniences.

rustix is low-level and, and while the net API supports Winsock2 on Windows, the rest of the APIs do not support Windows; for higher-level and more portable APIs built on this functionality, see the cap-std, memfd, timerfd, and io-streams crates, for example.

rustix currently has two backends available:

  • linux_raw, which uses raw Linux system calls and vDSO calls, and is supported on Linux on x86-64, x86, aarch64, riscv64gc, powerpc64le, arm (v5 onwards), mipsel, and mips64el, with stable, nightly, and 1.63 Rust.

    • By being implemented entirely in Rust, avoiding libc, errno, and pthread cancellation, and employing some specialized optimizations, most functions compile down to very efficient code, which can often be fully inlined into user code.
    • Most functions in linux_raw preserve memory, I/O safety, and pointer provenance all the way down to the syscalls.
  • libc, which uses the libc crate which provides bindings to native libc libraries on Unix-family platforms, and windows-sys for Winsock2 on Windows, and is portable to many OS's.

The linux_raw backend is enabled by default on platforms which support it. To enable the libc backend instead, either enable the "use-libc" cargo feature, or set the RUSTFLAGS environment variable to --cfg=rustix_use_libc when building.

Cargo features

The modules rustix::io, rustix::fd, and rustix::ffi are enabled by default. The rest of the API is conditional with cargo feature flags:

Name Description
event rustix::event—Polling and event operations.
fs rustix::fs and rustix::path—Filesystem operations.
io_uring rustix::io_uring—Linux io_uring.
mm rustix::mm—Memory map operations.
net rustix::net and rustix::path—Network-related operations.
param rustix::param—Process parameters.
pipe rustix::pipe—Pipe operations.
process rustix::process—Process-associated operations.
procfs rustix::procfs—Utilities for reading /proc on Linux.
pty rustix::pty—Pseudoterminal operations.
rand rustix::rand—Random-related operations.
stdio rustix::stdio—Stdio-related operations.
system rustix::system—System-related operations.
termios rustix::termios—Terminal I/O stream operations.
thread rustix::thread—Thread-associated operations.
time rustix::time—Time-related operations.
use-libc Enable the libc backend.

64-bit Large File Support (LFS) and Year 2038 (y2038) support

rustix automatically uses 64-bit APIs when available, and avoids exposing 32-bit APIs that would have the year-2038 problem or fail to support large files. For instance, rustix::fstatvfs calls fstatvfs64, and returns a struct that's 64-bit even on 32-bit platforms.

Similar crates

rustix is similar to nix, simple_libc, unix, nc, and uapi. rustix is architected for I/O safety with most APIs using OwnedFd and AsFd to manipulate file descriptors rather than File or even c_int, and supporting multiple backends so that it can use direct syscalls while still being usable on all platforms libc supports. Like nix, rustix has an optimized and flexible filename argument mechanism that allows users to use a variety of string types, including non-UTF-8 string types.

relibc is a similar project which aims to be a full "libc", including C-compatible interfaces and higher-level C/POSIX standard-library functionality; rustix just aims to provide safe and idiomatic Rust interfaces to low-level syscalls. relibc also doesn't tend to support features not supported on Redox, such as *at functions like openat, which are important features for rustix.

rustix has its own code for making direct syscalls, similar to the syscall, sc, and scall crates, using the Rust asm! macro. rustix can also use Linux's vDSO mechanism to optimize Linux clock_gettime on all architectures, and all Linux system calls on x86. And rustix's syscalls report errors using an optimized Errno type.

rustix's *at functions are similar to the openat crate, but rustix provides them as free functions rather than associated functions of a Dir type. rustix's CWD constant exposes the special AT_FDCWD value in a safe way, so users don't need to open . to get a current-directory handle.

rustix's openat2 function is similar to the openat2 crate, but uses I/O safety types rather than RawFd. rustix does not provide dynamic feature detection, so users must handle the NOSYS error themselves.

rustix's termios module is similar to the termios crate, but uses I/O safety types rather than RawFd, and the flags parameters to functions such as tcsetattr are enums rather than bare integers. And, rustix calls its tcgetattr function tcgetattr, rather than Termios::from_fd.

Minimum Supported Rust Version (MSRV)

This crate currently works on the version of Rust on Debian stable, which is currently Rust 1.63. This policy may change in the future, in minor version releases, so users using a fixed version of Rust should pin to a specific version of this crate.

More Repositories

1

wasmtime

A fast and secure runtime for WebAssembly
Rust
14,541
star
2

wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
C
4,528
star
3

lucet

Lucet, the Sandboxing WebAssembly Compiler.
Rust
4,074
star
4

cranelift

Cranelift code generator
2,482
star
5

javy

JS to WebAssembly toolchain
Rust
1,958
star
6

wasm-tools

CLI and Rust libraries for low-level manipulation of WebAssembly modules
Rust
1,105
star
7

wit-bindgen

A language binding generator for WebAssembly interface types
Rust
887
star
8

wizer

The WebAssembly Pre-Initializer
Rust
859
star
9

wasmtime-go

Go WebAssembly runtime powered by Wasmtime
Go
729
star
10

cap-std

Capability-oriented version of the Rust standard library
Rust
614
star
11

cranelift-jit-demo

JIT compiler and runtime for a toy language, using Cranelift
Rust
588
star
12

jco

JavaScript tooling for working with WebAssembly Components
Rust
468
star
13

cargo-wasi

A lightweight Cargo subcommand to build Rust code for the `wasm32-wasi` target
Rust
435
star
14

cargo-component

A Cargo subcommand for creating WebAssembly components based on the component model proposal.
Rust
405
star
15

wasmtime-dotnet

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/
C#
391
star
16

wasmtime-py

Python WebAssembly runtime powered by Wasmtime
Python
352
star
17

wasi

Experimental WASI API bindings for Rust
Rust
210
star
18

wasmparser

A simple event-driven library for parsing WebAssembly binary files
178
star
19

regalloc2

A new register allocator
Rust
178
star
20

wasi.dev

HTML
170
star
21

ComponentizeJS

JS -> WebAssembly Component
Rust
163
star
22

registry

WebAssembly Registry (Warg)
Rust
153
star
23

wasmtime-demos

Historical and dated demos for Wasmtime usage and WASI content
C#
152
star
24

wat

Rust WAT and WAST parser (WebAssembly Text Format)
113
star
25

regalloc.rs

Modular register allocator algorithms
Rust
107
star
26

WASI-Virt

Virtual implementations of WASI APIs
Rust
98
star
27

componentize-py

Rust
89
star
28

spidermonkey-wasm-rs

Rust
86
star
29

preview2-prototyping

Polyfill adapter for preview1-using wasm modules to call preview2 functions.
Rust
78
star
30

wasmtime-rb

Ruby WebAssembly runtime powered by Wasmtime
Rust
73
star
31

wasmtime-cpp

C++
70
star
32

wasm-interface-types

Raw Rust toolchain support for Wasm Interface Types
Rust
70
star
33

wac

WebAssembly Composition (WAC) tooling
Rust
69
star
34

sightglass

A benchmark suite and tool to compare different implementations of the same primitives.
C
64
star
35

rfcs

RFC process for Bytecode Alliance projects
57
star
36

component-docs

Documentation around creating and using WebAssembly Components
Rust
46
star
37

target-lexicon

Target "triple" support
Rust
44
star
38

userfaultfd-rs

Rust bindings for the Linux userfaultfd functionality
Rust
42
star
39

system-interface

Extensions to the Rust standard library
Rust
40
star
40

wasi-nn

High-level bindings for wasi-nn system calls
CSS
36
star
41

wasmprinter

Rust library to print a WebAssembly binary to its textual format
32
star
42

spidermonkey-wasm-build

Utilities to compile SpiderMonkey to wasm32-wasi
JavaScript
22
star
43

wit-deps

WIT dependency manager
Rust
20
star
44

meetings

Python
20
star
45

filecheck

Library for writing tests for utilities that read text files and produce text output
Rust
20
star
46

vscode-wit

Visual Studio Code extension to recognize and highlight the WebAssembly Interface Type (WIT) IDL.
TypeScript
19
star
47

wamr-rust-sdk

Rust
15
star
48

wasm-score

A benchmark for standalone WebAssembly
C
15
star
49

cranelift.vim

Vim editor configuration for working with cranelift IR (clif) files
Vim Script
14
star
50

arf-strings

Encoding and decoding for ARF strings
C
12
star
51

SIG-Registries

11
star
52

bytecodealliance.org

CSS
10
star
53

subscribe-to-label-action

A GitHub action that allows users to subscribe to a label and automatically get @'d when the label is applied
JavaScript
10
star
54

SIG-Guest-Languages

Special Interest Group (SIG) whose goal is to investigate how best to integrate Wasm and components into dynamic programming language ecosystems in a way that feels native to those ecosystems.
10
star
55

wasm-spec-interpreter

Rust bindings for the Wasm spec interpreter.
Rust
8
star
56

governance

7
star
57

wasm-parallel-gzip

Some example scripts for building a parallel compression/decompression tool for WebAssembly
Makefile
6
star
58

wamr-python

Python
6
star
59

fs-set-times

Set filesystem timestamps
Rust
5
star
60

arena-btree

Rust
5
star
61

wasmtime-libfuzzer-corpus

libFuzzer corpus for our wasmtime fuzz targets
Shell
5
star
62

wamr.dev

The WAMR homepage
HTML
5
star
63

wasmtime.dev

The Wasmtime homepage
CSS
4
star
64

cm-go

4
star
65

libc-test

Mirror of git://nsz.repo.hu:49100/repo/libc-test (see https://wiki.musl-libc.org/libc-test.html for more information)
C
3
star
66

wamr-app-framework

WebAssembly Micro Runtime Application Framework
C
2
star
67

wasmtime-wasi-nn

2
star
68

actions

GitHub actions to setup wasm-tools and wasmtime
TypeScript
1
star
69

label-messager-action

Automatically leave a message when an issue or pull request has a certain label
JavaScript
1
star
70

wasm-ml-meetings

Informal working group for machine learning and WebAssembly, especially wasi-nn
1
star