Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Define _GNU_SOURCE for all selftests code

Define _GNU_SOURCE is the base CFLAGS instead of relying on selftests to
manually #define _GNU_SOURCE, which is repetitive and error prone. E.g.
kselftest_harness.h requires _GNU_SOURCE for asprintf(), but if a selftest
includes kvm_test_harness.h after stdio.h, the include guards result in
the effective version of stdio.h consumed by kvm_test_harness.h not
defining asprintf():

In file included from x86_64/fix_hypercall_test.c:12:
In file included from include/kvm_test_harness.h:11:
../kselftest_harness.h:1169:2: error: call to undeclared function
'asprintf'; ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
1169 | asprintf(&test_name, "%s%s%s.%s", f->name,
| ^

When including the rseq selftest's "library" code, #undef _GNU_SOURCE so
that rseq.c controls whether or not it wants to build with _GNU_SOURCE.

Reported-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Acked-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Acked-by: Oliver Upton <oliver.upton@linux.dev>
Acked-by: Anup Patel <anup@brainfault.org>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Link: https://lore.kernel.org/r/20240423190308.2883084-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

+17 -120
+2 -2
tools/testing/selftests/kvm/Makefile
··· 226 226 endif 227 227 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ 228 228 -Wno-gnu-variable-sized-type-not-at-end -MD -MP -DCONFIG_64BIT \ 229 - -fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \ 230 - -fno-builtin-strnlen \ 229 + -D_GNU_SOURCE -fno-builtin-memcmp -fno-builtin-memcpy \ 230 + -fno-builtin-memset -fno-builtin-strnlen \ 231 231 -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ 232 232 -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ 233 233 -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
-2
tools/testing/selftests/kvm/aarch64/arch_timer.c
··· 5 5 * 6 6 * Copyright (c) 2021, Google LLC. 7 7 */ 8 - #define _GNU_SOURCE 9 - 10 8 #include "arch_timer.h" 11 9 #include "delay.h" 12 10 #include "gic.h"
-1
tools/testing/selftests/kvm/aarch64/page_fault_test.c
··· 7 7 * hugetlbfs with a hole). It checks that the expected handling method is 8 8 * called (e.g., uffd faults with the right address and write/read flag). 9 9 */ 10 - #define _GNU_SOURCE 11 10 #include <linux/bitmap.h> 12 11 #include <fcntl.h> 13 12 #include <test_util.h>
-3
tools/testing/selftests/kvm/aarch64/psci_test.c
··· 10 10 * - A test for KVM's handling of PSCI SYSTEM_SUSPEND and the associated 11 11 * KVM_SYSTEM_EVENT_SUSPEND UAPI. 12 12 */ 13 - 14 - #define _GNU_SOURCE 15 - 16 13 #include <linux/psci.h> 17 14 18 15 #include "kvm_util.h"
-1
tools/testing/selftests/kvm/aarch64/vgic_init.c
··· 4 4 * 5 5 * Copyright (C) 2020, Red Hat, Inc. 6 6 */ 7 - #define _GNU_SOURCE 8 7 #include <linux/kernel.h> 9 8 #include <sys/syscall.h> 10 9 #include <asm/kvm.h>
-3
tools/testing/selftests/kvm/arch_timer.c
··· 19 19 * 20 20 * Copyright (c) 2021, Google LLC. 21 21 */ 22 - 23 - #define _GNU_SOURCE 24 - 25 22 #include <stdlib.h> 26 23 #include <pthread.h> 27 24 #include <linux/sizes.h>
-3
tools/testing/selftests/kvm/demand_paging_test.c
··· 6 6 * Copyright (C) 2018, Red Hat, Inc. 7 7 * Copyright (C) 2019, Google, Inc. 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for pipe2 */ 11 - 12 9 #include <inttypes.h> 13 10 #include <stdio.h> 14 11 #include <stdlib.h>
-3
tools/testing/selftests/kvm/dirty_log_test.c
··· 4 4 * 5 5 * Copyright (C) 2018, Red Hat, Inc. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_name */ 9 - 10 7 #include <stdio.h> 11 8 #include <stdlib.h> 12 9 #include <pthread.h>
-2
tools/testing/selftests/kvm/guest_memfd_test.c
··· 4 4 * 5 5 * Author: Chao Peng <chao.p.peng@linux.intel.com> 6 6 */ 7 - 8 - #define _GNU_SOURCE 9 7 #include <stdlib.h> 10 8 #include <string.h> 11 9 #include <unistd.h>
-3
tools/testing/selftests/kvm/hardware_disable_test.c
··· 4 4 * kvm_arch_hardware_disable is called and it attempts to unregister the user 5 5 * return notifiers. 6 6 */ 7 - 8 - #define _GNU_SOURCE 9 - 10 7 #include <fcntl.h> 11 8 #include <pthread.h> 12 9 #include <semaphore.h>
+6 -6
tools/testing/selftests/kvm/include/kvm_util_base.h
··· 27 27 28 28 /* 29 29 * Provide a version of static_assert() that is guaranteed to have an optional 30 - * message param. If _ISOC11_SOURCE is defined, glibc (/usr/include/assert.h) 31 - * #undefs and #defines static_assert() as a direct alias to _Static_assert(), 32 - * i.e. effectively makes the message mandatory. Many KVM selftests #define 33 - * _GNU_SOURCE for various reasons, and _GNU_SOURCE implies _ISOC11_SOURCE. As 34 - * a result, static_assert() behavior is non-deterministic and may or may not 35 - * require a message depending on #include order. 30 + * message param. _GNU_SOURCE is defined for all KVM selftests, _GNU_SOURCE 31 + * implies _ISOC11_SOURCE, and if _ISOC11_SOURCE is defined, glibc #undefs and 32 + * #defines static_assert() as a direct alias to _Static_assert() (see 33 + * usr/include/assert.h). Define a custom macro instead of redefining 34 + * static_assert() to avoid creating non-deterministic behavior that is 35 + * dependent on include order. 36 36 */ 37 37 #define __kvm_static_assert(expr, msg, ...) _Static_assert(expr, msg) 38 38 #define kvm_static_assert(expr, ...) __kvm_static_assert(expr, ##__VA_ARGS__, #expr)
-3
tools/testing/selftests/kvm/include/userfaultfd_util.h
··· 5 5 * Copyright (C) 2018, Red Hat, Inc. 6 6 * Copyright (C) 2019-2022 Google LLC 7 7 */ 8 - 9 - #define _GNU_SOURCE /* for pipe2 */ 10 - 11 8 #include <inttypes.h> 12 9 #include <time.h> 13 10 #include <pthread.h>
-2
tools/testing/selftests/kvm/kvm_binary_stats_test.c
··· 6 6 * 7 7 * Test the fd-based interface for KVM statistics. 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for program_invocation_short_name */ 11 9 #include <fcntl.h> 12 10 #include <stdio.h> 13 11 #include <stdlib.h>
-2
tools/testing/selftests/kvm/kvm_create_max_vcpus.c
··· 6 6 * 7 7 * Test for KVM_CAP_MAX_VCPUS and KVM_CAP_MAX_VCPU_ID. 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for program_invocation_short_name */ 11 9 #include <fcntl.h> 12 10 #include <stdio.h> 13 11 #include <stdlib.h>
-3
tools/testing/selftests/kvm/kvm_page_table_test.c
··· 8 8 * page size have been pre-allocated on your system, if you are planning to 9 9 * use hugepages to back the guest memory for testing. 10 10 */ 11 - 12 - #define _GNU_SOURCE /* for program_invocation_name */ 13 - 14 11 #include <stdio.h> 15 12 #include <stdlib.h> 16 13 #include <time.h>
-3
tools/testing/selftests/kvm/lib/assert.c
··· 4 4 * 5 5 * Copyright (C) 2018, Google LLC. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for getline(3) and strchrnul(3)*/ 9 - 10 7 #include "test_util.h" 11 8 12 9 #include <execinfo.h>
-2
tools/testing/selftests/kvm/lib/kvm_util.c
··· 4 4 * 5 5 * Copyright (C) 2018, Google LLC. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_name */ 9 7 #include "test_util.h" 10 8 #include "kvm_util.h" 11 9 #include "processor.h"
-2
tools/testing/selftests/kvm/lib/memstress.c
··· 2 2 /* 3 3 * Copyright (C) 2020, Google LLC. 4 4 */ 5 - #define _GNU_SOURCE 6 - 7 5 #include <inttypes.h> 8 6 #include <linux/bitmap.h> 9 7
-2
tools/testing/selftests/kvm/lib/test_util.c
··· 4 4 * 5 5 * Copyright (C) 2020, Google LLC. 6 6 */ 7 - 8 - #define _GNU_SOURCE 9 7 #include <stdio.h> 10 8 #include <stdarg.h> 11 9 #include <assert.h>
-3
tools/testing/selftests/kvm/lib/userfaultfd_util.c
··· 6 6 * Copyright (C) 2018, Red Hat, Inc. 7 7 * Copyright (C) 2019-2022 Google LLC 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for pipe2 */ 11 - 12 9 #include <inttypes.h> 13 10 #include <stdio.h> 14 11 #include <stdlib.h>
-1
tools/testing/selftests/kvm/lib/x86_64/sev.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #define _GNU_SOURCE /* for program_invocation_short_name */ 3 2 #include <stdint.h> 4 3 #include <stdbool.h> 5 4
-2
tools/testing/selftests/kvm/max_guest_memory_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #define _GNU_SOURCE 3 - 4 2 #include <stdio.h> 5 3 #include <stdlib.h> 6 4 #include <pthread.h>
-3
tools/testing/selftests/kvm/memslot_modification_stress_test.c
··· 6 6 * Copyright (C) 2018, Red Hat, Inc. 7 7 * Copyright (C) 2020, Google, Inc. 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for program_invocation_name */ 11 - 12 9 #include <stdio.h> 13 10 #include <stdlib.h> 14 11 #include <sys/syscall.h>
-3
tools/testing/selftests/kvm/riscv/arch_timer.c
··· 7 7 * 8 8 * Copyright (c) 2024, Intel Corporation. 9 9 */ 10 - 11 - #define _GNU_SOURCE 12 - 13 10 #include "arch_timer.h" 14 11 #include "kvm_util.h" 15 12 #include "processor.h"
+9 -3
tools/testing/selftests/kvm/rseq_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #define _GNU_SOURCE /* for program_invocation_short_name */ 2 + 3 + /* 4 + * Include rseq.c without _GNU_SOURCE defined, before including any headers, so 5 + * that rseq.c is compiled with its configuration, not KVM selftests' config. 6 + */ 7 + #undef _GNU_SOURCE 8 + #include "../rseq/rseq.c" 9 + #define _GNU_SOURCE 10 + 3 11 #include <errno.h> 4 12 #include <fcntl.h> 5 13 #include <pthread.h> ··· 27 19 #include "kvm_util.h" 28 20 #include "processor.h" 29 21 #include "test_util.h" 30 - 31 - #include "../rseq/rseq.c" 32 22 33 23 /* 34 24 * Any bug related to task migration is likely to be timing-dependent; perform
-2
tools/testing/selftests/kvm/s390x/cmma_test.c
··· 7 7 * Authors: 8 8 * Nico Boehr <nrb@linux.ibm.com> 9 9 */ 10 - 11 - #define _GNU_SOURCE /* for program_invocation_short_name */ 12 10 #include <fcntl.h> 13 11 #include <stdio.h> 14 12 #include <stdlib.h>
-2
tools/testing/selftests/kvm/s390x/sync_regs_test.c
··· 10 10 * 11 11 * Test expected behavior of the KVM_CAP_SYNC_REGS functionality. 12 12 */ 13 - 14 - #define _GNU_SOURCE /* for program_invocation_short_name */ 15 13 #include <fcntl.h> 16 14 #include <stdio.h> 17 15 #include <stdlib.h>
-1
tools/testing/selftests/kvm/set_memory_region_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #define _GNU_SOURCE /* for program_invocation_short_name */ 3 2 #include <fcntl.h> 4 3 #include <pthread.h> 5 4 #include <sched.h>
-1
tools/testing/selftests/kvm/steal_time.c
··· 4 4 * 5 5 * Copyright (C) 2020, Red Hat, Inc. 6 6 */ 7 - #define _GNU_SOURCE 8 7 #include <stdio.h> 9 8 #include <time.h> 10 9 #include <sched.h>
-2
tools/testing/selftests/kvm/x86_64/amx_test.c
··· 6 6 * 7 7 * Tests for amx #NM exception and save/restore. 8 8 */ 9 - 10 - #define _GNU_SOURCE /* for program_invocation_short_name */ 11 9 #include <fcntl.h> 12 10 #include <stdio.h> 13 11 #include <stdlib.h>
-3
tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c
··· 4 4 * 5 5 * Test for KVM_CAP_EXIT_ON_EMULATION_FAILURE. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_short_name */ 9 - 10 7 #include "flds_emulation.h" 11 8 12 9 #include "test_util.h"
-2
tools/testing/selftests/kvm/x86_64/hwcr_msr_test.c
··· 2 2 /* 3 3 * Copyright (C) 2023, Google LLC. 4 4 */ 5 - 6 - #define _GNU_SOURCE /* for program_invocation_short_name */ 7 5 #include <sys/ioctl.h> 8 6 9 7 #include "test_util.h"
-2
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c
··· 7 7 * This work is licensed under the terms of the GNU GPL, version 2. 8 8 * 9 9 */ 10 - 11 - #define _GNU_SOURCE /* for program_invocation_short_name */ 12 10 #include <fcntl.h> 13 11 #include <stdio.h> 14 12 #include <stdlib.h>
-1
tools/testing/selftests/kvm/x86_64/hyperv_evmcs.c
··· 4 4 * 5 5 * Tests for Enlightened VMCS, including nested guest state. 6 6 */ 7 - #define _GNU_SOURCE /* for program_invocation_short_name */ 8 7 #include <fcntl.h> 9 8 #include <stdio.h> 10 9 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/hyperv_ipi.c
··· 5 5 * Copyright (C) 2022, Red Hat, Inc. 6 6 * 7 7 */ 8 - 9 - #define _GNU_SOURCE /* for program_invocation_short_name */ 10 8 #include <pthread.h> 11 9 #include <inttypes.h> 12 10
-1
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
··· 4 4 * 5 5 * Tests for Hyper-V extensions to SVM. 6 6 */ 7 - #define _GNU_SOURCE /* for program_invocation_short_name */ 8 7 #include <fcntl.h> 9 8 #include <stdio.h> 10 9 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/hyperv_tlb_flush.c
··· 5 5 * Copyright (C) 2022, Red Hat, Inc. 6 6 * 7 7 */ 8 - 9 - #define _GNU_SOURCE /* for program_invocation_short_name */ 10 8 #include <asm/barrier.h> 11 9 #include <pthread.h> 12 10 #include <inttypes.h>
-2
tools/testing/selftests/kvm/x86_64/nested_exceptions_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #define _GNU_SOURCE /* for program_invocation_short_name */ 3 - 4 2 #include "test_util.h" 5 3 #include "kvm_util.h" 6 4 #include "processor.h"
-3
tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
··· 5 5 * 6 6 * Copyright (C) 2022, Google LLC. 7 7 */ 8 - 9 - #define _GNU_SOURCE 10 - 11 8 #include <fcntl.h> 12 9 #include <stdint.h> 13 10 #include <time.h>
-2
tools/testing/selftests/kvm/x86_64/platform_info_test.c
··· 9 9 * Verifies expected behavior of controlling guest access to 10 10 * MSR_PLATFORM_INFO. 11 11 */ 12 - 13 - #define _GNU_SOURCE /* for program_invocation_short_name */ 14 12 #include <fcntl.h> 15 13 #include <stdio.h> 16 14 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
··· 2 2 /* 3 3 * Copyright (C) 2023, Tencent, Inc. 4 4 */ 5 - 6 - #define _GNU_SOURCE /* for program_invocation_short_name */ 7 5 #include <x86intrin.h> 8 6 9 7 #include "pmu.h"
-3
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
··· 9 9 * Verifies the expected behavior of allow lists and deny lists for 10 10 * virtual PMU events. 11 11 */ 12 - 13 - #define _GNU_SOURCE /* for program_invocation_short_name */ 14 - 15 12 #include "kvm_util.h" 16 13 #include "pmu.h" 17 14 #include "processor.h"
-1
tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c
··· 2 2 /* 3 3 * Copyright (C) 2022, Google LLC. 4 4 */ 5 - #define _GNU_SOURCE /* for program_invocation_short_name */ 6 5 #include <fcntl.h> 7 6 #include <limits.h> 8 7 #include <pthread.h>
-1
tools/testing/selftests/kvm/x86_64/set_boot_cpu_id.c
··· 4 4 * 5 5 * Copyright (C) 2020, Red Hat, Inc. 6 6 */ 7 - #define _GNU_SOURCE /* for program_invocation_name */ 8 7 #include <fcntl.h> 9 8 #include <stdio.h> 10 9 #include <stdlib.h>
-1
tools/testing/selftests/kvm/x86_64/set_sregs_test.c
··· 10 10 * That bug allowed a user-mode program that called the KVM_SET_SREGS 11 11 * ioctl to put a VCPU's local APIC into an invalid state. 12 12 */ 13 - #define _GNU_SOURCE /* for program_invocation_short_name */ 14 13 #include <fcntl.h> 15 14 #include <stdio.h> 16 15 #include <stdlib.h>
-3
tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c
··· 5 5 * Test that KVM emulates instructions in response to EPT violations when 6 6 * allow_smaller_maxphyaddr is enabled and guest.MAXPHYADDR < host.MAXPHYADDR. 7 7 */ 8 - 9 - #define _GNU_SOURCE /* for program_invocation_short_name */ 10 - 11 8 #include "flds_emulation.h" 12 9 13 10 #include "test_util.h"
-1
tools/testing/selftests/kvm/x86_64/smm_test.c
··· 4 4 * 5 5 * Tests for SMM. 6 6 */ 7 - #define _GNU_SOURCE /* for program_invocation_short_name */ 8 7 #include <fcntl.h> 9 8 #include <stdio.h> 10 9 #include <stdlib.h>
-1
tools/testing/selftests/kvm/x86_64/state_test.c
··· 6 6 * 7 7 * Tests for vCPU state save/restore, including nested guest state. 8 8 */ 9 - #define _GNU_SOURCE /* for program_invocation_short_name */ 10 9 #include <fcntl.h> 11 10 #include <stdio.h> 12 11 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/sync_regs_test.c
··· 8 8 * including requesting an invalid register set, updates to/from values 9 9 * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled. 10 10 */ 11 - 12 - #define _GNU_SOURCE /* for program_invocation_short_name */ 13 11 #include <fcntl.h> 14 12 #include <stdio.h> 15 13 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/ucna_injection_test.c
··· 17 17 * delivered into the guest or not. 18 18 * 19 19 */ 20 - 21 - #define _GNU_SOURCE /* for program_invocation_short_name */ 22 20 #include <pthread.h> 23 21 #include <inttypes.h> 24 22 #include <string.h>
-2
tools/testing/selftests/kvm/x86_64/userspace_msr_exit_test.c
··· 4 4 * 5 5 * Tests for exiting into userspace on registered MSRs 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_short_name */ 9 7 #include <sys/ioctl.h> 10 8 11 9 #include "kvm_test_harness.h"
-3
tools/testing/selftests/kvm/x86_64/vmx_dirty_log_test.c
··· 4 4 * 5 5 * Copyright (C) 2018, Red Hat, Inc. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_name */ 9 - 10 7 #include <stdio.h> 11 8 #include <stdlib.h> 12 9 #include <linux/bitmap.h>
-1
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
··· 10 10 * and check it can be retrieved with KVM_GET_MSR, also test 11 11 * the invalid LBR formats are rejected. 12 12 */ 13 - #define _GNU_SOURCE /* for program_invocation_short_name */ 14 13 #include <sys/ioctl.h> 15 14 16 15 #include <linux/bitmap.h>
-1
tools/testing/selftests/kvm/x86_64/vmx_preemption_timer_test.c
··· 9 9 * value instead of partially decayed timer value 10 10 * 11 11 */ 12 - #define _GNU_SOURCE /* for program_invocation_short_name */ 13 12 #include <fcntl.h> 14 13 #include <stdio.h> 15 14 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/xapic_ipi_test.c
··· 19 19 * Migration is a command line option. When used on non-numa machines will 20 20 * exit with error. Test is still usefull on non-numa for testing IPIs. 21 21 */ 22 - 23 - #define _GNU_SOURCE /* for program_invocation_short_name */ 24 22 #include <getopt.h> 25 23 #include <pthread.h> 26 24 #include <inttypes.h>
-1
tools/testing/selftests/kvm/x86_64/xapic_state_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #define _GNU_SOURCE /* for program_invocation_short_name */ 3 2 #include <fcntl.h> 4 3 #include <stdio.h> 5 4 #include <stdlib.h>
-2
tools/testing/selftests/kvm/x86_64/xss_msr_test.c
··· 4 4 * 5 5 * Tests for the IA32_XSS MSR. 6 6 */ 7 - 8 - #define _GNU_SOURCE /* for program_invocation_short_name */ 9 7 #include <sys/ioctl.h> 10 8 11 9 #include "test_util.h"