Skip to content

Commit

Permalink
[GR-55550] Fix type signatures in WasiModule.
Browse files Browse the repository at this point in the history
PullRequest: graal/18384
  • Loading branch information
jirkamarsik committed Jul 24, 2024
2 parents a5b2d47 + 904e4e2 commit 473b1d2
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 71 deletions.
1 change: 1 addition & 0 deletions wasm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This changelog summarizes major changes to the WebAssembly engine implemented in
## Version 24.1.0

* Implemented the [SIMD](https://github.com/WebAssembly/simd) proposal. This feature is enabled by default and can be disabled with the option `--wasm.SIMD=false`.
* Added limited support for `fd_advise` in `wasi_snapshot_preview1`.

## Version 23.1.0

Expand Down
2 changes: 1 addition & 1 deletion wasm/mx.wasm/mx_wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def emscripten_init(args):
@mx.command(_suite.name, "wasm")
def wasm(args, **kwargs):
"""Run a WebAssembly program."""
vmArgs, wasmArgs = mx.extract_VM_args(args, useDoubleDash=False, defaultAllVMArgs=False)
vmArgs, wasmArgs = mx.extract_VM_args(args, useDoubleDash=True, defaultAllVMArgs=False)
path_args = mx.get_runtime_jvm_args([
"TRUFFLE_API",
"org.graalvm.wasm",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enable-io=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int 0
185 changes: 185 additions & 0 deletions wasm/src/org.graalvm.wasm.test/src/test/wasi/fd_advise-file.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
;;
;; Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
;; DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
;;
;; The Universal Permissive License (UPL), Version 1.0
;;
;; Subject to the condition set forth below, permission is hereby granted to any
;; person obtaining a copy of this software, associated documentation and/or
;; data (collectively the "Software"), free of charge and under any and all
;; copyright rights in the Software, and any and all patent rights owned or
;; freely licensable by each licensor hereunder covering either (i) the
;; unmodified Software as contributed to or provided by such licensor, or (ii)
;; the Larger Works (as defined below), to deal in both
;;
;; (a) the Software, and
;;
;; (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
;; one is included with the Software each a "Larger Work" to which the Software
;; is contributed by such licensors),
;;
;; without restriction, including without limitation the rights to copy, create
;; derivative works of, display, perform, and distribute the Software and make,
;; use, sell, offer for sale, import, export, have made, and have sold the
;; Software and the Larger Work(s), and to sublicense the foregoing rights on
;; either these or other terms.
;;
;; This license is subject to the following condition:
;;
;; The above copyright notice and either this complete permission notice or at a
;; minimum a reference to the UPL must be included in all copies or substantial
;; portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;

(module
(import "wasi_snapshot_preview1" "path_open" (func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_advise" (func $fd_advise (param i32 i64 i64 i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_close" (func $fd_close (param i32) (result i32)))

(memory 1)

(data (i32.const 0) "file.txt")

(export "memory" (memory 0))

;; Pre-opened temporary directory fd
(global $directory_fd i32 (i32.const 3))

;; Memory location of file path
(global $file_path_address i32 (i32.const 0))
(global $file_path_length i32 (i32.const 8))

;; Address of opened file fd
(global $file_fd_address i32 (i32.const 12))

;; Rights for path_open
(global $all_rights i64 (i64.const 536870911))
(global $advise_right i64 (i64.const 128))

;; Advice for fd_advise
(global $sequential_access_advice i32 (i32.const 1))

;; Errno codes
(global $errno_inval i32 (i32.const 28))
(global $errno_badf i32 (i32.const 8))
(global $errno_notcapable i32 (i32.const 76))

(func (export "_main") (result i32) (local $ret i32)
;; Open file "file.txt" in pre-opened directory
(local.set $ret
(call $path_open
(global.get $directory_fd) ;; pre-opened "test" directory fd
(i32.const 0) ;; dirflags
(global.get $file_path_address) ;; pointer to path "file.txt"
(global.get $file_path_length) ;; path length
(i32.const 0) ;; oflags
(global.get $all_rights) ;; rights base (all rights set)
(global.get $all_rights) ;; rights inherting (all rights set)
(i32.const 0) ;; fdflags
(global.get $file_fd_address) ;; fd address
)
)
;; Exit in case of error
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))

;; Valid advice
(local.set $ret
(call $fd_advise
(i32.load (global.get $file_fd_address)) ;; "file.txt" fd
(i64.const 0) ;; offset of advised region
(i64.const 0) ;; length of advised region
(global.get $sequential_access_advice) ;; sequential access advice
)
)
;; Exit in case of error
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))

;; Invalid advice
(local.set $ret
(call $fd_advise
(i32.load (global.get $file_fd_address)) ;; "file.txt" fd
(i64.const 0) ;; offset of advised region
(i64.const 0) ;; length of advised region
(i32.const 42) ;; invalid advice
)
)
;; Check that error code is inval
(if (i32.ne (local.get $ret) (global.get $errno_inval)) (then (return (i32.const -1))))

;; Invalid length of advised region
(local.set $ret
(call $fd_advise
(i32.load (global.get $file_fd_address)) ;; "file.txt" fd
(i64.const 1) ;; offset of advised region
(i64.const -1) ;; length of advised region (must be non-negative)
(global.get $sequential_access_advice) ;; sequential access advice
)
)
;; Check that error code is inval
(if (i32.ne (local.get $ret) (global.get $errno_inval)) (then (return (i32.const -2))))

;; Invalid file descriptor
(local.set $ret
(call $fd_advise
(i32.add (i32.load (global.get $file_fd_address)) (i32.const 1)) ;; invalid fd
(i64.const 0) ;; offset of advised region
(i64.const 0) ;; length of advised region
(global.get $sequential_access_advice) ;; sequential access advice
)
)
;; Check that error code is badf
(if (i32.ne (local.get $ret) (global.get $errno_badf)) (then (return (i32.const -3))))

;; Free opened file
(local.set $ret (call $fd_close (i32.load (global.get $file_fd_address))))
;; Exit in case of error
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))

