x86/atomic: Fix UB on various atomic helpers
PATCH 1/1 x86/atomic: Fix UB on various atomic helpers
The atomic interface is signed. There's UB if a caller adds over INT_MAX or subtracts below INT_MIN where the addition/subtraction is done in C, just like in a regular int. Make the outward facing interface respect two's complement on wraparound by round-tripping through unsigned for arithmetic.
While at it, fix the while loop to have Xen style.
---
I'd prefer to turn this into an unsigned interface, but then some callers would need changing in non-trivial ways.
OTOH, one could argue the atomics merely highlight the UB already present in signed arithmetic, and thus this doesn't need fixing. I'd prefer to remove UB where possible.
--- xen/arch/x86/include/asm/atomic.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
xen/arch/x86/include/asm/atomic.h +6 -3
@@ -122,7 +122,7 @@ static inline void atomic_add(int i, atomic_t *v)static inline int atomic_add_return (int i,atomic_t *v) { -return i +arch_fetch_and_add (&v->counter, i); +return (unsigned int )i +arch_fetch_and_add (&v->counter, i); }static inline void atomic_sub (int i,atomic_t *v)
@@ -135,7 +135,9 @@ static inline void atomic_sub(int i, atomic_t *v)static inline int atomic_sub_return (int i,atomic_t *v) { -return arch_fetch_and_add (&v->counter, -i) - i; +unsigned int ui = i; + +return arch_fetch_and_add (&v->counter, -ui) - ui; }static inline int atomic_sub_and_test (int i,atomic_t *v)
@@ -221,8 +223,9 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)int c, old; c =atomic_read (v); -while (c != u && (old =atomic_cmpxchg (v, c, c + a)) != c) +while ( c != u && (old =atomic_cmpxchg (v, c, (unsigned int )c + a)) != c ) c = old; +return c; }
base-commit: 7168cc02f86fb069da6e7405afbf5c7a1c215b0d
Signature · 1 line
2.43.0
xen/arch/x86/include/asm/atomic.h static inline void atomic_add(int i, atomic_t *v) · line 126
Quoted 9 lines · Alejandro Vallejo
xen/arch/x86/include/asm/atomic.h +1
-1@@ -122,7 +122,7 @@ static inline void atomic_add(int i, atomic_t *v)static inline int atomic_add_return (int i,atomic_t *v) { -return i +arch_fetch_and_add (&v->counter, i); +return (unsigned int )i +arch_fetch_and_add (&v->counter, i); }
I'm concerned seeing this kind of change: It's merely papering over an issue in the caller. There's not going to be UB if the caller passes suitable values. And the atomic types hardly are meant to be used in contexts where overflow could occur.
Furthermore different parts of the C spec say different things here [1], [2]. Hence whether overflow in signed calculations is undefined or implementation-defined doesn't become entirely clear, according to my reading.
Yes, [2] is from "Conversions" when here we talk about expression results, but unsigned overflow not being UB also only follows from what's said under "Conversions".
Jan
[1] From "Expressions":
"If an exceptional condition occurs during the evaluation of an expression
(that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined."
[2] From "Conversions" / "Arithmetic operands" / "Signed and unsigned Integers":
"Otherwise, the new type is signed and the value cannot be represented in
it; either the result is implementation-defined or an implementation- defined signal is raised."
Quoted 16 lines · Jan Beulich
xen/arch/x86/include/asm/atomic.h static inline void atomic_add(int i, atomic_t *v) · line 126
Quoted 9 lines · Alejandro Vallejo
xen/arch/x86/include/asm/atomic.h +1
-1@@ -122,7 +122,7 @@ static inline void atomic_add(int i, atomic_t *v)static inline int atomic_add_return (int i,atomic_t *v) { -return i +arch_fetch_and_add (&v->counter, i); +return (unsigned int )i +arch_fetch_and_add (&v->counter, i); }I'm concerned seeing this kind of change: It's merely papering over an issue in the caller. There's not going to be UB if the caller passes suitable values. And the atomic types hardly are meant to be used in contexts where overflow could occur.
Quoted 4 lines
Furthermore different parts of the C spec say different things here [1], [2]. Hence whether overflow in signed calculations is undefined or implementation-defined doesn't become entirely clear, according to my reading.
I won't dwell on interpreting the standard, because I only care how GCC and Clang developers interpreted it; and that's a hard trap on signed overflow with sanitisers enabled.
I can live without this patch (it's just paranoia), but thinking about it some more, perhaps we ought to pass -fno-strict-overflow, like Linux does, to the compiler. This brings forward post-C23 overflow semantics and removes this whole class of UB wholesale. Here and elsewhere.
Thoughts?
Cheers,
Alejandro
Quoted 26 lines · Alejandro Vallejo
Quoted 16 lines · Jan Beulich
xen/arch/x86/include/asm/atomic.h static inline void atomic_add(int i, atomic_t *v) · line 126
Quoted 9 lines · Alejandro Vallejo
xen/arch/x86/include/asm/atomic.h +1
-1@@ -122,7 +122,7 @@ static inline void atomic_add(int i, atomic_t *v)static inline int atomic_add_return (int i,atomic_t *v) { -return i +arch_fetch_and_add (&v->counter, i); +return (unsigned int )i +arch_fetch_and_add (&v->counter, i); }I'm concerned seeing this kind of change: It's merely papering over an issue in the caller. There's not going to be UB if the caller passes suitable values. And the atomic types hardly are meant to be used in contexts where overflow could occur.
Quoted 4 lines
Furthermore different parts of the C spec say different things here [1], [2]. Hence whether overflow in signed calculations is undefined or implementation-defined doesn't become entirely clear, according to my reading.
I won't dwell on interpreting the standard, because I only care how GCC and Clang developers interpreted it; and that's a hard trap on signed overflow with sanitisers enabled.
Quoted 6 lines
I can live without this patch (it's just paranoia), but thinking about it some more, perhaps we ought to pass -fno-strict-overflow, like Linux does, to the compiler. This brings forward post-C23 overflow semantics and removes this whole class of UB wholesale. Here and elsewhere.
Thoughts?
Doing so sounds dangerous for an existing code base, as in risking to hide real issues. As said in the earlier reply, atomic_t uses very likely are intended to never overflow. If there were use cases, we may need to introduce uatomic_t (the unsigned counterpart that you mentioned as an option to _convert_ to), to be used there.
Jan