<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tim</title>
    <description>The latest articles on DEV Community by Tim (@vars1ty).</description>
    <link>https://dev.to/vars1ty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3959339%2Ffa6ea1d7-5e3a-4796-8bb4-f99bfab2575e.jpeg</url>
      <title>DEV Community: Tim</title>
      <link>https://dev.to/vars1ty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vars1ty"/>
    <language>en</language>
    <item>
      <title>Abusing c_variadic in Rust</title>
      <dc:creator>Tim</dc:creator>
      <pubDate>Sat, 30 May 2026 03:47:47 +0000</pubDate>
      <link>https://dev.to/vars1ty/abusing-cvariadic-in-rust-4hjp</link>
      <guid>https://dev.to/vars1ty/abusing-cvariadic-in-rust-4hjp</guid>
      <description>&lt;h2&gt;
  
  
  What is &lt;code&gt;c_variadic&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;c_variadic&lt;/code&gt; is a recently stabilized feature in Rust that lets you send variadics through C/C++ FFI interop.&lt;/p&gt;

&lt;p&gt;The best example is with &lt;code&gt;int printf(const char* format, ...)&lt;/code&gt;: The &lt;code&gt;...&lt;/code&gt; are your variadics and allows you to pass any number of parameters.&lt;/p&gt;

&lt;p&gt;You can utilize it like this in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v_params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="s"&gt;"My magic number is: %d"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;1234&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Backstory
&lt;/h2&gt;

&lt;p&gt;When I was developing a cheat for a video game I eventually grew sick of users requesting new features; I'm not a machine - I chose to implement the Rune scripting language into it.&lt;/p&gt;

&lt;p&gt;It worked great at the start, did its job and it opened up the potential for custom community-driven sub-cheat menus.&lt;/p&gt;

&lt;p&gt;Although as the project itself shifted to being a cheat framework, this didn't meet my own demands, which were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Injecting into unprotected games via &lt;code&gt;LoadLibraryA&lt;/code&gt; - ✅&lt;/li&gt;
&lt;li&gt;Hooking ImGui based on the game renderer (using hudhook, great project) - ✅&lt;/li&gt;
&lt;li&gt;Adding WinAPI Memory R/W functions, alongside with pattern scanning and such - ✅&lt;/li&gt;
&lt;li&gt;Adding runtime ImGui UI building into Rune scripting - ✅&lt;/li&gt;
&lt;li&gt;Adding function hooks and function pointer calling into Rune scripting - 💀&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yeah... 2 challenges from it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How do I add function calling?&lt;/li&gt;
&lt;li&gt;How do I hook functions and call back to Rune?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;I first tried to tackle the first problem, I could dynamically cast a Rune &lt;code&gt;Value&lt;/code&gt; into its native type by doing loads of type-checking and conversions.&lt;/p&gt;

&lt;p&gt;So I tried to just call a function with 1 parameter using it and casting the desired function pointer into a &lt;code&gt;extern "C" fn(p_0: *const std::ffi::c_void) -&amp;gt; *const std::ffi::c_void&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This worked and the return value was obtainable &amp;amp; sent back to the calling Rune function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-parameter functions
&lt;/h3&gt;

&lt;p&gt;Not every function is a single parameter. In fact, almost none are in a game.&lt;/p&gt;

&lt;p&gt;I can collect the parameters from a Rune function by having the user just pass them all into a large &lt;code&gt;Vec&amp;lt;Value&amp;gt;&lt;/code&gt; and then doing type-conversion; that part is solved.&lt;/p&gt;

&lt;p&gt;But how would I call functions with varying amount of parameters? I can't change the extern-syntax at runtime; Rust is typed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abusing c_variadic
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;c_variadic&lt;/code&gt; available, I first tried to just force a random function as &lt;code&gt;unsafe extern "C" fn(...) -&amp;gt; *const std::ffi::c_void&lt;/code&gt; - and prayed.&lt;/p&gt;

&lt;p&gt;The result? &lt;strong&gt;It worked&lt;/strong&gt; but with one caveat: It only works on &lt;strong&gt;64-bit&lt;/strong&gt; and that's mainly because &lt;strong&gt;32-bit&lt;/strong&gt; often use different calling-conventions, like &lt;code&gt;thiscall&lt;/code&gt;, &lt;code&gt;fastcall&lt;/code&gt; and whatnot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing it in Rune
&lt;/h3&gt;

&lt;p&gt;Rune obviously doesn't have variadics like that, and shouldn't.&lt;/p&gt;

&lt;p&gt;So... how should it be implemented?&lt;br&gt;
Simple: &lt;code&gt;match params_vec.len() { ... }&lt;/code&gt; - account for a &lt;em&gt;n&lt;/em&gt; amount of parameters.&lt;/p&gt;

&lt;p&gt;I opted for &lt;code&gt;15&lt;/code&gt; parameters, making a large match-statement and getting the next parameter from the vector and passing it in.&lt;/p&gt;

&lt;p&gt;The result? &lt;strong&gt;It worked&lt;/strong&gt; - but with the downside that we have absolutely no idea how many parameters a function pointer can actually take, so a user could pass in 10 into a function that only takes 2.&lt;/p&gt;

