| Crates.io | stackaroo |
| lib.rs | stackaroo |
| version | 1.2.0 |
| created_at | 2025-11-06 20:55:02.771262+00 |
| updated_at | 2025-11-17 19:56:22.270635+00 |
| description | Swap out of the OS-provided stack |
| homepage | |
| repository | https://github.com/Palkovsky/stackaroo |
| max_upload_size | |
| id | 1920484 |
| size | 77,737 |
Nice, long knife to stab yourself in the eye
stackaroo is a highly unsafe library, letting you swap out the OS-provided thread stack with your own custom stack (e.g. heap-backed).
This library was created as part of research into Linux kernel internals. It provides platform-specific assembly implementations (x86_64 and AArch64) to perform direct stack pointer manipulation, allowing you to:
use stackaroo::swap_to_heap;
struct Args {
n: u64,
result: u64,
}
fn fibonacci(args: &mut Args) {
fn fib(n: u64) -> u64 {
let huge_array = [0u8; 1024 * 1024]; // 1MB per frame
std::hint::black_box(&huge_array); // Don't let it be compiled-out
if n <= 1 { return n; }
fib(n - 1) + fib(n - 2)
}
args.result = fib(args.n);
}
fn main() {
unsafe {
const STACK_SIZE: usize = 1 << 30; // 1GB
let mut args = Args { n: 32, result: 0 };
swap_to_heap(fibonacci, Some(&mut args), STACK_SIZE).unwrap();
println!("Fibonacci({}) = {}", args.n, args.result);
}
}
This is a Cargo workspace containing two crates:
stackaroo)no_std compatiblestd/tls) or one swap globally (no-std)Available features:
std (default): Enables standard library support (implies alloc)tls (default): Enables thread-local storage for thread-safe stack swapping (requires std)alloc: Enables heap allocation support (required for swap_to_heap)stackaroo-ffi)stackaroo.h) automatically via cbindgenAvailable features:
std (default): Enables standard library supportalloc: Enables heap allocation supportTo use stackaroo from C or C++, build the stackaroo-ffi crate:
cd stackaroo-ffi
cargo build --release
This will generate:
target/release/libstackaroo.a (static library)target/release/stackaroo.dll / .so / .dylib (dynamic library)stackaroo.h (C header file in the stackaroo-ffi directory)Example C usage:
#include "stackaroo.h"
#include <stdio.h>
#include <stdlib.h>
void my_function(void* arg) {
int* value = (int*)arg;
*value *= 2;
printf("Value doubled: %d\n", *value);
}
int main() {
// Allocate 1MB stack
size_t stack_size = 1024 * 1024;
void* stack = aligned_alloc(16, stack_size);
void* stack_top = (char*)stack + stack_size;
int value = 42;
StackarooError err = stackaroo_swap_to(my_function, &value, stack_top);
if (err == STACKAROOERROR_OK) {
printf("Success! Value is now: %d\n", value);
}
free(stack);
return 0;
}
Link with: -lstackaroo (and ensure the library path is in your linker search path)
This library is extremely unsafe and should only be used by people who understand the implications of manual stack management. It's primarily intended for research, kernel development, and testing scenarios where you need to bypass OS stack limitations.
This project is licensed under the Apache License 2.0.