Skip to content

Commit ec52bb3

Browse files
committed
[GR-50086] Add missing WASI functions as unimplemented nodes.
PullRequest: graal/16283
2 parents 740f10f + a63e5b1 commit ec52bb3

File tree

14 files changed

+416
-24
lines changed

14 files changed

+416
-24
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stdin=Hello Graal! 🚀
2+
zero-memory=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stdout Hello Graal! 🚀
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;;
2+
;; Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
;; DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
;;
5+
;; The Universal Permissive License (UPL), Version 1.0
6+
;;
7+
;; Subject to the condition set forth below, permission is hereby granted to any
8+
;; person obtaining a copy of this software, associated documentation and/or
9+
;; data (collectively the "Software"), free of charge and under any and all
10+
;; copyright rights in the Software, and any and all patent rights owned or
11+
;; freely licensable by each licensor hereunder covering either (i) the
12+
;; unmodified Software as contributed to or provided by such licensor, or (ii)
13+
;; the Larger Works (as defined below), to deal in both
14+
;;
15+
;; (a) the Software, and
16+
;;
17+
;; (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
;; one is included with the Software each a "Larger Work" to which the Software
19+
;; is contributed by such licensors),
20+
;;
21+
;; without restriction, including without limitation the rights to copy, create
22+
;; derivative works of, display, perform, and distribute the Software and make,
23+
;; use, sell, offer for sale, import, export, have made, and have sold the
24+
;; Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
;; either these or other terms.
26+
;;
27+
;; This license is subject to the following condition:
28+
;;
29+
;; The above copyright notice and either this complete permission notice or at a
30+
;; minimum a reference to the UPL must be included in all copies or substantial
31+
;; portions of the Software.
32+
;;
33+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
;; SOFTWARE.
40+
;;
41+
42+
(module
43+
(import "wasi_snapshot_preview1" "fd_read" (func $fd_read (param i32 i32 i32 i32) (result i32)))
44+
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
45+
(memory 1)
46+
(export "memory" (memory 0))
47+
;; iovec { buf = 24, buf_len = 24}
48+
(data (i32.const 0) "\18\00\00\00\18\00\00\00")
49+
;; iovec { buf = 48, buf_len = 8}
50+
(data (i32.const 8) "\28\00\00\00\08\00\00\00")
51+
;; iovec { buf = 24, buf_len = 17}
52+
(data (i32.const 16) "\18\00\00\00\11\00\00\00")
53+
(func (export "_main") (result i32) (local $ret i32)
54+
;; Read 17 bytes from stdin
55+
(local.set $ret
56+
(call $fd_read
57+
(i32.const 0) ;; fd 0 (stdin)
58+
(i32.const 0) ;; iovec at address 0
59+
(i32.const 2) ;; iovec size
60+
(i32.const 64) ;; address at which to write the number of bytes read
61+
)
62+
)
63+
;; Exit in case of error
64+
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))
65+
;; Number of bytes read should equal 17
66+
(if (i32.ne (i32.load (i32.const 64)) (i32.const 17)) (then (return (i32.const -1))))
67+
68+
;; Write path to stdout
69+
(local.set $ret
70+
(call $fd_write
71+
(i32.const 1) ;; fd 1 (stdout)
72+
(i32.const 16) ;; iovec at address 0
73+
(i32.const 1) ;; iovec size
74+
(i32.const 68) ;; address at which to write the number of bytes written
75+
)
76+
)
77+
;; Exit in case of error
78+
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))
79+
80+
;; Success
81+
(i32.const 0)
82+
)
83+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stdin=Hello Graal! 🚀
2+
zero-memory=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stdout Hello Graal! 🚀
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
;;
2+
;; Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
;; DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
;;
5+
;; The Universal Permissive License (UPL), Version 1.0
6+
;;
7+
;; Subject to the condition set forth below, permission is hereby granted to any
8+
;; person obtaining a copy of this software, associated documentation and/or
9+
;; data (collectively the "Software"), free of charge and under any and all
10+
;; copyright rights in the Software, and any and all patent rights owned or
11+
;; freely licensable by each licensor hereunder covering either (i) the
12+
;; unmodified Software as contributed to or provided by such licensor, or (ii)
13+
;; the Larger Works (as defined below), to deal in both
14+
;;
15+
;; (a) the Software, and
16+
;;
17+
;; (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
;; one is included with the Software each a "Larger Work" to which the Software
19+
;; is contributed by such licensors),
20+
;;
21+
;; without restriction, including without limitation the rights to copy, create
22+
;; derivative works of, display, perform, and distribute the Software and make,
23+
;; use, sell, offer for sale, import, export, have made, and have sold the
24+
;; Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
;; either these or other terms.
26+
;;
27+
;; This license is subject to the following condition:
28+
;;
29+
;; The above copyright notice and either this complete permission notice or at a
30+
;; minimum a reference to the UPL must be included in all copies or substantial
31+
;; portions of the Software.
32+
;;
33+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
;; SOFTWARE.
40+
;;
41+
42+
(module
43+
(import "wasi_snapshot_preview1" "fd_read" (func $fd_read (param i32 i32 i32 i32) (result i32)))
44+
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
45+
(memory 1)
46+
(export "memory" (memory 0))
47+
;; iovec { buf = 16, buf_len = 8}
48+
(data (i32.const 0) "\10\00\00\00\08\00\00\00")
49+
;; iovec { buf = 24, buf_len = 9 }
50+
(data (i32.const 8) "\18\00\00\00\09\00\00\00")
51+
(func (export "_main") (result i32) (local $ret i32)
52+
;; Read 17 bytes from stdin
53+
(local.set $ret
54+
(call $fd_read
55+
(i32.const 0) ;; fd 0 (stdin)
56+
(i32.const 0) ;; iovec at address 0
57+
(i32.const 2) ;; iovec size
58+
(i32.const 36) ;; address at which to write the number of bytes read
59+
)
60+
)
61+
;; Exit in case of error
62+
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))
63+
;; Number of bytes read should equal 17
64+
(if (i32.ne (i32.load (i32.const 36)) (i32.const 17)) (then (return (i32.const -1))))
65+
66+
;; Write path to stdout
67+
(local.set $ret
68+
(call $fd_write
69+
(i32.const 1) ;; fd 1 (stdout)
70+
(i32.const 0) ;; iovec at address 0
71+
(i32.const 2) ;; iovec size
72+
(i32.const 40) ;; address at which to write the number of bytes written
73+
)
74+
)
75+
;; Exit in case of error
76+
(if (i32.ne (local.get $ret) (i32.const 0)) (then (return (local.get $ret))))
77+
78+
;; Success
79+
(i32.const 0)
80+
)
81+
)

