Remove tokstyle exemptions from build files.

We put some tokstyle exemptions into the source files themselves,
instead. This way we can check some of the code in those files, and more
in the future when tokstyle supports more constructs (like apidsl).

Also: hacked ping_array.api.h to not emit `_array` as parameter names.
We'll need to fix apidsl to do this better. This works for now.
This commit is contained in:
iphydf 2020-04-09 23:56:19 +00:00
parent 7f9f8045cd
commit c08b2fb3e2
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
16 changed files with 67 additions and 56 deletions

View File

@ -45,8 +45,8 @@ cc_library(
hdrs = ["bwcontroller.h"], hdrs = ["bwcontroller.h"],
deps = [ deps = [
":ring_buffer", ":ring_buffer",
"//c-toxcore/toxcore",
"//c-toxcore/toxcore:Messenger", "//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:toxcore",
], ],
) )
@ -130,10 +130,7 @@ CIMPLE_SRCS = glob(
"*.c", "*.c",
"*.h", "*.h",
], ],
exclude = [ exclude = ["*.api.h"],
"*.api.h",
"toxav.h",
],
) )
sh_test( sh_test(

View File

@ -10,6 +10,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
//!TOKSTYLE-
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -686,5 +688,7 @@ typedef TOXAV_ERR_BIT_RATE_SET Toxav_Err_Bit_Rate_Set;
typedef TOXAV_ERR_SEND_FRAME Toxav_Err_Send_Frame; typedef TOXAV_ERR_SEND_FRAME Toxav_Err_Send_Frame;
typedef TOXAV_CALL_CONTROL Toxav_Call_Control; typedef TOXAV_CALL_CONTROL Toxav_Call_Control;
//!TOKSTYLE+
#endif // C_TOXCORE_TOXAV_TOXAV_H #endif // C_TOXCORE_TOXAV_TOXAV_H
%} %}

View File

@ -9,6 +9,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
//!TOKSTYLE-
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -815,4 +817,6 @@ typedef TOXAV_ERR_BIT_RATE_SET Toxav_Err_Bit_Rate_Set;
typedef TOXAV_ERR_SEND_FRAME Toxav_Err_Send_Frame; typedef TOXAV_ERR_SEND_FRAME Toxav_Err_Send_Frame;
typedef TOXAV_CALL_CONTROL Toxav_Call_Control; typedef TOXAV_CALL_CONTROL Toxav_Call_Control;
//!TOKSTYLE+
#endif // C_TOXCORE_TOXAV_TOXAV_H #endif // C_TOXCORE_TOXAV_TOXAV_H

View File

