Skip to content

Conversation

@R4ken
Copy link

@R4ken R4ken commented Sep 30, 2025

Add add webassembly-micro-runtime port

JIRA: RTOS-1087

Description

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (refactoring, style fixes, git/CI config, submodule management, no code logic changes)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: ia32-generic-qemu armv7a9-zynq700-qemu riscv64-generic-qemu aarch64a53-zynqmp-qemu

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

@gemini-code-assist
Copy link

Summary of Changes

Hello @R4ken, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new port for the WebAssembly Micro Runtime (WAMR) to the Phoenix-RTOS ecosystem. It establishes a foundational layer that allows WebAssembly modules to run on Phoenix-RTOS, complete with custom platform adaptations for file system access, threading primitives, and mathematical functions. The integration supports multiple architectures and includes configurations for resource-constrained environments, paving the way for deploying WebAssembly applications on the RTOS.

Highlights

  • WebAssembly Micro Runtime (WAMR) Port: Introduces WAMR version 2.4.2, enabling WebAssembly execution on Phoenix-RTOS, expanding the system's application capabilities.
  • Phoenix-RTOS Platform Integration: Implements a custom platform layer for WAMR, adapting its build system and core functionalities to align with Phoenix-RTOS specifics, ensuring seamless operation within the RTOS environment.
  • Cross-Architecture Support: Configures WAMR for various target architectures including ARMv7m, ARM, AARCH64, and RISCV64, broadening the range of devices that can run WebAssembly modules.
  • Custom System Call and Library Adaptations: Patches WAMR's POSIX-like interfaces for file system operations, threading primitives (semaphores, rwlocks), clock resolution, and mathematical functions to ensure compatibility with Phoenix-RTOS's unique environment.
  • Native Library Integration: Provides mechanisms and example code for linking native C functions directly into WASM modules, allowing WebAssembly applications to interact with host-specific functionalities.
  • Build System Enhancements: Modifies the main build.sh script to include WAMR and sets up a dedicated build script for WAMR with specific flags for Phoenix-RTOS, including configurations optimized for low-memory environments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@R4ken R4ken marked this pull request as draft September 30, 2025 08:16
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a port for the WebAssembly Micro Runtime (WAMR) to Phoenix-RTOS, which is a significant new feature. The changes include a build script, patches to adapt WAMR to the Phoenix-RTOS environment, and example files. The patches correctly disable features not supported on Phoenix-RTOS, such as AOT/JIT and dynamic linking, and provide workarounds for missing system calls and library functions. However, the review has identified several issues, including a critical compilation error, potential memory leaks, incorrect logic in file operations, and leftover debug code. These issues should be addressed to ensure the stability and correctness of the port.

Comment on lines 12 to 14
{
strncpy(msg, buffer, buf_len);
}

Choose a reason for hiding this comment

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

high

The use of strncpy is unsafe as it does not guarantee null-termination if the source string is as long as or longer than buf_len. This can lead to buffer overflows when the destination buffer msg is read as a string later. Additionally, the size of the destination buffer msg is not known, which is a security risk. The function signature should ideally be changed to include the destination buffer size. For a safer implementation within the current signature, you should ensure null-termination.

{
    if (buf_len > 0) {
        strncpy((char *)msg, (const char *)buffer, buf_len - 1);
        msg[buf_len - 1] = '\0';
    }
}

Copy link
Author

Choose a reason for hiding this comment

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

This code is shown as an example of usage of native libraries and is not functional part of runtime

Comment on lines +873 to +880
+double nearbyint(double x)
+{
+ return round(x);
+}
+float nearbyintf(float x)
+{
+ return round(x);
+}

Choose a reason for hiding this comment

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

high

The implementations of nearbyint and nearbyintf using round are not correct. round() rounds halfway cases away from zero and may raise the FE_INEXACT floating-point exception. In contrast, nearbyint() should round according to the current floating-point rounding mode (e.g., to nearest even) and must not raise the FE_INEXACT exception. This discrepancy can lead to subtle bugs and incorrect calculations in floating-point sensitive code.

Copy link
Author

Choose a reason for hiding this comment

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

