rust/tests/ui/lint/invalid_null_args.stderr
Luigi Sartor Piucco 8a8717e971
fix: don't panic on volatile access to null
According to
https://discourse.llvm.org/t/rfc-volatile-access-to-non-dereferenceable-memory-may-be-well-defined/86303/4,
LLVM allows volatile operations on null and handles it correctly. This
should be allowed in Rust as well, because I/O memory may be hard-coded
to address 0 in some cases, like the AVR chip ATtiny1626.

A test case that ensured a failure when passing null to volatile was
removed, since it's now valid.

Due to the addition of `maybe_is_aligned` to `ub_checks`,
`maybe_is_aligned_and_not_null` was refactored to use it.

docs: revise restrictions on volatile operations

A distinction between usage on Rust memory vs. non-Rust memory was
introduced. Documentation was reworded to explain what that means, and
make explicit that:

- No trapping can occur from volatile operations;
- On Rust memory, all safety rules must be respected;
- On Rust memory, the primary difference from regular access is that
  volatile always involves a memory dereference;
- On Rust memory, the only data affected by an operation is the one
  pointed to in the argument(s) of the function;
- On Rust memory, provenance follows the same rules as non-volatile
  access;
- On non-Rust memory, any address known to not contain Rust memory is
  valid (including 0 and usize::MAX);
- On non-Rust memory, no Rust memory may be affected (it is implicit
  that any other non-Rust memory may be affected, though, even if not
  referenced by the pointer). This should be relevant when, for example,
  reading register A causes a flag to change in register B, or writing
  to A causes B to change in some way. Everything affected mustn't be
  inside an allocation.
- On non-Rust memory, provenance is irrelevant and a pointer with none
  can be used in a valid way.

fix: don't lint null as UB for volatile

Also remove a now-unneeded `allow` line.

fix: additional wording nits
2025-07-18 13:41:34 -03:00

301 lines
16 KiB
Plaintext

error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:8:5
|
LL | / ptr::write(
LL | |
LL | | ptr::null_mut() as *mut u32,
| | --------------- null pointer originates from here
LL | | mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
LL | | );
| |_____^
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
= note: `#[deny(invalid_null_arguments)]` on by default
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:15:5
|
LL | / ptr::write(
LL | |
LL | | null_ptr as *mut u32,
LL | | mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
LL | | );
| |_____^
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
note: null pointer originates from here
--> $DIR/invalid_null_args.rs:14:20
|
LL | let null_ptr = ptr::null_mut();
| ^^^^^^^^^^^^^^^
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:21:23
|
LL | let _: &[usize] = std::slice::from_raw_parts(ptr::null(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:23:23
|
LL | let _: &[usize] = std::slice::from_raw_parts(ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:25:23
|
LL | let _: &[usize] = std::slice::from_raw_parts(0 as *mut _, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-^^^^^^^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:27:23
|
LL | let _: &[usize] = std::slice::from_raw_parts(mem::transmute(0usize), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:30:23
|
LL | let _: &[usize] = std::slice::from_raw_parts_mut(ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:33:5
|
LL | ptr::copy::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0);
| ^^^^^^^^^^^^^^^^^^^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:35:5
|
LL | ptr::copy::<usize>(ptr::NonNull::dangling().as_ptr(), ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:38:5
|
LL | ptr::copy_nonoverlapping::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:40:5
|
LL | / ptr::copy_nonoverlapping::<usize>(
LL | |
LL | | ptr::NonNull::dangling().as_ptr(),
LL | | ptr::null_mut(),
| | --------------- null pointer originates from here
LL | | 0,
LL | | );
| |_____^
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:51:17
|
LL | let _a: A = ptr::read(ptr::null());
| ^^^^^^^^^^-----------^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:53:17
|
LL | let _a: A = ptr::read(ptr::null_mut());
| ^^^^^^^^^^---------------^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:56:17
|
LL | let _a: A = ptr::read_unaligned(ptr::null());
| ^^^^^^^^^^^^^^^^^^^^-----------^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:58:17
|
LL | let _a: A = ptr::read_unaligned(ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^---------------^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:65:17
|
LL | let _a: A = ptr::replace(ptr::null_mut(), v);
| ^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:68:5
|
LL | ptr::swap::<A>(ptr::null_mut(), &mut v);
| ^^^^^^^^^^^^^^^---------------^^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:70:5
|
LL | ptr::swap::<A>(&mut v, ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^^^^---------------^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:73:5
|
LL | ptr::swap_nonoverlapping::<A>(ptr::null_mut(), &mut v, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:75:5
|
LL | ptr::swap_nonoverlapping::<A>(&mut v, ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:78:5
|
LL | ptr::write(ptr::null_mut(), v);
| ^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:81:5
|
LL | ptr::write_unaligned(ptr::null_mut(), v);
| ^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:87:5
|
LL | ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:92:18
|
LL | let _a: u8 = ptr::read(const_ptr);
| ^^^^^^^^^^^^^^^^^^^^
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
note: null pointer originates from here
--> $DIR/invalid_null_args.rs:14:20
|
LL | let null_ptr = ptr::null_mut();
| ^^^^^^^^^^^^^^^
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:99:5
|
LL | std::slice::from_raw_parts::<()>(ptr::null(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:101:5
|
LL | std::slice::from_raw_parts::<Zst>(ptr::null(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:103:5
|
LL | std::slice::from_raw_parts_mut::<()>(ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: calling this function with a null pointer is undefined behavior, even if the result of the function is unused
--> $DIR/invalid_null_args.rs:105:5
|
LL | std::slice::from_raw_parts_mut::<Zst>(ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------^^^^
| |
| null pointer originates from here
|
= help: for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
error: aborting due to 28 previous errors