https://github.com/eleviant created https://github.com/llvm/llvm-project/pull/196283
Patch tests that memcpy from nullptr is not treated as UB and is not optimized out when -fms-kernel is provided, because null pointer is a valid address in kernel space. >From fc3ea0f3a413b322978545e841c31b9fa319acad Mon Sep 17 00:00:00 2001 From: Evgeny Leviant <[email protected]> Date: Wed, 15 Apr 2026 12:02:58 +0200 Subject: [PATCH] [clang] Add test for memcpy from nullptr. NFC Patch tests that memcpy from nullptr is not treated as UB and is not optimized out when -fms-kernel is provided, because null pointer is a valid address in kernel space. --- clang/test/CodeGen/MSKernel/memcpy0.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 clang/test/CodeGen/MSKernel/memcpy0.c diff --git a/clang/test/CodeGen/MSKernel/memcpy0.c b/clang/test/CodeGen/MSKernel/memcpy0.c new file mode 100644 index 0000000000000..7c31e6b86ec56 --- /dev/null +++ b/clang/test/CodeGen/MSKernel/memcpy0.c @@ -0,0 +1,21 @@ +// REQUIRES: x86-registered-target +// RUN: %clang_cc1 -fms-kernel -triple x86_64-windows-msvc -O2 -emit-llvm %s -o - | FileCheck %s --check-prefix=IR +// RUN: %clang_cc1 -fms-kernel -triple x86_64-windows-msvc -O2 -S %s -o - | FileCheck %s --check-prefix=ASM + +// LLVM selects alignment of 1 << 32 for null pointer, +// which is a maximum allowed value according to https://llvm.org/docs/LangRef.html +// IR: define {{.*}} void @builtin_copy_from_nullptr +// IR-NEXT: entry: +// IR-NEXT: tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %dst, ptr align 4294967296 null, i64 %n, i1 false) +// IR-NEXT: ret void + +// ASM: builtin_copy_from_nullptr: +// ASM-NEXT: # %bb.0: +// ASM-NEXT: movq %rdx, %r8 +// ASM-NEXT: xorl %edx, %edx +// ASM-NEXT: jmp memcpy + +void builtin_copy_from_nullptr(void* dst, long long n) { + __builtin_memcpy(dst, (void*)0, n); +} + _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
