@@ -36,16 +36,14 @@ impl MachineAlloc {
3636 }
3737
3838 /// SAFETY: There must be no existing `MiriAllocBytes`
39- #[ allow( clippy:: cast_sign_loss) ]
40- #[ allow( clippy:: cast_possible_truncation) ]
4139 pub unsafe fn enable ( ) {
4240 let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
4341 alloc. enabled = true ;
4442 // This needs to specifically be the system pagesize!
4543 alloc. page_size = unsafe {
4644 let ret = libc:: sysconf ( libc:: _SC_PAGE_SIZE) ;
4745 if ret > 0 {
48- ret as _
46+ ret. try_into ( ) . unwrap ( )
4947 } else {
5048 4096 // fallback
5149 }
@@ -107,7 +105,6 @@ impl MachineAlloc {
107105 }
108106
109107 /// SAFETY: See alloc::alloc()
110- #[ allow( clippy:: cast_possible_wrap) ]
111108 unsafe fn alloc_inner ( & mut self , layout : Layout ) -> * mut u8 {
112109 let ( size, align) = MachineAlloc :: normalized_layout ( layout) ;
113110
@@ -122,11 +119,11 @@ impl MachineAlloc {
122119 if pinfo[ idx / 8 ..idx / 8 + size / 8 ] . iter ( ) . all ( |v| * v == 0 ) {
123120 pinfo[ idx / 8 ..idx / 8 + size / 8 ] . fill ( 255 ) ;
124121 unsafe {
125- let ret = page. offset ( idx as isize ) ;
122+ let ret = page. offset ( idx. try_into ( ) . unwrap ( ) ) ;
126123 if ret. addr ( ) >= page. addr ( ) + self . page_size {
127124 panic ! ( "Returing {} from page {}" , ret. addr( ) , page. addr( ) ) ;
128125 }
129- return page. offset ( idx as isize ) ;
126+ return page. offset ( idx. try_into ( ) . unwrap ( ) ) ;
130127 }
131128 }
132129 }
@@ -225,45 +222,47 @@ impl MachineAlloc {
225222 ///
226223 /// SAFETY: Accessing memory after this point will result in a segfault
227224 /// unless it is first unprotected.
228- pub unsafe fn prepare_ffi ( ) -> Option < ( ) > {
225+ pub unsafe fn prepare_ffi ( ) -> Result < ( ) , nix :: errno :: Errno > {
229226 let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
230227 unsafe {
231- alloc. mprotect ( ProtFlags :: PROT_NONE ) . unwrap ( ) ;
228+ alloc. mprotect ( ProtFlags :: PROT_NONE ) ? ;
232229 }
233230 alloc. ffi_mode = true ;
234- Some ( ( ) )
231+ Ok ( ( ) )
235232 }
236233
237- /// Deprotects all owned memory by setting it to RW.
238- pub fn unprep_ffi ( ) -> Option < ( ) > {
234+ /// Deprotects all owned memory by setting it to RW. Erroring here is very
235+ /// likely unrecoverable, so it may panic if applying those permissions
236+ /// fails.
237+ pub fn unprep_ffi ( ) {
239238 let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
240239 let default_flags = ProtFlags :: PROT_READ | ProtFlags :: PROT_WRITE ;
241240 unsafe {
242- alloc. mprotect ( default_flags) ? ;
241+ alloc. mprotect ( default_flags) . unwrap ( ) ;
243242 }
244243 alloc. ffi_mode = false ;
245- Some ( ( ) )
246244 }
247245
248246 /// Applies `prot` to every page managed by the allocator.
249247 ///
250248 /// SAFETY: Accessing memory in violation of the protection flags will
251249 /// trigger a segfault.
252- unsafe fn mprotect ( & mut self , prot : ProtFlags ) -> Option < ( ) > {
250+ unsafe fn mprotect ( & mut self , prot : ProtFlags ) -> Result < ( ) , nix :: errno :: Errno > {
253251 for & pg in & self . pages {
254252 unsafe {
253+ // We already know only non-null ptrs are pushed to self.pages
255254 let addr: std:: ptr:: NonNull < std:: ffi:: c_void > =
256- std:: ptr:: NonNull :: new_unchecked ( pg as * mut _ ) ;
257- nix:: sys:: mman:: mprotect ( addr, self . page_size , prot) . ok ( ) ?;
255+ std:: ptr:: NonNull :: new_unchecked ( pg. cast ( ) ) ;
256+ nix:: sys:: mman:: mprotect ( addr, self . page_size , prot) ?;
258257 }
259258 }
260259 for & ( hpg, size) in & self . huge_allocs {
261260 unsafe {
262- let addr = std:: ptr:: NonNull :: new_unchecked ( hpg as * mut _ ) ;
263- nix:: sys:: mman:: mprotect ( addr, size, prot) . ok ( ) ?;
261+ let addr = std:: ptr:: NonNull :: new_unchecked ( hpg. cast ( ) ) ;
262+ nix:: sys:: mman:: mprotect ( addr, size, prot) ?;
264263 }
265264 }
266- Some ( ( ) )
265+ Ok ( ( ) )
267266 }
268267}
269268
0 commit comments