Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions build/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,23 @@ pub(crate) const CONTROLS: &[Control] = &[
red(SDK, "transport-end-keeps-inflight", None, &[Fails(
"transport::inflight::tests::an_end_answers_every_tag_once_and_a_late_completion_nothing",
)]),
// The descriptor table's hold beside its writer.
red(SDK, "fd-hold-revives", None, &[
Fails("fd::table::model::a_call_beside_a_close_holds_a_live_object_or_none"),
Fails("fd::table::model::two_calls_beside_a_close_let_the_object_go_once"),
Fails("fd::table::model::a_call_beside_a_dup2_holds_the_old_object_or_the_new"),
]),
red(SDK, "fd-hold-unchecked", None, &[Fails(
"fd::table::model::a_call_on_a_closed_descriptor_never_holds_the_object_that_took_its_slot",
)]),
red(SDK, "fd-publish-relaxed", None, &[
Fails("fd::table::model::a_call_on_a_closed_descriptor_never_holds_the_object_that_took_its_slot"),
Fails("fd::table::model::a_call_on_a_reopened_descriptor_holds_the_old_object_or_the_new"),
]),
red(SDK, "fd-reuse-relaxed", None, &[
Fails("fd::table::model::a_call_on_a_closed_descriptor_never_holds_the_object_that_took_its_slot"),
Fails("fd::table::model::a_call_on_a_reopened_descriptor_holds_the_old_object_or_the_new"),
]),
];

/// Whether a control's run showed its teeth.
Expand Down
25 changes: 25 additions & 0 deletions issues/calls-on-two-served-files-meet-on-one-process-wide-lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
status: open
kind: finding
opened: 2026-11-11
---

# Calls on two served files meet on one process-wide lock

