hao-runtime 0.0.10

Host the .net runtime (CoreCLR or Mono) in a rust process
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Rewrite a managed assembly's IL with hao before the runtime JITs it.
//!
//! The Godot/CoreCLR modding path: a native mod intercepts the game assembly's
//! load (see [`intercept_assembly`]) and hands CoreCLR a hao-rewritten copy. The
//! rewrite ([`patch_call`]) adds a `[DllImport]`-style P/Invoke to a function the
//! mod exports and calls it from a lifecycle method — so the method the engine
//! already runs each frame on the main thread now also calls back into Rust. No
//! injected assembly, no C#; pure LD_PRELOAD + hao.

use hao::{
    dotnet::{
        entries::{Entry, ImplMap, Method, ModuleRef},
        il::Instruction,
        md::streams::tables_stream::{MethodFlags, PInvokeMapFlags},
        signature::{MethodSig, TypeSig},
    },
    Module,
};

use crate::{Error, Result};

/// Rewrites `assembly` so `type_name::method_name` begins by calling the native
/// function `symbol`, exported from `library` (resolved as a `[DllImport]`).
/// Returns the patched assembly bytes.
///
/// The native function is bound as `static extern void symbol()`, so it takes no
/// arguments; it runs wherever the engine calls the patched method.
pub fn patch_call(
    assembly: &[u8],
    type_name: &str,
    method_name: &str,
    library: &str,
    symbol: &str,
) -> Result<Vec<u8>> {
    let mut module =
        Module::from_bytes(assembly).map_err(|e| Error::Other(format!("reading assembly: {e}")))?;

    let target = module
        .find_method(type_name, method_name)
        .ok_or_else(|| Error::NotFound(format!("{type_name}::{method_name}")))?;
    let declaring = target
        .get()
        .declaring_type
        .upgrade()
        .ok_or_else(|| Error::Other(format!("{type_name} has no declaring type")))?;

    // Add `static extern void symbol()`, forwarded to `library` via P/Invoke.
    let module_ref = Entry::new(ModuleRef {
        name: library.to_string(),
        ..Default::default()
    });
    module.module_refs.push(module_ref.clone());
    let mut pinvoke = Method::public_static(symbol, MethodSig::new_static(TypeSig::VOID, vec![]));
    pinvoke.flags |= MethodFlags::PinvokeImpl;
    pinvoke.impl_map = Some(ImplMap {
        flags: PInvokeMapFlags::CallConvCdecl | PInvokeMapFlags::NoMangle,
        import_name: symbol.to_string(),
        scope: module_ref,
    });
    let pinvoke = declaring.add_method(pinvoke);

    // Call it first thing in the target method.
    {
        let mut method = target.get_mut();
        let body = method
            .body
            .as_mut()
            .ok_or_else(|| Error::Other(format!("{type_name}::{method_name} has no body")))?;
        body.insert(0, [Instruction::call(pinvoke)]);
    }

    module
        .write()
        .map_err(|e| Error::Other(format!("writing assembly: {e}")))
}

#[cfg(all(unix, target_arch = "x86_64"))]
pub use intercept::{arm as arm_interception, intercept_assembly};

/// Hooks the file loader so that when the runtime opens a target assembly, it
/// gets a rewritten copy instead. The only platform-specific part of the
/// assembly-rewrite path.
#[cfg(all(unix, target_arch = "x86_64"))]
mod intercept {
    use std::{
        cell::Cell,
        ffi::{c_char, c_int, c_void, CStr, CString},
        sync::{
            atomic::{AtomicBool, Ordering},
            Mutex,
        },
    };

    const RTLD_DEFAULT: *mut c_void = std::ptr::null_mut();

    unsafe extern "C" {
        fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
        fn memfd_create(name: *const c_char, flags: c_int) -> c_int;
    }

    use crate::Result;

    type PatchFn = Box<dyn Fn(&[u8]) -> Result<Vec<u8>> + Send + Sync>;

