aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/add/add.rs.html8
-rw-r--r--src/fib/fib.rs.html8
-rw-r--r--src/juicebox_asm/rt.rs.html90
3 files changed, 84 insertions, 22 deletions
diff --git a/src/add/add.rs.html b/src/add/add.rs.html
index 580daa0..ba76c7d 100644
--- a/src/add/add.rs.html
+++ b/src/add/add.rs.html
@@ -36,8 +36,7 @@
<a href="#35" id="35">35</a>
<a href="#36" id="36">36</a>
<a href="#37" id="37">37</a>
-<a href="#38" id="38">38</a>
-<a href="#39" id="39">39</a></pre></div><pre class="rust"><code><span class="doccomment">//! Add example.
+<a href="#38" id="38">38</a></pre></div><pre class="rust"><code><span class="doccomment">//! Add example.
//!
//! Jit compile a function at runtime (generate native host code) which calls a function defined in
//! the example based on the SystemV abi to demonstrate the [`juicebox_asm`] crate.
@@ -69,9 +68,8 @@
<span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new();
<span class="kw">let </span>add42 = <span class="kw">unsafe </span>{ rt.add_code::&lt;<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(u32) -&gt; u32&gt;(asm.into_code()) };
- <span class="comment">// Write out JIT code for visualization.
- // Disassemble for example with `ndisasm -b 64 jit.asm`.
- </span>rt.dump();
+ <span class="comment">// Disassemble JIT code and write to stdout.
+ </span>rt.disasm();
<span class="kw">let </span>res = add42(<span class="number">5</span>);
<span class="macro">assert_eq!</span>(res, <span class="number">47</span>);
diff --git a/src/fib/fib.rs.html b/src/fib/fib.rs.html
index c1a746f..99da282 100644
--- a/src/fib/fib.rs.html
+++ b/src/fib/fib.rs.html
@@ -75,8 +75,7 @@
<a href="#74" id="74">74</a>
<a href="#75" id="75">75</a>
<a href="#76" id="76">76</a>
-<a href="#77" id="77">77</a>
-<a href="#78" id="78">78</a></pre></div><pre class="rust"><code><span class="doccomment">//! Fibonacci example.
+<a href="#77" id="77">77</a></pre></div><pre class="rust"><code><span class="doccomment">//! Fibonacci example.
//!
//! Jit compile a function at runtime (generate native host code) to compute the fibonacci sequence
//! to demonstrate the [`juicebox_asm`] crate.
@@ -144,9 +143,8 @@
</span><span class="kw">let </span><span class="kw-2">mut </span>rt = Runtime::new();
<span class="kw">let </span>fib = <span class="kw">unsafe </span>{ rt.add_code::&lt;<span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn</span>(u64) -&gt; u64&gt;(asm.into_code()) };
- <span class="comment">// Write out JIT code for visualization.
- // Disassemble for example with `ndisasm -b 64 jit.asm`.
- </span>rt.dump();
+ <span class="comment">// Disassemble JIT code and write to stdout.
+ </span>rt.disasm();
<span class="kw">for </span>n <span class="kw">in </span><span class="number">0</span>..<span class="number">15 </span>{
<span class="kw">let </span>fib_jit = fib(n);
diff --git a/src/juicebox_asm/rt.rs.html b/src/juicebox_asm/rt.rs.html
index b0fad10..d138a8e 100644
--- a/src/juicebox_asm/rt.rs.html
+++ b/src/juicebox_asm/rt.rs.html
@@ -266,7 +266,40 @@
<a href="#265" id="265">265</a>
<a href="#266" id="266">266</a>
<a href="#267" id="267">267</a>
-<a href="#268" id="268">268</a></pre></div><pre class="rust"><code><span class="doccomment">//! Simple `mmap`ed runtime.
+<a href="#268" id="268">268</a>
+<a href="#269" id="269">269</a>
+<a href="#270" id="270">270</a>
+<a href="#271" id="271">271</a>
+<a href="#272" id="272">272</a>
+<a href="#273" id="273">273</a>
+<a href="#274" id="274">274</a>
+<a href="#275" id="275">275</a>
+<a href="#276" id="276">276</a>
+<a href="#277" id="277">277</a>
+<a href="#278" id="278">278</a>
+<a href="#279" id="279">279</a>
+<a href="#280" id="280">280</a>
+<a href="#281" id="281">281</a>
+<a href="#282" id="282">282</a>
+<a href="#283" id="283">283</a>
+<a href="#284" id="284">284</a>
+<a href="#285" id="285">285</a>
+<a href="#286" id="286">286</a>
+<a href="#287" id="287">287</a>
+<a href="#288" id="288">288</a>
+<a href="#289" id="289">289</a>
+<a href="#290" id="290">290</a>
+<a href="#291" id="291">291</a>
+<a href="#292" id="292">292</a>
+<a href="#293" id="293">293</a>
+<a href="#294" id="294">294</a>
+<a href="#295" id="295">295</a>
+<a href="#296" id="296">296</a>
+<a href="#297" id="297">297</a>
+<a href="#298" id="298">298</a>
+<a href="#299" id="299">299</a>
+<a href="#300" id="300">300</a>
+<a href="#301" id="301">301</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.
@@ -424,22 +457,55 @@
</span><span class="kw">unsafe </span>{ <span class="self">Self</span>::as_fn::&lt;F&gt;(fn_start) }
}
- <span class="doccomment">/// Dump the code added so far to the runtime into a file called `jit.asm` in the processes
- /// current working directory.
- ///
- /// The code can be inspected with a disassembler as for example `ndiasm` from
- /// [nasm.us](https://nasm.us/index.php).
- /// ```sh
- /// ndisasm -b 64 jit.asm
- /// ```
+ <span class="doccomment">/// Disassemble the code currently added to the runtime, using
+ /// [`ndisasm`](https://nasm.us/index.php) and print it to _stdout_. If
+ /// `ndisasm` is not available on the system this prints a warning and
+ /// becomes a nop.
///
/// # Panics
///
- /// Panics if writing the file failed.
- </span><span class="kw">pub fn </span>dump(<span class="kw-2">&amp;</span><span class="self">self</span>) {
+ /// Panics if anything goes wrong with spawning, writing to or reading from
+ /// the `ndisasm` child process.
+ </span><span class="kw">pub fn </span>disasm(<span class="kw-2">&amp;</span><span class="self">self</span>) {
<span class="macro">assert!</span>(<span class="self">self</span>.idx &lt;= <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>);
+
+ <span class="comment">// Create ndisasm process, which expects input on stdin.
+ </span><span class="kw">let </span><span class="kw-2">mut </span>child = <span class="kw">match </span>std::process::Command::new(<span class="string">"ndisasm"</span>)
+ .args([<span class="string">"-b64"</span>, <span class="string">"-"</span>])
+ .stdin(std::process::Stdio::piped())
+ .stdout(std::process::Stdio::piped())
+ .spawn()
+ {
+ <span class="prelude-val">Ok</span>(child) =&gt; child,
+ <span class="prelude-val">Err</span>(err) <span class="kw">if </span>err.kind() == std::io::ErrorKind::NotFound =&gt; {
+ <span class="macro">println!</span>(<span class="string">"Runtime::disasm: ndisasm not found, skipping!"</span>);
+ <span class="kw">return</span>;
+ }
+ <span class="prelude-val">Err</span>(err) =&gt; {
+ <span class="macro">panic!</span>(<span class="string">"{:?}"</span>, err);
+ }
+ };
+
+ <span class="comment">// Write code to stdin of ndisasm.
+ </span><span class="kw">use </span>std::io::Write;
+ child
+ .stdin
+ .take()
+ .expect(<span class="string">"failed to take stdin"</span>)
+ .write_all(code)
+ .expect(<span class="string">"failed to write bytes to stdin"</span>);
+
+ <span class="comment">// Wait for output from ndisasm and print to stdout.
+ </span><span class="macro">println!</span>(
+ <span class="string">"{}"</span>,
+ String::from_utf8_lossy(
+ <span class="kw-2">&amp;</span>child
+ .wait_with_output()
+ .expect(<span class="string">"failed to get stdout"</span>)
+ .stdout
+ )
+ );
}
<span class="doccomment">/// Reinterpret the block of code pointed to by `fn_start` as `F`.