Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongkeunahn committed Oct 1, 2023
1 parent 33d0be6 commit 68af465
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Check

on:
- push
- pull_request

env:
CARGO_NET_RETRY: 10
Expand All @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions src/platform/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ impl<const N: usize> Reader<N> {
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;
Expand Down Expand Up @@ -132,7 +130,7 @@ impl<const N: usize> Reader<N> {
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; }
Expand Down
2 changes: 1 addition & 1 deletion src/platform/loader/amd64_pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!() }
Expand Down
67 changes: 31 additions & 36 deletions src/platform/malloc/dlmalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
}

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 {
Expand All @@ -216,7 +216,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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
}
}

Expand Down Expand Up @@ -404,7 +404,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
} 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 {
Expand Down Expand Up @@ -433,7 +433,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
return ret;
}

return ptr::null_mut();
ptr::null_mut()
}

pub unsafe fn realloc(&mut self, oldmem: *mut u8, bytes: usize) -> *mut u8 {
Expand All @@ -453,7 +453,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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 {
Expand Down Expand Up @@ -555,7 +555,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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();
Expand All @@ -564,7 +564,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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 {
Expand Down Expand Up @@ -598,7 +598,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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;
Expand Down Expand Up @@ -633,7 +633,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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
Expand Down Expand Up @@ -757,7 +757,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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
Expand All @@ -772,8 +772,8 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
let offset = ssize + mem::size_of::<usize>() * 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
Expand Down Expand Up @@ -924,7 +924,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
}

// 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();
}

Expand Down Expand Up @@ -1031,7 +1031,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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::<usize>() * 8 - 1) & 1];
let c = &mut (*t).child[(k >> (mem::size_of::<usize>() * 8 - 1)) & 1];
k <<= 1;
if !c.is_null() {
t = *c;
Expand Down Expand Up @@ -1143,12 +1143,10 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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() {
Expand Down Expand Up @@ -1260,17 +1258,14 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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;
}
}

Expand Down Expand Up @@ -1322,8 +1317,8 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
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));
Expand Down Expand Up @@ -1353,7 +1348,7 @@ impl<A: DlmallocAllocator> Dlmalloc<A> {
} else {
MAX_RELEASE_CHECK_RATE
};
return released;
released
}

// Sanity checks
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/platform/malloc/dlmalloc_linux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use super::dlmalloc_interface::DlmallocAllocator;
use super::super::os::linux::syscall;

Expand All @@ -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,
Expand All @@ -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)
}
}

Expand All @@ -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
}
}

Expand All @@ -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()
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/platform/malloc/dlmalloc_windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]

use core::ptr;
use super::dlmalloc_interface::DlmallocAllocator;

Expand Down
2 changes: 1 addition & 1 deletion src/platform/os/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
25 changes: 12 additions & 13 deletions src/platform/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/platform/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 68af465

Please sign in to comment.