From 92d6735e3fee9c48c879852a992ee87c511bd259 Mon Sep 17 00:00:00 2001 From: oddcoder Date: Wed, 3 Jun 2020 23:19:08 +0200 Subject: [PATCH] Add more scanf tests --- src/crti/src/lib.rs | 6 ++++-- src/crtn/src/lib.rs | 6 ++++-- src/header/stdio/lookaheadreader.rs | 27 +++++++++++---------------- src/header/stdio/mod.rs | 3 +-- src/header/sys_random/mod.rs | 10 +++++----- src/platform/redox/ptrace.rs | 8 ++++---- src/platform/redox/socket.rs | 5 ++++- src/platform/test/mod.rs | 8 +++++--- tests/Makefile | 1 + tests/expected/stdio/fscanf.stderr | 0 tests/expected/stdio/fscanf.stdout | 14 ++++++++++++++ tests/stdio/fscanf.c | 13 +++++++++++++ 12 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 tests/expected/stdio/fscanf.stderr create mode 100644 tests/expected/stdio/fscanf.stdout create mode 100644 tests/stdio/fscanf.c diff --git a/src/crti/src/lib.rs b/src/crti/src/lib.rs index 8071f05900..77e8f055da 100644 --- a/src/crti/src/lib.rs +++ b/src/crti/src/lib.rs @@ -27,7 +27,8 @@ global_asm!( ); // https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crti.s #[cfg(target_arch = "aarch64")] -global_asm!(r#" +global_asm!( + r#" .section .init .global _init .type _init,%function @@ -45,7 +46,8 @@ global_asm!(r#" mov x29,sp // stp: "stores two doublewords from the first and second argument to memory addressed by addr" // Body will be filled in by gcc and ended by crtn.o -"#); +"# +); #[panic_handler] #[linkage = "weak"] diff --git a/src/crtn/src/lib.rs b/src/crtn/src/lib.rs index 6704d357e9..b2d9608678 100644 --- a/src/crtn/src/lib.rs +++ b/src/crtn/src/lib.rs @@ -23,7 +23,8 @@ global_asm!( ); // https://git.musl-libc.org/cgit/musl/tree/crt/aarch64/crtn.s #[cfg(target_arch = "aarch64")] -global_asm!(r#" +global_asm!( + r#" .section .init // This happens after crti.o and gcc has inserted code // ldp: "loads two doublewords from memory addressed by the third argument to the first and second" @@ -35,7 +36,8 @@ global_asm!(r#" // ldp: "loads two doublewords from memory addressed by the third argument to the first and second" ldp x29,x30,[sp],#16 ret -"#); +"# +); #[panic_handler] #[linkage = "weak"] diff --git a/src/header/stdio/lookaheadreader.rs b/src/header/stdio/lookaheadreader.rs index 5c08d86751..91092f5af5 100644 --- a/src/header/stdio/lookaheadreader.rs +++ b/src/header/stdio/lookaheadreader.rs @@ -1,4 +1,4 @@ -use super::{fseek_locked, FILE, SEEK_SET, ftell_locked}; +use super::{fseek_locked, ftell_locked, FILE, SEEK_SET}; use crate::core_io::Read; struct LookAheadBuffer { buf: *const u8, @@ -27,23 +27,22 @@ impl From<*const u8> for LookAheadBuffer { } } - struct LookAheadFile<'a> { f: &'a mut FILE, look_ahead: i64, } -impl <'a> LookAheadFile<'a> { +impl<'a> LookAheadFile<'a> { fn look_ahead(&mut self) -> Result, i32> { let buf = &mut [0]; - let seek = unsafe{ ftell_locked(self.f)}; - unsafe{fseek_locked(self.f, self.look_ahead, SEEK_SET)}; + let seek = unsafe { ftell_locked(self.f) }; + unsafe { fseek_locked(self.f, self.look_ahead, SEEK_SET) }; let ret = match self.f.read(buf) { Ok(0) => Ok(None), Ok(_) => Ok(Some(buf[0])), Err(_) => Err(-1), }; - unsafe{fseek_locked(self.f, seek, SEEK_SET)}; + unsafe { fseek_locked(self.f, seek, SEEK_SET) }; self.look_ahead += 1; ret } @@ -53,17 +52,13 @@ impl <'a> LookAheadFile<'a> { } } -impl <'a> From<&'a mut FILE> for LookAheadFile<'a> { +impl<'a> From<&'a mut FILE> for LookAheadFile<'a> { fn from(f: &'a mut FILE) -> LookAheadFile<'a> { - let look_ahead = unsafe{ftell_locked(f)} as i64; - LookAheadFile{ - f, - look_ahead, - } + let look_ahead = unsafe { ftell_locked(f) } as i64; + LookAheadFile { f, look_ahead } } } - enum LookAheadReaderEnum<'a> { FILE(LookAheadFile<'a>), // (buffer, location) @@ -72,7 +67,7 @@ enum LookAheadReaderEnum<'a> { pub struct LookAheadReader<'a>(LookAheadReaderEnum<'a>); -impl <'a> LookAheadReader<'a> { +impl<'a> LookAheadReader<'a> { pub fn lookahead1(&mut self) -> Result, i32> { match &mut self.0 { LookAheadReaderEnum::FILE(f) => f.look_ahead(), @@ -87,13 +82,13 @@ impl <'a> LookAheadReader<'a> { } } -impl <'a> From<&'a mut FILE> for LookAheadReader<'a> { +impl<'a> From<&'a mut FILE> for LookAheadReader<'a> { fn from(f: &'a mut FILE) -> LookAheadReader { LookAheadReader(LookAheadReaderEnum::FILE(f.into())) } } -impl <'a> From<*const u8> for LookAheadReader<'a > { +impl<'a> From<*const u8> for LookAheadReader<'a> { fn from(buff: *const u8) -> LookAheadReader<'a> { LookAheadReader(LookAheadReaderEnum::BUFFER(buff.into())) } diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 2ec1ef15f7..2b22cf4ac5 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -516,7 +516,6 @@ pub unsafe extern "C" fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) /// Seek to an offset `offset` from `whence` #[no_mangle] pub unsafe extern "C" fn fseeko(stream: *mut FILE, off: off_t, whence: c_int) -> c_int { - let mut stream = (*stream).lock(); fseek_locked(&mut *stream, off, whence) } @@ -1051,7 +1050,7 @@ pub unsafe extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_ pub unsafe extern "C" fn vfscanf(file: *mut FILE, format: *const c_char, ap: va_list) -> c_int { let ret = { let mut file = (*file).lock(); - let f :&mut FILE = &mut *file; + let f: &mut FILE = &mut *file; let reader: LookAheadReader = f.into(); scanf::scanf(reader, format, ap) }; diff --git a/src/header/sys_random/mod.rs b/src/header/sys_random/mod.rs index 7543ca995d..957f8c7753 100644 --- a/src/header/sys_random/mod.rs +++ b/src/header/sys_random/mod.rs @@ -1,14 +1,14 @@ use core::slice; -use crate::platform::{Pal, Sys, types::*}; +use crate::platform::{types::*, Pal, Sys}; pub const GRND_NONBLOCK: c_uint = 1; pub const GRND_RANDOM: c_uint = 2; #[no_mangle] pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_uint) -> ssize_t { - Sys::getrandom(slice::from_raw_parts_mut( - buf as *mut u8, - buflen as usize - ), flags) + Sys::getrandom( + slice::from_raw_parts_mut(buf as *mut u8, buflen as usize), + flags, + ) } diff --git a/src/platform/redox/ptrace.rs b/src/platform/redox/ptrace.rs index b5fd2d16e6..665f5dcd00 100644 --- a/src/platform/redox/ptrace.rs +++ b/src/platform/redox/ptrace.rs @@ -5,6 +5,10 @@ //! compatibility. So, this module will be a hellhole. use super::super::{errno, types::*, Pal, PalPtrace, PalSignal, Sys}; +#[cfg(target_arch = "aarch64")] +use crate::header::arch_aarch64_user::user_regs_struct; +#[cfg(target_arch = "x86_64")] +use crate::header::arch_x64_user::user_regs_struct; use crate::{ c_str::CString, fs::File, @@ -12,10 +16,6 @@ use crate::{ io::{self, prelude::*}, sync::{Mutex, Once}, }; -#[cfg(target_arch = "aarch64")] -use crate::header::arch_aarch64_user::user_regs_struct; -#[cfg(target_arch = "x86_64")] -use crate::header::arch_x64_user::user_regs_struct; use alloc::collections::{btree_map::Entry, BTreeMap}; use core::mem; diff --git a/src/platform/redox/socket.rs b/src/platform/redox/socket.rs index 5937bdfb3d..01d6e85193 100644 --- a/src/platform/redox/socket.rs +++ b/src/platform/redox/socket.rs @@ -143,7 +143,10 @@ unsafe fn inner_get_name( inner_af_unix(&buf[5..], address, address_len); } else { // Socket doesn't belong to any scheme - panic!("socket {:?} doesn't match either tcp, udp or chan schemes", str::from_utf8(buf)); + panic!( + "socket {:?} doesn't match either tcp, udp or chan schemes", + str::from_utf8(buf) + ); } Ok(0) diff --git a/src/platform/test/mod.rs b/src/platform/test/mod.rs index d5fb9e0478..721db1b930 100644 --- a/src/platform/test/mod.rs +++ b/src/platform/test/mod.rs @@ -68,12 +68,14 @@ fn clock_gettime() { #[test] fn getrandom() { - use crate::header::sys_random; - use crate::platform::types::ssize_t; + use crate::{header::sys_random, platform::types::ssize_t}; let mut arrays = [[0; 32]; 32]; for i in 1..arrays.len() { - assert_eq!(Sys::getrandom(&mut arrays[i], 0), arrays[i].len() as ssize_t); + assert_eq!( + Sys::getrandom(&mut arrays[i], 0), + arrays[i].len() as ssize_t + ); for j in 0..arrays.len() { if i != j { diff --git a/tests/Makefile b/tests/Makefile index 7ff599552b..594b17d6d4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -45,6 +45,7 @@ EXPECT_NAMES=\ stdio/ungetc_multiple \ stdio/ungetc_ftell \ stdio/fscanf_offby1 \ + stdio/fscanf \ stdio/printf_neg_pad \ stdlib/a64l \ stdlib/alloc \ diff --git a/tests/expected/stdio/fscanf.stderr b/tests/expected/stdio/fscanf.stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/expected/stdio/fscanf.stdout b/tests/expected/stdio/fscanf.stdout new file mode 100644 index 0000000000..73f11e68cf --- /dev/null +++ b/tests/expected/stdio/fscanf.stdout @@ -0,0 +1,14 @@ +1 2 3 9 +9 +16 +35 +48 +92 +109 +152 +201 +241 +276 +293 +299 +301 diff --git a/tests/stdio/fscanf.c b/tests/stdio/fscanf.c new file mode 100644 index 0000000000..01d4c9c03b --- /dev/null +++ b/tests/stdio/fscanf.c @@ -0,0 +1,13 @@ +//@ 1 2 3 +//@ SS +#include +int main() { + FILE *f = fopen("stdio/fscanf.c", "r"); + int x, y, z; + fscanf(f, "//@ %d %d %d",&x , &y, &z); + printf("%d %d %d %ld\n", x, y, z, ftell(f)); + while(-1 != fscanf(f, "%*[^\n]")) { + printf("%ld\n", ftell(f)); + getc(f); + } +} \ No newline at end of file