Skip to content

Commit 236d79d

Browse files
authored
examples: enable message-passing-interface-rs example for no-std and promote it to a common example (#254)
1 parent 18cf8f5 commit 236d79d

File tree

10 files changed

+53
-32
lines changed

10 files changed

+53
-32
lines changed

examples/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ FEATURES ?=
2828
# Define example categories based on std/no-std support
2929
# STD-only examples (require STD=y to build)
3030
STD_ONLY_EXAMPLES = \
31-
message_passing_interface-rs \
3231
tls_client-rs \
3332
tls_server-rs \
3433
secure_db_abstraction-rs
@@ -50,6 +49,7 @@ COMMON_EXAMPLES = \
5049
hello_world-rs \
5150
hotp-rs \
5251
inter_ta-rs \
52+
message_passing_interface-rs \
5353
property-rs \
5454
random-rs \
5555
secure_storage-rs \

examples/message_passing_interface-rs/host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ edition = "2018"
3030
url = "=2.5.0"
3131
proto = { path = "../proto" }
3232
optee-teec = { path = "../../../optee-teec" }
33+
serde_json = "1.0"
3334

3435
[profile.release]
3536
lto = true

examples/message_passing_interface-rs/host/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,3 @@ strip: host
3838

3939
clean:
4040
@cargo clean
41-

examples/message_passing_interface-rs/host/src/main.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use optee_teec::{
19-
Context, ErrorKind, Operation, ParamNone, ParamTmpRef, ParamType, ParamValue, Uuid,
20-
};
18+
use optee_teec::{Context, ErrorKind, Operation, ParamNone, ParamTmpRef, Uuid};
2119

2220
type Result<T> = optee_teec::Result<T>;
2321

@@ -53,24 +51,23 @@ impl EnclaveClient {
5351

5452
pub fn invoke(&mut self, input: &proto::EnclaveInput) -> Result<proto::EnclaveOutput> {
5553
let command_id = input.command as u32;
56-
let mut serialized_input = proto::serde_json::to_vec(input).map_err(|e| {
54+
let mut serialized_input = serde_json::to_vec(input).map_err(|e| {
5755
eprintln!("Failed to serialize input: {}", e);
5856
ErrorKind::BadParameters
5957
})?;
6058

6159
let p0 = ParamTmpRef::new_input(serialized_input.as_mut_slice());
6260
let p1 = ParamTmpRef::new_output(&mut self.buffer);
63-
let p2 = ParamValue::new(0, 0, ParamType::ValueInout);
6461

65-
let mut operation = Operation::new(0, p0, p1, p2, ParamNone);
62+
let mut operation = Operation::new(0, p0, p1, ParamNone, ParamNone);
6663

6764
let uuid = Uuid::parse_str(&self.uuid)?;
6865
let mut session = self.context.open_session(uuid)?;
6966
session.invoke_command(command_id, &mut operation)?;
70-
let len = operation.parameters().2.a() as usize;
67+
let len = operation.parameters().1.updated_size();
7168

72-
let output: proto::EnclaveOutput = proto::serde_json::from_slice(&self.buffer[0..len])
73-
.map_err(|e| {
69+
let output: proto::EnclaveOutput =
70+
serde_json::from_slice(&self.buffer[0..len]).map_err(|e| {
7471
eprintln!("Failed to deserialize output: {}", e);
7572
ErrorKind::BadParameters
7673
})?;

examples/message_passing_interface-rs/proto/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ edition = "2018"
2626

2727
[dependencies]
2828
num_enum = { version = "0.7.3", default-features = false }
29-
serde = { version = "1.0", features = ["derive"] }
30-
serde_json = "1.0"
29+
serde = { version = "1.0", default-features = false, features = ["derive"] }

examples/message_passing_interface-rs/proto/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#![no_std]
19+
20+
extern crate alloc;
21+
22+
use alloc::string::String;
1823
use num_enum::FromPrimitive;
1924
use serde::{Serialize, Deserialize};
20-
pub use serde_json;
2125

2226
#[derive(Serialize, Deserialize, FromPrimitive, Debug, Copy, Clone)]
2327
#[repr(u32)]

examples/message_passing_interface-rs/ta/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/message_passing_interface-rs/ta/Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ repository = "https://github.com/apache/teaclave-trustzone-sdk.git"
2424
description = "An example of Rust OP-TEE TrustZone SDK."
2525
edition = "2018"
2626

27+
[features]
28+
default = []
29+
std = ["optee-utee/std", "optee-utee-sys/std"]
30+
2731
[dependencies]
2832
proto = { path = "../proto" }
29-
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys", features = ["std"] }
30-
optee-utee = { path = "../../../optee-utee", features = ["std"] }
33+
optee-utee-sys = { path = "../../../optee-utee/optee-utee-sys" }
34+
optee-utee = { path = "../../../optee-utee" }
35+
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
3136

3237
[build-dependencies]
3338
proto = { path = "../proto" }
3439
optee-utee-build = { path = "../../../optee-utee-build" }
3540

3641
[profile.release]
3742
panic = "abort"
38-
lto = false
43+
lto = true
3944
opt-level = 1

examples/message_passing_interface-rs/ta/Makefile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,38 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
# STD-ONLY example
19-
2018
UUID ?= $(shell cat "../uuid.txt")
2119

22-
TARGET ?= aarch64-unknown-optee
20+
TARGET ?= aarch64-unknown-linux-gnu
2321
CROSS_COMPILE ?= aarch64-linux-gnu-
2422
OBJCOPY := $(CROSS_COMPILE)objcopy
2523
# Configure the linker to use GCC, which works on both cross-compilation and ARM machines
2624
LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)gcc\"
2725

26+
# fix for the error: "unwinding panics are not supported without std" reported by clippy
27+
# Set panic=abort for std and no-std
28+
RUSTFLAGS := -C panic=abort
29+
ifeq ($(BUILDER),xargo)
30+
EXTRA_FLAGS =
31+
else
32+
EXTRA_FLAGS = -Z build-std=core,alloc -Z build-std-features=panic_immediate_abort
33+
endif
34+
2835
TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
2936
SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
3037
OUT_DIR := $(CURDIR)/target/$(TARGET)/release
3138

39+
BUILDER ?= cargo
40+
FEATURES ?=
41+
3242
all: clippy ta strip sign
3343

3444
clippy:
3545
@cargo fmt
36-
@xargo clippy --target $(TARGET) -- -D warnings -D clippy::unwrap_used -D clippy::expect_used -D clippy::panic
46+
@RUSTFLAGS="$(RUSTFLAGS)" $(BUILDER) clippy --target $(TARGET) $(EXTRA_FLAGS) $(FEATURES) -- -D warnings -D clippy::unwrap_used -D clippy::expect_used -D clippy::panic
3747

3848
ta: clippy
39-
@xargo build --target $(TARGET) --release --config $(LINKER_CFG)
49+
@RUSTFLAGS="$(RUSTFLAGS)" $(BUILDER) build --target $(TARGET) --release $(FEATURES) --config $(LINKER_CFG) $(EXTRA_FLAGS)
4050

4151
strip: ta
4252
@$(OBJCOPY) --strip-unneeded $(OUT_DIR)/ta $(OUT_DIR)/stripped_ta

examples/message_passing_interface-rs/ta/src/main.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#![cfg_attr(not(feature = "std"), no_std)]
1819
#![no_main]
1920

21+
extern crate alloc;
22+
23+
use alloc::format;
2024
use optee_utee::{
2125
ta_close_session, ta_create, ta_destroy, ta_invoke_command, ta_open_session, trace_println,
2226
};
2327
use optee_utee::{ErrorKind, Parameters, Result};
2428
use proto::{self, Command};
25-
use std::io::Write;
2629

2730
fn handle_invoke(command: Command, input: proto::EnclaveInput) -> Result<proto::EnclaveOutput> {
2831
match command {
@@ -69,23 +72,26 @@ fn invoke_command(cmd_id: u32, params: &mut Parameters) -> Result<()> {
6972
trace_println!("[+] TA invoke command");
7073
let mut p0 = unsafe { params.0.as_memref()? };
7174
let mut p1 = unsafe { params.1.as_memref()? };
72-
let mut p2 = unsafe { params.2.as_value()? };
7375

74-
let input: proto::EnclaveInput = proto::serde_json::from_slice(p0.buffer()).map_err(|e| {
76+
let input: proto::EnclaveInput = serde_json::from_slice(p0.buffer()).map_err(|e| {
7577
trace_println!("Failed to deserialize input: {}", e);
7678
ErrorKind::BadFormat
7779
})?;
7880
let output = handle_invoke(Command::from(cmd_id), input)?;
7981

80-
let output_vec = proto::serde_json::to_vec(&output).map_err(|e| {
82+
let output_vec = serde_json::to_vec(&output).map_err(|e| {
8183
trace_println!("Failed to serialize output: {}", e);
8284
ErrorKind::BadFormat
8385
})?;
84-
p1.buffer().write_all(&output_vec).map_err(|e| {
85-
trace_println!("Failed to write output: {}", e);
86-
ErrorKind::BadParameters
87-
})?;
88-
p2.set_a(output_vec.len() as u32);
86+
87+
let len = output_vec.len();
88+
if len > p1.buffer().len() {
89+
trace_println!("Buffer too small, cannot copy all bytes");
90+
return Err(ErrorKind::BadParameters.into());
91+
}
92+
93+
p1.buffer()[..len].copy_from_slice(&output_vec);
94+
p1.set_updated_size(len);
8995

9096
Ok(())
9197
}

0 commit comments

Comments
 (0)