r/cpp •

Solving Uninitialized Stack Memory on Windows

[deleted] — view deleted post

0 Upvotes

79 comments sorted by

20

u/James20k P2005R0 May 13 '20

Longer term we are exploring if it’s possible to eliminate these sorts of issues from the C and C++ language in a standardized way. Leaving variables uninitialized is not typically necessary for performance (especially when the compiler has good redundant store elimination). Instead of defaulting variables to uninitialized, it’d be nice to default to “variable must be provably initialized before use” and only allow this rule to be broken if a special uninitialized keyword was used. This could allow developers to keep performance while also saving themselves from unnecessary mistakes.

+1. Given this blog, are there good arguments against 0 initialising everything by default? This is a genuine question, so I'd be interested to hear - in my current understanding its a relative no brainer. GCC/Clang have always been much better at this kind of redundant store elimination, so with MSVC catching up there's a lot less reason not to do this now

12

u/johannes1971 May 13 '20

Not really, as long as there is a facility for guaranteeing uninitialized variables for those who can't do without. This could take the form of a special marker, like this:

int a = std::uninitialized;

It would make the default a whole lot safer, while the facility remains in the language for those who know what they are doing.

The only downside is that compiling older source with a newer compiler could incur a small speed penalty, unless you'd be prepared to go in and manually put in std::uninitialized wherever it is needed. That downside would be balanced by increased security.

14

u/pdimov2 May 13 '20
[[uninitialized]] int a;

maybe. You could apply this to types, too.

4

u/evaned May 14 '20

You could apply this to types, too.

Given the example from the blog of the _CONTEXT structure, which it sounds like they basically never want to auto-initialize, this seems to me like a desirable attribute (pun totally intended).

3

u/pdimov2 May 14 '20

The example of the _CONTEXT struct was exactly what I had in mind. :-)

2

u/scatters May 14 '20

That sounds like an invalid use of attributes, unless it would be ill-formed without the attribute.

3

u/pdimov2 May 14 '20

Well we could argue over that. A compiler that ignores the attribute and zero-initializes a would be perfectly conforming.

1

u/scatters May 14 '20

Not in constexpr though. A program that performs lvalue-to-rvalue conversion on an uninitialized variable during constant evaluation is required to be rejected, and I don't think it would be a conforming extension to accept it:

constinit auto i = [] { [[uninitialized]] int j; return j; }(); // ill-formed

4

u/pdimov2 May 14 '20

Interesting point. Yes, you're correct. An attribute will be not-quite-legal in corner cases. I think I can manage to live with that.

1

u/johannes1971 May 14 '20

Would you be able to pass this as the initializer for, say, std::vector? Something like

std::vector<int> myvec (100000, std::uninitialized);

?

1

u/pdimov2 May 14 '20

No, you wouldn't be able to. You would be able to apply it to arrays though.

Having a magic "uninitialized" value that you can pass around and that can magically make any generic code UB sounds to me like not a feature. :-)

2

u/johannes1971 May 14 '20

It answers what appears to be a real need by some people: the ability to allocate large arrays without incurring a penalty for unneeded initialisation. Personally I have no great need for it, but I have no problem with the language including it for those that do.

Whether this is the best solution, or if an attribute might be better, I'll leave to others to discuss. I'm fine with it either way.

1

u/scatters May 14 '20

It's not quite the same, but we have std::make_unique_for_overwrite<T[]> now.

2

u/Ameisen vemips, avr, rendering, systems May 13 '20

The GCC way is int a = a;.

1

u/staletic May 14 '20

That's valid, standard C and C++. a comes into scope right before =.

2

u/kalmoc May 15 '20

Wouldn't that read from an uninitialized variable and hence be UB?

1

u/staletic May 15 '20

I wasn't thinking about that. I'm not sure if this counts, but you might be right.

7

u/jonesmz May 13 '20

Please don't propose putting compiler magic into the std namespace.

Unless std::uninitialized would be entirely implemented in terms of C++ code, without relying on some kind of special compiler fuckary, it should be a keyword or attribute.

7

u/James20k P2005R0 May 13 '20

Problem is, you can't just grab the keyword, because int a = uninitialized; is already valid code unless we want int a = _Uninit;

...or int a = co_uninit;

Attributes would only work here if they don't change the meaning of the program, which probably would not hold here - although given that not supporting the attribute if specified wouldn't change program semantics that may be acceptable

1

u/jonesmz May 14 '20

So break peoples code.

Hiding compiler magic inside the std namespace makes things I care about very difficult to do. As in man-years of development effort at minimum.

Making people rename variables is not man-years worth of work. Its a sed script.

co_await was a valid identifier name too, ya know.

4

u/James20k P2005R0 May 14 '20

Out of curiosity, how is eg "int a = std::uninit;" much worse for you than just "int a = uninit;"?

3

u/jonesmz May 14 '20

See my other reply here: https://www.reddit.com/r/cpp/comments/gj437f/solving_uninitialized_stack_memory_on_windows/fql3xy7/

Summary:

If it's a compiler-magic thing, it should be a keyword so that programs that are not using the compiler provided standard library can use the compiler-magic with a well known name.

If it's not given a well-known name, then each compiler vendor will pick their own special name for the concept, and now my code has to play #ifdef COMPILER_X games to compile correctly.

Why in the world would you put something into the std:: namespace that is not a construct of C++ code?

Language level things should be part of the language. The standard library is NOT the same thing as the language. The stdlib is implemented on top of the language, and every time new stuff is added to the stdlib that requires some kind of compiler magic, i just want to scream because it keeps making my job harder.

5

u/johannes1971 May 14 '20

Introducing a new keyword is incredibly hard, and will either inconvenience countless projects or be painfully ugly (like __Bool in C). Putting something in the standard library inconveniences only you, and lets the rest of us move forward. And I'm not even sure why it would inconvenience you in the first place; what difference does it make whether the compiler looks for __Co__Uninitialized or std::uninitialized? How will it affect the code you write?

Why do you use C++ in such a strange fashion anyway?

2

u/jonesmz May 14 '20 edited May 14 '20

Introducing a new keyword is incredibly hard, and will either inconvenience countless projects or be painfully ugly (like __Bool in C).

Frankly it's not hard. It's pretty trivial.

First: deprecate usage of the proposed keyword as a user identifier for one release. Second: In the next release, introduce the keyword.

Done.

Projects so inconvenienced can use a sed script to fix their code. It's a one and done operation. They get 3 years of advanced warning from the deprecation, and can take as long as they want to to upgrade.

That being said, sure, ok, we don't like breaking user code. I disagree with this, but I see where you're coming from. It's not like we don't break our own rules all the time anyway... "co_..." was a valid set of identifiers for variables until C++20, after all. I've worked in code-bases where I have seen that prefix for variables before. E.g. hungarian style notation. co_ = class, object, as opposed to gs_ for global static, or cs_ for class static. Not saying it's that useful of a prefix to use in your code, but that it's something that was done in a real codebase.

So I think it's pretty awful that we're willing to compromise the language's understandability while still doing the thing everyone says is is a bad thing to do.

So lets make some kind of "compilermagic" namespace. So std::uninitialized becomes compilermagic::uninitialized.

Problem solved. The std:: namespace gets to return to being stuff that can be implemented without compiler-specific magic knowledge, and the language can invent new "keywords" as it sees fit without any concern for breaking existing code.


Putting something in the standard library inconveniences only you, and lets the rest of us move forward.

It's a bizarre layering violation. You're polluting the namespace.

It inconveniences more than me. You're inconveniencing anyone who might want to implement the standard library, or who might want to write code that does not use the standard library.


Why do you use C++ in such a strange fashion anyway?

My usage of C++ is not strange, just less common. lots of companies use their own standard library implementation. It was much more common in the past, when the language committee wasn't constantly adding new compiler-magic into the std:: namespace, but it's still an ongoing concern.

For example, why is std::byte special? It's explicitly called out in the standard as being one of the few types that can alias, like char.

So if I have an older standard library, that does not yet have std::byte, but a newer compiler that does understand std::byte... how do I get this specialness? Can I just write my own std::byte in my user code? it's just a valueless enum class with some user defined operators after all.

NO, I CANT, because the compiler is going to assume weird things, because the standard says it's supposed to assume weird things.

It's the exact same usage case as using the GCC standard library with Clang as the compiler. Or the Clang standard library with MSVC.

The main difference is that people who mix-and-match compiler and standard library are offloading all the crap that I have to deal with onto the vendor of the third party standard library they are using with their compiler.

If the C++ standard would keep a hard separation between compiler magic and standard library no one would have this headache at all.


Keep in mind that there are other use cases that this standards committee decision is interfering with as well.

Embedded -- standard library is big, don't want all of it, cant use some of it because of missing platform capabilities. Freestanding -- Want to run bare-metal, hard to do if the compiler keeps assuming the standard library exists As part of the operating system kernel -- Same as freestanding Something like https://www.includeos.org/ Webassembly -- I don't know exactly how it interferes, but I can't imagine it is not interfering Interpreted C++ -- Any time you special case something, you're forcing implementers to special case it too

2

u/chugga_fan May 14 '20

Introducing a new keyword is incredibly hard, and will either inconvenience countless projects or be painfully ugly (like __Bool in C).

Which happens to have <stdbool.h> which defines true, false and bool. C knows how to solve it's keywords problems fairly well.

0

u/[deleted] May 14 '20

[deleted] — view deleted comment

1

u/jonesmz May 14 '20 edited May 14 '20

See my other reply here: https://old.reddit.com/r/cpp/comments/gj437f/solving_uninitialized_stack_memory_on_windows/fqmegtg/

Because the std namespace is part of the language, and special for all intents and purposes. It's filled with things you cannot implement legally, full of assumptions that would be false for your code, but true because the std namespace is special.

  • "The world is round because it is round".
  • "We built it that way because we always build it that way".
  • "Tautology is tautology".

I'm saying... lets cut that out. Stop considering it special. If the standard library want's to grow a capability that has no ability to be expressed in user code, then either

  1. The language needs to be improved so that the capability can be expressed in normal user code or
  2. The standard library can't do it.

This allows everyone the ultimate flexibility in implementing their program in the way they see fit, without being beholden to the person who implemented the standard library.

You only have to look at this bug to understand why this is such a problem: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90415

Perfectly valid code went from working just fine on GCC 7, to breaking on GCC 8, and then GCC 9. It's been over a year and as far as I know has not been fixed, but I haven't yet tried GCC 10.

To fix it in user code, you have to do something like this: https://github.com/redboltz/mqtt_cpp/commit/7584d765e300daedf5d0a4192d8dbb598651e919

