qemu-e2k/include
Eric Blake f9919116b8 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 <eblake@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20200625162602.700741-1-eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2020-06-26 09:39:39 -04:00
..
authz Include generated QAPI headers less 2019-08-16 13:31:51 +02:00
block osdep: Make MIN/MAX evaluate arguments only once 2020-06-26 09:39:39 -04:00
chardev chardev: Use QEMUChrEvent enum in IOEventHandler typedef 2020-01-08 11:15:35 +01:00
crypto crypto/linux_keyring: add 'secret_keyring' secret object. 2020-06-15 11:33:51 +01:00
disas disas: include an optional note for the start of disassembly 2020-05-15 15:25:16 +01:00
exec osdep: Make MIN/MAX evaluate arguments only once 2020-06-26 09:39:39 -04:00
fpu softfloat: return low bits of quotient from floatx80_modrem 2020-06-26 09:39:38 -04:00
hw virtio,acpi,pci: fixes, cleanups. 2020-06-25 16:52:42 +01:00
io io/task: Move 'qom/object.h' header to source 2020-06-10 12:09:37 -04:00
libdecnumber include: Make headers more self-contained 2019-08-16 13:31:51 +02:00
migration vmstate.h: provide VMSTATE_VARRAY_UINT16_ALLOC macro 2020-06-18 21:05:50 +08:00
monitor hmp: Implement qom-get HMP command 2020-06-01 18:44:27 +01:00
net hw/net/can: Make CanBusClientInfo::can_receive() return a boolean 2020-03-31 21:14:35 +08:00
qapi qapi: Only input visitors can actually fail 2020-04-30 07:26:40 +02:00
qemu osdep: Make MIN/MAX evaluate arguments only once 2020-06-26 09:39:39 -04:00
qom qom: Less verbose object_initialize_child() 2020-06-15 22:05:28 +02:00
scsi scsi: explicitly list guest-recoverable sense codes 2019-07-15 11:20:42 +02:00
standard-headers Linux headers: update 2020-06-18 12:13:36 +02:00
sysemu Qdev patches for 2020-06-23 2020-06-25 09:34:52 +01:00
tcg tcg: call qemu_spin_destroy for tb->jmp_lock 2020-06-16 14:49:05 +01:00
ui ui/win32-kbd-hook: handle AltGr in a hook procedure 2020-05-19 09:06:44 +02:00
user linux-user: Include trace-root.h in syscall-trace.h 2020-01-15 15:13:09 -10:00
elf.h target/arm/arch_dump: Add SVE notes 2020-01-23 15:34:04 +00:00
glib-compat.h glib: bump min required glib library version to 2.48 2019-08-22 10:46:34 +01:00
qemu-common.h Update copyright date for user-facing copyright strings 2020-03-22 11:16:21 +00:00
qemu-io.h Include qemu-common.h exactly where needed 2019-06-12 13:20:20 +02:00
trace-tcg.h trace: get rid of generated-events.h/generated-events.c 2016-10-12 09:54:52 +02:00