Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of fd_write changed, seems to not be printing entire supplied array #3303

Closed
DenialAdams opened this issue Sep 5, 2021 · 2 comments
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@DenialAdams
Copy link

DenialAdams commented Sep 5, 2021

I recently updated my local version of wasmtime and noticed that the behavior of fd_write seems to have changed. I am supplying two strings to print: a supplied string, and a newline character. This used to print the newline character, which is what I expect, but seems to no longer do so. I bisected the behavior change back to PR #2444, but I didn't see this behavior change explicitly called out.

Basically, I'm wondering:

  1. Is this new behavior intended, or a bug?
  2. If this new behavior is intended, what am I doing wrong in the example program?

Thanks for reading!

Test Case

(module
  (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
  (memory 1)
  (export "memory" (memory 0))
  (data 0 (i32.const 16) "\n")
  (data 0 (i32.const 18) "world on a new line!")
  (data 0 (i32.const 47) "Hello,")
  (func $print (param i32) (param i32)
    (i32.store (i32.const 0) (local.get 0))
    (i32.store (i32.const 4) (local.get 1))
    (i32.store (i32.const 8) (i32.const 16))
    (i32.store (i32.const 12) (i32.const 1))
    (call $fd_write (i32.const 1) (i32.const 0) (i32.const 2) (i32.const 0))
    drop
  )
  (func $main
    (export "_start")
    i32.const 47
    i32.const 6
    call $print
    i32.const 18
    i32.const 20
    call $print
  )
)

Steps to Reproduce

  1. wasmtime repro.wast (contents above)
  2. Observe that the newline is not printed

Expected Results

Hello,
world on a new line!

Actual Results

Hello,world on a new line!

Versions and Environment

I first observed this behavior on wasmtime 0.29.0. I bisected the issue back to PR #2444, which seems to be where the behavior changed.

Operating system: Windows 10

Architecture: x86

@DenialAdams DenialAdams added the bug Incorrect behavior in the current implementation that needs fixing label Sep 5, 2021
@bjorn3
Copy link
Contributor

bjorn3 commented Sep 5, 2021

fd_write returns the amount of bytes written. You have to call it in a loop until all bytes are written. This matches the posix write syscall. From the man page of the write syscall:

The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)

Higher level api's generally do this loop for you.

@DenialAdams
Copy link
Author

fd_write returns the amount of bytes written. You have to call it in a loop until all bytes are written. This matches the posix write syscall. From the man page of the write syscall:

The number of bytes written may be less than count if, for example, there is insufficient space on the underlying physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).)

Higher level api's generally do this loop for you.

Thank you, this makes a lot of sense! Sorry for the false report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

No branches or pull requests

2 participants