Visual Studio Code, rebuilt in Rust
Rust
refactor(terminal): forbid unsafe code in ghostty_vt
rokartur4ac154163f12
diff --git a/docs/memory-safety.md b/docs/memory-safety.mdindex 40cec10710f..3405277ba7f 100644--- a/docs/memory-safety.md+++ b/docs/memory-safety.md@@ -1,12 +1,12 @@# Memory safety-VSRustium is Rust, so memory safety is the default. It stops being the default in two places: `unsafe` blocks in the port's own code, and code the port links but does not own (GPUI, the OS frameworks, Oniguruma, SQLite). This file sets the rules for the first place, lists every file that has `unsafe` code, and names the checks that fail on a violation. [ARCHITECTURE.md](../ARCHITECTURE.md) has the map of the code.+VSRustium is Rust, so memory safety is the default. It stops being the default in two places: `unsafe` blocks in the port's own code, and code the port links but does not own (GPUI, the OS frameworks, Oniguruma, SQLite, libghostty-vt through libghostty-rs). This file sets the rules for the first place, lists every file that has `unsafe` code, and names the checks that fail on a violation. [ARCHITECTURE.md](../ARCHITECTURE.md) has the map of the code.## The rules1. `unsafe` code is allowed for one purpose: a call across a boundary that Rust cannot check. That is FFI to the OS (libc, AppKit and WebKit through `objc2`, Win32 and COM through `windows`, GTK), and the process calls that Rust marks unsafe (`pre_exec`, `set_var`).2. `unsafe` code is not allowed for speed, for a borrow the compiler rejects, or to silence the compiler: no unchecked indexing, no `transmute` between Rust types, no `static mut`, no `unsafe impl Send` or `Sync` for a port type.-3. `unsafe` code sits in the platform glue of `app` and `exthost`: the file named after the OS beside the portable module (ARCHITECTURE.md, "Platform glue per OS"), or one small function with `cfg` in the module that needs it. The crates `config`, `diff`, `files`, `pixeldiff`, `search`, `storage` and `textmate` have none.+3. `unsafe` code sits in the platform glue of `app` and `exthost`: the file named after the OS beside the portable module (ARCHITECTURE.md, "Platform glue per OS"), or one small function with `cfg` in the module that needs it. The crates `config`, `diff`, `files`, `ghostty_vt`, `pixeldiff`, `search`, `storage` and `textmate` have none.4. Each `unsafe` block and each `unsafe impl` has a `// SAFETY:` comment on the line before it. The comment states the fact that makes the block sound: why each pointer is valid, who owns and frees the object, which thread runs the call, how long a buffer lives. It does not repeat what the call does.5. The function around the block is safe to call. Its signature and its body make the `SAFETY` fact true for every caller. If that is not possible, the function is an `unsafe fn` and each caller states the fact.@@ -80,7 +80,7 @@ Each check fails on a violation. "Shown red" names the planted violation that pr| Rule | Check | Runs | Shown red ||---|---|---|---|-| No `unsafe` in the seven pure crates | `#![forbid(unsafe_code)]` at each crate root | Every build | An `unsafe` block added to `diff/src/lib.rs` stops the build. |+| No `unsafe` in the eight pure crates | `#![forbid(unsafe_code)]` at each crate root | Every build | An `unsafe` block added to `diff/src/lib.rs` stops the build. || An unsafe operation in an `unsafe fn` needs its own block | `unsafe_op_in_unsafe_fn = "deny"` in `native/Cargo.toml` | Every build | A raw pointer read without a block in an `unsafe fn` of `exthost` stops the build. || Each block has a `SAFETY` comment | `clippy::undocumented_unsafe_blocks = "deny"` in `native/Cargo.toml` | `cargo clippy -p app -p exthost --all-targets --locked -- -A clippy::all`, in the `test` jobs of `.github/workflows/ci.yml` | A block without a comment in `exthost/src/utc.rs` fails clippy. || `unsafe` only in the listed files | `bun port/tools/docs-check.ts` | Seconds, anywhere, and in the `checks` job of `.github/workflows/ci.yml` | A file with `unsafe` that is not in the inventory fails the check. |diff --git a/native/crates/ghostty_vt/src/lib.rs b/native/crates/ghostty_vt/src/lib.rsindex df3a18ddb55..c111dfa6aa2 100644--- a/native/crates/ghostty_vt/src/lib.rs+++ b/native/crates/ghostty_vt/src/lib.rs@@ -1,6 +1,8 @@//! libghostty-vt, through the libghostty-vt crate's safe bindings, as the terminal screen the integrated terminal//! draws, in the shape the port used from alacritty_terminal (lines relative to the active area's top, negative into-//! the scrollback). The crate has no `unsafe`.+//! the scrollback).++#![forbid(unsafe_code)]mod selection;