apache / incubator-teaclave-sgx-sdk

Apache Teaclave (incubating) SGX SDK helps developers to write Intel SGX applications in the Rust programming language, and also known as Rust SGX SDK.
https://teaclave.apache.org
Apache License 2.0
1.17k stars 264 forks source link

v2.0 compile issue "mismatched types" #455

Closed lythesia closed 8 months ago

lythesia commented 8 months ago

I'm using v2.0.0-preview branch to play with demo, code snip as following: (using sgx_types and sgx_crypto)

fn sha_hash(input: &str) -> sgx_types::error::SgxResult<String> {
    let v = match Sha256::digest(input.as_bytes()) {
        Ok(v) => v,
        Err(e) => return Err(e),
    };
    let bytes = v.as_slice();
    let result = base64::engine::general_purpose::STANDARD.encode(&bytes);
    Ok(result)
}

rustc fails with

error[E0308]: mismatched types
   --> src/lib.rs:52:30
    |
52  |         Err(e) => return Err(e),
    |                          --- ^ expected `SgxStatus`, found `sgx_types::error::SgxStatus`
    |                          |
    |                          arguments to this enum variant are incorrect
    |
    = note: `sgx_types::error::SgxStatus` and `SgxStatus` have similar names, but are actually distinct types
note: `sgx_types::error::SgxStatus` is defined in crate `sgx_types`
   --> /home/ubuntu/preview/incubator-teaclave-sgx-sdk/sgx_types/src/error/mod.rs:24:1
    |
24  | / impl_enum! {
25  | |     #[repr(u32)]
26  | |     #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
27  | |     pub enum SgxStatus {
...   |
112 | |     }
113 | | }
    | |_^
note: `SgxStatus` is defined in crate `sgx_types`
   --> /home/ubuntu/preview/incubator-teaclave-sgx-sdk/sgx_types/src/error/mod.rs:24:1
    |
24  | / impl_enum! {
25  | |     #[repr(u32)]
26  | |     #[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
27  | |     pub enum SgxStatus {
...   |
112 | |     }
113 | | }
    | |_^
    = note: perhaps two different versions of crate `sgx_types` are being used?

I'm confused SgxStatus is defined in single local crate, it cannot be imported as multiple versions? and I tried to mimic this to minimal reproducible case with std codes (manually written), but it just compiles and runs

No idea what causes this, can you help?

yangfh2004 commented 8 months ago

Which sample did you build? Make sure you include:

extern crate sgx_types

to declare the dependency.

lythesia commented 8 months ago

Which sample did you build? Make sure you include:

extern crate sgx_types

to declare the dependency.

not particular one, you can just put this snip to helloworld sample and compile (base64 line not matter, can remove)

yes, I tried add or remove extern crate sgx_types at head of lib.rs, neither helps

yangfh2004 commented 8 months ago

Did you include this in your Cargo.toml

[target.'cfg(not(target_vendor = "teaclave"))'.dependencies]
sgx_types = { git = "REPO_ADDRESS", branch = "BRANCH_NAME" }
lythesia commented 8 months ago

I cloned repo to local and import with sgx_types = { path = ".." }

lythesia commented 8 months ago

hi, I tried again based on helloworld sample, it works well, but this example use very few deps and features

hellworld sample, Cargo.toml of enclave

[target.'cfg(not(target_vendor = "teaclave"))'.dependencies]
sgx_types = { path = "../../../sgx_types" }
sgx_tstd = { path = "../../../sgx_tstd" }
sgx_crypto = { path = "../../../sgx_crypto" } # added

and no features enabled for sysroot Rust_Std_Features :=

head lines in lib.rs:

#![cfg_attr(not(target_vendor = "teaclave"), no_std)]
#![cfg_attr(target_vendor = "teaclave", feature(rustc_private))]

#[cfg(not(target_vendor = "teaclave"))]
#[macro_use]
extern crate sgx_tstd as std;
extern crate sgx_types;

regarding my demo (based on hyper-rustls-https-server actually), Cargo.toml of enclave

[dependencies]
sgx_libc = { path = "../../incubator-teaclave-sgx-sdk/sgx_libc" }
sgx_types = { path = "../../incubator-teaclave-sgx-sdk/sgx_types" }
sgx_crypto = { path = "../../incubator-teaclave-sgx-sdk/sgx_crypto" }
tokio = { version = "1.0", features = ["macros", "net", "rt-multi-thread"] }
jsonrpsee-http-server = "0.15.1"
base64 = "0.21.7"

features enabled: Rust_Std_Features := --features backtrace,net,thread,untrusted_time,untrusted_fs,unsupported_process,capi (added some)

head lines in lib.rs (exact same with hyper sample):

extern crate sgx_libc;
extern crate sgx_types;

and this one got compile error as posted

I noticed the head lines difference of the two, but not very clear about what it means

also I tried adding sgx_tstd dep and replace the above head lines with exact same lines in helloword example (with other imports fixed), got same compile error still

lythesia commented 8 months ago

got some findings by comparing cargo tree output of both

the fix is to remove sgx_types from Cargo.toml (and still need extern crate sgx_types), though I don't know why this import makes rustc see two copies of sgx_types code :(

lythesia commented 8 months ago

Did you include this in your Cargo.toml

[target.'cfg(not(target_vendor = "teaclave"))'.dependencies]
sgx_types = { git = "REPO_ADDRESS", branch = "BRANCH_NAME" }

I kind of know why here's cfg, which is defined in the target spec file, so it's not imported when building enclave crate still don't know how rustc find sgx_types w/o actually import it (in a explicit way), but I thinks it's rust question not teaclave's, I'll close this one, many thanks!