MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / create

Function create

crates/wasi/src/cli/worker_thread_stdin.rs:97–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

95}
96
97fn create() -> GlobalStdin {
98 std::thread::spawn(|| {
99 let state = GlobalStdin::get();
100 loop {
101 // Wait for a read to be requested, but don't hold the lock across
102 // the blocking read.
103 let mut lock = state.state.lock().unwrap();
104 lock = state
105 .read_requested
106 .wait_while(lock, |state| !matches!(state, StdinState::ReadRequested(_)))
107 .unwrap();
108
109 // Extract the size hint from the request and cap it to `MAX_READ_SIZE_ALLOC`
110 // to avoid guest-controlled unbounded allocation.
111 // The `.max(1)` ensures a zero-length read is never misinterpreted as EOF.
112 let size_hint = match *lock {
113 StdinState::ReadRequested(size) => size.min(MAX_READ_SIZE_ALLOC).max(1),
114 _ => unreachable!(),
115 };
116 drop(lock);
117
118 let mut bytes = BytesMut::zeroed(size_hint);
119 let (new_state, done) = match std::io::stdin().read(&mut bytes) {
120 Ok(0) => (StdinState::Closed, true),
121 Ok(nbytes) => {
122 bytes.truncate(nbytes);
123 (StdinState::Data(bytes), false)
124 }
125 Err(e) => (StdinState::Error(e), true),
126 };
127
128 // After the blocking read completes the state should not have been
129 // tampered with.
130 debug_assert!(matches!(
131 *state.state.lock().unwrap(),
132 StdinState::ReadRequested(_)
133 ));
134 let mut lock = state.state.lock().unwrap();
135 *lock = new_state;
136 state.read_completed.notify_waiters();
137 if done {
138 break;
139 }
140 }
141 });
142
143 GlobalStdin::default()
144}
145
146struct WasiStdin;
147

Callers 9

getMethod · 0.70
p2_api_read_onlyFunction · 0.50
instantiate_no_imageFunction · 0.50
instantiate_imageFunction · 0.50
dynamicFunction · 0.50
reset_with_pagemapFunction · 0.50
newMethod · 0.50

Calls 12

spawnFunction · 0.85
DataClass · 0.85
getFunction · 0.50
dropFunction · 0.50
stdinFunction · 0.50
ErrorClass · 0.50
unwrapMethod · 0.45
lockMethod · 0.45
maxMethod · 0.45
minMethod · 0.45
readMethod · 0.45
truncateMethod · 0.45

Tested by 4

p2_api_read_onlyFunction · 0.40
instantiate_no_imageFunction · 0.40
instantiate_imageFunction · 0.40
reset_with_pagemapFunction · 0.40