Skip to content

zyedidia/transmute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Transmute without Unsafe!

unsafe forbidden badge

This crate allows you to convert any type to any other type without the use of unsafe (thanks to the genius idea presented here: rust-lang/rust#57893 (comment). Update: for compatibility with the next-generation trait solver, this library now makes use of this issue instead: rust-lang/rust#133361).

It provides one function, implemented entirely in Safe Rust:

fn transmute<T, U>(x: T) -> U

Usable in projects that require dependencies to use #![forbid(unsafe_code)].

Never fight the borrow checker again!

All versions of Rust since 1.0 are supported.

Examples:

Sometimes you want to dereference a null pointer. This crate allows you to do so without any unsafe code.

let p = core::ptr::null_mut();
let x = transmute::<*mut i64, &'static i64>(p);
println!("{}", x); // BOOM

Sometimes you want to grow an array, but don't want to allocate more space. Thanks to modern technology, this is now possible to do safely™. Just steal the nearby memory with transmute.

let p = [0];
let mut x = transmute::<[u64; 1], [u64; 100]>(p);
// this slice now has 100 elements: yay so much space for our bytes
assert!(x.len() == 100);
x[50] = 42;

⚠️⚠️⚠️ WARNING ⚠️⚠️⚠️

THIS CRATE IS NOT SAFE.

This crate abuses a bug in the Rust compiler (rust-lang/rust#57893) to circumvent the Rust type system.

Please do not actually use this.

Moral of the story: you cannot rely solely on Rust for supply chain security or isolation against an adversary. If you need adversarially-secure isolation, you should use sandboxing tools such as LFI or WebAssembly, or use hardware-based isolation via multiple processes or virtualization.

About

Never fight the borrow checker again!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages