Skip to content

Conversation

@cimacmillan
Copy link
Contributor

What

  • Updated copy_buffer_to_iovec_app so that it copies as much of the buffer into the iovec as specified
  • Throw invalid value when allocating an iovec of size 0

Why

  • A bug found from TCP client example which allocates 1024 for the iovec size (where the buf size is also 1024) but received bytes is passed in as the buf_size argument to copy_buffer_to_iovec_app. This would return early after hitting this check buf + data->buf_len > buf_begin + buf_size. However, if the amount to copy is less than the iovec size, we should copy that much of the buf size. Eg TCP client sample receives 27(?) bytes at a time, and this copies 27 bytes into the iovec of size 1024
  • The TCP client example attempts to recv bytes of size 0, this attempts to wasm malloc size 0, which outputs a warning. We should early return if recv bytes of size 0

@cimacmillan cimacmillan changed the title Copy min amount from recv buffer Copy max amount from socket recv buffer Sep 16, 2022
@lum1n0us
Copy link
Collaborator

LGTM

@cimacmillan cimacmillan changed the title Copy max amount from socket recv buffer Copy only received bytes from socket recv buffer Sep 20, 2022
@cimacmillan cimacmillan changed the title Copy only received bytes from socket recv buffer Copy only received bytes from socket recv buffer into the app buffer Sep 20, 2022
@wenyongh
Copy link
Contributor

LGTM

Copy link
Collaborator

@loganek loganek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just left one minor comment. Thanks.

return __WASI_EINVAL;
}

if (size_to_copy == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonder if we need this check given that we already have this check in L1893

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right. It saves one validate_app_addr call but then maybe it's preferred that if we pass in an invalid app addr, we always return __WASI_EINVAL even if the size_to_copy is 0

@cimacmillan
Copy link
Contributor Author

cimacmillan commented Sep 20, 2022

@wenyongh The failing macos-latest check is unrelated to the change. Is it OK to merge / or can the check be retried? I don't have permissions to trigger the workflow manually

@wenyongh wenyongh merged commit 8dd1c8a into bytecodealliance:dev/socket Sep 20, 2022
wenyongh added a commit that referenced this pull request Sep 22, 2022
Implement more socket APIs, refer to #1336 and below PRs:
- Implement wasi_addr_resolve function (#1319)
- Fix socket-api byte order issue when host/network order are the same (#1327)
- Enhance sock_addr_local syscall (#1320)
- Implement sock_addr_remote syscall (#1360)
- Add support for IPv6 in WAMR (#1411)
- Implement ns lookup allowlist (#1420)
- Implement sock_send_to and sock_recv_from system calls (#1457)
- Added http downloader and multicast socket options (#1467)
- Fix `bind()` calls to receive the correct size of `sockaddr` structure (#1490)
- Assert on correct parameters (#1505)
- Copy only received bytes from socket recv buffer into the app buffer (#1497)

Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Callum Macmillan <[email protected]>
vickiegpt pushed a commit to vickiegpt/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
…ytecodealliance#1497)

**What**

* Updated `copy_buffer_to_iovec_app` so that it copies as much of the buffer into the iovec as specified
* Throw invalid value when allocating an iovec of size 0

**Why**

* A bug found from TCP client example which allocates 1024 for the iovec size (where the buf size is also 1024) but received bytes is passed in as the `buf_size` argument to `copy_buffer_to_iovec_app`. This would return early after hitting this check `buf + data->buf_len > buf_begin + buf_size`. However, if the amount to copy is less than the iovec size, we should copy that much of the buf size. Eg TCP client sample receives 27(?) bytes at a time, and this copies 27 bytes into the iovec of size 1024
* The TCP client example attempts to recv bytes of size 0, this attempts to wasm malloc size 0, which outputs a warning. We should early return if recv bytes of size 0
vickiegpt pushed a commit to vickiegpt/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
Implement more socket APIs, refer to bytecodealliance#1336 and below PRs:
- Implement wasi_addr_resolve function (bytecodealliance#1319)
- Fix socket-api byte order issue when host/network order are the same (bytecodealliance#1327)
- Enhance sock_addr_local syscall (bytecodealliance#1320)
- Implement sock_addr_remote syscall (bytecodealliance#1360)
- Add support for IPv6 in WAMR (bytecodealliance#1411)
- Implement ns lookup allowlist (bytecodealliance#1420)
- Implement sock_send_to and sock_recv_from system calls (bytecodealliance#1457)
- Added http downloader and multicast socket options (bytecodealliance#1467)
- Fix `bind()` calls to receive the correct size of `sockaddr` structure (bytecodealliance#1490)
- Assert on correct parameters (bytecodealliance#1505)
- Copy only received bytes from socket recv buffer into the app buffer (bytecodealliance#1497)

Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Callum Macmillan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants