vita-system-alloc-wrapper

Crates.iovita-system-alloc-wrapper
lib.rsvita-system-alloc-wrapper
version0.1.0
sourcesrc
created_at2023-05-21 10:39:30.598621
updated_at2023-05-21 10:39:30.598621
descriptionA proxy for std::alloc::System which wraps dealloc in std::hint::black_box
homepagehttps://github.com/vita-rust/std-newlib/tree/main/crates/vita-system-alloc-wrapper
repositoryhttps://github.com/vita-rust/std-newlib
max_upload_size
id869884
size3,421
Nikolay Arhipov (nikarh)

documentation

README

vita-system-alloc-wrapper

The standard library in Rust has a std::alloc::System allocator, which internally calls libc::malloc and libc::free for allocations. For PlayStation Vita, these functions are provided by newlib, which uses a preallocated pool for heap memory.

This should work and works for the majority of cases. Unfortunately though, with the -O3 optimization level compiled specifically for the PlayStation Vita target, it is possible for LLVM to incorrectly optimize libc::free calls, leading to crashes when heap-allocated structs are dropped in Rust.

This crate does a hack to fix that by providing a custom allocator, which proxies, all calls to std::alloc::System allocator, and wrapping dealloc call in std::hint::black_box, which prevents LLVM from doing any optimizations on libc::free call.

If you are experiencing crashes in Drop calls in seemingly correct code on PlayStation Vita, try adding this crate as a dependency, and registering provided allocator as a GlobalAllocator:

#[cfg(target_os = "vita")]
#[global_allocator]
static GLOBAL: vita_system_alloc_wrapper::SystemAllocWrapper = vita_system_alloc_wrapper::SystemAllocWrapper;
Commit count: 20

cargo fmt