forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative.rs
More file actions
343 lines (311 loc) · 10.9 KB
/
Copy pathnative.rs
File metadata and controls
343 lines (311 loc) · 10.9 KB
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
//! Native function and library registry.
//!
//! Provides a thread-safe global registry of dynamically loaded libraries and
//! resolved symbols. Symbols are keyed by `(library_name, symbol_name)` so the
//! same name can be provided by different libraries.
use std::collections::{HashMap, HashSet};
use std::ffi::c_void;
use std::sync::{Mutex, OnceLock};
use super::marshal::Signature;
/// A loaded dynamic library.
pub struct NativeLibrary {
inner: libloading::Library,
name: String,
}
impl NativeLibrary {
/// Open a dynamic library by path.
///
/// # Safety
/// The caller must ensure the path points to a valid shared library.
pub unsafe fn open(path: &str) -> Result<Self, String> {
let inner = unsafe { libloading::Library::new(path) }.map_err(|e| e.to_string())?;
Ok(Self {
inner,
name: path.to_string(),
})
}
/// Return the path/name used to open this library.
pub fn name(&self) -> &str {
&self.name
}
/// Resolve a symbol from this library.
///
/// # Safety
/// The caller must ensure the symbol actually has the requested type.
pub unsafe fn resolve<T>(&self, symbol: &[u8]) -> Result<libloading::Symbol<'_, T>, String> {
self.inner.get(symbol).map_err(|e| {
format!(
"failed to resolve {}: {}",
String::from_utf8_lossy(symbol),
e
)
})
}
}
impl std::fmt::Debug for NativeLibrary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NativeLibrary")
.field("name", &self.name)
.finish()
}
}
/// A native function callable through the FFI layer.
///
/// The function pointer is stored as an opaque `*const c_void` so it can be
/// transmuted to the correct `extern "C"` signature at call time.
#[derive(Debug, Clone)]
pub struct NativeFunction {
pub ptr: *const c_void,
pub signature: Signature,
pub library: Option<String>,
pub symbol: String,
}
// SAFETY: `*const c_void` is used as an opaque function pointer. The registry
// guarantees that the pointed-to function outlives the registry entry, and all
// access is serialized by the enclosing `Mutex`.
unsafe impl Send for NativeFunction {}
// SAFETY: function pointers are immutable once registered; shared access is
// safe because `call_native` only reads from the pointer.
unsafe impl Sync for NativeFunction {}
impl NativeFunction {
/// Create a native function entry from a raw C function pointer.
///
/// # Safety
/// `ptr` must point to a function whose ABI matches `signature`.
pub unsafe fn new(
ptr: *const c_void,
signature: Signature,
library: Option<String>,
symbol: String,
) -> Self {
Self {
ptr,
signature,
library,
symbol,
}
}
}
#[derive(Debug, Default, Clone)]
pub enum FfiPolicy {
#[default]
AllowAll,
Allowlist(HashSet<String>),
}
/// Internal registry backing the global `FFI_REGISTRY`.
#[derive(Debug, Default)]
pub struct FfiRegistry {
functions: HashMap<(Option<String>, String), NativeFunction>,
libraries: HashMap<String, NativeLibrary>,
policy: FfiPolicy,
}
impl FfiRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn set_policy(&mut self, policy: FfiPolicy) {
self.policy = policy;
}
pub fn is_lib_allowed(&self, path: &str) -> bool {
match &self.policy {
FfiPolicy::AllowAll => true,
FfiPolicy::Allowlist(allowed) => allowed.contains(path),
}
}
/// Load a dynamic library and keep it open for symbol resolution.
///
/// # Safety
/// The caller must ensure `path` points to a valid shared library.
pub unsafe fn load_library(&mut self, path: &str) -> Result<NativeLibrary, String> {
if !self.is_lib_allowed(path) {
return Err(format!("FFI: library '{}' not in allowlist", path));
}
if let Some(_lib) = self.libraries.get(path) {
// Library is already open; return a reference-equivalent description.
return unsafe { NativeLibrary::open(path) };
}
let stored = unsafe { NativeLibrary::open(path)? };
self.libraries.insert(path.to_string(), stored);
unsafe { NativeLibrary::open(path) }
}
/// Resolve a registered native function.
pub fn resolve(&self, library: Option<&str>, symbol: &str) -> Option<NativeFunction> {
self.functions
.get(&(library.map(String::from), symbol.to_string()))
.cloned()
}
/// Register a native function under its symbol (and optional library).
pub fn register(&mut self, function: NativeFunction) {
let key = (function.library.clone(), function.symbol.clone());
self.functions.insert(key, function);
}
/// Resolve a native function, loading its library on demand if necessary.
///
/// First tries a pre-registered function under `(Some(library), symbol)`,
/// then `(None, symbol)`. If neither is found, the library is opened and
/// the symbol is resolved as an opaque function pointer.
///
/// # Safety
/// `library` must name a valid shared library when the function is not
/// pre-registered.
pub unsafe fn resolve_or_load(
&mut self,
library: &str,
symbol: &str,
signature: Signature,
) -> Result<NativeFunction, String> {
if let Some(func) = self.resolve(Some(library), symbol) {
return Ok(func);
}
if let Some(func) = self.resolve(None, symbol) {
return Ok(func);
}
let lib = self.load_library(library)?;
let symbol_name = symbol.to_string();
// SAFETY: caller guarantees the symbol exists and has the requested type.
let sym = unsafe { lib.resolve::<unsafe extern "C" fn()>(symbol.as_bytes())? };
let ptr: *const c_void = *sym as *const c_void;
let func = NativeFunction::new(ptr, signature, Some(library.to_string()), symbol_name);
self.register(func.clone());
Ok(func)
}
}
/// Global thread-safe FFI registry.
pub static FFI_REGISTRY: OnceLock<Mutex<FfiRegistry>> = OnceLock::new();
fn global_registry() -> &'static Mutex<FfiRegistry> {
FFI_REGISTRY.get_or_init(|| Mutex::new(FfiRegistry::new()))
}
/// Register a native function in the global registry.
///
/// # Safety
/// `ptr` must point to a function whose C ABI matches `signature`. The
/// function must remain valid for the lifetime of the registry entry.
pub unsafe fn register_native_function(
name: &str,
ptr: *const c_void,
signature: Signature,
) -> Result<(), String> {
let func = NativeFunction::new(ptr, signature, None, name.to_string());
let mut reg = global_registry().lock().map_err(|e| e.to_string())?;
reg.register(func);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ffi_allowlist() {
let mut reg = FfiRegistry::new();
// By default, AllowAll permits any load. We'll use a nonexistent lib to prove
// it tries to load it (which fails) rather than rejecting by policy.
let nonexistent = "libnonexistent_does_not_exist.so";
let err = unsafe { reg.load_library(nonexistent) }.unwrap_err();
assert!(
!err.contains("not in allowlist"),
"Should not be blocked by policy"
);
// Now set a strict allowlist
let mut allowed = HashSet::new();
allowed.insert("liballowed.so".to_string());
reg.set_policy(FfiPolicy::Allowlist(allowed));
// Unallowed library fails by policy
let err_denied = unsafe { reg.load_library(nonexistent) }.unwrap_err();
assert_eq!(
err_denied,
format!("FFI: library '{}' not in allowlist", nonexistent)
);
// Allowed library fails at load time (since it doesn't exist), not by policy
let err_allowed = unsafe { reg.load_library("liballowed.so") }.unwrap_err();
assert!(
!err_allowed.contains("not in allowlist"),
"Should not be blocked by policy"
);
}
use crate::ffi::marshal::{CType, Signature};
use std::ffi::c_void;
#[test]
fn test_ffi_registry_new() {
let registry = FfiRegistry::new();
assert!(registry.functions.is_empty());
assert!(registry.libraries.is_empty());
}
#[test]
fn test_registry_register_and_lookup() {
let mut registry = FfiRegistry::new();
let dummy_ptr = std::ptr::null::<c_void>();
// SAFETY: null pointer is never called.
let func = unsafe {
NativeFunction::new(
dummy_ptr,
Signature::new(vec![], CType::Unit),
None,
"test_fn".to_string(),
)
};
registry.register(func);
let found = registry.resolve(None, "test_fn");
assert!(found.is_some());
assert_eq!(found.unwrap().symbol, "test_fn");
}
#[test]
fn test_registry_list() {
let mut registry = FfiRegistry::new();
let dummy_ptr = std::ptr::null::<c_void>();
// SAFETY: null pointers are never called.
let func1 = unsafe {
NativeFunction::new(
dummy_ptr,
Signature::new(vec![], CType::Unit),
None,
"fn_a".to_string(),
)
};
let func2 = unsafe {
NativeFunction::new(
dummy_ptr,
Signature::new(vec![], CType::Unit),
None,
"fn_b".to_string(),
)
};
registry.register(func1);
registry.register(func2);
assert_eq!(registry.functions.len(), 2);
let names: Vec<&str> = registry
.functions
.values()
.map(|f| f.symbol.as_str())
.collect();
assert!(names.contains(&"fn_a"));
assert!(names.contains(&"fn_b"));
}
#[test]
fn test_registry_duplicate_name() {
let mut registry = FfiRegistry::new();
let dummy_ptr = std::ptr::null::<c_void>();
// SAFETY: null pointers are never called.
let func1 = unsafe {
NativeFunction::new(
dummy_ptr,
Signature::new(vec![], CType::Unit),
None,
"dup".to_string(),
)
};
let func2 = unsafe {
NativeFunction::new(
dummy_ptr,
Signature::new(vec![], CType::Unit),
None,
"dup".to_string(),
)
};
registry.register(func1);
// Second registration with the same name does not panic (HashMap overwrite).
registry.register(func2);
assert_eq!(registry.functions.len(), 1);
let found = registry.resolve(None, "dup");
assert!(found.is_some());
assert_eq!(found.unwrap().symbol, "dup");
}
}