@ -286,14 +286,7 @@ CIMPLE_SRCS = glob(
"*.c", "*.c",
"*.h", "*.h",
], ],
exclude = [ exclude = ["*.api.h"],
"*.api.h",
"ccompat.h",
"crypto_core_mem.c",
"ping_array.h",
"tox.h",
"tox_api.c",
],
) )
sh_test( sh_test(

View File

@ -4,6 +4,8 @@
#ifndef C_TOXCORE_TOXCORE_CCOMPAT_H #ifndef C_TOXCORE_TOXCORE_CCOMPAT_H
#define C_TOXCORE_TOXCORE_CCOMPAT_H #define C_TOXCORE_TOXCORE_CCOMPAT_H
//!TOKSTYLE-
// Variable length arrays. // Variable length arrays.
// VLA(type, name, size) allocates a variable length array with automatic // VLA(type, name, size) allocates a variable length array with automatic
// storage duration. VLA_SIZE(name) evaluates to the runtime size of that array // storage duration. VLA_SIZE(name) evaluates to the runtime size of that array
@ -48,4 +50,6 @@
#define GNU_PRINTF(f, a) #define GNU_PRINTF(f, a)
#endif #endif
//!TOKSTYLE+
#endif // C_TOXCORE_TOXCORE_CCOMPAT_H #endif // C_TOXCORE_TOXCORE_CCOMPAT_H

View File

@ -69,7 +69,7 @@ const CRYPTO_SHA512_SIZE = 64;
* "aaaa" and "baaa" also takes 4 time. With a regular `memcmp`, the latter may * "aaaa" and "baaa" also takes 4 time. With a regular `memcmp`, the latter may
* take 1 time, because it immediately knows that the two strings are not equal. * take 1 time, because it immediately knows that the two strings are not equal.
*/ */
static int32_t crypto_memcmp(const void *p1, const void *p2, size_t length); static int32_t crypto_memcmp(const uint8_t *p1, const uint8_t *p2, size_t length);
/** /**
* A `bzero`-like function which won't be optimised away by the compiler. Some * A `bzero`-like function which won't be optimised away by the compiler. Some

View File

@ -83,7 +83,7 @@ uint32_t crypto_sha512_size(void);
* "aaaa" and "baaa" also takes 4 time. With a regular `memcmp`, the latter may * "aaaa" and "baaa" also takes 4 time. With a regular `memcmp`, the latter may
* take 1 time, because it immediately knows that the two strings are not equal. * take 1 time, because it immediately knows that the two strings are not equal.
*/ */
int32_t crypto_memcmp(const void *p1, const void *p2, size_t length); int32_t crypto_memcmp(const uint8_t *p1, const uint8_t *p2, size_t length);
/** /**
* A `bzero`-like function which won't be optimised away by the compiler. Some * A `bzero`-like function which won't be optimised away by the compiler. Some

View File

@ -37,8 +37,7 @@ void crypto_memzero(void *data, size_t length)
{ {
#ifndef VANILLA_NACL #ifndef VANILLA_NACL
sodium_memzero(data, length); sodium_memzero(data, length);
#else #elif defined(_WIN32)
#ifdef _WIN32
SecureZeroMemory(data, length); SecureZeroMemory(data, length);
#elif defined(HAVE_MEMSET_S) #elif defined(HAVE_MEMSET_S)
@ -53,32 +52,33 @@ void crypto_memzero(void *data, size_t length)
#elif defined(HAVE_EXPLICIT_BZERO) #elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(data, length); explicit_bzero(data, length);
#else #else
volatile unsigned char *volatile pnt = //!TOKSTYLE-
(volatile unsigned char *volatile) data; volatile uint8_t *volatile pnt = data;
//!TOKSTYLE+
size_t i = (size_t) 0U; size_t i = (size_t) 0U;
while (i < length) { while (i < length) {
pnt[i++] = 0U; pnt[i] = 0U;
++i;
} }
#endif
#endif #endif
} }
int32_t crypto_memcmp(const void *p1, const void *p2, size_t length) int32_t crypto_memcmp(const uint8_t *p1, const uint8_t *p2, size_t length)
{ {
#ifndef VANILLA_NACL #ifndef VANILLA_NACL
return sodium_memcmp(p1, p2, length); return sodium_memcmp(p1, p2, length);
#else #else
const volatile unsigned char *volatile b1 = //!TOKSTYLE-
(const volatile unsigned char *volatile) p1; const volatile uint8_t *volatile b1 = p1;
const volatile unsigned char *volatile b2 = const volatile uint8_t *volatile b2 = p2;
(const volatile unsigned char *volatile) p2; //!TOKSTYLE+
size_t i; size_t i;
unsigned char d = (unsigned char) 0U; uint8_t d = (uint8_t) 0U;
for (i = 0U; i < length; i++) { for (i = 0U; i < length; ++i) {
d |= b1[i] ^ b2[i]; d |= b1[i] ^ b2[i];
} }

View File

@ -1,6 +1,7 @@
#include "crypto_core.h" #include "crypto_core.h"
#include <algorithm> #include <algorithm>
#include <vector>
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -29,7 +30,7 @@ enum {
CRYPTO_TEST_MEMCMP_EPS = 10, CRYPTO_TEST_MEMCMP_EPS = 10,
}; };
clock_t memcmp_time(void *a, void *b, size_t len) { clock_t memcmp_time(uint8_t const *a, uint8_t const *b, size_t len) {
clock_t start = clock(); clock_t start = clock();
volatile int result = crypto_memcmp(a, b, len); volatile int result = crypto_memcmp(a, b, len);
(void)result; (void)result;
@ -41,8 +42,8 @@ clock_t memcmp_time(void *a, void *b, size_t len) {
* equal and non-equal arrays to reduce the influence of external effects * equal and non-equal arrays to reduce the influence of external effects
* such as the machine being a little more busy 1 second later. * such as the machine being a little more busy 1 second later.
*/ */
void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *same_median, std::pair<clock_t, clock_t> memcmp_median(uint8_t const *src, uint8_t const *same,
clock_t *not_same_median) { uint8_t const *not_same, size_t len) {
clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; clock_t same_results[CRYPTO_TEST_MEMCMP_ITERATIONS];
clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS]; clock_t not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS];
@ -52,9 +53,10 @@ void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *s
} }
std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); std::sort(same_results, same_results + CRYPTO_TEST_MEMCMP_ITERATIONS);
*same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; clock_t const same_median = same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2];
std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS); std::sort(not_same_results, not_same_results + CRYPTO_TEST_MEMCMP_ITERATIONS);
*not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2]; clock_t const not_same_median = not_same_results[CRYPTO_TEST_MEMCMP_ITERATIONS / 2];
return {same_median, not_same_median};
} }
/** /**
@ -63,32 +65,28 @@ void memcmp_median(void *src, void *same, void *not_same, size_t len, clock_t *s
*/ */
TEST(CryptoCore, MemcmpTimingIsDataIndependent) { TEST(CryptoCore, MemcmpTimingIsDataIndependent) {
// A random piece of memory. // A random piece of memory.
auto *src = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; std::vector<uint8_t> src(CRYPTO_TEST_MEMCMP_SIZE);
random_bytes(src, CRYPTO_TEST_MEMCMP_SIZE); random_bytes(src.data(), CRYPTO_TEST_MEMCMP_SIZE);
// A separate piece of memory containing the same data. // A separate piece of memory containing the same data.
auto *same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; std::vector<uint8_t> same = src;
memcpy(same, src, CRYPTO_TEST_MEMCMP_SIZE);
// Another piece of memory containing different data. // Another piece of memory containing different data.
auto *not_same = new uint8_t[CRYPTO_TEST_MEMCMP_SIZE]; std::vector<uint8_t> not_same(CRYPTO_TEST_MEMCMP_SIZE);
random_bytes(not_same, CRYPTO_TEST_MEMCMP_SIZE); random_bytes(not_same.data(), CRYPTO_TEST_MEMCMP_SIZE);
clock_t same_median; // Once we have C++17:
clock_t not_same_median; // auto const [same_median, not_same_median] =
memcmp_median(src, same, not_same, CRYPTO_TEST_MEMCMP_SIZE, &same_median, &not_same_median); auto const result =
memcmp_median(src.data(), same.data(), not_same.data(), CRYPTO_TEST_MEMCMP_SIZE);
delete[] not_same;
delete[] same;
delete[] src;
clock_t const delta = clock_t const delta =
same_median > not_same_median ? same_median - not_same_median : not_same_median - same_median; std::max(result.first, result.second) - std::min(result.first, result.second);
EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS) EXPECT_LT(delta, CRYPTO_TEST_MEMCMP_EPS)
<< "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n" << "Delta time is too long (" << delta << " >= " << CRYPTO_TEST_MEMCMP_EPS << ")\n"
<< "Time of the same data comparison: " << same_median << " clocks\n" << "Time of the same data comparison: " << result.first << " clocks\n"
<< "Time of the different data comparison: " << not_same_median << " clocks"; << "Time of the different data comparison: " << result.second << " clocks";
} }
} // namespace } // namespace

