title: How do I execute raw bytes of machine shellcode compatible with LLVM frontend executables
body: <p>I'm trying to execute some x86 machine code that execute <code>jmp $</code> (<code>\xeb\xfe</code>) directly from Rust but the executable just crashes.</p>
  
  <p>I successfully did it in C with the GCC compiler but it also crashes with Clang:</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  
  char code[] = "\xeb\xfe";
  
  int main(int argc, char **argv){
      int (*func)();
      func = (int (*)()) code;
      (int)(*func)();
  }
  </code></pre>
  
  <p>To do it in pure Rust, I converted my byte array into a void pointer (<code>*const ()</code>) then converted it into a <code>unsafe extern "C" fn () -&gt; !</code> with <code>std::mem::transmute</code>:</p>
  
  <pre><code>static shellcode: [u8; 73] = *b"\xeb\xfe";
  fn main() -&gt; std::io::Result&lt;()&gt; {
      let raw: unsafe extern "C" fn() -&gt; ! =
          unsafe { std::mem::transmute(&amp;shellcode.as_ptr() as *const _ as *const ()) };
      unsafe { raw() };
      return Ok(());
  }
  </code></pre>
  
  <p>I already read <a href="https://stackoverflow.com/questions/55856247/how-to-execute-raw-instructions-from-a-memory-buffer-in-rust">How to execute raw instructions from a memory buffer in Rust?</a> and the answer is basically what I did so I'm kind of confused...</p>
  
  <p>I have compiled the Rust code for x86/x64 and both crashed, I think that the "shellcode" isn't compatible with LLVM.</p>