qemu-patch-raspberry4/hw
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
..
9pfs xen/9pfs: increase max ring order to 9 2020-05-25 11:45:40 +02:00
acpi Rename use_acpi_pci_hotplug to more appropriate use_acpi_hotplug_bridge 2020-06-24 19:03:57 -04:00
adc hw/adc/stm32f2xx_adc: Correct memory region size and access size 2020-06-05 17:23:09 +01:00
alpha sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
arm virtio,acpi,pci: fixes, cleanups. 2020-06-25 16:52:42 +01:00
audio hw/audio/gus: Fix registers 32-bit access 2020-06-19 11:20:09 +02:00
block virtio,acpi,pci: fixes, cleanups. 2020-06-25 16:52:42 +01:00
char hw/char: RX62N serial communication interface (SCI) 2020-06-22 18:37:12 +02:00
core qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
cpu sysbus: Convert qdev_set_parent_bus() use with Coccinelle, part 3 2020-06-15 22:06:04 +02:00
cris sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
display sysbus: Convert qdev_set_parent_bus() use with Coccinelle, part 3 2020-06-15 22:06:04 +02:00
dma sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
gpio hw/unicore32/puv3: Use qemu_log_mask(ERROR) instead of debug printf() 2020-06-09 19:01:56 +02:00
hppa sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
hyperv qdev: Convert bus-less devices to qdev_realize() with Coccinelle 2020-06-15 22:06:04 +02:00
i2c hw/i2c: Add header for ARM SBCon two-wire serial bus interface 2020-06-23 11:39:47 +01:00
i386 virtio,acpi,pci: fixes, cleanups. 2020-06-25 16:52:42 +01:00
ide qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
input acpi: move aml builder code for i8042 (kbd+mouse) device 2020-06-24 17:18:28 -04:00
intc hw/intc: RX62N interrupt controller (ICUa) 2020-06-22 18:37:12 +02:00
ipack qdev: Unrealize must not fail 2020-05-15 07:08:14 +02:00
ipmi various: Remove unnecessary OBJECT() cast 2020-05-15 07:08:14 +02:00
isa fdc: Reject clash between -drive if=floppy and -global isa-fdc 2020-06-23 16:07:07 +02:00
lm32 sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
m68k qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
mem nvdimm: Plug memory leak in uuid property setter 2020-05-27 07:44:59 +02:00
microblaze qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
mips sysbus: Convert qdev_set_parent_bus() use with Coccinelle, part 1 2020-06-15 22:06:04 +02:00
misc * hw: arm: Set vendor property for IMX SDHCI emulations 2020-06-16 13:36:31 +01:00
moxie hw: Make MachineClass::is_default a boolean type 2020-02-28 14:57:19 -05:00
net hw/net/e1000e: Do not abort() on invalid PSRCTL register value 2020-06-18 21:05:52 +08:00
nios2 sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
nubus hw: Remove unnecessary DEVICE() cast 2020-05-15 07:08:52 +02:00
nvram sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
openrisc sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
pci pci: pci_create(), pci_create_multifunction() are now unused, drop 2020-06-15 22:05:28 +02:00
pci-bridge sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
pci-host qdev: Convert bus-less devices to qdev_realize() with Coccinelle 2020-06-15 22:06:04 +02:00
pcmcia sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
ppc qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
rdma lockable: Replace locks with lock guard macros 2020-05-04 16:07:43 +01:00
riscv hw/riscv: sifive_u: Add a dummy DDR memory controller device 2020-06-19 08:25:27 -07:00
rtc sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
rx hw/rx: Add RX GDB simulator 2020-06-22 18:37:12 +02:00
s390x s390x/css: Refactor the css_queue_crw() routine 2020-06-18 12:13:54 +02:00
scsi hw/scsi/megasas: Fix possible out-of-bounds array access in tracepoints 2020-06-26 09:39:37 -04:00
sd sd/milkymist-memcard: Fix error API violation 2020-06-23 16:07:21 +02:00
semihosting semihosting: remove the pthread include which seems unused 2020-06-10 11:29:44 +02:00
sh4 hw/sh4: Extract timer definitions to 'hw/timer/tmu012.h' 2020-06-22 18:37:12 +02:00
smbios hw/smbios/smbios: Remove unused include 2020-02-06 10:38:57 +01:00
sparc sysbus: Convert to sysbus_realize() etc. with Coccinelle 2020-06-15 22:05:28 +02:00
sparc64 fdc: Reject clash between -drive if=floppy and -global isa-fdc 2020-06-23 16:07:07 +02:00
ssi ssi: ssi_create_slave_no_init() is now unused, drop 2020-06-15 22:05:28 +02:00
timer hw/timer: RX62N compare match timer (CMT) 2020-06-22 18:37:12 +02:00
tpm tpm: Move backend code under the 'backends/' directory 2020-06-19 07:25:55 -04:00
tricore hw: Do not initialize MachineClass::is_default to 0 2020-02-28 14:57:19 -05:00
unicore32 hw/unicore32/puv3: Use qemu_log_mask(ERROR) instead of debug printf() 2020-06-09 19:01:56 +02:00
usb osdep: Make MIN/MAX evaluate arguments only once 2020-06-26 09:39:39 -04:00
vfio vfio-ccw: Add support for the CRW region and IRQ 2020-06-18 12:13:54 +02:00
virtio Stop vhost-user sending uninitialized mmap_offsets 2020-06-24 17:18:28 -04:00
watchdog hw/watchdog/cmsdk-apb-watchdog: Add trace event for lock status 2020-06-23 11:39:47 +01:00
xen xen: Actually fix build without passthrough 2020-06-26 09:39:37 -04:00
xenpv trivial: Remove xenfb_enabled from sysemu.h 2020-02-04 09:00:57 +01:00
xtensa qdev: Make qdev_prop_set_drive() match the other helpers 2020-06-23 16:07:07 +02:00
Kconfig hw/rx: RX62N microcontroller (MCU) 2020-06-22 18:37:12 +02:00
Makefile.objs xen: fix build without pci passthrough 2020-06-12 11:20:12 -04:00