    struct Target {
        /// The file name (not path) to match, e.g. `sts2.dll`.
        basename: String,
        patch: PatchFn,
        /// A `/proc/self/fd/{fd}` path to the rewritten copy, built on first open.
        patched: Mutex<Option<CString>>,
    }

    static TARGETS: Mutex<Vec<Target>> = Mutex::new(Vec::new());
    static HOOKED: AtomicBool = AtomicBool::new(false);

    thread_local! {
        // Set while we read/write the rewritten copy, so our own file I/O isn't
        // itself intercepted.
        static BUILDING: Cell<bool> = const { Cell::new(false) };
    }

    /// Serves a rewritten copy of `basename` (matched by file name) whenever the
    /// runtime opens it. `patch` receives the original bytes and returns the
    /// replacement (see [`patch_call`](super::patch_call)). Call it from a mod's
    /// entry point, before the runtime loads the assembly.
    ///
    /// The file-open hooks are NOT installed immediately — they are armed by
    /// [`arm`] (called automatically from `coreclr_init_detour` after
    /// `coreclr_initialize` returns). This avoids the open/openat hooks
    /// interfering with `dlopen` during early game startup.
    pub fn intercept_assembly(
        basename: &str,
        patch: impl Fn(&[u8]) -> Result<Vec<u8>> + Send + Sync + 'static,
    ) {
        TARGETS
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push(Target {
                basename: basename.to_string(),
                patch: Box::new(patch),
                patched: Mutex::new(None),
            });
    }

    /// Installs the open/openat hooks so registered targets are intercepted.
    /// Called automatically after `coreclr_initialize` returns — the CLR is
    /// loaded but managed assemblies haven't been opened yet.
    pub fn arm() {
        if TARGETS.lock().unwrap_or_else(|e| e.into_inner()).is_empty() {
            return;
        }
        install();
    }

    fn install() {
        if HOOKED.swap(true, Ordering::SeqCst) {
            return;
        }
        // Hook libc's file-open entry points. On many systems open==open64 and
        // openat==openat64 (64-bit offsets are the default), so deduplicate by
        // resolved address to avoid double-patching the same function.
        let mut hooked_addrs = Vec::new();
        hook_dedup(c"open", open_detour as *mut u8, &mut hooked_addrs);
        hook_dedup(c"open64", open_detour as *mut u8, &mut hooked_addrs);
        hook_dedup(c"openat", openat_detour as *mut u8, &mut hooked_addrs);
        hook_dedup(c"openat64", openat_detour as *mut u8, &mut hooked_addrs);
    }

    fn hook_dedup(name: &CStr, detour: *mut u8, seen: &mut Vec<usize>) {
        let addr = unsafe { dlsym(RTLD_DEFAULT, name.as_ptr()) } as usize;
        if addr == 0 {
            tracing::debug!("not hooking {name:?}: symbol not found");
            return;
        }
        if seen.contains(&addr) {
            tracing::debug!("{name:?} shares address with an already-hooked function, skipping");
            return;
        }
        seen.push(addr);
        match unsafe { hooking::Hook::by_name(None, name, detour) } {
            Ok(mut hook) => {
                if let Err(e) = unsafe { hook.apply_hook() } {
                    tracing::warn!("applying the {name:?} hook failed: {e:?}");
                }
                std::mem::forget(hook);
            }
            Err(e) => tracing::debug!("not hooking {name:?}: {e:?}"),
        }
    }

    /// If `path` names a target assembly, the path of its rewritten copy.
    fn redirect(path: *const c_char) -> Option<CString> {
        if path.is_null() || BUILDING.with(Cell::get) {
            return None;
        }
        let path = unsafe { CStr::from_ptr(path) }
            .to_string_lossy()
            .into_owned();
        let name = path.rsplit('/').next().unwrap_or(&path);
        // Lock scope: find target and get/build cached result, then drop lock
        // before any I/O (diag opens files → open_detour → redirect → deadlock
        // if the lock is still held).
        let result = {
            let targets = TARGETS.lock().unwrap_or_else(|e| e.into_inner());
            let target = targets.iter().find(|t| t.basename == name)?;
            let mut cache = target.patched.lock().unwrap_or_else(|e| e.into_inner());
            if cache.is_none() {
                *cache = build(&path, &target.patch);
            }
            cache.clone()
        };
        result
    }

