Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug assertions to write_bytes and copy* #58783

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/book
Submodule book updated 145 files
2 changes: 1 addition & 1 deletion src/doc/edition-guide
2 changes: 1 addition & 1 deletion src/doc/nomicon
2 changes: 1 addition & 1 deletion src/doc/reference
2 changes: 1 addition & 1 deletion src/doc/rust-by-example
Submodule rust-by-example updated 45 files
+1 −1 CONTRIBUTING.md
+5 −8 README.md
+0 −1 src/SUMMARY.md
+1 −1 src/cargo/conventions.md
+1 −1 src/cargo/test.md
+8 −12 src/conversion/string.md
+2 −2 src/custom_types/enum/enum_use.md
+1 −1 src/custom_types/enum/testcase_linked_list.md
+1 −1 src/error.md
+7 −8 src/error/multiple_error_types/boxing_errors.md
+14 −11 src/error/multiple_error_types/define_error_type.md
+9 −1 src/error/multiple_error_types/wrap_error.md
+4 −4 src/error/option_unwrap/and_then.md
+5 −2 src/expression.md
+4 −6 src/flow_control/for.md
+1 −1 src/flow_control/if_else.md
+2 −2 src/flow_control/match/destructuring/destructure_pointers.md
+1 −1 src/generics/assoc_items/the_problem.md
+1 −1 src/generics/bounds.md
+2 −2 src/generics/multi_bounds.md
+0 −10 src/generics/new_types.md
+1 −6 src/hello/print.md
+2 −2 src/hello/print/fmt.md
+1 −1 src/hello/print/print_display.md
+1 −1 src/hello/print/print_display/testcase_list.md
+1 −3 src/index.md
+0 −1 src/macros/designators.md
+3 −3 src/macros/dry.md
+0 −3 src/macros/dsl.md
+2 −2 src/mod/super.md
+2 −2 src/mod/use.md
+3 −14 src/mod/visibility.md
+1 −1 src/primitives/literals.md
+1 −3 src/scope/borrow.md
+1 −1 src/scope/lifetime/elision.md
+0 −33 src/scope/lifetime/trait.md
+12 −8 src/std_misc/file/create.md
+2 −2 src/std_misc/threads/testcase_mapreduce.md
+1 −1 src/testing.md
+1 −1 src/testing/doc_testing.md
+2 −2 src/trait/derive.md
+1 −1 src/trait/iter.md
+1 −1 src/trait/ops.md
+4 −1 src/types/inference.md
+1 −1 src/unsafe.md
2 changes: 1 addition & 1 deletion src/doc/rustc-guide
28 changes: 28 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
since = "1.18.0")]
pub use crate::ptr::drop_in_place;

use crate::mem;

extern "rust-intrinsic" {
// N.B., these intrinsics take raw pointers because they mutate aliased
// memory, which is not valid for either `&` or `&mut`.
Expand Down Expand Up @@ -1323,6 +1325,26 @@ mod real_intrinsics {
}
}

/// Checks whether `ptr` is properly aligned with respect to
/// `align_of::<T>()`.
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
return !ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0;
}

/// Checks whether the regions of memory starting at `src` and `dst` of size
/// `count * size_of::<T>()` overlap.
fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
let src_usize = src as usize;
let dst_usize = dst as usize;
let size = mem::size_of::<T>().checked_mul(count).unwrap();
let diff = if src_usize > dst_usize {
src_usize - dst_usize
} else {
dst_usize - src_usize
};
size > diff
}

/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
/// and destination must *not* overlap.
///
Expand Down Expand Up @@ -1409,6 +1431,9 @@ mod real_intrinsics {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
real_intrinsics::copy_nonoverlapping(src, dst, count);
}

Expand Down Expand Up @@ -1466,6 +1491,8 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
real_intrinsics::copy(src, dst, count)
}

