Skip to content

Commit e734f12

Browse files
committed
Auto merge of #824 - RalfJung:c_str, r=RalfJung
use Memory::read_c_str, avoid a few to_ptr This is the Miri side of rust-lang/rust#62257
2 parents 226156f + 47bfc62 commit e734f12

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/shims/foreign_items.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
318318

319319
"dlsym" => {
320320
let _handle = this.read_scalar(args[0])?;
321-
let symbol = this.read_scalar(args[1])?.to_ptr()?;
322-
let symbol_name = this.memory().get(symbol.alloc_id)?.read_c_str(tcx, symbol)?;
321+
let symbol = this.read_scalar(args[1])?.not_undef()?;
322+
let symbol_name = this.memory().read_c_str(symbol)?;
323323
let err = format!("bad c unicode symbol: {:?}", symbol_name);
324324
let symbol_name = ::std::str::from_utf8(symbol_name).unwrap_or(&err);
325325
if let Some(dlsym) = Dlsym::from_str(symbol_name)? {
@@ -433,8 +433,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
433433

434434
"getenv" => {
435435
let result = {
436-
let name_ptr = this.read_scalar(args[0])?.to_ptr()?;
437-
let name = this.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
436+
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
437+
let name = this.memory().read_c_str(name_ptr)?;
438438
match this.machine.env_vars.get(name) {
439439
Some(&var) => Scalar::Ptr(var),
440440
None => Scalar::ptr_null(&*this.tcx),
@@ -448,12 +448,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
448448
{
449449
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
450450
if !this.is_null(name_ptr)? {
451-
let name_ptr = name_ptr.to_ptr()?;
452-
let name = this
453-
.memory()
454-
.get(name_ptr.alloc_id)?
455-
.read_c_str(tcx, name_ptr)?
456-
.to_owned();
451+
let name = this.memory().read_c_str(name_ptr)?.to_owned();
457452
if !name.is_empty() && !name.contains(&b'=') {
458453
success = Some(this.machine.env_vars.remove(&name));
459454
}
@@ -473,11 +468,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
473468
let mut new = None;
474469
{
475470
let name_ptr = this.read_scalar(args[0])?.not_undef()?;
476-
let value_ptr = this.read_scalar(args[1])?.to_ptr()?;
477-
let value = this.memory().get(value_ptr.alloc_id)?.read_c_str(tcx, value_ptr)?;
471+
let value_ptr = this.read_scalar(args[1])?.not_undef()?;
472+
let value = this.memory().read_c_str(value_ptr)?;
478473
if !this.is_null(name_ptr)? {
479-
let name_ptr = name_ptr.to_ptr()?;
480-
let name = this.memory().get(name_ptr.alloc_id)?.read_c_str(tcx, name_ptr)?;
474+
let name = this.memory().read_c_str(name_ptr)?;
481475
if !name.is_empty() && !name.contains(&b'=') {
482476
new = Some((name.to_owned(), value.to_owned()));
483477
}
@@ -552,8 +546,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
552546
}
553547

554548
"strlen" => {
555-
let ptr = this.read_scalar(args[0])?.to_ptr()?;
556-
let n = this.memory().get(ptr.alloc_id)?.read_c_str(tcx, ptr)?.len();
549+
let ptr = this.read_scalar(args[0])?.not_undef()?;
550+
let n = this.memory().read_c_str(ptr)?.len();
557551
this.write_scalar(Scalar::from_uint(n as u64, dest.layout.size), dest)?;
558552
}
559553

0 commit comments

Comments
 (0)