You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by iv...@apache.org on 2026/04/01 04:40:09 UTC

(teaclave-trustzone-sdk) 02/03: optee-utee-sys: bump rust edition

This is an automated email from the ASF dual-hosted git repository.

ivila pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/teaclave-trustzone-sdk.git

commit c47ba65efeeab51606ebdb24844dce13a7c0c338
Author: ivila <39...@qq.com>
AuthorDate: Tue Mar 31 19:24:51 2026 +0800

    optee-utee-sys: bump rust edition
---
 optee-utee/Cargo.toml                              |   1 -
 optee-utee/optee-utee-sys/Cargo.toml               |   3 +-
 optee-utee/optee-utee-sys/build.rs                 |  14 +--
 optee-utee/optee-utee-sys/src/lib.rs               |  16 +--
 optee-utee/optee-utee-sys/src/tee_api.rs           |   9 +-
 optee-utee/optee-utee-sys/src/tee_api_private.rs   |  21 ----
 optee-utee/optee-utee-sys/src/tee_api_types.rs     |   2 +-
 .../src/tee_internal_api_extensions.rs             |   2 +-
 optee-utee/optee-utee-sys/src/tee_tcpsocket.rs     |   2 +-
 optee-utee/optee-utee-sys/src/tee_udpsocket.rs     |   2 +-
 optee-utee/optee-utee-sys/src/trace.rs             |   2 +-
 optee-utee/optee-utee-sys/src/user_ta_header.rs    |   8 +-
 optee-utee/optee-utee-sys/src/utee_syscalls.rs     |   4 +-
 optee-utee/systest/Cargo.toml                      |   6 +-
 optee-utee/systest/build.rs                        | 116 +++++++++++++--------
 15 files changed, 94 insertions(+), 114 deletions(-)

diff --git a/optee-utee/Cargo.toml b/optee-utee/Cargo.toml
index 98cfe52..fcf989a 100644
--- a/optee-utee/Cargo.toml
+++ b/optee-utee/Cargo.toml
@@ -38,7 +38,6 @@ rand = "0.8.5"
 once_cell = "1.20.2"
 serde = { version = "1.0.215" }
 serde_json = { version = "1.0.133" }
-# disable linking when running unit tests
 optee-utee-sys = { version = "0.8.0", path = "optee-utee-sys", features = ["mock"] }
 
 [features]
diff --git a/optee-utee/optee-utee-sys/Cargo.toml b/optee-utee/optee-utee-sys/Cargo.toml
index 9087bfd..ff7d435 100644
--- a/optee-utee/optee-utee-sys/Cargo.toml
+++ b/optee-utee/optee-utee-sys/Cargo.toml
@@ -22,11 +22,10 @@ authors = ["Teaclave Contributors <de...@teaclave.apache.org>"]
 license = "Apache-2.0"
 repository = "https://github.com/apache/teaclave-trustzone-sdk.git"
 description = "Native bindings to the libutee library."
-edition = "2018"
+edition = "2024"
 links = "utee"
 
 [dependencies]
-libc = "0.2"
 mockall = { version = "0.14.0", optional = true }
 
 [features]
