WasmEdge / wasmedge-rust-sdk

Embed WasmEdge functions in a Rust host app
Apache License 2.0
30 stars 15 forks source link

Refactor/sdk preview #98

Closed L-jasmine closed 11 months ago

L-jasmine commented 11 months ago

Refactor the SDK to resolve memory-unsafe bugs caused by its usage.

See https://github.com/WasmEdge/wasmedge-rust-sdk/pull/83

juntao commented 11 months ago

Hello, I am a code review bot on flows.network. Here are my reviews of code commits in this PR.


Overall Summary:

In general, the pull request titled "Refactor/sdk preview" includes a variety of changes across different files. The change sets include adding attributes, fixing memory leaks, removing unused code, adding new files and functionality, modifying existing code, and making adjustments to dependencies.

Important Findings:

  1. There are several potential issues and errors that need to be addressed, such as typos, misuse of attributes, unhandled error cases, and unclear changes without proper context or explanation.

  2. There are significant refactorings throughout the codebase. It is important to carefully review these changes to ensure correctness and avoid introducing new bugs.

  3. Some changes result in the introduction of new functionality, like managing socket writability and handling socket events, implementing a virtual file system, and adding timeout functionality. However, there are potential problems and lack of documentation to understand these changes fully.

  4. The commit messages and pull request descriptions often lack context and explanation, making it difficult to understand the purpose and motivation behind the changes. It is recommended to provide more detailed information in the commit messages or pull request description.

Overall, the pull request shows a mix of potential issues, important findings, and new functionality. Addressing the potential problems and providing more documentation will be crucial for a comprehensive review.

Details

Commit ecaa6cb9da0f3a25d771ce653e61350522966252

Key changes:

Potential problems:

Commit 2116d0b7125504d8b3323365ff41cf3e85761243

The key changes in this patch include:

One potential problem is the deletion of the Drop implementation for ExportType and Module. This may cause memory leaks if the inner field is not properly freed.

Another potential problem is the removal of code related to Externals, including functions, globals, memory, and tables. This may impact the functionality of the codebase if these features were being used.

It is also worth noting that there are changes in a large number of files, indicating significant refactorings throughout the codebase. It would be important to review these changes in detail to ensure they are correct and do not introduce new bugs.

Commit 949c9876187cfc1a7105452b1792cb06acf8a7f9

Key changes in the patch:

  1. Added a new struct SocketWritable to handle checking and setting socket writability.
  2. Added a new struct SocketWritableFuture as a future for checking socket writability.
  3. Added a writable() method to SocketWritable to asynchronously wait for the socket to become writable.
  4. Added a writable() method to AsyncWasiSocket to set the socket as writable and await for it to become writable.
  5. Modified the readable() method in AsyncWasiSocket to use the readable() method from AsyncWasiSocketInner.
  6. Modified the readable() method in AsyncWasiSocket to return Ok(()) instead of Ok(None).
  7. Modified the readable() method in AsyncWasiSocket to clear the ready state of the socket after reading.
  8. Added a new field writable of type SocketWritable to AsyncWasiSocket.

Potential problems:

  1. In the readable() method of AsyncWasiSocket, the match expression on r does not handle the case when r is Err(e).
  2. In the wait_fd() function, the handler closure does not properly handle the case when the Result is an error. The current implementation sets the error field of __wasi_event_t but does not handle it in the return value.
  3. The AsyncWasiSocket struct now has a new field writable which is not used in the existing code. It is unclear how this field is intended to be used.
  4. The changes in AsyncWasiSocket::readable() to clear the ready state of the socket after reading may have unintended consequences if the clearing of ready state is not necessary or if it is done at the wrong time.

Overall, the changes seem to introduce new functionality for managing socket writability and handle socket events. However, there are potential problems that need to be addressed and more information is needed to understand the use case for the new writable field in AsyncWasiSocket.

Commit 0524f2267559f5dc6517f5329078a1fd1a51f961

Key changes:

  1. Added the line socket.writable.set_writable(); to the code.

Potential problems:

  1. There is no description or explanation provided for the purpose of the change. It would be helpful to have more context to understand why this line was added.

Overall, the changes seem simple and isolated to one file in the project. However, without further information, it is difficult to assess the impact of this change and whether it is necessary or if there might be any unintended side effects. It is recommended to ask the author for more details about the change.

Commit d7f841eda90b63b9beab06d3178bd10282f42b3d

Key Changes:

Potential Problems:

Commit 7408e6278d4172c303bc214adc736b8b0c270d63

Key changes in this GitHub patch:

  1. In the file async_tokio.rs, the state field of the AsyncWasiSocket struct has been changed from WasiSocketState to Box<WasiSocketState>.
  2. In the file sync.rs, the seek method of the WasiFile struct has been refactored to set the seek position before reading or writing to the file.
  3. In the file mod.rs, the WasiCtx struct was modified. The vfs field was changed from an Arc<RwLock<ObjectPool<VFD>>> to an ObjectPool<VFD>. The push_preopen, insert_vfd, remove_closed, remove, get, and get_mut methods were modified to use the new vfs field directly instead of the RwLock. The vfs field initialization was also modified to directly assign the vfs parameter value.

Potential problems:

  1. It's unclear why the state field in AsyncWasiSocket is changed to a Box<WasiSocketState>. This change could have impacts on other parts of the codebase that rely on this field being directly accessible without indirection.
  2. The refactoring in sync.rs could introduce bugs if the seek position is not correctly set before reading or writing to the file.
  3. The modification of the WasiCtx struct and related methods could introduce synchronization issues since the Arc<RwLock<ObjectPool<VFD>>> has been replaced with a direct ObjectPool<VFD>. This change could impact the thread-safety of the code if multiple threads access the vfs field simultaneously.

Commit 882a3b2cd00003615b06105965233fd44a4dd98b

Key changes:

Potential problems:

Commit 47a360488a27dcc65f759517938b9556c81b59ad

Key changes:

Potential problems:

Commit f71e855d8372776f38c758819d9642cdcba18560

Key changes in the patch:

Potential problems:

Commit 317c7a990f8fc0e781df02334b18fd23b0f91ba8

Key changes:

Potential problems:

Commit 89ea9a2713dd3da31b18cfa9fde172e1a7f6d58e

Key changes in the patch:

Potential problems:

Commit 7cfb54777a586951fa4fe879dfc8639ab94abb35

Key changes:

Potential problems:

Overall, the code changes seem reasonable, but further clarification and tests would be beneficial.

Commit bf772c0d7b52818c0736e9ecd0a9da5d0701b0c3

Key Changes:

Potential Problems:

Suggestions for Improvement:

Commit 2ffdbb0f96c270faf76d43e94ff2c8b3050c4c81

Key changes in the patch:

  1. In crates/wasmedge-sys/src/async/module.rs, commented out the creation of an async function.
  2. In crates/wasmedge-sys/src/frame.rs, added a new method memory_mut to retrieve a mutable reference to a memory instance by its index.
  3. In crates/wasmedge-sys/src/store.rs, updated the documentation comment to clarify the purpose of the Store struct.
  4. In src/async/import.rs and src/import.rs, updated the documentation comment to clarify the purpose of the ImportObjectBuilder struct.
  5. In src/store.rs, updated the documentation comment to clarify the purpose of the Store struct.

Potential problems:

  1. The comment in crates/wasmedge-sys/src/async/module.rs suggests that an async function was commented out. If this function is needed, its removal may cause functionality issues.
  2. The new method memory_mut in crates/wasmedge-sys/src/frame.rs is not documented or explained further. It would be helpful to provide more context and examples for its usage.
  3. The documentation comments in src/async/import.rs, src/import.rs, and src/store.rs could be more detailed and provide more examples to help users understand their purpose and usage.

Commit 9c95c51facbca52b324ce7d973f111cef0ae4a37

Key changes:

Potential problems:

Overall, the refactoring seems reasonable, but it is important to carefully evaluate the safety and correctness of the unsafe code and consider error handling.

Commit aa2639d36340822e31aeae59d67df80d5b7b131f

Key changes:

Potential problems:

Commit 4cccc4aef1ccc7277f85a0f802e764de7dbc92c2

Key changes in this patch include:

  1. The WasiModule struct is changed from using composition to using a tuple struct with an Instance field.
  2. The Drop implementation for WasiModule is removed.
  3. The AsRef and AsMut trait implementations are added for WasiModule.
  4. Several methods in the WasiModule implementation are modified to use the Instance field instead of the inner field.

Potential problems:

  1. The WasiModule struct's Clone trait implementation is removed. It should be verified if this is intentional or if it needs to be re-implemented.
  2. The Drop implementation for WasiModule is removed, which means that the underlying InnerInstance may not be properly cleaned up. This change should be reviewed to ensure that there are no memory leaks.
  3. The changed WasiModule struct now exposes the Instance field directly, which could potentially lead to misuse or unintended modifications. This change should be reviewed to ensure that it doesn't introduce any issues related to encapsulation or safety.
  4. The methods in the WasiModule implementation are modified to use the Instance field directly. This change should be reviewed to ensure that it doesn't introduce any logic errors or unexpected behavior.

Overall, the changes seem to be focused on refactoring the WasiModule struct and its usage. It is important to thoroughly review the changes to ensure that they don't introduce any issues or regressions.

Commit 18073d36d9f48bb515f14c691c1f29c6c55a0700

Key Changes:

Potential Problems:

Commit 6132ffe6db3098e2bc11d6e0bbb6d57ec0e5399b

Key Changes:

Potential Problems:

Overall, this change seems to be a minor adjustment to the CI workflow.

Commit 2d27eabecfb96b840e1fde42aae30b9328139111

Key changes in the patch:

Potential problems:

Suggestions:

Commit 17221d00a3a2446bd77431cc2e5ce74d0160a93d

The key changes in this pull request are:

Potential problems that can be found in this patch are:

Overall, a clearer explanation of the changes, their purpose, and their impact would be beneficial for a thorough review of this pull request.