    /// Reads the original assembly, rewrites it, and serves the copy from an
    /// anonymous in-memory file (`memfd_create`), returning a
    /// `/proc/self/fd/{fd}` path that the CLR can open.
    fn build(path: &str, patch: &PatchFn) -> Option<CString> {
        BUILDING.with(|b| b.set(true));
        let result = (|| {
            let original = std::fs::read(path).ok()?;
            BUILDING.with(|b| b.set(false));
            let patched = match patch(&original) {
                Ok(bytes) => bytes,
                Err(e) => {
                    tracing::error!("rewriting {path} failed: {e}");
                    return None;
                }
            };
            let fd = unsafe { memfd_create(c"hao-rewrite".as_ptr(), 0) };
            if fd < 0 {
                tracing::error!("memfd_create failed for {path}");
                return None;
            }
            let written =
                unsafe { libc_write(fd, patched.as_ptr() as *const c_void, patched.len()) };
            if written < 0 || written as usize != patched.len() {
                unsafe { libc_close(fd) };
                tracing::error!("write to memfd failed for {path}");
                return None;
            }
            tracing::info!("serving a rewritten {path} from memfd (fd {fd})");
            CString::new(format!("/proc/self/fd/{fd}")).ok()
        })();
        BUILDING.with(|b| b.set(false));
        result
    }

    unsafe fn libc_write(fd: c_int, buf: *const c_void, count: usize) -> isize {
        unsafe extern "C" {
            fn write(fd: c_int, buf: *const c_void, count: usize) -> isize;
        }
        unsafe { write(fd, buf, count) }
    }

    unsafe fn libc_close(fd: c_int) -> c_int {
        unsafe extern "C" {
            fn close(fd: c_int) -> c_int;
        }
        unsafe { close(fd) }
    }

    extern "C" fn open_detour(path: *const c_char, flags: c_int, mode: c_int) -> c_int {
        let original = hooking::original_function_ptr().expect("original! inside the open hook");
        let original: unsafe extern "C" fn(*const c_char, c_int, c_int) -> c_int =
            unsafe { std::mem::transmute(original.as_ptr()) };
        match redirect(path) {
            Some(temp) => unsafe { original(temp.as_ptr(), flags, mode) },
            None => unsafe { original(path, flags, mode) },
        }
    }

