about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-01-31 15:56:53 +0000
committerbors <bors@rust-lang.org>2025-01-31 15:56:53 +0000
commit9c8fe3078e26f95524e4f032e3f46591740a1fc5 (patch)
tree2e84c6d5a194bd3a1b695910c68eae9f5c8379b1
parent73993387fda0418261f19435fd65284f7db05822 (diff)
parent27454db7d156cf8b99d6d59145ae2a1b92368a49 (diff)
downloadrust-9c8fe3078e26f95524e4f032e3f46591740a1fc5.tar.gz
rust-9c8fe3078e26f95524e4f032e3f46591740a1fc5.zip
Auto merge of #134424 - 1c3t3a:null-checks, r=saethlin
Insert null checks for pointer dereferences when debug assertions are enabled

Similar to how the alignment is already checked, this adds a check
for null pointer dereferences in debug mode. It is implemented similarly
to the alignment check as a `MirPass`.

This inserts checks in the same places as the `CheckAlignment` pass and additionally
also inserts checks for `Borrows`, so code like
```rust
let ptr: *const u32 = std::ptr::null();
let val: &u32 = unsafe { &*ptr };
```
will have a check inserted on dereference. This is done because null references
are UB. The alignment check doesn't cover these places, because in `&(*ptr).field`,
the exact requirement is that the final reference must be aligned. This is something to
consider further enhancements of the alignment check.

For now this is implemented as a separate `MirPass`, to make it easy to disable
this check if necessary.

This is related to a 2025H1 project goal for better UB checks in debug
mode: https://github.com/rust-lang/rust-project-goals/pull/177.

r? `@saethlin`
-rw-r--r--src/driver.rs2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/driver.rs b/src/driver.rs
index 8201f332d33..c548f262a92 100644
--- a/src/driver.rs
+++ b/src/driver.rs
@@ -166,6 +166,8 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
         // MIR passes can be enabled / disabled separately, we should figure out, what passes to
         // use for Clippy.
         config.opts.unstable_opts.mir_opt_level = Some(0);
+        config.opts.unstable_opts.mir_enable_passes =
+            vec![("CheckNull".to_owned(), false), ("CheckAlignment".to_owned(), false)];
 
         // Disable flattening and inlining of format_args!(), so the HIR matches with the AST.
         config.opts.unstable_opts.flatten_format_args = false;