Expand Down Expand Up @@ -1544,5 +1571,6 @@ pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
real_intrinsics::write_bytes(dst, val, count)
}
5 changes: 3 additions & 2 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::cmp::Ordering::{self, Less, Equal, Greater};
use crate::cmp;
use crate::fmt;
use crate::intrinsics::assume;
use crate::intrinsics::is_aligned_and_not_null;
use crate::isize;
use crate::iter::*;
use crate::ops::{FnMut, Try, self};
Expand Down Expand Up @@ -5084,7 +5085,7 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
"attempt to create slice covering half the address space");
Repr { raw: FatPtr { data, len } }.rust
Expand All @@ -5105,7 +5106,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
"attempt to create slice covering half the address space");
Repr { raw: FatPtr { data, len } }.rust_mut
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,9 @@ extern "C" {
pub fn LLVMRustArchiveIteratorFree(AIR: &'a mut ArchiveIterator<'a>);
pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);

pub fn LLVMRustGetSectionName(SI: &SectionIterator<'_>, data: &mut *const c_char) -> size_t;
#[allow(improper_ctypes)]
pub fn LLVMRustGetSectionName(SI: &SectionIterator<'_>,
data: &mut Option<std::ptr::NonNull<c_char>>) -> size_t;

#[allow(improper_ctypes)]
pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
Expand Down
11 changes: 7 additions & 4 deletions src/librustc_codegen_llvm/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_data_structures::owning_ref::OwningRef;
use rustc_codegen_ssa::METADATA_FILENAME;

use std::path::Path;
use std::ptr;
use std::slice;
use rustc_fs_util::path_to_c_string;

Expand Down Expand Up @@ -67,10 +66,14 @@ fn search_meta_section<'a>(of: &'a ObjectFile,
unsafe {
let si = mk_section_iter(of.llof);
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
let mut name_buf = ptr::null();
let mut name_buf = None;
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
let name = slice::from_raw_parts(name_buf as *const u8, name_len as usize).to_vec();
let name = String::from_utf8(name).unwrap();
let name = name_buf.map_or(
"".to_string(),
|buf| String::from_utf8(
slice::from_raw_parts(buf.as_ptr() as *const u8,
name_len as usize)
.to_vec()).unwrap());
debug!("get_metadata_section: name {}", name);
if read_metadata_section_name(target) == name {
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
Expand Down
2 changes: 1 addition & 1 deletion src/llvm-project
Submodule llvm-project updated 68 files
+0 −15 clang-tools-extra/clangd/Threading.cpp
+6 −7 clang-tools-extra/clangd/index/Background.h
+4 −4 clang-tools-extra/clangd/index/BackgroundIndexStorage.cpp
+8 −7 clang-tools-extra/docs/README.txt
+31 −58 clang-tools-extra/docs/ReleaseNotes.rst
+0 −23 clang-tools-extra/docs/_static/clang-tools-extra-styles.css
+0 −3 clang-tools-extra/docs/_templates/layout.html
+161 −1 clang-tools-extra/docs/clangd.rst
+ clang-tools-extra/docs/clangd/ApplyClangTidyFixInVSCode.gif
+ clang-tools-extra/docs/clangd/ApplyFixInVSCode.gif
+ clang-tools-extra/docs/clangd/CodeCompletionInEmacsCompanyMode.png
+ clang-tools-extra/docs/clangd/CodeCompletionInSublimeText.png
+ clang-tools-extra/docs/clangd/CodeCompletionInVSCode.png
+ clang-tools-extra/docs/clangd/CodeCompletionInYCM.png
+ clang-tools-extra/docs/clangd/CodeCompletionInsertsNamespaceQualifiersInVSCode.gif
+0 −29 clang-tools-extra/docs/clangd/DeveloperDocumentation.rst
+ clang-tools-extra/docs/clangd/DiagnosticsInEmacsEglot.png
+ clang-tools-extra/docs/clangd/ErrorsInVSCode.png
+0 −181 clang-tools-extra/docs/clangd/Extensions.rst
+0 −231 clang-tools-extra/docs/clangd/Features.rst
+ clang-tools-extra/docs/clangd/FindAllReferencesInVSCode.gif
+ clang-tools-extra/docs/clangd/FormatSelectionInVSCode.gif
+ clang-tools-extra/docs/clangd/GoToDefinitionInVSCode.gif
+0 −371 clang-tools-extra/docs/clangd/Installation.rst
+ clang-tools-extra/docs/clangd/NavigationWithBreadcrumbsInVSCode.gif
+ clang-tools-extra/docs/clangd/OutlineInVSCode.png
+ clang-tools-extra/docs/clangd/SignatureHelpInVSCode.gif
+0 −27 clang-tools-extra/docs/clangd/index.rst
+1 −1 clang-tools-extra/docs/conf.py
+7 −3 clang-tools-extra/docs/index.rst
+1 −1 clang-tools-extra/test/clangd/background-index.test
+5 −45 clang/docs/AttributeReference.rst
+680 −1,435 clang/docs/DiagnosticsReference.rst
+102 −10 clang/docs/ReleaseNotes.rst
+0 −6 clang/include/clang/AST/APValue.h
+5 −5 clang/include/clang/Basic/AttrDocs.td
+4 −6 clang/include/clang/Basic/TargetInfo.h
+1 −6 clang/include/clang/Driver/CLCompatOptions.td
+0 −20 clang/lib/AST/APValue.cpp
+6 −8 clang/lib/AST/ExprConstant.cpp
+2 −9 clang/lib/CodeGen/CGStmt.cpp
+3 −12 clang/lib/Sema/SemaStmtAsm.cpp
+0 −15 clang/test/CodeGen/x86-64-inline-asm.c
+0 −6 clang/test/Driver/cl-options.c
+1 −23 clang/test/Sema/inline-asm-validate-x86.c
+0 −24 clang/test/SemaCXX/constant-expression-cxx1y.cpp
+0 −8 clang/test/SemaCXX/enable_if.cpp
+4 −1 libcxx/docs/ReleaseNotes.rst
+22 −3 libunwind/CMakeLists.txt
+26 −0 libunwind/include/libunwind.h
+132 −0 libunwind/src/AddressSpace.hpp
+2 −0 libunwind/src/Unwind-EHABI.cpp
+6 −13 libunwind/src/Unwind-seh.cpp
+6 −33 libunwind/src/UnwindCursor.hpp
+90 −7 libunwind/src/libunwind.cpp
+0 −2 llvm/.gitignore
+1 −1 llvm/bindings/go/llvm/ir.go
+43 −70 llvm/docs/ReleaseNotes.rst
+120 −134 llvm/include/llvm/IR/IntrinsicsX86.td
+2 −11 llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+5 −4 llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
+2 −2 llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+2 −5 llvm/lib/Target/X86/X86ISelLowering.cpp
+0 −48 llvm/test/CodeGen/AArch64/win64-jumptable.ll
+0 −40 llvm/test/CodeGen/AArch64/wineh-try-catch-cbz.ll
+2 −2 llvm/test/CodeGen/AArch64/wineh-try-catch.ll
+0 −22 llvm/test/CodeGen/X86/pr40891.ll
+0 −21 llvm/test/Transforms/DeadStoreElimination/X86/gather-null-pointer.ll
2 changes: 1 addition & 1 deletion src/stdsimd
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 143 files
2 changes: 1 addition & 1 deletion src/tools/clippy
2 changes: 1 addition & 1 deletion src/tools/miri
Submodule miri updated 83 files
+10 −20 .appveyor.yml
+5 −15 .travis.yml
+1 −3 Cargo.toml
+30 −55 README.md
+1 −1 benches/helpers/miri_helper.rs
+1 −1 rust-version
+3 −3 src/bin/miri-rustc-tests.rs
+1 −16 src/bin/miri.rs
+36 −153 src/fn_call.rs
+13 −13 src/helpers.rs
+8 −8 src/intrinsic.rs
+80 −77 src/lib.rs
+24 −34 src/operator.rs
+517 −478 src/stacked_borrows.rs
+7 −7 src/tls.rs
+1 −1 test-cargo-miri/run-test.py
+3 −4 test-cargo-miri/test.stdout.ref
+1 −1 test-cargo-miri/test.stdout.ref2
+2 −19 test-cargo-miri/tests/test.rs
+1 −1 tests/compile-fail/deallocate-bad-alignment.rs
+1 −1 tests/compile-fail/deallocate-bad-size.rs
+1 −1 tests/compile-fail/deallocate-twice.rs
+0 −13 tests/compile-fail/getrandom.rs
+3 −1 tests/compile-fail/ptr_eq_integer.rs
+1 −1 tests/compile-fail/reallocate-bad-size.rs
+1 −1 tests/compile-fail/reallocate-change-alloc.rs
+1 −1 tests/compile-fail/reallocate-dangling.rs
+1 −1 tests/compile-fail/stacked_borrows/alias_through_mutation.rs
+1 −1 tests/compile-fail/stacked_borrows/aliasing_mut1.rs
+1 −1 tests/compile-fail/stacked_borrows/aliasing_mut2.rs
+1 −1 tests/compile-fail/stacked_borrows/aliasing_mut3.rs
+1 −1 tests/compile-fail/stacked_borrows/aliasing_mut4.rs
+1 −1 tests/compile-fail/stacked_borrows/box_exclusive_violation1.rs
+1 −1 tests/compile-fail/stacked_borrows/buggy_as_mut_slice.rs
+1 −1 tests/compile-fail/stacked_borrows/buggy_split_at_mut.rs
+1 −1 tests/compile-fail/stacked_borrows/deallocate_against_barrier.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_read1.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_read2.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_read3.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_read4.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_read5.rs
+0 −8 tests/compile-fail/stacked_borrows/illegal_read6.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_write1.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_write2.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_write3.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_write4.rs
+1 −1 tests/compile-fail/stacked_borrows/illegal_write5.rs
+1 −1 tests/compile-fail/stacked_borrows/invalidate_against_barrier1.rs
+1 −1 tests/compile-fail/stacked_borrows/invalidate_against_barrier2.rs
+1 −1 tests/compile-fail/stacked_borrows/load_invalid_mut.rs
+1 −1 tests/compile-fail/stacked_borrows/load_invalid_shr.rs
+1 −1 tests/compile-fail/stacked_borrows/mut_exclusive_violation1.rs
+1 −1 tests/compile-fail/stacked_borrows/outdated_local.rs
+1 −1 tests/compile-fail/stacked_borrows/pass_invalid_mut.rs
+1 −1 tests/compile-fail/stacked_borrows/pass_invalid_shr.rs
+1 −1 tests/compile-fail/stacked_borrows/pointer_smuggling.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_mut.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_mut_option.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_mut_tuple.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_shr.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_shr_option.rs
+1 −1 tests/compile-fail/stacked_borrows/return_invalid_shr_tuple.rs
+0 −14 tests/compile-fail/stacked_borrows/shared_rw_borrows_are_weak1.rs
+0 −14 tests/compile-fail/stacked_borrows/shared_rw_borrows_are_weak2.rs
+5 −2 tests/compile-fail/stacked_borrows/shr_frozen_violation1.rs
+1 −1 tests/compile-fail/stacked_borrows/static_memory_modification.rs
+1 −1 tests/compile-fail/stacked_borrows/transmute-is-no-escape.rs
+1 −1 tests/compile-fail/stacked_borrows/unescaped_local.rs
+19 −1 tests/compiletest.rs
+6 −20 tests/run-pass/2phase.rs
+8 −13 tests/run-pass/async-fn.rs
+0 −26 tests/run-pass/calloc.rs
+0 −5 tests/run-pass/closures.rs
+3 −17 tests/run-pass/hashmap.rs
+1 −1 tests/run-pass/ptr_arith_offset.rs
+1 −1 tests/run-pass/ptr_offset.rs
+0 −45 tests/run-pass/realloc.rs
+45 −1 tests/run-pass/refcell.rs
+2 −2 tests/run-pass/regions-mock-trans.rs
+0 −26 tests/run-pass/stacked-borrows.rs
+0 −59 tests/run-pass/stacked-borrows/interior_mutability.rs
+0 −4 tests/run-pass/vecs.rs
+1 −1 travis.sh
2 changes: 1 addition & 1 deletion src/tools/rls
Submodule rls updated from 20e326 to 6840dd
2 changes: 1 addition & 1 deletion src/tools/rustfmt