#attributes #obfuscation #security #macro #debugging

macro no-std random_struct_layout

The custom attributes to randomize struct members layout

5 unstable releases

0.3.2 May 11, 2023
0.3.1 May 11, 2023
0.2.1 May 11, 2023
0.1.1 Aug 13, 2022
0.1.0 Aug 13, 2022

#925 in Procedural macros

49 downloads per month

MIT license

7KB
87 lines

random_struct_layout

This crate provides custom attributes to randomize struct layout, like gcc's Randomizing structure layout(https://lwn.net/Articles/722293/).

Example

use offset;
use random_struct_layout::layout_randomize;

#[macro_use]
extern crate random_struct_layout;

#[layout_randomize(Debug)]
struct Data {
    a: i32,
    b: i32,
    c: i32,
    d: i64,
    e: i64,
}

fn main() {
    let data = Data {
        a: 0x10,
        b: 0x20,
        c: 0x30,
        d: 0x30,
        e: 0x40,
    };

    println!("{}", offset::offset_of!(Data::a));
    println!("{}", offset::offset_of!(Data::b));
    println!("{}", offset::offset_of!(Data::c));
    println!("{}", offset::offset_of!(Data::d));
    println!("{}", offset::offset_of!(Data::e));

    /* example output
    20
    16
    0
    8
    24
    */

    println!("{:x?}", data); // Debug print order is same as normal one.
                             // Data { a: 10, b: 20, c: 30, d: 30, e: 40 }
}

#[layout_randomize]
struct HasDst {
    value1: i32,
    value2: i32,

    #[dst]
    value3: [u8], // support dst type.
}

Dependencies

~1.5MB
~40K SLoC