Skip to content

Commit b213f88

Browse files
committed
promote memrchr to work on any platform
1 parent 9a7bc39 commit b213f88

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/shims/foreign_items.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
332332
}
333333
}
334334

335+
"memrchr" => {
336+
let ptr = this.read_scalar(args[0])?.not_undef()?;
337+
let val = this.read_scalar(args[1])?.to_i32()? as u8;
338+
let num = this.read_scalar(args[2])?.to_machine_usize(this)?;
339+
if let Some(idx) = this
340+
.memory
341+
.read_bytes(ptr, Size::from_bytes(num))?
342+
.iter()
343+
.rev()
344+
.position(|&c| c == val)
345+
{
346+
let new_ptr = ptr.ptr_offset(Size::from_bytes(num - idx as u64 - 1), this)?;
347+
this.write_scalar(new_ptr, dest)?;
348+
} else {
349+
this.write_null(dest)?;
350+
}
351+
}
352+
335353
"strlen" => {
336354
let ptr = this.read_scalar(args[0])?.not_undef()?;
337355
let n = this.memory.read_c_str(ptr)?.len();

src/shims/foreign_items/posix.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
157157
}
158158
}
159159

160-
"memrchr" => {
161-
let ptr = this.read_scalar(args[0])?.not_undef()?;
162-
let val = this.read_scalar(args[1])?.to_i32()? as u8;
163-
let num = this.read_scalar(args[2])?.to_machine_usize(this)?;
164-
if let Some(idx) = this
165-
.memory
166-
.read_bytes(ptr, Size::from_bytes(num))?
167-
.iter()
168-
.rev()
169-
.position(|&c| c == val)
170-
{
171-
let new_ptr = ptr.ptr_offset(Size::from_bytes(num - idx as u64 - 1), this)?;
172-
this.write_scalar(new_ptr, dest)?;
173-
} else {
174-
this.write_null(dest)?;
175-
}
176-
}
177-
178160
// Hook pthread calls that go to the thread-local storage memory subsystem.
179161
"pthread_key_create" => {
180162
let key_place = this.deref_operand(args[0])?;

0 commit comments

Comments
 (0)