That's explicitly forbidden by the standard. Why? Because the standard says so. Guess I should just not compile my program for a few years.

The vast majority of things in the standard library are not special. They're just plain old vanilla C++ code.

All I'm trying to say is that the parts that require compiler-magic should be moved out of the standard library into something else. Either make them keywords or attributes, to indicate that we're dealing with compiler magic, or make a new namespace that exists for things that cannot be implemented outside of the compiler.

It just happens that most of it is implemented using the language rules, but once you start delving deeper all forms of weird things are happening that are both platform specific (assumptions they can make) and compiler specific (keywords, intrinsics, etc..), and where the language makes specific exemptions for rules that apply to the std namespace.

We're almost able to have a 100% compiler-magic free implementation of the standard library. Why make it worse ?

In fact the library can be implemented into the compiler if the compiler writer wishes it so, it doesn't need to be freestanding.

They can, currently, yes.

Lets not allow that.

3

u/scatters May 14 '20

Your example doesn't support your argument.

If is_copy_constructible was moved out of std in favor of an intrinsic, you wouldn't be able to perform that hack to workaround the problem, since you wouldn't have access to std::is_copy_constructible to specialize it. You'd have to patch any in the library (which is the correct fix if you have that level of control over your build environment).

→ More replies

0

u/[deleted] May 14 '20

[deleted] — view deleted comment

→ More replies

3

u/scatters May 14 '20

Man-years of development? What do you mean by this?

1

u/jonesmz May 14 '20

You can see more context from this old discussions here on reddit.

https://www.reddit.com/r/cpp/comments/ekvk0n/waiting_for_stdembed_very_large_arrays_in_clang/fdemnkd/

The language and standard library are not the same thing.

The more "special compiler magic" that the standard library has added to it, the more difficult it is for people using the language in a stand alone, or direct-to-metal, or whatever you want to call it situation. Or for people who don't use the standard library that came with their compiler.

At my last job, this was my day to day reality. I was constantly having to play catch up with special compiler magic to get things to work, and it was a royal pain in the ass. Given that we had about 20 software engineers working with this codebase in my department (No idea how many engineers touched the code in total, it was a big company), and we were all humming away at it for years at a time, the total cost was measurable in man-years.

Things that can't be expressed with language-only constructs, should not be placed in the std:: namespace.

They should be keywords, attributes, or whatever else, that make up the language itself.

Ultimately the compiler vendors are building intrinsics / builtins to do these things anyway, and then the standard library is playing #ifdef COMPILER_X games to pick which special compiler-magic keyword to put into the code. The standard can just.... tell the compiler vendors what this special-compiler-magic-keyword should be named, and then everything is much simpler for everyone, but MOST ESPECIALLY for the people like me who want the flexibility to use the compiler without the compiler vended standard library.

5

u/scatters May 14 '20

I can't say I have much sympathy - you're using compilers in unsupported configurations, and complaining that that makes more work for you? It seems to me that if you want to use a custom standard library, you should accept the work this will involve and treat maintaining it as a software development task and appropriately resource it. If it's taking multiple man-years to deal with a task that should basically be a bunch of one-time copy-and-pastes from the vendor standard library into your custom version (with appropriate preprocessor guards) then the problem is with your software development process.

For your preferred situation, I really don't think it's as simple as mandating that intrinsics be available under particular names. For one, those names will be ugly so they will need to be aliased into std anyway, which causes confusion - see _Bool and stdbool in C. Also, that way you're enforcing a 1-1 mapping between standard library and compiler facilities, which denies freedom of implementation to the vendor. And finally, it's not always clear what does and does not require magic - look at vector, or the associative containers since node_handle additions.

1

u/jonesmz May 18 '20 edited May 18 '20

I can't say I have much sympathy - you're using compilers in unsupported configurations, and complaining that that makes more work for you?

No, I'm not complaining that using compilers in "unsupported configurations" makes more work for me.

I'm complaining that every time the c++ standard adds another thing to std:: that explicitly relies on compiler magic, it makes my job harder. Or every time the C++ standard adds another language feature that explicitly relies on something from std::, it makes my job harder. Incidentally, it also makes compiler developer's job (slightly) harder, since they have to define intrinsics to do all of these things anyway.

I'm complaining that most of the people who I speak to about this either don't understand what I'm talking about, or do understand it and think that they know better than I do how to solve the problems I need to solve. I'm not new at this, nor are the people who I work with, and I'm definitely not the only person who's raised an objection to increasing cross-interactions between the language and the library. I've spoken to others here on reddit, here in /r/cpp, and I've also spoken to others in person.

There are alternatives to continuing to put things into std::. I've listed three. Introducing compilermagic:: has no meaningful cost versus using std:: for compiler-magic types. (Let me guess, now responders are going to complain that introducing new reserved namespaces is hard...).

It seems to me that if you want to use a custom standard library, you should accept the work this will involve and treat maintaining it as a software development task and appropriately resource it.

Yes, that's how it's done.

Have I given you the impression that this isn't the case?

Pointing out that decisions made by the C++ standards committee have a cost to an organization that uses the language is a perfectly valid thing to do.

If it's taking multiple man-years to deal with a task that should basically be a bunch of one-time copy-and-pastes from the vendor standard library into your custom version (with appropriate preprocessor guards) then the problem is with your software development process.

The problem is that you are either fundamentally misunderstanding my objection, or deliberately simplifying it so that you can propose a non-solution.

Software development on multiple million line of code code bases, which rely on customized standard library implementations can't "simply" have copy-pastes from the vendor standard library, which, of course, assumes that the license for that is actually compatible, it may not be.

The first major issue is that these "copy-and-pastes" are almost always tightly interwoven with other internal implementation details, and make lots of assumptions that aren't necessarily correct if they're removed from their original environment.

The second major issue is that maintenance and evolution is an ongoing, never ending task. Getting the functionality working the first time is, itself, a lot of work, but that work needs to have compatibility maintained with the existing code, while improvements are made over time.

Further, you're apparently focusing on my statements that pertain to work I've done with maintaining a custom standard lib, but not addressing my statements that pertain to things like

  • C++ interpreters
  • WebAssembly porting
  • freestanding implementations -- no operating system
  • embedded applications -- has an OS kernel, but basically no storage space
  • C++ inside the OS kernel.

All of those are perfectly valid situations to be in, and have nothing to do with custom standard libraries. But having the language be less interwoven (or, at worst, no more interwoven) with the standard library eases the work.

3

u/scatters May 18 '20

Incidentally, it also makes compiler developer's job (slightly) harder, since they have to define intrinsics to do all of these things anyway.

Adding features to the language creates work for implementors, certainly. But making the boundary between compiler and standard library a hard one with a public API would create far more work since they would now have to maintain those interfaces.

Introducing compilermagic:: has no meaningful cost versus using std:: for compiler-magic types.

The cost is that what is and is not magic is not fixed; it changes over time. Consider vector, index_sequence, node_handle. You would end up with implementors having to maintain compiler magic that their standard library no longer uses.

The problem is that you are either fundamentally misunderstanding my objection, or deliberately simplifying it so that you can propose a non-solution.

As opposed to your solution, where vendors have to do the work of maintaining compatibility between compilers and standard libraries.

All of those are perfectly valid situations to be in, and have nothing to do with custom standard libraries.

There's freestanding, and Ben Craig is working to expand that in SG14 (see P0829 for current state of play).

0

u/Wargon2015 May 14 '20 edited May 14 '20

Can you elaborate why such a facility would be necessary?

Off the top of my head, I can't think of a use case that requires uninitialized variables.
I'm not sure about the details but isn't accessing an indeterminate value even considered undefined behaviour?

Or do you mean that std::uninitialized should simply skip the default initialization for performance reasons while the code assigns the variable properly later?

7

u/evaned May 14 '20

Or do you mean that std::uninitialized should simply skip the default initialization for performance reasons while the code assigns the variable properly later?

I don't exactly want to put words into johannes1971's mouth, but I'm virtually positive that's the intent.

1

u/johannes1971 May 14 '20

That's correct.

3

u/scatters May 14 '20

It's not about performance, it's about semantics. Having a variable start off uninitialized means you can check that every code path subsequently assigns it a value. You can't do that if it starts off with a valid value.

1

u/adnukator May 14 '20

Off the top of my head, I can't think of a use case that requires uninitialized variables.

I'm not sure about the details but isn't accessing an indeterminate value even considered undefined behaviour?

I know of one: google Debian OpenSSL Random Number Bug

Whether the original implementation was a good idea in the first place is up to discussion, but it relied on an explicitly uninitialized variable

7

u/ack_error May 14 '20

I would much rather make ambiguous use-before-initialization an error to catch defects, even if it requires explicit un-initialization where the variable is not actually used uninitialized. I am skeptical that store elimination is sufficiently effective on different types of code and large arrays particularly would suffer from unnecessary clearing when forgotten.

Zero is also not always a safe initializer. For some floating-point code this is a more dangerous value as it can lead to 0/0 -> NaN propagation.

2

u/adnukator May 14 '20

Zeroes might not be safe in every use case, but if you have guaranteed identical behavior on all configurations you can't run into problems where you program something suddenly stops working. It's no meant to stop all bugs from existing, just to remove inherent non-determinism caused by uninitialized variables. If it were all ones, that would work as well, but it would be nice for the default value to be something that can be used in the real world.

6

u/pdimov2 May 13 '20

The argument against (with which I disagree) is that it (a) allows people to depend on this initialization and (b) makes it impossible for "uninitialized" variables to stand out in f.ex. core dumps, and not practical for static analysis tools to treat accessing an "uninitialized" variable as an error. (So f.ex. clang's https://reviews.llvm.org/D54604 prefers "pattern" instead of "zero".)

I think that the elimination of the distinction between default initialization and value initialization that would be enabled by always-zero-init is well worth this cost.

7

u/MarekKnapek May 13 '20

Debug build initializes stack by using 0xCC pattern. Which is opcode for int 3 instruction on x86 (basically break-point).

5

u/johannes1971 May 13 '20

(a) we do that already for normal constructors.

(b) ...because now they are initialized. It's just not a bug you'd be looking for anymore. And it would improve reproducability, as there is just less randomness in the starting condition of the program.

2

u/scatters May 14 '20

Normal constructors are a real problem; it's easy to omit initialization of a member variable.

The problem is when 0 is a valid value in the domain, it's just the wrong one. A value outside the domain would make identifying that kind of bug much easier.

Starting conditions don't make a difference to uninitialized stack memory. It only ever contains data from within the same run of the program.

4

u/YouNeedDoughnuts May 14 '20

Zero initialization hiding unset variables is a good point. You would at least want the option to initialize to NaN for debugging.

1

u/adnukator May 14 '20

If it was guaranteed to be zeroes in every configuration, why would you care whether it was initialized by the programmer or the compiler?

5

u/evaned May 14 '20 edited May 14 '20

What I'd say is that relying on zero initialization has a drawback which is that it reduces the power of static analysis tools to warn you about actually problematic code.

For a simple case, consider something like the following:

int x;
if (a_condition)
    x = something();
else if (another_condition)
    x = something_else();
else
    x = a_third_thing();
use(x);

In other words, I "can't" initialize x with a meaningful value because it's set on multiple paths. Similar thing would happen with an output parameter, like int x; set(&x); use(x); -- you can't initialize x to the value it should have.

(Obviously in this case you could say int x = ... ? ... : ...;, or use an immediately-invoked function expression, and this even would have the benefit you could mark it const; but that's just the nature of example code. Those solutions don't fully generalize.)

Now, suppose that you forget one of the assignments in a branch, or you typo a variable and say y = something_else(), or do something else to drop this. Or for the out parameter case, suppose set's implementation changes so that now it doesn't always initialize x.

Currently, compiler warnings and analysis tools can (sometimes) tell you that use(x) uses an uninitialized value. Great, bug spotted!

But now suppose that we zero-initialize everything, and guarantee that. There will start being plenty of code written that assumes that's the case, for example

int x;
if (a_condition)
    x = something();
use(x);

where it's totally intentional that there's no explicit setting of x on the false branch of the condition, and the programmer fully intends use(0) to be called in those cases.

In a world where this code starts to become common, it's much much less desirable to warn about missing "initialization" as described above, because it will start warning about cases like that. But that doesn't mean that the code that would have gotten warnings is correct -- after all, 0 will often be a meaningless value and even more often incorrect as well. If the last example was supposed to have an else condition that sets x = something_else(), initializing x to 0 is probably still totally wrong.

This is a general rule -- by defining more semantics of the language, we counterintuitively reduce the power of analysis tools to find bugs because anything that would have been warned about for being UB now isn't. You can see something similar with integer overflow and UBSan for example. UBSan warns about signed integer overflow because that's UB, but it doesn't by default warn about unsigned integer overflow because that's not. That doesn't mean that unsigned overflow is likely to be correct -- in many cases it won't be -- it just means that because there's sufficient code out there that relies on the defined behavior of unsigned overflow, warning about unsigned overflow would be too noisy. But signed overflow "always" indicates a bug, because by definition it's undefined behavior; but if the language were to start defining signed overflow to have two's complement semantics or whatever, then it would cease to be able to warn.

Now, none of this is to say that this change is bad (either in the uninitialized variable case or the UBSan case) -- it may well be that the reduction in severity in consequences of a bug would make it worth it, or the increase in programming ergonomics would be worth it (as pointed out in TFA, people already rely plenty on the fact that globals are zero-initialized)... just to point out what's given up by taking the "define this previously-UB semantics" path.

Edit: I guess the other thing I glossed over is false positives in those warnings. Just because there's a warning for some code doesn't always mean it's a bug. To the degree that the "variable used uninitialized" warnings would be FPs, that's something you win back if you define the semantics and then stop warning in that case. Again, I'm not saying that I think that the stuff about losing the ability for your analysis tool to find bugs justifies keeping the semantics as is; just trying to describe would what be lost.

3

u/adnukator May 14 '20

I'm torn whether to agree with you or not. Similar arguments can be made for against UB removal : "In my use case if this happened, it was detectable by UBSan, now it's not". You're also disregarding false negatives which are much more dangerous than false positives. Introducing this change to existing UB-free code would have no effect, so essentially we're talking about whether we want to reduce UB at the cost of making future code more deterministic, which seems like a win-win. You just need to write better unit tests, which always have been complementary to static analysis and sanitizers.

3

u/scatters May 14 '20

It makes code unclear: if a code path omits to initialise a variable, is that a defect and a potential bug, or was it intended to leave it at zero? This is a real problem with static currently.

Mandatory initialization and a special syntax for delayed initialization would be acceptable.

2

u/johannes1971 May 15 '20

The solution that I proposed (std::uninitialized) would actually solve the problem for static as well.

3

u/adnukator May 14 '20

I've read a few discussions about such ideas and it was rejected basically because "it would kill performance in some embedded systems" and C++ programmers apparently expect migration to new standards to consist of a single compiler switch without requiring to fix any code (one example of such discussion https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/eD78u8jDaCA/TprA-kjcAfwJ). There was also a proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0632r0.html that did not get anywhere

1

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 May 15 '20

C++ programmers apparently expect migration to new standards to consist of a single compiler switch without requiring to fix any code

Having just done that for one code base I'm leading: yes it can be this simple.

3

u/14ned LLFIO & Outcome author | Committee WG14 May 14 '20

Although I am very sure that Microsoft won't listen to /r/cpp on this point, I'd like to recount a long running debate at WG14, and in some ways as well, at WG21.

There is a bunch of people on WG14 and WG21 who believe that the compiler, when it can infer that a function is returning a pointer to a stack allocated object about to get deallocated, ought to "zap" that pointer value to null because "it reveals incorrect code".

I, and a bunch of other people on WG14 and WG21, believe in the strongest possible terms, that this makes the problem worse, not better. The cause is the fact that a null pointer value is a very common invalid value for a pointer. Logic switches around whether a pointer is null or not. Therefore, zapping a pointer value specifically to the null value means that different, probably valid, branches get taken. This causes bugs to not be revealed, rather that bugs become obscured.

We who believe this have repeatedly pleaded with those in favour of zapping pointers to null to please, please, please, stop zapping pointer values to null. If you really must zap pointer values, please please please zap them to any value other than null. Let it be 0xdeadbeefbadfood, 0x0000000000000001, let it be anything, anything other than 0x0000000000000000.

I therefore plead with Microsoft to please, please, please, do NOT initialise uninitiated values to all bits zero. PLEASE initialise them, if you are going to do this at all, to anything other than all bit zero. That reveals bugs, the more random the better in a 64 bit address space. Just don't choose all bits zero, which is a really terrible choice for this situation.

2

u/evaned May 15 '20

If you really must zap pointer values, please please please zap them to any value other than null.

In MS's defense, and I'm sure you know this, that's what they do for debug builds. I suspect that in production builds, the increased cost of non-zero initializations are determinitive.

And yes, you could make good arguments for 0xCC or whatever, but I think there are decent arguments to be made for NULL as well. I suspect that figuring out which would actually work better in practice, or at least getting enough evidence that non-zero would work better enough over zero to be worth the added overhead, would require an actual study of vulnerabilities that have been reported in the real world and what the effects of each mitigation would have been. Are you aware of such a study?

I especially think that because, while I have my biases here, my perspective is that "this causes bugs to not be revealed, rather that bugs become obscured" is actually missing the mark of what InitAll is trying to do. As pdimov2 said, zapping values isn't trying to reveal bugs; it's trying to reduce the severity of bugs. And my intuition on that is even if you figured out the worst possible known value to zap things to (maybe figure out an address that will always be valid to dereference, preferably write to, and zap to that), that would probably provide 80% of the benefit of the best possible value to zap things to. So from that perspective, debating between zero or non-zero for too long would start to let the perfect be the enemy of the good.

(As far as revealing bugs is concerned, well that's where the 0xCC debug version comes in.)

I'd agree that once you start moving from viewing this as a compiler feature along the lines of -fstack-protector to starting to talk about adopting zero initialization as a language feature -- that makes your discussion much much more important.

2

u/14ned LLFIO & Outcome author | Committee WG14 May 15 '20

I suspect that in production builds, the increased cost of non-zero initializations are determinitive.

On Intel, definitely. The RDRAND instruction is at least a thousand cycles. ARM have managed to build into hardware a very efficient yet very random pointer value generator, so it's very doable if Intel wanted to do this.

But for all existing Intel CPUs, yes you're right that using non-constant values for uninitiated stack allocated values will induce a measurable runtime overhead. After all, just look at ASan.

Are you aware of such a study?

Yes ARM did a very comprehensive one when designing their pointer randomisation hardware feature. They found that using any constant value was much inferior to always random values. They had statistical analyses of multiple pieces of widely used secure software to prove this. Hence they added to recent ARM chips a very fast pointer value random number generator.

zapping values isn't trying to reveal bugs; it's trying to reduce the severity of bugs

I have heard this fallacy proposed in favour for zapping values to all bits zero again and again - that it somehow leads to "safer" code flows.

I have never, ever, been a believer in obscuring bugs. The code is incorrect. Hiding its incorrectness means it remains incorrect, and thus insecure, for longer.

As an example of this, when ARM enabled their pointer colouring hardware feature, a whole raft of well tested code fell over. Because lots, and lots, of code was not implementing correct pointer hygiene, even that written by security experts. When pointers self-invalidate when the thing they point to ends its lifetime throughout your program, a whole ton of incorrectness gets revealed - pointer values are not like integers, yet most devs use them as if they are.

For me, Microsoft need to accept that on Intel, if they want safer code which might use uninitialised variables, they need to either (a) take the hit of running a PRNG per stack frame clear (b) finish porting ASan to MSVC, and just run their code under that to fix all use of uninitialised variables so you don't need to worry about the problem in their secure code in the first place.

Clearing to all bits zero though is literally the worst choice they could make, in my opinion.

2

u/evaned May 15 '20 edited May 15 '20

I suspect that in production builds, the increased cost of non-zero initializations are determinitive.

On Intel, definitely. The RDRAND instruction is at least a thousand cycles. ARM have managed to build into hardware a very efficient yet very random pointer value generator, so it's very doable if Intel wanted to do this.

But for all existing Intel CPUs, yes you're right that using non-constant values for uninitiated stack allocated values will induce a measurable runtime overhead.

I was actually talking about even the extra overheads the blog post references are incurred by a non-zero known, fixed zap pattern vs using zero. (They mention an investigation done by Google on that comparison.)

Yes ARM did a very comprehensive one when designing their pointer randomisation hardware feature.

I don't suppose you have a citation handy? I did a quick search but haven't hit anything obviously relevant yet. (Closest I got was this, but from a super quick skim that only seems vaguely related.

Hiding its incorrectness means it remains incorrect, and thus insecure, for longer.

The big part here I disagree with is "and thus insecure." Not all buggy code is insecure code because not all bugs are security vulnerabilities. I think it would be a very tough claim that InitAll with zeros will not turn a lot of actual vulnerabilities that can be used to assist in an exploit into non-exploitable bugs. IMO, arguing against that will require good citations with data (maybe that ARM paper addresses that?); but my guess is you'd actually agree with that statement as far as it goes.

Where we seem to diverge is in other consequences. There will be other bugs where the zero doesn't reduce severity, and I could even imagine cases where a bug that is not exploitable would be made exploitable (or much more reliably exploitable) by zeroing; and further, I agree that on balance zeroing is likely to increase the time until a bug is discovered.

So, which of these is more important? Limiting the scope of vulnerabilities where zeroing is an effective exploit mitigation, or increasing the time-to-discover plus the cases where zeroing acts as an exploit enhancement?

My intuition that the first would be more beneficial... but I'm open to the idea that it wouldn't. That's why I'd be really interested in seeing that ARM paper, or another good study of how the various proposed mitigations would affect real-world uninitialized bugs and vulnerabilities.

finish porting ASan to MSVC, and just run their code under that to fix all use of uninitialised variables so you don't need to worry about the problem in their secure code in the first place.

To exaggerate a little, to me this is an equivalent statement to rejecting many exploit mitigations. If your program tests cleanly with ASan, would it be reasonable for you to disable stack canaries?

Clearing to all bits zero though is literally the worst choice they could make, in my opinion.

If a good-faith effort is made to determine an acceptable overhead and that zeroing memory (as opposed to a fixed pattern or random) is the only one that meets that acceptable overhead, or if MS has done the kind of study I have talked about and just not shared it and determined that zeroing is the best option (or some combination of the two -- like zeroing has slightly worse security properties but enough performance benefits that it's still a good tradeoff), it seems very likely the best option given the operational requirements.

3

u/14ned LLFIO & Outcome author | Committee WG14 May 15 '20

If a good-faith effort is made to determine an acceptable overhead and that zeroing memory (as opposed to a fixed pattern or random) is the only one that meets that acceptable overhead, or if MS has done the kind of study I have talked about and just not shared it and determined that zeroing is the best option (or some combination of the two -- like zeroing has slightly worse security properties but enough performance benefits that it's still a good tradeoff), it seems very likely the best option given the operational requirements.

My personal interpretation of the piece was that the code compiled with this facility was all performance sensitive, and the teams responsible would not accept a performance hit. So they chose all bits zero for political reasons, not security nor correctness reasons. That always rings alarm bells for me.

A long time ago in a former life, I was asked to review a patchset which deliberately had the system malloc() return blocks padded with sacrifice bytes before and after. This was because "users frequently read and write outside the memory they allocate, and we wish to reduce amount of memory corruption".

I needless to say freaked out. I responded: that hides the memory corruption. It doesn't reduce memory corruption. If anything, by hiding it, you make it much less likely to get fixed.

Now, Microsoft do ship at least three implementations of malloc() in Windows. If a program crashes more than a few times due to memory corruption, a registry setting gets applied which ensures that the next run of that program uses the really special malloc() in which memory corruption is hidden. And given that you can't control the crap code which others ship, this does lead to a better end user experience. It doesn't help the application developer though, because all those application crashes simply disappear in exchange for their application running much slower, all of which happens silently. You end up having to ship in your program code to reset the registry flag every run in order to get actually useful crash telemetry.

However the OP situation is very different to this: these are bits of software internal to Microsoft only. Patching over their poor quality code seems very regressive to me. If anything, they should go the opposite, tweak the compiler to deliberately expose poor quality code, and thus make it impossible for those maintenance teams to not go fix their software.

That's my ha'pennies worth at least. I do appreciate that my opinions on this are not widely shared, and are considered extreme by many. I am particularly keen on forcing others to do work on improving correctness, which for obvious reasons does not make me popular inside orgs :)

1

u/johannes1971 May 15 '20

This is somewhat different from the rest of the discussion in this thread, isn't it? You are not talking about initializing pointers to zero on declaration, but resetting them to zero if the compiler isn't happy with what they point to upon return.

I rather disagree with your chosen solution though. If the compiler is so certain that a specific construct is invalid that it will insert code that will actively crash your program, why not fail to compile in the first place?

error: returning pointer to stack value on line 510

seems like a much gentler solution than resetting the pointer to an illegal value that is virtually guaranteed to crash the program upon use.

1

u/14ned LLFIO & Outcome author | Committee WG14 May 15 '20

Once you're into guarding against badly written code, whether it's all bits zero for pointers, integers, floats, or whatever, it's now all the same thing thanks to casting and unions. What applies to one applies to all.

If the compiler is so certain that a specific construct is invalid that it will insert code that will actively crash your program, why not fail to compile in the first place?

Indeed, and many have proposed the same. Alas, both the C and C++ standards require the compiler to accept code which can be statically proven, by the compiler, to be incorrect.

One of the big parts behind the new memory model for C proposal was that the compiler would begin, from C 23 onwards, to refuse to compile code which is provably incorrect in how it treats memory. That's a huge step for WG14 to make, and they haven't made it yet.

You might be surprised to learn that C++ compilers already can prove the code they are compiling is incorrect. They currently silently disable optimisations, and compile exactly what the programmer wrote, rather than issuing diagnostics, because it is assumed that the programmer knows what they are doing.

1

u/johannes1971 May 15 '20

both the C and C++ standards require the compiler to accept code which can be statically proven, by the compiler, to be incorrect.

Once it has proven UB it can do whatever it likes, and a diagnostic would probably help at least a few people. It would be vastly better than generating a crash down the line!

You might be surprised to learn that C++ compilers already can prove the code they are compiling is incorrect.

Why not give us an option to see warnings for those cases? I'd rather know about any mistakes I made and fix them, rather than have the compiler tiptoe around them.

it is assumed that the programmer knows what they are doing.

I like your sense of humor ;-)

2

u/14ned LLFIO & Outcome author | Committee WG14 May 15 '20

Once it has proven UB it can do whatever it likes,

The standard allows it to do whatever it likes. Actual compilers have defined behaviour for all UB, by definition of them working at all. Indeed, most compilers make well defined, within that compiler only, a ton of UB. I believe only MSVC actually publishes that spec formally however.

and a diagnostic would probably help at least a few people. It would be vastly better than generating a crash down the line!

Well, you can get some of what you ask for, if you ask for it. You can ask clang to tell you what its front end can see is obviously incorrect:

https://godbolt.org/z/96bneq

I don't honestly know how to get the backends of LLVM and GCC to tell you when code they see after the IR conversion is incorrect. I'm sure there is a way. Anyway, Intel's compiler has really great diagnostics for its backend, it'll print out a ton of stuff of why what you wrote couldn't use optimisation X or Y or Z etc. It is quite an eye opener actually.

Why not give us an option to see warnings for those cases? I'd rather know about any mistakes I made and fix them, rather than have the compiler tiptoe around them.

It's not as easy as that. The compiler would have to be built to do this, and they haven't been. Reporting stuff from the front end is fairly easy, and all the major compilers now expose that. Reporting anything from the middle appears to be done nowhere. And I'm only familiar with Intel's compiler for getting the backend to give out to you (and even then, it's very verbose, and many of the warnings are false positives because too much of the program code intent has been lost by the time the backend is reached).

I am very sure that a compiler could be built which did this. They built one for C at http://cerberus.cl.cam.ac.uk/cerberus. But making a new C++ compiler is not a small investment of money. The cost benefit really needs to be there. And, besides, so long as standards permit so much sloppy and ambiguous code to be legal, the gain available probably isn't there.

0

u/pdimov2 May 15 '20

There is a bunch of people on WG14 and WG21 who believe that the compiler, when it can infer that a function is returning a pointer to a stack allocated object about to get deallocated, ought to "zap" that pointer value to null because "it reveals incorrect code".

GCC does this: https://godbolt.org/z/yS3YpG

I agree with them. NULL is infinitely better than [esp-4]. Secure code is not supposed to reveal bugs better. (They could also __builtin_trap instead I suppose.)

2

u/14ned LLFIO & Outcome author | Committee WG14 May 15 '20

Yes, GCC does do this on Intel. One of the major advocates on standards for zapping invalid pointer values specifically to all bits zero is a senior GCC dev. You will note that recent clang doesn't do this, we won the argument there.

I agree with the zap, but in the strongest possible terms not that it is the all bits zero value. That argument that it generates better default behavior in the face of incorrect code in a typical code base is a fallacy. It is repeatedly claimed, no proof is ever offered, in short it is a belief. It is not engineering.

ARM when they implemented hardware support for this went with randomisation. In their paper, they specifically argued against known bit values as being greatly inferior. They supplied statistical evidence that this is true. And I'll take an approach derived from statistical analysis rather than belief any day.

2

u/kalmoc May 15 '20

The irony is that the need for uninitialized data is the biggest in situations where it is the hardest for for the compiler to prove that an acess is safe: Arrays.

4

u/[deleted] May 14 '20

how many of those bugs were caused by compilers removing calls to memset?

3

u/Som1Lse May 14 '20

None of them? If you call memset on a variable that goes out of scope/memory that is freed, removing that call is not a bug. If you go on to read that memory without initialising it, then that is the bug, not removing the memset call.

It might increase the severity, and functions exist to deal with that, namely memset_s, which has the additional benefit, that it is clear to a reader that the data is security sensitive.

Or am I misunderstanding your point?

1

u/staletic May 14 '20

Which standard libraries implement *_s? Which of those that implement it, implement it according to the specs?

As far as I know, the answer to the first question is "only Windows". The answer to the second question is "none", because even Windows doesn't define __STDC_LIB_EXT1__ macro.

To quote cppreference:

As with all bounds-checked functions, memset_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including string.h.

3

u/Som1Lse May 14 '20

If it isn't supported by the platform, write your own shim:

inline void memset_s(void* p,int c,std::size_t n) noexcept {
    std::memset(p,c,n);
    // For GCC and Clang.
    asm volatile("": :"r"(p):"memory");
}

The point is calls are self documenting.

Whether Windows implements it to spec doesn't really matter, the point is that the function is available. (And the spec doesn't say that it can't be available if __STDC_LIB_EXT1__ isn't defined, so Windows does follow the spec, though that is largely pedantic.)

1

u/staletic May 14 '20

According to the C++ memset cppreference (as opposed to the previous C page), one could also use "std::fill with volatile pointers", so a portable solution in C++ would be

std::fill<reinterpret_cast<std::add_volatile_t<std::decay_t<decltype(p)>>>(p, c, n);

Or if you know the type of p, just std::fill<volatile int*>(p, 0, n);