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
#![feature(proc_macro_diagnostic)]

//! Macro crate for `binary_enclave`.

use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::spanned::Spanned;
use syn::{parse_macro_input, GenericArgument, ItemStatic, PathArguments, Type};

/// setup required linker options and trait impls
///
/// this puts in to place whats required for our enclave. Without
/// it, it will not be locatable for writing to after compilation.
/// Some systems may limit the length of section names. It is
/// recommended to keep the section name short and simple.
///
/// ```
/// #[enclave(binary_section_name)]
/// pub static OUR_STATIC: Enclave<ConfStruct, 128> = Enclave::new()
/// ```
#[proc_macro_attribute]
pub fn enclave(attr: TokenStream, item: TokenStream) -> TokenStream {
    let item: ItemStatic = parse_macro_input!(item as ItemStatic);

    let (link_section, section) = if cfg!(target_os = "linux") {
        (format!(".{}", attr), format!(".{}", attr))
    } else if cfg!(target_os = "macos") {
        (format!("__DATA,__{}", attr), format!("__{}", attr))
    } else {
        ("".to_string(), "".to_string())
    };

    let segment = match item.ty.as_ref() {
        Type::Path(path) => {
            let seg = &path.path.segments;
            seg.first().unwrap().clone()
        }
        _ => {
            item.ty
                .span()
                .unwrap()
                .error("not sure how to handle this type")
                .emit();
            return TokenStream::new();
        }
    };

    if segment.ident != Ident::new("Enclave", Span::call_site()) {
        item.ty
            .span()
            .unwrap()
            .error("enclave must be of type Enclave");
        return TokenStream::new();
    }

    let generics = match segment.arguments {
        PathArguments::AngleBracketed(gens) => gens,
        _ => return TokenStream::new(),
    };

    let ty = match generics.args.first().unwrap() {
        GenericArgument::Type(ty) => ty,
        _ => return TokenStream::new(),
    };

    let output = quote! {
        #[no_mangle]
        #[link_section = #link_section]
        #item

        impl binary_enclave::EnclaveLocator for #ty {
            const SECTION: &'static str = #section;
        }
    };

    TokenStream::from(output)
}