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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#![forbid(unsafe_code)]
#![doc(html_logo_url = "https://github.com/mxxo/plutonium/raw/master/pluto.png")]
//! Helping you make your programs less safe.
//!
//! You can learn more about `plutonium` at the [*Rust Security Advisory Database*](https://rustsec.org/advisories/RUSTSEC-2020-0011.html).
//!
//! ## Usage
//! Add `plutonium` to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! plutonium = "*"
//! ```
//!
//! and go:
//! ```
//! use plutonium::safe;
//!
//! #[safe]
//! fn super_safe(x: f32) -> i32 {
//!     std::mem::transmute::<f32, i32>(x)
//! }
//!
//! #[safe]
//! unsafe fn deref_null() {
//!     *std::ptr::null::<u8>();
//! }
//!
//! println!("{:?}", super_safe(1.0));
//! deref_null();
//! ```
//! ## Roadmap:
//! 1. Disable `#![forbid(unsafe_code)]`
//! 2. Add `#![forbid(safe_code)]` proc-macro lint

extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
    fold::Fold, parse_macro_input, parse_quote, Block, Expr, ExprUnsafe, ItemFn, Stmt, Token,
};

/// Turn unsafe code into "safe" code.
/// ```
/// use plutonium::safe;
///
/// #[safe]
/// fn a_very_safe_function() {
///     let num = 5;
///     let r1 = &num as *const i32;
///     println!("r1 is: {}", *r1);
/// }
///
/// #[safe]
/// unsafe fn an_even_more_safe_function() -> i32 {
///     1
/// }
///
/// a_very_safe_function();
/// println!("{}", an_even_more_safe_function());
/// ```
#[proc_macro_attribute]
pub fn safe(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(item as ItemFn);
    let mut safe_fn = input_fn.clone();

    if input_fn.sig.unsafety.is_some() {
        safe_fn.sig.unsafety = None;
    }
    safe_fn.block = Box::new(MakeFnBodyUnsafe.fold_block(*input_fn.block));

    quote!(#safe_fn).into()
}

struct MakeFnBodyUnsafe;

impl Fold for MakeFnBodyUnsafe {
    fn fold_block(&mut self, block: Block) -> Block {
        Block {
            brace_token: block.brace_token,
            stmts: vec![Stmt::Expr(Expr::Unsafe(ExprUnsafe {
                attrs: vec![parse_quote! { #[allow(unused_unsafe)] }],
                unsafe_token: Token!(unsafe)(block.brace_token.span),
                block,
            }))],
        }
    }
}

/// Apply extreme optimizations to your code (requires Rust 1.45 or later).
///
/// **Get stuff done** with the help of `optimize!`
/// ```
/// # use rand::Rng;
/// use plutonium::optimize;
///
/// macro_rules! qd_bench {
///     ($($tokens:tt)*) => {{
///         let start = std::time::Instant::now();
///         $($tokens)*;
///         start.elapsed()
///     }}
/// };
///
/// let mut vec = Vec::<i32>::with_capacity(1000);
/// for _ in 0..1000 {
///     vec.push(rand::thread_rng().gen_range(1, 101));
/// }
/// let mut vec2 = vec.clone();
///
/// let unoptimized_time = qd_bench!(
///     vec.sort()
/// );
/// let optimized_time = qd_bench!(
///     optimize!(vec2.sort())
/// );
///
/// assert!(optimized_time < unoptimized_time);
/// ```
#[proc_macro]
pub fn optimize(_tokens: TokenStream) -> TokenStream {
    TokenStream::new()
}

/// Imbue values with interesting properties.
///
/// Release mode is the most exciting way to use `#[unby]`.
/// ```no_run
/// use plutonium::unby;
///
/// #[unby]
/// fn enby() -> bool { 2 + 2 == 4 }
///
/// let mut x = 1;
///
/// if enby() { x = 2; }
/// if !enby() { x = 3; }
///
/// // neither true nor false
/// assert_eq!(x, 1);
/// ```
#[proc_macro_attribute]
pub fn unby(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut unby_fn = parse_macro_input!(item as ItemFn);
    unby_fn.block = Box::new(parse_quote! {{
        #[allow(invalid_value)]
        unsafe { std::mem::MaybeUninit::uninit().assume_init() }
    }});
    quote!(#unby_fn).into()
}

/// Fallthrough match arms
/// ```
/// # use plutonium::fallout;
/// #[fallout]
/// fn switch(x: i32) -> String {
///     let mut s = String::new();
///     match x {
///         1 => s += "1",
///         2 => s += "2",
///         _ => ( /* do nothing */ ),
///     }
///     s
/// }
/// assert_eq!(switch(1), "12".to_string());
/// ```
/// Use `breaks` to deconvolve match arms:
/// ```
/// # use plutonium::fallout;
/// #[fallout]
/// fn speaker(boxxx: Box<i32>) -> &'static str {
///     match *boxxx {
///         13 => { "13"; break; },
///         14 => "14",
///         _ => "lol",
///     }
/// }
/// assert_eq!(speaker(Box::new(13)), "13");
/// assert_eq!(speaker(Box::new(14)), "lol");
/// ```
///
/// ## Behold, the revenant:
/// ```
/// # use plutonium::fallout;
/// #[fallout]
/// fn send(from: *const i16, to: *mut i16, count: i32) {
///     let mut pos = from;
///     let n = (count + 7) / 8;
///     unsafe {
///         match count % 8 {
///             0 => { *to = *pos; pos = pos.add(1); },
///             7 => { *to = *pos; pos = pos.add(1); },
///             6 => { *to = *pos; pos = pos.add(1); },
///             5 => { *to = *pos; pos = pos.add(1); },
///             4 => { *to = *pos; pos = pos.add(1); },
///             3 => { *to = *pos; pos = pos.add(1); },
///             2 => { *to = *pos; pos = pos.add(1); },
///             1 => { *to = *pos; pos = pos.add(1); },
///             _ => (),
///         }
///         for _ in (1..n).rev() {
///             *to=*pos;   pos   =  pos.add(1);  *to=*pos;pos     =pos.add(1);
///             *to    =*   pos   ;  pos          =pos         .add(1);
///             *to    =*   pos   ;  pos=pos.add  (1);*to=*pos   ;pos=pos.add(1);
///             *to    =*   pos   ;  pos          =pos                 .add(1);
///             *to    =*   pos   ;  pos          =pos             .add(1);
///             *to = *pos ;pos =    pos          .add         (1);
///         }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn fallout(_attr: TokenStream, item: TokenStream) -> TokenStream {
    if let Ok(mut fallout_fn) = syn::parse::<ItemFn>(item.clone()) {
        fallout_fn.block.stmts = fallthrough_stmts(&fallout_fn.block.stmts);
        return quote!(#fallout_fn).into()
    }
    item
}

fn fallthrough_stmts(stmts: &Vec<Stmt>) -> Vec<Stmt> {
    let mut fallthru_stmts = Vec::with_capacity(stmts.len());
    for stmt in stmts {
        match stmt {
            Stmt::Local(_) | Stmt::Item(_) => fallthru_stmts.push(stmt.clone()),
            Stmt::Expr(expr) => fallthru_stmts.push(Stmt::Expr(fallthrough_expr(expr))),
            Stmt::Semi(expr, semi) => fallthru_stmts.push(Stmt::Semi(fallthrough_expr(expr), *semi)),
        }
    };
    fallthru_stmts
}

fn fallthrough_expr(expr: &syn::Expr) -> syn::Expr {
    // skip anything other than top level matches for now
    match expr {
        Expr::Match(m) => {
            let mut arm_masher = FallThru { arm_exprs: Vec::new() };
            let mut mashed_arms: Vec<_> = m.arms.iter().rev().map(|arm| arm_masher.fold_arm(arm.clone())).collect();
            Expr::Match(syn::ExprMatch {
                arms: { mashed_arms.reverse(); mashed_arms },
                ..m.clone()
            })
        },
        _ => expr.clone()
    }
}

struct FallThru {
    arm_exprs: Vec<syn::Expr>,
}

impl Fold for FallThru {
    fn fold_arm(&mut self, mut arm: syn::Arm) -> syn::Arm {
        let (breakless_body, arm_ending) = FallThru::parse_arm(arm.body);
        if let ArmEnd::Break = arm_ending {
            self.arm_exprs.clear();
        }
        self.arm_exprs.push(*breakless_body);
        arm.body = self.as_arm_body();
        arm
    }
}

#[derive(Debug, Clone, Copy)]
enum ArmEnd { Break, FallThru }

impl FallThru {
    fn as_arm_body(&self) -> Box<syn::Expr> {
        if self.arm_exprs.len() == 0 {
            panic!("arm exprs is empty");
        }
        // we start at the bottom and walk upwards, so the first statement in the
        // vector is the bottom-most in the match
        let mut stmts: Vec<syn::Stmt> = Vec::with_capacity(self.arm_exprs.len());
        for i in 0..self.arm_exprs.len() {
            if i == 0 {
                stmts.push(syn::Stmt::Expr(self.arm_exprs[i].clone()));
            } else {
                stmts.push(syn::Stmt::Semi(
                    self.arm_exprs[i].clone(),
                    parse_quote!(;),
                ));
            }
        }
        stmts.reverse();
        Box::new(syn::Expr::Block (
            syn::ExprBlock {
                attrs: Vec::new(),
                label: None,
                block: Block {
                    brace_token: syn::token::Brace { span: Span::call_site() },
                    stmts
                },
            }
        ))
    }

    fn parse_arm(expr: Box<syn::Expr>) -> (Box<syn::Expr>, ArmEnd) {
        match *expr {
            Expr::Break(_) => (Box::new(parse_quote!{()}), ArmEnd::Break),
            Expr::Block(mut block_expr) => {
                match block_expr.block.stmts.last() {
                    Some(syn::Stmt::Expr(Expr::Break(_)))
                    | Some(syn::Stmt::Semi(Expr::Break(_), _)) => {
                        let _ = block_expr.block.stmts.pop();
                        // remove semicolon from second-last statement
                        match block_expr.block.stmts.pop() {
                            Some(syn::Stmt::Semi(expr, _)) => {
                                block_expr.block.stmts.push(syn::Stmt::Expr(expr));
                            },
                            // push non-semis back into the vec
                            Some(other) => block_expr.block.stmts.push(other),
                            None => {},
                        }
                        (Box::new(Expr::Block(block_expr)), ArmEnd::Break)
                    },
                    _ => (Box::new(Expr::Block(block_expr)), ArmEnd::FallThru),
                }
            },
            other => (Box::new(other), ArmEnd::FallThru),
        }
    }
}