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

yuanz pushed a commit to branch no-std
in repository 
https://gitbox.apache.org/repos/asf/incubator-teaclave-trustzone-sdk.git

commit 693be179fa5691581286a914b44c7867f32a62c3
Author: Ali Zhang <[email protected]>
AuthorDate: Tue May 7 14:21:41 2024 -0700

    examples: Add error_handling-rs
    
    Add error_handling-rs to exercise some of the failure paths in the
    optee_rust stack.
    
    This initial commit exposes a double-free bug in optee-utee.
    
    Test: run `error_handling-rs` in the qemu environment described at
    https://optee.readthedocs.io/en/latest/building/optee_with_rust.html
    and observe the following output.
    
    thread 'main' panicked at src/main.rs:42:5:
    assertion `left == right` failed
      left: TargetDead
     right: Generic
    note: run with `RUST_BACKTRACE=1` environment variable to display a 
backtrace
    101
---
 examples/error_handling-rs/Makefile         |  36 ++++++++++
 examples/error_handling-rs/host/Cargo.toml  |  33 +++++++++
 examples/error_handling-rs/host/Makefile    |  37 ++++++++++
 examples/error_handling-rs/host/src/main.rs |  43 ++++++++++++
 examples/error_handling-rs/proto/Cargo.toml |  30 ++++++++
 examples/error_handling-rs/proto/build.rs   |  36 ++++++++++
 examples/error_handling-rs/proto/src/lib.rs |  39 +++++++++++
 examples/error_handling-rs/ta/Cargo.toml    |  39 +++++++++++
 examples/error_handling-rs/ta/Makefile      |  42 ++++++++++++
 examples/error_handling-rs/ta/build.rs      | 103 ++++++++++++++++++++++++++++
 examples/error_handling-rs/ta/src/main.rs   |  94 +++++++++++++++++++++++++
 examples/error_handling-rs/ta/ta_static.rs  | 102 +++++++++++++++++++++++++++
 examples/error_handling-rs/uuid.txt         |   1 +
 13 files changed, 635 insertions(+)

