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
37 changes: 37 additions & 0 deletions issues/a-programs-own-toyos-never-sees-the-services-std-adopted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
status: open
kind: defect
opened: 2026-11-12
---

# A program's own `toyos` never sees the services std adopted

std links a copy of `toyos` of its own (`sdk/toyos/src/lib.rs`, `std_io`),
and a program that uses the SDK links another. Each copy keeps its own
adopted services (`toyos::endow::adopt`, `ADOPTED` in
`sdk/toyos/src/endow.rs`), though `adopt`'s contract speaks of "this
process":
- std's `adopt_services` (`sdk/std/os/fs.rs`) fills std's copy, which std's
file resolution and spawns read.
- The program's `toyos::endow::services` and `connector` read its own copy,
which never adopted anything. Its `adopt` fills a set std never reads.

Seen in `adopted_services_stay` (guest run 43156966054, main 302fb3698): the
adopter's `services()` listed nothing after std had adopted `fs:/system`, and
the check on it went red; #1280 removed that check. The supervisor is the
one adopter today and keeps its own connectors, so nothing else reads the
wrong copy yet.

This is the defect #1226 closed for the stdio stream state, which each copy
kept for itself. That state now lives once, in the copy that owns the
descriptor table (the `fd-table` feature: std's in a Rust program, libc's in
a C one), which defines `toyos_stdio_*` beside `toyos_fd_*`, and every copy
reaches it through those.

**Owner**: the orchestrator, which assigns the fix after #1227 lands.

**Exit**: one adopted set per process, held by the copy that owns the
descriptor table and reached by every copy through it, as #1226's stdio state
is. A program's `toyos::endow::services` and `connector` then see what std
adopted, and an adoption through either copy is the one std resolves files
through.
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,10 @@ By mechanisms eliminated:
connect leaves the caller's table as it was), `abuse_listener_hijack`,
`user_copy_faults`, `adopted_services_stay`, `handle_lifetime`,
`process_tree` and `launch_toctou` among them;
- the `testcases` members #1252 adapted, which have only been compiled
in that form, passing: `poll_belongs_to_its_handle` (#1261's, which
connects through its connector), `messages_are_whole`, `moved_rights`
and `mutual_kill`. The shared-boot guest row #1271 adds runs them
too, once it lands;
- the `testcases` members #1252 adapted passing on metal:
`poll_belongs_to_its_handle` (#1261's, which connects through its
connector), `messages_are_whole`, `moved_rights` and `mutual_kill`.
On QEMU, `testcases_members_exit_0` runs them;
- no kernel panic from the slot-order assertions;
- `spawn_cost` within its guard.
- **Owed on metal:** the spawn A/B of main before #1252 against main
Expand Down
12 changes: 7 additions & 5 deletions kernel/src/object/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum HandleError {
BadHandle,
/// The slot has moved past this handle: closed, or its generations ran out.
Stale,
/// Ends the caller like the two above, except through [`HandleTable::get_sent`], the one lookup of a handle a peer sent — `SYS_CONNECT`'s connector and `SYS_SPAWN`'s place and job — where it is `InvalidArgument`.
/// Ends the caller like the two above. [`HandleTable::get_sent`], the one lookup of a handle a peer sent (`SYS_CONNECT`'s connector and `SYS_SPAWN`'s place and job), answers a wrong type `InvalidArgument` and never builds this.
WrongType { held: &'static str, wanted: &'static str },
/// The handle is fine and does not carry what the call needs.
Rights { held: Rights, needed: Rights },
Expand Down Expand Up @@ -318,16 +318,18 @@ impl HandleTable {
.ok_or(HandleError::WrongType { held: entry.object.kind(), wanted: T::NAME })
}

/// [`get`](Self::get) for a handle a peer sent, whose type is that peer's claim rather than proof of a bug in this caller: a wrong type refuses `InvalidArgument`, and every other failure is `get`'s.
/// [`get`](Self::get) for a handle a peer sent, whose type is that peer's claim rather than proof of a bug in this caller: a wrong type refuses `InvalidArgument` whatever rights the handle carries, since a right means something only on its kind, and every other failure is `get`'s.
pub fn get_sent<T: KObjectVariant>(
&self,
h: RawHandle,
need: Rights,
) -> Result<Arc<T>, Refusal> {
match self.get::<T>(h, need) {
Err(HandleError::WrongType { .. }) => Err(SyscallError::InvalidArgument.into()),
other => other.map_err(Refusal::from),
let entry = self.entry_of(h)?;
let object = T::from_ref(&entry.object).ok_or(SyscallError::InvalidArgument)?;
if !entry.rights.contains(need) {
return Err(HandleError::Rights { held: entry.rights, needed: need }.into());
}
Ok(object.clone())
}

/// The borrowing accessor, for a call that completes while still holding the guard.
Expand Down
4 changes: 2 additions & 2 deletions sdk/std/os/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ pub fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Resu
///
/// For the one process no parent endows a service: the supervisor makes the
/// machine's directory capabilities itself. No child inherits them. Refused,
/// and the connectors closed, when this process has adopted some already or
/// has resolved a file.
/// and the connectors closed, when this process has adopted some already, or
/// once std has resolved a file or looked up a service of its own.
///
/// # Safety
/// Each handle names a connector: it is taken as one on the caller's word.
Expand Down
4 changes: 2 additions & 2 deletions tests/guest/rust-tests/src/bin/adopted_services_stay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ fn adopter() {
let system: Connector = Endowments::get().take(GIVEN).expect("the adopter is given /system");
// SAFETY: a connector, as this process was endowed it.
unsafe { std::os::toyos::fs::adopt_services(vec![(SYSTEM.to_string(), system.into())]) }.expect("the adopter adopts /system");
// Non-vacuity: the adopter holds it, and its spawn reads the helper's image through it.
assert_eq!(toyos::endow::services().collect::<Vec<_>>(), [SYSTEM], "what the adopter holds");
// Non-vacuity: std reads the helper's image through `/system`, which it
// holds only by adopting it.
let status = Command::new(SELF).arg("helper").status().expect("spawn the helper through the adopted /system");
assert_eq!(status.code(), Some(HELD_NONE), "the helper held a service: {status:?}");
}
Expand Down
5 changes: 2 additions & 3 deletions tests/guest/rust-tests/src/bin/process_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ fn a_manage_only_handle_is_no_place() {
println!(" a MANAGE-only handle is no place: PermissionDenied");
}

/// A pipe's write end carries `WRITE`, so the kernel reaches the type it is
/// not: the supervisor answers the launch refused, and the arms after this one are the supervisor
/// answering the next.
/// A pipe's write end is no place: the supervisor answers the launch refused,
/// and the arms after this one are the supervisor answering the next.
fn a_pipe_is_no_place() {
let (_read, write) = toyos::pipe_pair().expect("a pipe of our own");
let place = write.as_handle().try_clone_to_owned().expect("a duplicate to send");
Expand Down