Crates.io | vita-system-alloc-wrapper |
lib.rs | vita-system-alloc-wrapper |
version | 0.1.0 |
source | src |
created_at | 2023-05-21 10:39:30.598621 |
updated_at | 2023-05-21 10:39:30.598621 |
description | A proxy for std::alloc::System which wraps dealloc in std::hint::black_box |
homepage | https://github.com/vita-rust/std-newlib/tree/main/crates/vita-system-alloc-wrapper |
repository | https://github.com/vita-rust/std-newlib |
max_upload_size | |
id | 869884 |
size | 3,421 |
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;