From 9b475b0efdb5a262938d716f4f619e47b6ce3d7f Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 4 Mar 2019 18:02:55 +0800 Subject: [PATCH] fix strange bug in sgx set_len --- sefs-fuse/app/src/sgx_dev.rs | 6 +++--- sefs-fuse/enclave/src/lib.rs | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/sefs-fuse/app/src/sgx_dev.rs b/sefs-fuse/app/src/sgx_dev.rs index 526cf57..eb46d52 100644 --- a/sefs-fuse/app/src/sgx_dev.rs +++ b/sefs-fuse/app/src/sgx_dev.rs @@ -59,21 +59,21 @@ impl File for SgxFile { fn write_at(&self, buf: &[u8], offset: usize) -> DevResult { match file_write_at(self.fd, offset, buf) { size if size > 0 => Ok(size as usize), - _ => panic!(), + e => panic!("write_at {}", e), } } fn set_len(&self, len: usize) -> DevResult<()> { match file_set_len(self.fd, len) { 0 => Ok(()), - _ => panic!(), + e => panic!("set_len {}", e), } } fn flush(&self) -> DevResult<()> { match file_flush(self.fd) { 0 => Ok(()), - _ => panic!(), + e => panic!("flush {}", e), } } } diff --git a/sefs-fuse/enclave/src/lib.rs b/sefs-fuse/enclave/src/lib.rs index da146b5..976bc66 100644 --- a/sefs-fuse/enclave/src/lib.rs +++ b/sefs-fuse/enclave/src/lib.rs @@ -64,7 +64,10 @@ pub extern "C" fn ecall_set_sefs_dir(path: *const u8, len: usize) -> i32 { macro_rules! try_io { ($expr:expr) => (match $expr { Ok(val) => val, - Err(err) => return err.raw_os_error().unwrap(), + Err(err) => { + error!("{:?}", err); + return err.raw_os_error().unwrap(); + } }); } @@ -140,9 +143,15 @@ pub extern "C" fn ecall_file_set_len(fd: usize, len: usize) -> i32 { debug!("set_len fd = {}, len = {}", fd, len); let current_len = try_io!(file.seek(SeekFrom::End(0))) as usize; if current_len < len { - let mut zeros = Vec::::new(); - zeros.resize(len - current_len, 0); - try_io!(file.write(zeros.as_slice())); + static ZEROS: [u8; 0x1000] = [0; 0x1000]; + let mut rest_len = len - current_len; + while rest_len != 0 { + let l = rest_len.min(0x1000); + try_io!(file.write(&ZEROS[..l])); + rest_len -= l; + } + // NOTE: Don't try to write a large slice at once. + // It will cause Error 12: "Cannot allocate memory" } // TODO: how to shrink a file? 0