From a2bdb3bb94936c45b68f91b4ffd938dd3df5f8c1 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 10:50:38 -0400 Subject: [PATCH 1/9] Shim 'libc::getrandom' in addition to 'libc::syscall(libc::SYS_getrandom)' --- src/shims/foreign_items.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 4fc8b1d54cfc..6ae99c9bc15c 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -293,15 +293,9 @@ fn emulate_foreign_item( // is called if a `HashMap` is created the regular way (e.g. HashMap). match this.read_scalar(args[0])?.to_usize(this)? { id if id == sys_getrandom => { - let ptr = this.read_scalar(args[1])?.not_undef()?; - let len = this.read_scalar(args[2])?.to_usize(this)?; - - // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, - // neither of which have any effect on our current PRNG - let _flags = this.read_scalar(args[3])?.to_i32()?; - - this.gen_random(ptr, len as usize)?; - this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?; + // The first argument is the syscall id, + // so skip over it + linux_getrandom(this, &args[1..], dest)?; } id => { throw_unsup_format!("miri does not support syscall ID {}", id) @@ -309,6 +303,10 @@ fn emulate_foreign_item( } } + "getrandom" => { + linux_getrandom(this, args, dest)?; + } + "dlsym" => { let _handle = this.read_scalar(args[0])?; let symbol = this.read_scalar(args[1])?.not_undef()?; @@ -969,3 +967,20 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option(this: &mut MiriEvalContext<'mir, 'tcx>, + args: &[OpTy<'tcx, Tag>], + dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> { + let ptr = this.read_scalar(args[0])?.not_undef()?; + let len = this.read_scalar(args[1])?.to_usize(this)?; + + + // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, + // neither of which have any effect on our current PRNG + let _flags = this.read_scalar(args[2])?.to_i32()?; + + this.gen_random(ptr, len as usize)?; + this.write_scalar(Scalar::from_uint(len, dest.layout.size), dest)?; + Ok(()) +} From 56a9a283e74078a3a48974a5e294bcb8f8e025c1 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 15:53:38 -0400 Subject: [PATCH 2/9] Cleanup formatting --- src/shims/foreign_items.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 6ae99c9bc15c..24bdc98fe8eb 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -969,13 +969,14 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option(this: &mut MiriEvalContext<'mir, 'tcx>, - args: &[OpTy<'tcx, Tag>], - dest: PlaceTy<'tcx, Tag>) -> InterpResult<'tcx> { +fn linux_getrandom<'tcx>( + this: &mut MiriEvalContext<'_, 'tcx>, + args: &[OpTy<'tcx, Tag>], + dest: PlaceTy<'tcx, Tag> +) -> InterpResult<'tcx> { let ptr = this.read_scalar(args[0])?.not_undef()?; let len = this.read_scalar(args[1])?.to_usize(this)?; - // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, // neither of which have any effect on our current PRNG let _flags = this.read_scalar(args[2])?.to_i32()?; From 6b087d25362a3461f76cdee5a62fb76d2d8023fd Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 15:53:49 -0400 Subject: [PATCH 3/9] Add test --- tests/run-pass/linux-getrandom.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/run-pass/linux-getrandom.rs diff --git a/tests/run-pass/linux-getrandom.rs b/tests/run-pass/linux-getrandom.rs new file mode 100644 index 000000000000..ded5596d8072 --- /dev/null +++ b/tests/run-pass/linux-getrandom.rs @@ -0,0 +1,12 @@ +// only-linux: Uses Linux-only APIs + +#![feature(rustc_private)] +extern crate libc; + +fn main() { + let mut buf = [0u8; 5]; + unsafe { + assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); + assert_eq!(libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5); + } +} From 8650f02bc983a34b3c7923850fca07c2ddf2d544 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 15:59:20 -0400 Subject: [PATCH 4/9] Add trailing comma --- src/shims/foreign_items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 24bdc98fe8eb..887fb8e5d80a 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -972,7 +972,7 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option( this: &mut MiriEvalContext<'_, 'tcx>, args: &[OpTy<'tcx, Tag>], - dest: PlaceTy<'tcx, Tag> + dest: PlaceTy<'tcx, Tag>, ) -> InterpResult<'tcx> { let ptr = this.read_scalar(args[0])?.not_undef()?; let len = this.read_scalar(args[1])?.to_usize(this)?; From a74a04f35676f9061691844b2852c499d953a731 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 16:11:52 -0400 Subject: [PATCH 5/9] Test 'libc::getrandom' as well --- tests/run-pass/linux-getrandom.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/run-pass/linux-getrandom.rs b/tests/run-pass/linux-getrandom.rs index ded5596d8072..b698b54a3c0e 100644 --- a/tests/run-pass/linux-getrandom.rs +++ b/tests/run-pass/linux-getrandom.rs @@ -8,5 +8,8 @@ fn main() { unsafe { assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); assert_eq!(libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5); + + assert_eq!(libc::getrandom(0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); + assert_eq!(libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5); } } From 8a758177073229df9c0bdcd69f3bcd7bbe5601c9 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 16:30:33 -0400 Subject: [PATCH 6/9] Fix identation --- tests/run-pass/linux-getrandom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/run-pass/linux-getrandom.rs b/tests/run-pass/linux-getrandom.rs index b698b54a3c0e..d9d588221c97 100644 --- a/tests/run-pass/linux-getrandom.rs +++ b/tests/run-pass/linux-getrandom.rs @@ -6,7 +6,7 @@ fn main() { let mut buf = [0u8; 5]; unsafe { - assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); + assert_eq!(libc::syscall(libc::SYS_getrandom, 0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); assert_eq!(libc::syscall(libc::SYS_getrandom, buf.as_mut_ptr() as *mut libc::c_void, 5 as libc::size_t, 0 as libc::c_uint), 5); assert_eq!(libc::getrandom(0 as *mut libc::c_void, 0 as libc::size_t, 0 as libc::c_uint), 0); From 66d10c877d4b68d730e6aa4759a36a68dfe97152 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 16:40:30 -0400 Subject: [PATCH 7/9] Ignore other platforms instead of using only-linux --- tests/run-pass/linux-getrandom.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/run-pass/linux-getrandom.rs b/tests/run-pass/linux-getrandom.rs index d9d588221c97..8da2ce46fe7d 100644 --- a/tests/run-pass/linux-getrandom.rs +++ b/tests/run-pass/linux-getrandom.rs @@ -1,5 +1,7 @@ -// only-linux: Uses Linux-only APIs - +// Unfortunately, compiletest_rs does not support 'only-linux', +// so we need to ignore Windows and OS X instead +// ignore-macos: Uses Linux-only APIs +// ignore-windows: Uses Linux-only APIs #![feature(rustc_private)] extern crate libc; From a208f2fccf30329f148fbc1f742e4c0af2417059 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 16:44:32 -0400 Subject: [PATCH 8/9] Improve formatting Co-Authored-By: Ralf Jung --- tests/run-pass/linux-getrandom.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/run-pass/linux-getrandom.rs b/tests/run-pass/linux-getrandom.rs index 8da2ce46fe7d..f582a282c59b 100644 --- a/tests/run-pass/linux-getrandom.rs +++ b/tests/run-pass/linux-getrandom.rs @@ -1,5 +1,5 @@ // Unfortunately, compiletest_rs does not support 'only-linux', -// so we need to ignore Windows and OS X instead +// so we need to ignore Windows and macOS instead. // ignore-macos: Uses Linux-only APIs // ignore-windows: Uses Linux-only APIs #![feature(rustc_private)] From f830a6c69ecef2e87a24be1dca68537411b04f84 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 4 Aug 2019 16:57:17 -0400 Subject: [PATCH 9/9] Apply more formatting fixes Co-Authored-By: Ralf Jung --- src/shims/foreign_items.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 887fb8e5d80a..538109eceae7 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -294,7 +294,7 @@ fn emulate_foreign_item( match this.read_scalar(args[0])?.to_usize(this)? { id if id == sys_getrandom => { // The first argument is the syscall id, - // so skip over it + // so skip over it. linux_getrandom(this, &args[1..], dest)?; } id => { @@ -968,7 +968,7 @@ fn eval_path_scalar(&mut self, path: &[&str]) -> InterpResult<'tcx, Option( this: &mut MiriEvalContext<'_, 'tcx>, args: &[OpTy<'tcx, Tag>], @@ -978,7 +978,7 @@ fn linux_getrandom<'tcx>( let len = this.read_scalar(args[1])?.to_usize(this)?; // The only supported flags are GRND_RANDOM and GRND_NONBLOCK, - // neither of which have any effect on our current PRNG + // neither of which have any effect on our current PRNG. let _flags = this.read_scalar(args[2])?.to_i32()?; this.gen_random(ptr, len as usize)?;