Skip to content

Latest commit

 

History

History
305 lines (229 loc) · 11.6 KB

HACKING.md

File metadata and controls

305 lines (229 loc) · 11.6 KB

Hacking on ufat

ufat is built with cargo. It depends on the libc, sha2, lexopt and serde_json crates. lexopt parses the command line of the tool, and serde_json reads and writes JSON.

ufat does not link against libblkid. It loads libblkid.so.1 with dlopen() when it needs it. The workspace has a crate for each part of that:

  • dlopen loads a library and looks up its functions.
  • dlopen-note writes the .note.dlopen ELF note of the ELF dlopen metadata specification (https://uapi-group.org/specifications/specs/elf_dlopen_metadata/), which tells packaging tools that a program loads a library.
  • blkid has the functions of libblkid that ufat calls, and declares libblkid in such a note.

Requirements

  • rustc and cargo 1.85 or newer. ufat has to keep building with rustc 1.85.
  • Linux with glibc or musl, on x86_64, i686, aarch64, armv7, loongarch64, powerpc, powerpc64, powerpc64le, riscv64, s390x or sparc64. The build fails on other architectures. musl is tested on x86_64 only.
  • rustfmt, for the formatting check, and clippy, for the lints.

Building

cargo build --locked
cargo build --release --locked

cargo build builds the library and the tool target/debug/ufat. --release builds target/release/ufat. cargo install --locked --path . installs the tool into ~/.cargo/bin. With --root /usr/local it goes into /usr/local/bin.

Cargo.lock is part of the tree. --locked makes cargo use the versions it lists.

With rustup, cargo +1.85.0 selects rustc 1.85:

cargo +1.85.0 build --locked

Tests

cargo test --locked

cargo test runs:

  • the unit tests of the library and of the uapi16, varlink, dlopen, dlopen-note and blkid crates,
  • the integration tests in tests/,
  • the crash consistency harness.

The tests build with the dev profile, which is optimized and keeps the overflow checks and debug assertions. --release turns those off.

Set TMPDIR to a directory with a short path that is not a symbolic link. Some tests create Unix sockets below it, and socket paths are limited to 108 bytes. One test compares the targets of /proc/self/fd with paths below TMPDIR.

cargo test runs the test binaries one after the other. RUST_TEST_THREADS= limits how many tests of one binary run at the same time.

tests/varlink.rs also drives the service with varlinkctl when it is installed.

tests/cli.rs and tests/varlink.rs make images with mkfs.ext4, mkfs.xfs, mkfs.btrfs, mkfs.exfat, mkfs.ntfs, mkfs.fat, mkswap, cryptsetup and sfdisk, and skip the ones that are not installed. Whether ufat can detect them depends on the presence of libblkid. The tests check whether libblkid can be loaded and expect what ufat does with it, or without it. The tests under qemu in the CI usually run without it.

Formatting

cargo fmt --all
cargo fmt --all --check
rustfmt --check --edition 2021 verify/lib.rs

The code is formatted with rustfmt. rustfmt.toml sets the line width to 120 columns. cargo fmt --all formats the code, and --check checks it. verify/lib.rs belongs to no package, so rustfmt checks it directly.

Lints

cargo clippy --locked --workspace --all-targets -- -D warnings

clippy checks every crate, the tests and the helpers included. It takes the oldest rustc ufat builds with from rust-version in Cargo.toml, so it does not suggest functions that rustc 1.85 lacks.

The lints depend on the target and on the version of rustc. --target checks another architecture once rustup target add has installed it. That needs no cross linker.

The case folding table

src/fat/name/fold_table.rs is generated by tools/fold-table.rs from UnicodeData.txt and SpecialCasing.txt of Unicode 16.0.0. tools/fold-table.sha256 holds their hashes. To check the table, download the two files, check their hashes, and compare the generated table with the one in the tree:

mkdir ucd
curl -sSfL -o ucd/UnicodeData.txt https://www.unicode.org/Public/16.0.0/ucd/UnicodeData.txt
curl -sSfL -o ucd/SpecialCasing.txt https://www.unicode.org/Public/16.0.0/ucd/SpecialCasing.txt
(cd ucd && sha256sum -c ../tools/fold-table.sha256)
cargo run --locked -q -p fold-table ucd | diff -u src/fat/name/fold_table.rs -

The table never changes. A newer Unicode version would make some names that differ today one name.

Exhaustive sweeps

cargo test --locked --test exhaustive -- --ignored

tests/exhaustive.rs checks the layout computation, the time conversion and the name handling over large ranges of inputs. It takes about a minute on 16 cores. Its tests are ignored by default, so a plain cargo test skips them.

Kani proofs

The pure modules contain proofs for the Kani model checker in #[cfg(kani)] blocks. verify/lib.rs is a crate root that holds only these modules. Install Kani with:

cargo install --locked kani-verifier
cargo kani setup

Then run:

verify/run.sh
HARNESS=layout::proofs::compute_fixed_params_any_total verify/run.sh
SLOW=1 verify/run.sh
DIR=1 verify/run.sh

HARNESS= runs one harness. SLOW=1 adds the harnesses that take longer than half an hour. DIR=1 adds the harness of the directory index, which has not finished yet.

Up to four harnesses run at the same time. KANI_JOBS= changes that. KANI_VERBOSE=1 runs them one after the other and prints every check. Some harnesses need more memory than a laptop has. The default set takes about half an hour on a 512 core machine with KANI_JOBS=24.

Reference tests

tests/reference.rs compares ufat with mkfs.fat, fsck.fat, mtools and sha256sum. A test whose tool is not installed is skipped. UFAT_MTOOLS= points at a directory with mtools binaries, for example a local build.

Crash consistency

tests/crash/crash.rs records the writes of a test scenario. It uses a copy of the library built with --cfg ufat_trace, the fat-trace package. It then builds every state a crash could leave on the device and checks each one with fsck and against the expected files. Three models of the device are checked:

  • Every sector is written whole, and writes land in order.
  • A write of several sectors can stop at any sector boundary.
  • Between two flushes, the device can keep any subset of the writes.

The harness runs as part of cargo test. For its full output, run it on its own:

cargo test --locked -p fat-trace -- --nocapture
UFAT_CRASH_FULL=1 cargo test --locked -p fat-trace -- --nocapture

UFAT_CRASH_FULL=1 checks every subset of the writes between two flushes instead of a sample.

Fuzzing

cargo build --locked -p ufat -p fuzz-seeds
tests/fuzz/run.sh
ITERATIONS=20000 WORKERS=16 tests/fuzz/run.sh

tests/fuzz/run.sh mutates FAT images and manifests and runs the tool and the service on them. It also sends malformed Varlink messages to the service. The default run takes about 15 seconds. It needs python3. mkfs.fat and mtools add more seed images when they are installed.

The results go to target/fuzz. FUZZ_OUT= changes that. Every crash, hang or unexpected exit is stored under out/findings together with the input that caused it. tests/fuzz/repro/ holds checks for defects found earlier.

Coverage

tools/coverage.sh

tools/coverage.sh builds everything with coverage instrumentation in target/coverage and runs cargo test with the arguments it was given. It then merges the profiles of the test run and prints the line coverage of every file. The report is saved in target/coverage/report.txt.

The report needs llvm-profdata and llvm-cov at least as new as the LLVM in rustc. rustup component add llvm-tools installs matching ones, and the script uses them. LLVM_PROFDATA= and LLVM_COV= select others.

Benchmarks

cargo build --release --locked -p ufat -p bench
bench/run.sh
BENCH_CASES='^populate|^fsck' bench/run.sh

bench/run.sh generates an ESP of 270 MB and trees of 10000 files. It times ufat against mkfs.fat, mcopy, fsck.fat and sha256sum on them, and checks that ufat assemble and mkfs.fat write the same bytes. BENCH_CASES= selects cases by a regular expression. The other settings are described at the top of bench/run.sh. The results are in $BENCH_DIR/out/summary.txt.

bench/compare.sh compares two ufat binaries, BASE and HEAD:

cargo build --release --locked -p ufat -p bench
bench/compare.sh BASE HEAD
bench/compare.py "$BENCH_DIR/out"

It runs eleven small workloads: assemble, populate, check and manifest with trees of 10000 files and with an ESP, a plan from the manifest of such a tree, and two Varlink calls. Every workload runs once under cachegrind, which counts the instructions, and once under strace, which counts the system calls. Then it runs nine times with each binary, alternating, for the wall time, the peak RSS and the bytes read and written. It needs valgrind and strace.

HEAD writes the manifests of the workloads that read a manifest, assemble/esp-stream, plan/wide-manifest and varlink/Assemble-esp-stream, and BASE reads them too. So BASE has to read what HEAD writes. That holds as long as both binaries have the same manifest reader. Against an older BASE those workloads can fail.

bench/compare.py prints the results of HEAD and their change from BASE. It fails when HEAD exceeds BASE on a workload by more than a limit:

Metric Limit
Instructions 3% and 1 million
System calls 2% and 20
Bytes written, bytes read 1% and 4 KiB
Peak RSS 10% and 1 MiB
Wall time 50% and 20 ms

A commit that makes a workload slower on purpose names the workload in a Perf-regression: trailer. Glob patterns work:

Perf-regression: populate/* varlink/Populate-wide

bench/compare.py --accept FILE reads such names from FILE and reports those regressions without failing.

Continuous integration

GitHub Actions runs .github/workflows/ci.yml for every pull request, for every push to main and once a week. It runs:

  • the build and the tests with rustc 1.85 and with the current stable rustc, on x86_64 and aarch64, with warnings turned into errors,
  • the same with musl on x86_64,
  • the case folding table check, which generates the table from the Unicode 16.0.0 files,
  • clippy with rustc 1.85 and with the current stable rustc, for every supported architecture and for musl on x86_64,
  • rustfmt,
  • the build and the tests for the other architectures, under qemu except for i686,
  • the exhaustive sweeps,
  • the fuzzing campaign, with 5000 iterations in the weekly run,
  • the coverage report, which is shown in the job summary,
  • Kani with --only-codegen, which checks that every proof builds. The proofs themselves need more time and memory than the CI has,
  • a check that the crates from outside the tree are libc, lexopt, sha2 and serde_json, and the crates they need,
  • bench/compare.sh with the release binary and that of the base: the target branch for a pull request, main before a push, and main of a week earlier for the weekly run. It accepts the regressions that the Perf-regression: trailers of the commits since the base name,
  • a check that every commit of a pull request, and every commit a push adds, carries a Signed-off-by: line from its author. Merge commits are left out.

The build jobs fail when mkfs.fat, fsck.fat, mtools or a varlinkctl with --push-fd is missing, instead of skipping the tests that need them.

Dependabot proposes updates of the actions and of the crates once a month. It keeps libc on 0.2. It updates sha2 and the crates it needs in one pull request, and skips sha2 0.11.1 and later, which need rustc 1.88.

Submitting changes

Changes go in as pull requests on GitHub. Every commit carries a Signed-off-by: line from its author. git commit -s adds one. With it the author certifies the Developer Certificate of Origin in the file DCO. Merge commits don't need one.