llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

ftrivial-auto-var-init adds init for some types like std::string (unnecessarily?)

Open jvoung opened this issue 3 years ago • 5 comments

It seems like (some?) auto vars of type std::string can have "auto-init" even though the constructor should have initialized the variable.

Examples in libstdc++: https://godbolt.org/z/9c5Gxra9a libc++: https://godbolt.org/z/c1jM8bG3j It seems like GCC doesn't do this with their version of ftrivial-auto-var-init.

Is this intentional? Perhaps partly I'm misinterpreting what "trivial" means in the flag/documentation. Other part is, could we optimize / do better, especially if also using hardened libc++.

jvoung avatar Sep 15 '23 14:09 jvoung

Thinking about this more:

  1. I can understand that optimizing can be challenging (below), but let me know if I missed something.
  2. Would this be a case where it would help to allow attribute((uninitialized)) on a type or fields (e.g., for basic_string class)? This is assuming that the API protects from accessing uninitialized memory, especially w/ hardened libc++. And if we're willing to consider escape hatches like .data(), as a different class of issue.
  3. Misc: can we update the documentation for the ftrivial-auto-var-init flag to clarify "trivial"? My hunch is that the difference w/ GCC is a different interpretation of what the flag means.

Thanks!

===== Why DSE may not optimize Cases (short vs long vs either)

known short (e.g., constant):

  • store:
    • auto-init sets all 24 bytes (for libc++)
    • ctor inits only a subset of the bytes (size, is_long, N bytes in buffer). The branch for long is dead code.
  • load:
    • if string escapes the compiler may not see the loads and will be conservative.
    • or, the compiler sees the loads, but at this point doesn't know if the string is short or long, so takes the union of the two possibilities in terms of fields loaded (potentially all of the fields).
      • The compiler may know short/long for constants at the time of the ctor (branch on strlen of the constant), but may not know at the time of the load (branch on different condition -- the value loaded from is_long field).
  • so most auto-init is untouched by DSE (potentially loaded later)

known long (e.g., constant):

  • auto-init sets all bytes
  • ctor inits all fields of the struct. The branch for short is dead code
  • DSE can eliminate auto-init, because ctor clearly inits all fields already

unknown short/long:

  • auto-init sets all bytes
  • ctor may be for short or long -- in the worst case it is the short version
  • loads can be either short or long -- in the worst case all fields
  • so most auto-init is untouched by DSE

jvoung avatar Sep 29 '23 15:09 jvoung

@llvm/issue-subscribers-clang-codegen

It seems like (some?) auto vars of type std::string can have "auto-init" even though the constructor should have initialized the variable.

Examples in https://godbolt.org/z/6Kf7bdzf8 It seems like GCC doesn't do this with their version of ftrivial-auto-var-init.

Is this intentional? Perhaps partly I'm misinterpreting what "trivial" means in the flag/documentation. Other part is, could we optimize / do better, especially if also using hardened libc++.

llvmbot avatar Oct 02 '23 07:10 llvmbot

@llvm/issue-subscribers-c-1

It seems like (some?) auto vars of type std::string can have "auto-init" even though the constructor should have initialized the variable.

Examples in https://godbolt.org/z/6Kf7bdzf8 It seems like GCC doesn't do this with their version of ftrivial-auto-var-init.

Is this intentional? Perhaps partly I'm misinterpreting what "trivial" means in the flag/documentation. Other part is, could we optimize / do better, especially if also using hardened libc++.

llvmbot avatar Oct 02 '23 07:10 llvmbot

@jvoung thanks for the report! I think it would be good to break this down in a few separate cases.

AFAIC in sso_init and unknown_init, the constructor does not initialize all fields. Not removing the auto-init code there is working as expected I think, as there is some uninitialized memory. Are there cases where all fields are initialized by the constructor, but the auto-init code isn't removed?

fhahn avatar Nov 11 '23 10:11 fhahn

Hi @fhahn, thanks for taking a look!

Yes, the ctor does not initialize all of the short __data_ array, so I do agree that the optimizer is working as expected.

No I haven't seen cases where auto-init isn't removed say in the std::string "definitely long" case where it does init all the fields. You can construct some cases involving sret where there is not-optimized-out auto-init in "definitely long" cases https://godbolt.org/z/44njsjorh, but I think that is a different/general issue w/ sret.

Practically speaking, how much is that initialization protecting vs the cost? Vulnerabilities w/ vs w/out ftrivial-auto-var-init around std::string? The public APIs, especially with hardened libc++ also protect against (some) access of that uninitialized memory. There is a gap (in the short case), if you access out of bounds via .c_str() or .data(). Are there other gaps?

In terms of cost, we haven't seen runtime overhead around this std::string auto-init code, but there is some code size increase given the prevalence of std::string. In one of our benchmarks, std::string was about 7% of auto vars with remaining auto-init after optimization (though some of it may be the sret issue). So, not huge, but was mainly worried about the prevalence of std::string.

jvoung avatar Nov 14 '23 14:11 jvoung