Repository navigation
Fix the semihosting file open modes for update and append - #4312
Merged
bugadani merged 1 commit intoSep 6, 2026
Merged
Conversation
`open_file` in probe-rs-tools/src/bin/probe-rs/rpc/utils/semihosting.rs mapped the ARM semihosting SYS_OPEN mode strings onto `File::options()` with three mistakes: - `r+` and `r+b` set `truncate(true)`. C `fopen` opens `r+` for update without truncating, so a target that opened an existing file to update it found the file empty. - `a` and `ab` set `write(true)` but never `append(true)`, so writes landed at offset 0 and overwrote the start of the file instead of appending to it. - `a+` and `a+b` set `read(true).write(false).create(true)`. `create` needs write or append access, so `File::options().open()` always returned `InvalidInput` and the open never succeeded. Use `truncate(false)` for `r+`, and `append(true)` for `a` and `a+`. Add tests over `open_file` for the five distinct behaviours of the mode table: `r` and `r+` keep the contents, `w` truncates, `a` and `a+` append.
bugadani
approved these changes
Sep 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
open_fileinprobe-rs-tools/src/bin/probe-rs/rpc/utils/semihosting.rsmaps the ARM semihostingSYS_OPENmode strings ontoFile::options(). Three of the six rows do not do what Cfopendoes, so a target that opens a file for update or for appending gets the wrong file.Line 196,
"r+" | "r+b", setstruncate(true).fopenopensr+for update and leaves the contents alone, so a target that reopened a file to patch part of it found the file empty.Line 199,
"a" | "ab", setswrite(true)and neverappend(true), so the writes land at offset 0 and overwrite the start of the file. Writingabto a file holding0123456789producedab23456789instead of0123456789ab.Line 200,
"a+" | "a+b", setsread(true).write(false).create(true).createneeds write or append access, soFile::options().open()returnsInvalidInputwith "creating or truncating a file requires write or append access" anda+never opens a file at all. That is the same std rule #3960 ran into forr, which fixedronly.The fix is
truncate(false)forr+, andappend(true)foraanda+.wandw+were already right.The tests call
open_filedirectly, so they need no probe and no target. They cover the five distinct behaviours of the mode table. Against the mode table on master,randwpass and the other three fail:With the change, on macOS 26.2 arm64 with rustc 1.95.0:
cargo test -p probe-rs-tools --bin probe-rs semihosting::givestest result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 150 filtered out.cargo fmt --all -- --checkreports no diff.cargo clippy -p probe-rs-tools --all-features --all-targets --locked -- -D warnings -A clippy::collapsible_matchfinishes clean. The allow coversutil/cargo.rs:86, which the clippy in my toolchain also rejects on master, before this change.Must
errorsorwarnings.cargo fmtwas run.changelogs/. See https://github.com/probe-rs/probe-rs/blob/master/CONTRIBUTING.md#changelog-entries.Nice to have