Skip to content

Commit

Permalink
Merge pull request #2522 from serde-rs/leak
Browse files Browse the repository at this point in the history
Leak all memory allocated during macro expansion
  • Loading branch information
dtolnay authored Jul 22, 2023
2 parents 22be673 + 50e2f4b commit 27414c9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions precompiled/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@ extern crate proc_macro2;

use proc_macro2::watt;
use proc_macro2::watt::buffer::InputBuffer;
use std::alloc::{GlobalAlloc, Layout, System};
use std::io::{self, Read, Write};
use std::sync::atomic::Ordering;

struct MonotonicAllocator;

#[global_allocator]
static ALLOCATOR: MonotonicAllocator = MonotonicAllocator;

unsafe impl GlobalAlloc for MonotonicAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
System.alloc(layout)
}

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
// Leak: this cuts 3% of code size from the precompiled macro binary.
// There is no way that serde_derive would fill up all memory on the
// host. When the subprocess exits, operating system will clean this up.
}
}

fn main() {
let mut buf = Vec::new();
io::stdin().read_to_end(&mut buf).unwrap();
Expand Down

0 comments on commit 27414c9

Please sign in to comment.