Rewrite CompactValue to avoid undefined behavior from the use of a union for type-punning #1154

Closed
htpiv wants to merge 4 commits from htpiv/use-bit-cast into main
htpiv commented 2022-07-18 16:32:59 -07:00 (Migrated from github.com)

C++ does not, pedantically, allow the use of unions for type-punning in the way that C does. Most compilers, in practice, do support it; however, recent versions of MSVC appear to have a bug that cause bad code to be generated due to this U.B. (see: https://developercommunity.visualstudio.com/t/Bad-code-generated-for-std::isnan-compil/10082631). This led to a series of issues in the react-native-windows project, see:

In C++20, the <bit> header and bit_cast function provide a pleasant API for type-punning. Since C++20 is not universally available, if the feature-test macro for bit_cast is not defined, memcpy is used instead.

C++ does not, pedantically, allow the use of unions for type-punning in the way that C does. Most compilers, in practice, do support it; however, recent versions of MSVC appear to have a bug that cause bad code to be generated due to this U.B. (see: https://developercommunity.visualstudio.com/t/Bad-code-generated-for-std::isnan-compil/10082631). This led to a series of issues in the react-native-windows project, see: * https://github.com/microsoft/react-native-windows/issues/4122 * https://github.com/microsoft/react-native-windows/issues/8675 In C++20, the `<bit>` header and `bit_cast` function provide a pleasant API for type-punning. Since C++20 is not universally available, if the feature-test macro for `bit_cast` is not defined, memcpy is used instead.
facebook-github-bot commented 2022-07-18 16:33:04 -07:00 (Migrated from github.com)

Hi @htpiv!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

Hi @htpiv! Thank you for your pull request and welcome to our community. # Action Required In order to merge **any pull request** (code, docs, etc.), we **require** contributors to sign our **Contributor License Agreement**, and we don't seem to have one on file for you. # Process In order for us to review and merge your suggested changes, please sign at <https://code.facebook.com/cla>. **If you are contributing on behalf of someone else (eg your employer)**, the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the **pull request will be tagged** with `CLA signed`. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it. If you have received this in error or have any questions, please contact us at [cla@fb.com](mailto:cla@fb.com?subject=CLA%20for%20facebook%2Fyoga%20%231154). Thanks!
facebook-github-bot commented 2022-07-19 11:38:33 -07:00 (Migrated from github.com)

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
facebook-github-bot commented 2022-07-19 11:54:16 -07:00 (Migrated from github.com)

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
facebook-github-bot commented 2022-07-22 11:57:41 -07:00 (Migrated from github.com)

@rozele has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@rozele has imported this pull request. If you are a Meta employee, you can view this diff [on Phabricator](https://www.internalfb.com/diff/D38082048).
rozele (Migrated from github.com) reviewed 2022-07-25 10:02:53 -07:00
@@ -12,0 +13,4 @@
#include <bit>
#else
#include <cstring>
#endif
rozele (Migrated from github.com) commented 2022-07-25 10:01:53 -07:00

Looks like internal builds are failing because std::memcpy requires #include <cstring>

Looks like internal builds are failing because std::memcpy requires `#include <cstring>`
rozele (Migrated from github.com) commented 2022-07-25 10:02:43 -07:00

Should this be:

    return std::bit_cast<float>(u);
Should this be: ``` return std::bit_cast<float>(u); ```

Pull request closed

Sign in to join this conversation.
No description provided.