lucadibello opened a new pull request, #54:
URL: https://github.com/apache/teaclave-java-tee-sdk/pull/54

   This PR builds on #53  (Ubuntu 26.04 / SGX SDK 2.30 / GraalVM 25 port) and 
contains its commits until that one is merged. Only the last commit, _"Support 
multi-threaded hosts and in-enclave threads in TEE_SDK mode"_, is new here.
   
   ## Description
   
   In `TEE_SDK` mode the SDK breaks as soon as more than one thread is 
involved. Concurrent ECALLs from several host threads, or a `Thread.start()` 
inside the enclave, end up sharing or freeing each other's GraalVM 
`IsolateThread`s, which shows up as crashes or as calls that silently return 
nothing. I hit this while building [Confidential 
Storm](https://github.com/lucadibello/confidential-storm), a stream processing 
framework that calls into one enclave from many worker threads and runs 
background threads inside it (Master's thesis: [*Secure Differentially Private 
Stream 
Processing*](https://thesis.bul.sbu.usi.ch/theses/2521-2526DiBello/pdf?1782979668),
 USI, 2026).
   
   The root cause is thread identity. Inside the enclave `pthread_self()` 
returns the TCS, so to SubstrateVM a TCS *is* the OS thread. The host thread 
that entered through it is invisible. This PR makes the enclave runtime follow 
this model:
   
   - **Concurrent ECALLs**: an ECALL reuses the `IsolateThread` already 
registered on its TCS, so the isolate holds at most one per TCS, regardless of 
the host thread that comes in.
   
   - **In-enclave threads**: the enclave is now linked with 
`-Wl,--wrap=pthread_create,--wrap=pthread_join`, allowing us to wrap these 
methods and perform extra operations. Now, any new thread first unregisters any 
`IsolateThread` left on its TCS by an earlier ECALL. Before, SVM did not 
unregister isolates left on the assigned TCS, even if it belonged to another 
host thread.
   
   - **Timed waits:** SGX has no timed condition variable and the 
`pthread_cond_timedwait` shim returned immediately, so `Thread.sleep`, 
`parkNanos` and `wait(ms)` would busy-spin rather than properly set the thread 
to sleep. The shim now sleeps on the host (at most 1 ms per call) and lets the 
caller re-check its condition.
   
   - **Per-ECALL state:** The callback table was kept in a static field that 
concurrent ECALLs overwrote, and it's is now passed per ECALL to completeavoid 
data races. `SecureRandom` entropy comes from `sgx_read_rand` directly 
(`NativeEnclaveRandom`) rather than using callbacks.
   
   - **Failure reporting:** The host JNI ignored the ECALL's `sgx_status_t`, so 
e.g. `SGX_ERROR_OUT_OF_TCS` turned into an empty result. It now throws the 
matching exception with the error code. A failed `create` no longer leaks the 
enclave, and the enclave only copies results into host buffers that 
`sgx_is_outside_enclave` accepts.
   
   ## Limits
   
   As also noted in the README: on `Enclave.destroy()`, SVM waits for 
in-enclave threads to exit with no timeout (`TearDownFailureNanos` is 0), so 
background threads must be stopped first. If a thread is unresponsive, then 
also the enclave becomes unresponsive.
   
   ## How has this been tested?
   
   Similarly as #53, I unfortunately have limited access to machines with 
SGX2-enabled CPUs, so I could only test in simulation mode (`SGX_MODE=SIM`). 
The enclave build/signing procedure is exactly the same as for harware, but 
runs on the SGX SDK's simulation runtime, so hardware TCS scheduling / EPC 
limits are not enforced. As also cited in the previous PR, remote attestation 
needs real hardware, so its test is skipped. I would be grateful if someone 
with SGX2 hardware could run `make install && make test` on this branch, in 
particular `TestEnclaveConcurrency`.
   
   `TestEnclaveConcurrency` now covers concurrent ECALLs, in-enclave and 
background threads (including `SecureRandom` and GC pressure from them), thread 
churn, 48 host threads against the 50-TCS limit, and lifecycle races (destroy 
during ECALLs, back-to-back create/destroy, concurrent load and invoke).
   
   In the Ubuntu 26.04 image (`tools/cicd/ubuntu-26.04/Dockerfile`):
   
   ```sh
   make install SGX_MODE=SIM
   make test SGX_MODE=SIM
   ```
   
   - `test/enclave`: 5 tests, 0 failures.
   - `test/host` (`MOCK_IN_JVM`, `MOCK_IN_SVM` and `TEE_SDK` in simulation): 
106 tests, 0 failures, 1 skipped (`TestRemoteAttestation`).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to