diff --git a/optee-utee/optee-utee-sys/build.rs b/optee-utee/optee-utee-sys/build.rs
index 4793463..ba3e997 100644
--- a/optee-utee/optee-utee-sys/build.rs
+++ b/optee-utee/optee-utee-sys/build.rs
@@ -19,24 +19,12 @@ use std::env::{self, VarError};
 use std::path::PathBuf;
 
 fn main() -> Result<(), VarError> {
-    if !is_feature_enable("no_link")? {
+    if !cfg!(feature = "no_link") {
         link();
     }
     Ok(())
 }
 
-// Check if feature enabled.
-// Refer to: https://doc.rust-lang.org/cargo/reference/features.html#build-scripts
-fn is_feature_enable(feature: &str) -> Result<bool, VarError> {
-    let feature_env = format!("CARGO_FEATURE_{}", feature.to_uppercase().replace("-", "_"));
-
-    match env::var(feature_env) {
-        Err(VarError::NotPresent) => Ok(false),
-        Ok(_) => Ok(true),
-        Err(err) => Err(err),
-    }
-}
-
 fn link() {
     let ta_dev_kit_dir = env::var("TA_DEV_KIT_DIR").expect("TA_DEV_KIT_DIR not set");
     let library_path = PathBuf::from(ta_dev_kit_dir).join("lib");
diff --git a/optee-utee/optee-utee-sys/src/lib.rs b/optee-utee/optee-utee-sys/src/lib.rs
index 4fb0bd2..b17d69a 100644
--- a/optee-utee/optee-utee-sys/src/lib.rs
+++ b/optee-utee/optee-utee-sys/src/lib.rs
@@ -44,19 +44,9 @@ mod user_ta_header;
 mod utee_syscalls;
 mod utee_types;
 
-// Currently, the libc crate does not support optee_os, and patching it in
-// build-std within the TA project does not affect optee-utee-sys. Therefore,
-// we need to define the type directly in the crate to ensure compatibility.
-#[cfg(feature = "std")]
-mod libc_compat {
-    pub type size_t = usize;
-    pub type intmax_t = i64;
-}
-
-#[cfg(not(feature = "std"))]
-mod libc_compat {
-    pub use libc::{intmax_t, size_t};
-}
+pub type size_t = usize;
+// https://github.com/OP-TEE/optee_os/blob/c2b0684fcd89929976a8726e6e3af922b48dd2c7/lib/libutils/isoc/include/stdint.h#L92
+pub type intmax_t = i64;
 
 #[cfg(feature = "mock")]
 pub mod mock_api {
diff --git a/optee-utee/optee-utee-sys/src/tee_api.rs b/optee-utee/optee-utee-sys/src/tee_api.rs
index f3b165a..15aa993 100644
--- a/optee-utee/optee-utee-sys/src/tee_api.rs
+++ b/optee-utee/optee-utee-sys/src/tee_api.rs
@@ -17,11 +17,10 @@
 
 #[cfg_attr(feature = "mock", mockall::automock)]
 pub mod api {
-    use crate::libc_compat::{intmax_t, size_t};
     use crate::*;
     use core::ffi::*;
 
-    extern "C" {
+    unsafe extern "C" {
         // Property access functions
 
         pub fn TEE_GetPropertyAsString(
@@ -584,12 +583,6 @@ pub mod api {
             n: *const TEE_BigInt,
             context: *const TEE_BigIntFMMContext,
         ) -> c_void;
-        pub fn TEE_BigIntFMMConvertToBigInt(
-            dest: *mut TEE_BigInt,
-            src: *const TEE_BigIntFMM,
-            n: *const TEE_BigInt,
-            context: *const TEE_BigIntFMMContext,
-        ) -> c_void;
         pub fn TEE_BigIntComputeFMM(
             dest: *mut TEE_BigIntFMM,
             op1: *const TEE_BigIntFMM,
diff --git a/optee-utee/optee-utee-sys/src/tee_api_private.rs b/optee-utee/optee-utee-sys/src/tee_api_private.rs
deleted file mode 100644
index 30f7d4b..0000000
--- a/optee-utee/optee-utee-sys/src/tee_api_private.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//   http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-use libc::*;
-use super::*;
-
-
diff --git a/optee-utee/optee-utee-sys/src/tee_api_types.rs b/optee-utee/optee-utee-sys/src/tee_api_types.rs
index f925204..21302a6 100644
--- a/optee-utee/optee-utee-sys/src/tee_api_types.rs
+++ b/optee-utee/optee-utee-sys/src/tee_api_types.rs
@@ -17,7 +17,7 @@
 
 // Common Definitions
 
-use crate::libc_compat::size_t;
+use crate::size_t;
 use core::ffi::*;
 
 #[allow(non_camel_case_types)]
diff --git a/optee-utee/optee-utee-sys/src/tee_internal_api_extensions.rs b/optee-utee/optee-utee-sys/src/tee_internal_api_extensions.rs
index c1397d9..3a42756 100644
--- a/optee-utee/optee-utee-sys/src/tee_internal_api_extensions.rs
+++ b/optee-utee/optee-utee-sys/src/tee_internal_api_extensions.rs
@@ -20,7 +20,7 @@ pub mod api {
     use crate::*;
     use core::ffi::*;
 
-    extern "C" {
+    unsafe extern "C" {
         /// tee_invoke_supp_plugin() - invoke a tee-supplicant's plugin
         /// @uuid:       uuid of the plugin
         /// @cmd:        command for the plugin
diff --git a/optee-utee/optee-utee-sys/src/tee_tcpsocket.rs b/optee-utee/optee-utee-sys/src/tee_tcpsocket.rs
index 453f9da..10bc645 100644
--- a/optee-utee/optee-utee-sys/src/tee_tcpsocket.rs
+++ b/optee-utee/optee-utee-sys/src/tee_tcpsocket.rs
@@ -26,7 +26,7 @@ pub struct TEE_tcpSocket_Setup_s {
     pub server_port: u16,
 }
 
-extern "C" {
+unsafe extern "C" {
     pub static TEE_tcpSocket: *const TEE_iSocket;
 }
 
diff --git a/optee-utee/optee-utee-sys/src/tee_udpsocket.rs b/optee-utee/optee-utee-sys/src/tee_udpsocket.rs
index 3ecf1af..28a93c3 100644
--- a/optee-utee/optee-utee-sys/src/tee_udpsocket.rs
+++ b/optee-utee/optee-utee-sys/src/tee_udpsocket.rs
@@ -26,7 +26,7 @@ pub struct TEE_udpSocket_Setup_s {
     pub server_port: u16,
 }
 
-extern "C" {
+unsafe extern "C" {
     pub static TEE_udpSocket: *const TEE_iSocket;
 }
 
diff --git a/optee-utee/optee-utee-sys/src/trace.rs b/optee-utee/optee-utee-sys/src/trace.rs
index 651de6b..77559e5 100644
--- a/optee-utee/optee-utee-sys/src/trace.rs
+++ b/optee-utee/optee-utee-sys/src/trace.rs
@@ -17,7 +17,7 @@
 
 use core::ffi::*;
 
-extern "C" {
+unsafe extern "C" {
     pub fn trace_ext_puts(str: *const c_char);
     pub fn trace_ext_get_thread_id() -> c_int;
     pub fn trace_set_level(level: c_int);
diff --git a/optee-utee/optee-utee-sys/src/user_ta_header.rs b/optee-utee/optee-utee-sys/src/user_ta_header.rs
index 0bbb235..f40131c 100644
--- a/optee-utee/optee-utee-sys/src/user_ta_header.rs
+++ b/optee-utee/optee-utee-sys/src/user_ta_header.rs
@@ -37,7 +37,7 @@ pub struct ta_head {
     pub depr_entry: u64,
 }
 
-extern "C" {
+unsafe extern "C" {
     pub fn __utee_entry(
         func: c_ulong,
         session_id: c_ulong,
@@ -52,16 +52,16 @@ extern "C" {
 /// is a raw pointer that must point to a valid `utee_params` structure initialized
 /// by the OP-TEE runtime environment. This function should never be called directly
 /// from user code - it is only exported for the OP-TEE OS loader.
-#[no_mangle]
+#[unsafe(no_mangle)]
 unsafe fn __ta_entry(
     func: c_ulong,
     session_id: c_ulong,
     up: *mut utee_params,
     cmd_id: c_ulong,
 ) -> ! {
-    let res: u32 = __utee_entry(func, session_id, up, cmd_id);
+    let res: u32 = unsafe { __utee_entry(func, session_id, up, cmd_id) };
 
-    _utee_return(res.into());
+    unsafe { _utee_return(res.into()) };
 }
 
 unsafe impl Sync for ta_head {}
diff --git a/optee-utee/optee-utee-sys/src/utee_syscalls.rs b/optee-utee/optee-utee-sys/src/utee_syscalls.rs
index 05abbad..7b08fa3 100644
--- a/optee-utee/optee-utee-sys/src/utee_syscalls.rs
+++ b/optee-utee/optee-utee-sys/src/utee_syscalls.rs
@@ -17,10 +17,10 @@
 
 use super::utee_types::utee_object_info;
 use super::*;
-use crate::libc_compat::size_t;
+use crate::size_t;
 use core::ffi::*;
 
-extern "C" {
+unsafe extern "C" {
     pub fn _utee_return(ret: c_ulong) -> !;
     pub fn _utee_log(buf: *const c_void, len: size_t);
     pub fn _utee_panic(code: c_ulong);
diff --git a/optee-utee/systest/Cargo.toml b/optee-utee/systest/Cargo.toml
index eec72b7..3c7cba9 100644
--- a/optee-utee/systest/Cargo.toml
+++ b/optee-utee/systest/Cargo.toml
@@ -22,12 +22,12 @@ authors = ["Teaclave Contributors <de...@teaclave.apache.org>"]
 license = "Apache-2.0"
 repository = "https://github.com/apache/teaclave-trustzone-sdk.git"
 description = "Automatic testing of FFI bindings for the libutee library."
-edition = "2018"
+edition = "2024"
 
 [dependencies]
 optee-utee-sys = { path = "../optee-utee-sys" }
-libc = "0.2.59"
+libc = "0.2"
 
 [build-dependencies]
-ctest = "0.4"
+ctest = "0.5"
 cc = "1.2.19"
diff --git a/optee-utee/systest/build.rs b/optee-utee/systest/build.rs
index 15d208c..aa5ee5b 100644
--- a/optee-utee/systest/build.rs
+++ b/optee-utee/systest/build.rs
@@ -21,7 +21,6 @@ use std::io::Write;
 use std::path::PathBuf;
 
 fn main() {
-    let mut cfg = ctest::TestGenerator::new();
     let ta_include_path = {
         let ta_dev_kit_dir = env::var("TA_DEV_KIT_DIR").expect("TA_DEV_KIT_DIR not set");
         let include_path = PathBuf::from(ta_dev_kit_dir).join("include");
@@ -33,7 +32,21 @@ fn main() {
         }
         include_path
     };
-    cfg.header("tee_api_types.h")
+    let mut cfg = generate_cfg(ta_include_path.clone());
+    ctest::generate_test(&mut cfg, "../optee-utee-sys/src/lib.rs", "all.rs").unwrap();
+    println!("cargo:rustc-link-lib=static=mbedtls");
+    println!("cargo:rustc-link-lib=static=utee");
+    println!("cargo:rustc-link-lib=static=utils");
+    println!("cargo:rustc-link-lib=static=dl");
+
+    build_and_link_undefined(ta_include_path);
+}
+
+fn generate_cfg(ta_include_path: PathBuf) -> ctest::TestGenerator {
+    let mut cfg = ctest::TestGenerator::new();
+    cfg.edition(2024)
+        .language(ctest::Language::C)
+        .header("tee_api_types.h")
         .header("tee_api_defines.h")
         .header("utee_types.h")
         .header("user_ta_header.h")
@@ -41,51 +54,70 @@ fn main() {
         .header("utee_syscalls.h")
         .header("tee_tcpsocket.h")
         .header("tee_udpsocket.h")
+        .header("tee_internal_api.h")
         .header("tee_internal_api_extensions.h")
         .header("__tee_tcpsocket_defines_extensions.h")
         .include(ta_include_path.display().to_string())
-        .type_name(|s, _is_struct, _is_union| {
-            if s == "utee_params"
-                || s == "ta_head"
-                || s == "utee_attribute"
-                || s == "utee_object_info"
-                || s == "user_ta_property"
-                || s == "TEE_tcpSocket_Setup_s"
-                || s == "TEE_udpSocket_Setup_s"
-            {
-                return format!("struct {}", s);
+        .rename_type(|s| match s {
+            "u64" => Some("uint64_t".to_string()),
+            "u32" => Some("uint32_t".to_string()),
+            "u16" => Some("uint16_t".to_string()),
+            "c_char" => Some("char".to_string()),
+            _ => None,
+        })
+        .rename_struct_ty(|ty| {
+            if ty.starts_with("TEE") {
+                return Some(ty.to_string());
+            }
+            None
+        })
+        .skip_struct(|s| {
+            let s = s.ident();
+            match s {
+                "Memref"
+                | "Value"
+                | "ta_prop"
+                | "user_ta_property"
+                | "TEE_iSocket_s"
+                | "TEE_udpSocket_Setup_s"
+                | "TEE_tcpSocket_Setup_s"
+                | "TEE_SEReaderProperties"
+                | "TEE_SEAID" => true,
+                _ => s.ends_with("Handle"),
             }
-            s.to_string()
+        })
+        .skip_struct_field(|s, field| {
+            let s = s.ident();
+            let field = field.ident();
+            (s == "ta_head" && field == "entry") 
+                || (s == "TEE_OperationInfoMultiple" && field == "keyInformation") // va_list
+                || (s == "TEE_Attribute" && field == "content") // anonymous union fields
+        })
+        .skip_fn(|s| {
+            let s = s.ident();
+            match s {
+                "__utee_entry" => true,
+                _ => false,
+            }
+        })
+        .skip_union(|s| match s.ident() {
+            "content" => true,
+            _ => false,
+        })
+        .skip_union_field(|s, _field| {
+            let s = s.ident();
+            s == "TEE_Param" // TEE_Param only have anonymous fields
+        })
+        .rename_union_ty(|s| match s {
+            "TEE_Param" => Some(s.to_string()),
+            _ => None,
         });
-    cfg.skip_struct(|s| {
-        s == "Memref"
-            || s == "Value"
-            || s == "content"
-            || s.ends_with("Handle")
-            || s == "ta_prop"
-            || s == "user_ta_property"
-            || s == "TEE_iSocket_s" // untestable due to `const struct`
-    });
-    cfg.skip_field(|s, field| {
-        (s == "ta_head" && field == "entry")
-            || field == "content"
-            || field == "value"
-            || field == "memref"
-            || field == "keyInformation"
-    });
-    cfg.skip_type(|s| s == "Memref" || s == "Value");
-    cfg.skip_fn(|s| s == "TEE_BigIntFMMConvertToBigInt" || s == "__utee_entry");
-    cfg.skip_const(|s| s.starts_with("TA_PROP_STR") || s == "TEE_HANDLE_NULL");
-    cfg.skip_roundtrip(|s| s.starts_with("TEE_") || s.starts_with("utee_") || s == "ta_head");
-    cfg.skip_static(|s| s == "TEE_tcpSocket" || s == "TEE_udpSocket");
-    cfg.generate("../optee-utee-sys/src/lib.rs", "all.rs");
-    println!("cargo:rustc-link-lib=static=mbedtls");
-    println!("cargo:rustc-link-lib=static=utee");
-    println!("cargo:rustc-link-lib=static=utils");
-    println!("cargo:rustc-link-lib=static=dl");
+    cfg
+}
 
-    let out_dir = env::var("OUT_DIR").unwrap();
-    let undefined_path = PathBuf::from(&out_dir).join("undefined.c");
+fn build_and_link_undefined(ta_include_path: PathBuf) {
+    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
+    let undefined_path = out_dir.join("undefined.c");
 
     let mut buffer = File::create(&undefined_path).unwrap();
     write!(
@@ -131,6 +163,6 @@ fn main() {
         .file(&undefined_path.display().to_string())
         .compile("undefined");
 
-    println!("cargo:rustc-link-search=native={}", out_dir);
+    println!("cargo:rustc-link-search=native={}", out_dir.display());
     println!("cargo:rustc-link-lib=static=undefined");
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org