1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![feature(allocator_api, maybe_uninit)]

extern crate libc;
extern crate time;

use libc::c_void;
use std::alloc::{Alloc, AllocErr, GlobalAlloc, Layout, System};
use std::fs::File;
use std::mem;
use std::os::unix::io::AsRawFd;
use std::process;
use std::ptr::{null_mut, NonNull};
use std::sync::atomic::{AtomicBool, Ordering};

static mut TRACE_FD: i32 = -1;
static TRACE_ACTIVATE: AtomicBool = AtomicBool::new(false);

pub struct Allocator {}

impl Allocator {
    pub fn new() -> Allocator {
        Allocator {}
    }

    pub fn initialize(f: &File) {
        unsafe {
            TRACE_FD = f.as_raw_fd();
        }
    }

    pub fn write_to_stdout() {
        unsafe {
            TRACE_FD = 0;
        }
    }

    pub fn write_to_stderr() {
        unsafe {
            TRACE_FD = 1;
        }
    }

    pub fn activate() {
        TRACE_ACTIVATE.store(true, Ordering::Relaxed);
    }

    pub fn deactivate() {
        TRACE_ACTIVATE.store(false, Ordering::Relaxed);
    }
}

fn to_hex(i: u8) -> u8 {
    if i > 15 {
        process::exit(42);
    }

    if i < 10 {
        48 + i
    } else {
        (i - 10) + 65
    }
}

enum Action {
    Allocating,
    Deallocating,
}

unsafe fn print_size(address: usize, size: usize, action: Action) {
    if TRACE_FD != -1 && TRACE_ACTIVATE.load(Ordering::Relaxed) {
        let mut buf: [u8; 100] = mem::MaybeUninit::uninit().assume_init();
        let psz = mem::size_of::<usize>();
        let shr = (psz as u32) * 8 - 8;

        let mut index = 0;
        let mut counter = 8;

        let c = time::precise_time_ns() as usize;

        loop {
            if counter == 0 {
                break;
            }

            let current = ((c.overflowing_shl(64 - 8 * counter as u32).0)
                .overflowing_shr(shr)
                .0) as u8;
            buf[index] = to_hex(current >> 4);
            buf[index + 1] = to_hex((current << 4) >> 4);

            counter -= 1;
            index += 2;
        }

        buf[index] = b' ';

        index += 1;

        match action {
            Action::Allocating => buf[index] = b'A',
            Action::Deallocating => buf[index] = b'D',
        };

        buf[index + 1] = b' ';

        index += 2;
        counter = 8;

        let c = address;

        loop {
            if counter == 0 {
                break;
            }

            let current = ((c.overflowing_shl(64 - 8 * counter as u32).0)
                .overflowing_shr(shr)
                .0) as u8;

            buf[index] = to_hex(current >> 4);
            buf[index + 1] = to_hex((current << 4) >> 4);

            counter -= 1;
            index += 2;
        }

        buf[index] = b' ';

        index += 1;
        counter = 8;

        loop {
            if counter == 0 {
                break;
            }

            let current = ((size.overflowing_shl(64 - 8 * counter as u32).0)
                .overflowing_shr(shr)
                .0) as u8;

            buf[index] = to_hex(current >> 4);
            buf[index + 1] = to_hex((current << 4) >> 4);

            counter -= 1;
            index += 2;
        }
        buf[index] = b'\n';

        libc::write(TRACE_FD, buf.as_ptr() as *const c_void, index + 1);
    }
}

unsafe impl<'a> Alloc for &'a Allocator {
    unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
        let size = layout.size();
        let res = System.alloc(layout);

        print_size(res as usize, size, Action::Allocating);

        Ok(NonNull::new(res).unwrap())
    }

    unsafe fn dealloc(&mut self, ptr: std::ptr::NonNull<u8>, layout: Layout) {
        let ptr = ptr.as_ptr();

        print_size(ptr as usize, layout.size(), Action::Deallocating);

        System.dealloc(ptr, layout)
    }
}

unsafe impl GlobalAlloc for Allocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let size = layout.size();
        let ptr = System.alloc(layout);

        print_size(ptr as usize, size, Action::Allocating);

        ptr
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        print_size(ptr as usize, layout.size(), Action::Deallocating);

        System.dealloc(ptr, layout)
    }
}