diff --git a/examples/error_handling-rs/Makefile 
b/examples/error_handling-rs/Makefile
new file mode 100644
index 0000000..db4e5bb
--- /dev/null
+++ b/examples/error_handling-rs/Makefile
@@ -0,0 +1,36 @@
+# 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.
+
+TARGET ?= aarch64-unknown-linux-gnu
+CROSS_COMPILE ?= aarch64-linux-gnu-
+
+# If _HOST or _TA specific compiler/target are not specified, then use common
+# compiler/target for both
+CROSS_COMPILE_HOST ?= $(CROSS_COMPILE)
+CROSS_COMPILE_TA ?= $(CROSS_COMPILE)
+TARGET_HOST ?= $(TARGET)
+TARGET_TA ?= $(TARGET)
+
+all:
+       $(q)make -C host TARGET=$(TARGET_HOST) \
+               CROSS_COMPILE=$(CROSS_COMPILE_HOST)
+       $(q)make -C ta TARGET=$(TARGET_TA) \
+               CROSS_COMPILE=$(CROSS_COMPILE_TA)
+
+clean:
+       $(q)make -C host clean
+       $(q)make -C ta clean
diff --git a/examples/error_handling-rs/host/Cargo.toml 
b/examples/error_handling-rs/host/Cargo.toml
new file mode 100644
index 0000000..b27393b
--- /dev/null
+++ b/examples/error_handling-rs/host/Cargo.toml
@@ -0,0 +1,33 @@
+# 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.
+
+[package]
+name = "error_handling-rs"
+version = "0.1.0"
+authors = ["Teaclave Contributors <[email protected]>"]
+license = "Apache-2.0"
+repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git";
+description = "A test of Teaclave SDK's error handling  capabilities"
+edition = "2018"
+
+[dependencies]
+libc = "0.2.48"
+proto = { path = "../proto" }
+optee-teec = { path = "../../../optee-teec" }
+
+[profile.release]
+lto = true
diff --git a/examples/error_handling-rs/host/Makefile 
b/examples/error_handling-rs/host/Makefile
new file mode 100644
index 0000000..d92fe4a
--- /dev/null
+++ b/examples/error_handling-rs/host/Makefile
@@ -0,0 +1,37 @@
+# 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.
+
+NAME := error_handling-rs
+
+TARGET ?= aarch64-unknown-linux-gnu
+CROSS_COMPILE ?= aarch64-linux-gnu-
+OBJCOPY := $(CROSS_COMPILE)objcopy
+LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
+
+OUT_DIR := $(CURDIR)/target/$(TARGET)/release
+
+
+all: host strip
+
+host:
+       @cargo build --target $(TARGET) --release --config $(LINKER_CFG)
+
+strip: host
+       @$(OBJCOPY) --strip-unneeded $(OUT_DIR)/$(NAME) $(OUT_DIR)/$(NAME)
+
+clean:
+       @cargo clean
diff --git a/examples/error_handling-rs/host/src/main.rs 
b/examples/error_handling-rs/host/src/main.rs
new file mode 100644
index 0000000..d4eac18
--- /dev/null
+++ b/examples/error_handling-rs/host/src/main.rs
@@ -0,0 +1,43 @@
+// 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 optee_teec::{Context, ErrorKind, Operation, ParamType, Session, Uuid};
+use optee_teec::ParamNone;
+use proto::{UUID, Command};
+
+fn main() -> optee_teec::Result<()> {
+    test_error_handling();
+    Ok(())
+}
+
+fn test_error_handling() {
+    let mut ctx = Context::new().unwrap();
+    let uuid = Uuid::parse_str(UUID).unwrap();
+    let mut session = ctx.open_session(uuid).unwrap();
+    let mut operation = Operation::new(0, ParamNone, ParamNone, ParamNone, 
ParamNone);
+
+    // Test successful invocation return Ok().
+    session.invoke_command(Command::ReturnSuccess as u32, &mut 
operation).expect("success");
+
+    // Test error invocation returns the requested error.
+    let e = session.invoke_command(Command::ReturnGenericError as u32, &mut 
operation).expect_err("generic error");
+    assert_eq!(e.kind(), ErrorKind::Generic);
+
+    // Test repeated error invocation also returns the requested error.
+    let e = session.invoke_command(Command::ReturnGenericError as u32, &mut 
operation).expect_err("generic error");
+    assert_eq!(e.kind(), ErrorKind::Generic);
+}
diff --git a/examples/error_handling-rs/proto/Cargo.toml 
b/examples/error_handling-rs/proto/Cargo.toml
new file mode 100644
index 0000000..284c8d4
--- /dev/null
+++ b/examples/error_handling-rs/proto/Cargo.toml
@@ -0,0 +1,30 @@
+# 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.
+
+[package]
+name = "proto"
+version = "0.2.0"
+authors = ["Teaclave Contributors <[email protected]>"]
+license = "Apache-2.0"
+repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git";
+description = "Data structures and functions shared by host and TA."
+edition = "2018"
+
+[dependencies]
+
+[build_dependencies]
+uuid = { version = "1.6.1", default-features = false }
diff --git a/examples/error_handling-rs/proto/build.rs 
b/examples/error_handling-rs/proto/build.rs
new file mode 100644
index 0000000..b9d0612
--- /dev/null
+++ b/examples/error_handling-rs/proto/build.rs
@@ -0,0 +1,36 @@
+// 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 std::fs;
+use std::path::PathBuf;
+use std::fs::File;
+use std::env;
+use std::io::Write;
+
+fn main() {
+    let uuid = match fs::read_to_string("../uuid.txt") {
+        Ok(u) => {
+            u.trim().to_string()
+        },
+        Err(_) => {
+            panic!("Cannot find uuid.txt");
+        }
+    };
+    let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
+    let mut buffer = File::create(out.join("uuid.txt")).unwrap();
+    write!(buffer, "{}", uuid).unwrap();
+}
diff --git a/examples/error_handling-rs/proto/src/lib.rs 
b/examples/error_handling-rs/proto/src/lib.rs
new file mode 100644
index 0000000..74f3a8a
--- /dev/null
+++ b/examples/error_handling-rs/proto/src/lib.rs
@@ -0,0 +1,39 @@
+// 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.
+
+#![no_std]
+
+pub enum Command {
+    // Ask the TA to simply return `TEE_SUCCESS` when handling this command.
+    ReturnSuccess,
+    // Ask the TA to simply return `TEE_ERROR_GENERIC` when handling this 
command.
+    ReturnGenericError,
+    Unknown,
+}
+
+impl From<u32> for Command {
+    #[inline]
+    fn from(value: u32) -> Command {
+        match value {
+            0 => Command::ReturnSuccess,
+            1 => Command::ReturnGenericError,
+            _ => Command::Unknown,
+        }
+    }
+}
+
+pub const UUID: &str = &include_str!(concat!(env!("OUT_DIR"), "/uuid.txt"));
diff --git a/examples/error_handling-rs/ta/Cargo.toml 
b/examples/error_handling-rs/ta/Cargo.toml
new file mode 100644
index 0000000..7e7cae7
--- /dev/null
+++ b/examples/error_handling-rs/ta/Cargo.toml
@@ -0,0 +1,39 @@
+# 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.
+
+[package]
+name = "ta"
+version = "0.2.0"
+authors = ["Teaclave Contributors <[email protected]>"]
+license = "Apache-2.0"
+repository = "https://github.com/apache/incubator-teaclave-trustzone-sdk.git";
+description = "An example of Rust OP-TEE TrustZone SDK."
+edition = "2018"
+
+[dependencies]
+proto = { path = "../proto" }
+optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys", 
default-features = false }
+optee-utee = { path = "../../../optee-utee", default-features = false }
+
+[build_dependencies]
+uuid = { version = "1.6.1", default-features = false }
+proto = { path = "../proto" }
+
+[profile.release]
+panic = "abort"
+lto = true
+opt-level = 1
diff --git a/examples/error_handling-rs/ta/Makefile 
b/examples/error_handling-rs/ta/Makefile
new file mode 100644
index 0000000..a6de23e
--- /dev/null
+++ b/examples/error_handling-rs/ta/Makefile
@@ -0,0 +1,42 @@
+# 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.
+
+UUID ?= $(shell cat "../uuid.txt")
+
+TARGET ?= aarch64-unknown-linux-gnu
+CROSS_COMPILE ?= aarch64-linux-gnu-
+OBJCOPY := $(CROSS_COMPILE)objcopy
+LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld.bfd\"
+
+TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
+SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
+OUT_DIR := $(CURDIR)/target/$(TARGET)/release
+
+all: ta strip sign
+
+ta:
+       @cargo build --target $(TARGET) --release --config $(LINKER_CFG)
+
+strip: ta
+       @$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta
+
+sign: strip
+       @$(SIGN) --uuid $(UUID) --key $(TA_SIGN_KEY) --in 
$(OUT_DIR)/stripped_ta --out $(OUT_DIR)/$(UUID).ta
+       @echo "SIGN =>  ${UUID}"
+
+clean:
+       @cargo clean
diff --git a/examples/error_handling-rs/ta/build.rs 
b/examples/error_handling-rs/ta/build.rs
new file mode 100644
index 0000000..5618f03
--- /dev/null
+++ b/examples/error_handling-rs/ta/build.rs
@@ -0,0 +1,103 @@
+// 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 proto;
+use std::env;
+use std::fs::File;
+use std::io::{BufRead, BufReader, Write};
+use std::path::{Path, PathBuf};
+use uuid::Uuid;
+
+fn main() -> std::io::Result<()> {
+    let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
+
+    let mut buffer = File::create(out.join("user_ta_header.rs"))?;
+    buffer.write_all(include_bytes!("ta_static.rs"))?;
+
+    let tee_uuid = Uuid::parse_str(proto::UUID).unwrap();
+    let (time_low, time_mid, time_hi_and_version, clock_seq_and_node) = 
tee_uuid.as_fields();
+
+    write!(buffer, "\n")?;
+    write!(
+        buffer,
+        "const TA_UUID: optee_utee_sys::TEE_UUID = optee_utee_sys::TEE_UUID {{
+    timeLow: {:#x},
+    timeMid: {:#x},
+    timeHiAndVersion: {:#x},
+    clockSeqAndNode: {:#x?},
+}};",
+        time_low, time_mid, time_hi_and_version, clock_seq_and_node
+    )?;
+
+    let mut aarch64_flag = true;
+    match env::var("TARGET") {
+        Ok(ref v) if v == "arm-unknown-linux-gnueabihf" => {
+            println!("cargo:rustc-link-arg=--no-warn-mismatch");
+            aarch64_flag = false;
+        },
+        _ => {}
+    };
+
+    let optee_os_dir = env::var("TA_DEV_KIT_DIR").unwrap();
+    let search_path = Path::new(&optee_os_dir).join("lib");
+
+    let optee_os_path = &PathBuf::from(optee_os_dir.clone());
+    let mut ta_lds = File::create(out.join("ta.lds"))?;
+    let f = File::open(optee_os_path.join("src/ta.ld.S"))?;
+    let f = BufReader::new(f);
+
+    for line in f.lines() {
+        let l = line?;
+
+        if aarch64_flag {
+            if l.starts_with('#') ||
+                l == "OUTPUT_FORMAT(\"elf32-littlearm\")" ||
+                l == "OUTPUT_ARCH(arm)" {
+                continue;
+            }
+        } else {
+            if l.starts_with('#') ||
+                l == "OUTPUT_FORMAT(\"elf64-littleaarch64\")" ||
+                l == "OUTPUT_ARCH(aarch64)" {
+                continue;
+            }
+        }
+
+        if l == "\t. = ALIGN(4096);" {
+            write!(ta_lds, "\t. = ALIGN(65536);\n")?;
+        } else {
+            write!(ta_lds, "{}\n", l)?;
+        }
+    }
+
+    println!("cargo:rustc-link-search={}", out.display());
+    println!("cargo:rerun-if-changed=ta.lds");
+
+    println!("cargo:rustc-link-search={}", search_path.display());
+    println!("cargo:rustc-link-lib=static=utee");
+    println!("cargo:rustc-link-lib=static=utils");
+    println!("cargo:rustc-link-arg=-Tta.lds");
+    println!("cargo:rustc-link-arg=-e__ta_entry");
+    println!("cargo:rustc-link-arg=-pie");
+    println!("cargo:rustc-link-arg=-Os");
+    println!("cargo:rustc-link-arg=--sort-section=alignment");
+
+    let mut dyn_list = File::create(out.join("dyn_list"))?;
+    write!(dyn_list, "{{ __elf_phdr_info; trace_ext_prefix; trace_level; 
ta_head; }};\n")?;
+    println!("cargo:rustc-link-arg=--dynamic-list=dyn_list");
+    Ok(())
+}
diff --git a/examples/error_handling-rs/ta/src/main.rs 
b/examples/error_handling-rs/ta/src/main.rs
new file mode 100644
index 0000000..7f13f91
--- /dev/null
+++ b/examples/error_handling-rs/ta/src/main.rs
@@ -0,0 +1,94 @@
+// 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.
+
+#![no_std]
+#![no_main]
+#![feature(c_size_t)]
+
+extern crate alloc;
+
+use alloc::boxed::Box;
+use alloc::vec;
+use alloc::vec::Vec;
+
+use optee_utee::{
+    ta_close_session, ta_create, ta_destroy, ta_invoke_command, 
ta_open_session, trace_println,
+};
+use optee_utee::{Error, ErrorKind, Parameters, Result};
+use proto::Command;
+
+pub struct SessionContext {
+    stuff_on_heap: Vec<u8>,
+}
+
+impl Default for SessionContext {
+    fn default() -> Self {
+        Self {
+            stuff_on_heap: vec![1, 2, 3, 4],
+        }
+    }
+}
+
+#[ta_create]
+fn create() -> Result<()> {
+    trace_println!("[+] TA create");
+    Ok(())
+}
+
+#[ta_open_session]
+fn open_session(_params: &mut Parameters, _sess_ctx: &mut SessionContext) -> 
Result<()> {
+    trace_println!("[+] TA open session");
+    Ok(())
+}
+
+#[ta_close_session]
+fn close_session(_sess_ctx: &mut SessionContext) {
+    trace_println!("[+] TA close session");
+}
+
+#[ta_destroy]
+fn destroy() {
+    trace_println!("[+] TA destroy");
+}
+
+#[ta_invoke_command]
+fn invoke_command(
+    _sess_ctx: &mut SessionContext,
+    cmd_id: u32,
+    params: &mut Parameters,
+) -> Result<()> {
+    trace_println!("[+] TA invoke command");
+    match Command::from(cmd_id) {
+        Command::ReturnSuccess => Ok(()),
+        Command::ReturnGenericError => Err(Error::new(ErrorKind::Generic)),
+        _ => Err(Error::new(ErrorKind::NotSupported)),
+    }
+}
+
+// TA configurations
+const TA_FLAGS: u32 = 0;
+const TA_DATA_SIZE: u32 = 32 * 1024;
+const TA_STACK_SIZE: u32 = 2 * 1024;
+const TA_VERSION: &[u8] = b"0.1\0";
+const TA_DESCRIPTION: &[u8] = b"This is a TA for error handling tests.\0";
+const EXT_PROP_VALUE_1: &[u8] = b"Error handling TA\0";
+const EXT_PROP_VALUE_2: u32 = 0x0010;
+const TRACE_LEVEL: i32 = 4;
+const TRACE_EXT_PREFIX: &[u8] = b"TA\0";
+const TA_FRAMEWORK_STACK_SIZE: u32 = 2048;
+
+include!(concat!(env!("OUT_DIR"), "/user_ta_header.rs"));
diff --git a/examples/error_handling-rs/ta/ta_static.rs 
b/examples/error_handling-rs/ta/ta_static.rs
new file mode 100644
index 0000000..53ca210
--- /dev/null
+++ b/examples/error_handling-rs/ta/ta_static.rs
@@ -0,0 +1,102 @@
+// 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 core::ffi::*;
+use core::mem;
+use core::primitive::u64;
+
+#[no_mangle]
+pub static mut trace_level: c_int = TRACE_LEVEL;
+
+#[no_mangle]
+pub static trace_ext_prefix: &[u8] = TRACE_EXT_PREFIX;
+
+#[no_mangle]
+#[link_section = ".ta_head"]
+pub static ta_head: optee_utee_sys::ta_head = optee_utee_sys::ta_head {
+    uuid: TA_UUID,
+    stack_size: TA_STACK_SIZE + TA_FRAMEWORK_STACK_SIZE,
+    flags: TA_FLAGS,
+    depr_entry: u64::MAX,
+};
+
+#[no_mangle]
+#[link_section = ".bss"]
+pub static ta_heap: [u8; TA_DATA_SIZE as usize] = [0; TA_DATA_SIZE as usize];
+
+#[no_mangle]
+pub static ta_heap_size: c_size_t = mem::size_of::<u8>() * TA_DATA_SIZE as 
usize;
+static FLAG_BOOL: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_SINGLE_INSTANCE) 
!= 0;
+static FLAG_MULTI: bool = (TA_FLAGS & optee_utee_sys::TA_FLAG_MULTI_SESSION) 
!= 0;
+static FLAG_INSTANCE: bool = (TA_FLAGS & 
optee_utee_sys::TA_FLAG_INSTANCE_KEEP_ALIVE) != 0;
+
+#[no_mangle]
+pub static ta_num_props: c_size_t = 9;
+
+#[no_mangle]
+pub static ta_props: [optee_utee_sys::user_ta_property; 9] = [
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_SINGLE_INSTANCE,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL,
+        value: &FLAG_BOOL as *const bool as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_MULTI_SESSION,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL,
+        value: &FLAG_MULTI as *const bool as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_KEEP_ALIVE,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_BOOL,
+        value: &FLAG_INSTANCE as *const bool as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_DATA_SIZE,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32,
+        value: &TA_DATA_SIZE as *const u32 as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_STACK_SIZE,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32,
+        value: &TA_STACK_SIZE as *const u32 as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_VERSION,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING,
+        value: TA_VERSION as *const [u8] as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: optee_utee_sys::TA_PROP_STR_DESCRIPTION,
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING,
+        value: TA_DESCRIPTION as *const [u8] as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: "gp.ta.description\0".as_ptr(),
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_STRING,
+        value: EXT_PROP_VALUE_1 as *const [u8] as *mut _,
+    },
+    optee_utee_sys::user_ta_property {
+        name: "gp.ta.version\0".as_ptr(),
+        prop_type: optee_utee_sys::user_ta_prop_type::USER_TA_PROP_TYPE_U32,
+        value: &EXT_PROP_VALUE_2 as *const u32 as *mut _,
+    },
+];
+
+#[no_mangle]
+pub unsafe extern "C" fn tahead_get_trace_level() -> c_int {
+    return trace_level;
+}
diff --git a/examples/error_handling-rs/uuid.txt 
b/examples/error_handling-rs/uuid.txt
new file mode 100644
index 0000000..29d5aa1
--- /dev/null
+++ b/examples/error_handling-rs/uuid.txt
@@ -0,0 +1 @@
+ec59c1fc-b9e0-4c3c-8756-0a3cc48f0088
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to