View File

@ -20,7 +20,7 @@ extern "C" {
class mono_Time { struct this; } class mono_Time { struct this; }
class ping_Array { class ping { class array {
struct this; struct this;
@ -55,7 +55,7 @@ uint64_t add(const mono_Time::this *mono_time, const uint8_t *data, uint32_t len
*/ */
int32_t check(const mono_Time::this *mono_time, uint8_t[length] data, uint64_t ping_id); int32_t check(const mono_Time::this *mono_time, uint8_t[length] data, uint64_t ping_id);
} } }
%{ %{
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -39,14 +39,14 @@ struct Ping_Array *ping_array_new(uint32_t size, uint32_t timeout);
/** /**
* Free all the allocated memory in a Ping_Array. * Free all the allocated memory in a Ping_Array.
*/ */
void ping_array_kill(struct Ping_Array *_array); void ping_array_kill(struct Ping_Array *array);
/** /**
* Add a data with length to the Ping_Array list and return a ping_id. * Add a data with length to the Ping_Array list and return a ping_id.
* *
* @return ping_id on success, 0 on failure. * @return ping_id on success, 0 on failure.
*/ */
uint64_t ping_array_add(struct Ping_Array *_array, const struct Mono_Time *mono_time, const uint8_t *data, uint64_t ping_array_add(struct Ping_Array *array, const struct Mono_Time *mono_time, const uint8_t *data,
uint32_t length); uint32_t length);
/** /**
@ -56,7 +56,7 @@ uint64_t ping_array_add(struct Ping_Array *_array, const struct Mono_Time *mono_
* *
* @return length of data copied on success, -1 on failure. * @return length of data copied on success, -1 on failure.
*/ */
int32_t ping_array_check(struct Ping_Array *_array, const struct Mono_Time *mono_time, uint8_t *data, size_t length, int32_t ping_array_check(struct Ping_Array *array, const struct Mono_Time *mono_time, uint8_t *data, size_t length,
uint64_t ping_id); uint64_t ping_id);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -14,6 +14,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
//!TOKSTYLE-
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -2854,5 +2856,7 @@ typedef TOX_CONNECTION Tox_Connection;
typedef TOX_FILE_CONTROL Tox_File_Control; typedef TOX_FILE_CONTROL Tox_File_Control;
typedef TOX_CONFERENCE_TYPE Tox_Conference_Type; typedef TOX_CONFERENCE_TYPE Tox_Conference_Type;
//!TOKSTYLE+
#endif // C_TOXCORE_TOXCORE_TOX_H #endif // C_TOXCORE_TOXCORE_TOX_H
%} %}

View File

@ -13,6 +13,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
//!TOKSTYLE-
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -3251,4 +3253,6 @@ typedef TOX_CONNECTION Tox_Connection;
typedef TOX_FILE_CONTROL Tox_File_Control; typedef TOX_FILE_CONTROL Tox_File_Control;
typedef TOX_CONFERENCE_TYPE Tox_Conference_Type; typedef TOX_CONFERENCE_TYPE Tox_Conference_Type;
//!TOKSTYLE+
#endif // C_TOXCORE_TOXCORE_TOX_H #endif // C_TOXCORE_TOXCORE_TOX_H

View File

@ -7,6 +7,7 @@
#define SET_ERROR_PARAMETER(param, x) do { if (param) { *param = x; } } while (0) #define SET_ERROR_PARAMETER(param, x) do { if (param) { *param = x; } } while (0)
//!TOKSTYLE-
#define CONST_FUNCTION(lowercase, uppercase) \ #define CONST_FUNCTION(lowercase, uppercase) \
uint32_t tox_##lowercase(void) \ uint32_t tox_##lowercase(void) \
@ -60,6 +61,8 @@ ACCESSORS(void *, log_, user_data)
ACCESSORS(bool,, local_discovery_enabled) ACCESSORS(bool,, local_discovery_enabled)
ACCESSORS(bool,, experimental_thread_safety) ACCESSORS(bool,, experimental_thread_safety)
//!TOKSTYLE+
const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options)
{ {
return options->savedata_data; return options->savedata_data;

View File

@ -201,7 +201,7 @@ crypto_pwhash_scryptsalsa208sha256_str_verify(const char str[crypto_pwhash_scryp
return -1; return -1;
} }
escrypt_free_local(&escrypt_local); escrypt_free_local(&escrypt_local);
ret = crypto_memcmp(wanted, str, sizeof wanted); ret = crypto_memcmp((const uint8_t *) wanted, (const uint8_t *) str, sizeof wanted);
crypto_memzero(wanted, sizeof wanted); crypto_memzero(wanted, sizeof wanted);
return ret; return ret;

View File

@ -1,7 +1,7 @@
#ifndef C_TOXCORE_TOXENCRYPTSAVE_DEFINES_H #ifndef C_TOXCORE_TOXENCRYPTSAVE_DEFINES_H
#define C_TOXCORE_TOXENCRYPTSAVE_DEFINES_H #define C_TOXCORE_TOXENCRYPTSAVE_DEFINES_H
#define TOX_ENC_SAVE_MAGIC_NUMBER "toxEsave" #define TOX_ENC_SAVE_MAGIC_NUMBER ((const uint8_t *)"toxEsave")
#define TOX_ENC_SAVE_MAGIC_LENGTH 8 #define TOX_ENC_SAVE_MAGIC_LENGTH 8
#endif #endif