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

[libcxx][ios] initialize __fill_val_ in _FillHelper #110279

Merged
merged 1 commit into from
Sep 30, 2024

Conversation

daltenty
Copy link
Member

@daltenty daltenty commented Sep 27, 2024

This is a small fix to #89305. In the __init function of _FillHelper, __fill_val_ was left uninitialized. This worked for the implementation in the PR because we always checked __set_ before trying to read it, and would initialize if it was unset.

However it turns out in earlier versions of the header (at least on AIX which followed this path), we do a read of __fill_val_ even if __set_ was false before initializing, to check if it matched the sentinel value, so this causes undesired behaviour and UB.

(cherry picked from commit 6c2bb185d91552032b1140d7c08b43ecf114e066)
@daltenty daltenty requested a review from a team as a code owner September 27, 2024 15:02
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Sep 27, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 27, 2024

@llvm/pr-subscribers-libcxx

Author: David Tenty (daltenty)

Changes

This is a small fix to #89305. In the __init function of _FillHelper, __fill_val_ was left uninitialized. This worked for the implementation in the PR because we always checked __set_ before trying to read it, and would initialize if it was unset.

However it turns out in earlier versions of the header (at least on AIX which followed this path), we do a read of __fill_val_ even if __set_ was false before initializing to check if it matched the sentinel value, so this causes undesired behaviour and UB.


Full diff: https://github.com/llvm/llvm-project/pull/110279.diff

1 Files Affected:

  • (modified) libcxx/include/ios (+4-1)
diff --git a/libcxx/include/ios b/libcxx/include/ios
index 61a05fadd29a17..d4f15a269a11a6 100644
--- a/libcxx/include/ios
+++ b/libcxx/include/ios
@@ -524,7 +524,10 @@ template <class _Traits>
 // Attribute 'packed' is used to keep the layout compatible with the previous
 // definition of the '__fill_' and '_set_' pair in basic_ios on AIX & z/OS.
 struct _LIBCPP_PACKED _FillHelper {
-  _LIBCPP_HIDE_FROM_ABI void __init() { __set_ = false; }
+  _LIBCPP_HIDE_FROM_ABI void __init() {
+    __set_      = false;
+    __fill_val_ = _Traits::eof();
+  }
   _LIBCPP_HIDE_FROM_ABI _FillHelper& operator=(typename _Traits::int_type __x) {
     __set_      = true;
     __fill_val_ = __x;

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

How did you catch the bug? Is it possible to add a test that triggers that?

@daltenty
Copy link
Member Author

How did you catch the bug? Is it possible to add a test that triggers that?

We have internal runs that test using the old headers with the new dylib (I guess it would be nice to have such runs for at least a few releases in the community CI ideally)

Adding a legitimate test against the current code doesn't seem straight forward, this depends on the old internal behaviour of basic_ios<_CharT, _Traits>::fill() in the header

Copy link
Contributor

@xingxue-ibm xingxue-ibm left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks for the fix!

@daltenty daltenty merged commit f3a4def into llvm:main Sep 30, 2024
64 checks passed
@daltenty daltenty deleted the daltenty/fix-ios-_FillHelper-init branch September 30, 2024 23:24
VitaNuo pushed a commit to VitaNuo/llvm-project that referenced this pull request Oct 2, 2024
This is a small fix to llvm#89305.
In the `__init` function of `_FillHelper`, `__fill_val_` was left
uninitialized. This worked for the implementation in the PR because we
always checked `__set_` before trying to read it, and would initialize
if it was unset.

However it turns out in earlier versions of the header (at least on AIX
which followed this path), we do a read of `__fill_val_` even if
`__set_` was false before initializing, to check if it matched the
sentinel value, so this causes undesired behaviour and UB.
VitaNuo pushed a commit to VitaNuo/llvm-project that referenced this pull request Oct 2, 2024
This is a small fix to llvm#89305.
In the `__init` function of `_FillHelper`, `__fill_val_` was left
uninitialized. This worked for the implementation in the PR because we
always checked `__set_` before trying to read it, and would initialize
if it was unset.

However it turns out in earlier versions of the header (at least on AIX
which followed this path), we do a read of `__fill_val_` even if
`__set_` was false before initializing, to check if it matched the
sentinel value, so this causes undesired behaviour and UB.
Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
This is a small fix to llvm#89305.
In the `__init` function of `_FillHelper`, `__fill_val_` was left
uninitialized. This worked for the implementation in the PR because we
always checked `__set_` before trying to read it, and would initialize
if it was unset.

However it turns out in earlier versions of the header (at least on AIX
which followed this path), we do a read of `__fill_val_` even if
`__set_` was false before initializing, to check if it matched the
sentinel value, so this causes undesired behaviour and UB.
xgupta pushed a commit to xgupta/llvm-project that referenced this pull request Oct 4, 2024
This is a small fix to llvm#89305.
In the `__init` function of `_FillHelper`, `__fill_val_` was left
uninitialized. This worked for the implementation in the PR because we
always checked `__set_` before trying to read it, and would initialize
if it was unset.

However it turns out in earlier versions of the header (at least on AIX
which followed this path), we do a read of `__fill_val_` even if
`__set_` was false before initializing, to check if it matched the
sentinel value, so this causes undesired behaviour and UB.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants