vasi_macro/lib.rs
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
// https://github.com/rust-lang/rfcs/blob/master/text/2585-unsafe-block-in-unsafe-fn.md
#![deny(unsafe_op_in_unsafe_fn)]
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Attribute, GenericParam, Generics, Type};
/// Implement `vasi::VirtualAddressSpaceIndependent` for the annotated type.
/// Requires all fields to implement `vasi::VirtualAddressSpaceIndependent`.
///
/// An empty struct fails becase Rust doesn't consider fieldless structs to be
/// FFI-safe:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[derive(VirtualAddressSpaceIndependent)]
/// #[repr(C)]
/// struct Foo {}
/// ```
///
/// FFI-safe structs containing only `VirtualAddressSpaceIndependent`
/// fields qualify:
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// x: i32,
/// }
/// ```
///
/// `#[repr(transparent)]` is OK too.
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(transparent)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// x: i32,
/// }
/// ```
///
/// A struct containing a *reference* doesn't qualify:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo<'a> {
/// x: &'a i32,
/// }
/// ```
///
/// A struct containing a [Box] doesn't qualify:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// x: Box<i32>,
/// }
/// ```
///
/// A struct containing a *pointer* doesn't qualify:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// x: *const i32,
/// }
/// ```
///
/// A field can be allow-listed with the attribute `unsafe_assume_virtual_address_space_independent`:
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// // SAFETY: we ensure the pointer isn't dereferenced
/// // outside of its original virtual address space.
/// #[unsafe_assume_virtual_address_space_independent]
/// x: *const i32,
/// }
/// ```
///
/// A union containing only `VirtualAddressSpaceIndependent` fields qualifies:
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// union Foo {
/// x: i32,
/// y: i32,
/// }
/// ```
///
/// A union containing a non-vasi member doesn't qualify:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct Foo {
/// x: i32,
/// y: *const i32,
/// }
/// ```
///
/// An enum containing only `VirtualAddressSpaceIndependent` variants qualifies:
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// enum Foo {
/// Bar(i32),
/// Baz(i32),
/// }
/// ```
///
/// An enum containing a non-vasi variant doesn't qualify:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// enum Foo {
/// Bar(i32),
/// Baz(*const i32),
/// }
/// ```
///
/// A generic type *conditionally* implements VirtualAddressSpaceIndependent,
/// if its type parameters do (as the derive macros in the std crate behave).
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct MyWrapper<T> {
/// val: T,
/// }
///
/// static_assertions::assert_impl_all!(MyWrapper<i32>: vasi::VirtualAddressSpaceIndependent);
/// static_assertions::assert_not_impl_all!(MyWrapper<* const i32>: vasi::VirtualAddressSpaceIndependent);
/// ```
///
/// Generic type with existing bounds are also supported.
/// ```
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct MyWrapper<T: Copy> {
/// val: T,
/// }
/// static_assertions::assert_impl_all!(MyWrapper<i32>: vasi::VirtualAddressSpaceIndependent);
/// static_assertions::assert_not_impl_all!(MyWrapper<* const i32>: vasi::VirtualAddressSpaceIndependent);
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct MyWrapper2<T> where T: Copy {
/// val: T,
/// }
/// static_assertions::assert_impl_all!(MyWrapper2<i32>: vasi::VirtualAddressSpaceIndependent);
/// static_assertions::assert_not_impl_all!(MyWrapper2<* const i32>: vasi::VirtualAddressSpaceIndependent);
/// ```
///
/// As with e.g. Copy and Clone, a field that is dependent on a type parameter
/// but still isn't VirtualAddressSpaceIndependent will cause the macro not to
/// compile:
/// ```compile_fail
/// use vasi::VirtualAddressSpaceIndependent;
///
/// #[repr(C)]
/// #[derive(VirtualAddressSpaceIndependent)]
/// struct MyWrapper<T> {
/// val: *const T,
/// }
/// ```
#[proc_macro_derive(
VirtualAddressSpaceIndependent,
attributes(unsafe_assume_virtual_address_space_independent)
)]
pub fn derive_virtual_address_space_independent(
tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = syn::parse(tokens).unwrap();
// Build the trait implementation
impl_derive_virtual_address_space_independent(ast)
}
// Add a bound `T: VirtualAddressSpaceIndependent` to every type parameter T.
fn add_trait_bounds(mut generics: Generics) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(ref mut type_param) = *param {
type_param
.bounds
.push(parse_quote!(vasi::VirtualAddressSpaceIndependent));
}
}
generics
}
fn assume_vasi(attrs: &[Attribute]) -> bool {
attrs.iter().any(|attr| {
attr.path()
.is_ident("unsafe_assume_virtual_address_space_independent")
})
}
fn impl_derive_virtual_address_space_independent(ast: syn::DeriveInput) -> proc_macro::TokenStream {
let name = &ast.ident;
// This will contain calls to a function `check` that accepts VirtualAddressSpaceIndependent types,
// which is how we validate that the fields are VirtualAddressSpaceIndependent.
// e.g. for an input struct definition
// ```
// struct MyStruct {
// x: u32,
// y: i32,
// }
// ```
//
// We'll end up generating code like:
// ```
// impl VirtualAddressSpaceIndependent for MyStruct {
// const IGNORE: () = {
// fn check<T: VirtualAddressSpaceIndependent>() {}
// check::<u32>(); // check type of MyStruct::x
// check::<i32>(); // check type of MyStruct::y
// };
// }
// ```
let types: Vec<&Type> = match &ast.data {
syn::Data::Struct(s) => s
.fields
.iter()
.filter(|field| !assume_vasi(&field.attrs))
.map(|field| &field.ty)
.collect(),
syn::Data::Enum(e) => e
.variants
.iter()
.flat_map(|variant| {
variant
.fields
.iter()
.filter(|field| !assume_vasi(&field.attrs))
.map(|field| &field.ty)
})
.collect(),
syn::Data::Union(u) => u
.fields
.named
.iter()
.filter(|field| !assume_vasi(&field.attrs))
.map(|field| &field.ty)
.collect(),
};
// These will fail to compile if any of the types aren't VirtualAddressSpaceIndependent.
let calls_to_check: TokenStream = types
.into_iter()
.map(|ty| quote! {check::<#ty>();})
.collect();
// Add a bound `T: VirtualAddressSpaceIndependent` to every type parameter T.
// This allows generic types to be conditionally VirtualAddressSpaceIndependent,
// iff their type parameters are.
let generics = add_trait_bounds(ast.generics);
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let gen = quote! {
unsafe impl #impl_generics vasi::VirtualAddressSpaceIndependent for #name #ty_generics #where_clause {
const IGNORE: () = {
const fn check<T: ::vasi::VirtualAddressSpaceIndependent>() {}
#calls_to_check
};
}
#[deny(improper_ctypes_definitions)]
const _: () = {
// Force compilation to fail if the type isn't FFI safe.
extern "C" fn _vasi_validate_ffi_safe #impl_generics (_: #name #ty_generics) #where_clause {}
};
};
gen.into()
}