### Install cargo-afl Source: https://github.com/tokio-rs/prost/blob/master/FUZZING.md Install the necessary cargo-afl tool before running fuzz tests. ```bash cargo install -f afl ``` -------------------------------- ### Install build tools on MacOS Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Install automake and libtool on MacOS to resolve build errors related to missing autoreconf. ```ignore brew install automake brew install libtool ``` -------------------------------- ### Example .proto File Definition Source: https://github.com/tokio-rs/prost/blob/master/README.md Defines a Person message with nested PhoneNumber and PhoneType, and an AddressBook message. This serves as the input for Prost code generation. ```protobuf syntax = "proto3"; package tutorial; message Person { string name = 1; int32 id = 2; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; } // Our address book file is just one of these. message AddressBook { repeated Person people = 1; } ``` -------------------------------- ### Converting i32 to Rust Enum using TryFrom Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Example of converting an i32 to a Rust enum using `TryFrom`. ```rust let phone_type = 2i32; match PhoneType::try_from(phone_type) { Ok(PhoneType::Mobile) => ..., Ok(PhoneType::Home) => ..., Ok(PhoneType::Work) => ..., Err(_) => ..., } ``` -------------------------------- ### Protobuf Enum Definition Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Example of a protobuf enum definition. ```protobuf enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } ``` -------------------------------- ### Protobuf Oneof Definition Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Example of a protobuf message with a oneof field. ```protobuf message Foo { oneof widget { int32 quux = 1; string bar = 2; } } ``` -------------------------------- ### Tag Inference for Existing Rust Types Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Demonstrates how Prost infers tags for existing Rust structs. Fields are tagged sequentially starting from 1, with options to skip tags using the `tag` attribute. ```rust use prost; use prost::{Enumeration, Message}; #[derive(Clone, PartialEq, Message)] struct Person { #[prost(string, tag = "1")] pub id: String, // tag=1 // NOTE: Old "name" field has been removed // pub name: String, // tag=2 (Removed) #[prost(string, tag = "6")] pub given_name: String, // tag=6 #[prost(string)] pub family_name: String, // tag=7 #[prost(string)] pub formatted_name: String, // tag=8 #[prost(uint32, tag = "3")] pub age: u32, // tag=3 #[prost(uint32)] pub height: u32, // tag=4 #[prost(enumeration = "Gender")] pub gender: i32, // tag=5 // NOTE: Skip to less commonly occurring fields #[prost(string, tag = "16")] pub name_prefix: String, // tag=16 (eg. mr/mrs/ms) #[prost(string)] pub name_suffix: String, // tag=17 (eg. jr/esq) #[prost(string)] pub maiden_name: String, // tag=18 } #[derive(Clone, Copy, Debug, PartialEq, Eq, Enumeration)] pub enum Gender { Unknown = 0, Female = 1, Male = 2, } ``` -------------------------------- ### Protobuf Package to Rust Module Mapping Source: https://github.com/tokio-rs/prost/blob/master/README.md Prost translates the Protobuf 'package' specifier into a Rust module structure. For example, 'package foo.bar;' results in Rust types within the 'foo::bar' module. ```protobuf package foo.bar; ``` -------------------------------- ### Generated Rust Code for Person and AddressBook Source: https://github.com/tokio-rs/prost/blob/master/README.md The Rust code generated by Prost from the example .proto file. It includes structs for Person, PhoneNumber, and AddressBook, along with the PhoneType enum. ```rust #[derive(Clone, PartialEq, ::prost::Message)] pub struct Person { #[prost(string, tag="1")] pub name: ::prost::alloc::string::String, /// Unique ID number for this person. #[prost(int32, tag="2")] pub id: i32, #[prost(string, tag="3")] pub email: ::prost::alloc::string::String, #[prost(message, repeated, tag="4")] pub phones: ::prost::alloc::vec::Vec, } /// Nested message and enum types in `Person`. pub mod person { #[derive(Clone, PartialEq, ::prost::Message)] pub struct PhoneNumber { #[prost(string, tag="1")] pub number: ::prost::alloc::string::String, #[prost(enumeration="PhoneType", tag="2")] pub r#type: i32, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] #[repr(i32)] pub enum PhoneType { Mobile = 0, Home = 1, Work = 2, } } /// Our address book file is just one of these. #[derive(Clone, PartialEq, ::prost::Message)] pub struct AddressBook { #[prost(message, repeated, tag="1")] pub people: ::prost::alloc::vec::Vec, } ``` -------------------------------- ### Develop with Nix Flakes Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Enter a development shell with all project dependencies configured using Nix flakes. ```bash nix develop ``` -------------------------------- ### Build and run afl fuzz target Source: https://github.com/tokio-rs/prost/blob/master/FUZZING.md Navigate to the target directory, build the binary, and initiate the fuzzing process. ```bash cd fuzz/afl// cargo afl build --bin fuzz-target cargo afl fuzz -i in -o out target/debug/fuzz-target ``` -------------------------------- ### Develop with Minimum Rust Version using Nix Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Enter a development shell using the minimum supported Rust version as required by policy, ensuring compatibility testing. ```bash nix develop .#rust_minimum_version ``` -------------------------------- ### Generate proto3 binary test corpus Source: https://github.com/tokio-rs/prost/blob/master/fuzz/afl/proto3/README.md Uses the prost library to encode a TestAllTypesProto3 message into a binary file. Ensure the protobuf test messages are available in your project dependencies. ```rust use prost::Message; use protobuf::test_messages::proto3::TestAllTypesProto3; fn main() { let msg = TestAllTypesProto3 { optional_int32: 42, optional_fixed64: 9983748923, optional_bool: true, recursive_message: Some( Box::new(TestAllTypesProto3 { repeated_int32: vec![1, 2, 99, 50, -5], ..Default::default() }) ), repeated_sfixed32: vec![1, -1, 1, -1], repeated_float: vec![-1.0, 10.10, 1.337, std::f32::NAN], ..Default::default() }; let mut buf = vec![]; msg.encode(&mut buf).unwrap(); std::fs::write("proto3-default.bin", buf).unwrap(); } ``` -------------------------------- ### Prost no_std Configuration Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Configure Prost for no_std environments by disabling default features and using BTreeMaps for map fields. ```ignore [dependencies] prost = { version = "0.14.3", default-features = false, features = ["derive"] } # Only necessary if using Protobuf well-known types: prost-types = { version = "0.14.3", default-features = false } ``` ```rust let mut config = prost_build::Config::new(); config.btree_map(&["."]); ``` -------------------------------- ### Reproduce a crash Source: https://github.com/tokio-rs/prost/blob/master/FUZZING.md Use the reproduction binary to analyze specific crash files generated by the fuzzer. ```bash cd fuzz/afl// cargo build --bin reproduce cargo run --bin reproduce -- out/crashes/ ``` -------------------------------- ### Add Prost and Prost-Types to Cargo.toml Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Include prost and prost-types in your project's dependencies. Prost-types is only needed if you plan to use Protobuf's well-known types. ```toml [dependencies] prost = "0.14" # Only necessary if using Protobuf well-known types: prost-types = "0.14" ``` -------------------------------- ### Generated Rust Code from Protobuf Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md This Rust code is generated from a .proto file using Prost. It defines structs and enums corresponding to the Protobuf message definitions. ```rust #[derive(Clone, PartialEq, ::prost::Message)] pub struct Person { #[prost(string, tag="1")] pub name: ::prost::alloc::string::String, /// Unique ID number for this person. #[prost(int32, tag="2")] pub id: i32, #[prost(string, tag="3")] pub email: ::prost::alloc::string::String, #[prost(message, repeated, tag="4")] pub phones: ::prost::alloc::vec::Vec, } /// Nested message and enum types in `Person`. pub mod person { #[derive(Clone, PartialEq, ::prost::Message)] pub struct PhoneNumber { #[prost(string, tag="1")] pub number: ::prost::alloc::string::String, #[prost(enumeration="PhoneType", tag="2")] pub r#type: i32, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] #[repr(i32)] pub enum PhoneType { Mobile = 0, Home = 1, Work = 2, } } /// Our address book file is just one of these. #[derive(Clone, PartialEq, ::prost::Message)] pub struct AddressBook { #[prost(message, repeated, tag="1")] pub people: ::prost::alloc::vec::Vec, } ``` -------------------------------- ### Protobuf Message to Rust Struct Generation Source: https://github.com/tokio-rs/prost/blob/master/README.md A simple Protobuf message declaration is converted into a corresponding Rust struct. Prost applies derive attributes like Clone, Debug, PartialEq, and Message. ```protobuf // Sample message. message Foo { } ``` -------------------------------- ### Converting Rust Enum to i32 Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md How to convert a Rust enum value to an i32. ```rust PhoneType::Mobile as i32 ``` -------------------------------- ### Protobuf Message with Enum Field Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md A protobuf message containing an enum field. ```protobuf message PhoneNumber { string number = 1; PhoneType type = 2; } ``` -------------------------------- ### Generated Rust Enum Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md The corresponding Rust enum generated from a protobuf enum. ```rust pub enum PhoneType { Mobile = 0, Home = 1, Work = 2, } ``` -------------------------------- ### Generated Rust Struct for Oneof Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md The Rust struct and module generated from a protobuf message with a oneof field. ```rust pub struct Foo { pub widget: Option, } pub mod foo { pub enum Widget { Quux(i32), Bar(String), } } ``` -------------------------------- ### Generated Rust Struct for Protobuf Message Source: https://github.com/tokio-rs/prost/blob/master/README.md This Rust struct is generated by Prost from a Protobuf message definition. It includes derive attributes and a public struct field. ```rust /// Sample message. #[derive(Clone, Debug, PartialEq, Message)] pub struct Foo { } ``` -------------------------------- ### Generated Rust Struct with Enum Accessors Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md The Rust struct generated from a protobuf message with an enum field, including accessor methods. ```rust pub struct PhoneNumber { pub number: String, pub r#type: i32, // the `r#` is needed because `type` is a Rust keyword } impl PhoneNumber { pub fn r#type(&self) -> PhoneType { ... } pub fn set_type(&mut self, value: PhoneType) { ... } } ``` -------------------------------- ### Generated Enum Associated Functions Source: https://github.com/tokio-rs/prost/blob/master/prost/README.md Associated functions added to Rust enums by the `prost::Enumeration` derive macro. ```rust impl PhoneType { pub const fn is_valid(value: i32) -> bool { ... } #[deprecated] pub fn from_i32(value: i32) -> Option { ... } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.