|
14 | 14 | //! munmap shim which would partily unmap a region of address space previously mapped by mmap will
|
15 | 15 | //! report UB.
|
16 | 16 |
|
17 |
| -use crate::{helpers::round_to_next_multiple_of, *}; |
| 17 | +use crate::*; |
18 | 18 | use rustc_target::abi::Size;
|
19 | 19 |
|
20 | 20 | impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
|
@@ -96,7 +96,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
96 | 96 | }
|
97 | 97 |
|
98 | 98 | let align = this.machine.page_align();
|
99 |
| - let map_length = round_to_next_multiple_of(length, this.machine.page_size); |
| 99 | + let Some(map_length) = length.checked_next_multiple_of(this.machine.page_size) else { |
| 100 | + this.set_last_error(Scalar::from_i32(this.eval_libc_i32("EINVAL")))?; |
| 101 | + return Ok(this.eval_libc("MAP_FAILED")); |
| 102 | + }; |
| 103 | + if map_length > this.target_usize_max() { |
| 104 | + this.set_last_error(Scalar::from_i32(this.eval_libc_i32("EINVAL")))?; |
| 105 | + return Ok(this.eval_libc("MAP_FAILED")); |
| 106 | + } |
100 | 107 |
|
101 | 108 | let ptr =
|
102 | 109 | this.allocate_ptr(Size::from_bytes(map_length), align, MiriMemoryKind::Mmap.into())?;
|
@@ -129,7 +136,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
|
129 | 136 | return Ok(Scalar::from_i32(-1));
|
130 | 137 | }
|
131 | 138 |
|
132 |
| - let length = Size::from_bytes(round_to_next_multiple_of(length, this.machine.page_size)); |
| 139 | + let Some(length) = length.checked_next_multiple_of(this.machine.page_size) else { |
| 140 | + this.set_last_error(Scalar::from_i32(this.eval_libc_i32("EINVAL")))?; |
| 141 | + return Ok(Scalar::from_i32(-1)); |
| 142 | + }; |
| 143 | + if length > this.target_usize_max() { |
| 144 | + this.set_last_error(Scalar::from_i32(this.eval_libc_i32("EINVAL")))?; |
| 145 | + return Ok(this.eval_libc("MAP_FAILED")); |
| 146 | + } |
| 147 | + |
| 148 | + let length = Size::from_bytes(length); |
133 | 149 | this.deallocate_ptr(
|
134 | 150 | addr,
|
135 | 151 | Some((length, this.machine.page_align())),
|
|
0 commit comments