mirror of
https://github.com/ipxe/ipxe
synced 2026-01-31 19:49:50 +03:00
[libc] Replace linker_assert() with build_assert()
We currently implement build-time assertions via a mechanism that generates a call to an undefined external function that will cause the link to fail unless the compiler can prove that the asserted condition is true (and thereby eliminate the undefined function call). This assertion mechanism can be used for conditions that are not amenable to the use of static_assert(), since static_assert() will not allow for proofs via dead code elimination. Add __attribute__((error(...))) to the undefined external function, so that the error is raised at compile time rather than at link time. This allows us to provide a more meaningful error message (which will include the file name and line number, as with any other compile-time error), and avoids the need for the caller to specify a unique symbol name for the external function. Change the name from linker_assert() to build_assert(), since the assertion now takes place at compile time rather than at link time. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -472,10 +472,10 @@ void gcm_setiv ( struct gcm_context *context, const void *iv, size_t ivlen ) {
|
||||
union gcm_block *check = ( ( void * ) context );
|
||||
|
||||
/* Sanity checks */
|
||||
linker_assert ( &context->hash == check, gcm_bad_layout );
|
||||
linker_assert ( &context->len == check + 1, gcm_bad_layout );
|
||||
linker_assert ( &context->ctr == check + 2, gcm_bad_layout );
|
||||
linker_assert ( &context->key == check + 3, gcm_bad_layout );
|
||||
build_assert ( &context->hash == check );
|
||||
build_assert ( &context->len == check + 1 );
|
||||
build_assert ( &context->ctr == check + 2 );
|
||||
build_assert ( &context->key == check + 3 );
|
||||
|
||||
/* Reset non-key state */
|
||||
memset ( context, 0, offsetof ( typeof ( *context ), key ) );
|
||||
|
||||
@@ -155,11 +155,11 @@ static void md4_digest ( struct md4_context *context ) {
|
||||
|
||||
/* Sanity checks */
|
||||
assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
||||
linker_assert ( &u.ddd.dd.digest.h[0] == a, md4_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[1] == b, md4_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[2] == c, md4_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[3] == d, md4_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.data.dword[0] == w, md4_bad_layout );
|
||||
build_assert ( &u.ddd.dd.digest.h[0] == a );
|
||||
build_assert ( &u.ddd.dd.digest.h[1] == b );
|
||||
build_assert ( &u.ddd.dd.digest.h[2] == c );
|
||||
build_assert ( &u.ddd.dd.digest.h[3] == d );
|
||||
build_assert ( &u.ddd.dd.data.dword[0] == w );
|
||||
|
||||
DBGC ( context, "MD4 digesting:\n" );
|
||||
DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
||||
|
||||
@@ -178,11 +178,11 @@ static void md5_digest ( struct md5_context *context ) {
|
||||
|
||||
/* Sanity checks */
|
||||
assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
||||
linker_assert ( &u.ddd.dd.digest.h[0] == a, md5_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[1] == b, md5_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[2] == c, md5_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[3] == d, md5_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.data.dword[0] == w, md5_bad_layout );
|
||||
build_assert ( &u.ddd.dd.digest.h[0] == a );
|
||||
build_assert ( &u.ddd.dd.digest.h[1] == b );
|
||||
build_assert ( &u.ddd.dd.digest.h[2] == c );
|
||||
build_assert ( &u.ddd.dd.digest.h[3] == d );
|
||||
build_assert ( &u.ddd.dd.data.dword[0] == w );
|
||||
|
||||
DBGC ( context, "MD5 digesting:\n" );
|
||||
DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
||||
|
||||
@@ -145,12 +145,12 @@ static void sha1_digest ( struct sha1_context *context ) {
|
||||
|
||||
/* Sanity checks */
|
||||
assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
||||
linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
|
||||
build_assert ( &u.ddd.dd.digest.h[0] == a );
|
||||
build_assert ( &u.ddd.dd.digest.h[1] == b );
|
||||
build_assert ( &u.ddd.dd.digest.h[2] == c );
|
||||
build_assert ( &u.ddd.dd.digest.h[3] == d );
|
||||
build_assert ( &u.ddd.dd.digest.h[4] == e );
|
||||
build_assert ( &u.ddd.dd.data.dword[0] == w );
|
||||
|
||||
DBGC ( context, "SHA1 digesting:\n" );
|
||||
DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
||||
|
||||
@@ -140,15 +140,15 @@ static void sha256_digest ( struct sha256_context *context ) {
|
||||
|
||||
/* Sanity checks */
|
||||
assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
|
||||
linker_assert ( &u.ddd.dd.digest.h[0] == a, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[1] == b, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[2] == c, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[3] == d, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[4] == e, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[5] == f, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[6] == g, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.digest.h[7] == h, sha256_bad_layout );
|
||||
linker_assert ( &u.ddd.dd.data.dword[0] == w, sha256_bad_layout );
|
||||
build_assert ( &u.ddd.dd.digest.h[0] == a );
|
||||
build_assert ( &u.ddd.dd.digest.h[1] == b );
|
||||
build_assert ( &u.ddd.dd.digest.h[2] == c );
|
||||
build_assert ( &u.ddd.dd.digest.h[3] == d );
|
||||
build_assert ( &u.ddd.dd.digest.h[4] == e );
|
||||
build_assert ( &u.ddd.dd.digest.h[5] == f );
|
||||
build_assert ( &u.ddd.dd.digest.h[6] == g );
|
||||
build_assert ( &u.ddd.dd.digest.h[7] == h );
|
||||
build_assert ( &u.ddd.dd.data.dword[0] == w );
|
||||
|
||||
DBGC ( context, "SHA256 digesting:\n" );
|
||||
DBGC_HDA ( context, 0, &context->ddd.dd.digest,
|
||||
|
||||
@@ -156,15 +156,15 @@ static void sha512_digest ( struct sha512_context *context ) {
|
||||
|
||||
/* Sanity checks */
|
||||
assert ( ( context->len % sizeof ( context->ddq.dd.data ) ) == 0 );
|
||||
linker_assert ( &u.ddq.dd.digest.h[0] == a, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[1] == b, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[2] == c, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[3] == d, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[4] == e, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[5] == f, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[6] == g, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.digest.h[7] == h, sha512_bad_layout );
|
||||
linker_assert ( &u.ddq.dd.data.qword[0] == w, sha512_bad_layout );
|
||||
build_assert ( &u.ddq.dd.digest.h[0] == a );
|
||||
build_assert ( &u.ddq.dd.digest.h[1] == b );
|
||||
build_assert ( &u.ddq.dd.digest.h[2] == c );
|
||||
build_assert ( &u.ddq.dd.digest.h[3] == d );
|
||||
build_assert ( &u.ddq.dd.digest.h[4] == e );
|
||||
build_assert ( &u.ddq.dd.digest.h[5] == f );
|
||||
build_assert ( &u.ddq.dd.digest.h[6] == g );
|
||||
build_assert ( &u.ddq.dd.digest.h[7] == h );
|
||||
build_assert ( &u.ddq.dd.data.qword[0] == w );
|
||||
|
||||
DBGC ( context, "SHA512 digesting:\n" );
|
||||
DBGC_HDA ( context, 0, &context->ddq.dd.digest,
|
||||
|
||||
Reference in New Issue
Block a user