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
better codegen of undefined #460
Comments
In safety mode, initializing to This is different from what you're proposing, which is memset to The value Zig tries very hard to prevent undefined behavior, and helping you find where your code is reading uninitialized memory is part of that goal. If your code actually uses undefined values, it is unquestionably a bug. We don't want the compiler to enable that bug to go unnoticed by setting uninitialized memory to 0. Of course this isn't a perfect solution. Valgrind can give much more sophisticated diagnostics than |
I'm going to re-open this issue for a couple reasons:
|
Assigning always the same fixed value to |
I agree with this statement. However this introduces non reproducible builds, which is something to discuss before causing to happen. |
Randomness can be reproducible if it's externally seeded. The command line option to enable |
Compiler initializes random generator (e.g. xoroshiro128 - http://vigna.di.unimi.it/xorshift/xoroshiro128plus.c ) from current time at the start and then inserts initialization memcpy after variable declaration. No hardcoded random numbers in executable. The generator could always set the highest bit to 1, to make the number large or negative, to increase probability of undesirable effects. Forcing "everything defined" would simply use value 0 instead of the generator. Specifying the seed on command line would be too much hassle and it wouldn't speed up bug discovery. |
I think there's value in 0xaa. The pattern is very noticeable in both hex and binary, and it's also a large or negative number. It's likely nothing will be mapped to address 0xaaaaaaaaaaaaaaaa. If we do want to use a random seed and achieve reproducible builds, we can't seed based on time. But we could seed based on seemingly random things such as number of characters in all the source code combined. |
If 0xaa is/will be the default then there's no point in my proposal - it is already here. Under the term "reproducible build" I understand generated binary being always the same on the disk, byte for byte, not actual execution always following the same path. If my understanding is correct source of the seed doesn't matter. |
We can use valgrind's client request mechanism to not set undefined bytes to 0xaa when RUNNING_ON_VALGRIND is true. it still doesn't solve assigning undefined later, but I sent a request to the valgrind mailing list asking about it. |
quick response. we can have our cake and eat it too. |
I moved all the issues mentioned in #460 (comment) to their own issues. |
Variable in Zig can be left uninitialized by
undefined
.It would be handy if there was compiler command line option/project config file settings to override this and make all
undefined
variables initialized with their default.If the application was behaving unpredictably before and after forced initialization it looks OK one knows the cause.
This is a feature in Jai language.
The text was updated successfully, but these errors were encountered: