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

References in C FFI should emit the restrict qualifier in the tests #68

Open
gnzlbg opened this issue May 24, 2019 · 2 comments
Open

References in C FFI should emit the restrict qualifier in the tests #68

gnzlbg opened this issue May 24, 2019 · 2 comments

Comments

@gnzlbg
Copy link
Owner

gnzlbg commented May 24, 2019

When a C API has a restrict pointer, int foo(int * restrict); the pointer must not alias any other pointer. In the Rust side, this means that a raw *mut c_int pointer is not enough, and a &mut c_int must be used. Otherwise, the API is "unsound", as in, it allows passing it aliasing pointers.

However, some of these APIs do not require the pointer to point to initialize memory, that is, passing them a pointer to an uninitialized value is "ok" for C. Doing that via &mut c_int might be UB, and we might need to generate "something else" here.

This came up when validating the FFI wrapper in libc of lio_listio in FreeBSD, which takes a *const noalias *mut noalias T. Right now, we use *mut T and call it a day, but when validating the bindings, clang rightfully complains that our type is not compatible with the C type because it is missing the restrict qualifier. That is, we would be calling a function type that requires noalias from a prototype that does not, which is unsound.

AFAICT there is no way to express this from Rust. The first step here would be to start using &/&mut since that would make the API sound, even though that would forbid passing pointers to uninitialized memory.

cc @ralfj @Centril

@gnzlbg
Copy link
Owner Author

gnzlbg commented May 24, 2019

cc @joshtriplett

@gnzlbg
Copy link
Owner Author

gnzlbg commented May 27, 2019

Update: the lio_listio function in musl has the same issue.

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

No branches or pull requests

1 participant