Use static_assert more.

This commit is contained in:
Adam Ierymenko 2020-03-31 13:45:05 -07:00
parent b9f60fc3c2
commit 7fd78a87d1
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
4 changed files with 22 additions and 21 deletions

View file

@ -99,6 +99,8 @@ private:
ZT_Fingerprint _fp; ZT_Fingerprint _fp;
}; };
static_assert(sizeof(Fingerprint) == sizeof(ZT_Fingerprint),"Fingerprint should be the same size as the underlying C ZT_Fingerprint");
} // namespace ZeroTier } // namespace ZeroTier
#endif #endif

View file

@ -485,6 +485,11 @@ private:
sockaddr_storage _data; sockaddr_storage _data;
}; };
static_assert(sizeof(sockaddr_storage) == sizeof(InetAddress),"InetAddress sizing incorrect");
static_assert(sizeof(sockaddr_in) <= sizeof(InetAddress),"InetAddress sizing incorrect");
static_assert(sizeof(sockaddr_in6) <= sizeof(InetAddress),"InetAddress sizing incorrect");
static_assert(sizeof(sockaddr) <= sizeof(InetAddress),"InetAddress sizing incorrect");
static ZT_INLINE InetAddress *asInetAddress(sockaddr_in *p) noexcept { return reinterpret_cast<InetAddress *>(p); } static ZT_INLINE InetAddress *asInetAddress(sockaddr_in *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
static ZT_INLINE InetAddress *asInetAddress(sockaddr_in6 *p) noexcept { return reinterpret_cast<InetAddress *>(p); } static ZT_INLINE InetAddress *asInetAddress(sockaddr_in6 *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
static ZT_INLINE InetAddress *asInetAddress(sockaddr *p) noexcept { return reinterpret_cast<InetAddress *>(p); } static ZT_INLINE InetAddress *asInetAddress(sockaddr *p) noexcept { return reinterpret_cast<InetAddress *>(p); }

View file

@ -1017,6 +1017,11 @@ ZT_PACKED_STRUCT(struct UNSUPPORTED_OPERATION__NETWORK_CONFIG_REQUEST
/****************************************************************************/ /****************************************************************************/
static_assert(sizeof(Protocol::Header) == ZT_PROTO_MIN_PACKET_LENGTH,"Protocol::Header struct packing error");
static_assert(sizeof(Protocol::FragmentHeader) == ZT_PROTO_MIN_FRAGMENT_LENGTH,"Protocol::FragmentHeader struct packing error");
static_assert(ZT_PROTO_MAX_PACKET_LENGTH < ZT_BUF_MEM_SIZE,"maximum packet length won't fit in Buf");
static_assert(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START == (ZT_PROTO_MIN_PACKET_LENGTH-1),"encrypted packet section must start right before protocol verb at one less than minimum packet size");
/** /**
* Convenience function to pull packet ID from a raw buffer * Convenience function to pull packet ID from a raw buffer
* *

View file

@ -48,7 +48,7 @@
#ifdef __UNIX_LIKE__ #ifdef __UNIX_LIKE__
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h> // NOLINT(modernize-deprecated-headers,hicpp-deprecated-headers)
#include <sys/types.h> #include <sys/types.h>
#endif #endif
@ -68,7 +68,7 @@ static int64_t now()
tmp.HighPart = ft.dwHighDateTime; tmp.HighPart = ft.dwHighDateTime;
return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds ); return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds );
#else #else
timeval tv; timeval tv; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
gettimeofday(&tv,nullptr); gettimeofday(&tv,nullptr);
return ( (1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000) ); return ( (1000LL * (int64_t)tv.tv_sec) + (int64_t)(tv.tv_usec / 1000) );
#endif #endif
@ -232,8 +232,6 @@ public:
long *cnt; long *cnt;
}; };
#define ZT_T_ASSERT(e) if (!(e)) { ZT_T_PRINTF("FAILED (simple assert: " #e ")" ZT_EOL_S); return "simple assert: " #e; }
extern "C" const char *ZTT_general() extern "C" const char *ZTT_general()
{ {
try { try {
@ -266,21 +264,12 @@ extern "C" const char *ZTT_general()
{ {
ZT_T_PRINTF("[general] Checking structure sizes, alignment, and packing... "); ZT_T_PRINTF("[general] Checking structure sizes, alignment, and packing... ");
ZT_T_ASSERT(sizeof(StructPackingTestSample) == 146); static_assert(sizeof(StructPackingTestSample) == 146,"StructPackingTestSample packed incorrectly");
StructPackingTestSample packtest; StructPackingTestSample packtest; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
if (((uintptr_t)&(packtest.woop) - (uintptr_t)&packtest) != 8) if (((uintptr_t)&(packtest.woop) - (uintptr_t)&packtest) != 8)
return "structure packing check failed: incorrect index of test field in example packed struct (compiler or environment problem)"; return "structure packing check failed: incorrect index of test field in example packed struct (compiler or environment problem)";
if (((uintptr_t)&(packtest.hoho[5]) - (uintptr_t)&packtest) != 57) if (((uintptr_t)&(packtest.hoho[5]) - (uintptr_t)&packtest) != 57)
return "structure packing check failed: incorrect index of test array indexed field in example packed struct (compiler or environment problem)"; return "structure packing check failed: incorrect index of test array indexed field in example packed struct (compiler or environment problem)";
ZT_T_ASSERT(ZT_PROTO_MAX_PACKET_LENGTH < ZT_BUF_MEM_SIZE);
ZT_T_ASSERT(ZT_PROTO_PACKET_ENCRYPTED_SECTION_START < ZT_PROTO_MIN_PACKET_LENGTH);
ZT_T_ASSERT(sizeof(Protocol::Header) == ZT_PROTO_MIN_PACKET_LENGTH);
ZT_T_ASSERT(sizeof(Protocol::FragmentHeader) == ZT_PROTO_MIN_FRAGMENT_LENGTH);
ZT_T_ASSERT(sizeof(sockaddr_storage) == sizeof(InetAddress));
ZT_T_ASSERT(sizeof(sockaddr_in) <= sizeof(InetAddress));
ZT_T_ASSERT(sizeof(sockaddr_in6) <= sizeof(InetAddress));
ZT_T_ASSERT(sizeof(sockaddr) <= sizeof(InetAddress));
ZT_T_ASSERT(sizeof(Fingerprint) == sizeof(ZT_Fingerprint));
ZT_T_PRINTF("OK" ZT_EOL_S); ZT_T_PRINTF("OK" ZT_EOL_S);
} }
@ -477,7 +466,7 @@ extern "C" const char *ZTT_general()
return "Map::get() failed"; return "Map::get() failed";
} }
} }
for(Map<uint64_t,uint64_t>::iterator i(tm.begin());i!=tm.end();) { for(Map<uint64_t,uint64_t>::iterator i(tm.begin());i!=tm.end();) { // NOLINT(hicpp-use-auto,modernize-use-auto)
if ((i->first & 1U) == 0) if ((i->first & 1U) == 0)
tm.erase(i++); tm.erase(i++);
else ++i; else ++i;
@ -549,7 +538,7 @@ extern "C" const char *ZTT_general()
FCV<Buf::Slice,16> ref; FCV<Buf::Slice,16> ref;
int frags = 1 + (int)(Utils::random() % 16); int frags = 1 + (int)(Utils::random() % 16);
int skip = ((k & 3U) == 1) ? -1 : (int)(Utils::random() % frags); int skip = ((k & 3) == 1) ? -1 : (int)(Utils::random() % frags);
bool complete = false; bool complete = false;
message.resize(frags); message.resize(frags);
ref.resize(frags); ref.resize(frags);
@ -1069,7 +1058,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
ZT_T_PRINTF("[crypto] Benchmarking Curve25519 ECDH... "); ZT_T_PRINTF("[crypto] Benchmarking Curve25519 ECDH... ");
int64_t start = now(); int64_t start = now();
for(int i=0;i<150;++i) { for(int i=0;i<150;++i) {
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
C25519::agree(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub2,key); C25519::agree(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub2,key);
foo = key[0]; // prevent optimization foo = key[0]; // prevent optimization
} }
@ -1084,7 +1073,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature... "); ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature... ");
int64_t start = now(); int64_t start = now();
for(int i=0;i<150;++i) { for(int i=0;i<150;++i) {
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
C25519::sign(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub1,sig,sizeof(sig),sig); C25519::sign(C25519_TEST_VECTORS[t].priv1,C25519_TEST_VECTORS[t].pub1,sig,sizeof(sig),sig);
foo = sig[0]; foo = sig[0];
} }
@ -1097,7 +1086,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature verification... "); ZT_T_PRINTF("[crypto] Benchmarking Ed25519 signature verification... ");
int64_t start = now(); int64_t start = now();
for(int i=0;i<15;++i) { for(int i=0;i<15;++i) {
for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { for (int t=0;t<ZT_NUM_C25519_TEST_VECTORS;++t) { // NOLINT(modernize-loop-convert)
if (C25519::verify(C25519_TEST_VECTORS[t].pub1,C25519_TEST_VECTORS[t].agreementSha512,64,C25519_TEST_VECTORS[t].agreementSignedBy1,96)) if (C25519::verify(C25519_TEST_VECTORS[t].pub1,C25519_TEST_VECTORS[t].agreementSha512,64,C25519_TEST_VECTORS[t].agreementSignedBy1,96))
++foo; ++foo;
} }
@ -1109,7 +1098,7 @@ extern "C" const char *ZTT_benchmarkCrypto()
{ {
uint8_t key[48]; uint8_t key[48];
ZT_T_PRINTF("[crypto] Benchmarking ECC384 ECDH... "); ZT_T_PRINTF("[crypto] Benchmarking ECC384 ECDH... ");
volatile uint8_t *volatile pub = (volatile uint8_t *)ECC384_TV0_PUBLIC; volatile uint8_t *volatile pub = (volatile uint8_t *)ECC384_TV0_PUBLIC; // NOLINT(hicpp-use-auto,modernize-use-auto)
int64_t start = now(); int64_t start = now();
for(int i=0;i<500;++i) { for(int i=0;i<500;++i) {
ECC384ECDH((const uint8_t *)pub,ECC384_TV0_PRIVATE,key); ECC384ECDH((const uint8_t *)pub,ECC384_TV0_PRIVATE,key);