#![no_std]
extern crate alloc;
#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/cpu.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/portable/cpu.rs")]
pub mod cpu;
#[path = "shared/rt/crash.rs"]
pub mod crash;
#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/fx.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/aarch64/fx.rs")]
pub mod fx;
#[path = "shared/rt/portable/fx.rs"]
pub mod scalar_fx;
pub mod portable {
pub use crate::scalar_fx as fx;
}
#[path = "shared/rt/sync.rs"]
pub mod sync;
#[cfg_attr(not(target_arch = "aarch64"), path = "shared/rt/x87.rs")] #[cfg_attr(target_arch = "aarch64", path = "shared/rt/portable/math.rs")]
pub mod x87;
#[path = "shared/plat/modabi.rs"]
pub mod modabi;
mod mem;
use alloc::vec::Vec;
use core::alloc::{GlobalAlloc, Layout};
use core::mem::ManuallyDrop;
use core::sync::atomic::{AtomicPtr, AtomicU32, Ordering};
use modabi::*;
static HOST: AtomicPtr<HostTable> = AtomicPtr::new(core::ptr::null_mut());
#[inline]
pub fn host() -> &'static HostTable {
let p = HOST.load(Ordering::Acquire);
if p.is_null() {
trap();
}
unsafe { &*p }
}
pub fn trap() -> ! {
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)); }
}
pub mod os {
pub fn yield_now() {
(crate::host().yield_now)()
}
pub fn exit(code: i32) -> ! {
let mut s = crate::crash::StackText::<64>::new();
let _ = core::fmt::Write::write_fmt(&mut s, format_args!("exit({code}) inside the module"));
(crate::host().panic)(s.bytes().as_ptr(), s.bytes().len())
}
pub fn write_stdout(b: &[u8]) {
let b = b.strip_suffix(b"\n").unwrap_or(b);
(crate::host().log)(b.as_ptr(), b.len())
}
pub fn write_stderr(b: &[u8]) {
write_stdout(b)
}
}
struct HostAlloc;
unsafe impl GlobalAlloc for HostAlloc {
unsafe fn alloc(&self, l: Layout) -> *mut u8 {
(host().alloc)(l.size(), l.align(), 0)
}
unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 {
(host().alloc)(l.size(), l.align(), 1)
}
unsafe fn dealloc(&self, p: *mut u8, l: Layout) {
(host().free)(p, l.size(), l.align())
}
}
#[global_allocator]
static ALLOC: HostAlloc = HostAlloc;
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
use core::fmt::Write;
let mut s = crash::StackText::<1024>::new(); let _ = write!(s, "{info}");
let p = HOST.load(Ordering::Acquire);
if p.is_null() {
trap();
}
let h = unsafe { &*p };
(h.panic)(s.bytes().as_ptr(), s.bytes().len())
}
pub fn entry(h: *const HostTable, tab: &'static ModTable) -> *const ModTable {
if h.is_null() {
return core::ptr::null();
}
let t = unsafe { &*h };
if t.version != MOD_VERSION || (t.size as usize) < core::mem::size_of::<HostTable>() {
return core::ptr::null();
}
HOST.store(h as *mut HostTable, Ordering::Release);
if t.cpu & cpu::CPU_AVX2 == 0 && cpu::features() & cpu::CPU_AVX2 != 0 {
cpu::force_baseline();
}
tab
}
#[macro_export]
macro_rules! module_entry {
($name:ident, $table:path) => {
$crate::plugin_abi! {
#[allow(unsafe_code)] #[unsafe(no_mangle)]
pub fn $name(host: *const $crate::modabi::HostTable) -> *const $crate::modabi::ModTable {
$crate::entry(host, &$table) }
}
};
}
pub struct JxlRequest<'a> {
pub w: u32,
pub h: u32,
pub gray: bool,
pub alpha: bool,
pub rgba: Vec<u8>, pub effort: u32,
pub quality: u32,
pub flags: u32,
pub icc: Option<&'a [u8]>,
pub exif: Option<&'a [u8]>,
pub xmp: Option<&'a [u8]>,
pub cancel: Option<&'a AtomicU32>,
}
pub trait JxlWriter {
fn encode(req: JxlRequest<'_>) -> Result<Vec<u8>, i32>;
}
pub const fn jxl_table<W: JxlWriter>() -> ModTable {
ModTable {
size: core::mem::size_of::<ModTable>() as u32,
version: MOD_VERSION,
magic: MOD_MAGIC,
kind: MOD_JXLENC,
jxl_encode: jxl_encode::<W>,
}
}
unsafe fn blob<'a>(on: bool, p: *const u8, n: usize) -> Option<&'a [u8]> {
if !on {
return None;
}
if p.is_null() || n == 0 {
return Some(&[]);
}
Some(unsafe { core::slice::from_raw_parts(p, n) })
}
const ERR_HDR: i32 = -2;
crate::plugin_abi! { fn jxl_encode<W: JxlWriter>(req: *const JxlReq, res: *mut JxlRes) -> i32 {
if req.is_null() || res.is_null() {
return ERR_HDR;
}
let r = unsafe { &*req };
if r.size as usize != core::mem::size_of::<JxlReq>() || r.rgba.is_null() || r.rgba_len > r.rgba_cap {
return ERR_HDR;
}
let rgba = unsafe { Vec::from_raw_parts(r.rgba, r.rgba_len, r.rgba_cap) };
match r.test {
JXT_PANIC => panic!("test panic inside the module (jfgui_JXLENC_TEST=panic)"),
#[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)) },
_ => {}
}
let q = unsafe {
JxlRequest {
w: r.w,
h: r.h,
gray: r.gray != 0,
alpha: r.alpha != 0,
rgba,
effort: r.effort,
quality: r.quality,
flags: r.flags,
icc: blob(r.blobs & JXB_ICC != 0, r.icc, r.icc_len),
exif: blob(r.blobs & JXB_EXIF != 0, r.exif, r.exif_len),
xmp: blob(r.blobs & JXB_XMP != 0, r.xmp, r.xmp_len),
cancel: (r.cancel as *const AtomicU32).as_ref(),
}
};
match W::encode(q) {
Ok(v) => {
let mut v = ManuallyDrop::new(v); unsafe { *res = JxlRes { data: v.as_mut_ptr(), len: v.len(), cap: v.capacity() } };
0
}
Err(e) => e,
}
}}
extern crate self as rt;
#[path = "shared/rt/abi.rs"]
mod abi;