Skip to content

Commit d2c491f

Browse files
committed
opt: optee-utee: macros: update the version of syn
1 parent 5ae842d commit d2c491f

File tree

4 files changed

+97
-101
lines changed

4 files changed

+97
-101
lines changed

optee-utee/macros/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ edition = "2018"
2828
proc-macro = true
2929

3030
[dependencies]
31-
quote = "0.6"
32-
syn = { version = "0.15", features = ["full"] }
31+
quote = "1.0"
32+
syn = { version = "2.0", features = ["full"] }
3333
# The newer versions of these crates require rustc 1.81, while our custom
3434
# patched STD version is 1.80, causing compatibility issues. To resolve this,
3535
# we have to set the crates to an exact compatible version.

optee-utee/macros/src/lib.rs

Lines changed: 87 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
extern crate alloc;
1919
extern crate proc_macro;
2020

21-
#[cfg(not(feature = "std"))]
22-
use alloc::vec::Vec;
2321
use proc_macro::TokenStream;
2422
use quote::quote;
2523
use syn::parse_macro_input;
@@ -30,21 +28,22 @@ use syn::spanned::Spanned;
3028
/// # Examples
3129
///
3230
/// ``` no_run
33-
/// #[ta_crate]
34-
/// fn ta_crate() -> Result<()> { }
31+
/// #[ta_create]
32+
/// fn ta_create() -> Result<()> { }
3533
/// ```
3634
#[proc_macro_attribute]
3735
pub fn ta_create(_args: TokenStream, input: TokenStream) -> TokenStream {
3836
let f = parse_macro_input!(input as syn::ItemFn);
39-
let ident = &f.ident;
37+
let f_sig = &f.sig;
38+
let f_ident = &f_sig.ident;
4039

4140
// check the function signature
42-
let valid_signature = f.constness.is_none()
41+
let valid_signature = f_sig.constness.is_none()
4342
&& matches!(f.vis, syn::Visibility::Inherited)
44-
&& f.abi.is_none()
45-
&& f.decl.inputs.is_empty()
46-
&& f.decl.generics.where_clause.is_none()
47-
&& f.decl.variadic.is_none();
43+
&& f_sig.abi.is_none()
44+
&& f_sig.inputs.is_empty()
45+
&& f_sig.generics.where_clause.is_none()
46+
&& f_sig.variadic.is_none();
4847

4948
if !valid_signature {
5049
return syn::parse::Error::new(
@@ -58,7 +57,7 @@ pub fn ta_create(_args: TokenStream, input: TokenStream) -> TokenStream {
5857
quote!(
5958
#[no_mangle]
6059
pub extern "C" fn TA_CreateEntryPoint() -> optee_utee_sys::TEE_Result {
61-
match #ident() {
60+
match #f_ident() {
6261
Ok(_) => optee_utee_sys::TEE_SUCCESS,
6362
Err(e) => e.raw_code()
6463
}
@@ -80,16 +79,17 @@ pub fn ta_create(_args: TokenStream, input: TokenStream) -> TokenStream {
8079
#[proc_macro_attribute]
8180
pub fn ta_destroy(_args: TokenStream, input: TokenStream) -> TokenStream {
8281
let f = parse_macro_input!(input as syn::ItemFn);
83-
let ident = &f.ident;
82+
let f_sig = &f.sig;
83+
let f_ident = &f_sig.ident;
8484

8585
// check the function signature
86-
let valid_signature = f.constness.is_none()
86+
let valid_signature = f_sig.constness.is_none()
8787
&& matches!(f.vis, syn::Visibility::Inherited)
88-
&& f.abi.is_none()
89-
&& f.decl.inputs.is_empty()
90-
&& f.decl.generics.where_clause.is_none()
91-
&& f.decl.variadic.is_none()
92-
&& matches!(f.decl.output, syn::ReturnType::Default);
88+
&& f_sig.abi.is_none()
89+
&& f_sig.inputs.is_empty()
90+
&& f_sig.generics.where_clause.is_none()
91+
&& f_sig.variadic.is_none()
92+
&& matches!(f_sig.output, syn::ReturnType::Default);
9393

9494
if !valid_signature {
9595
return syn::parse::Error::new(
@@ -103,7 +103,7 @@ pub fn ta_destroy(_args: TokenStream, input: TokenStream) -> TokenStream {
103103
quote!(
104104
#[no_mangle]
105105
pub extern "C" fn TA_DestroyEntryPoint() {
106-
#ident();
106+
#f_ident()
107107
}
108108

109109
#f
@@ -128,15 +128,16 @@ pub fn ta_destroy(_args: TokenStream, input: TokenStream) -> TokenStream {
128128
#[proc_macro_attribute]
129129
pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
130130
let f = parse_macro_input!(input as syn::ItemFn);
131-
let ident = &f.ident;
131+
let f_sig = &f.sig;
132+
let f_ident = &f_sig.ident;
132133

133134
// check the function signature
134-
let valid_signature = f.constness.is_none()
135+
let valid_signature = f_sig.constness.is_none()
135136
&& matches!(f.vis, syn::Visibility::Inherited)
136-
&& f.abi.is_none()
137-
&& (f.decl.inputs.len() == 1 || f.decl.inputs.len() == 2)
138-
&& f.decl.generics.where_clause.is_none()
139-
&& f.decl.variadic.is_none();
137+
&& f_sig.abi.is_none()
138+
&& (f_sig.inputs.len() == 1 || f_sig.inputs.len() == 2)
139+
&& f_sig.generics.where_clause.is_none()
140+
&& f_sig.variadic.is_none();
140141

141142
if !valid_signature {
142143
return syn::parse::Error::new(
@@ -147,16 +148,16 @@ pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
147148
.into();
148149
}
149150

150-
match f.decl.inputs.len() {
151+
match f_sig.inputs.len() {
151152
1 => quote!(
152153
#[no_mangle]
153154
pub extern "C" fn TA_OpenSessionEntryPoint(
154-
param_types: u32,
155-
params: &mut [optee_utee_sys::TEE_Param; 4],
156-
sess_ctx: *mut *mut c_void,
155+
param_types: optee_utee::RawParamTypes,
156+
params: &mut optee_utee::RawParams,
157+
_: *mut *mut core::ffi::c_void,
157158
) -> optee_utee_sys::TEE_Result {
158159
let mut parameters = Parameters::from_raw(params, param_types);
159-
match #ident(&mut parameters) {
160+
match #f_ident(&mut parameters) {
160161
Ok(_) => optee_utee_sys::TEE_SUCCESS,
161162
Err(e) => e.raw_code()
162163
}
@@ -167,32 +168,23 @@ pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
167168
.into(),
168169

169170
2 => {
170-
let input_types: Vec<_> = f
171-
.decl
172-
.inputs
173-
.iter()
174-
.map(|arg| match arg {
175-
syn::FnArg::Captured(val) => &val.ty,
176-
_ => unreachable!(),
177-
})
178-
.collect();
179-
let ctx_type = match input_types.last().unwrap() {
180-
syn::Type::Reference(r) => &r.elem,
181-
_ => unreachable!(),
171+
let ctx_type = match extract_fn_arg_mut_ref_type(&f_sig.inputs[1]) {
172+
Ok(v) => v,
173+
Err(e) => return e.to_compile_error().into(),
182174
};
183175

184176
quote!(
185177
// To eliminate the clippy error: this public function might dereference a raw pointer but is not marked `unsafe`
186178
// we just expand the unsafe block, but the session-related macros need refactoring in the future
187179
#[no_mangle]
188180
pub unsafe extern "C" fn TA_OpenSessionEntryPoint(
189-
param_types: u32,
190-
params: &mut [optee_utee_sys::TEE_Param; 4],
191-
sess_ctx: *mut *mut c_void,
181+
param_types: optee_utee::RawParamTypes,
182+
params: &mut optee_utee::RawParams,
183+
sess_ctx: *mut *mut core::ffi::c_void,
192184
) -> optee_utee_sys::TEE_Result {
193185
let mut parameters = Parameters::from_raw(params, param_types);
194186
let mut ctx: #ctx_type = Default::default();
195-
match #ident(&mut parameters, &mut ctx) {
187+
match #f_ident(&mut parameters, &mut ctx) {
196188
Ok(_) =>
197189
{
198190
*sess_ctx = Box::into_raw(Box::new(ctx)) as _;
@@ -225,16 +217,17 @@ pub fn ta_open_session(_args: TokenStream, input: TokenStream) -> TokenStream {
225217
#[proc_macro_attribute]
226218
pub fn ta_close_session(_args: TokenStream, input: TokenStream) -> TokenStream {
227219
let f = parse_macro_input!(input as syn::ItemFn);
228-
let ident = &f.ident;
220+
let f_sig = &f.sig;
221+
let f_ident = &f_sig.ident;
229222

230223
// check the function signature
231-
let valid_signature = f.constness.is_none()
224+
let valid_signature = f_sig.constness.is_none()
232225
&& matches!(f.vis, syn::Visibility::Inherited)
233-
&& f.abi.is_none()
234-
&& (f.decl.inputs.is_empty() || f.decl.inputs.len() == 1)
235-
&& f.decl.generics.where_clause.is_none()
236-
&& f.decl.variadic.is_none()
237-
&& matches!(f.decl.output, syn::ReturnType::Default);
226+
&& f_sig.abi.is_none()
227+
&& (f_sig.inputs.is_empty() || f_sig.inputs.len() == 1)
228+
&& f_sig.generics.where_clause.is_none()
229+
&& f_sig.variadic.is_none()
230+
&& matches!(f_sig.output, syn::ReturnType::Default);
238231

239232
if !valid_signature {
240233
return syn::parse::Error::new(
@@ -245,41 +238,32 @@ pub fn ta_close_session(_args: TokenStream, input: TokenStream) -> TokenStream {
245238
.into();
246239
}
247240

248-
match f.decl.inputs.len() {
241+
match f_sig.inputs.len() {
249242
0 => quote!(
250243
#[no_mangle]
251-
pub extern "C" fn TA_CloseSessionEntryPoint(sess_ctx: *mut c_void) {
252-
#ident();
244+
pub extern "C" fn TA_CloseSessionEntryPoint(_: *mut core::ffi::c_void) {
245+
#f_ident()
253246
}
254247

255248
#f
256249
)
257250
.into(),
258251
1 => {
259-
let input_types: Vec<_> = f
260-
.decl
261-
.inputs
262-
.iter()
263-
.map(|arg| match arg {
264-
syn::FnArg::Captured(val) => &val.ty,
265-
_ => unreachable!(),
266-
})
267-
.collect();
268-
let t = match input_types.first().unwrap() {
269-
syn::Type::Reference(r) => &r.elem,
270-
_ => unreachable!(),
252+
let ctx_type = match extract_fn_arg_mut_ref_type(&f_sig.inputs[0]) {
253+
Ok(v) => v,
254+
Err(e) => return e.to_compile_error().into(),
271255
};
272256

273257
quote!(
274258
// To eliminate the clippy error: this public function might dereference a raw pointer but is not marked `unsafe`
275259
// we just expand the unsafe block, but the session-related macros need refactoring in the future
276260
#[no_mangle]
277-
pub unsafe extern "C" fn TA_CloseSessionEntryPoint(sess_ctx: *mut c_void) {
261+
pub unsafe extern "C" fn TA_CloseSessionEntryPoint(sess_ctx: *mut core::ffi::c_void) {
278262
if sess_ctx.is_null() {
279263
panic!("sess_ctx is null");
280264
}
281-
let mut b = Box::from_raw(sess_ctx as *mut #t);
282-
#ident(&mut b);
265+
let mut b = Box::from_raw(sess_ctx as *mut #ctx_type);
266+
#f_ident(&mut b);
283267
drop(b);
284268
}
285269

@@ -306,15 +290,16 @@ pub fn ta_close_session(_args: TokenStream, input: TokenStream) -> TokenStream {
306290
#[proc_macro_attribute]
307291
pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream {
308292
let f = parse_macro_input!(input as syn::ItemFn);
309-
let ident = &f.ident;
293+
let f_sig = &f.sig;
294+
let f_ident = &f_sig.ident;
310295

311296
// check the function signature
312-
let valid_signature = f.constness.is_none()
297+
let valid_signature = f_sig.constness.is_none()
313298
&& matches!(f.vis, syn::Visibility::Inherited)
314-
&& f.abi.is_none()
315-
&& (f.decl.inputs.len() == 2 || f.decl.inputs.len() == 3)
316-
&& f.decl.generics.where_clause.is_none()
317-
&& f.decl.variadic.is_none();
299+
&& f_sig.abi.is_none()
300+
&& (f_sig.inputs.len() == 2 || f_sig.inputs.len() == 3)
301+
&& f_sig.generics.where_clause.is_none()
302+
&& f_sig.variadic.is_none();
318303

319304
if !valid_signature {
320305
return syn::parse::Error::new(
@@ -325,17 +310,17 @@ pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream
325310
.into();
326311
}
327312

328-
match f.decl.inputs.len() {
313+
match f_sig.inputs.len() {
329314
2 => quote!(
330315
#[no_mangle]
331316
pub extern "C" fn TA_InvokeCommandEntryPoint(
332-
sess_ctx: *mut c_void,
317+
_: *mut core::ffi::c_void,
333318
cmd_id: u32,
334319
param_types: u32,
335-
params: &mut [optee_utee_sys::TEE_Param; 4],
320+
params: &mut optee_utee::RawParams,
336321
) -> optee_utee_sys::TEE_Result {
337322
let mut parameters = Parameters::from_raw(params, param_types);
338-
match #ident(cmd_id, &mut parameters) {
323+
match #f_ident(cmd_id, &mut parameters) {
339324
Ok(_) => {
340325
optee_utee_sys::TEE_SUCCESS
341326
},
@@ -347,36 +332,27 @@ pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream
347332
)
348333
.into(),
349334
3 => {
350-
let input_types: Vec<_> = f
351-
.decl
352-
.inputs
353-
.iter()
354-
.map(|arg| match arg {
355-
syn::FnArg::Captured(val) => &val.ty,
356-
_ => unreachable!(),
357-
})
358-
.collect();
359-
let t = match input_types.first().unwrap() {
360-
syn::Type::Reference(r) => &r.elem,
361-
_ => unreachable!(),
335+
let ctx_type = match extract_fn_arg_mut_ref_type(&f_sig.inputs[0]) {
336+
Ok(v) => v,
337+
Err(e) => return e.to_compile_error().into(),
362338
};
363339

364340
quote!(
365341
// To eliminate the clippy error: this public function might dereference a raw pointer but is not marked `unsafe`
366342
// we just expand the unsafe block, but the session-related macros need refactoring in the future
367343
#[no_mangle]
368344
pub unsafe extern "C" fn TA_InvokeCommandEntryPoint(
369-
sess_ctx: *mut c_void,
345+
sess_ctx: *mut core::ffi::c_void,
370346
cmd_id: u32,
371347
param_types: u32,
372-
params: &mut [optee_utee_sys::TEE_Param; 4],
348+
params: &mut optee_utee::RawParams,
373349
) -> optee_utee_sys::TEE_Result {
374350
if sess_ctx.is_null() {
375351
return optee_utee_sys::TEE_ERROR_SECURITY;
376352
}
377353
let mut parameters = Parameters::from_raw(params, param_types);
378-
let mut b = Box::from_raw(sess_ctx as *mut #t);
379-
match #ident(&mut b, cmd_id, &mut parameters) {
354+
let mut b = Box::from_raw(sess_ctx as *mut #ctx_type);
355+
match #f_ident(&mut b, cmd_id, &mut parameters) {
380356
Ok(_) => {
381357
core::mem::forget(b);
382358
optee_utee_sys::TEE_SUCCESS
@@ -395,3 +371,17 @@ pub fn ta_invoke_command(_args: TokenStream, input: TokenStream) -> TokenStream
395371
_ => unreachable!(),
396372
}
397373
}
374+
375+
fn extract_fn_arg_mut_ref_type(fn_arg: &syn::FnArg) -> Result<&syn::Type, syn::parse::Error> {
376+
if let syn::FnArg::Typed(ty) = fn_arg {
377+
if let syn::Type::Reference(type_ref) = ty.ty.as_ref() {
378+
if type_ref.mutability.is_some() {
379+
return Ok(&*type_ref.elem);
380+
}
381+
}
382+
};
383+
Err(syn::parse::Error::new(
384+
fn_arg.span(),
385+
"this argument should have signature `_: &mut T`",
386+
))
387+
}

optee-utee/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ pub use self::error::{Error, ErrorKind, Result};
5050
pub use self::extension::*;
5151
pub use self::identity::{Identity, LoginType};
5252
pub use self::object::*;
53-
pub use self::parameter::{ParamType, ParamTypes, Parameter, Parameters};
53+
pub use self::parameter::{
54+
ParamType, ParamTypes, Parameter, Parameters, RawParamType, RawParamTypes, RawParams,
55+
};
5456
pub use self::ta_session::{TaSession, TaSessionBuilder};
5557
pub use self::tee_parameter::{ParamIndex, TeeParams};
5658
pub use self::time::*;

optee-utee/src/parameter.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ use crate::{Error, ErrorKind, Result};
1919
use core::{marker, slice};
2020
use optee_utee_sys as raw;
2121

22+
pub type RawParamType = u32;
23+
pub type RawParamTypes = u32;
24+
pub type RawParams = [raw::TEE_Param; 4];
25+
2226
pub struct Parameters(pub Parameter, pub Parameter, pub Parameter, pub Parameter);
2327

2428
impl Parameters {
25-
pub fn from_raw(tee_params: &mut [raw::TEE_Param; 4], param_types: u32) -> Self {
29+
pub fn from_raw(tee_params: &mut RawParams, param_types: u32) -> Self {
2630
let (f0, f1, f2, f3) = ParamTypes::from(param_types).into_flags();
2731
let p0 = Parameter::from_raw(&mut tee_params[0], f0);
2832
let p1 = Parameter::from_raw(&mut tee_params[1], f1);

0 commit comments

Comments
 (0)