1#![no_std]
30
31extern crate alloc;
32
33#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/cpu.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/portable/cpu.rs")]
34pub mod cpu;
35#[path = "shared/rt/crash.rs"]
36pub mod crash;
37#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/fx.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/aarch64/fx.rs")]
38pub mod fx;
39#[path = "shared/rt/portable/fx.rs"]
40pub mod scalar_fx;
41pub mod portable {
43 pub use crate::scalar_fx as fx;
45}
46#[path = "shared/rt/sync.rs"]
47pub mod sync;
48#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/x87.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/portable/math.rs")]
49pub mod x87;
50#[path = "shared/plat/modabi.rs"]
51pub mod modabi;
52mod mem;
53
54use alloc::vec::Vec;
55use core::alloc::{GlobalAlloc, Layout};
56use core::mem::ManuallyDrop;
57use core::sync::atomic::{AtomicPtr, AtomicU32, Ordering};
58
59use modabi::*;
60
61static HOST: AtomicPtr<HostTable> = AtomicPtr::new(core::ptr::null_mut());
62
63#[inline]
65pub fn host() -> &'static HostTable {
66 let p = HOST.load(Ordering::Acquire);
67 if p.is_null() {
68 trap();
69 }
70 unsafe { &*p }
73}
74
75pub fn trap() -> ! {
77 unsafe { #[cfg(target_arch = "x86_64")] core::arch::asm!("ud2", options(noreturn, nomem, nostack)); #[cfg(target_arch = "aarch64")] core::arch::asm!("udf #0", options(noreturn, nomem, nostack)); }
79}
80
81pub mod os {
83 pub fn yield_now() {
85 (crate::host().yield_now)()
86 }
87
88 pub fn exit(code: i32) -> ! {
90 let mut s = crate::crash::StackText::<64>::new();
91 let _ = core::fmt::Write::write_fmt(&mut s, format_args!("exit({code}) inside the module"));
92 (crate::host().panic)(s.bytes().as_ptr(), s.bytes().len())
93 }
94
95 pub fn write_stdout(b: &[u8]) {
97 let b = b.strip_suffix(b"\n").unwrap_or(b);
98 (crate::host().log)(b.as_ptr(), b.len())
99 }
100
101 pub fn write_stderr(b: &[u8]) {
103 write_stdout(b)
104 }
105}
106
107struct HostAlloc;
109
110unsafe impl GlobalAlloc for HostAlloc {
111 unsafe fn alloc(&self, l: Layout) -> *mut u8 {
112 (host().alloc)(l.size(), l.align(), 0)
113 }
114 unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
115 (host().alloc)(l.size(), l.align(), 1)
116 }
117 unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
118 (host().free)(p, l.size(), l.align())
119 }
120}
121
122#[global_allocator]
123static ALLOC: HostAlloc = HostAlloc;
124
125#[panic_handler]
126fn panic(info: &core::panic::PanicInfo) -> ! {
127 use core::fmt::Write;
128 let mut s = crash::StackText::<1024>::new(); let _ = write!(s, "{info}");
130 let p = HOST.load(Ordering::Acquire);
131 if p.is_null() {
132 trap();
133 }
134 let h = unsafe { &*p };
136 (h.panic)(s.bytes().as_ptr(), s.bytes().len())
137}
138
139pub fn entry(h: *const HostTable, tab: &'static ModTable) -> *const ModTable {
142 if h.is_null() {
143 return core::ptr::null();
144 }
145 let t = unsafe { &*h };
147 if t.version != MOD_VERSION || (t.size as usize) < core::mem::size_of::<HostTable>() {
148 return core::ptr::null();
149 }
150 HOST.store(h as *mut HostTable, Ordering::Release);
151 if t.cpu & cpu::CPU_AVX2 == 0 && cpu::features() & cpu::CPU_AVX2 != 0 {
152 cpu::force_baseline();
153 }
154 tab
155}
156
157#[macro_export]
159macro_rules! module_entry {
160 ($name:ident, $table:path) => {
161 $crate::plugin_abi! {
162 #[allow(unsafe_code)] #[unsafe(no_mangle)]
164 pub fn $name(host: *const $crate::modabi::HostTable) -> *const $crate::modabi::ModTable {
165 $crate::entry(host, &$table) }
166 }
167 };
168}
169
170pub struct JxlRequest<'a> {
172 pub w: u32,
173 pub h: u32,
174 pub gray: bool,
175 pub alpha: bool,
176 pub rgba: Vec<u8>, pub effort: u32,
178 pub quality: u32,
179 pub flags: u32,
180 pub icc: Option<&'a [u8]>,
181 pub exif: Option<&'a [u8]>,
182 pub xmp: Option<&'a [u8]>,
183 pub cancel: Option<&'a AtomicU32>,
184}
185
186pub trait JxlWriter {
188 fn encode(req: JxlRequest<'_>) -> Result<Vec<u8>, i32>;
190}
191
192pub const fn jxl_table<W: JxlWriter>() -> ModTable {
194 ModTable {
195 size: core::mem::size_of::<ModTable>() as u32,
196 version: MOD_VERSION,
197 magic: MOD_MAGIC,
198 kind: MOD_JXLENC,
199 jxl_encode: jxl_encode::<W>,
200 }
201}
202
203unsafe fn blob<'a>(on: bool, p: *const u8, n: usize) -> Option<&'a [u8]> {
208 if !on {
209 return None;
210 }
211 if p.is_null() || n == 0 {
212 return Some(&[]);
213 }
214 Some(unsafe { core::slice::from_raw_parts(p, n) })
216}
217
218const ERR_HDR: i32 = -2;
219
220crate::plugin_abi! { fn jxl_encode<W: JxlWriter>(req: *const JxlReq, res: *mut JxlRes) -> i32 {
221 if req.is_null() || res.is_null() {
222 return ERR_HDR;
223 }
224 let r = unsafe { &*req };
226 if r.size as usize != core::mem::size_of::<JxlReq>() || r.rgba.is_null() || r.rgba_len > r.rgba_cap {
227 return ERR_HDR;
228 }
229 let rgba = unsafe { Vec::from_raw_parts(r.rgba, r.rgba_len, r.rgba_cap) };
232 match r.test {
233 JXT_PANIC => panic!("test panic inside the module (jfgui_JXLENC_TEST=panic)"),
234 #[cfg(target_arch = "x86_64")] JXT_FAULT => unsafe { core::arch::asm!("mov dword ptr [{0}], {1:e}", in(reg) 0x10usize, in(reg) 16u32, options(nostack)) }, #[cfg(target_arch = "aarch64")] JXT_FAULT => unsafe { core::arch::asm!("str {1:w}, [{0}]", in(reg) 0x10usize, in(reg) 16u32, options(nostack)) },
236 _ => {}
237 }
238 let q = unsafe {
240 JxlRequest {
241 w: r.w,
242 h: r.h,
243 gray: r.gray != 0,
244 alpha: r.alpha != 0,
245 rgba,
246 effort: r.effort,
247 quality: r.quality,
248 flags: r.flags,
249 icc: blob(r.blobs & JXB_ICC != 0, r.icc, r.icc_len),
250 exif: blob(r.blobs & JXB_EXIF != 0, r.exif, r.exif_len),
251 xmp: blob(r.blobs & JXB_XMP != 0, r.xmp, r.xmp_len),
252 cancel: (r.cancel as *const AtomicU32).as_ref(),
253 }
254 };
255 match W::encode(q) {
256 Ok(v) => {
257 let mut v = ManuallyDrop::new(v); unsafe { *res = JxlRes { data: v.as_mut_ptr(), len: v.len(), cap: v.capacity() } };
260 0
261 }
262 Err(e) => e,
263 }
264}}
265
266extern crate self as rt;
269#[path = "shared/rt/abi.rs"]
270mod abi;