;; Open file "file.txt" in pre-opened directory 3 without advise capabilities
(local.set $ret
(call $path_open
(global.get $directory_fd) ;; pre-opened "test" directory fd
(i32.const 0) ;; dirflags
(global.get $file_path_address) ;; pointer to path "file.txt"
(global.get $file_path_length) ;; path length
(i32.const 0) ;; oflags
(i64.sub (global.get $all_rights) (global.get $advise_right)) ;; rights base (all rights set except for fd_advise)
(i64.sub (global.get $all_rights) (global.get $advise_right)) ;; rights inheriting (all rights set except for fd_advise)
(i32.const 0) ;; fdflags
(global.get $file_fd_address) ;; fd address
)
)
;; Exit in case of error
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))

;; Insufficient capabilities for advise
(local.set $ret
(call $fd_advise
(i32.load (global.get $file_fd_address)) ;; "file.txt" fd without advise rights
(i64.const 0) ;; offset of advised region
(i64.const 0) ;; length of advised region
(global.get $sequential_access_advice) ;; sequential access advice
)
)
;; Check that error code is notcapable
(if (i32.ne (local.get $ret) (global.get $errno_notcapable)) (then (return (i32.const -4))))

;; Free opened file
(local.set $ret (call $fd_close (i32.load (global.get $file_fd_address))))
;; Exit in case of error
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))

;; Clear random fd number so that this test is deterministic
(i32.store (global.get $file_fd_address) (i32.const 0))
;; Success
(i32.const 0)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ args-sizes-get
proc-exit
environ-get
environ-sizes-get
fd_advise-file
fd_prestat_get
fd_prestat_dir_name
fd_read-stdin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public final String readString(int startOffset, int length, Node node) {
* @param string the string to write
* @param offset memory index where to write the string
* @param length the maximum number of bytes to write, including the trailing null character
* @return the number of bytes written, including the trailing null character
* @return the number of bytes written
*/
@CompilerDirectives.TruffleBoundary
public final int writeString(Node node, String string, int offset, int length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.wasm.predefined.wasi;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.VirtualFrame;
import org.graalvm.wasm.WasmArguments;
import org.graalvm.wasm.WasmContext;
import org.graalvm.wasm.WasmInstance;
import org.graalvm.wasm.WasmLanguage;
import org.graalvm.wasm.WasmModule;
import org.graalvm.wasm.predefined.WasmBuiltinRootNode;
import org.graalvm.wasm.predefined.wasi.fd.Fd;
import org.graalvm.wasm.predefined.wasi.types.Advice;
import org.graalvm.wasm.predefined.wasi.types.Errno;

public class WasiFdAdviseNode extends WasmBuiltinRootNode {

public WasiFdAdviseNode(WasmLanguage language, WasmModule module) {
super(language, module);
}

@Override
public Object executeWithContext(VirtualFrame frame, WasmContext context, WasmInstance instance) {
final Object[] args = frame.getArguments();
return fdAdvise(context, (int) WasmArguments.getArgument(args, 0),
(long) WasmArguments.getArgument(args, 1),
(long) WasmArguments.getArgument(args, 2),
(int) WasmArguments.getArgument(args, 3));
}

@TruffleBoundary
private static int fdAdvise(WasmContext context, int fd, long offset, long length, int adviceValue) {
final Fd handle = context.fdManager().get(fd);
if (handle == null) {
return Errno.Badf.ordinal();
}
if (adviceValue < 0 || adviceValue >= Advice.values().length) {
return Errno.Inval.ordinal();
}
final Advice advice = Advice.values()[adviceValue];
if (length < 0) {
return Errno.Inval.ordinal();
}
return handle.advise(offset, length, advice).ordinal();
}

@Override
public String builtinNodeName() {
return "__wasi_fd_advise";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -84,7 +84,7 @@ private static int fdClose(WasmContext context, int fd) {

@Override
public String builtinNodeName() {
return "___wasi_fd_close";
return "__wasi_fd_close";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -76,7 +76,7 @@ private int fdFilestatGet(WasmContext context, WasmMemory memory, int fd, int bu

@Override
public String builtinNodeName() {
return "___wasi_fd_filestat_get";
return "__wasi_fd_filestat_get";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -76,7 +76,7 @@ private int fdPrestatDirName(WasmContext context, WasmMemory memory, int fd, int

@Override
public String builtinNodeName() {
return "___wasi_fd_prestat_dir_name";
return "__wasi_fd_prestat_dir_name";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -76,7 +76,7 @@ private int fdPrestatGet(WasmContext context, WasmMemory memory, int fd, int buf

@Override
public String builtinNodeName() {
return "___wasi_fd_prestat_get";
return "__wasi_fd_prestat_get";
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -81,7 +81,7 @@ private int fdSeek(WasmContext context, WasmMemory memory, int fd, long offset,

@Override
public String builtinNodeName() {
return "___wasi_fd_seek";
return "__wasi_fd_seek";
}

}
Loading

0 comments on commit 473b1d2

Please sign in to comment.