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 263 forks source link

support cargo test #232

Open brenzi opened 4 years ago

brenzi commented 4 years ago

Is there any chance we can make cargo test work with our enclave code?

it would be great if we could use standard cargo test. Right now we can only do handmade testing which is a lot less convenient to track

right now we get a clash between std and sgx_tstd:

   Compiling substratee-worker-enclave v0.6.0 (/home/abrenzikofer/substraTEE-worker/enclave)
error: duplicate lang item in crate `std` (which `test` depends on): `f32_runtime`.
  |
  = note: the lang item is first defined in crate `sgx_tstd` (which `substratee_worker_enclave` depends on)

error: duplicate lang item in crate `std` (which `test` depends on): `f64_runtime`.
  |
  = note: the lang item is first defined in crate `sgx_tstd` (which `substratee_worker_enclave` depends on)

error: duplicate lang item in crate `std` (which `test` depends on): `panic_impl`.
  |
  = note: the lang item is first defined in crate `sgx_tstd` (which `substratee_worker_enclave` depends on)

error: duplicate lang item in crate `std` (which `test` depends on): `begin_panic`.
  |
  = note: the lang item is first defined in crate `sgx_tstd` (which `substratee_worker_enclave` depends on)

error: duplicate lang item in crate `std` (which `test` depends on): `oom`.
  |
  = note: the lang item is first defined in crate `sgx_tstd` (which `substratee_worker_enclave` depends on)

error: aborting due to 5 previous errors

error: could not compile `substratee-worker-enclave`.
dingelish commented 4 years ago

generally speaking, cargo test collects all #[test]s and creates a binary with a fixed driver and libtest. we don't have chance to change the compiler ad libtest. so the only chance to use cargo test is to test the untrusted app, not the enclave.

good news is that we can use inventory and a proc-macro to implement something very similar to #[test] which collect every test fn pointer (decorated by the customized proc macro attribute) into a static array and then drive these test fn ptrs with our own prebuilt driver. in this way, though we cannot use #[test], we can construct an only with all test fns automatically, and export only 1 ECALL fn for triggering the tests.

dingelish commented 4 years ago

though our initial PoC works, it has problem with ld from LLVM9, which optimizes out ctors. related issues: https://github.com/mmastrac/rust-ctor/issues/27 https://github.com/mmastrac/rust-ctor/issues/43

brenzi commented 4 years ago

this approach is acceptable. important for us is that tests are collected automatically and results are summarized clearly

volcano0dr commented 4 years ago

though our initial PoC works, it has problem with ld from LLVM9, which optimizes out ctors. related issues: mmastrac/rust-ctor#27 mmastrac/rust-ctor#43

Regarding the # [ctros] link issue, unused symbols are pruned, even if # [used] is used. After testing, the following method can be used to temporarily solve this problem: When linking test-enclave.a in your Makefile, use the whole-archice ld command line option -Wl,-whole-archive -ltest-enclave -Wl,-no-whole-archive

mssun commented 4 years ago

@brenzi, please refer to this PR (https://github.com/apache/incubator-teaclave/pull/269) on how to use inventory to automatically collect test cases.

dingelish commented 4 years ago

@mssun perhaps we could "downport" it to the sdk and make it available to developers like @brenzi

mssun commented 4 years ago

In addition to the automatic collection of test cases, we can also provide a cargo subcommand such as cargo teaclave. When executing cargo teaclave test, it can automatically provide an integration test driver.

The only thing bothers me about "downport" is that the update of SDK requires huge efforts. Test drivers, however, is not as stable as std. A better way is to implement this in the Teaclave platform first and stablize in the SDK when necessary.

In summary, @brenzi, you can simply implement your own test drivers first (similar with https://github.com/apache/incubator-teaclave/pull/269). Then, we can gradually improve the test toolchain in the SDK.