Author: eleviant
Date: 2026-05-06T20:00:43+02:00
New Revision: a4ddeba8daff36c8e6285fa3603185b4c9b41718

URL: 
https://github.com/llvm/llvm-project/commit/a4ddeba8daff36c8e6285fa3603185b4c9b41718
DIFF: 
https://github.com/llvm/llvm-project/commit/a4ddeba8daff36c8e6285fa3603185b4c9b41718.diff

LOG: [clang] Don't omit null pointer checks with -fms-kernel (#193800)

In kernel space, a null (zero) address may be valid, so treating it as "always 
invalid" and bypassing null checks is not correct. With -fms-kernel, we 
override the default behavior and disable assumptions about null pointers. 
However, -fdelete-null-pointer-checks can still be used to re-enable these 
optimizations.

Added: 
    clang/test/CodeGen/MSKernel/null-deref.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Options/Options.td

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c2323b58a3e06..4de2b90cbabe5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -291,6 +291,8 @@ Modified Compiler Flags
 - The `-mno-outline` and `-moutline` compiler flags are now allowed on RISC-V 
and X86, which both support the machine outliner.
 - The `-mno-outline` flag will now add the `nooutline` IR attribute, so that
   `-mno-outline` and `-moutline` objects can be mixed correctly during LTO.
+- The `-fms-kernel` flag will now implicitly add 
-fno-delete-null-pointer-checks.
+  Still -fdelete-null-pointer-checks can be used to override this behavior.
 
 Removed Compiler Flags
 ----------------------

diff  --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index e21ea8a6529a1..863e6b3d0871f 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -3037,10 +3037,10 @@ defm rewrite_includes : BoolFOption<"rewrite-includes",
 defm directives_only : OptInCC1FFlag<"directives-only", "">;
 
 defm delete_null_pointer_checks : BoolFOption<"delete-null-pointer-checks",
-  CodeGenOpts<"NullPointerIsValid">, DefaultFalse,
+  CodeGenOpts<"NullPointerIsValid">, Default<"LangOpts->Kernel">,
   NegFlag<SetTrue, [], [ClangOption, CC1Option],
           "Do not treat usage of null pointers as undefined behavior">,
-  PosFlag<SetFalse, [], [ClangOption], "Treat usage of null pointers as 
undefined behavior (default)">,
+  PosFlag<SetFalse, [], [ClangOption, CC1Option], "Treat usage of null 
pointers as undefined behavior (default)">,
   BothFlags<[], [ClangOption, CLOption]>>,
   DocBrief<[{When enabled, treat null pointer dereference, creation of a 
reference to null,
 or passing a null pointer to a function parameter annotated with the "nonnull"

diff  --git a/clang/test/CodeGen/MSKernel/null-deref.c 
b/clang/test/CodeGen/MSKernel/null-deref.c
new file mode 100644
index 0000000000000..f23409115f50d
--- /dev/null
+++ b/clang/test/CodeGen/MSKernel/null-deref.c
@@ -0,0 +1,16 @@
+// Check that null pointer checks are not omited in kernel mode compilations
+// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-pc-windows-msvc 
%s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-pc-windows-msvc 
-fdelete-null-pointer-checks %s -emit-llvm -o - | FileCheck %s 
--check-prefix=NOCHECK
+
+// CHECK: define dso_local i32 @process(ptr noundef %p) #0
+// CHECK: attributes #0 = {{.*}} null_pointer_is_valid
+// NOCHECK-NOT: null_pointer_is_valid
+
+struct Obj { int value; int extra; };
+
+int process(struct Obj* p) {
+    int v = p->value;
+    if (!p)
+        return -1;
+    return v + p->extra;
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to