Skip to content

Commit fbbca59

Browse files
committed
avoid Scalar::from_(u)int in favor of giving the size explicitly
1 parent 3d3e2b6 commit fbbca59

File tree

11 files changed

+61
-64
lines changed

11 files changed

+61
-64
lines changed

src/eval.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::convert::TryFrom;
66
use rand::rngs::StdRng;
77
use rand::SeedableRng;
88

9-
use rustc::ty::layout::{LayoutOf, Size};
9+
use rustc::ty::layout::LayoutOf;
1010
use rustc::ty::{self, TyCtxt};
1111
use rustc_hir::def_id::DefId;
1212

@@ -96,7 +96,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
9696
// First argument: pointer to `main()`.
9797
let main_ptr = ecx.memory.create_fn_alloc(FnVal::Instance(main_instance));
9898
// Second argument (argc): length of `config.args`.
99-
let argc = Scalar::from_uint(u64::try_from(config.args.len()).unwrap(), ecx.pointer_size());
99+
let argc = Scalar::from_machine_usize(u64::try_from(config.args.len()).unwrap(), &ecx);
100100
// Third argument (`argv`): created from `config.args`.
101101
let argv = {
102102
// Put each argument in memory, collect pointers.
@@ -152,10 +152,9 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
152152
let cmd_place = ecx.allocate(ecx.layout_of(cmd_type)?, MiriMemoryKind::Machine.into());
153153
ecx.machine.cmd_line = Some(cmd_place.ptr);
154154
// Store the UTF-16 string. We just allocated so we know the bounds are fine.
155-
let char_size = Size::from_bytes(2);
156155
for (idx, &c) in cmd_utf16.iter().enumerate() {
157156
let place = ecx.mplace_field(cmd_place, idx)?;
158-
ecx.write_scalar(Scalar::from_uint(c, char_size), place.into())?;
157+
ecx.write_scalar(Scalar::from_u16(c), place.into())?;
159158
}
160159
}
161160
argv

src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
100100
/// Test if this immediate equals 0.
101101
fn is_null(&self, val: Scalar<Tag>) -> InterpResult<'tcx, bool> {
102102
let this = self.eval_context_ref();
103-
let null = Scalar::from_int(0, this.memory.pointer_size());
103+
let null = Scalar::ptr_null(this);
104104
this.ptr_eq(val, null)
105105
}
106106

src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
316316
let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
317317
// First argument: `size`.
318318
// (`0` is allowed here -- this is expected to be handled by the lang item).
319-
let size = Scalar::from_uint(layout.size.bytes(), ecx.pointer_size());
319+
let size = Scalar::from_machine_usize(layout.size.bytes(), ecx);
320320

321321
// Second argument: `align`.
322-
let align = Scalar::from_uint(layout.align.abi.bytes(), ecx.pointer_size());
322+
let align = Scalar::from_machine_usize(layout.align.abi.bytes(), ecx);
323323

324324
// Call the `exchange_malloc` lang item.
325325
let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();

src/shims/env.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
108108
name_op: OpTy<'tcx, Tag>, // LPCWSTR
109109
buf_op: OpTy<'tcx, Tag>, // LPWSTR
110110
size_op: OpTy<'tcx, Tag>, // DWORD
111-
) -> InterpResult<'tcx, u64> {
111+
) -> InterpResult<'tcx, u32> {
112112
let this = self.eval_context_mut();
113113
this.assert_target_os("windows", "GetEnvironmentVariableW");
114114

@@ -124,17 +124,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
124124

125125
let buf_ptr = this.read_scalar(buf_op)?.not_undef()?;
126126
// `buf_size` represents the size in characters.
127-
let buf_size = u64::try_from(this.read_scalar(size_op)?.to_u32()?).unwrap();
127+
let buf_size = u64::from(this.read_scalar(size_op)?.to_u32()?);
128128
let (success, len) = this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?;
129129

