Skip to content

Commit

Permalink
Merge #939
Browse files Browse the repository at this point in the history
939: Fix WASI append bug, add test r=MarkMcCaskey a=MarkMcCaskey

resolves #936 

Co-authored-by: Mark McCaskey <[email protected]>
  • Loading branch information
bors[bot] and Mark McCaskey authored Nov 8, 2019
2 parents 38f4fdb + 6478342 commit c7638be
Show file tree
Hide file tree
Showing 29 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## **[Unreleased]**

- [#936](https://github.com/wasmerio/wasmer/pull/939) Fix bug causing attempts to append to files with WASI to delete the contents of the file
- [#921](https://github.com/wasmerio/wasmer/pull/921) In LLVM backend, annotate all memory accesses with TBAA metadata.
- [#883](https://github.com/wasmerio/wasmer/pull/883) Allow floating point operations to have arbitrary inputs, even including SNaNs.
- [#856](https://github.com/wasmerio/wasmer/pull/856) Expose methods in the runtime C API to get a WASI import object
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
.arg("+nightly")
.arg("--target=wasm32-wasi")
.arg("-C")
.arg("opt-level=s")
.arg("opt-level=z")
.arg(file)
.arg("-o")
.arg(&wasm_out_name)
Expand Down
18 changes: 18 additions & 0 deletions lib/wasi-tests/tests/wasitests/fd_append.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// !!! THIS IS A GENERATED FILE !!!
// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME
// Files autogenerated with cargo build (build/wasitests.rs).

#[test]
fn test_fd_append() {
assert_wasi_output!(
"../../wasitests/fd_append.wasm",
"fd_append",
vec![],
vec![(
".".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/temp")
),],
vec![],
"../../wasitests/fd_append.out"
);
}
1 change: 1 addition & 0 deletions lib/wasi-tests/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod close_preopen_fd;
mod create_dir;
mod envvar;
mod fd_allocate;
mod fd_append;
mod fd_close;
mod fd_pread;
mod fd_read;
Expand Down
Binary file modified lib/wasi-tests/wasitests/close_preopen_fd.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_allocate.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions lib/wasi-tests/wasitests/fd_append.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Hello, world!\nGoodbye, world!\n"
48 changes: 48 additions & 0 deletions lib/wasi-tests/wasitests/fd_append.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Args:
// mapdir: .:wasitests/test_fs/temp

use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::path::PathBuf;

static STR1: &str = "Hello, world!\n";
static STR2: &str = "Goodbye, world!\n";

fn main() {
let file = {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("wasitests/test_fs/temp");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from(".");

base.push("fd_append_test");
base
};

{
let mut file_handle = OpenOptions::new()
.create_new(true)
.append(true)
.open(&file)
.expect("Couldn't create file");
file_handle.write(STR1.as_bytes()).unwrap();
}
{
let mut file_handle = OpenOptions::new()
.append(true)
.open(&file)
.expect("Couldn't reopen file to append");
file_handle.write(STR2.as_bytes()).unwrap();
}

let mut file_handle = OpenOptions::new()
.read(true)
.open(&file)
.expect("Couldn't reopen file to read");

let mut test = String::new();
file_handle.read_to_string(&mut test);

assert_eq!(&test, &format!("{}{}", STR1, STR2));
println!("{:?}", &test);
}
Binary file added lib/wasi-tests/wasitests/fd_append.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_close.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_pread.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_read.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_sync.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_link.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_rename.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_symlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/poll_oneoff.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/readlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/wasi_sees_virtual_root.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/writing.wasm
Binary file not shown.
20 changes: 17 additions & 3 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,12 +1724,25 @@ pub fn path_open(
}
}
let mut open_options = std::fs::OpenOptions::new();
let write_permission = adjusted_rights & __WASI_RIGHT_FD_WRITE != 0;
// append, truncate, and create all require the permission to write
let (append_permission, truncate_permission, create_permission) =
if write_permission {
(
fs_flags & __WASI_FDFLAG_APPEND != 0,
o_flags & __WASI_O_TRUNC != 0,
o_flags & __WASI_O_CREAT != 0,
)
} else {
(false, false, false)
};
let open_options = open_options
.read(true)
// TODO: ensure these rights are actually valid given parent, etc.
.write(adjusted_rights & __WASI_RIGHT_FD_WRITE != 0)
.create(o_flags & __WASI_O_CREAT != 0)
.truncate(o_flags & __WASI_O_TRUNC != 0);
.write(write_permission)
.create(create_permission)
.append(append_permission)
.truncate(truncate_permission);
open_flags |= Fd::READ;
if adjusted_rights & __WASI_RIGHT_FD_WRITE != 0 {
open_flags |= Fd::WRITE;
Expand Down Expand Up @@ -1798,6 +1811,7 @@ pub fn path_open(
let mut open_options = std::fs::OpenOptions::new();
let open_options = open_options
.read(true)
.append(fs_flags & __WASI_FDFLAG_APPEND != 0)
// TODO: ensure these rights are actually valid given parent, etc.
// write access is required for creating a file
.write(true)
Expand Down

0 comments on commit c7638be

Please sign in to comment.