Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined behavior in wasi implementation (and public-facing API is error prone) #457

Open
SoniEx2 opened this issue Dec 19, 2023 · 3 comments

Comments

@SoniEx2
Copy link

SoniEx2 commented Dec 19, 2023

The wasi implementation is full of code like (e.g.)

m3ApiRawFunction(m3_wasi_generic_args_sizes_get)
{
    m3ApiReturnType  (uint32_t)
    m3ApiGetArgMem   (__wasi_size_t *      , argc)
    m3ApiGetArgMem   (__wasi_size_t *      , argv_buf_size)

    m3ApiCheckMem(argc,             sizeof(__wasi_size_t));
    m3ApiCheckMem(argv_buf_size,    sizeof(__wasi_size_t));

but this can create UB by blowing past the end of the allocated memory when doing m3ApiGetArgMem. Simply blowing past the allocation is enough to make it UB, regardless of the presence of m3ApiCheckMem.

This entire m3ApiGetArgMem API is extremely error-prone.

@vshymanskyy
Copy link
Member

Any suggestions on how to fix it?

@SoniEx2
Copy link
Author

SoniEx2 commented Dec 21, 2023

we would get rid of m3ApiGetArgMem entirely.

m3ApiCheckMem seems inherently unsound too, as in most cases you'd either be passing valid pointers which pass the mem check, or you'd be passing invalid pointers which are UB. so that would have to be replaced.

alternatively: instead of getting rid of m3ApiGetArgMem, we could bake m3ApiCheckMem into it, but without the UB, and get rid of m3ApiCheckMem instead.

edit: hmm m3ApiCheckMem actually looks fine for use on arrays... but the initial m3ApiGetArgMem should have its own checks.

@SoniEx2
Copy link
Author

SoniEx2 commented Dec 23, 2023

another problem with m3ApiGetArgMem is pointer alignment...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants