wasm-micro-runtime
wasm-micro-runtime copied to clipboard
SGX: printf returns the msg size stored the buffer
Dear WAMR developers,
When working with WASI calls inside Intel SGX, I discovered a glitch when writing into stdout/stderr using the code snippet present here:
https://github.com/bytecodealliance/wasm-micro-runtime/blob/0020b3ae6874d275a7de9fc95fe078355363aab5/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c#L1249-L1262
Indeed, when this code is executed for printing text into stdout/stderr, the implementation of os_printf
for SGX always returns 0, since the return value of the OCALL is not considered. As a result, writing to stdout/stderr from the enclave creates an infinite loop, where the text is written in the console over and over.
While I could not find where the loop occurs (either in WAMR or in musl for Wasm), I propose to change the implementation of the os_printf
for Intel SGX, so the number of bytes is returned by its implementation by using the return value of the formatting function (vsnprintf
). Note that we could also use the return value of the printf
called in the OCALL, but I prefer this approach since we can trust the return value of vsnprintf
, which is not the case with the concrete printf
.
Feel free to comment or suggest changes if you feel this approach is not the right way to fix the issue. I'll gladly apply them.
Cheers!
@JamesMenetrey I submitted PR #1387 to fix this issue, could you please help check whether it works for you? Thanks.
Fixed with PR #1387.
Hello @wenyongh,
Many thanks for your update. I'll keep you informed in a couple of weeks (I'm currently on vacation).
Cheers!
Hey @wenyongh,
I am coming back to you regarding #1387. My code with your fix works perfectly! Thanks!
Cheers