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

api(IB): Add span-based constuctors to ImageBuf #4401

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

lgritz
Copy link
Collaborator

@lgritz lgritz commented Sep 1, 2024

The flavor of ImageBuf that wraps caller-owned memory is constructed or reset by merely passing a raw pointer and it assumes that the range of memory implied by the strides is entirely safe to read from or write to. And even if correct, there is not much within ImageBuf internals to prevent bugs from buffer overruns.

This PR introduces a new constructor and reset() method to ImageBuf where instead of taking a raw pointer, it takes a span describing the extent of the safely addressible memory (and also an optional "pointer to first pixel" within the buffer, which is necessary if you're using negative strides or for some other reason are describing safe memory that starts before the location of pixel 0). That span is stored in the ImageBuf, and also it remembers if it was a cspan (which should indicate a read-only IB) or a non-const span (which should wrap writable memory).

At this moment, I'm just adding this API and will start using and documenting it as the perferred method, with the raw pointer versions being regarded as unsafe for now (and maybe some day will be deprecated?). But we're not yet using the span internally for safety checks.

In the future, my intent is to gradually add safeguards to do bounds checking against the span for IB operations that touch the wrapped memory. Because we'll most likely bounds check only in debug mode, it shouldn't ever impact performance, but it will make debugging, sanitizer, and fuzz tests more robust to catch and zero in on buffer overruns. I also like how it forces the caller, at the call site, to think hard about exactly what range of memory the IB is wrapping, and not merely pass a single pointer and hope it works out.

I also changed all ImageBuf ctrs and resets within the OIIO codebase to use the new span-based versions.

This is part of an overall trend to migrate from raw pointer based API calls to span-based API design as much as possible.

@lgritz
Copy link
Collaborator Author

lgritz commented Sep 6, 2024

Any objections or concerns?

@lgritz
Copy link
Collaborator Author

lgritz commented Sep 6, 2024

I will merge this tomorrow if nobody has comments by then.

The flavor of ImageBuf that wraps caller-owned memory is constructed
or reset by merely passing a raw pointer and it assumes that the range
of memory implied by the strides is entirely safe to read from or
write to. And even if correct, there is not much within ImageBuf
internals to prevent bugs from buffer overruns.

This PR introduces a new constructor and reset() method to ImageBuf
where instead of taking a raw pointer, it takes a span describing the
extent of the safely addressible memory (and also an optional "pointer
to first pixel" within the buffer, which is necessary if you're using
negative strides or for some other reason are describing safe memory
that starts before the location of pixel 0). That span is stored in
the ImageBuf, and also it remembers if it was a cspan (which should
indicate a read-only IB) or a non-const span (which should wrap
writable memory.

At this moment, I'm just adding this API and will start using and
documenting it as the perferred method, with the raw pointer versions
being regarded as unsafe for now (and maybe some day will be
deprecated?).  But we're not yet using the span in other IB methods.

In the future, my intent is to gradually add safeguards to do bounds
checking against the span for IB operations that touch the wrapped
memory. Because we'll most likely bounds check only in debug mode, it
shouldn't ever impact performance, but it will make debugging,
sanitizer, and fuzz tests more robust to catch and zero in on buffer
overruns. I also like how it forces the caller, at the call site, to
think hard about exactly what range of memory the IB is wrapping, and
not merely pass a single pointer and hope it works out.

I also dhanged all ImageBuf ctrs and resets within the OIIO codebase
to use the new span-based versions

This is part of an overall trend to migrate from raw pointer based API
calls to span-based API design.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
@lgritz lgritz merged commit c0bf970 into AcademySoftwareFoundation:master Sep 9, 2024
26 checks passed
@lgritz lgritz deleted the lg-ibctr-span branch September 9, 2024 01:39
zachlewis pushed a commit to zachlewis/OpenImageIO that referenced this pull request Sep 16, 2024
…ation#4401)

The flavor of ImageBuf that wraps caller-owned memory is constructed or
reset by merely passing a raw pointer and it assumes that the range of
memory implied by the strides is entirely safe to read from or write to.
And even if correct, there is not much within ImageBuf internals to
prevent bugs from buffer overruns.

This PR introduces a new constructor and reset() method to ImageBuf
where instead of taking a raw pointer, it takes a span describing the
extent of the safely addressible memory (and also an optional "pointer
to first pixel" within the buffer, which is necessary if you're using
negative strides or for some other reason are describing safe memory
that starts before the location of pixel 0). That span is stored in the
ImageBuf, and also it remembers if it was a cspan (which should indicate
a read-only IB) or a non-const span (which should wrap writable memory).

At this moment, I'm just adding this API and will start using and
documenting it as the perferred method, with the raw pointer versions
being regarded as unsafe for now (and maybe some day will be
deprecated?). But we're not yet using the span internally for safety
checks.

In the future, my intent is to gradually add safeguards to do bounds
checking against the span for IB operations that touch the wrapped
memory. Because we'll most likely bounds check only in debug mode, it
shouldn't ever impact performance, but it will make debugging,
sanitizer, and fuzz tests more robust to catch and zero in on buffer
overruns. I also like how it forces the caller, at the call site, to
think hard about exactly what range of memory the IB is wrapping, and
not merely pass a single pointer and hope it works out.

I also changed all ImageBuf ctrs and resets within the OIIO codebase to
use the new span-based versions.

This is part of an overall trend to migrate from raw pointer based API
calls to span-based API design as much as possible.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
Signed-off-by: Zach Lewis <zachcanbereached@gmail.com>
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

Successfully merging this pull request may close these issues.

1 participant