    extern "C" fn openat_detour(
        dirfd: c_int,
        path: *const c_char,
        flags: c_int,
        mode: c_int,
    ) -> c_int {
        let original = hooking::original_function_ptr().expect("original! inside the openat hook");
        let original: unsafe extern "C" fn(c_int, *const c_char, c_int, c_int) -> c_int =
            unsafe { std::mem::transmute(original.as_ptr()) };
        match redirect(path) {
            Some(temp) => unsafe { original(dirfd, temp.as_ptr(), flags, mode) },
            None => unsafe { original(dirfd, path, flags, mode) },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hao::dotnet::{
        entries::{TypeDef, Version},
        il::{MethodBody, OpCode},
        md::streams::tables_stream::TypeAttributes,
        module::{ModuleKind, TargetRuntime},
    };

    /// A tiny assembly with `Game.Loop::Tick()` whose body is just `ret`.
    fn sample() -> Vec<u8> {
        let mut module = Module::new("Patchee", ModuleKind::Dll, TargetRuntime::NetCore(9));
        module.add_assembly_ref(
            "System.Runtime",
            Version {
                major: 9,
                ..Default::default()
            },
            &TargetRuntime::NetCore(9).framework_token(),
        );
        let object = module.corlib_type("System.Object").unwrap();
        let ty = module.add_type(TypeDef::new(
            TypeAttributes::Public | TypeAttributes::BeforeFieldInit,
            "Game",
            "Loop",
            Some(object),
        ));
        let tick = ty.add_method(Method::public_static(
            "Tick",
            MethodSig::new_static(TypeSig::VOID, vec![]),
        ));
        tick.get_mut().body = Some(MethodBody {
            instructions: vec![Instruction::ret()],
            ..Default::default()
        });
        module.write().unwrap()
    }

    #[test]
    fn splices_a_pinvoke_call() {
        let patched = patch_call(&sample(), "Game.Loop", "Tick", "libhook.so", "hao_tick").unwrap();
        let reloaded = Module::from_bytes(&patched).unwrap();

        // The P/Invoke method was added, forwarded to our library.
        let pinvoke = reloaded
            .find_method("Game.Loop", "hao_tick")
            .expect("pinvoke method");
        let pinvoke = pinvoke.get();
        let impl_map = pinvoke.impl_map.as_ref().expect("impl map");
        assert_eq!(impl_map.import_name, "hao_tick");
        assert_eq!(impl_map.scope.get().name, "libhook.so");

        // Tick now calls it before anything else.
        let tick = reloaded.find_method("Game.Loop", "Tick").unwrap();
        let tick = tick.get();
        let body = tick.body.as_ref().unwrap();
        assert_eq!(body.instructions[0].opcode, OpCode::Call);
        assert_eq!(body.instructions.last().unwrap().opcode, OpCode::Ret);
    }

    #[test]
    fn roundtrip_size() {
        let path = "/games/SteamLibrary/steamapps/common/Slay the Spire 2/data_sts2_linuxbsd_x86_64/GodotSharp.dll";
        let Ok(original) = std::fs::read(path) else {
            eprintln!("skipping: {path} not found");
            return;
        };
        let module = Module::from_bytes(&original).unwrap();
        let roundtripped = module.write().unwrap();
        eprintln!(
            "original: {} bytes, roundtripped: {} bytes, ratio: {:.2}x",
            original.len(),
            roundtripped.len(),
            roundtripped.len() as f64 / original.len() as f64
        );
        assert!(
            roundtripped.len() < original.len() * 3 / 2,
            "roundtripped size ({}) is more than 1.5x the original ({})",
            roundtripped.len(),
            original.len()
        );
    }

    #[test]
    fn patches_real_godotsharp() {
        let path = "/games/SteamLibrary/steamapps/common/Slay the Spire 2/data_sts2_linuxbsd_x86_64/GodotSharp.dll";
        let Ok(original) = std::fs::read(path) else {
            eprintln!("skipping: {path} not found");
            return;
        };
        let patched = patch_call(
            &original,
            "Godot.SceneTree",
            "InvokeGodotClassMethod",
            "/home/samm/src/projects/xur/target/release/libxur_stub.so",
            "hao_godot_tick",
        )
        .expect("patch_call failed");

        let reloaded = Module::from_bytes(&patched).expect("reload failed");
        let pinvoke = reloaded
            .find_method("Godot.SceneTree", "hao_godot_tick")
            .expect("hao_godot_tick not found");
        let p = pinvoke.get();
        eprintln!("flags: {:?}", p.flags);
        let impl_map = p.impl_map.as_ref().expect("NO IMPL_MAP on hao_godot_tick");
        eprintln!(
            "impl_map: import={} scope={}",
            impl_map.import_name,
            impl_map.scope.get().name
        );
        assert_eq!(impl_map.import_name, "hao_godot_tick");
        assert!(
            impl_map.scope.get().name.contains("libxur_stub"),
            "scope should reference libxur_stub.so, got: {}",
            impl_map.scope.get().name
        );

        eprintln!("module_refs: {}", reloaded.module_refs.len());
        for mr in &reloaded.module_refs {
            eprintln!("  {}", mr.get().name);
        }
    }
}