Repository navigation
Description
In nvx::host, the hexadecimal text of identities has two parsers and three writers that differ only in a constant, or not at all. host::platform::parse_hex_id and parse_hex32 are the same function for 16 and for 32 bytes, and the lowercase-hexadecimal Display loop is written out three times: as host::snapshot::key's private write_hex, and inline in the Display of BootId and of CaptureIdentity. Replace them with one const-generic parse_hex::<N> and one write_hex, both crate-private in host::platform, where the parsers already are.
Area: nvx (host::platform, host::process, host::config, and host::snapshot::key; feature host, every target). Base revision: 621f8b2c200 on dev.
What is duplicated
At the base revision:
| Code | What it does | Every use |
|---|---|---|
platform::parse_hex_id |
Parses exactly 32 hexadecimal digits, in either case, into the 16 bytes that they spell | BootId::from_str; platform::parse_host_id; the Linux boot_id |
platform::parse_hex32 |
The same body, for 64 digits and 32 bytes | CaptureIdentity's Deserialize; the FromStr of the identity! macro, which declares KeyIdentity |
snapshot::key::write_hex |
Writes bytes as lowercase hexadecimal digits, two for each byte | The Display of the identity! macro |
BootId's Display |
The same loop, inline | |
CaptureIdentity's Display |
The same loop, inline |
parse_hex_id and parse_hex32 differ only in their length, 32 or 64 digits, and their array, 16 or 32 bytes, and their names do not show that they are one parser.
What replaces it
In host::platform, where the two parsers are now:
pub(crate) fn parse_hex<const N: usize>(text: &str) -> Option<[u8; N]>: the parsers' body, for2 * Ndigits into[u8; N], which replaces both. Each caller already fixes the length through the type that it builds, a newtype of[u8; 16]or[u8; 32], or a[u8; 16]that it returns, so every call infersN.pub(crate) fn write_hex(formatter: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result:snapshot::key's function, moved, whichBootId,CaptureIdentity, and theidentity!macro'sDisplaycall.
The callers change only their imports and calls. A new unit test in nvx/src/host/platform/tests.rs covers both functions: a round trip in either case, and refusals of a short, long, or odd length, a non-hexadecimal digit, a sign, and a non-ASCII byte. About 80 changed lines in 6 files.
Why it is better
- One parser and one writer define the hexadecimal text of every identity of the host:
BootId, the host's ID,CaptureIdentity, andKeyIdentity. A change to that text lands in all of them at once, and the next identity reuses the pair instead of adding a third parser or a fourth loop. - The parser's name and signature say what it does, for any length, instead of the unrelated-looking
parse_hex_idandparse_hex32. host::platformlies beneathprocess,config, andsnapshotinhost's order of dependencies, whichtests/host_modules.rschecks, and all three already import its parsers, so sharing the writer adds no dependency betweenhost's modules.
Why behavior is unchanged
parse_hex::<16>andparse_hex::<32>run the same code asparse_hex_idandparse_hex32: the same check of the length in bytes, and the samechar::to_digit(16)of each digit, which takes either case and refuses signs and non-ASCII bytes.write_hexis the loop that the threeDisplayimpls run now,write!(formatter, "{byte:02x}")for each byte, which ignores the formatter's width and fill as they do.- No error message,
ErrorKind, serde form, protocol byte, golden fixture, or command line changes. Both functions are private to the crate, so the public API andnvx/public-api/do not change.
Acceptance criteria
-
host::platformholds oneparse_hex::<N>and onewrite_hex, andparse_hex_id,parse_hex32,snapshot::key'swrite_hex, and the inline loops ofBootIdandCaptureIdentityare gone. The diff touches onlynvx/src/host/platform/{mod,tests}.rs,nvx/src/host/platform/linux/process.rs,nvx/src/host/process.rs,nvx/src/host/config/mod.rs, andnvx/src/host/snapshot/key.rs, with fewer than 1000 changed lines (about 80), insertions plus deletions asgit diff --shortstatreports them. -
nvx/public-api/linux.txtandnvx/public-api/windows.txtare unchanged, andpython3 scripts/public_api.pypasses. - Protocol bytes, golden fixtures, serde forms, error messages, and
ErrorKinds are unchanged. - The existing tests are unchanged, and the affected ones pass at the base revision and with the change: those of
host::platform,host::process,host::config, andhost::snapshot::key, among themhost_identities_are_read_as_openvmm_reads_them,boot_identities_have_one_text_form,the_current_boot_has_one_identity,equal_captures_have_one_identity_in_one_text, andidentities_have_a_text_form. - A new unit test covers
parse_hex::<N>andwrite_hex. - The gates that
python3 scripts/local_gates.py --base origin/devselects pass, among them check-nvx: formatting, clippy with-D warningsfor every feature set that it lints, the tests,cargo docwith-D warnings, the public API, the MSRV check, the macOS check, and the guest binaries.
Follow-ups
Not part of this issue:
- The host's other lowercase-hexadecimal text, which builds a
Stringbyte by byte:host::cli::hex, with featurecli, and the inline encodings ofvm_directory.rs, a Windows pipe's name,platform/windows/log_claim.rs, a namespace event's name, andsnapshot/capture.rs, a snapshot description'shostId. A crate-privatehex(bytes) -> Stringbesidewrite_hexwould serve them andhost::cli::hex's callers, andcli/download/archive.rs'ssha256_filecan callhost::cli::hexalready. The same encodings inhost/cli/download.rsandhost/cli/run.rswait for arm64: add native cold-boot foundation #1509, and that ofhost/sandbox/distro.rsfor host: make the sandbox items that only their own module uses private #1683. - The same text outside
host, which builds without featurehost:guest_restore'swrite_hex, the same function assnapshot::key's;GenerationId'sDisplay;protocol::view::tokens; theMagicarm ofRecordError'sDisplay;guest::reseed'shex_valueandprint_hex;Uuid'sDisplayand its parser; andguest::time::decoders'sformat_hexandparse_hex::<N>, whose file arm64: add native cold-boot foundation #1509 changes. One crate-private module could serve them andhostalike, but a new top-level module changes the layouts innvx/README.mdanddoc/project-structure.md, which arm64: add native cold-boot foundation #1509 and ci: check that every pull request references an issue #1679 change now. - Duplications that the survey found but did not confirm:
host/platform/windows/private.rs:check_vm_dirrepeatsplain_dir_problem's open of a plain directory andprivacy_problem's read of a handle's owner and DACL, andvm_dir_problemrepeatsprivacy_problem's walk of the access control entries.VmConfig::openvmm_argumentsandhost::vm'srestore_argumentsrepeat the refusal of an empty path or one with a NUL, with the same message, besidecheck_endpointandcheck_argument_nuls.
The survey turned up no suspected bug.
Activity