Skip to content

Fix UB in FFI State Get/Set Backwards Compatibility Case - #39155

Closed
mgoldfarb-nvidia wants to merge 3 commits into
openxla:mainfrom
mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state
Closed

mgoldfarb-nvidia wants to merge 3 commits into
openxla:mainfrom
mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state

Conversation

@mgoldfarb-nvidia

Copy link
Copy Markdown
Contributor

📝 Summary of Changes
Replaces reinterpret_cast with std::memcpy to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
🐛 Bug Fix

Also fixes the implementation to correctly set the state.
@mgoldfarb-nvidia

Copy link
Copy Markdown
Contributor Author

FYI @ezhulenev

Comment thread xla/ffi/ffi_api.cc
auto* v02 = reinterpret_cast<XLA_FFI_State_Args_V02*>(args);
// memcpy to safely avoid U.B.
XLA_FFI_State_Args_V02 v02{};
std::memcpy(&v02, args, XLA_FFI_State_Args_V02_STRUCT_SIZE);

@ezhulenev ezhulenev Mar 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why memcpy and what UB it avoids?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://en.cppreference.com/w/cpp/language/reinterpret_cast.html

Strict aliasing between two unrelated types causes UB. So reads and writes through args may not reflect . The memcpy is the pre-C++20 compliant way to circumvent this rule. In many cases compiler should optimize away an actual copy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use absl::bit_cast instead? It's a backport of std::bit_cast and is basically a wrapper around the memcpy pattern; plus some additional checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem with absl::bit_cast (and std::bit_cast) is it (looks like it) requires the type sizes to be equal for writes. At least in the fallback path that uses std::memcpy

inline Dest bit_cast(const Source& source) {
  Dest dest;
  memcpy(static_cast<void*>(std::addressof(dest)),
         static_cast<const void*>(std::addressof(source)), sizeof(dest));
  return dest;
}

Its possible we could modify this function s.t. we pass void* from the FFI API then first cast to size_t to determine the size then cast to one concrete type. However, this could still be a U.B. violation and a bigger change than makes sense unless we start adding more cases where we are making ABI breaking changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. In that case the current version is fine.

Comment thread xla/ffi/ffi_api.cc Resolved
@ezhulenev
ezhulenev requested a review from beckerhe March 13, 2026 02:20
Comment thread xla/ffi/ffi_api.cc
auto* v02 = reinterpret_cast<XLA_FFI_State_Args_V02*>(args);
// memcpy to safely avoid U.B.
XLA_FFI_State_Args_V02 v02{};
std::memcpy(&v02, args, XLA_FFI_State_Args_V02_STRUCT_SIZE);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use absl::bit_cast instead? It's a backport of std::bit_cast and is basically a wrapper around the memcpy pattern; plus some additional checks.

Comment thread xla/ffi/ffi_api.cc Resolved
Comment thread xla/ffi/ffi_api.cc Resolved
copybara-service Bot pushed a commit that referenced this pull request Mar 13, 2026
Imported from GitHub PR #39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02 by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6 by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

Reverts d1bced5

FUTURE_COPYBARA_INTEGRATE_REVIEW=#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Mar 13, 2026
Imported from GitHub PR openxla/xla#39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02a0aa43ff92219ba2b1aa4ddf64306e1ca by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6bc35632fe2984021f43d9b1414d4c7c3b by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

Reverts 95aacc5

FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6bc35632fe2984021f43d9b1414d4c7c3b
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Mar 16, 2026
Imported from GitHub PR openxla/xla#39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02a0aa43ff92219ba2b1aa4ddf64306e1ca by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6bc35632fe2984021f43d9b1414d4c7c3b by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6bc35632fe2984021f43d9b1414d4c7c3b
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit that referenced this pull request Mar 16, 2026
Imported from GitHub PR #39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02 by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6 by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

FUTURE_COPYBARA_INTEGRATE_REVIEW=#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit that referenced this pull request Mar 16, 2026
Imported from GitHub PR #39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02 by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6 by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

FUTURE_COPYBARA_INTEGRATE_REVIEW=#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Mar 16, 2026
Imported from GitHub PR openxla/xla#39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02a0aa43ff92219ba2b1aa4ddf64306e1ca by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6bc35632fe2984021f43d9b1414d4c7c3b by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#39155 from mgoldfarb-nvidia:mgoldfarb/fix_ffi_backwards_compat_bug_state d18d5b6bc35632fe2984021f43d9b1414d4c7c3b
PiperOrigin-RevId: 883152448
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Mar 16, 2026
Imported from GitHub PR openxla/xla#39155

📝 Summary of Changes
Replaces `reinterpret_cast` with `std::memcpy` to avoid U.B.
Implementation of Get_State was not setting state pointer when using the backward compatibility path

🎯 Justification
Support state with V0.2 FFI in latest JAX.

🚀 Kind of Contribution
 🐛 Bug Fix

Copybara import of the project:

--
b0bed02a0aa43ff92219ba2b1aa4ddf64306e1ca by Michael Goldfarb <[email protected]>:

Fix UB in state get/set when using backwards compat mode.
Also fixes the implementation to correctly set the state.

--
d18d5b6bc35632fe2984021f43d9b1414d4c7c3b by Michael Goldfarb <[email protected]>:

Fix write to state.

Merging this change closes #39155

PiperOrigin-RevId: 884428841
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.

3 participants