wasm-micro-runtime icon indicating copy to clipboard operation
wasm-micro-runtime copied to clipboard

SGX: printf returns the msg size stored the buffer

Open JamesMenetrey opened this issue 1 year ago • 0 comments

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 avatar Aug 05 '22 13:08 JamesMenetrey

@JamesMenetrey I submitted PR #1387 to fix this issue, could you please help check whether it works for you? Thanks.

wenyongh avatar Aug 15 '22 07:08 wenyongh

Fixed with PR #1387.

wenyongh avatar Aug 16 '22 09:08 wenyongh

Hello @wenyongh,

Many thanks for your update. I'll keep you informed in a couple of weeks (I'm currently on vacation).

Cheers!

JamesMenetrey avatar Aug 16 '22 14:08 JamesMenetrey

Hey @wenyongh,

I am coming back to you regarding #1387. My code with your fix works perfectly! Thanks!

Cheers

JamesMenetrey avatar Sep 02 '22 15:09 JamesMenetrey