plat/web_lib.rs
1// Browser dynamic-library boundary. Native machine code and arbitrary address access are
2// explicitly unsupported; no host or shell escape is substituted for the native API.
3
4/// Native guarded-call fault code.
5pub const ERR_FAULT: i32 = -64;
6/// Readable native segment flag.
7pub const LIBA_R: u32 = 1;
8/// Writable native segment flag.
9pub const LIBA_W: u32 = 2;
10/// Executable native segment flag.
11pub const LIBA_X: u32 = 4;
12/// Native library input failure.
13pub const LIBE_IO: u32 = 1;
14/// Unsupported native binary format, including all browser DLL/SO attempts.
15pub const LIBE_FORMAT: u32 = 2;
16/// Unsupported native dependencies.
17pub const LIBE_DEPS: u32 = 3;
18/// Unsupported relocation.
19pub const LIBE_RELOC: u32 = 4;
20/// Allocation failure.
21pub const LIBE_NOMEM: u32 = 5;
22/// Unsupported initializer.
23pub const LIBE_INIT: u32 = 6;
24/// Unsupported native mapping.
25pub const LIBE_MAP: u32 = 7;
26/// Native plugin size limit, retained in the common API.
27pub const LIB_FILE_MAX: u64 = 64 << 20;
28/// Native segment count limit.
29pub const LH_SEGMAX: usize = 16;
30/// No recorded fault.
31pub const GK_NONE: u32 = 0;
32/// Access violation category.
33pub const GK_ACCESS: u32 = 1;
34/// Illegal instruction category.
35pub const GK_ILLEGAL: u32 = 2;
36/// Arithmetic fault category.
37pub const GK_ARITH: u32 = 3;
38/// Bus fault category.
39pub const GK_BUS: u32 = 4;
40/// Breakpoint category.
41pub const GK_BREAK: u32 = 5;
42/// Stack exhaustion category.
43pub const GK_STACK: u32 = 6;
44/// Native guard slot count.
45pub const GT_MAX: usize = 128;
46
47/// Native segment description; no such segments are loaded in the browser.
48#[derive(Clone, Copy, Default, Debug)]
49pub struct LibSeg {
50 /// Inclusive segment start.
51 pub start: usize,
52 /// Exclusive segment end.
53 pub end: usize,
54 /// LIBA flags.
55 pub flags: u32,
56}
57/// Opaque native plugin handle; browser loading always returns an error.
58pub struct Lib {
59 _private: (),
60}
61impl Lib {
62 /// No native image base exists.
63 pub fn base(&self) -> usize {
64 0
65 }
66 /// No native image mapping exists.
67 pub fn span(&self) -> usize {
68 0
69 }
70 /// No native segments exist.
71 pub fn segs(&self) -> &[LibSeg] {
72 &[]
73 }
74}
75/// Reject native DLL/SO input on WebAssembly.
76pub fn lib_open(_path: &str) -> Result<Lib, u32> {
77 Err(LIBE_FORMAT)
78}
79/// Native symbols cannot be resolved.
80pub fn lib_sym(_lib: &Lib, _name: &str) -> usize {
81 0
82}
83/// Consume an opaque handle.
84pub fn lib_close(_lib: Lib) {}
85/// No native segment can contain a requested address.
86pub fn lib_seg(_lib: &Lib, _address: usize) -> (usize, u32) {
87 (0, 0)
88}
89
90/// Compatibility fault record for APIs returning an unsupported-operation error.
91#[derive(Clone, Copy, Default, Debug)]
92#[repr(C)]
93pub struct GuardInfo {
94 /// Fault kind.
95 pub kind: u32,
96 /// Platform error code.
97 pub code: u32,
98 /// Faulting code address.
99 pub pc: u64,
100 /// Faulting data address.
101 pub addr: u64,
102 /// Caller tag.
103 pub tag: u64,
104}
105/// Browser traps cannot be installed as recoverable native exception handlers.
106pub fn guard_init() -> i32 {
107 crate::ERR_UNSUPPORTED
108}
109/// No native guard stack is installed.
110pub fn guard_tag() -> u64 {
111 0
112}
113/// Reject arbitrary function-pointer execution through the native plugin boundary.
114pub fn guard_call(
115 _f: usize,
116 _a: u64,
117 _b: u64,
118 _c: u64,
119 _d: u64,
120 _info: Option<&mut GuardInfo>,
121) -> (u64, u64) {
122 (
123 crate::ERR_UNSUPPORTED as i64 as u64,
124 crate::ERR_UNSUPPORTED as i64 as u64,
125 )
126}
127/// Reject a request for native fault recovery; the supplied closure is not invoked.
128pub fn guard_run<R, F: FnOnce() -> R>(_f: F, _info: Option<&mut GuardInfo>) -> Result<R, i32> {
129 Err(crate::ERR_UNSUPPORTED)
130}
131/// Arbitrary address reads are unavailable; use bounded host buffers instead.
132pub fn guard_read(_address: usize, _len: usize) -> Option<alloc::vec::Vec<u8>> {
133 None
134}
135/// Arbitrary address writes are unavailable.
136pub fn guard_write(_address: usize, _bytes: &[u8]) -> bool {
137 false
138}
139/// Native library memory is unavailable.
140pub fn lib_read(_lib: &Lib, _address: usize, _len: usize) -> Option<alloc::vec::Vec<u8>> {
141 None
142}
143/// Native instruction fault tests are unavailable.
144pub fn guard_test_code(_kind: u32) -> usize {
145 0
146}
147/// No native guard test state exists.
148pub fn guard_test_state() -> (u32, u32, bool) {
149 (0, 0, false)
150}