diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 924a13c8..2483019b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,6 +2,7 @@ name: Check on: - push + - pull_request env: CARGO_NET_RETRY: 10 @@ -28,7 +29,7 @@ jobs: - name: Clippy run: cargo clippy env: - RUSTFLAGS: "-D warnings" + RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc" - name: Test run: cargo test --lib -- --test-threads 1 env: diff --git a/src/platform/io/reader.rs b/src/platform/io/reader.rs index b8a55e74..5849ffaa 100644 --- a/src/platform/io/reader.rs +++ b/src/platform/io/reader.rs @@ -85,9 +85,7 @@ impl Reader { pub fn try_consume(&mut self, bytes: usize) -> usize { let mut consumed = 0; while consumed < bytes { - if self.off == self.len { - if self.try_refill(1) == 0 { break; } - } + if self.off == self.len && self.try_refill(1) == 0 { break; } let delta = core::cmp::min(self.len - self.off, bytes - consumed); self.off += delta; consumed -= delta; @@ -132,7 +130,7 @@ impl Reader { self.off += i + 1; break total + i; } else { - unsafe { buf.as_mut_vec() }.extend_from_slice(&range); + unsafe { buf.as_mut_vec() }.extend_from_slice(range); self.off = self.len; total += len; if self.try_refill(1) == 0 { break total; } diff --git a/src/platform/loader/amd64_pe.rs b/src/platform/loader/amd64_pe.rs index 098fe494..dd9550f8 100644 --- a/src/platform/loader/amd64_pe.rs +++ b/src/platform/loader/amd64_pe.rs @@ -58,7 +58,7 @@ pub unsafe extern "sysv64" fn relocate( ptr::write(patch_addr as *mut u16, (tmp >> 16) as u16); }, IMAGE_REL_BASED_DIR64 => { - ptr::write(patch_addr as *mut u64, ptr::read(patch_addr as *const u64) + reloc_delta as u64); + ptr::write(patch_addr as *mut u64, ptr::read(patch_addr as *const u64) + reloc_delta); }, IMAGE_REL_BASED_ABSOLUTE => (), _ => { unreachable!() } diff --git a/src/platform/malloc/dlmalloc.rs b/src/platform/malloc/dlmalloc.rs index d06324db..825a0777 100644 --- a/src/platform/malloc/dlmalloc.rs +++ b/src/platform/malloc/dlmalloc.rs @@ -200,7 +200,7 @@ impl Dlmalloc { } fn align_offset_usize(&self, addr: usize) -> usize { - align_up(addr, self.malloc_alignment()) - (addr as usize) + align_up(addr, self.malloc_alignment()) - addr } fn top_foot_size(&self) -> usize { @@ -216,7 +216,7 @@ impl Dlmalloc { fn align_as_chunk(&self, ptr: *mut u8) -> *mut Chunk { unsafe { let chunk = Chunk::to_mem(ptr as *mut Chunk); - ptr.offset(self.align_offset(chunk) as isize) as *mut Chunk + ptr.add(self.align_offset(chunk)) as *mut Chunk } } @@ -404,7 +404,7 @@ impl Dlmalloc { } else { self.least_addr = cmp::min(tbase, self.least_addr); let mut sp = &mut self.seg as *mut Segment; - while !sp.is_null() && (*sp).base != tbase.offset(tsize as isize) { + while !sp.is_null() && (*sp).base != tbase.add(tsize) { sp = (*sp).next; } if !sp.is_null() && !Segment::is_extern(sp) && Segment::sys_flags(sp) == flags { @@ -433,7 +433,7 @@ impl Dlmalloc { return ret; } - return ptr::null_mut(); + ptr::null_mut() } pub unsafe fn realloc(&mut self, oldmem: *mut u8, bytes: usize) -> *mut u8 { @@ -453,7 +453,7 @@ impl Dlmalloc { ptr::copy_nonoverlapping(oldmem, ptr, cmp::min(oc, bytes)); self.free(oldmem); } - return ptr; + ptr } unsafe fn try_realloc_chunk(&mut self, p: *mut Chunk, nb: usize, can_move: bool) -> *mut Chunk { @@ -555,7 +555,7 @@ impl Dlmalloc { if ptr.is_null() { return ptr::null_mut(); } - let newp = ptr.offset(offset as isize) as *mut Chunk; + let newp = ptr.add(offset) as *mut Chunk; let psize = newmmsize - offset - self.mmap_foot_pad(); (*newp).head = psize; (*Chunk::plus_offset(newp, psize)).head = Chunk::fencepost_head(); @@ -564,7 +564,7 @@ impl Dlmalloc { self.footprint = self.footprint + newmmsize - oldmmsize; self.max_footprint = cmp::max(self.max_footprint, self.footprint); self.check_mmapped_chunk(newp); - return newp; + newp } fn mmap_align(&self, a: usize) -> usize { @@ -598,7 +598,7 @@ impl Dlmalloc { let pos = if (br as usize - p as usize) > self.min_chunk_size() { br as *mut u8 } else { - (br as *mut u8).offset(alignment as isize) + (br as *mut u8).add(alignment) }; let newp = pos as *mut Chunk; let leadsize = pos as usize - p as usize; @@ -633,7 +633,7 @@ impl Dlmalloc { debug_assert!(Chunk::size(p) >= nb); debug_assert_eq!(align_up(mem as usize, alignment), mem as usize); self.check_inuse_chunk(p); - return mem; + mem } // consolidate and bin a chunk, differs from exported versions of free @@ -757,7 +757,7 @@ impl Dlmalloc { let ret = Chunk::to_mem(p); self.check_malloced_chunk(ret, size); self.check_malloc_state(); - return ret; + ret } // add a segment to hold a new noncontiguous region @@ -772,8 +772,8 @@ impl Dlmalloc { let offset = ssize + mem::size_of::() * 4 + self.malloc_alignment() - 1; let rawsp = old_end.offset(-(offset as isize)); let offset = self.align_offset(Chunk::to_mem(rawsp as *mut Chunk)); - let asp = rawsp.offset(offset as isize); - let csp = if asp < old_top.offset(self.min_chunk_size() as isize) { + let asp = rawsp.add(offset); + let csp = if asp < old_top.add(self.min_chunk_size()) { old_top } else { asp @@ -924,7 +924,7 @@ impl Dlmalloc { } // If dv is a better fit, then return null so malloc will use it - if v.is_null() || (self.dvsize >= size && !(rsize < self.dvsize - size)) { + if v.is_null() || (self.dvsize >= size && rsize >= self.dvsize - size) { return ptr::null_mut(); } @@ -1031,7 +1031,7 @@ impl Dlmalloc { let mut k = size << leftshift_for_tree_index(idx); loop { if Chunk::size(TreeChunk::chunk(t)) != size { - let c = &mut (*t).child[(k >> mem::size_of::() * 8 - 1) & 1]; + let c = &mut (*t).child[(k >> (mem::size_of::() * 8 - 1)) & 1]; k <<= 1; if !c.is_null() { t = *c; @@ -1143,12 +1143,10 @@ impl Dlmalloc { if r.is_null() { self.clear_treemap((*chunk).index); } + } else if (*xp).child[0] == chunk { + (*xp).child[0] = r; } else { - if (*xp).child[0] == chunk { - (*xp).child[0] = r; - } else { - (*xp).child[1] = r; - } + (*xp).child[1] = r; } if !r.is_null() { @@ -1260,17 +1258,14 @@ impl Dlmalloc { let sp = self.segment_holding(self.top as *mut u8); debug_assert!(!sp.is_null()); - if !Segment::is_extern(sp) { - if Segment::can_release_part(&self.system_allocator, sp) { - if (*sp).size >= extra && !self.has_segment_link(sp) { - let newsize = (*sp).size - extra; - if self - .system_allocator - .free_part((*sp).base, (*sp).size, newsize) - { - released = extra; - } - } + if !Segment::is_extern(sp) && + Segment::can_release_part(&self.system_allocator, sp) && + (*sp).size >= extra && !self.has_segment_link(sp) { + let newsize = (*sp).size - extra; + if self + .system_allocator + .free_part((*sp).base, (*sp).size, newsize) { + released = extra; } } @@ -1322,8 +1317,8 @@ impl Dlmalloc { let psize = Chunk::size(p); // We can unmap if the first chunk holds the entire segment and // isn't pinned. - let chunk_top = (p as *mut u8).offset(psize as isize); - let top = base.offset((size - self.top_foot_size()) as isize); + let chunk_top = (p as *mut u8).add(psize); + let top = base.add(size - self.top_foot_size()); if !Chunk::inuse(p) && chunk_top >= top { let tp = p as *mut TreeChunk; debug_assert!(Segment::holds(sp, sp as *mut u8)); @@ -1353,7 +1348,7 @@ impl Dlmalloc { } else { MAX_RELEASE_CHECK_RATE }; - return released; + released } // Sanity checks @@ -1663,7 +1658,7 @@ impl Chunk { } unsafe fn next(me: *mut Chunk) -> *mut Chunk { - (me as *mut u8).offset(((*me).head & !FLAG_BITS) as isize) as *mut Chunk + (me as *mut u8).add((*me).head & !FLAG_BITS) as *mut Chunk } unsafe fn prev(me: *mut Chunk) -> *mut Chunk { @@ -1722,7 +1717,7 @@ impl Chunk { } unsafe fn plus_offset(me: *mut Chunk, offset: usize) -> *mut Chunk { - (me as *mut u8).offset(offset as isize) as *mut Chunk + (me as *mut u8).add(offset) as *mut Chunk } unsafe fn minus_offset(me: *mut Chunk, offset: usize) -> *mut Chunk { @@ -1785,7 +1780,7 @@ impl Segment { } unsafe fn top(seg: *mut Segment) -> *mut u8 { - (*seg).base.offset((*seg).size as isize) + (*seg).base.add((*seg).size) } } diff --git a/src/platform/malloc/dlmalloc_linux.rs b/src/platform/malloc/dlmalloc_linux.rs index 19d9c763..5909a90a 100644 --- a/src/platform/malloc/dlmalloc_linux.rs +++ b/src/platform/malloc/dlmalloc_linux.rs @@ -1,3 +1,5 @@ +#![allow(clippy::not_unsafe_ptr_arg_deref)] + use super::dlmalloc_interface::DlmallocAllocator; use super::super::os::linux::syscall; @@ -16,7 +18,7 @@ unsafe impl DlmallocAllocator for System { fn alloc(&self, size: usize) -> (*mut u8, usize, u32) { let addr = unsafe { syscall::mmap( - 0 as *mut _, + core::ptr::null_mut(), size, syscall::PROT_WRITE | syscall::PROT_READ, syscall::MAP_ANON | syscall::MAP_PRIVATE, @@ -27,7 +29,7 @@ unsafe impl DlmallocAllocator for System { if core::ptr::eq(addr, syscall::MAP_FAILED) { (core::ptr::null_mut(), 0, 0) } else { - (addr as *mut u8, size, 0) + (addr, size, 0) } } @@ -37,7 +39,7 @@ unsafe impl DlmallocAllocator for System { if core::ptr::eq(ptr, syscall::MAP_FAILED) { core::ptr::null_mut() } else { - ptr as *mut u8 + ptr } } @@ -47,7 +49,7 @@ unsafe impl DlmallocAllocator for System { if !core::ptr::eq(rc, syscall::MAP_FAILED) { return true; } - syscall::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize).is_null() + syscall::munmap(ptr.add(newsize) as *mut _, oldsize - newsize).is_null() } } diff --git a/src/platform/malloc/dlmalloc_windows.rs b/src/platform/malloc/dlmalloc_windows.rs index 85e1d871..86f617f0 100644 --- a/src/platform/malloc/dlmalloc_windows.rs +++ b/src/platform/malloc/dlmalloc_windows.rs @@ -1,3 +1,5 @@ +#![allow(clippy::not_unsafe_ptr_arg_deref)] + use core::ptr; use super::dlmalloc_interface::DlmallocAllocator; diff --git a/src/platform/os/linux.rs b/src/platform/os/linux.rs index 5fa1ef41..37e8079b 100644 --- a/src/platform/os/linux.rs +++ b/src/platform/os/linux.rs @@ -9,7 +9,7 @@ pub mod syscall { pub const MAP_PRIVATE: i32 = 0x02; pub const MAP_ANON: i32 = 0x20; pub const MREMAP_MAYMOVE: i32 = 0x01; - pub const MAP_FAILED: *mut u8 = unsafe { core::mem::transmute(usize::MAX) }; + pub const MAP_FAILED: *mut u8 = usize::MAX as *mut u8; pub const RLIMIT_STACK: usize = 3; #[cfg(target_arch = "x86_64")] diff --git a/src/platform/os/windows.rs b/src/platform/os/windows.rs index 794eb648..4c948741 100644 --- a/src/platform/os/windows.rs +++ b/src/platform/os/windows.rs @@ -185,12 +185,11 @@ mod services_override { bytes_read as usize }} basm_abi!{pub unsafe fn svc_write_stdio(fd: usize, buf: *const u8, count: usize) -> usize { - let handle; - match fd { - 1 => { handle = WINAPI.GetStdHandle(WinApi::STD_OUTPUT_HANDLE); }, - 2 => { handle = WINAPI.GetStdHandle(WinApi::STD_ERROR_HANDLE); }, + let handle = match fd { + 1 => { WINAPI.GetStdHandle(WinApi::STD_OUTPUT_HANDLE) }, + 2 => { WINAPI.GetStdHandle(WinApi::STD_ERROR_HANDLE) }, _ => { return 0; } - } + }; let mut bytes_written: u32 = 0; let mut ov: Overlapped = Default::default(); ov.set_off(WINAPI.io_off[fd]); @@ -214,14 +213,14 @@ pub unsafe fn init() { WINAPI.ptr_GetModuleHandleW = Some(core::mem::transmute((*pd).win_GetModuleHandleW as usize)); WINAPI.ptr_GetProcAddress = Some(core::mem::transmute((*pd).win_GetProcAddress as usize)); WINAPI.kernel32 = WINAPI.GetModuleHandleW(WinApi::KERNEL32DLL.as_ptr()); - WINAPI.ptr_VirtualAlloc = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"VirtualAlloc\0".as_ptr() as *const u8))); - WINAPI.ptr_VirtualFree = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"VirtualFree\0".as_ptr() as *const u8))); - WINAPI.ptr_ExitProcess = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"ExitProcess\0".as_ptr() as *const u8))); - WINAPI.ptr_GetStdHandle = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetStdHandle\0".as_ptr() as *const u8))); - WINAPI.ptr_ReadFile = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"ReadFile\0".as_ptr() as *const u8))); - WINAPI.ptr_WriteFile = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"WriteFile\0".as_ptr() as *const u8))); - WINAPI.ptr_GetOverlappedResult = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetOverlappedResult\0".as_ptr() as *const u8))); - WINAPI.ptr_GetLastError = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetLastError\0".as_ptr() as *const u8))); + WINAPI.ptr_VirtualAlloc = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"VirtualAlloc\0".as_ptr()))); + WINAPI.ptr_VirtualFree = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"VirtualFree\0".as_ptr()))); + WINAPI.ptr_ExitProcess = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"ExitProcess\0".as_ptr()))); + WINAPI.ptr_GetStdHandle = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetStdHandle\0".as_ptr()))); + WINAPI.ptr_ReadFile = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"ReadFile\0".as_ptr()))); + WINAPI.ptr_WriteFile = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"WriteFile\0".as_ptr()))); + WINAPI.ptr_GetOverlappedResult = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetOverlappedResult\0".as_ptr()))); + WINAPI.ptr_GetLastError = Some(core::mem::transmute(WINAPI.GetProcAddress(WINAPI.kernel32, b"GetLastError\0".as_ptr()))); allocator::install_malloc_impl( dlmalloc_alloc, diff --git a/src/platform/services.rs b/src/platform/services.rs index d65fb603..b8e01f97 100644 --- a/src/platform/services.rs +++ b/src/platform/services.rs @@ -97,6 +97,6 @@ pub fn write_stdio(fd: usize, buf: &[u8]) -> usize { #[inline(always)] pub fn platform_data() -> *const PlatformData { unsafe { - core::mem::transmute(addr(9)) + addr(9) as *const PlatformData } } \ No newline at end of file