&lt;p&gt;The solution? Let them blow their foot off. &lt;/p&gt;
&lt;h2&gt;
  
  
  Function Hooks
&lt;/h2&gt;

&lt;p&gt;The first problem was solved, now this... The library I was using for hooking was &lt;code&gt;retour&lt;/code&gt; in case you were wondering btw.&lt;/p&gt;
&lt;h3&gt;
  
  
  Where do hooks go?
&lt;/h3&gt;

&lt;p&gt;This was probably the biggest problem of it all: &lt;strong&gt;Where do they get sent off to?&lt;/strong&gt; - Rune as mentioned, is a VM-based language and we can't just redirect it to a VM pointer.&lt;/p&gt;
&lt;h3&gt;
  
  
  The (ugly) solution
&lt;/h3&gt;

&lt;p&gt;I opted for creating a struct which held data, here's a 1:1 representation of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/// Holds the information about a Rune detour.&lt;/span&gt;
&lt;span class="nd"&gt;#[derive(Default)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;RDetour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// The ID of the detour.&lt;/span&gt;
    &lt;span class="n"&gt;detour_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// The pointer of which function will be treated as target, and will be redirected to a&lt;/span&gt;
    &lt;span class="cd"&gt;/// determined detour holder from `determine_detour_holder()`.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;from_ptr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;isize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// The `RawDetour` instance.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;detour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RawDetour&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// Rune function to be called as a callback, should return a `isize` of the original functions&lt;/span&gt;
    &lt;span class="cd"&gt;/// return value as a pointer, or a modified value if needed.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;rune_function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SyncFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// Optional paramater to be passed into `rune_function` upon callback.&lt;/span&gt;
    &lt;span class="cd"&gt;/// Can be a structure for example, so that variables can be updated.&lt;/span&gt;
    &lt;span class="n"&gt;opt_param&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ValueWrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice one special type: &lt;code&gt;ValueWrapper&lt;/code&gt; - and that's just a standard Rune &lt;code&gt;Value&lt;/code&gt; but with force-implemented &lt;code&gt;Send + Sync&lt;/code&gt;, and that's for one ugly reason: &lt;strong&gt;Every RDetour struct instance was stored globally&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It was behind an Atomic RefCell which helped a ton, but yeah not the code you'd typically wanna be writing.&lt;/p&gt;

&lt;p&gt;To actually have a real, native function that the hook can call back to, I opted for creating a macro that generated &lt;code&gt;35&lt;/code&gt; "detour holder" functions that all registered themselves linearly, which then created a new &lt;code&gt;RDetour&lt;/code&gt; struct instance and saved it globally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing new hooks from Rune
&lt;/h3&gt;

&lt;p&gt;To install new hooks from within Rune, I ran through these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the first-available detour out of the 35 total ones&lt;/li&gt;
&lt;li&gt;Switch out &lt;code&gt;rune_function&lt;/code&gt; to an user-defined Rune-level callback that the hook would call&lt;/li&gt;
&lt;li&gt;Switch out &lt;code&gt;from_ptr&lt;/code&gt; to what the user specified&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;opt_param&lt;/code&gt; if the user has defined a custom Rune &lt;code&gt;Value&lt;/code&gt; to access within &lt;code&gt;rune_function&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create the hook&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If a step failed, then it would log the error visually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Processing the incoming data
&lt;/h3&gt;

&lt;p&gt;Since we have native hook/detour holders, all data goes to native stub-like functions.&lt;/p&gt;

&lt;p&gt;Once data was received, the stub had generated code that would instruct it to look up the hook that was set on its ID.&lt;/p&gt;

&lt;p&gt;That helper function (&lt;code&gt;call_rune_function_on_id&lt;/code&gt;) would then get the Rune callback function and call it with these parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Trampoline pointer the user should call themselves to continue control-flow. Skip it and the function will just be a glorified &lt;code&gt;return;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A 10-param-sized tuple, the closest to a variadic in Rune. The parameters were - you guessed it - obtained by having every stub-function have a &lt;code&gt;c_vardiadic&lt;/code&gt; body&lt;/li&gt;
&lt;li&gt;The optional Rune &lt;code&gt;Value&lt;/code&gt; so the user can utilize it, if desired&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the user utilized the newly-added &lt;code&gt;fn_ptr_call&lt;/code&gt; function on the trampoline pointer and the right parameters, it would continue on as expected.&lt;/p&gt;

&lt;p&gt;The users were often instructed to see how many parameters the function &lt;em&gt;actually wanted&lt;/em&gt;, and not just blow off the whole hill with 10.&lt;/p&gt;

&lt;p&gt;They often didn't listen (neither did I, I'm lazy) - and it worked for 99% of all cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While Rust is a safe language by design, it very much is capable of the same things as C/C++ if you know what you are doing.&lt;/p&gt;

&lt;p&gt;Unsafe Rust shouldn't be frowned upon, it should be taught and studied, there's tons of neat tricks that you can utilize.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>ffi</category>
      <category>systems</category>
      <category>software</category>
    </item>
  </channel>
</rss>
