Skip to content

Add fuzz target for fdt_mut - #167

Open
vishruthasgowda wants to merge 3 commits into
google:mainfrom
vishruthasgowda:fuzz-fdt-mut
Open

vishruthasgowda wants to merge 3 commits into
google:mainfrom
vishruthasgowda:fuzz-fdt-mut

Conversation

@vishruthasgowda

Copy link
Copy Markdown
Contributor

Adds a fuzz target for fdt_mut.

The target starts from tests/dtb/test_props.dtb with some spare buffer space and applies an arbitrary sequence of operations (add_property, set_value, remove_property, remove_child, remove node, compact) on randomly chosen nodes. After every operation it checks that the buffer still parses as a valid FDT, and that each operation has its expected effect (e.g. adding a duplicate property fails, an added property is readable with the same value, remove_* returns true only if the item existed).

Ran for ~2 minutes (~200k executions) locally with no crashes.

Notes / possible follow-ups:

Fixes #166

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.32%. Comparing base (6db8e5d) to head (e7e28bc).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #167   +/-   ##
=======================================
  Coverage   95.32%   95.32%           
=======================================
  Files          27       27           
  Lines        4040     4040           
=======================================
  Hits         3851     3851           
  Misses        189      189           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@m4tx m4tx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it seems pretty good to me! I've left a few comments, PTAL.

Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
Comment thread fuzz/Cargo.toml Resolved
Comment thread fuzz/fuzz_targets/fdt_mut.rs Resolved
@vishruthasgowda

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've addressed all the comments:

  • Added AddNode
  • RemoveChild/RemoveProperty/SetValue now pick the n-th element by index
  • Split the name lists into PROPERTY_NAMES and NODE_NAMES
  • Node paths are now collected once and updated on add/remove
  • Now uses fuzz/corpus/parse/all.dtb
  • Moved the operation logic into Op::apply
  • Relaxed the arbitrary version to "1"
  • Ran clippy and rustfmt

PTAL.

@vishruthasgowda
vishruthasgowda requested a review from m4tx November 29, 2026 03:26

@m4tx m4tx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks much better now. A few comments before we merge this.

}

fn assert_valid(fdt: &FdtMut<&mut [u8]>) {
Fdt::new(fdt.as_read_only().data()).expect("FDT must stay valid after every operation");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's validate with Fdt::with_options(fdt.as_read_only().data(), FdtOptions::new().max_depth(usize::MAX)) - otherwise, subsequent AddNode operations could potentially exceed the default 64 depth limit.

forget_subtree(paths, &path);
}
}
Op::Compact => fdt.compact(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also assert here that the new size is smaller than the old one, and that another compact() call doesn't change the blob.

return;
};
assert!(n.remove_child(&name), "existing child must be removed");
assert!(n.child(&name).is_none());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can fail on the following chain of operations:

  1. AddNode adds "a"
  2. AddNode adds "a@0"
  3. RemoveNode with "a" removes the first node it finds with or without address, so the "a" node
  4. n.child("a") still returns the "a@0" node, and this check fails.

I've double checked and this is consistent with libfdt, so we want to keep this behaviour.

Let's change this check to something like assert!(!n.children().any(|c| c.name() == name)) to enforce equal-name check in case of a scenario described above.

let Some(n) = fdt.find_node_mut(&path) else {
return;
};
if n.remove().is_ok() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n.remove() should succeed on any non-root node (and we check for the "/" path above). Let's .expect() this here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fuzz tests for fdt_mut

2 participants