Problem Description
The current design of the Allocator API relies heavily on explicit parameterization (e.g., Vec<T, A = Global>). While this works well for isolated or top-level applications, it creates a massive ecosystem bottleneck due to viral propagation.
For example, suppose I want to write a secure application where cryptographic tokens must be stored on a memory page configured with mlock and MADV_DONTDUMP. Under the current paradigm:
- I create a custom
SecureAllocator.
- To use high-level libraries like
jsonwebtoken, that crate must be explicitly rewritten to accept custom allocators for its internal data structures.
- Furthermore,
jsonwebtoken's cryptographic backends (and their dependencies) must also explicitly support and forward the allocator parameter.
If a single crate in this dependency chain does not support the Allocator API or fails to forward the generic allocator parameter down the line, the secure memory guarantee is broken, as allocations revert to the Global allocator. Expecting the entire Rust ecosystem to update their signatures to accommodate generic allocators is impractical and likely to fail.
Proposed Alternative: Scoped/Thread-Local Allocator Contexts
Instead of threading an allocator explicitly through every struct and function signature down the call stack, I want to propose a solution handled by the compiler/language. This would provide a mechanism to temporarily override or wrap the allocator for a specific execution scope, dictating that everything down the line uses it.
Conceptually, this would behave exactly as if we have changed the #[global_allocator] exclusively for that specific section of code.
This could look like a scoped context or wrapper function:
// Concept: Overriding the allocator down the entire call-stack line
rust::alloc::with_allocator(SecureAllocator, || {
// Everything executed inside this closure—including third-party crates
// that know nothing about SecureAllocator—defaults to using it instead of Global.
let token = jsonwebtoken::decode::<Claims>(...);
});
Problem Description
The current design of the Allocator API relies heavily on explicit parameterization (e.g.,
Vec<T, A = Global>). While this works well for isolated or top-level applications, it creates a massive ecosystem bottleneck due to viral propagation.For example, suppose I want to write a secure application where cryptographic tokens must be stored on a memory page configured with
mlockandMADV_DONTDUMP. Under the current paradigm:SecureAllocator.jsonwebtoken, that crate must be explicitly rewritten to accept custom allocators for its internal data structures.jsonwebtoken's cryptographic backends (and their dependencies) must also explicitly support and forward the allocator parameter.If a single crate in this dependency chain does not support the Allocator API or fails to forward the generic allocator parameter down the line, the secure memory guarantee is broken, as allocations revert to the
Globalallocator. Expecting the entire Rust ecosystem to update their signatures to accommodate generic allocators is impractical and likely to fail.Proposed Alternative: Scoped/Thread-Local Allocator Contexts
Instead of threading an allocator explicitly through every struct and function signature down the call stack, I want to propose a solution handled by the compiler/language. This would provide a mechanism to temporarily override or wrap the allocator for a specific execution scope, dictating that everything down the line uses it.
Conceptually, this would behave exactly as if we have changed the #[global_allocator] exclusively for that specific section of code.
This could look like a scoped context or wrapper function: