[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Signed overflow Was: Re: GNU poke 2.0.92 with ubsan
From: |
Jose E. Marchesi |
Subject: |
Signed overflow Was: Re: GNU poke 2.0.92 with ubsan |
Date: |
Mon, 07 Feb 2022 14:39:49 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Hi Bruno.
So I tried with ubsan with your patch, plus these changes:
diff --git a/libpoke/pkl-fold.c b/libpoke/pkl-fold.c
index 71656d75..2c96bfb4 100644
--- a/libpoke/pkl-fold.c
+++ b/libpoke/pkl-fold.c
@@ -227,8 +227,8 @@ EMUL_UU (bnoto) { return ~op; }
/* Check for overflow in several signed */ \
/* arithmetic operations. */ \
size_t size = PKL_AST_TYPE_I_SIZE (type); \
- int64_t op_val = ((int64_t) PKL_AST_INTEGER_VALUE (op) \
- << (64 - size)); \
+ int64_t op_val = ((uint64_t) (int64_t) PKL_AST_INTEGER_VALUE
(op)) \
+ << (64 - size); \
\
switch (PKL_AST_EXP_CODE (PKL_PASS_NODE)) \
{ \
@@ -583,10 +583,10 @@ EMUL_UU (bnoto) { return ~op; }
/* Check for overflow in several signed */ \
/* arithmetic operations. */ \
size_t size = PKL_AST_TYPE_I_SIZE (type); \
- int64_t op1_val = ((int64_t) PKL_AST_INTEGER_VALUE (op1) \
- << (64 - size)); \
- int64_t op2_val = ((int64_t) PKL_AST_INTEGER_VALUE (op2) \
- << (64 - size)); \
+ int64_t op1_val = ((uint64_t)(int64_t) PKL_AST_INTEGER_VALUE
(op1)) \
+ << (64 - size); \
+ int64_t op2_val = ((uint64_t)(int64_t) PKL_AST_INTEGER_VALUE
(op2)) \
+ << (64 - size); \
\
switch (PKL_AST_EXP_CODE (PKL_PASS_NODE)) \
{ \
diff --git a/libpoke/pvm-val.h b/libpoke/pvm-val.h
index 9489bf4a..1954e40e 100644
--- a/libpoke/pvm-val.h
+++ b/libpoke/pvm-val.h
@@ -64,7 +64,7 @@
<< (32 - PVM_VAL_INT_SIZE ((V))) \
>> (32 - PVM_VAL_INT_SIZE ((V))))
#define PVM_MAKE_INT(V,S) \
- (((((int64_t) (V)) & 0xffffffff) << 32) \
+ (((((uint64_t)(int64_t) (V)) & 0xffffffff) << 32) \
| ((((S) - 1) & 0x1f) << 3) \
| PVM_VAL_TAG_INT)
diff --git a/libpoke/pvm.jitter b/libpoke/pvm.jitter
index 80fc58df..495eaa29 100644
--- a/libpoke/pvm.jitter
+++ b/libpoke/pvm.jitter
@@ -323,7 +323,7 @@ late-header-c
{ \
CTYPE a = PVM_VAL_##TYPE (JITTER_TOP_STACK ()); \
int size = PVM_VAL_##TYPE##_SIZE (JITTER_TOP_STACK ()); \
- int64_t a64 = ((int64_t) a << (64 - size)); \
+ int64_t a64 = ((uint64_t)(int64_t) a) << (64 - size); \
\
if (INT_NEGATE_OVERFLOW (a64)) \
PVM_RAISE_DFL (PVM_E_OVERFLOW); \
@@ -404,7 +404,7 @@ late-header-c
CTYPE a = PVM_VAL_##TYPE (JITTER_UNDER_TOP_STACK ()); \
CTYPE b = PVM_VAL_##TYPE (JITTER_TOP_STACK ()); \
int size = PVM_VAL_##TYPE##_SIZE (JITTER_TOP_STACK ()); \
- int64_t a64 = ((int64_t) a << (64 - size)); \
+ int64_t a64 = ((uint64_t)(int64_t) a) << (64 - size); \
\
if (INT_DIVIDE_OVERFLOW (a64, b)) \
PVM_RAISE_DFL (PVM_E_OVERFLOW); \
@@ -422,7 +422,7 @@ late-header-c
CTYPE a = PVM_VAL_##TYPE (JITTER_UNDER_TOP_STACK ()); \
CTYPE b = PVM_VAL_##TYPE (JITTER_TOP_STACK ()); \
int size = PVM_VAL_##TYPE##_SIZE (JITTER_TOP_STACK ()); \
- int64_t a64 = ((int64_t) a << (64 - size)); \
+ int64_t a64 = ((uint64_t) (int64_t) a) << (64 - size); \
\
if (INT_DIVIDE_OVERFLOW (a64, b)) \
PVM_RAISE_DFL (PVM_E_OVERFLOW); \
The testsuite now passes without failures and without UBSAN warnings.
However, the changes in pkl-fold.c trigger a compile-time warning
(-Woverflow) in the gnulib macros INT_*_OVERFLOW.
This is one of these warnings:
In file included from ../../gl-libpoke/xalloc.h:27:0,
from ../../libpoke/pkl-fold.c:28:
../../libpoke/pkl-fold.c: In function 'pkl_fold_or':
../../gl-libpoke/intprops.h:310:65: warning: integer overflow in expression
[-Woverflow]
: (a) % (_GL_INT_CONVERT (a, _GL_SIGNED_INT_MAXIMUM (b)) + 1)) \
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
../../gl-libpoke/intprops.h:300:19: note: in expansion of macro
'_GL_UNSIGNED_NEG_MULTIPLE'
: (b) < 0 && ! _GL_UNSIGNED_NEG_MULTIPLE (a, b, max))
^~~~~~~~~~~~~~~~~~~~~~~~~
../../gl-libpoke/intprops.h:388:3: note: in expansion of macro
'_GL_REMAINDER_OVERFLOW'
op_result_overflow (a, b, \
^~~~~~~~~~~~~~~~~~
../../gl-libpoke/intprops.h:378:3: note: in expansion of macro
'_GL_BINARY_OP_OVERFLOW'
_GL_BINARY_OP_OVERFLOW (a, b, _GL_REMAINDER_OVERFLOW)
^~~~~~~~~~~~~~~~~~~~~~
../../libpoke/pkl-fold.c:613:23: note: in expansion of macro
'INT_REMAINDER_OVERFLOW'
if (INT_REMAINDER_OVERFLOW (op1_val, \
- Re: GNU poke 2.0.92 invokes undefined behaviour on signed integers, (continued)
- Re: GNU poke 2.0.92 invokes undefined behaviour on signed integers, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 on arm, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 on mips, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 on x86_64-{32,x32}, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 on alpha, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 with ubsan, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 with ubsan, Mohammad-Reza Nabipoor, 2022/02/06
- Re: GNU poke 2.0.92 with ubsan, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 with ubsan, Bruno Haible, 2022/02/06
- Re: GNU poke 2.0.92 with ubsan, Jose E. Marchesi, 2022/02/07
- Signed overflow Was: Re: GNU poke 2.0.92 with ubsan,
Jose E. Marchesi <=
- Re: Signed overflow Was: Re: GNU poke 2.0.92 with ubsan, Bruno Haible, 2022/02/07
- Re: Signed overflow Was: Re: GNU poke 2.0.92 with ubsan, Jose E. Marchesi, 2022/02/07
Re: GNU poke 2.0.92 on s390x, Bruno Haible, 2022/02/06
Re: GNU poke 2.0.92 on riscv64, Bruno Haible, 2022/02/06