Files
phy/bytecheck/index.html
Orion Kindel 0ce894e6b0 doc
2025-03-18 10:30:23 -05:00

112 lines
14 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="bytecheck"><title>bytecheck - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="bytecheck" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../bytecheck/index.html">bytecheck</a><span class="version">0.8.0</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#bytecheck" title="bytecheck">bytecheck</a><ul><li><a href="#design" title="Design">Design</a></li><li><a href="#layout-stability" title="Layout stability">Layout stability</a></li><li><a href="#features" title="Features">Features</a></li><li><a href="#example" title="Example">Example</a></li></ul></li></ul><h3><a href="#reexports">Crate Items</a></h3><ul class="block"><li><a href="#reexports" title="Re-exports">Re-exports</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li><li><a href="#derives" title="Derive Macros">Derive Macros</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>bytecheck</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/bytecheck/lib.rs.html#1-1789">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="bytecheck"><a class="doc-anchor" href="#bytecheck">§</a>bytecheck</h2>
<p>bytecheck is a memory validation framework for Rust.</p>
<p>For some types, creating an invalid value immediately results in undefined
behavior. This can cause some issues when trying to validate potentially
invalid bytes, as just casting the bytes to your type can technically cause
errors. This makes it difficult to write validation routines, because until
youre certain that the bytes represent valid values you cannot cast them.</p>
<p>bytecheck provides a framework for performing these byte-level validations
and implements checks for basic types along with a derive macro to implement
validation for custom structs and enums.</p>
<h3 id="design"><a class="doc-anchor" href="#design">§</a>Design</h3>
<p><a href="trait.CheckBytes.html" title="trait bytecheck::CheckBytes"><code>CheckBytes</code></a> is at the heart of bytecheck, and does the heavy lifting of
verifying that some bytes represent a valid type. Implementing it can be
done manually or automatically with the <a href="derive.CheckBytes.html" title="derive bytecheck::CheckBytes">derive macro</a>.</p>
<h3 id="layout-stability"><a class="doc-anchor" href="#layout-stability">§</a>Layout stability</h3>
<p>The layouts of types may change between compiler versions, or even different
compilations. To guarantee stable type layout between compilations, structs,
enums, and unions can be annotated with <code>#[repr(C)]</code>, and enums specifically
can be annotated with <code>#[repr(int)]</code> or <code>#[repr(C, int)]</code> as well. See
<a href="https://doc.rust-lang.org/reference/type-layout.html">the references page on type layout</a> for more details.</p>
<h3 id="features"><a class="doc-anchor" href="#features">§</a>Features</h3>
<ul>
<li><code>derive</code>: Re-exports the macros from <code>bytecheck_derive</code>. Enabled by
default.</li>
<li><code>simdutf8</code>: Uses the <code>simdutf8</code> crate to validate UTF-8 strings. Enabled
by default.</li>
</ul>
<h4 id="crates"><a class="doc-anchor" href="#crates">§</a>Crates</h4>
<p>Bytecheck provides integrations for some common crates by default. In the
future, crates should depend on bytecheck and provide their own integration.</p>
<ul>
<li><a href="https://docs.rs/uuid/1"><code>uuid-1</code></a></li>
</ul>
<h3 id="example"><a class="doc-anchor" href="#example">§</a>Example</h3>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>bytecheck::{CheckBytes, check_bytes, rancor::Failure};
<span class="attr">#[derive(CheckBytes, Debug)]
#[repr(C)]
</span><span class="kw">struct </span>Test {
a: u32,
b: char,
c: bool,
}
<span class="attr">#[repr(C, align(<span class="number">4</span>))]
</span><span class="kw">struct </span>Aligned&lt;<span class="kw">const </span>N: usize&gt;([u8; N]);
<span class="macro">macro_rules!</span> bytes {
($(<span class="macro-nonterminal">$byte</span>:literal,)<span class="kw-2">*</span>) =&gt; {
(<span class="kw-2">&amp;</span>Aligned([$(<span class="macro-nonterminal">$byte</span>,)<span class="kw-2">*</span>]).<span class="number">0 </span><span class="kw">as </span><span class="kw-2">&amp;</span>[u8]).as_ptr()
};
($(<span class="macro-nonterminal">$byte</span>:literal),<span class="kw-2">*</span>) =&gt; {
<span class="macro">bytes!</span>($(<span class="macro-nonterminal">$byte</span>,)<span class="kw-2">*</span>)
};
}
<span class="comment">// In this example, the architecture is assumed to be little-endian
</span><span class="attr">#[cfg(target_endian = <span class="string">"little"</span>)]
</span><span class="kw">unsafe </span>{
<span class="comment">// These are valid bytes for a `Test`
</span>check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0x78u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">1u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap();
<span class="comment">// Changing the bytes for the u32 is OK, any bytes are a valid u32
</span>check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">42u8</span>, <span class="number">16u8</span>, <span class="number">20u8</span>, <span class="number">3u8</span>,
<span class="number">0x78u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">1u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap();
<span class="comment">// Characters outside the valid ranges are invalid
</span>check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0x00u8</span>, <span class="number">0xd8u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">1u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap_err();
check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0x00u8</span>, <span class="number">0x00u8</span>, <span class="number">0x11u8</span>, <span class="number">0u8</span>,
<span class="number">1u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap_err();
<span class="comment">// 0 is a valid boolean value (false) but 2 is not
</span>check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0x78u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap();
check_bytes::&lt;Test, Failure&gt;(
<span class="macro">bytes!</span>[
<span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">0x78u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>, <span class="number">0u8</span>,
<span class="number">2u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>, <span class="number">255u8</span>,
].cast()
).unwrap_err();
}</code></pre></div>
</div></details><h2 id="reexports" class="section-header">Re-exports<a href="#reexports" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name" id="reexport.rancor"><code>pub use <a class="mod" href="../rancor/index.html" title="mod rancor">rancor</a>;</code></div></li></ul><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.InvalidEnumDiscriminantError.html" title="struct bytecheck::InvalidEnumDiscriminantError">Invalid<wbr>Enum<wbr>Discriminant<wbr>Error</a></div><div class="desc docblock-short">An error resulting from an invalid enum tag.</div></li><li><div class="item-name"><a class="struct" href="struct.NamedEnumVariantCheckContext.html" title="struct bytecheck::NamedEnumVariantCheckContext">Named<wbr>Enum<wbr>Variant<wbr>Check<wbr>Context</a></div><div class="desc docblock-short">Context for errors resulting from checking enum variants with named fields.</div></li><li><div class="item-name"><a class="struct" href="struct.StructCheckContext.html" title="struct bytecheck::StructCheckContext">Struct<wbr>Check<wbr>Context</a></div><div class="desc docblock-short">Context for errors resulting from invalid structs.</div></li><li><div class="item-name"><a class="struct" href="struct.TupleStructCheckContext.html" title="struct bytecheck::TupleStructCheckContext">Tuple<wbr>Struct<wbr>Check<wbr>Context</a></div><div class="desc docblock-short">Context for errors resulting from invalid tuple structs.</div></li><li><div class="item-name"><a class="struct" href="struct.UnnamedEnumVariantCheckContext.html" title="struct bytecheck::UnnamedEnumVariantCheckContext">Unnamed<wbr>Enum<wbr>Variant<wbr>Check<wbr>Context</a></div><div class="desc docblock-short">Context for errors resulting from checking enum variants with unnamed
fields.</div></li></ul><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.CheckBytes.html" title="trait bytecheck::CheckBytes">Check<wbr>Bytes</a></div><div class="desc docblock-short">A type that can check whether a pointer points to a valid value.</div></li><li><div class="item-name"><a class="trait" href="trait.Verify.html" title="trait bytecheck::Verify">Verify</a></div><div class="desc docblock-short">A type that can check whether its invariants are upheld.</div></li></ul><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="fn" href="fn.check_bytes.html" title="fn bytecheck::check_bytes">check_<wbr>bytes</a><sup title="unsafe function"></sup></div><div class="desc docblock-short">Checks whether the given pointer points to a valid value.</div></li><li><div class="item-name"><a class="fn" href="fn.check_bytes_with_context.html" title="fn bytecheck::check_bytes_with_context">check_<wbr>bytes_<wbr>with_<wbr>context</a><sup title="unsafe function"></sup></div><div class="desc docblock-short">Checks whether the given pointer points to a valid value within the given
context.</div></li></ul><h2 id="derives" class="section-header">Derive Macros<a href="#derives" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="derive" href="derive.CheckBytes.html" title="derive bytecheck::CheckBytes">Check<wbr>Bytes</a></div><div class="desc docblock-short">Derives <code>CheckBytes</code> for the labeled type.</div></li></ul></section></div></main></body></html>