diff options
author | johannst <johannst@users.noreply.github.com> | 2024-02-28 18:32:44 +0000 |
---|---|---|
committer | johannst <johannst@users.noreply.github.com> | 2024-02-28 18:32:44 +0000 |
commit | 130746bc856f5c2eb5672cceb0e1304ee2c95b1e (patch) | |
tree | 2699369515d78aefb1fc584b56d3b69864e5357d /src/juicebox_asm/rt.rs.html | |
parent | 8c21a3da4a334d551f69a5e320a5ba4cd7dec6f4 (diff) | |
download | juicebox-asm-130746bc856f5c2eb5672cceb0e1304ee2c95b1e.tar.gz juicebox-asm-130746bc856f5c2eb5672cceb0e1304ee2c95b1e.zip |
deploy: 7cc72737a0140f5f71e9d83d4f87503eb4c7604f
Diffstat (limited to 'src/juicebox_asm/rt.rs.html')
-rw-r--r-- | src/juicebox_asm/rt.rs.html | 155 |
1 files changed, 136 insertions, 19 deletions
diff --git a/src/juicebox_asm/rt.rs.html b/src/juicebox_asm/rt.rs.html index efc55d3..d58d3e7 100644 --- a/src/juicebox_asm/rt.rs.html +++ b/src/juicebox_asm/rt.rs.html @@ -1,4 +1,5 @@ -<!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="Source of the Rust file `src/rt.rs`."><title>rt.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-fa3bb1812debf86c.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="juicebox_asm" data-themes="" data-resource-suffix="" data-rustdoc-version="1.74.0 (79e9716c9 2023-11-13)" data-channel="1.74.0" data-search-js="search-8be46b629f5f14a8.js" data-settings-js="settings-74424d7eec62a23e.js" ><script src="../../static.files/storage-fec3eaa3851e447d.js"></script><script defer src="../../static.files/src-script-3280b574d94e47b4.js"></script><script defer src="../../src-files.js"></script><script defer src="../../static.files/main-c5bd66d33317d69f.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-5d8b3c7633ad77ba.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc src"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><nav class="sub"><a class="sub-logo-container" href="../../juicebox_asm/index.html"><img class="rust-logo" src="../../static.files/rust-logo-151179464ae7ed46.svg" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><div data-nosnippet><pre class="src-line-numbers"><a href="#1" id="1">1</a> +<!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="Source of the Rust file `src/rt.rs`."><title>rt.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="juicebox_asm" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../../static.files/src-script-39ed315d46fb705f.js"></script><script defer src="../../src-files.js"></script><script defer src="../../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc src"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><div class="sidebar-resizer"></div> + <main><nav class="sub"><form class="search-form"><span></span><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><div data-nosnippet><pre class="src-line-numbers"><a href="#1" id="1">1</a> <a href="#2" id="2">2</a> <a href="#3" id="3">3</a> <a href="#4" id="4">4</a> @@ -200,21 +201,125 @@ <a href="#200" id="200">200</a> <a href="#201" id="201">201</a> <a href="#202" id="202">202</a> +<a href="#203" id="203">203</a> +<a href="#204" id="204">204</a> +<a href="#205" id="205">205</a> +<a href="#206" id="206">206</a> +<a href="#207" id="207">207</a> +<a href="#208" id="208">208</a> +<a href="#209" id="209">209</a> +<a href="#210" id="210">210</a> +<a href="#211" id="211">211</a> +<a href="#212" id="212">212</a> +<a href="#213" id="213">213</a> +<a href="#214" id="214">214</a> +<a href="#215" id="215">215</a> +<a href="#216" id="216">216</a> +<a href="#217" id="217">217</a> +<a href="#218" id="218">218</a> +<a href="#219" id="219">219</a> +<a href="#220" id="220">220</a> +<a href="#221" id="221">221</a> +<a href="#222" id="222">222</a> +<a href="#223" id="223">223</a> +<a href="#224" id="224">224</a> +<a href="#225" id="225">225</a> +<a href="#226" id="226">226</a> +<a href="#227" id="227">227</a> +<a href="#228" id="228">228</a> +<a href="#229" id="229">229</a> +<a href="#230" id="230">230</a> +<a href="#231" id="231">231</a> +<a href="#232" id="232">232</a> +<a href="#233" id="233">233</a> +<a href="#234" id="234">234</a> +<a href="#235" id="235">235</a> +<a href="#236" id="236">236</a> +<a href="#237" id="237">237</a> +<a href="#238" id="238">238</a> +<a href="#239" id="239">239</a> +<a href="#240" id="240">240</a> +<a href="#241" id="241">241</a> +<a href="#242" id="242">242</a> +<a href="#243" id="243">243</a> +<a href="#244" id="244">244</a> +<a href="#245" id="245">245</a> +<a href="#246" id="246">246</a> +<a href="#247" id="247">247</a> +<a href="#248" id="248">248</a> +<a href="#249" id="249">249</a> +<a href="#250" id="250">250</a> +<a href="#251" id="251">251</a> +<a href="#252" id="252">252</a> +<a href="#253" id="253">253</a> +<a href="#254" id="254">254</a> +<a href="#255" id="255">255</a> +<a href="#256" id="256">256</a> +<a href="#257" id="257">257</a> +<a href="#258" id="258">258</a> +<a href="#259" id="259">259</a> +<a href="#260" id="260">260</a> </pre></div><pre class="rust"><code><span class="doccomment">//! Simple `mmap`ed runtime. //! //! This runtime supports adding code to executable pages and turn the added code into user //! specified function pointer. -</span><span class="kw">use </span>nix::sys::mman::{mmap, mprotect, munmap, MapFlags, ProtFlags}; +</span><span class="attr">#[cfg(not(target_os = <span class="string">"linux"</span>))] +</span><span class="macro">compile_error!</span>(<span class="string">"This runtime is only supported on linux"</span>); -<span class="attr">#[cfg(not(target_os = <span class="string">"linux"</span>))] -</span><span class="macro">compile_error!</span>(<span class="string">"This runtime is only supported on linux"</span>); +<span class="kw">use </span>nix::sys::mman::{mmap, mprotect, munmap, MapFlags, ProtFlags}; + +<span class="kw">mod </span>perf { + <span class="kw">use </span>std::fs; + <span class="kw">use </span>std::io::Write; + + <span class="doccomment">/// Provide support for the simple [perf jit interface][perf-jit]. + /// + /// This allows a simple (static) jit runtime to generate meta data describing the generated + /// functions, which is used during post-processing by `perf report` to symbolize addresses + /// captured while executing jitted code. + /// + /// By the nature of this format, this can not be used for dynamic jit runtimes, which reuses + /// memory which previously contained jitted code. + /// + /// [perf-jit]: https://elixir.bootlin.com/linux/v6.6.6/source/tools/perf/Documentation/jit-interface.txt + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">struct </span>PerfMap { + file: std::fs::File, + } + + <span class="kw">impl </span>PerfMap { + <span class="doccomment">/// Create an empty perf map file. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new() -> <span class="self">Self </span>{ + <span class="kw">let </span>name = <span class="macro">format!</span>(<span class="string">"/tmp/perf-{}.map"</span>, nix::unistd::getpid()); + <span class="kw">let </span>file = fs::OpenOptions::new() + .truncate(<span class="bool-val">true</span>) + .create(<span class="bool-val">true</span>) + .write(<span class="bool-val">true</span>) + .open(<span class="kw-2">&</span>name) + .unwrap_or_else(|<span class="kw">_</span>| <span class="macro">panic!</span>(<span class="string">"Failed to open perf map file {}"</span>, <span class="kw-2">&</span>name)); + + PerfMap { file } + } + + <span class="doccomment">/// Add an entry to the perf map file. + </span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>add_entry(<span class="kw-2">&mut </span><span class="self">self</span>, start: usize, len: usize) { + <span class="comment">// Each line has the following format, fields separated with spaces: + // START SIZE NAME + // + // START and SIZE are hex numbers without 0x. + // NAME is the rest of the line, so it could contain special characters. + </span><span class="macro">writeln!</span>(<span class="self">self</span>.file, <span class="string">"{:x} {:x} jitfn_{:x}"</span>, start, len, start) + .expect(<span class="string">"Failed to write PerfMap entry"</span>); + } + } +} <span class="doccomment">/// A simple `mmap`ed runtime with executable pages. </span><span class="kw">pub struct </span>Runtime { buf: <span class="kw-2">*mut </span>u8, len: usize, idx: usize, + perf: <span class="prelude-ty">Option</span><perf::PerfMap>, } <span class="kw">impl </span>Runtime { @@ -225,7 +330,7 @@ /// Panics if the `mmap` call fails. </span><span class="kw">pub fn </span>new() -> Runtime { <span class="comment">// Allocate a single page. - </span><span class="kw">let </span>len = core::num::NonZeroUsize::new(<span class="number">4096</span>).expect(<span class="string">"Value is non zero"</span>); + </span><span class="kw">let </span>len = core::num::NonZeroUsize::new(<span class="number">4096</span>).expect(<span class="string">"Value is non zero"</span>); <span class="kw">let </span>buf = <span class="kw">unsafe </span>{ mmap( <span class="prelude-val">None</span>, @@ -235,16 +340,23 @@ <span class="number">0</span>, <span class="comment">/* fd */ </span><span class="number">0</span>, <span class="comment">/* off */ </span>) - .expect(<span class="string">"Failed to mmap runtime code page"</span>) <span class="kw">as </span><span class="kw-2">*mut </span>u8 + .expect(<span class="string">"Failed to mmap runtime code page"</span>) <span class="kw">as </span><span class="kw-2">*mut </span>u8 }; Runtime { buf, len: len.get(), idx: <span class="number">0</span>, + perf: <span class="prelude-val">None</span>, } } + <span class="kw">pub fn </span>with_profile() -> Runtime { + <span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); + rt.perf = <span class="prelude-val">Some</span>(perf::PerfMap::new()); + rt + } + <span class="doccomment">/// Add the block of `code` to the runtime and a get function pointer of type `F`. /// /// # Panics @@ -262,21 +374,21 @@ /// let mut rt = juicebox_asm::Runtime::new(); /// /// let code = [ 0x90 /* nop */, 0xc3 /* ret */ ]; - /// let nop = unsafe { rt.add_code::<extern "C" fn()>(&code) }; + /// let nop = unsafe { rt.add_code::<extern "C" fn()>(&code) }; /// /// nop(); /// ``` </span><span class="kw">pub unsafe fn </span>add_code<F>(<span class="kw-2">&mut </span><span class="self">self</span>, code: <span class="kw">impl </span>AsRef<[u8]>) -> F { <span class="comment">// Get pointer to start of next free byte. - </span><span class="macro">assert!</span>(<span class="self">self</span>.idx < <span class="self">self</span>.len, <span class="string">"Runtime code page full"</span>); + </span><span class="macro">assert!</span>(<span class="self">self</span>.idx < <span class="self">self</span>.len, <span class="string">"Runtime code page full"</span>); <span class="kw">let </span>fn_start = <span class="self">self</span>.buf.add(<span class="self">self</span>.idx); <span class="comment">// Copy over code. </span><span class="kw">let </span>code = code.as_ref(); - <span class="macro">assert!</span>(!code.is_empty(), <span class="string">"Adding empty code not supported"</span>); + <span class="macro">assert!</span>(!code.is_empty(), <span class="string">"Adding empty code not supported"</span>); <span class="macro">assert!</span>( code.len() <= (<span class="self">self</span>.len - <span class="self">self</span>.idx), - <span class="string">"Code does not fit on the runtime code page" + <span class="string">"Code does not fit on the runtime code page" </span>); <span class="self">self</span>.unprotect(); <span class="kw">unsafe </span>{ std::ptr::copy_nonoverlapping(code.as_ptr(), fn_start, code.len()) }; @@ -285,6 +397,11 @@ <span class="comment">// Increment index to next free byte. </span><span class="self">self</span>.idx += code.len(); + <span class="comment">// Add perf map entry. + </span><span class="kw">if let </span><span class="prelude-val">Some</span>(map) = <span class="kw-2">&mut </span><span class="self">self</span>.perf { + map.add_entry(fn_start <span class="kw">as </span>usize, code.len()); + } + <span class="comment">// Return function to newly added code. </span><span class="kw">unsafe </span>{ <span class="self">Self</span>::as_fn::<F>(fn_start) } } @@ -304,7 +421,7 @@ </span><span class="kw">pub fn </span>dump(<span class="kw-2">&</span><span class="self">self</span>) { <span class="macro">assert!</span>(<span class="self">self</span>.idx <= <span class="self">self</span>.len); <span class="kw">let </span>code = <span class="kw">unsafe </span>{ core::slice::from_raw_parts(<span class="self">self</span>.buf, <span class="self">self</span>.idx) }; - std::fs::write(<span class="string">"jit.asm"</span>, code).expect(<span class="string">"Failed to write file"</span>); + std::fs::write(<span class="string">"jit.asm"</span>, code).expect(<span class="string">"Failed to write file"</span>); } <span class="doccomment">/// Reinterpret the block of code pointed to by `fn_start` as `F`. @@ -326,7 +443,7 @@ <span class="self">self</span>.len, ProtFlags::PROT_READ | ProtFlags::PROT_EXEC, ) - .expect(<span class="string">"Failed to RX mprotect runtime code page"</span>); + .expect(<span class="string">"Failed to RX mprotect runtime code page"</span>); } } @@ -339,7 +456,7 @@ <span class="kw">unsafe </span>{ <span class="comment">// Add write permissions to code page. </span>mprotect(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len, ProtFlags::PROT_WRITE) - .expect(<span class="string">"Failed to W mprotect runtime code page"</span>); + .expect(<span class="string">"Failed to W mprotect runtime code page"</span>); } } } @@ -349,7 +466,7 @@ /// [`Runtime::add_code`]. </span><span class="kw">fn </span>drop(<span class="kw-2">&mut </span><span class="self">self</span>) { <span class="kw">unsafe </span>{ - munmap(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len).expect(<span class="string">"Failed to munmap runtime"</span>); + munmap(<span class="self">self</span>.buf.cast(), <span class="self">self</span>.len).expect(<span class="string">"Failed to munmap runtime"</span>); } } } @@ -363,7 +480,7 @@ <span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); <span class="kw">let </span>code = [<span class="number">0u8</span>; <span class="number">4096</span>]; <span class="kw">unsafe </span>{ - rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); + rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); } } @@ -373,7 +490,7 @@ <span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); <span class="kw">let </span>code = [<span class="number">0u8</span>; <span class="number">4097</span>]; <span class="kw">unsafe </span>{ - rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); + rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); } } @@ -383,12 +500,12 @@ <span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); <span class="kw">let </span>code = [<span class="number">0u8</span>; <span class="number">4096</span>]; <span class="kw">unsafe </span>{ - rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); + rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); } <span class="kw">let </span>code = [<span class="number">0u8</span>; <span class="number">1</span>]; <span class="kw">unsafe </span>{ - rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); + rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); } } @@ -398,7 +515,7 @@ <span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new(); <span class="kw">let </span>code = [<span class="number">0u8</span>; <span class="number">0</span>]; <span class="kw">unsafe </span>{ - rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); + rt.add_code::<<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>()>(code); } } } |