自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
模糊模組
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
libfuzzer-sys
crate 可支援 Rust 模糊測試,提供與 LLVM 的 libFuzzer 模糊測試引擎繫結的功能。詳情請參閱 libfuzzer-sys 存放區,以及 LLVM libFuzzer 專案頁面。
rust_fuzz
模組會產生模糊處理器二進位檔,並在執行時開始模糊處理 (類似 cc_fuzz
模組)。由於模糊測試器會運用 libFuzzer
模糊測試引擎,因此可以使用多個引數來控制模糊測試。這些項目在 libFuzzer 說明文件中列舉。
rust_fuzz
模組是 rust_binary
模組的擴充功能,因此會共用相同的屬性和考量事項。此外,它們實作許多與 cc_fuzz
模組相同的屬性和功能。
建構 rust_fuzz
模組時,系統會傳送 --cfg fuzzing
標記,可用於支援程式庫程式碼的條件編譯,以改善模糊測試。
編寫基本的 Rust 模糊測試工具
您可以使用以下程式碼,在 Android.bp
建構檔案中定義模糊測試模組:
rust_fuzz {
name: "example_rust_fuzzer",
srcs: ["fuzzer.rs"],
// Config for running the target on fuzzing infrastructure can be set under
// fuzz_config. This shares the same properties as cc_fuzz's fuzz_config.
fuzz_config: {
fuzz_on_haiku_device: true,
fuzz_on_haiku_host: false,
},
// Path to a corpus of sample inputs, optional. See https://llvm.org/docs/LibFuzzer.html#corpus
corpus: ["testdata/*"],
// Path to a dictionary of sample byte sequences, optional. See https://llvm.org/docs/LibFuzzer.html#dictionaries
dictionary: "example_rust_fuzzer.dict",
}
fuzzer.rs
檔案包含簡單的模糊測試器:
fn heap_oob() {
let xs = vec![0, 1, 2, 3];
let val = unsafe { *xs.as_ptr().offset(4) };
println!("Out-of-bounds heap value: {}", val);
}
fuzz_target!(|data: &[u8]| {
let magic_number = 327;
if data.len() == magic_number {
heap_oob();
}
});
這裡的 fuzz_target!(|data: &[u8]| { /* fuzz using data here */ });
會定義由 libFuzzer
引擎呼叫的模糊處理目標進入點。data
引數是 libFuzzer
引擎提供的位元組序列,可用於操縱輸入內容,以便對目標函式進行模糊測試。
在這個範例的模糊測試器中,系統只會檢查資料長度,以判斷是否要呼叫 heap_oob
函式,而呼叫此函式會導致讀取超出邊界。libFuzzer
是覆蓋率導向的模糊測試器,因此當它判斷前 326 B 的資料不會產生新的執行路徑時,就會快速收斂問題長度。
在樹狀結構中,找出這個範例:tools/security/fuzzing/example_rust_fuzzer/。如要查看較複雜的例子,請參閱legacy_blob_fuzzer,瞭解如何在樹狀結構中模糊處理 rustlib
依附元件。
如需如何編寫結構感知 Rust 模糊測試器的指南,請參閱 Rust Fuzz 書籍,這是 Rust Fuzz 專案的官方說明文件。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Fuzz modules\n\nRust fuzzing is supported through the `libfuzzer-sys` crate, which provides\nbindings to LLVM's libFuzzer fuzzing engine. For more information, see the [libfuzzer-sys](https://github.com/rust-fuzz/libfuzzer)\nrepository as well as the [LLVM libFuzzer project page](https://llvm.org/docs/LibFuzzer.html).\n\nThe `rust_fuzz` module produces a fuzzer binary which begins fuzzing when it's\nrun (similar to `cc_fuzz` modules). As the fuzzer leverages the `libFuzzer`\nfuzzing engine, it can take a number of arguments to control fuzzing. These are\nenumerated in the [libFuzzer documentation](https://llvm.org/docs/LibFuzzer.html#options).\n\n`rust_fuzz` modules are an extension of `rust_binary` modules, and as such share\nthe same properties and considerations. Additionally, they implement many of the\nsame properties and functionality as do the `cc_fuzz` modules.\n\nWhen building `rust_fuzz` modules, the `--cfg fuzzing` flag is emitted which can\nbe used to support conditional compilation of library code to improve fuzzing.\n\nWrite a basic Rust fuzzer\n-------------------------\n\nYou can define a fuzz module in an `Android.bp` build file with this code: \n\n rust_fuzz {\n name: \"example_rust_fuzzer\",\n srcs: [\"fuzzer.rs\"],\n\n // Config for running the target on fuzzing infrastructure can be set under\n // fuzz_config. This shares the same properties as cc_fuzz's fuzz_config.\n fuzz_config: {\n fuzz_on_haiku_device: true,\n fuzz_on_haiku_host: false,\n },\n\n // Path to a corpus of sample inputs, optional. See https://llvm.org/docs/LibFuzzer.html#corpus\n corpus: [\"testdata/*\"],\n\n // Path to a dictionary of sample byte sequences, optional. See https://llvm.org/docs/LibFuzzer.html#dictionaries\n dictionary: \"example_rust_fuzzer.dict\",\n }\n\nThe `fuzzer.rs` file contains a simple fuzzer: \n\n fn heap_oob() {\n let xs = vec![0, 1, 2, 3];\n let val = unsafe { *xs.as_ptr().offset(4) };\n println!(\"Out-of-bounds heap value: {}\", val);\n }\n\n fuzz_target!(|data: &[u8]| {\n let magic_number = 327;\n if data.len() == magic_number {\n heap_oob();\n }\n });\n\nHere `fuzz_target!(|data: &[u8]| { /* fuzz using data here */ });` defines the\nfuzz-target entry-point called by the `libFuzzer` engine. The `data` argument is\na sequence of bytes provided by the `libFuzzer` engine to be manipulated as input\nto fuzz the targeted function.\n\nIn this example fuzzer, only the length of the data gets checked to determine\nwhether to call the `heap_oob` function, the calling of which results in an\nout-of-bounds read. `libFuzzer` is a coverage-guided fuzzer, so it quickly converges on the\nproblematic length as it determines that the first 326 B of data don't\nresult in new execution paths.\n\nLocate this example, in-tree, at [tools/security/fuzzing/example_rust_fuzzer/](https://android.googlesource.com/platform/tools/security/+/669e5608d5bea0171b4888bed099725c4300b346/fuzzing/example_rust_fuzzer/fuzzer.rs).\nTo view a slightly more complex example of another fuzzer (which fuzzes a `rustlib`\ndependency) in-tree, see the [legacy_blob_fuzzer](https://android.googlesource.com/platform/system/security/+/2f503e597130e1d65dbf9fb4cd0fbd6a2f9ccb07/keystore2/src/fuzzers/legacy_blob_fuzzer.rs).\n\nFor guidance on [how to write structure-aware Rust fuzzers](https://rust-fuzz.github.io/book/cargo-fuzz/structure-aware-fuzzing.html),\nsee the [Rust Fuzz book](https://rust-fuzz.github.io/book/introduction.html), the official documentation for the Rust Fuzz project."]]