Skip to content

Commit f991911

Browse files
ebblakebonzini
authored andcommitted
osdep: Make MIN/MAX evaluate arguments only once
I'm not aware of any immediate bugs in qemu where a second runtime evaluation of the arguments to MIN() or MAX() causes a problem, but proactively preventing such abuse is easier than falling prey to an unintended case down the road. At any rate, here's the conversation that sparked the current patch: https://lists.gnu.org/archive/html/qemu-devel/2018-12/msg05718.html Update the MIN/MAX macros to only evaluate their argument once at runtime; this uses typeof(1 ? (a) : (b)) to ensure that we are promoting the temporaries to the same type as the final comparison (we have to trigger type promotion, as typeof(bitfield) won't compile; and we can't use typeof((a) + (b)) or even typeof((a) + 0), as some of our uses of MAX are on void* pointers where such addition is undefined). However, we are unable to work around gcc refusing to compile ({}) in a constant context (such as the array length of a static variable), even when only used in the dead branch of a __builtin_choose_expr(), so we have to provide a second macro pair MIN_CONST and MAX_CONST for use when both arguments are known to be compile-time constants and where the result must also be usable as a constant; this second form evaluates arguments multiple times but that doesn't matter for constants. By using a void expression as the expansion if a non-constant is presented to this second form, we can enlist the compiler to ensure the double evaluation is not attempted on non-constants. Alas, as both macros now rely on compiler intrinsics, they are no longer usable in preprocessor #if conditions; those will just have to be open-coded or the logic rewritten into #define or runtime 'if' conditions (but where the compiler dead-code-elimination will probably still apply). I tested that both gcc 10.1.1 and clang 10.0.0 produce errors for all forms of macro mis-use. As the errors can sometimes be cryptic, I'm demonstrating the gcc output: Use of MIN when MIN_CONST is needed: In file included from /home/eblake/qemu/qemu-img.c:25: /home/eblake/qemu/include/qemu/osdep.h:249:5: error: braced-group within expression allowed only inside a function 249 | ({ \ | ^ /home/eblake/qemu/qemu-img.c:92:12: note: in expansion of macro ‘MIN’ 92 | char array[MIN(1, 2)] = ""; | ^~~ Use of MIN_CONST when MIN is needed: /home/eblake/qemu/qemu-img.c: In function ‘is_allocated_sectors’: /home/eblake/qemu/qemu-img.c:1225:15: error: void value not ignored as it ought to be 1225 | i = MIN_CONST(i, n); | ^ Use of MIN in the preprocessor: In file included from /home/eblake/qemu/accel/tcg/translate-all.c:20: /home/eblake/qemu/accel/tcg/translate-all.c: In function ‘page_check_range’: /home/eblake/qemu/include/qemu/osdep.h:249:6: error: token "{" is not valid in preprocessor expressions 249 | ({ \ | ^ Fix the resulting callsites that used #if or computed a compile-time constant min or max to use the new macros. cpu-defs.h is interesting, as CPU_TLB_DYN_MAX_BITS is sometimes used as a constant and sometimes dynamic. It may be worth improving glib's MIN/MAX definitions to be saner, but that is a task for another day. Signed-off-by: Eric Blake <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Tested-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 47f0d11 commit f991911

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