wasm/src/org.graalvm.wasm.test/src/test/wasi/wasm_test_index

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ environ-sizes-get
66
fd_prestat_get
77
fd_prestat_dir_name
88
fd_read-stdin
9+
fd_read-stdin-eof
10+
fd_read-stdin-multi
911
fd_seek-file
1012
fd_fdstat_get-file
1113
fd_filestat_get-file

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/predefined/wasi/WasiClockTimeGetNode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ private Object clockTimeGet(WasmMemory memory, int clockIdValue, int resultAddre
8181
memory.store_i64(this, resultAddress, realtimeNow());
8282
break;
8383
case Monotonic:
84+
memory.store_i64(this, resultAddress, monotonicNow());
85+
break;
8486
case ProcessCputimeId:
8587
case ThreadCputimeId:
8688
throw unimplementedClock(clockId);
@@ -93,6 +95,11 @@ public static long realtimeNow() {
9395
return ChronoUnit.NANOS.between(Instant.EPOCH, Instant.now());
9496
}
9597

98+
@TruffleBoundary
99+
public static long monotonicNow() {
100+
return System.nanoTime();
101+
}
102+
96103
@TruffleBoundary
97104
private static WasmException unimplementedClock(final Clockid clockId) {
98105
throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented ClockID: " + clockId.name());
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.wasm.predefined.wasi;
42+
43+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
44+
import com.oracle.truffle.api.frame.VirtualFrame;
45+
import org.graalvm.wasm.WasmArguments;
46+
import org.graalvm.wasm.WasmContext;
47+
import org.graalvm.wasm.WasmInstance;
48+
import org.graalvm.wasm.WasmLanguage;
49+
import org.graalvm.wasm.WasmModule;
50+
import org.graalvm.wasm.predefined.WasmBuiltinRootNode;
51+
import org.graalvm.wasm.predefined.wasi.fd.Fd;
52+
import org.graalvm.wasm.predefined.wasi.types.Errno;
53+
54+
public class WasiFdFilestatSetTimesNode extends WasmBuiltinRootNode {
55+
protected WasiFdFilestatSetTimesNode(WasmLanguage language, WasmModule module) {
56+
super(language, module);
57+
}
58+
59+
@Override
60+
public Object executeWithContext(VirtualFrame frame, WasmContext context, WasmInstance instance) {
61+
final Object[] args = frame.getArguments();
62+
return fdstatSetTime(context,
63+
(int) WasmArguments.getArgument(args, 0),
64+
(long) WasmArguments.getArgument(args, 1),
65+
(long) WasmArguments.getArgument(args, 2),
66+
(int) WasmArguments.getArgument(args, 3));
67+
}
68+
69+
@TruffleBoundary
70+
private int fdstatSetTime(WasmContext context, int fd, long atim, long mtim, int fstFlags) {
71+
final Fd handle = context.fdManager().get(fd);
72+
if (handle == null) {
73+
return Errno.Badf.ordinal();
74+
}
75+
return handle.fdstatSetTimes(this, atim, mtim, fstFlags).ordinal();
76+
}
77+
78+
@Override
79+
public String builtinNodeName() {
80+
return "__wasi_fd_filestat_set_times";
81+
}
82+
}

0 commit comments

Comments
 (0)