A call through a descriptor holds its object without a lock (#1234). A served
file's object is an index into `files::owner`'s `OPEN`
(`sdk/toyos/src/files/owner.rs`), one `Lock` over every served file the
process holds. `held()` takes it in every call on a served file
(`on_fd_held`) to clone that file's `Arc`, and `release_held` takes it when
the descriptor table lets one go. So two threads reading two different served
files still meet on one lock word each call, which is the collision the
descriptor table's hold no longer has. The critical section is short, so a
collision costs a brief wait. Found by inspection in the review of #1234;
nothing has measured it.

Owner: the SDK's served-file client (`toyos::files`).

**Exit**: a call on one served file takes no lock that a call on another
served file takes, or a metal measurement of two threads on two served files
shows the lock costs nothing material, with that measurement cited here before
the file is deleted.
27 changes: 0 additions & 27 deletions issues/every-descriptor-call-takes-one-process-wide-lock-twice.md

This file was deleted.

15 changes: 15 additions & 0 deletions sdk/toyos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ transport-no-resume = []
# A look without the gate is believed when it names a violation, so a cursor
# another holder moved under it ends the call.
transport-ungated-violation = []
# The descriptor table's controls (`src/fd/table.rs`), declared and run as the
# transport's are; `src/fd/table/model.rs` holds a call beside the writer.
#
# A call counts itself onto a slot whose count is 0, so it holds an object
# the close already let go.
fd-hold-revives = []
# A call keeps its reference without looking at the descriptor again, so it
# holds whatever took the slot its descriptor named.
fd-hold-unchecked = []
# The writer stores a new object's count `Relaxed`, so a call that counts
# itself on need not see the object written.
fd-publish-relaxed = []
# The writer finds a slot free by a `Relaxed` load, so its write of a new
# object need not follow the last call's read of the old one.
fd-reuse-relaxed = []

[lints.rust]
# The guest's target: its stream sinks are compiled only for it.
Expand Down
95 changes: 66 additions & 29 deletions sdk/toyos/src/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,48 +389,70 @@ fn file_group(label: &str) -> Option<Fd> {
/// The table itself, in the copy that owns it.
#[cfg(all(feature = "fd-table", not(test)))]
mod owner {
use super::table::{Fault, Table};
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};

use super::table::{Fault, ObjectCell, Table, Writer};
use super::*;
use crate::endow::Endowments;
use crate::sync::Lock;
use crate::OwnedHandle;

/// The table, and whether its endowed descriptors were installed yet.
static TABLE: Lock<(bool, Table<MAX_FDS>)> = Lock::new((false, Table::new()));
type Descriptors = Table<AtomicU32, ObjectCell<Record>, MAX_FDS>;
type TableWriter<'a> = Writer<'a, AtomicU32, ObjectCell<Record>, MAX_FDS>;

static TABLE: Descriptors = Table::new();
/// Held by whoever changes what a descriptor names.
static WRITING: Lock<()> = Lock::new(());
/// Whether the endowed descriptors were installed yet.
static ENDOWED: AtomicBool = AtomicBool::new(false);

fn refusal(fault: Fault) -> Error {
match fault {
Fault::Refused(e) => e,
Fault::Broken => panic!("toyos::fd: the descriptor table is inconsistent"),
}
}

/// `f` on the table, whose first use installs every descriptor the
/// process was endowed at its number. Nothing panics under the lock: a
/// panic writes to stderr, a descriptor, which would wait on it forever.
fn table<R>(f: impl FnOnce(&mut Table<MAX_FDS>) -> Result<R, Fault>) -> Result<R, Error> {
/// `f` on the table's writer, whose first use installs every descriptor
/// the process was endowed at its number. Nothing panics under the lock.
fn change<R>(f: impl FnOnce(&mut TableWriter<'_>) -> Result<R, Fault>) -> Result<R, Error> {
let endowed = Endowments::get();
let mut refused = 0;
let answer = {
let mut held = TABLE.lock();
let (ready, table) = &mut *held;
let installed = match *ready {
let _held = WRITING.lock();
// SAFETY: the one writer while `WRITING` is held.
let mut writer = unsafe { TABLE.writer() };
let installed = match ENDOWED.load(Ordering::Relaxed) {
true => Ok(()),
false => {
*ready = true;
endow(table, endowed).map(|n| refused = n)
let installed = endow(&mut writer, endowed).map(|n| refused = n);
ENDOWED.store(true, Ordering::Release);
installed
}
};
installed.and_then(|()| f(table))
installed.and_then(|()| f(&mut writer))
};
// Said once the lock is let go, since stderr is a descriptor.
// Said once the lock is let go: the write may block.
for _ in 0..refused {
let _ = crate::files::write(2, b"toyos::fd: an fd: endowment whose number was taken was refused\n");
}
answer.map_err(|fault| match fault {
Fault::Refused(e) => e,
Fault::Broken => panic!("toyos::fd: the descriptor table is inconsistent"),
})
answer.map_err(refusal)
}

/// The table for a call, which takes no lock once the endowed
/// descriptors are installed.
fn table() -> Result<&'static Descriptors, Error> {
if !ENDOWED.load(Ordering::Acquire) {
change(|_| Ok(()))?;
}
Ok(&TABLE)
}

/// Install each `fd:` endowment at its number, a socket's taken up as
/// this process's holder first. One whose number another already took,
/// or whose socket this process holds no room for, is refused, its handle
/// closed: how many were.
fn endow(table: &mut Table<MAX_FDS>, endowed: &Endowments) -> Result<usize, Fault> {
fn endow(table: &mut TableWriter<'_>, endowed: &Endowments) -> Result<usize, Fault> {
let mut refused = 0;
let mut files = alloc::collections::BTreeMap::new();
for label in endowed.labels() {
Expand Down Expand Up @@ -485,16 +507,29 @@ mod owner {

#[no_mangle]
extern "C" fn toyos_fd_install(record: Record, inheritable: bool, floor: Fd) -> i32 {
table(|t| t.install(record, inheritable, floor)).unwrap_or_else(|e| {
change(|t| t.install(record, inheritable, floor)).unwrap_or_else(|e| {
// SAFETY: refused, so the record is still the caller's, given here.
unsafe { record.release() };
encode(e)
})
}

/// A call whose descriptor named a free slot, held under the writer's
/// lock, where that is a finished close or a broken table.
#[cold]
#[inline(never)]
fn settled(fd: Fd) -> Result<(usize, Record), Error> {
change(|t| t.acquire(fd))
}

#[no_mangle]
unsafe extern "C" fn toyos_fd_acquire(fd: Fd, out: *mut Record) -> i32 {
match table(|t| t.acquire(fd)) {
let held = table().and_then(|t| match t.acquire(fd, |record| released(Some(record))) {
Ok(Some(held)) => Ok(held),
Ok(None) => settled(fd),
Err(fault) => Err(refusal(fault)),
});
match held {
Ok((token, object)) => {
// SAFETY: the caller's live place.
unsafe { out.write(object) };
Expand All @@ -505,14 +540,16 @@ mod owner {
}

#[no_mangle]
extern "C" fn toyos_fd_unuse(token: i32) {
unsafe extern "C" fn toyos_fd_unuse(token: i32) {
let token = usize::try_from(token).expect("toyos::fd: a token no acquire answered");
released(table(|t| t.unuse(token)).expect("toyos::fd: a token no acquire answered"));
// SAFETY: the caller's: a token `toyos_fd_acquire` answered, given
// back once.
released(unsafe { TABLE.unuse(token) }.expect("toyos::fd: a token no acquire answered"));
}

#[no_mangle]
extern "C" fn toyos_fd_close(fd: Fd) -> i32 {
match table(|t| t.close(fd)) {
match change(|t| t.close(fd)) {
Ok(object) => {
released(object);
0
Expand All @@ -523,12 +560,12 @@ mod owner {

#[no_mangle]
extern "C" fn toyos_fd_dup(fd: Fd, floor: Fd, inheritable: bool) -> i32 {
table(|t| t.dup(fd, floor, inheritable)).unwrap_or_else(encode)
change(|t| t.dup(fd, floor, inheritable)).unwrap_or_else(encode)
}

#[no_mangle]
extern "C" fn toyos_fd_dup2(old: Fd, new: Fd, inheritable: bool) -> i32 {
match table(|t| t.dup2(old, new, inheritable)) {
match change(|t| t.dup2(old, new, inheritable)) {
Ok(displaced) => {
released(displaced);
new
Expand All @@ -539,7 +576,7 @@ mod owner {

#[no_mangle]
extern "C" fn toyos_fd_replace(fd: Fd, record: Record) -> i32 {
match table(|t| t.replace(fd, record)) {
match change(|t| t.replace(fd, record)) {
Ok(old) => {
released(old);
0
Expand All @@ -554,12 +591,12 @@ mod owner {

#[no_mangle]
extern "C" fn toyos_fd_inheritable(fd: Fd) -> i32 {
table(|t| t.inheritable(fd)).map_or_else(encode, i32::from)
table().and_then(|t| t.inheritable(fd).map_err(refusal)).map_or_else(encode, i32::from)
}

#[no_mangle]
extern "C" fn toyos_fd_set_inheritable(fd: Fd, inheritable: bool) -> i32 {
table(|t| t.set_inheritable(fd, inheritable)).map_or_else(encode, |()| 0)
change(|t| t.set_inheritable(fd, inheritable)).map_or_else(encode, |()| 0)
}
}

Expand Down
Loading