Phoenix does not support fenv.h floating point exceptions

@github-actions
Copy link

github-actions bot commented Sep 30, 2025

Unit Test Results

9 462 tests  +16   8 873 ✅ +16   51m 18s ⏱️ -59s
  561 suites +16     589 💤 ± 0 
    1 files   ± 0       0 ❌ ± 0 

Results for commit 39b044f. ± Comparison against base commit 5eec628.

♻️ This comment has been updated with latest results.

fi

mkdir -p "${PRODUCT_MINI_DIR}"
(cd "${PRODUCT_MINI_DIR}" && cmake ${WAMR_FLAGS} .. && make)

Choose a reason for hiding this comment

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

📝 [shellcheck] reported by reviewdog 🐶
Double quote to prevent globbing and word splitting. SC2086

Copy link
Author

Choose a reason for hiding this comment

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

Double quotes preserve indentation between flags which breaks passing them to cmake

fi

mkdir -p "${PRODUCT_MINI_DIR}"
(cd "${PRODUCT_MINI_DIR}" && cmake ${WAMR_FLAGS} .. && make)

Choose a reason for hiding this comment

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

[shellcheck (suggestion)] reported by reviewdog 🐶

Suggested change
(cd "${PRODUCT_MINI_DIR}" && cmake ${WAMR_FLAGS} .. && make)
(cd "${PRODUCT_MINI_DIR}" && cmake "${WAMR_FLAGS}" .. && make)

@R4ken R4ken marked this pull request as ready for review September 30, 2025 14:18
@oI0ck oI0ck self-assigned this Nov 25, 2025
@oI0ck
Copy link
Member

oI0ck commented Nov 25, 2025

Ported runtime seems to run well, though I have some nitpicks.

Patching core/shared/platform/linux and then copying it as core/shared/platform/phoenix is a bit hacky.

Also, instead of patching core/platform/common/posix, I'd be better to implement those interfaces in Phoenix platform directory. That way the implementation should be easier to upstream later on, and, basing of what Zephyr did, it shouldn't be that much work either (~1k LoC in their directory).

@oI0ck
Copy link
Member

oI0ck commented Nov 26, 2025

Actually, since we are planning on adding missing POSIX features, we can probably leave this patchset as it is.
When they'll be ready, we can use common/posix without substantially changing them with patches.

Add add webassembly-micro-runtime port

JIRA: RTOS-1087
oI0ck
oI0ck previously approved these changes Nov 26, 2025
Copy link
Member

@oI0ck oI0ck left a comment

Choose a reason for hiding this comment

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

This pull request is a bit hacky, due to Phoenix not being fully POSIX compilant.
The patchset should probably be reworked before upstreaming in the future, however, it makes little sense to change it now, before we implement missing POSIX features.

Porting this runtime was a considerable undertaking, thank you for your work.

+/* Missing definitions */
+double nearbyint(double x);
+float nearbyintf(float x);
+#define O_ACCMODE 3 // Mask for accessing R/W access mode
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
+#define O_ACCMODE 3 // Mask for accessing R/W access mode

O_ACCMODE is already defined in fcntl.h. Moreover its value here is incorrect.

#define O_RDONLY   0x00001U
#define O_WRONLY   0x00002U
#define O_RDWR     0x00004U
#define O_APPEND   0x00008U
#define O_CREAT    0x00100U
#define O_TRUNC    0x00200U
#define O_EXCL     0x00400U
#define O_SYNC     0x00800U
#define O_NONBLOCK 0x01000U
#define O_NDELAY   O_NONBLOCK
#define O_NOCTTY   0x02000U
#define O_CLOEXEC  0x04000U
#define O_RSYNC    0x08000U
#define O_DSYNC    0x10000U

#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)

That'd make O_ACCMODE resolve to 7, not 3

Copy link
Member

Choose a reason for hiding this comment

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

Fixed this in latest push

@oI0ck
Copy link
Member

oI0ck commented Nov 26, 2025

Merge after phoenix-rtos/libphoenix#433

@oI0ck oI0ck requested a review from Darchiv November 26, 2025 13:50
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.

3 participants