130130
if success {
131131
// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
132132
// not including the terminating null character.
133-
len
133+
u32::try_from(len).unwrap()
134134
} else {
135135
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
136136
// required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
137-
len + 1
137+
u32::try_from(len).unwrap().checked_add(1).unwrap()
138138
}
139139
}
140140
None => {
@@ -344,7 +344,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
344344
// Collect all the pointers to each variable in a vector.
345345
let mut vars: Vec<Scalar<Tag>> = this.machine.env_vars.map.values().map(|&ptr| ptr.into()).collect();
346346
// Add the trailing null pointer.
347-
vars.push(Scalar::from_int(0, this.pointer_size()));
347+
vars.push(Scalar::from_machine_usize(0, this));
348348
// Make an array with all these pointers inside Miri.
349349
let tcx = this.tcx;
350350
let vars_layout =

src/shims/foreign_items.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4747
fn malloc(&mut self, size: u64, zero_init: bool, kind: MiriMemoryKind) -> Scalar<Tag> {
4848
let this = self.eval_context_mut();
4949
if size == 0 {
50-
Scalar::from_int(0, this.pointer_size())
50+
Scalar::ptr_null(this)
5151
} else {
5252
let align = this.min_align(size, kind);
5353
let ptr = this.memory.allocate(Size::from_bytes(size), align, kind.into());
@@ -78,7 +78,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7878
let new_align = this.min_align(new_size, kind);
7979
if this.is_null(old_ptr)? {
8080
if new_size == 0 {
81-
Ok(Scalar::from_int(0, this.pointer_size()))
81+
Ok(Scalar::ptr_null(this))
8282
} else {
8383
let new_ptr =
8484
this.memory.allocate(Size::from_bytes(new_size), new_align, kind.into());
@@ -88,7 +88,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8888
let old_ptr = this.force_ptr(old_ptr)?;
8989
if new_size == 0 {
9090
this.memory.deallocate(old_ptr, None, kind.into())?;
91-
Ok(Scalar::from_int(0, this.pointer_size()))
91+
Ok(Scalar::ptr_null(this))
9292
} else {
9393
let new_ptr = this.memory.reallocate(
9494
old_ptr,
@@ -296,7 +296,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
296296
}
297297
};
298298

299-
this.write_scalar(Scalar::from_int(result, Size::from_bits(32)), dest)?;
299+
this.write_scalar(Scalar::from_i32(result), dest)?;
300300
}
301301
"memrchr" => {
302302
let ptr = this.read_scalar(args[0])?.not_undef()?;
@@ -334,7 +334,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
334334
"strlen" => {
335335
let ptr = this.read_scalar(args[0])?.not_undef()?;
336336
let n = this.memory.read_c_str(ptr)?.len();
337-
this.write_scalar(Scalar::from_uint(u64::try_from(n).unwrap(), dest.layout.size), dest)?;
337+
this.write_scalar(Scalar::from_machine_usize(u64::try_from(n).unwrap(), this), dest)?;
338338
}
339339

340340
// math functions

src/shims/foreign_items/posix.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2626
}
2727
"unsetenv" => {
2828
let result = this.unsetenv(args[0])?;
29-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
29+
this.write_scalar(Scalar::from_i32(result), dest)?;
3030
}
3131
"setenv" => {
3232
let result = this.setenv(args[0], args[1])?;
33-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
33+
this.write_scalar(Scalar::from_i32(result), dest)?;
3434
}
3535
"getcwd" => {
3636
let result = this.getcwd(args[0], args[1])?;
3737
this.write_scalar(result, dest)?;
3838
}
3939
"chdir" => {
4040
let result = this.chdir(args[0])?;
41-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
41+
this.write_scalar(Scalar::from_i32(result), dest)?;
4242
}
4343

4444
// File related shims
4545
"open" | "open64" => {
4646
let result = this.open(args[0], args[1])?;
47-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
47+
this.write_scalar(Scalar::from_i32(result), dest)?;
4848
}
4949
"fcntl" => {
5050
let result = this.fcntl(args[0], args[1], args.get(2).cloned())?;
51-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
51+
this.write_scalar(Scalar::from_i32(result), dest)?;
5252
}
5353
"read" => {
5454
let result = this.read(args[0], args[1], args[2])?;
55-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
55+
this.write_scalar(Scalar::from_machine_isize(result, this), dest)?;
5656
}
5757
"write" => {
5858
let fd = this.read_scalar(args[0])?.to_i32()?;
@@ -85,35 +85,36 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8585
this.write(args[0], args[1], args[2])?
8686
};
8787
// Now, `result` is the value we return back to the program.
88-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
88+
this.write_scalar(Scalar::from_machine_isize(result, this), dest)?;
8989
}
9090
"unlink" => {
9191
let result = this.unlink(args[0])?;
92-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
92+
this.write_scalar(Scalar::from_i32(result), dest)?;
9393
}
9494
"symlink" => {
9595
let result = this.symlink(args[0], args[1])?;
96-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
96+
this.write_scalar(Scalar::from_i32(result), dest)?;
9797
}
9898
"rename" => {
9999
let result = this.rename(args[0], args[1])?;
100-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
100+
this.write_scalar(Scalar::from_i32(result), dest)?;
101101
}
102102
"mkdir" => {
103103
let result = this.mkdir(args[0], args[1])?;
104-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
104+
this.write_scalar(Scalar::from_i32(result), dest)?;
105105
}
106106
"rmdir" => {
107107
let result = this.rmdir(args[0])?;
108-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
108+
this.write_scalar(Scalar::from_i32(result), dest)?;
109109
}
110110
"closedir" => {
111111
let result = this.closedir(args[0])?;
112-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
112+
this.write_scalar(Scalar::from_i32(result), dest)?;
113113
}
114114
"lseek" | "lseek64" => {
115115
let result = this.lseek64(args[0], args[1], args[2])?;
116-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
116+
// "lseek" is only used on macOS which is 64bit-only, so `i64` always works.
117+
this.write_scalar(Scalar::from_i64(result), dest)?;
117118
}
118119

119120
// Allocation
@@ -165,8 +166,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
165166
let name = this.read_scalar(args[0])?.to_i32()?;
166167

167168
let sysconfs = &[
168-
("_SC_PAGESIZE", Scalar::from_int(PAGE_SIZE, dest.layout.size)),
169-
("_SC_NPROCESSORS_ONLN", Scalar::from_int(NUM_CPUS, dest.layout.size)),
169+
("_SC_PAGESIZE", Scalar::from_int(PAGE_SIZE, this.pointer_size())),
170+
("_SC_NPROCESSORS_ONLN", Scalar::from_int(NUM_CPUS, this.pointer_size())),
170171
];
171172
let mut result = None;
172173
for &(sysconf_name, value) in sysconfs {

src/shims/foreign_items/posix/linux.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2424
// in the `posix` module.
2525
"close" => {
2626
let result = this.close(args[0])?;
27-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
27+
this.write_scalar(Scalar::from_i32(result), dest)?;
2828
}
2929
"opendir" => {
3030
let result = this.opendir(args[0])?;
3131
this.write_scalar(result, dest)?;
3232
}
3333
"readdir64_r" => {
3434
let result = this.linux_readdir64_r(args[0], args[1], args[2])?;
35-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
35+
this.write_scalar(Scalar::from_i32(result), dest)?;
3636
}
3737
// Linux-only
3838
"posix_fadvise" => {
@@ -48,7 +48,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
4848
"clock_gettime" => {
4949
// This is a POSIX function but it has only been tested on linux.
5050
let result = this.clock_gettime(args[0], args[1])?;
51-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
51+
this.write_scalar(Scalar::from_i32(result), dest)?;
5252
}
5353

5454
// Querying system information
@@ -59,11 +59,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5959
let size_place = this.deref_operand(args[2])?;
6060

6161
this.write_scalar(
62-
Scalar::from_uint(STACK_ADDR, addr_place.layout.size),
62+
Scalar::from_uint(STACK_ADDR, this.pointer_size()),
6363
addr_place.into(),
6464
)?;
6565
this.write_scalar(
66-
Scalar::from_uint(STACK_SIZE, size_place.layout.size),
66+
Scalar::from_uint(STACK_SIZE, this.pointer_size()),
6767
size_place.into(),
6868
)?;
6969

@@ -93,7 +93,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9393
id if id == sys_statx => {
9494
// The first argument is the syscall id, so skip over it.
9595
let result = this.linux_statx(args[1], args[2], args[3], args[4], args[5])?;
96-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
96+
this.write_scalar(Scalar::from_machine_isize(result.into(), this), dest)?;
9797
}
9898
id => throw_unsup_format!("miri does not support syscall ID {}", id),
9999
}
@@ -110,7 +110,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
110110
// FIXME: we just return an error; `num_cpus` then falls back to `sysconf`.
111111
let einval = this.eval_libc("EINVAL")?;
112112
this.set_last_error(einval)?;
113-
this.write_scalar(Scalar::from_int(-1, dest.layout.size), dest)?;
113+
this.write_scalar(Scalar::from_i32(-1), dest)?;
114114
}
115115

116116
// Incomplete shims that we "stub out" just to get pre-main initialziation code to work.

src/shims/foreign_items/posix/macos.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
2222
// File related shims
2323
"close$NOCANCEL" => {
2424
let result = this.close(args[0])?;
25-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
25+
this.write_scalar(Scalar::from_i32(result), dest)?;
2626
}
2727
"stat$INODE64" => {
2828
let result = this.macos_stat(args[0], args[1])?;
29-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
29+
this.write_scalar(Scalar::from_i32(result), dest)?;
3030
}
3131
"lstat$INODE64" => {
3232
let result = this.macos_lstat(args[0], args[1])?;
33-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
33+
this.write_scalar(Scalar::from_i32(result), dest)?;
3434
}
3535
"fstat$INODE64" => {
3636
let result = this.macos_fstat(args[0], args[1])?;
37-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
37+
this.write_scalar(Scalar::from_i32(result), dest)?;
3838
}
3939
"opendir$INODE64" => {
4040
let result = this.opendir(args[0])?;
4141
this.write_scalar(result, dest)?;
4242
}
4343
"readdir_r$INODE64" => {
4444
let result = this.macos_readdir_r(args[0], args[1], args[2])?;
45-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
45+
this.write_scalar(Scalar::from_i32(result), dest)?;
4646
}
4747

4848
// Environment related shims
@@ -53,11 +53,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5353
// Time related shims
5454
"gettimeofday" => {
5555
let result = this.gettimeofday(args[0], args[1])?;
56-
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
56+
this.write_scalar(Scalar::from_i32(result), dest)?;
5757
}
5858
"mach_absolute_time" => {
5959
let result = this.mach_absolute_time()?;
60-
this.write_scalar(Scalar::from_uint(result, dest.layout.size), dest)?;
60+
this.write_scalar(Scalar::from_u64(result), dest)?;
6161
}
6262

6363
// Access to command-line arguments
@@ -79,12 +79,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
7979
// Querying system information
8080
"pthread_get_stackaddr_np" => {
8181
let _thread = this.read_scalar(args[0])?.not_undef()?;
82-
let stack_addr = Scalar::from_uint(STACK_ADDR, dest.layout.size);
82+
let stack_addr = Scalar::from_uint(STACK_ADDR, this.pointer_size());
8383
this.write_scalar(stack_addr, dest)?;
8484
}
8585
"pthread_get_stacksize_np" => {
8686
let _thread = this.read_scalar(args[0])?.not_undef()?;
87-
let stack_size = Scalar::from_uint(STACK_SIZE, dest.layout.size);
87+
let stack_size = Scalar::from_uint(STACK_SIZE, this.pointer_size());
8888
this.write_scalar(stack_size, dest)?;
8989
}
9090

0 commit comments

Comments
 (0)