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

MTLBuffer contents pointer is nullable #718

Open
K0bin opened this issue Mar 16, 2025 · 2 comments
Open

MTLBuffer contents pointer is nullable #718

K0bin opened this issue Mar 16, 2025 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them bug Something isn't working I-unsound A soundness hole

Comments

@K0bin
Copy link

K0bin commented Mar 16, 2025

objc2_metal:

        /// Returns the data pointer of this buffer's shared copy.
        #[unsafe(method(contents))]
        #[unsafe(method_family = none)]
        fn contents(&self) -> NonNull<c_void>;

Apple documentation:

A pointer to the shared copy of the buffer data, or NULL for buffers allocated with a private resource storage mode (MTLStorageModePrivate).

@madsmtm madsmtm added A-framework Affects the framework crates and the translator for them I-unsound A soundness hole labels Mar 17, 2025
@madsmtm
Copy link
Owner

madsmtm commented Mar 17, 2025

Damn. This is a bug in Apple's headers (the header is marked NS_ASSUME_NONNULL_BEGIN, so contents should have been opted out with some sort of __nullable). Swift is actually also affected by this, they map the pointer as UnsafeMutableRawPointer instead of UnsafeMutableRawPointer?.

It's unfortunately a breaking change for me to properly fix, and while you can sometimes argue that it's fine when it's done to fix soundness issues, this isn't really a soundness issue for consumers unless they use MTLStorageModePrivate.

I'll probably fix for now by checking internally on access whether the pointer is NULL.

@madsmtm
Copy link
Owner

madsmtm commented Mar 17, 2025

Hmm, actually, I know that's what the docs say, but how are you observing this?

The small test below seems to indicate that the docs for contents are actually incorrect?

use objc2_metal::{MTLBuffer, MTLCreateSystemDefaultDevice, MTLDevice, MTLResourceOptions};

#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {}

fn main() {
    let device = MTLCreateSystemDefaultDevice().unwrap();
    let buffer = device.newBufferWithLength_options(100, MTLResourceOptions::StorageModePrivate).unwrap();
    println!("{:?}", buffer.contents());
}

@madsmtm madsmtm added the bug Something isn't working label Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them bug Something isn't working I-unsound A soundness hole
Projects
None yet
Development

No branches or pull requests

2 participants