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

-ftrivial-auto-var-init=zero issues with pointers to data members #63471

Open
dangelog opened this issue Jun 23, 2023 · 4 comments
Open

-ftrivial-auto-var-init=zero issues with pointers to data members #63471

dangelog opened this issue Jun 23, 2023 · 4 comments
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' documentation

Comments

@dangelog
Copy link

dangelog commented Jun 23, 2023

I can't find accurate documentation for what -ftrivial-auto-var-init=zero is meant to do; https://clang.llvm.org/docs/ClangCommandLineReference.html simply says

Initialize trivial automatic stack variables. Defaults to ‘uninitialized’. must be ‘uninitialized’, ‘zero’ or ‘pattern’.

I assume that means to memset(0) their storage. 0 is a bit pattern that works almost universally to set a "safe" default. However, pointers to data members are a problem: on Itanium, a null pointer to data member is represented by -1u, and not 0.

https://itanium-cxx-abi.github.io/cxx-abi/abi.html#data-member-pointers

This means that this snippet hits the assert under -ftrivial-auto-var-init=zero:

#include <cassert>
struct S {};

int main() {
    int S::*ptr;
    assert(ptr == nullptr);
}

https://gcc.godbolt.org/z/7sb6GcbPE

IMHO it would be more useful to have -ftrivial-auto-var-init=zero to mean "to value-initialize automatic variables", including non-static data members of classes, recursively, before a constructor is eventually run.

Such value-initialization for scalar taypes resolves into zero-initialization (and not zero-filling), as per https://eel.is/c++draft/dcl.init#general-9.3 , so the name =zero is still somehow appropriate. The difference is that zero-initialization will correctly sets all pointers types to null.

@tkoeppe
Copy link
Contributor

tkoeppe commented Jun 23, 2023

What does -ftrivial-auto-var-init=pattern do?

@dangelog
Copy link
Author

It seems to fill the variable with repeated 0xAA bytes. On GCC, it's filled with 0xFE.

https://gcc.godbolt.org/z/c7xv9e5P9

@dangelog
Copy link
Author

Hi,

It's been pointed out to me that I used some inaccurate wording in the report, so let me try clarify:

IMHO it would be more useful to have -ftrivial-auto-var-init=zero to mean "to value-initialize automatic variables", including non-static data members of classes, recursively, before a constructor is eventually run.

To turn this into a more correct statement: I would like for -ftrivial-auto-var-init=zero to fill the storage of automatic variables using the same value that would be there, had the automatic variable been zero-initialized.

  • For scalars, this would mean filling integers, enums and floating points with their representation of 0 (which is indeed 0x00 filling for integers, since their representation is mandated. It's also 0x00 for floating points on IEEE754, does Clang/LLVM support some more exoteric FP type?), and pointers with whatever the ABI mandates for their nullptr representation.
  • For classes, this means filling the storage of their scalar subobjects as indicated above, and zero-filling padding bits.

Hope this is more accurate.

--

Also: please let me know if you think this should be considered a bugfix for -ftrivial-auto-var-init=zero, or an outright new feature with its own command line switch.
I personally think it should be a bugfix: the behavior is going to be the same for most things, but users are going to get the added benefit that reads of uninitialized pointers always give back nullptr, no matter the kind of pointer / the ABI.

--

Twin GCC feature request: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110404

@EugeneZelenko EugeneZelenko added documentation clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' and removed new issue labels Jun 27, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Jun 27, 2023

@llvm/issue-subscribers-clang-driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' documentation
Projects
None yet
Development

No branches or pull requests

4 participants