hao-runtime 0.0.11

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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Host the .NET runtime in a Rust process and call into managed code.
//!
//! Two backends are available behind features:
//! - `coreclr` (default): .NET 8+, loading `libcoreclr` directly.
//! - `mono`: the Mono embedding API, as used by Unity.
//!
//! Managed methods are called through native function pointers, so the
//! methods have to be callable from native code: `[UnmanagedCallersOnly]`
//! static methods on CoreCLR, or any static method on Mono. Bindings generated
//! by `hao-runtime-macros` produce such methods for you.

#[cfg(all(feature = "coreclr", target_arch = "x86_64"))]
pub mod attach;
pub mod bindings;
pub mod console;
#[cfg(feature = "coreclr")]
mod coreclr;
mod error;
pub mod jit;
// Only CoreCLR consumes the `Hao.Loader` helper assembly; the `coreclr`
// feature always enables `hao`, so this is where it belongs.
#[cfg(feature = "coreclr")]
mod loader;
#[cfg(feature = "mono")]
mod mono;
#[cfg(feature = "coreclr")]
pub mod profiler;
#[cfg(feature = "reflect")]
pub mod reflect;
#[cfg(feature = "hao")]
pub mod rewrite;

use std::{ffi::c_void, path::Path, sync::Arc};

pub use bindings::{BoundAssembly, Object};
pub use error::{Error, Result};

/// The `hao` assembly-editing crate, re-exported so downstream crates can
/// build modules to load with [`Runtime::load_module`] without pinning
/// their own (possibly mismatched) `hao` version.
#[cfg(feature = "hao")]
pub use hao;

#[cfg(feature = "coreclr")]
pub use coreclr::{find_dotnet_root, CoreClrOptions};
#[cfg(feature = "mono")]
pub use mono::MonoOptions;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum Backend {
    CoreClr,
    Mono,
}

// hao-runtime wraps a *running* .NET runtime, so it needs a backend: without
// one `Inner` has no variants, `Runtime` has no constructor, and every
// `match &*self.0` is a non-exhaustive match on the (still inhabited) `&Inner`.
// Require a backend explicitly rather than emit a wall of E0004s.
#[cfg(not(any(feature = "coreclr", feature = "mono")))]
compile_error!("hao-runtime requires a backend feature: enable `coreclr` (the default) or `mono`");

enum Inner {
    #[cfg(feature = "coreclr")]
    CoreClr(coreclr::CoreClr),
    #[cfg(feature = "mono")]
    Mono(mono::Mono),
}

/// A handle to a running .NET runtime. Cheap to clone.
#[derive(Clone)]
pub struct Runtime(Arc<Inner>);

impl std::fmt::Debug for Runtime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Runtime({:?})", self.backend())
    }
}

impl Runtime {
    /// Starts CoreCLR with the newest installed framework, or returns the
    /// already running instance. Only one CoreCLR can run per process.
    #[cfg(feature = "coreclr")]
    pub fn coreclr() -> Result<Self> {
        Self::coreclr_with(&CoreClrOptions::default())
    }

    /// Starts CoreCLR with `options`. The options are ignored if it is already running.
    #[cfg(feature = "coreclr")]
    pub fn coreclr_with(options: &CoreClrOptions) -> Result<Self> {
        Self::coreclr_from(|| coreclr::CoreClr::new(options))
    }

    /// Attaches to a CoreCLR already running in this process, for a native mod
    /// loaded into a .NET game (e.g. Godot on CoreCLR). `host_handle`, `domain_id`
    /// and `create_delegate` (`coreclr_create_delegate`) are captured as the
    /// game's runtime initialises — see the `hao-godot` crate; `major` is the
    /// .NET major version. After this, assemblies can be loaded and methods
    /// called as usual. Returns the existing handle if hao already has one.
    #[cfg(feature = "coreclr")]
    pub fn coreclr_attach(
        host_handle: *mut c_void,
        domain_id: u32,
        create_delegate: *const c_void,
        major: u16,
    ) -> Result<Self> {
        Self::coreclr_from(|| {
            coreclr::CoreClr::attach(host_handle, domain_id, create_delegate, major)
        })
    }

    #[cfg(feature = "coreclr")]
    fn coreclr_from(make: impl FnOnce() -> Result<coreclr::CoreClr>) -> Result<Self> {
        static RUNTIME: std::sync::Mutex<Option<Runtime>> = std::sync::Mutex::new(None);
        let mut runtime = RUNTIME.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(runtime) = runtime.as_ref() {
            return Ok(runtime.clone());
        }
        let started = Runtime(Arc::new(Inner::CoreClr(make()?)));
        *runtime = Some(started.clone());
        Ok(started)
    }

    /// Starts Mono, or returns the already running instance.
    #[cfg(feature = "mono")]
    pub fn mono(options: &MonoOptions) -> Result<Self> {
        Self::mono_with(|| mono::Mono::new(options))
    }

    /// Attaches to a Mono runtime already running in this process, for a mod
    /// loaded into a Unity Mono game. `options.library` should point at the
    /// game's own Mono. After this, assemblies can be loaded and methods called
    /// as usual. Returns the existing handle if hao already has one.
    #[cfg(feature = "mono")]
    pub fn mono_attach(options: &MonoOptions) -> Result<Self> {
        Self::mono_with(|| mono::Mono::attach(options))
    }

    #[cfg(feature = "mono")]
    fn mono_with(make: impl FnOnce() -> Result<mono::Mono>) -> Result<Self> {
        static RUNTIME: std::sync::Mutex<Option<Runtime>> = std::sync::Mutex::new(None);
        let mut runtime = RUNTIME.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(runtime) = runtime.as_ref() {
            return Ok(runtime.clone());
        }
        let started = Runtime(Arc::new(Inner::Mono(make()?)));
        *runtime = Some(started.clone());
        Ok(started)
    }

    pub fn backend(&self) -> Backend {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(_) => Backend::CoreClr,
            #[cfg(feature = "mono")]
            Inner::Mono(_) => Backend::Mono,
        }
    }

    /// Lets the calling thread run managed code. Needed for Mono on threads it
    /// didn't start, a no-op on CoreCLR. Binding calls do this automatically.
    pub fn attach_thread(&self) {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(_) => {}
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.attach_thread(),
        }
    }

    /// Loads an assembly and makes it available to later loads by name.
    pub fn load_assembly(&self, path: impl AsRef<Path>) -> Result<()> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.load_assembly(path.as_ref()),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.load_assembly(path.as_ref()),
        }
    }

    /// Loads an assembly from memory. `name` is its assembly name.
    pub fn load_assembly_bytes(&self, name: &str, bytes: &[u8]) -> Result<()> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => {
                let _ = name;
                clr.load_assembly_bytes(bytes)
            }
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.load_assembly_bytes(name, bytes),
        }
    }

    /// Loads a module built or edited with hao, without writing it to disk.
    #[cfg(feature = "hao")]
    pub fn load_module(&self, module: &hao::Module) -> Result<()> {
        let bytes = module
            .write()
            .map_err(|e| Error::Other(format!("could not write {}: {}", module.name(), e)))?;
        let name = match &module.assembly {
            Some(assembly) => assembly.get().name.clone(),
            None => module.name().trim_end_matches(".dll").to_string(),
        };
        self.load_assembly_bytes(&name, &bytes)
    }

    /// A native pointer to the static method `type_name::method` in `assembly`.
    ///
    /// On CoreCLR the method must be `[UnmanagedCallersOnly]`; for a method that
    /// isn't, use [`method_pointer`](Self::method_pointer) instead, which calls
    /// any method by JIT-compiling it. On Mono the returned thunk takes an extra
    /// trailing `*mut *mut c_void` exception argument.
    pub fn function_pointer(
        &self,
        assembly: &str,
        type_name: &str,
        method: &str,
    ) -> Result<*const c_void> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.function_pointer(assembly, type_name, method),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.function_pointer(assembly, type_name, method),
        }
    }

    /// A native-callable pointer to a static method, whether or not it is
    /// `[UnmanagedCallersOnly]`. The method is JIT-compiled first, so IL patched
    /// before the assembly was loaded takes effect.
    ///
    /// The pointer uses the managed calling convention, which matches a plain
    /// `extern "C" fn(args) -> ret` for static methods with primitive and
    /// pointer arguments. Unlike [`function_pointer`](Self::function_pointer),
    /// it takes no trailing exception argument on either backend, so a managed
    /// exception crosses it as an unhandled error.
    pub fn method_pointer(
        &self,
        assembly: &str,
        type_name: &str,
        method: &str,
    ) -> Result<*const c_void> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.method_pointer(assembly, type_name, method),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.method_pointer(assembly, type_name, method),
        }
    }

    /// Like [`method_pointer`](Self::method_pointer), but resolves the type
    /// from whichever `AssemblyLoadContext` actually loaded the assembly.
    ///
    /// Mono has no `AssemblyLoadContext`, so there it is identical to
    /// [`method_pointer`](Self::method_pointer).
    pub fn method_pointer_from_loaded(
        &self,
        assembly: &str,
        type_name: &str,
        method: &str,
    ) -> Result<*const c_void> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.method_pointer_from_loaded(assembly, type_name, method),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.method_pointer(assembly, type_name, method),
        }
    }

    /// Resolves a well-known corlib method to a native-callable pointer,
    /// deriving the type name, method name and overload arity from the
    /// [`WellKnownMethod`](hao::dotnet::entries::well_known::WellKnownMethod)
    /// vocabulary instead of raw strings.
    ///
    /// On CoreCLR the method is found in the loaded `System.Private.CoreLib`;
    /// on Mono it is found in `mono_get_corlib` (`mscorlib`).
    #[cfg(feature = "hao")]
    pub fn corlib_method_pointer(
        &self,
        method: hao::dotnet::entries::well_known::WellKnownMethod,
    ) -> Result<*const c_void> {
        let (namespace, name) = method.declaring().full_name();
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => {
                let full_name = if namespace.is_empty() {
                    name.to_string()
                } else {
                    format!("{namespace}.{name}")
                };
                clr.find_method_from_loaded(
                    "System.Private.CoreLib",
                    &full_name,
                    method.method_name(),
                    method.param_count() as i32,
                )
            }
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.corlib_method_pointer(
                namespace,
                name,
                method.method_name(),
                method.param_count() as i32,
            ),
        }
    }

    /// Like [`method_pointer_from_loaded`](Self::method_pointer_from_loaded)
    /// but matches by parameter count, so it works with overloaded methods.
    pub fn find_method_from_loaded(
        &self,
        assembly: &str,
        type_name: &str,
        method: &str,
        param_count: i32,
    ) -> Result<*const c_void> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => {
                clr.find_method_from_loaded(assembly, type_name, method, param_count)
            }
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.find_method(assembly, type_name, method, param_count),
        }
    }

    /// Calls a managed method pointer through a P/Invoke wrapper that
    /// ensures the calling thread is registered with the CLR's GC.
    /// Falls back to `jit::call_native` on Mono (which handles thread
    /// attach separately).
    #[cfg(target_arch = "x86_64")]
    pub fn invoke(&self, ptr: usize, args: &[u64]) -> Result<u64> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.invoke(ptr, args),
            #[cfg(feature = "mono")]
            Inner::Mono(_) => Ok(unsafe { jit::call_native(ptr, args) }),
        }
    }

    /// Like [`invoke`](Self::invoke) but returns `f64`.
    #[cfg(target_arch = "x86_64")]
    pub fn invoke_float(&self, ptr: usize, args: &[u64]) -> Result<f64> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.invoke_float(ptr, args),
            #[cfg(feature = "mono")]
            Inner::Mono(_) => Ok(unsafe { jit::call_native_float(ptr, args) }),
        }
    }

    /// Requests a no-GC region for the next `budget` bytes of managed
    /// allocation.  Returns `true` if the runtime agreed to suppress GC.
    ///
    /// Mono (SGen) has no `GC.TryStartNoGCRegion` equivalent, so this is an
    /// honest no-op there that always returns `false`.
    pub fn suppress_gc(&self, budget: i64) -> Result<bool> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.suppress_gc(budget),
            #[cfg(feature = "mono")]
            Inner::Mono(_) => {
                let _ = budget;
                Ok(false)
            }
        }
    }

    /// Ends a no-GC region started by [`suppress_gc`](Self::suppress_gc).
    ///
    /// A no-op on Mono, which has no no-GC-region to end (see
    /// [`suppress_gc`](Self::suppress_gc)).
    pub fn resume_gc(&self) -> Result<()> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.resume_gc(),
            #[cfg(feature = "mono")]
            Inner::Mono(_) => Ok(()),
        }
    }

    /// Warms [`enumerate`](Self::enumerate) so it does no lazy setup on the
    /// game thread.
    ///
    /// On CoreCLR this JIT-compiles and caches the `Hao.Loader::Enumerate`
    /// helper, and MUST be called off a cooperative-mode thread (e.g. at init).
    /// On Mono it resolves and caches the corlib enumerator methods; `enumerate`
    /// resolves them lazily too, so warming is only an optimisation there.
    pub fn warm_enumerate(&self) -> Result<()> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.warm_enumerate(),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.warm_enumerate(),
        }
    }

    /// Materializes any managed `IEnumerable` into the addresses of its
    /// elements, by calling a JIT-compiled `foreach` helper and reading the
    /// resulting `object[]`. This handles concrete types native code cannot
    /// read by hand — lazy iterators, LINQ chains, dictionary views.
    ///
    /// Call [`warm_enumerate`](Self::warm_enumerate) once off a cooperative-mode
    /// thread first; this call must run ON the cooperative (game) thread, like
    /// any [`method_pointer`](Self::method_pointer) call.
    ///
    /// On Mono the returned addresses are live `MonoObject*`s that SGen may move
    /// at the next GC (each step of the walk is a safepoint), so read or pin
    /// them before continuing — the same raw-pointer contract as the rest of the
    /// Mono backend.
    pub fn enumerate(&self, obj: usize) -> Result<Vec<usize>> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.enumerate(obj),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.enumerate(obj),
        }
    }

    /// The .NET major version (e.g. 9) the runtime was started or attached
    /// with. `None` on Mono, where no CoreCLR framework version applies.
    pub fn dotnet_major(&self) -> Option<u16> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => Some(clr.major()),
            #[cfg(feature = "mono")]
            Inner::Mono(_) => None,
        }
    }

    /// Returns all type full names from a loaded assembly.
    pub fn type_names(&self, assembly: &str) -> Result<Vec<String>> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.type_names(assembly),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.type_names(assembly),
        }
    }

    /// Returns all method names from a type in a loaded assembly.
    pub fn method_names(&self, assembly: &str, type_name: &str) -> Result<Vec<String>> {
        match &*self.0 {
            #[cfg(feature = "coreclr")]
            Inner::CoreClr(clr) => clr.method_names(assembly, type_name),
            #[cfg(feature = "mono")]
            Inner::Mono(mono) => mono.method_names(assembly, type_name),
        }
    }
}