apache / apache/teaclave-sgx-sdk
v2.0.0-preview: crate 'cpufeatures' failed detect 'target_env = "sgx"'
- Dominant language
- Rust
- Stars
- 1.2k
- Forks
- 268
- PR merge metrics
- No merged PRs in 30d
Description
I built a demo to calc Sha256,but got `Illegal instruction (core dumped)`.
The Sha256 comes from crate [sha2](https://crates.io/crates/sha2).
```sh
cd samplecode/helloworld
BUILD_STD=cargo make
cd bin
./app
[+] Init Enclave Successful 2!
This is a normal world string passed into Enclave!
target_env = not sgx
Illegal instruction (core dumped)
```
After some research,I found crate `cpufeatures` which `sha2`'s dependent library failed detect `target_env = "sgx"`,
`cpufeatures` uses `cpuid` to check cpu features, then crash.
And I found the file [x86_64-unknown-linux-sgx.json](https://github.com/apache/incubator-teaclave-sgx-sdk/blob/v2.0.0-preview/rustlib/x86_64-unknown-linux-sgx.json), assign `env` the value `gnu` , than's the problem, if I change `env` value from `gnu` to `sgx`, it's ok.
```sh
[+] Init Enclave Successful 2!
This is a normal world string passed into Enclave!
target_env = sgx
This is a in-Enclave Rust string!
[+] ECall Success...
```
`My question is`: should I keep `sgx` value in `x86_64-unknown-linux-sgx.json`?
This is my demo codes:
```sh
diff --git a/samplecode/helloworld/enclave/Cargo.toml b/samplecode/helloworld/enclave/Cargo.toml
index 40d61163..81b6301a 100644
--- a/samplecode/helloworld/enclave/Cargo.toml
+++ b/samplecode/helloworld/enclave/Cargo.toml
@@ -31,3 +31,7 @@ default = []
[target.'cfg(not(target_vendor = "teaclave"))'.dependencies]
sgx_types = { path = "../../../sgx_types" }
sgx_tstd = { path = "../../../sgx_tstd" }
+
+[dependencies]
+sha2 = "0.10.2"
+hex-literal = "0.3.4"
diff --git a/samplecode/helloworld/enclave/src/lib.rs b/samplecode/helloworld/enclave/src/lib.rs
index 5fd6daf4..66984701 100644
--- a/samplecode/helloworld/enclave/src/lib.rs
+++ b/samplecode/helloworld/enclave/src/lib.rs
@@ -29,6 +29,9 @@ use std::slice;
use std::string::String;
use std::vec::Vec;
+use hex_literal::hex;
+use sha2::{Digest, Sha256, Sha512};
+
/// # Safety
#[no_mangle]
pub unsafe extern "C" fn say_something(some_string: *const u8, some_len: usize) -> SgxStatus {
@@ -53,6 +56,28 @@ pub unsafe extern "C" fn say_something(some_string: *const u8, some_len: usize)
// Rust style convertion
hello_string += String::from_utf8(word_vec).expect("Invalid UTF-8").as_str();
+ let target_env = {
+ let mut s = String::new();
+
+ #[cfg(not(target_env = "sgx"))]
+ s.push_str("not sgx");
+
+ #[cfg(target_env = "sgx")]
+ s.push_str("sgx");
+
+ s
+ };
+ println!("target_env = {}", target_env);
+
+ let mut hasher = Sha256::new();
+ // write input message
+ hasher.update(b"hello world");
+ let result = hasher.finalize();
+ assert_eq!(
+ result[..],
+ hex!("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")[..]
+ );
+
// Ocall to normal world for output
println!("{}", &hello_string);
```
Thanks.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with rustlib/x86_64-unknown-linux-sgx.json and reproduce the issue from samplecode/helloworld using BUILD_STD=cargo make, then run bin/app. Compare the target_env result with cpufeatures behavior and the SHA-256 failure; done means the SGX demo completes successfully without an illegal-instruction crash and the target configuration is correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- security
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100