-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Check assumptions on integer implementation at compile time #798
Conversation
It seems fairly easy to turn this into a compile time check, by using (that_expression - 1) as the size of array. However, maybe we want to include endianness into the check, and I'm not sure that can be done at compile time. |
Var arrays are C99 and "optional" in later standards and not supported by MSVC. |
@gmaxwell Yes, but this isn't relying on vararrays - the expression is a compile-time constant. In fact, it has to be constant. A vararray of negative size could compile. So you have to do something like define a struct with a member array (where a vararray isn't allowed). |
@gmaxwell this seems to work in gcc/clang/icc/msvc: https://godbolt.org/z/6dsar5 |
Yeah, I tested it with GCC 2.95 on i386 and tinyc and it seems to be happy there too. One downside is that when it fails you get an extremely opaque error. |
8be2c47
to
dd00182
Compare
Added conversions from signed to signed, and a check for CHAR_BIT. Anything else? Do we have any unusual assumptions on the size of |
We could use I read up a little bit on these techniques. The issues with VLAs was something else: In some cases this array trick silently didn't do anything when the expression is non-constant, e.g., when you have a compiler like GCC that supports VLAs as an extension and you happen to pass a non-constant value, then it can't be a compiler error even if the size is negative. This hit the Linux kernel for example. But what we do here is robust against this failure mode because VLAs are never supported on a file level / global scope. I verified this in godbolt. Even GCC errors out if you pass a non-constant value. (Clang fails at another stage but the message is pretty harsh: Approach ACK Maybe we want to move the assumptions to a separate header like in Bitcoin Core (https://github.com/bitcoin/bitcoin/blob/master/src/compat/assumptions.h). That's cleaner and it's easier to point people to the file, e.g., in the README. |
A separate file might make it marginally more likely to get a more useful error message (e.g. telling you what file it was in) from more compilers. |
dd00182
to
d09cf8a
Compare
That's a good point. Moved. |
You might want to a comment above it that says something like: /* This library, like most software, relies on a number of compiler implementation defined (not undefined) behaviours. Although the behaviours we require are essentially universal we test them specifically here to reduce the odds of experiencing an unwelcome surprise. */ |
d09cf8a
to
4a81f79
Compare
@gmaxwell Added. |
4a81f79
to
b1bbbb8
Compare
Include it in gen_context.c too. Sorry for dribbling changes. |
b1bbbb8
to
fdb6dbc
Compare
Done. Also added to bench_internal.c, tests_exhaustive.c, and valgrind_ctime_test.c. |
ACK fdb6dbc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added conversions from signed to signed, and a check for CHAR_BIT. Anything else? Do we have any unusual assumptions on the size of
int
etc?
ACK except nit. We should then add more assumptions from #792 later.
fdb6dbc
to
7c06899
Compare
@real-or-random Sounds good. |
ACK 7c06899 |
ACK 7c06899 code review and tested |
Summary: * Add support for (signed) __int128 * Compile-time check assumptions on integer types This is a backport of secp256k1 [[bitcoin-core/secp256k1#798 | PR798]] Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D7632
Summary: * Add support for (signed) __int128 * Compile-time check assumptions on integer types This is a backport of secp256k1 [[bitcoin-core/secp256k1#798 | PR798]] Test Plan: ninja check-secp256k1 Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D7632
A compile-time check is implemented in a new
src/assumptions.h
which verifies several aspects that are implementation-defined in C: