pheon/src/lib.rs
Jan Petykiewicz 54aa757678 Validate GDSII record sizes through one fallible path
Route every record read through size validation and report malformed payloads
as parse errors instead of panicking. Apply metadata size constraints through
the Record trait, hide Arrow reader internals, and remove unused wrappers and
scratch code.

Add coverage for direct, skipped, and payload reads, truncated buffers, and
FFI error offsets.
2026-11-16 20:44:48 -08:00

307 lines
11 KiB
Rust

pub mod basic;
pub mod record;
pub mod records;
mod elements;
pub mod library;
pub mod review;
use crate::library::{read_library, read_selected_library, scan_library};
use std::any::Any;
use std::cell::RefCell;
use std::mem::size_of;
use std::ffi::{CStr, CString, c_char};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::{Path, PathBuf};
use std::ptr;
#[cfg(unix)]
use std::os::unix::ffi::OsStringExt;
use std::str;
use std::fs;
use std::slice;
use arrow_array::ffi::{to_ffi, FFI_ArrowArray, FFI_ArrowSchema};
use arrow_array::{Array, StructArray};
thread_local! {
static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
}
/// Stable C API version. Increment for incompatible function/ownership changes.
#[unsafe(no_mangle)]
pub extern "C" fn pheon_abi_version() -> u32 {
1
}
/// Layout of the full-read and hierarchy-scan Arrow payloads.
#[unsafe(no_mangle)]
pub extern "C" fn pheon_arrow_schema_version() -> u32 {
1
}
fn clear_last_error() {
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = None);
}
fn set_last_error(message: String) -> i32 {
let sanitized = message.replace('\0', "\\0");
LAST_ERROR.with(|last_error| *last_error.borrow_mut() = Some(CString::new(sanitized).unwrap()));
-1
}
fn describe_panic(payload: Box<dyn Any + Send>) -> String {
if let Some(message) = payload.downcast_ref::<&str>() {
(*message).to_string()
} else if let Some(message) = payload.downcast_ref::<String>() {
message.clone()
} else {
"unexpected panic".to_string()
}
}
fn describe_parse_error(input: &[u8], (remaining, reason): (&[u8], crate::basic::ErrType)) -> String {
// Selected-cell errors may contain a bounded range rather than a file suffix.
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
let message = match reason {
crate::basic::ErrType::Failed(message) => message,
crate::basic::ErrType::Incomplete(Some(size)) => format!("Unexpected end of input; need {size} bytes"),
crate::basic::ErrType::Incomplete(None) => "Unexpected end of input".to_string(),
};
format!("at byte {offset}: {message}")
}
fn export_struct_array(
op_name: &str,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
build: impl FnOnce() -> Result<StructArray, String>,
) -> i32 {
clear_last_error();
if arr.is_null() || schema.is_null() {
return set_last_error(format!("{op_name}: Arrow output pointer was null"));
}
match catch_unwind(AssertUnwindSafe(build)) {
Ok(Ok(struct_arr)) => match to_ffi(&struct_arr.to_data()) {
Ok((ffi_arr, ffi_schema)) => {
unsafe {
*arr = ffi_arr;
*schema = ffi_schema;
}
0
},
Err(err) => set_last_error(format!("{op_name}: failed to export Arrow data: {err}")),
},
Ok(Err(message)) => set_last_error(format!("{op_name}: {message}")),
Err(payload) => set_last_error(format!("{op_name}: {}", describe_panic(payload))),
}
}
fn pathbuf_from_c(cpath: *const c_char) -> Result<PathBuf, String> {
if cpath.is_null() {
return Err("path pointer was null".to_string());
}
let cstr = unsafe { CStr::from_ptr(cpath) };
#[cfg(unix)]
{
return Ok(PathBuf::from(std::ffi::OsString::from_vec(cstr.to_bytes().to_vec())));
}
#[cfg(windows)]
{
let ustr = str::from_utf8(cstr.to_bytes())
.map_err(|_| "Non-UTF8 paths are not supported".to_string())?;
return Ok(PathBuf::from(ustr));
}
#[allow(unreachable_code)]
Err("Unsupported OS".to_string())
}
/// Return this thread's last parser error, or null if its last call succeeded.
/// The returned string remains valid until this thread's next parser call or exit.
#[unsafe(no_mangle)]
pub extern "C" fn last_error_message() -> *const c_char {
LAST_ERROR.with(|last_error| last_error.borrow()
.as_ref()
.map_or(ptr::null(), |message| message.as_ptr()))
}
/// Read a GDSII file and export its Arrow data. Return 0 on success or -1 on error.
///
/// # Safety
/// `cpath` must point to a null-terminated path. Non-null output pointers must
/// point to initialized, writable Arrow FFI structures. Null pointers are rejected.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_path(
cpath: *const c_char,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_path", arr, schema, || {
let path = pathbuf_from_c(cpath)?;
let input = fs::read(Path::new(&path))
.map_err(|err| format!("file read failed for {}: {err}", path.display()))?;
let (_input, struct_arr) = read_library(&input)
.map_err(|err| format!("read failed {}", describe_parse_error(&input, err)))?;
Ok(struct_arr)
})
}
unsafe fn input_slice<'a, T>(data: *const T, size: usize, name: &str) -> Result<&'a [T], String> {
if size == 0 {
return Ok(&[]);
}
if data.is_null() {
return Err(format!("{name} pointer was null"));
}
if !data.is_aligned() {
return Err(format!("{name} pointer was misaligned"));
}
if size.checked_mul(size_of::<T>()).is_none_or(|bytes| bytes > isize::MAX as usize) {
return Err(format!("{name} length is too large"));
}
Ok(unsafe { slice::from_raw_parts(data, size) })
}
/// Read a borrowed GDSII buffer, exporting Arrow data that owns its storage.
///
/// # Safety
/// `data` must cover `size` readable bytes (null is allowed for zero bytes).
/// Outputs must be initialized Arrow FFI structures, as for `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_bytes(
data: *const u8,
size: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let (_input, struct_arr) = read_library(input)
.map_err(|err| format!("read failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
/// Scan cell ranges and hierarchy references. Return 0 on success or -1 on error.
///
/// # Safety
/// Non-null `data` must point to `size` readable bytes. Null is allowed when
/// `size` is zero. Output pointers have the same requirements as `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn scan_bytes(
data: *const u8,
size: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("scan_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let (_input, struct_arr) = scan_library(input)
.map_err(|err| format!("scan failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
/// Read the cells in half-open byte ranges. Return 0 on success or -1 on error.
///
/// # Safety
/// `data` has the same requirements as `scan_bytes`. Non-null `ranges` must
/// point to `range_count * 2` readable, aligned u64 values, alternating start
/// and end offsets. Null is allowed for zero ranges. Output pointers have the
/// same requirements as `read_path`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn read_cells_bytes(
data: *const u8,
size: usize,
ranges: *const u64,
range_count: usize,
arr: *mut FFI_ArrowArray,
schema: *mut FFI_ArrowSchema,
) -> i32 {
export_struct_array("read_cells_bytes", arr, schema, || {
let input = unsafe { input_slice(data, size, "data") }?;
let range_size = range_count.checked_mul(2)
.ok_or_else(|| "range count is too large".to_string())?;
let flat_ranges = unsafe { input_slice(ranges, range_size, "ranges") }?;
let cell_ranges: Vec<(u64, u64)> = flat_ranges
.as_chunks::<2>()
.0
.iter()
.map(|&[start, end]| (start, end))
.collect();
let (_input, struct_arr) = read_selected_library(input, &cell_ranges)
.map_err(|err| format!("selected-cell read failed {}", describe_parse_error(input, err)))?;
Ok(struct_arr)
})
}
macro_rules! impl_intbe {
($source:ty, $target:ty, $arr:ident, $size:ident) => {{
let values = unsafe { std::slice::from_raw_parts_mut($arr, $size) };
for value in values {
// All source types represent the target bounds exactly in f64.
let number = *value as f64;
if !number.is_finite()
|| number < <$target>::MIN as f64
|| number > <$target>::MAX as f64
{
return *value;
}
let mut slot = [0; size_of::<$source>()];
slot[..size_of::<$target>()].copy_from_slice(&(*value as $target).to_be_bytes());
*value = <$source>::from_ne_bytes(slot);
}
0 as $source
}};
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f64_to_i16(arr: *mut f64, size: usize) -> f64 { impl_intbe!(f64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f64_to_i32(arr: *mut f64, size: usize) -> f64 { impl_intbe!(f64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f32_to_i16(arr: *mut f32, size: usize) -> f32 { impl_intbe!(f32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn f32_to_i32(arr: *mut f32, size: usize) -> f32 { impl_intbe!(f32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u64_to_i16(arr: *mut u64, size: usize) -> u64 { impl_intbe!(u64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u64_to_i32(arr: *mut u64, size: usize) -> u64 { impl_intbe!(u64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i64_to_i16(arr: *mut i64, size: usize) -> i64 { impl_intbe!(i64, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i64_to_i32(arr: *mut i64, size: usize) -> i64 { impl_intbe!(i64, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u32_to_i16(arr: *mut u32, size: usize) -> u32 { impl_intbe!(u32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u32_to_i32(arr: *mut u32, size: usize) -> u32 { impl_intbe!(u32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i32_to_i16(arr: *mut i32, size: usize) -> i32 { impl_intbe!(i32, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i32_to_i32(arr: *mut i32, size: usize) -> i32 { impl_intbe!(i32, i32, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn u16_to_i16(arr: *mut u16, size: usize) -> u16 { impl_intbe!(u16, i16, arr, size) }
#[unsafe(no_mangle)]
pub unsafe extern "C" fn i16_to_i16(arr: *mut i16, size: usize) -> i16 { impl_intbe!(i16, i16, arr, size) }