
Understand it
A host function receiving a guest pointer and length must interpret them relative to that module's current linear memory. First validate the requested range, then borrow only the verified bytes. An offset is not a native pointer, and a numerically small address does not establish permission. A length can overflow when added to the offset even if each input separately fits its integer type.
The lab models this boundary with a Rust byte slice and usize offsets. Use checked_add for the end offset and slice::get for the bounds check. Return None for overflow or an out-of-range end. An empty range at memory.len is valid, while an empty range beginning after memory.len is invalid. This precise convention avoids special-case pointer arithmetic that accidentally accepts an invalid start.
Decoding text is a second check. A valid byte range can contain invalid UTF-8, so use from_utf8 if the interface requires text. Do not reinterpret arbitrary guest bytes as a Rust struct whose alignment, layout, or validity requirements may not hold. Copy or decode a documented wire representation instead.
Local compiler profile: standard library only. The model uses no unsafe code and does not implement a runtime memory manager. In a real embedding, memory growth and reentrant calls may invalidate a cached host view, so follow the runtime's borrowing rules and reacquire the view at the appropriate boundary. Tests of this pure function do not establish that a separate native runtime binding follows those rules.
Read the code
fn window(
memory: &[u8],
offset: usize,
len: usize,
) -> Option<&[u8]> {
let end = offset.checked_add(len)?;
memory.get(offset..end)
}
fn main() {
assert_eq!(
window(b"crab", 1, 2),
Some(&b"ra"[..])
);
assert_eq!(
window(b"crab", usize::MAX, 2),
None
);
}
Checked offsets, lengths, and borrowed slices
Try it
Borrow the requested byte range or return None. Check addition overflow. A zero-length range at the end is valid; a start past the end is not. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
Is an in-bounds byte slice necessarily valid UTF-8?
Reveal the answer
- No; text decoding requires another validation step
- Yes, because all memory is UTF-8
- Only if its length is even
Range validity and text validity are separate contracts. Validate the range, then decode if text is required.
Go deeper
Common misconception: Checking the start offset alone makes the entire range safe.
The sum can overflow and the end can exceed memory even when the start is valid.
- Add: Compute the end with checked arithmetic.
- Borrow: Use get to check the complete range.
- Decode: Validate UTF-8 or another wire format separately.
Transfer challenge: Explain why a host view obtained before a reentrant guest call may need to be reacquired afterward.
Connections
- WASM Is Not the Browser API: Targets, host imports, and local execution
- Budget a Local Runtime: Independent CPU, memory, output, and storage limits