accel/tcg/translate-all.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,9 +2582,9 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
25822582
/* This function should never be called with addresses outside the
25832583
guest address space. If this assert fires, it probably indicates
25842584
a missing call to h2g_valid. */
2585-
#if TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS
2586-
assert(start < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2587-
#endif
2585+
if (TARGET_ABI_BITS > L1_MAP_ADDR_SPACE_BITS) {
2586+
assert(start < ((target_ulong)1 << L1_MAP_ADDR_SPACE_BITS));
2587+
}
25882588

25892589
if (len == 0) {
25902590
return 0;

hw/usb/hcd-xhci.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ struct XHCIState {
214214
uint32_t dcbaap_high;
215215
uint32_t config;
216216

217-
USBPort uports[MAX(MAXPORTS_2, MAXPORTS_3)];
217+
USBPort uports[MAX_CONST(MAXPORTS_2, MAXPORTS_3)];
218218
XHCIPort ports[MAXPORTS];
219219
XHCISlot slots[MAXSLOTS];
220220
uint32_t numports;

include/block/block.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ typedef struct HDGeometry {
133133
#define BDRV_SECTOR_BITS 9
134134
#define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS)
135135

136-
#define BDRV_REQUEST_MAX_SECTORS MIN(SIZE_MAX >> BDRV_SECTOR_BITS, \
137-
INT_MAX >> BDRV_SECTOR_BITS)
136+
#define BDRV_REQUEST_MAX_SECTORS MIN_CONST(SIZE_MAX >> BDRV_SECTOR_BITS, \
137+
INT_MAX >> BDRV_SECTOR_BITS)
138138
#define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS)
139139

140140
/*

include/exec/cpu-all.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,9 @@ extern unsigned long reserved_va;
176176
* avoid setting bits at the top of guest addresses that might need
177177
* to be used for tags.
178178
*/
179-
#if MIN(TARGET_VIRT_ADDR_SPACE_BITS, TARGET_ABI_BITS) <= 32
180-
# define GUEST_ADDR_MAX_ UINT32_MAX
181-
#else
182-
# define GUEST_ADDR_MAX_ (~0ul)
183-
#endif
179+
#define GUEST_ADDR_MAX_ \
180+
((MIN_CONST(TARGET_VIRT_ADDR_SPACE_BITS, TARGET_ABI_BITS) <= 32) ? \
181+
UINT32_MAX : ~0ul)
184182
#define GUEST_ADDR_MAX (reserved_va ? reserved_va - 1 : GUEST_ADDR_MAX_)
185183

186184
#else

include/exec/cpu-defs.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ typedef uint64_t target_ulong;
102102
* Skylake's Level-2 STLB has 16 1G entries.
103103
* Also, make sure we do not size the TLB past the guest's address space.
104104
*/
105-
# define CPU_TLB_DYN_MAX_BITS \
105+
# ifdef TARGET_PAGE_BITS_VARY
106+
# define CPU_TLB_DYN_MAX_BITS \
106107
MIN(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
108+
# else
109+
# define CPU_TLB_DYN_MAX_BITS \
110+
MIN_CONST(22, TARGET_VIRT_ADDR_SPACE_BITS - TARGET_PAGE_BITS)
111+
# endif
107112
# endif
108113

109114
typedef struct CPUTLBEntry {

include/qemu/osdep.h

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,55 @@ extern int daemon(int, int);
236236
#define SIZE_MAX ((size_t)-1)
237237
#endif
238238

239-
#ifndef MIN
240-
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
241-
#endif
242-
#ifndef MAX
243-
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
244-
#endif
239+
/*
240+
* Two variations of MIN/MAX macros. The first is for runtime use, and
241+
* evaluates arguments only once (so it is safe even with side
242+
* effects), but will not work in constant contexts (such as array
243+
* size declarations) because of the '{}'. The second is for constant
244+
* expression use, where evaluating arguments twice is safe because
245+
* the result is going to be constant anyway, but will not work in a
246+
* runtime context because of a void expression where a value is
247+
* expected. Thus, both gcc and clang will fail to compile if you use
248+
* the wrong macro (even if the error may seem a bit cryptic).
249+
*
250+
* Note that neither form is usable as an #if condition; if you truly
251+
* need to write conditional code that depends on a minimum or maximum
252+
* determined by the pre-processor instead of the compiler, you'll
253+
* have to open-code it.
254+
*/
255+
#undef MIN
256+
#define MIN(a, b) \
257+
({ \
258+
typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
259+
_a < _b ? _a : _b; \
260+
})
261+
#define MIN_CONST(a, b) \
262+
__builtin_choose_expr( \
263+
__builtin_constant_p(a) && __builtin_constant_p(b), \
264+
(a) < (b) ? (a) : (b), \
265+
((void)0))
266+
#undef MAX
267+
#define MAX(a, b) \
268+
({ \
269+
typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
270+
_a > _b ? _a : _b; \
271+
})
272+
#define MAX_CONST(a, b) \
273+
__builtin_choose_expr( \
274+
__builtin_constant_p(a) && __builtin_constant_p(b), \
275+
(a) > (b) ? (a) : (b), \
276+
((void)0))
245277

246-
/* Minimum function that returns zero only iff both values are zero.
247-
* Intended for use with unsigned values only. */
278+
/*
279+
* Minimum function that returns zero only if both values are zero.
280+
* Intended for use with unsigned values only.
281+
*/
248282
#ifndef MIN_NON_ZERO
249-
#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
250-
((b) == 0 ? (a) : (MIN(a, b))))
283+
#define MIN_NON_ZERO(a, b) \
284+
({ \
285+
typeof(1 ? (a) : (b)) _a = (a), _b = (b); \
286+
_a == 0 ? _b : (_b == 0 || _b > _a) ? _a : _b; \
287+
})
251288
#endif
252289

253290
/* Round number down to multiple */

migration/qemu-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "qapi/error.h"
3232

3333
#define IO_BUF_SIZE 32768
34-
#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
34+
#define MAX_IOV_SIZE MIN_CONST(IOV_MAX, 64)
3535

3636
struct QEMUFile {
3737
const QEMUFileOps *ops;

0 commit comments

Comments
 (0)