diff options
| author | Jeff Epler <jepler@gmail.com> | 2021-06-08 07:45:56 -0500 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-06-24 23:01:04 +1000 |
| commit | 413f34cd8f244d1ce8b3c73386fdd57241165a1b (patch) | |
| tree | 5448454b06510e0f0cdc0fcca2a3a99c57a6b068 /py | |
| parent | 0009a7dc305b9cce693b2e3fe94174c85dc1ef2e (diff) | |
all: Fix signed shifts and NULL access errors from -fsanitize=undefined.
Fixes the following (the line numbers match commit 0e87459e2bfd07):
../../extmod/crypto-algorithms/sha256.c:49:19: runtime error: left shif...
../../extmod/moduasyncio.c:106:35: runtime error: member access within ...
../../py/binary.c:210:13: runtime error: left shift of negative value -...
../../py/mpz.c:744:16: runtime error: negation of -9223372036854775808 ...
../../py/objint.c:109:22: runtime error: left shift of 1 by 31 places c...
../../py/objint_mpz.c:374:9: runtime error: left shift of 4611686018427...
../../py/objint_mpz.c:374:9: runtime error: left shift of negative valu...
../../py/parsenum.c:106:14: runtime error: left shift of 46116860184273...
../../py/runtime.c:395:33: runtime error: left shift of negative value ...
../../py/showbc.c:177:28: runtime error: left shift of negative value -...
../../py/vm.c:321:36: runtime error: left shift of negative value -1```
Testing was done on an amd64 Debian Buster system using gcc-8.3 and these
settings:
CFLAGS += -g3 -Og -fsanitize=undefined
LDFLAGS += -fsanitize=undefined
The introduced TASK_PAIRHEAP macro's conditional (x ? &x->i : NULL)
assembles (under amd64 gcc 8.3 -Os) to the same as &x->i, since i is the
initial field of the struct. However, for the purposes of undefined
behavior analysis the conditional is needed.
Signed-off-by: Jeff Epler <jepler@gmail.com>
Diffstat (limited to 'py')
| -rw-r--r-- | py/binary.c | 2 | ||||
| -rw-r--r-- | py/mpz.c | 2 | ||||
| -rw-r--r-- | py/objint.c | 6 | ||||
| -rw-r--r-- | py/runtime.c | 2 | ||||
| -rw-r--r-- | py/showbc.c | 2 | ||||
| -rw-r--r-- | py/smallint.h | 2 | ||||
| -rw-r--r-- | py/vm.c | 2 |
7 files changed, 9 insertions, 9 deletions
diff --git a/py/binary.c b/py/binary.c index 5c098b223..05e658c95 100644 --- a/py/binary.c +++ b/py/binary.c @@ -202,7 +202,7 @@ long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const delta = 1; } - long long val = 0; + unsigned long long val = 0; if (is_signed && *src & 0x80) { val = -1; } @@ -741,7 +741,7 @@ void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed) { unsigned long long uval; if (is_signed && val < 0) { z->neg = 1; - uval = -val; + uval = -(unsigned long long)val; } else { z->neg = 0; uval = val; diff --git a/py/objint.c b/py/objint.c index 00375d388..5ff5e7de4 100644 --- a/py/objint.c +++ b/py/objint.c @@ -106,14 +106,14 @@ STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) { #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE e |= u.i[MP_ENDIANNESS_BIG] != 0; #endif - if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) { + if ((e & ~(1U << MP_FLOAT_SIGN_SHIFT_I32)) == 0) { // handle case of -0 (when sign is set but rest of bits are zero) e = 0; } else { - e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32; + e += ((1U << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32; } } else { - e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1); + e &= ~((1U << MP_FLOAT_EXP_SHIFT_I32) - 1); } // 8 * sizeof(uintptr_t) counts the number of bits for a small int // TODO provide a way to configure this properly diff --git a/py/runtime.c b/py/runtime.c index cda26a9cf..261670d4f 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -392,7 +392,7 @@ mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { goto generic_binary_op; } else { // use standard precision - lhs_val <<= rhs_val; + lhs_val = (mp_uint_t)lhs_val << rhs_val; } break; } diff --git a/py/showbc.c b/py/showbc.c index 8941dd754..cb81b8835 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -174,7 +174,7 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip) { num--; } do { - num = (num << 7) | (*ip & 0x7f); + num = ((mp_uint_t)num << 7) | (*ip & 0x7f); } while ((*ip++ & 0x80) != 0); mp_printf(print, "LOAD_CONST_SMALL_INT " INT_FMT, num); break; diff --git a/py/smallint.h b/py/smallint.h index 58d843e8a..67daf9b9f 100644 --- a/py/smallint.h +++ b/py/smallint.h @@ -37,7 +37,7 @@ #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C #define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)MP_OBJ_WORD_MSBIT_HIGH) >> 1)) -#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & MP_OBJ_WORD_MSBIT_HIGH) == 0) +#define MP_SMALL_INT_FITS(n) ((((n) ^ ((mp_uint_t)(n) << 1)) & MP_OBJ_WORD_MSBIT_HIGH) == 0) // Mask to truncate mp_int_t to positive value #define MP_SMALL_INT_POSITIVE_MASK ~(MP_OBJ_WORD_MSBIT_HIGH | (MP_OBJ_WORD_MSBIT_HIGH >> 1)) @@ -312,7 +312,7 @@ dispatch_loop: DISPATCH(); ENTRY(MP_BC_LOAD_CONST_SMALL_INT): { - mp_int_t num = 0; + mp_uint_t num = 0; if ((ip[0] & 0x40) != 0) { // Number is negative num--; |
