use flpdf::{EncryptParams, ObjectHandle, ObjectStreamMode, Pdf, PdfWriter};
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::io::{self, Cursor, Write};
std::thread_local! {
static ALLOCATION_TRACKING: Cell<bool> = const { Cell::new(false) };
static LIVE_BYTES: Cell<usize> = const { Cell::new(0) };
static PEAK_LIVE_BYTES: Cell<usize> = const { Cell::new(0) };
static TOTAL_ALLOCATED_BYTES: Cell<usize> = const { Cell::new(0) };
static TOTAL_ALLOCATION_COUNT: Cell<usize> = const { Cell::new(0) };
}
struct LiveAllocationTracker;
fn record_allocation(size: usize) {
let _ = ALLOCATION_TRACKING.try_with(|tracking| {
if tracking.get() {
TOTAL_ALLOCATED_BYTES.with(|total| {
total.set(total.get().saturating_add(size));
});
TOTAL_ALLOCATION_COUNT.with(|total| {
total.set(total.get().saturating_add(1));
});
LIVE_BYTES.with(|live| {
let live_bytes = live.get().saturating_add(size);
live.set(live_bytes);
PEAK_LIVE_BYTES.with(|peak| {
if live_bytes > peak.get() {
peak.set(live_bytes);
}
});
});
}
});
}
fn record_deallocation(size: usize) {
let _ = ALLOCATION_TRACKING.try_with(|tracking| {
if tracking.get() {
LIVE_BYTES.with(|live| live.set(live.get().saturating_sub(size)));
}
});
}
unsafe impl GlobalAlloc for LiveAllocationTracker {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc(layout) };
if !pointer.is_null() {
record_allocation(layout.size());
}
pointer
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc_zeroed(layout) };
if !pointer.is_null() {
record_allocation(layout.size());
}
pointer
}
unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
let reallocated = unsafe { System.realloc(pointer, layout, new_size) };
if !reallocated.is_null() {
record_deallocation(layout.size());
record_allocation(new_size);
}
reallocated
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) };
record_deallocation(layout.size());
}
}
#[global_allocator]
static ALLOCATOR: LiveAllocationTracker = LiveAllocationTracker;
fn start_allocation_measurement_after_fixture_baseline() {
LIVE_BYTES.with(|live| live.set(0));
PEAK_LIVE_BYTES.with(|peak| peak.set(0));
TOTAL_ALLOCATED_BYTES.with(|total| total.set(0));
TOTAL_ALLOCATION_COUNT.with(|total| total.set(0));
ALLOCATION_TRACKING.with(|tracking| tracking.set(true));
}
#[derive(Clone, Copy, Debug)]
struct AllocationStats {
peak_live_bytes: usize,
total_allocated_bytes: usize,
total_allocation_count: usize,
}
fn finish_allocation_measurement() -> AllocationStats {
ALLOCATION_TRACKING.with(|tracking| tracking.set(false));
AllocationStats {
peak_live_bytes: PEAK_LIVE_BYTES.with(Cell::get),
total_allocated_bytes: TOTAL_ALLOCATED_BYTES.with(Cell::get),
total_allocation_count: TOTAL_ALLOCATION_COUNT.with(Cell::get),
}
}
struct DiscardWriter;
impl Write for DiscardWriter {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Ok(data.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn one_page_fixture() -> Pdf<Cursor<Vec<u8>>> {
Pdf::open(Cursor::new(
include_bytes!("../../../tests/fixtures/compat/one-page.pdf").to_vec(),
))
.expect("open the one-page linearization fixture")
}
fn write_linearized(pdf: &mut Pdf<Cursor<Vec<u8>>>, encrypted: bool) {
let mut writer = PdfWriter::new(pdf);
writer.set_object_stream_mode(ObjectStreamMode::Disable);
writer.set_compress_streams(false);
writer.set_static_id(true);
writer.set_linearization(true);
if encrypted {
writer.set_encryption_parameters(EncryptParams::v4_aes128(b"user", b"owner"));
}
writer
.set_output_writer(DiscardWriter)
.expect("install the discard output");
writer.write().expect("linearized write succeeds");
}
fn linearized_peak_for_object_count(object_count: usize) -> usize {
let mut pdf = attach_small_objects(object_count);
start_allocation_measurement_after_fixture_baseline();
write_linearized(&mut pdf, false);
finish_allocation_measurement().peak_live_bytes
}
fn linearized_allocation_stats_for_object_count(object_count: usize) -> AllocationStats {
let mut pdf = attach_small_objects(object_count);
start_allocation_measurement_after_fixture_baseline();
write_linearized(&mut pdf, false);
finish_allocation_measurement()
}
fn encrypted_linearized_allocation_stats_for_object_count(object_count: usize) -> AllocationStats {
let mut pdf = attach_small_objects(object_count);
start_allocation_measurement_after_fixture_baseline();
write_linearized(&mut pdf, true);
finish_allocation_measurement()
}
fn attach_small_objects(object_count: usize) -> Pdf<Cursor<Vec<u8>>> {
let mut pdf = one_page_fixture();
let root = pdf.root_handle().expect("resolve the live Catalog");
for index in 0..object_count {
let object = pdf
.make_indirect_from_object_handle(ObjectHandle::dictionary(vec![(
b"/V".to_vec(),
ObjectHandle::integer(index as i64),
)]))
.expect("create a per-object bookkeeping entry");
root.replace_key(format!("/Obj{index:05}").as_bytes(), object)
.expect("attach the object to the Catalog");
}
pdf
}
fn linearized_peak_for_padding(padding: usize) -> usize {
let mut pdf = one_page_fixture();
let root = pdf.root_handle().expect("resolve the live Catalog");
let payload = pdf
.make_indirect_from_object_handle(ObjectHandle::dictionary(vec![(
b"/Payload".to_vec(),
ObjectHandle::string(vec![b'p'; padding]),
)]))
.expect("create the padded object");
root.replace_key(b"/Padded", payload)
.expect("attach the padded object");
start_allocation_measurement_after_fixture_baseline();
write_linearized(&mut pdf, false);
finish_allocation_measurement().peak_live_bytes
}
const SMALL_OBJECT_COUNT: usize = 256;
const LARGE_OBJECT_COUNT: usize = 4096;
const PEAK_BYTES_PER_OBJECT_BOUND: usize = 710;
#[test]
fn linearized_route_keeps_no_second_complete_per_object_map() {
let small = linearized_peak_for_object_count(SMALL_OBJECT_COUNT);
let large = linearized_peak_for_object_count(LARGE_OBJECT_COUNT);
let extra_objects = LARGE_OBJECT_COUNT - SMALL_OBJECT_COUNT;
let growth = large.saturating_sub(small);
assert!(
growth <= PEAK_BYTES_PER_OBJECT_BOUND * extra_objects,
"linearization retained a second per-object map: small={small}, large={large}, \
growth={growth} over {extra_objects} objects ({:.1} bytes per object), \
bound={PEAK_BYTES_PER_OBJECT_BOUND}",
growth as f64 / extra_objects as f64,
);
}
const LINEARIZED_PADDING_BYTES: usize = 4 * 1024 * 1024;
const PASS_BODY_RESIDENCY_ALLOWANCE: usize = 256 * 1024;
#[test]
fn linearized_passes_retain_no_document_sized_body() {
let unpadded = linearized_peak_for_padding(0);
let padded = linearized_peak_for_padding(LINEARIZED_PADDING_BYTES);
assert!(
padded <= unpadded + PASS_BODY_RESIDENCY_ALLOWANCE,
"a layout pass retained the document body: unpadded={unpadded}, \
padded={padded} for a {LINEARIZED_PADDING_BYTES}-byte payload, \
allowance={PASS_BODY_RESIDENCY_ALLOWANCE}"
);
}
const TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND: usize = 3760;
const TOTAL_ALLOCATIONS_PER_OBJECT_BOUND: f64 = 36.0;
const ENCRYPTED_TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND: usize = 3600;
const ENCRYPTED_TOTAL_ALLOCATIONS_PER_OBJECT_BOUND: f64 = 50.0;
#[test]
fn linearized_body_emission_reuses_writer_owned_allocation_state() {
let small = linearized_allocation_stats_for_object_count(SMALL_OBJECT_COUNT);
let large = linearized_allocation_stats_for_object_count(LARGE_OBJECT_COUNT);
let extra_objects = LARGE_OBJECT_COUNT - SMALL_OBJECT_COUNT;
let allocated_bytes_per_object = large
.total_allocated_bytes
.saturating_sub(small.total_allocated_bytes)
/ extra_objects;
let allocations_per_object = large
.total_allocation_count
.saturating_sub(small.total_allocation_count) as f64
/ extra_objects as f64;
assert!(
allocated_bytes_per_object <= TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND,
"linearized body emission retained transient per-object writer state: \
small={small:?}, large={large:?}, allocated_bytes_per_object={allocated_bytes_per_object}, \
bound={TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND}"
);
assert!(
allocations_per_object <= TOTAL_ALLOCATIONS_PER_OBJECT_BOUND,
"linearized body emission still allocates per-object adapter maps: \
small={small:?}, large={large:?}, allocations_per_object={allocations_per_object:.2}, \
bound={TOTAL_ALLOCATIONS_PER_OBJECT_BOUND:.2}"
);
}
#[test]
fn encrypted_linearized_body_emission_reuses_writer_file_key_state() {
let small = encrypted_linearized_allocation_stats_for_object_count(SMALL_OBJECT_COUNT);
let large = encrypted_linearized_allocation_stats_for_object_count(LARGE_OBJECT_COUNT);
let extra_objects = LARGE_OBJECT_COUNT - SMALL_OBJECT_COUNT;
let allocated_bytes_per_object = large
.total_allocated_bytes
.saturating_sub(small.total_allocated_bytes)
/ extra_objects;
let allocations_per_object = large
.total_allocation_count
.saturating_sub(small.total_allocation_count) as f64
/ extra_objects as f64;
assert!(
allocated_bytes_per_object <= ENCRYPTED_TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND,
"encrypted linearized emission allocated a per-object file-key clone: \
small={small:?}, large={large:?}, allocated_bytes_per_object={allocated_bytes_per_object}, \
bound={ENCRYPTED_TOTAL_ALLOCATED_BYTES_PER_OBJECT_BOUND}"
);
assert!(
allocations_per_object <= ENCRYPTED_TOTAL_ALLOCATIONS_PER_OBJECT_BOUND,
"encrypted linearized emission allocated per-object writer state: \
small={small:?}, large={large:?}, allocations_per_object={allocations_per_object:.2}, \
bound={ENCRYPTED_TOTAL_ALLOCATIONS_PER_OBJECT_BOUND:.2}"
);
}
#[test]
fn the_padded_payload_reaches_the_linearized_output() {
let mut pdf = one_page_fixture();
let root = pdf.root_handle().expect("resolve the live Catalog");
let payload = pdf
.make_indirect_from_object_handle(ObjectHandle::dictionary(vec![(
b"/Payload".to_vec(),
ObjectHandle::string(vec![b'p'; LINEARIZED_PADDING_BYTES]),
)]))
.expect("create the padded object");
root.replace_key(b"/Padded", payload)
.expect("attach the padded object");
let mut writer = PdfWriter::new(&mut pdf);
writer.set_object_stream_mode(ObjectStreamMode::Disable);
writer.set_compress_streams(false);
writer.set_static_id(true);
writer.set_linearization(true);
writer
.set_output_memory()
.expect("install the memory output");
writer.write().expect("linearized write succeeds");
let written = writer.get_buffer().expect("linearized output buffer");
assert!(
written.len() > LINEARIZED_PADDING_BYTES,
"the padded payload never reached the output: {} bytes written for a \
{LINEARIZED_PADDING_BYTES}-byte payload",
written.len()
);
}