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

Setup Wasm test case #4

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# All new code should follow Webkit style guide

BasedOnStyle: WebKit
SortIncludes: false
35 changes: 16 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# # Wasm Module
# Introduction

[English | [简体中文](./README_zh-cn.md)]
[English|[简体中文](./README_zh-cn.md)]

The openvela Wasm framework provides additional modules and SDKs to build `WebAssembly` applications.
The Vela Wasm Framework provides extra modules and SDKs to build WebAssembly applications.

## Project Structure
# Project Structure

```tree
├── chre
Expand All @@ -16,24 +16,21 @@ The openvela Wasm framework provides additional modules and SDKs to build `WebAs
└── README_zh-cn.md
```

* `chre`: This module provides functionality from the Context Hub Runtime Environment to the Wasm runtime.
* `libc`: C standard library for Wasm.
* `memory`: Memory access management between Wasm and native code.
* `vela-sysroot`: A collection of additional header files for building WebAssembly applications.
* `vendor`: Wasm application SDKs for third-party chip vendors.
* `chre`: This module provides the function from Context Hub Runtime Environment to Wasm runtime
* `libc`: The C standard library for Wasm
* `memory`: Memory aceess management between wasm and native code
* `test`: Unit test cases for each module
* `vela-sysroot`: A collection of extra header files to build WebAssembly applications
* `vendor`: The Wasm application SDKs for 3rd party chip vendor

Choose a reason for hiding this comment

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

let's creae a new patch to change vela-sysroot to sysroot


# Usage

## How to use
## Function module for Wasm runtime

### Wasm runtime function module
Only WAMR (WebAssembly Micro Runtime) is supported now. To use the function module, you need to enable WAMR firstly in menuconfig, and then enable `WASM_LIBC` (libc) or `WASM_MEMORY_OPERATIONS` (memory) in the corresponding submenu.

Currently only WAMR (WebAssembly Micro Runtime) is supported. If you want to use this function module:
1. Enable `WAMR` in `menuconfig`.
And then the function from libc or memory will be available in `iwasm` command line tool, and can be used directly in Wasm applications.

2. Enable `WASM_LIBC` (libc) or `WASM_MEMORY_OPERATIONS` (memory) in the corresponding submenu.
## Build Wasm application by SDKs

3. The functions of libc or memory are available in the `iwasm` command line tool and can be used directly in Wasm applications.

### Build Wasm applications using SDK

The `vendor` directory provides a collection of SDKs for third-party chip vendors. Currently, only BL616 from `bouffalo lab` is supported. To build applications using the SDK, you can refer to the following document: [BL616 SDK](./vendor/bouffalo/README.md)
The `vendor` directory provides a collection of SDKs for 3rd party chip vendors, only bouffalo lab's BL616 is supported now. To build an applicaiton using the SDK, you can refer to the following documntation: [BL616 SDK](./vendor/bouffalo/README.md)
30 changes: 15 additions & 15 deletions README_zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Wasm 模块
# 介绍

[简体中文|[English](./README.md)]

Openvela Wasm 框架提供了额外的模块和 SDK 来构建 `WebAssembly` 应用程序。
Vela Wasm 框架提供了额外的模块和 SDK 来构建 WebAssembly 应用程序。

## 项目结构
# 项目结构

```tree
├── chre
Expand All @@ -16,21 +16,21 @@ Openvela Wasm 框架提供了额外的模块和 SDK 来构建 `WebAssembly` 应
└── README_zh-cn.md
```

* `chre`该模块提供了从 Context Hub Runtime Environment 到 Wasm 运行时的功能
* `libc`: 用于 Wasm 的 C 标准库
* `memory`: Wasm 和原生代码之间的内存访问管理
* `vela-sysroot`: 用于构建 WebAssembly 应用程序的额外头文件集合
* `vendor`: 第三方芯片供应商的 Wasm 应用程序 SDK
* `chre`: 该模块提供了从 Context Hub Runtime Environment 到 Wasm 运行时的功能
* `libc`: 用于 Wasm 的 C 标准库
* `memory`: Wasm 和原生代码之间的内存访问管理
* `vela-sysroot`: 用于构建 WebAssembly 应用程序的额外头文件集合
* `vendor`: 第三方芯片供应商的 Wasm 应用程序 SDK

Choose a reason for hiding this comment

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

ditto


## 如何使用
# 如何使用

### Wasm 运行时的功能模块
## Wasm 运行时的功能模块

目前仅支持 WAMR(WebAssembly Micro Runtime),您如果要使用该功能模块:
1. 在 `menuconfig` 中启用 `WAMR`
2. 在相应的子菜单中启用 `WASM_LIBC`(libc)或 `WASM_MEMORY_OPERATIONS`(memory)。
3. libc 或 memory 的功能可在 `iwasm` 命令行工具中使用,并可以直接在 Wasm 应用程序中使用。
1. 在 menuconfig 中启用 WAMR。
2. 在相应的子菜单中启用 `WASM_LIBC`(libc)或 `WASM_MEMORY_OPERATIONS`(内存)。
3. libc 或 memory 的功能将在 `iwasm` 命令行工具中可用,并可以直接在 Wasm 应用程序中使用。

### 使用 SDK 构建 Wasm 应用程序
## 使用 SDK 构建 Wasm 应用程序

`vendor` 目录提供了第三方芯片供应商的 SDK 集合,目前仅支持 `bouffalo lab` 的 BL616。要使用 SDK 构建应用程序,您可以参考以下文档:[BL616 SDK](./vendor/bouffalo/README.md)
`vendor` 目录提供了第三方芯片供应商的 SDK 集合,目前仅支持 bouffalo lab 的 BL616。要使用 SDK 构建应用程序,您可以参考以下文档:[BL616 SDK](./vendor/bouffalo/README.md)
26 changes: 26 additions & 0 deletions kvdb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ##############################################################################
# apps/frameworks/chre/CMakeLists.txt
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################

if(CONFIG_KVDB AND CONFIG_INTERPRETERS_WAMR)

target_sources(apps PRIVATE kvdb.c)
# register WAMR mod
nuttx_add_wamrmod(MODS kvdb)
endif()
23 changes: 23 additions & 0 deletions kvdb/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/frameworks/runtimes/wasm/chre/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################

ifeq ($(CONFIG_KVDB)$(CONFIG_INTERPRETERS_WAMR),yy)
CONFIGURED_APPS += $(APPDIR)/frameworks/runtimes/wasm/kvdb
endif
35 changes: 35 additions & 0 deletions kvdb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
############################################################################
# apps/frameworks/wasm/chre/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################

include $(APPDIR)/Make.defs

CSRCS += kvdb.c

# To delete the Kconfig file, an empty preconfig needs to be added.
preconfig::

# Set WAMR_MODULE_NAME and include Module.mk to add this module to the list of
# builtin modules for the WAMR runtime. WAMR_MODULE_NAME must be unique across all
# modules in system.

WAMR_MODULE_NAME = kvdb

include $(APPDIR)/interpreters/wamr/Module.mk
include $(APPDIR)/Application.mk
53 changes: 53 additions & 0 deletions kvdb/kvdb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024 Xiaomi Corperation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/****************************************************************************
* Included Files
****************************************************************************/

#include <stdint.h>
#include <sys/param.h>

#include "kvdb.h"
#include "wasm_export.h"

/****************************************************************************
* Private Functions
****************************************************************************/

static int property_set_buffer_wrapper(wasm_exec_env_t env,
const char* key,
const void* value,
size_t size)
{
return property_set_buffer(key, value, size);
}

static NativeSymbol g_kvdb_symbols[] = {
EXPORT_WASM_API_WITH_SIG2(property_set_buffer, "($*i)i"),
};

/****************************************************************************
* Public Functions
****************************************************************************/

bool wamr_module_kvdb_register(void)
{
/* Add frameworks section init hook here */

return wasm_runtime_register_natives("env", g_kvdb_symbols,
nitems(g_kvdb_symbols));
}
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated
32 changes: 32 additions & 0 deletions test/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# Copyright (C) 2024 Xiaomi Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

menu "Test"

config WASM_TEST_PRIORITY
int "task priority"
default 100

config WASM_TEST_STACKSIZE
int "stack size"
default DEFAULT_TASK_STACKSIZE

config WASM_TEST_LIBC
bool "Enable libc test"
default n
depends on WASM_LIBC && TESTING_CMOCKA

endmenu
19 changes: 19 additions & 0 deletions test/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (C) 2024 Xiaomi Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

ifneq ($(CONFIG_INTERPRETERS_WAMR),)
CONFIGURED_APPS += $(APPDIR)/frameworks/runtimes/wasm/test

Choose a reason for hiding this comment

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

should we add ifneq/endif

endif
51 changes: 51 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright (C) 2024 Xiaomi Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

include $(APPDIR)/Make.defs

PRIORITY = $(CONFIG_WASM_TEST_PRIORITY)
STACKSIZE = $(CONFIG_WASM_TEST_STACKSIZE)

ifneq ($(CONFIG_WASM_TEST_LIBC),)
PROGNAME += cmocka_wasm_test_libc
MAINSRC += libc_test.c
endif

# If any test case enabled, then enable the test module

ifneq ($(PROGNAME),)
ifneq ($(MAINSRC),)
MODULE = y
endif
endif

generated:
mkdir -p generated

# Rule to compile the libc proxy module

generated/libc.wasm: proxy/libc.c generated
$(WASI_SDK_PATH)/bin/clang --target=wasm32 -O3 -flto --sysroot=$(APPDIR)/frameworks/runtimes/wasm/vela-sysroot -nostdlib -z stack-size=4096 -Wl,--initial-memory=65536 -o generated/libc.wasm proxy/libc.c -Wl,--no-entry -Wl,--export-all -Wl,--allow-undefined

generated/libc_wasm_array.c: generated/libc.wasm
xxd -i $< > $@

context:: generated/libc_wasm_array.c

distclean::
$(call DELDIR, generated)

include $(APPDIR)/Application.mk
34 changes: 34 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Introduction

This directory contains the test cases for the libc API provided by Wasm runtimes.

# Requirements

1. **Test Cases**: The test cases should cover all the built-in libc functions of WAMR and addons implemented by ourselves.
2. **Framework**: We should use CMocka as the test framework.
3. **Execution**: Should have a shell command in nsh to run all the test cases.

# Design

For this task, we should call the Wasm runtime API to call libc functions and compare the result with expected value. But we can not directly call the internal function of Wasm runtime because it is not exposed directly.
So we need a proxy module that could be able to expose the internal APIs of Wasm runtime to us.

# Implementation

In order to test the API from https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c, we need to create a proxy module that contains some proxy functions like this:
```c
void *malloc_proxy(size_t size) {
return malloc(size);
}
```

Then we can call the malloc function in Wasm runtime through `malloc_proxy` and compare the result with expected value in native code.

# Proxy Module

The source code of proxy module is located at: proxy/libc.c

You can use this command to build the proxy module:
```sh
$(WASI_SDK_PATH)/bin/clang --target=wasm32 -O3 -flto --sysroot=PWD/frameworks/runtimes/wasm/vela-sysroot -nostdlib -z stack-size=4096 -Wl,--initial-memory=65536 -o proxy/libc.wasm proxy/libc.c -Wl,--no-entry -Wl,--export-all
```
Loading
Loading