| // Copyright 2026 The Bazel Authors. All rights reserved. | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| //! Test-only C FFI helpers for allocating and freeing Rust heap memory through | |
| //! the process-wide `TrackingAllocator`. | |
| use std::alloc::{alloc, dealloc, handle_alloc_error, Layout}; | |
| use std::ptr; | |
| /// Allocates `size` bytes with alignment `1` through the Rust global allocator | |
| /// (`TrackingAllocator`) and returns a non-null pointer that must be freed with | |
| /// [`bazel_tracking_allocator_test_free`] using the exact same `size`. | |
| /// | |
| /// Returns a null pointer if `size <= 0` or `size` exceeds `isize::MAX`. On | |
| /// out-of-memory, aborts via [`std::alloc::handle_alloc_error`]. Thread-safe | |
| /// and never unwinds across FFI; not safe as a critical FFM downcall. | |
| #[unsafe(no_mangle)] | |
| pub extern "C" fn bazel_tracking_allocator_test_alloc(size: i64) -> *mut u8 { | |
| if size <= 0 { | |
| return ptr::null_mut(); | |
| } | |
| let Ok(size) = usize::try_from(size) else { | |
| return ptr::null_mut(); | |
| }; | |
| let Ok(layout) = Layout::from_size_align(size, 1) else { | |
| return ptr::null_mut(); | |
| }; | |
| // SAFETY: `size > 0` (checked above) and `Layout::from_size_align(size, 1)` | |
| // guarantees a valid non-zero layout with `size <= isize::MAX`. | |
| let ptr = unsafe { alloc(layout) }; | |
| if ptr.is_null() { | |
| handle_alloc_error(layout); | |
| } | |
| ptr | |
| } | |
| /// Frees a block previously returned by [`bazel_tracking_allocator_test_alloc`] | |
| /// with the exact same `size`. | |
| /// | |
| /// Thread-safe and never unwinds across FFI; not safe as a critical FFM | |
| /// downcall. | |
| /// | |
| /// # Safety | |
| /// - If `ptr` is null or `size <= 0`, this function does nothing. | |
| /// - Otherwise, `ptr` must be a live pointer previously returned by | |
| /// [`bazel_tracking_allocator_test_alloc`] with the exact same `size`, and must not be freed more | |
| /// than once. | |
| #[unsafe(no_mangle)] | |
| pub unsafe extern "C" fn bazel_tracking_allocator_test_free(ptr: *mut u8, size: i64) { | |
| if ptr.is_null() || size <= 0 { | |
| return; | |
| } | |
| let Ok(size) = usize::try_from(size) else { | |
| return; | |
| }; | |
| let Ok(layout) = Layout::from_size_align(size, 1) else { | |
| return; | |
| }; | |
| // SAFETY: By this `unsafe extern "C" fn`'s `# Safety` contract, `ptr` is a | |
| // live pointer returned by `bazel_tracking_allocator_test_alloc` for | |
| // `Layout::from_size_align(size, 1)` and has not been freed before. | |
| unsafe { | |
| dealloc(ptr, layout); | |
| } | |
| } |