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

MTLPixelFormat and related enumeration types are unsound? #209

Open
drewcrawford opened this issue Apr 26, 2021 · 1 comment
Open

MTLPixelFormat and related enumeration types are unsound? #209

drewcrawford opened this issue Apr 26, 2021 · 1 comment
Labels

Comments

@drewcrawford
Copy link

Trying to figure out if I'm missing some neat trick. At first glance it appears that MTLPixelFormat and related are unsound.

For example, MTLPixelFormat is returned by objc here. If I have followed the controlflow correctly back to the source, I believe we transmute it here, e.g. we are transmuting the value from C.

The trouble with this is that on the C side, MTLPixelFormat is a glorified typedef for NSUInteger

typedef NS_ENUM(NSUInteger, MTLPixelFormat)

which has a legal range usually of 4 million values, any of which may be returned from a ObjC API. If the value returned is unknown to the Rust enum, this is UB for the Rust memory model.

I should mention to be clear that the "values unknown to Rust" problem is something that typically happens when new pixel formats are added in OS releases, such as depth16Unorm fairly recently. So we might expect older programs compiled with older metal-rs to UB when run on newer OS.

I believe this needs to be represented with constants and an integer type internally. Externally, it may be feasible to add an "unknown" case to the enum with a integer payload type. I am a little unclear on what the situation is with breakages (e.g., adding a payload to #[repr(u64)] pub enum will be a different memory layout) or if it would make more sense to just expose constant values as the binding.

Further reading

@kvark kvark added the bug label Apr 27, 2021
@kvark
Copy link
Member

kvark commented Apr 27, 2021

Thank you for raising this! Agree this is unsafe and needs to be fixed.
I'm thinking that we should fix it fully internally. All the functions returning enums should read integers first, check their value, and only then transmute. They'd panic otherwise, which is not expected but still safe (also, controversial).

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

No branches or pull requests

2 participants