Move casts to separate C file to prevent compiler from optimising them away

This commit is contained in:
rl1987 2019-03-20 18:54:11 +02:00
parent e52653e01a
commit 0bc9ed9d38
4 changed files with 77 additions and 6 deletions

View File

@ -211,6 +211,7 @@ src_test_test_slow_SOURCES += \
src/test/test_crypto_slow.c \
src/test/test_process_slow.c \
src/test/test_prob_distr.c \
src/test/ptr_helpers.c \
src/test/test_ptr_slow.c \
src/test/testing_common.c \
src/test/testing_rsakeys.c \
@ -315,6 +316,7 @@ noinst_HEADERS+= \
src/test/log_test_helpers.h \
src/test/rend_test_helpers.h \
src/test/test.h \
src/test/ptr_helpers.h \
src/test/test_helpers.h \
src/test/test_dir_common.h \
src/test/test_connection.h \

50
src/test/ptr_helpers.c Normal file
View File

@ -0,0 +1,50 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "ptr_helpers.h"
/**
* Cast <b> (inptr_t value) to a void pointer.
*/
void *
cast_intptr_to_voidstar(intptr_t x)
{
void *r = (void *)x;
return r;
}
/**
* Cast x (void pointer) to inptr_t value.
*/
intptr_t
cast_voidstar_to_intptr(void *x)
{
intptr_t r = (intptr_t)x;
return r;
}
/**
* Cast x (uinptr_t value) to void pointer.
*/
void *
cast_uintptr_to_voidstar(uintptr_t x)
{
void *r = (void *)x;
return r;
}
/**
* Cast x (void pointer) to uinptr_t value.
*/
uintptr_t
cast_voidstar_to_uintptr(void *x)
{
uintptr_t r = (uintptr_t)x;
return r;
}

18
src/test/ptr_helpers.h Normal file
View File

@ -0,0 +1,18 @@
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2019, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include <stdint.h>
void *
cast_intptr_to_voidstar(intptr_t x);
intptr_t
cast_voidstar_to_intptr(void *x);
void *
cast_uintptr_to_voidstar(uintptr_t x);
uintptr_t
cast_voidstar_to_uintptr(void *x);

View File

@ -6,6 +6,7 @@
#include "orconfig.h"
#include "core/or/or.h"
#include "test/test.h"
#include "test/ptr_helpers.h"
#include <stdint.h>
#include <limits.h>
@ -15,9 +16,9 @@ static void
assert_int_voidptr_roundtrip(int a)
{
intptr_t ap = (intptr_t)a;
void *b = (void *)ap;
intptr_t c = (intptr_t)b;
void *d = (void *)c;
void *b = cast_intptr_to_voidstar(ap);
intptr_t c = cast_voidstar_to_intptr(b);
void *d = cast_intptr_to_voidstar(c);
tt_assert(ap == c);
tt_assert(b == d);
@ -45,9 +46,9 @@ static void
assert_uint_voidptr_roundtrip(unsigned int a)
{
uintptr_t ap = (uintptr_t)a;
void *b = (void *)ap;
uintptr_t c = (uintptr_t)b;
void *d = (void *)c;
void *b = cast_uintptr_to_voidstar(ap);
uintptr_t c = cast_voidstar_to_uintptr(b);
void *d = cast_uintptr_to_voidstar(c);
tt_assert(ap == c);
tt_assert(b == d);