Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

cjdb
Copy link

@cjdb cjdb commented Dec 16, 2024

_InputArray is a type-erased array wrapper that relies on casting a void* to its original type, based on the value of an enum. C++'s strict aliasing rules don't permit this, unless the type we're casting the pointer to is the same type as what was originally erased. This is especially problematic for std::vector because the code assumes that vectors are simply a contiguous sequence of bytes, which isn't the case. (See #26289 for that discussion).

This patch introduces an _ArrayOps type that enables virtual dispatch based on the type, which is always known at compile-time. Relying on the type system allows us to keep most of the logic constant, although a some simplifications were made possible (e.g. combining some of the matrix types into a single if-statement).

Note: This is a reworking of #26423, applied to 5.x, and focuses only on std::vector (#26423 attempted to change everything; this PR only changes what is absolutely necessary, and leaves the rest as future work). I've tested this change against the internal failing code, and it passes with these changes.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@cjdb
Copy link
Author

cjdb commented Dec 16, 2024

@mshabunin why was the doxygen commit added here?

Edit: perhaps this is a tree issue. I'll try rebasing and pushing again.

Yup, was a tree issue on my end. Sorry for the ping.

cjdb added 4 commits December 16, 2024 23:50
This commit refactors the `init` functions into constructors, so that
the incoming `_ArrayOps` member properly initialised. We add a pointer
to an `_ArrayOps` object, but don't do anything with it just yet.
@cjdb
Copy link
Author

cjdb commented Dec 19, 2024

I think I've cleared all the GCC warnings at this point. I'll look into the Windows issues if I get time later today.

It looks like the Android CI needs to flip the C++17 switch before it'll pass.

@vrabaud
Copy link
Contributor

vrabaud commented Jan 30, 2025

@cjdb, maybe you can rebase and update to our internal version? Thx

@vrabaud
Copy link
Contributor

vrabaud commented May 5, 2025

@cjdb, can you please rebase? Thank you.

@vrabaud
Copy link
Contributor

vrabaud commented May 5, 2025

@asmorkalov , should I backport that to 4.x? It slightly changes the API for InputArray.

@asmorkalov
Copy link
Contributor

@vpisarev What is your opinion?

@vrabaud
Copy link
Contributor

vrabaud commented May 13, 2025

RFC in clang: https://discourse.llvm.org/t/adding-a-size-based-vector-to-libc-s-unstable-abi/86306 and implementation: llvm/llvm-project#139632

@vrabaud
Copy link
Contributor

vrabaud commented Sep 16, 2025

The commit has been officially merged in clang which makes OpenCV not working with any upcoming clang version.
Update: this is only problematic if the libc++ ABI changes or if the unstable one is used.

@cjdb
Copy link
Author

cjdb commented Sep 17, 2025

The commit has been officially merged in clang which makes OpenCV not working with any upcoming clang version. Update: this is only problematic if the libc++ ABI changes or if the unstable one is used.

Thanks for flagging. llvm/llvm-project#139632 has been merged, and this shouldn't affect anyone just yet (it's a prologue to the main change). llvm/llvm-project#155330 changes std::vector's ABI in the libc++ unstable ABI, and this will render upstream OpenCV unusable for anyone that wants to use it with the libc++ unstable ABI.

@cjdb
Copy link
Author

cjdb commented Sep 17, 2025

@cjdb, can you please rebase? Thank you.

Sorry, I didn't see this until now. I'll coordinate with you on the rebasing effort.

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

Successfully merging this pull request may close these issues.

3 participants