Browse Source

Get rid of unaligned access errors given by UBSan

rand
Gavin Howard 1 year ago
parent
commit
6f80c6c6d2
Signed by: gavin GPG Key ID: C08038BDF280D33E
  1. 8
      include/num.h
  2. 2
      include/rand.h
  3. 10
      include/vm.h
  4. 3
      release.sh
  5. 22
      src/num.c
  6. 4
      src/rand/rand.c

8
include/num.h

@ -72,9 +72,9 @@ typedef unsigned long ulong;
#if BC_LONG_BIT >= 64
typedef int_least32_t BcDig;
typedef uint_fast64_t BcBigDig;
typedef uint64_t BcBigDig;
#define BC_NUM_BIGDIG_MAX (UINT_FAST64_MAX)
#define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT64_MAX)
#define BC_BASE_DIGS (9)
#define BC_BASE_POW (1000000000)
@ -85,9 +85,9 @@ typedef uint_fast64_t BcBigDig;
#elif BC_LONG_BIT >= 32
typedef int_least16_t BcDig;
typedef uint_fast32_t BcBigDig;
typedef uint32_t BcBigDig;
#define BC_NUM_BIGDIG_MAX (UINT_FAST32_MAX)
#define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT32_MAX)
#define BC_BASE_DIGS (4)
#define BC_BASE_POW (10000)

2
include/rand.h

@ -216,7 +216,7 @@ BcRand bc_rand_bounded(BcRNG *r, BcRand bound);
void bc_rand_seed(BcRNG *r, ulong state1, ulong state2, ulong inc1, ulong inc2);
void bc_rand_push(BcRNG *r);
void bc_rand_pop(BcRNG *r);
void bc_rand_getUlongs(BcRNG *r, ulong *s1, ulong *s2, ulong *i1, ulong *i2);
void bc_rand_getRands(BcRNG *r, BcRand *s1, BcRand *s2, BcRand *i1, BcRand *i2);
extern const BcRandState bc_rand_multiplier;

10
include/vm.h

@ -119,15 +119,15 @@
#define BC_MAX(a, b) ((a) > (b) ? (a) : (b))
#define BC_MIN(a, b) ((a) < (b) ? (a) : (b))
#define BC_MAX_OBASE ((ulong) (BC_BASE_POW))
#define BC_MAX_DIM ((ulong) (SIZE_MAX - 1))
#define BC_MAX_SCALE ((ulong) (BC_NUM_BIGDIG_MAX - 1))
#define BC_MAX_STRING ((ulong) (BC_NUM_BIGDIG_MAX - 1))
#define BC_MAX_OBASE ((BcBigDig) (BC_BASE_POW))
#define BC_MAX_DIM ((BcBigDig) (SIZE_MAX - 1))
#define BC_MAX_SCALE ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
#define BC_MAX_STRING ((BcBigDig) (BC_NUM_BIGDIG_MAX - 1))
#define BC_MAX_NAME BC_MAX_STRING
#define BC_MAX_NUM BC_MAX_SCALE
#if BC_ENABLE_EXTRA_MATH
#define BC_MAX_RAND ((ulong) (((BcRand) 0) - 1))
#define BC_MAX_RAND ((BcBigDig) (((BcRand) 0) - 1))
#endif // BC_ENABLE_EXTRA_MATH
#define BC_MAX_EXP ((ulong) (BC_NUM_BIGDIG_MAX))

3
release.sh

@ -468,6 +468,9 @@ else
defcc="c99"
fi
ASAN_OPTIONS="abort_on_error=1"
UBSAN_OPTIONS="print_stack_trace=1,silence_unsigned_overflow=1"
build "$debug" "$defcc" "-g" "1" "$bits"
header "Running math library under --standard"

22
src/num.c

@ -2153,7 +2153,7 @@ BcStatus bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
BcStatus s;
BcNum pow, temp, temp2, intn, frac;
ulong state1, state2, inc1, inc2;
BcRand state1, state2, inc1, inc2;
BcDig pow_num[BC_RAND_NUM_SIZE];
bc_num_setup(&pow, pow_num, sizeof(pow_num) / sizeof(BcDig));
@ -2190,8 +2190,8 @@ BcStatus bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
// quotient and remainder are both guaranteed to be less than vm->max,
// which means we can use bc_num_bigdig2() here and not worry about
// overflow.
bc_num_bigdig2(&temp2, &state1);
bc_num_bigdig2(&temp, &state2);
bc_num_bigdig2(&temp2, (BcBigDig*) &state1);
bc_num_bigdig2(&temp, (BcBigDig*) &state2);
}
else state1 = state2 = 0;
@ -2202,7 +2202,7 @@ BcStatus bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
// Because temp2 is the mod of vm->max, from above, it is guaranteed
// to be small enough to use bc_num_bigdig2().
bc_num_bigdig2(&temp2, &inc1);
bc_num_bigdig2(&temp2, (BcBigDig*) &inc1);
if (bc_num_cmp(&temp, &vm->max) > 0) {
bc_num_copy(&temp2, &temp);
@ -2212,7 +2212,7 @@ BcStatus bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
// The if statement above ensures that temp is less than vm->max, which
// means that we can use bc_num_bigdig2() here.
bc_num_bigdig2(&temp, &inc2);
bc_num_bigdig2(&temp, (BcBigDig*) &inc2);
}
else inc1 = inc2 = 0;
@ -2229,7 +2229,7 @@ err:
BcStatus bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
BcStatus s;
ulong s1, s2, i1, i2;
BcRand s1, s2, i1, i2;
BcNum pow, conv, temp1, temp2, temp3;
BcDig pow_num[BC_RAND_NUM_SIZE];
BcDig temp1_num[BC_RAND_NUM_SIZE], temp2_num[BC_RAND_NUM_SIZE];
@ -2246,14 +2246,14 @@ BcStatus bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
s = bc_num_mul(&vm->max, &vm->max, &pow, 0);
if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
bc_rand_getUlongs(rng, &s1, &s2, &i1, &i2);
bc_rand_getRands(rng, &s1, &s2, &i1, &i2);
bc_num_bigdig2num(&conv, s2);
bc_num_bigdig2num(&conv, (BcBigDig) s2);
s = bc_num_mul(&conv, &vm->max, &temp1, 0);
if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
bc_num_bigdig2num(&conv, s1);
bc_num_bigdig2num(&conv, (BcBigDig) s1);
s = bc_num_add(&conv, &temp1, &temp2, 0);
if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
@ -2261,12 +2261,12 @@ BcStatus bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
s = bc_num_div(&temp2, &pow, &temp3, BC_RAND_STATE_BITS);
if (BC_ERR(s)) goto err;
bc_num_bigdig2num(&conv, i2);
bc_num_bigdig2num(&conv, (BcBigDig) i2);
s = bc_num_mul(&conv, &vm->max, &temp1, 0);
if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
bc_num_bigdig2num(&conv, i1);
bc_num_bigdig2num(&conv, (BcBigDig) i1);
s = bc_num_add(&conv, &temp1, &temp2, 0);
if (BC_ERROR_SIGNAL_ONLY(s)) goto err;

4
src/rand/rand.c

@ -360,8 +360,8 @@ static BcRandState bc_rand_getInc(BcRNGData *r) {
return res;
}
void bc_rand_getUlongs(BcRNG *r, ulong *s1, ulong *s2, ulong *i1, ulong *i2) {
void bc_rand_getRands(BcRNG *r, BcRand *s1, BcRand *s2, BcRand *i1, BcRand *i2)
{
BcRNGData *rng;
BcRandState inc;

Loading…
Cancel
Save