Skip to content

Commit 6e727dc

Browse files
authoredJul 26, 2024
Support more features for rt-thread (bytecodealliance#3661)
1, enable thread mgr 2, enable libc wasi 3, enable libc wasi threads 4, specify a function name of the module to run rather main
1 parent 4dfdbbb commit 6e727dc

File tree

15 files changed

+1277
-89
lines changed

15 files changed

+1277
-89
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*.so
1313
.clangd
1414
.DS_Store
15+
*.o
1516

1617
core/deps/**
1718
core/shared/mem-alloc/tlsf
@@ -37,4 +38,4 @@ tests/benchmarks/coremark/coremark*
3738
samples/workload/include/**
3839
!samples/workload/include/.gitkeep
3940

40-
# core/iwasm/libraries/wasi-threads
41+
# core/iwasm/libraries/wasi-threads

‎build-scripts/SConscript

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ if GetDepend(['WAMR_BUILD_LIBC_BUILTIN']):
3131

3232
if GetDepend(['WAMR_BUILD_LIBC_WASI']):
3333
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-wasi', 'SConscript'))
34+
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'common', 'posix', 'SConscript'))
35+
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'common', 'libc-util', 'SConscript'))
3436

3537
if GetDepend(['WAMR_BUILD_LIB_PTHREAD']):
36-
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-pthread', 'SConscript'))
38+
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'lib-pthread', 'SConscript'))
3739

3840
if GetDepend(['WAMR_BUILD_THREAD_MGR']):
3941
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'thread-mgr', 'SConscript'))
4042

4143
if GetDepend(['WAMR_BUILD_LIBC_EMCC']):
4244
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-emmc', 'SConscript'))
4345

46+
if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
47+
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'lib-wasi-threads', 'SConscript'))
48+
4449
objs += SConscript(os.path.join(cwd, 'SConscript_config'));
4550

4651
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'rt-thread', 'SConscript'))

‎build-scripts/SConscript_config

+21-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,27 @@ if GetDepend(['WAMR_BUILD_CUSTOM_NAME_SECTION']):
109109

110110
if GetDepend(['WAMR_BUILD_TAIL_CALL']):
111111
CPPDEFINES += ['WASM_ENABLE_TAIL_CALL=1']
112-
print('[WAMR] Tail call enabledd')
112+
print('[WAMR] Tail call enabled')
113+
114+
if GetDepend(['WAMR_BUILD_THREAD_MGR']):
115+
CPPDEFINES += ['WASM_ENABLE_THREAD_MGR=1']
116+
print('[WAMR] Thread manager enabled')
117+
118+
if GetDepend(['WAMR_BUILD_LIBC_WASI']):
119+
CPPDEFINES += ['WASM_ENABLE_LIBC_WASI=1']
120+
CPPDEFINES += ['WASM_ENABLE_MODULE_INST_CONTEXT=1']
121+
print('[WAMR] Libc wasi enabled')
122+
123+
if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
124+
CPPDEFINES += ['WASM_ENABLE_LIB_WASI_THREADS=1']
125+
print('[WAMR] Lib wasi threads enabled')
126+
127+
if GetDepend(['WAMR_BUILD_REF_TYPES']):
128+
CPPDEFINES += ['WASM_ENABLE_REF_TYPES=1']
129+
print('[WAMR] enable ref types')
130+
131+
CPPDEFINES += ['BH_MALLOC=wasm_runtime_malloc']
132+
CPPDEFINES += ['BH_FREE=wasm_runtime_free']
113133

114134
LIBS = ['m']
115135

‎core/iwasm/libraries/lib-pthread/SConscript

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ from building import *
99
cwd = GetCurrentDir()
1010

1111
src = Split('''
12-
libc_pthread_wrapper.c
12+
lib_pthread_wrapper.c
1313
''')
1414

1515
CPPPATH = [cwd]
1616

1717

18-
group = DefineGroup('iwasm_libc_pthread', src, depend = [''], CPPPATH = CPPPATH)
18+
group = DefineGroup('iwasm_lib_pthread', src, depend = [''], CPPPATH = CPPPATH)
1919

2020
Return('group')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
9+
cwd = GetCurrentDir()
10+
src = Glob('*.c')
11+
CPPPATH = [cwd]
12+
13+
group = DefineGroup('iwasm_lib_wasi_threads', src, depend = [''], CPPPATH = CPPPATH)
14+
15+
Return('group')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
import re
9+
10+
Import('rtconfig')
11+
12+
cwd = GetCurrentDir()
13+
src = Split('''
14+
libc_errno.c
15+
''')
16+
CPPPATH = [cwd]
17+
18+
group = DefineGroup('iwasm_libc_util', src, depend = [''], CPPPATH = CPPPATH)
19+
20+
Return('group')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
import re
9+
10+
Import('rtconfig')
11+
12+
cwd = GetCurrentDir()
13+
src = Split('''
14+
posix_file.c
15+
''')
16+
CPPPATH = [cwd]
17+
18+
group = DefineGroup('iwasm_common_posix', src, depend = [''], CPPPATH = CPPPATH)
19+
20+
Return('group')

‎core/shared/platform/common/posix/posix_file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* (platform_internal.h)
2727
*/
2828
#if !defined(CONFIG_HAS_D_INO)
29-
#if !defined(__NuttX__)
29+
#if !defined(__NuttX__) && !defined(__RTTHREAD__)
3030
#define CONFIG_HAS_D_INO 1
3131
#define CONFIG_HAS_ISATTY 1
3232
#else

‎core/shared/platform/common/posix/posix_thread.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55

66
#ifndef _GNU_SOURCE
7+
#if !defined(__RTTHREAD__)
78
#define _GNU_SOURCE
89
#endif
10+
#endif
911
#include "platform_api_vmcore.h"
1012
#include "platform_api_extension.h"
1113

@@ -448,7 +450,7 @@ os_thread_get_stack_boundary()
448450
addr += guard_size;
449451
}
450452
(void)stack_size;
451-
#elif defined(__APPLE__) || defined(__NuttX__)
453+
#elif defined(__APPLE__) || defined(__NuttX__) || defined(__RTTHREAD__)
452454
if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
453455
stack_size = pthread_get_stacksize_np(self);
454456

‎core/shared/platform/rt-thread/platform_internal.h

+66
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
#ifndef RTTHREAD_PLATFORM_INTERNAL_H
88
#define RTTHREAD_PLATFORM_INTERNAL_H
99

10+
#include <sys/ioctl.h>
11+
#include <fcntl.h>
12+
#include <limits.h>
13+
#include <errno.h>
14+
#include <poll.h>
15+
#if defined(RT_USING_PTHREADS)
16+
#include <pthread.h>
17+
#else
1018
#include <rtthread.h>
19+
#endif
1120
#include <stdbool.h>
1221
#include <string.h>
1322
#include <stdio.h>
1423
#include <stdlib.h>
1524
#include <math.h>
1625
#include <stdint.h>
1726
#include <ctype.h>
27+
#include <dirent.h>
28+
#include <assert.h>
1829

1930
#if defined(WASM_ENABLE_AOT)
2031
#if defined(RTT_WAMR_BUILD_TARGET_THUMB)
@@ -32,12 +43,67 @@
3243
#endif
3344
#endif /* WASM_ENABLE_AOT */
3445

46+
/* Use rt-thread's definition as default */
47+
#if 0 // defined(RT_USING_PTHREADS)
48+
typedef pthread_t korp_tid;
49+
typedef pthread_mutex_t korp_mutex;
50+
typedef pthread_cond_t korp_cond;
51+
typedef pthread_t korp_thread;
52+
#else
3553
typedef rt_thread_t korp_tid;
3654
typedef struct rt_mutex korp_mutex;
3755
typedef struct rt_thread korp_cond;
3856
typedef struct rt_thread korp_thread;
57+
#endif
3958
typedef unsigned int korp_sem;
4059

60+
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
61+
typedef uint32_t socklen_t;
62+
#endif
63+
64+
#if !defined(SOL_SOCKET)
65+
#define SOL_SOCKET 1
66+
#endif
67+
68+
#if !defined(SO_TYPE)
69+
#define SO_TYPE 3
70+
#endif
71+
72+
#if !defined(SOCK_DGRAM)
73+
#define SOCK_DGRAM 2
74+
#endif
75+
76+
#if !defined(SOCK_STREAM)
77+
#define SOCK_STREAM 1
78+
#endif
79+
80+
#if !defined(UTIME_NOW)
81+
#define UTIME_NOW -2L
82+
#endif
83+
84+
#if !defined(UTIME_OMIT)
85+
#define UTIME_OMIT -1L
86+
#endif
87+
88+
#if !defined(AT_SYMLINK_NOFOLLOW)
89+
#define AT_SYMLINK_NOFOLLOW 2
90+
#endif
91+
92+
#if !defined(AT_SYMLINK_FOLLOW)
93+
#define AT_SYMLINK_FOLLOW 4
94+
#endif
95+
96+
#if !defined(AT_REMOVEDIR)
97+
#define AT_REMOVEDIR 8
98+
#endif
99+
100+
#define DT_BLK 0x06
101+
#define DT_CHR 0x02
102+
#define DT_LNK 0x0A
103+
104+
#define PTHREAD_STACK_MIN 1024
105+
#define BH_THREAD_DEFAULT_PRIORITY 30
106+
41107
/* korp_rwlock is used in platform_api_extension.h,
42108
we just define the type to make the compiler happy */
43109
typedef struct {
+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
*/
6+
7+
#include "platform_api_vmcore.h"
8+
#include "platform_api_extension.h"
9+
10+
#include <errno.h>
11+
#include <stdio.h>
12+
#include <stddef.h>
13+
#include <fcntl.h>
14+
#include <stdint.h>
15+
16+
struct iovec {
17+
void *iov_base;
18+
size_t iov_len;
19+
};
20+
21+
ssize_t
22+
readv(int fd, const struct iovec *iov, int iovcnt)
23+
{
24+
ssize_t ntotal;
25+
ssize_t nread;
26+
size_t remaining;
27+
uint8_t *buffer;
28+
int i;
29+
30+
/* Process each entry in the struct iovec array */
31+
32+
for (i = 0, ntotal = 0; i < iovcnt; i++) {
33+
/* Ignore zero-length reads */
34+
35+
if (iov[i].iov_len > 0) {
36+
buffer = iov[i].iov_base;
37+
remaining = iov[i].iov_len;
38+
39+
/* Read repeatedly as necessary to fill buffer */
40+
41+
do {
42+
/* NOTE: read() is a cancellation point */
43+
44+
nread = read(fd, buffer, remaining);
45+
46+
/* Check for a read error */
47+
48+
if (nread < 0) {
49+
return nread;
50+
}
51+
52+
/* Check for an end-of-file condition */
53+
54+
else if (nread == 0) {
55+
return ntotal;
56+
}
57+
58+
/* Update pointers and counts in order to handle partial
59+
* buffer reads.
60+
*/
61+
62+
buffer += nread;
63+
remaining -= nread;
64+
ntotal += nread;
65+
} while (remaining > 0);
66+
}
67+
}
68+
69+
return ntotal;
70+
}
71+
72+
ssize_t
73+
writev(int fd, const struct iovec *iov, int iovcnt)
74+
{
75+
uint16_t i, num;
76+
int length;
77+
78+
num = 0;
79+
for (i = 0; i < iovcnt; i++) {
80+
if (iov[i].iov_len > 0) {
81+
length = write(fd, iov[i].iov_base, iov[i].iov_len);
82+
if (length != iov[i].iov_len)
83+
return errno;
84+
85+
num += iov[i].iov_len;
86+
}
87+
}
88+
return num;
89+
}
90+
91+
int
92+
fstatat(int fd, const char *path, struct stat *buf, int flag)
93+
{
94+
errno = ENOSYS;
95+
return -1;
96+
}
97+
98+
int
99+
mkdirat(int fd, const char *path, mode_t mode)
100+
{
101+
errno = ENOSYS;
102+
return -1;
103+
}
104+
105+
ssize_t
106+
readlinkat(int fd, const char *path, char *buf, size_t bufsize)
107+
{
108+
errno = EINVAL;
109+
return -1;
110+
}
111+
112+
int
113+
linkat(int fd1, const char *path1, int fd2, const char *path2, int flag)
114+
{
115+
errno = ENOSYS;
116+
return -1;
117+
}
118+
119+
int
120+
renameat(int fromfd, const char *from, int tofd, const char *to)
121+
{
122+
errno = ENOSYS;
123+
return -1;
124+
}
125+
126+
int
127+
symlinkat(const char *target, int fd, const char *path)
128+
{
129+
errno = ENOSYS;
130+
return -1;
131+
}
132+
133+
int
134+
unlinkat(int fd, const char *path, int flag)
135+
{
136+
errno = ENOSYS;
137+
return -1;
138+
}
139+
140+
int
141+
utimensat(int fd, const char *path, const struct timespec *ts, int flag)
142+
{
143+
errno = ENOSYS;
144+
return -1;
145+
}
146+
147+
DIR *
148+
fdopendir(int fd)
149+
{
150+
errno = ENOSYS;
151+
return NULL;
152+
}
153+
154+
int
155+
fdatasync(int fd)
156+
{
157+
errno = ENOSYS;
158+
return -1;
159+
}
160+
161+
ssize_t
162+
preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
163+
{
164+
errno = ENOSYS;
165+
return 0;
166+
}
167+
168+
ssize_t
169+
pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
170+
{
171+
errno = ENOSYS;
172+
return 0;
173+
}
174+
175+
char *
176+
realpath(char *path, char *resolved_path)
177+
{
178+
errno = ENOSYS;
179+
return NULL;
180+
}
181+
182+
int
183+
futimens(int fd, const struct timespec *times)
184+
{
185+
errno = ENOSYS;
186+
return -1;
187+
}
188+
189+
int
190+
posix_fallocate(int __fd, off_t __offset, off_t __length)
191+
{
192+
errno = ENOSYS;
193+
return -1;
194+
}

‎core/shared/platform/rt-thread/rtt_platform.c

+45-62
Original file line numberDiff line numberDiff line change
@@ -134,96 +134,79 @@ os_time_thread_cputime_us(void)
134134
return os_time_get_boot_us();
135135
}
136136

137-
korp_tid
138-
os_self_thread(void)
139-
{
140-
return rt_thread_self();
141-
}
142-
143-
uint8 *
144-
os_thread_get_stack_boundary(void)
137+
void *
138+
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
145139
{
146-
rt_thread_t tid = rt_thread_self();
147-
return tid->stack_addr;
148-
}
140+
void *buf_origin;
141+
void *buf_fixed;
142+
rt_ubase_t *addr_field;
149143

150-
void
151-
os_thread_jit_write_protect_np(bool enabled)
152-
{}
144+
buf_origin = rt_malloc(size + 8 + sizeof(rt_ubase_t));
145+
if (!buf_origin)
146+
return NULL;
153147

154-
int
155-
os_mutex_init(korp_mutex *mutex)
156-
{
157-
return rt_mutex_init(mutex, "wamr0", RT_IPC_FLAG_FIFO);
158-
}
148+
buf_fixed = buf_origin + sizeof(void *);
149+
if ((rt_ubase_t)buf_fixed & 0x7) {
150+
buf_fixed = (void *)((rt_ubase_t)(buf_fixed + 8) & (~7));
151+
}
159152

160-
int
161-
os_mutex_destroy(korp_mutex *mutex)
162-
{
163-
return rt_mutex_detach(mutex);
164-
}
153+
addr_field = buf_fixed - sizeof(rt_ubase_t);
154+
*addr_field = (rt_ubase_t)buf_origin;
165155

166-
int
167-
os_mutex_lock(korp_mutex *mutex)
168-
{
169-
return rt_mutex_take(mutex, RT_WAITING_FOREVER);
156+
memset(buf_origin, 0, size + 8 + sizeof(rt_ubase_t));
157+
return buf_fixed;
170158
}
171159

172-
int
173-
os_mutex_unlock(korp_mutex *mutex)
160+
void
161+
os_munmap(void *addr, size_t size)
174162
{
175-
return rt_mutex_release(mutex);
176-
}
163+
void *mem_origin;
164+
rt_ubase_t *addr_field;
177165

178-
/*
179-
* functions below was not implement
180-
*/
166+
if (addr) {
167+
addr_field = addr - sizeof(rt_ubase_t);
168+
mem_origin = (void *)(*addr_field);
181169

182-
int
183-
os_cond_init(korp_cond *cond)
184-
{
185-
return 0;
170+
rt_free(mem_origin);
171+
}
186172
}
187173

188174
int
189-
os_cond_destroy(korp_cond *cond)
175+
os_mprotect(void *addr, size_t size, int prot)
190176
{
191177
return 0;
192178
}
193179

180+
void
181+
os_dcache_flush(void)
182+
{}
183+
184+
void
185+
os_icache_flush(void *start, size_t len)
186+
{}
187+
194188
int
195-
os_cond_wait(korp_cond *cond, korp_mutex *mutex)
189+
os_getpagesize(void)
196190
{
197-
return 0;
191+
return 4096;
198192
}
199193

200194
void *
201-
os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
195+
os_mremap(void *in, size_t old_size, size_t new_size)
202196
{
203-
void *addr;
204-
205-
if ((addr = rt_malloc(size)))
206-
memset(addr, 0, size);
207-
208-
return addr;
197+
return os_realloc(in, new_size);
209198
}
210199

211-
void
212-
os_munmap(void *addr, size_t size)
200+
__wasi_errno_t
201+
os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
202+
__wasi_timestamp_t *time)
213203
{
214-
rt_free(addr);
204+
*time = rt_tick_get() * 1000ll * 1000ll;
205+
return 0;
215206
}
216207

217-
int
218-
os_mprotect(void *addr, size_t size, int prot)
208+
__wasi_errno_t
209+
os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution)
219210
{
220211
return 0;
221212
}
222-
223-
void
224-
os_dcache_flush(void)
225-
{}
226-
227-
void
228-
os_icache_flush(void *start, size_t len)
229-
{}
+385
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,385 @@
1+
/*
2+
* Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
*/
6+
7+
#include "platform_api_vmcore.h"
8+
#include "platform_api_extension.h"
9+
10+
int
11+
os_socket_accept(bh_socket_t server_sock, bh_socket_t *sock, void *addr,
12+
unsigned int *addrlen)
13+
{
14+
return BHT_ERROR;
15+
}
16+
17+
int
18+
os_socket_connect(bh_socket_t socket, const char *addr, int port)
19+
{
20+
return BHT_ERROR;
21+
}
22+
23+
int
24+
os_socket_recv_from(bh_socket_t socket, void *buf, unsigned int len, int flags,
25+
bh_sockaddr_t *src_addr)
26+
{
27+
return BHT_ERROR;
28+
}
29+
30+
int
31+
os_socket_send_to(bh_socket_t socket, const void *buf, unsigned int len,
32+
int flags, const bh_sockaddr_t *dest_addr)
33+
{
34+
return BHT_ERROR;
35+
}
36+
37+
int
38+
os_socket_addr_resolve(const char *host, const char *service,
39+
uint8_t *hint_is_tcp, uint8_t *hint_is_ipv4,
40+
bh_addr_info_t *addr_info, size_t addr_info_size,
41+
size_t *max_info_size)
42+
{
43+
return BHT_ERROR;
44+
}
45+
46+
int
47+
os_socket_close(bh_socket_t socket)
48+
{
49+
return BHT_ERROR;
50+
}
51+
52+
int
53+
os_socket_addr_local(bh_socket_t socket, bh_sockaddr_t *sockaddr)
54+
{
55+
return BHT_ERROR;
56+
}
57+
58+
int
59+
os_socket_addr_remote(bh_socket_t socket, bh_sockaddr_t *sockaddr)
60+
{
61+
return BHT_ERROR;
62+
}
63+
64+
int
65+
os_socket_bind(bh_socket_t socket, const char *host, int *port)
66+
{
67+
return BHT_ERROR;
68+
}
69+
70+
int
71+
os_socket_listen(bh_socket_t socket, int max_client)
72+
{
73+
return BHT_ERROR;
74+
}
75+
76+
int
77+
os_socket_create(bh_socket_t *sock, bool is_ipv4, bool is_tcp)
78+
{
79+
return BHT_ERROR;
80+
}
81+
82+
int
83+
os_socket_send(bh_socket_t socket, const void *buf, unsigned int len)
84+
{
85+
return BHT_ERROR;
86+
}
87+
88+
__wasi_errno_t
89+
os_socket_shutdown(bh_socket_t socket)
90+
{
91+
return __WASI_ENOSYS;
92+
}
93+
94+
int
95+
os_socket_inet_network(bool is_ipv4, const char *cp, bh_ip_addr_buffer_t *out)
96+
{
97+
return BHT_ERROR;
98+
}
99+
100+
int
101+
os_socket_set_send_timeout(bh_socket_t socket, uint64 timeout_us)
102+
{
103+
return BHT_ERROR;
104+
}
105+
106+
int
107+
os_socket_get_recv_timeout(bh_socket_t socket, uint64 *timeout_us)
108+
{
109+
return BHT_ERROR;
110+
}
111+
112+
int
113+
os_socket_set_send_buf_size(bh_socket_t socket, size_t bufsiz)
114+
{
115+
return BHT_ERROR;
116+
}
117+
118+
int
119+
os_socket_get_send_buf_size(bh_socket_t socket, size_t *bufsiz)
120+
{
121+
return BHT_ERROR;
122+
}
123+
124+
int
125+
os_socket_set_recv_buf_size(bh_socket_t socket, size_t bufsiz)
126+
{
127+
return BHT_ERROR;
128+
}
129+
130+
int
131+
os_socket_get_recv_buf_size(bh_socket_t socket, size_t *bufsiz)
132+
{
133+
return BHT_ERROR;
134+
}
135+
136+
int
137+
os_socket_set_broadcast(bh_socket_t socket, bool is_enabled)
138+
{
139+
return BHT_ERROR;
140+
}
141+
142+
int
143+
os_socket_get_broadcast(bh_socket_t socket, bool *is_enabled)
144+
{
145+
return BHT_ERROR;
146+
}
147+
148+
int
149+
os_socket_get_send_timeout(bh_socket_t socket, uint64 *timeout_us)
150+
{
151+
return BHT_ERROR;
152+
}
153+
154+
int
155+
os_socket_set_recv_timeout(bh_socket_t socket, uint64 timeout_us)
156+
{
157+
return BHT_ERROR;
158+
}
159+
160+
int
161+
os_socket_set_keep_alive(bh_socket_t socket, bool is_enabled)
162+
{
163+
return BHT_ERROR;
164+
}
165+
166+
int
167+
os_socket_get_keep_alive(bh_socket_t socket, bool *is_enabled)
168+
{
169+
return BHT_ERROR;
170+
}
171+
172+
int
173+
os_socket_set_reuse_addr(bh_socket_t socket, bool is_enabled)
174+
{
175+
return BHT_ERROR;
176+
}
177+
178+
int
179+
os_socket_get_reuse_addr(bh_socket_t socket, bool *is_enabled)
180+
{
181+
return BHT_ERROR;
182+
}
183+
184+
int
185+
os_socket_set_reuse_port(bh_socket_t socket, bool is_enabled)
186+
{
187+
return BHT_ERROR;
188+
}
189+
190+
int
191+
os_socket_get_reuse_port(bh_socket_t socket, bool *is_enabled)
192+
{
193+
return BHT_ERROR;
194+
}
195+
196+
int
197+
os_socket_set_linger(bh_socket_t socket, bool is_enabled, int linger_s)
198+
{
199+
return BHT_ERROR;
200+
}
201+
202+
int
203+
os_socket_get_linger(bh_socket_t socket, bool *is_enabled, int *linger_s)
204+
{
205+
return BHT_ERROR;
206+
}
207+
208+
int
209+
os_socket_set_tcp_no_delay(bh_socket_t socket, bool is_enabled)
210+
{
211+
return BHT_ERROR;
212+
}
213+
214+
int
215+
os_socket_get_tcp_no_delay(bh_socket_t socket, bool *is_enabled)
216+
{
217+
return BHT_ERROR;
218+
}
219+
220+
int
221+
os_socket_set_tcp_quick_ack(bh_socket_t socket, bool is_enabled)
222+
{
223+
return BHT_ERROR;
224+
}
225+
226+
int
227+
os_socket_get_tcp_quick_ack(bh_socket_t socket, bool *is_enabled)
228+
{
229+
return BHT_ERROR;
230+
}
231+
232+
int
233+
os_socket_set_tcp_keep_idle(bh_socket_t socket, uint32 time_s)
234+
{
235+
return BHT_ERROR;
236+
}
237+
238+
int
239+
os_socket_get_tcp_keep_idle(bh_socket_t socket, uint32 *time_s)
240+
{
241+
return BHT_ERROR;
242+
}
243+
244+
int
245+
os_socket_set_tcp_keep_intvl(bh_socket_t socket, uint32 time_s)
246+
{
247+
return BHT_ERROR;
248+
}
249+
250+
int
251+
os_socket_get_tcp_keep_intvl(bh_socket_t socket, uint32 *time_s)
252+
{
253+
return BHT_ERROR;
254+
}
255+
256+
int
257+
os_socket_set_tcp_fastopen_connect(bh_socket_t socket, bool is_enabled)
258+
{
259+
return BHT_ERROR;
260+
}
261+
262+
int
263+
os_socket_get_tcp_fastopen_connect(bh_socket_t socket, bool *is_enabled)
264+
{
265+
return BHT_ERROR;
266+
}
267+
268+
int
269+
os_socket_set_ip_multicast_loop(bh_socket_t socket, bool ipv6, bool is_enabled)
270+
{
271+
return BHT_ERROR;
272+
}
273+
274+
int
275+
os_socket_get_ip_multicast_loop(bh_socket_t socket, bool ipv6, bool *is_enabled)
276+
{
277+
return BHT_ERROR;
278+
}
279+
280+
int
281+
os_socket_set_ip_add_membership(bh_socket_t socket,
282+
bh_ip_addr_buffer_t *imr_multiaddr,
283+
uint32_t imr_interface, bool is_ipv6)
284+
{
285+
return BHT_ERROR;
286+
}
287+
288+
int
289+
os_socket_set_ip_drop_membership(bh_socket_t socket,
290+
bh_ip_addr_buffer_t *imr_multiaddr,
291+
uint32_t imr_interface, bool is_ipv6)
292+
{
293+
return BHT_ERROR;
294+
}
295+
296+
int
297+
os_socket_set_ip_ttl(bh_socket_t socket, uint8_t ttl_s)
298+
{
299+
return BHT_ERROR;
300+
}
301+
302+
int
303+
os_socket_get_ip_ttl(bh_socket_t socket, uint8_t *ttl_s)
304+
{
305+
return BHT_ERROR;
306+
}
307+
308+
int
309+
os_socket_set_ip_multicast_ttl(bh_socket_t socket, uint8_t ttl_s)
310+
{
311+
return BHT_ERROR;
312+
}
313+
314+
int
315+
os_socket_get_ip_multicast_ttl(bh_socket_t socket, uint8_t *ttl_s)
316+
{
317+
return BHT_ERROR;
318+
}
319+
320+
int
321+
os_socket_set_ipv6_only(bh_socket_t socket, bool is_enabled)
322+
{
323+
return BHT_ERROR;
324+
}
325+
326+
int
327+
os_socket_get_ipv6_only(bh_socket_t socket, bool *is_enabled)
328+
{
329+
return BHT_ERROR;
330+
}
331+
332+
static void
333+
swap16(uint8 *pData)
334+
{
335+
uint8 value = *pData;
336+
*(pData) = *(pData + 1);
337+
*(pData + 1) = value;
338+
}
339+
340+
static void
341+
swap32(uint8 *pData)
342+
{
343+
uint8 value = *pData;
344+
*pData = *(pData + 3);
345+
*(pData + 3) = value;
346+
347+
value = *(pData + 1);
348+
*(pData + 1) = *(pData + 2);
349+
*(pData + 2) = value;
350+
}
351+
352+
/** In-enclave implementation of POSIX functions **/
353+
static bool
354+
is_little_endian()
355+
{
356+
long i = 0x01020304;
357+
unsigned char *c = (unsigned char *)&i;
358+
return (*c == 0x04) ? true : false;
359+
}
360+
361+
uint16
362+
htons(uint16 value)
363+
{
364+
uint16 ret;
365+
if (is_little_endian()) {
366+
ret = value;
367+
swap16((uint8 *)&ret);
368+
return ret;
369+
}
370+
371+
return value;
372+
}
373+
374+
uint32
375+
htonl(uint32 value)
376+
{
377+
uint32 ret;
378+
if (is_little_endian()) {
379+
ret = value;
380+
swap32((uint8 *)&ret);
381+
return ret;
382+
}
383+
384+
return value;
385+
}

‎core/shared/platform/rt-thread/rtt_thread.c

+427
Large diffs are not rendered by default.

‎product-mini/platforms/rt-thread/iwasm.c

+70-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <dfs_file.h>
1212
#include <dfs_fs.h>
1313

14+
#if WASM_ENABLE_LIBC_WASI != 0
15+
#include "../common/libc_wasi.c"
16+
#endif
17+
1418
#ifdef WAMR_ENABLE_RTT_EXPORT
1519

1620
#ifdef WAMR_RTT_EXPORT_VPRINTF
@@ -160,6 +164,15 @@ static NativeSymbol native_export_symbols[] = {
160164

161165
#endif /* WAMR_ENABLE_RTT_EXPORT */
162166

167+
static void *
168+
app_instance_func(wasm_module_inst_t module_inst, const char *func_name,
169+
int app_argc, char **app_argv)
170+
{
171+
wasm_application_execute_func(module_inst, func_name, app_argc - 1,
172+
app_argv + 1);
173+
return wasm_runtime_get_exception(module_inst);
174+
}
175+
163176
/**
164177
* run WASM module instance.
165178
* @param module_inst instance of wasm module
@@ -170,12 +183,8 @@ static NativeSymbol native_export_symbols[] = {
170183
static void *
171184
app_instance_main(wasm_module_inst_t module_inst, int app_argc, char **app_argv)
172185
{
173-
const char *exception;
174-
175186
wasm_application_execute_main(module_inst, app_argc, app_argv);
176-
if ((exception = wasm_runtime_get_exception(module_inst)))
177-
rt_kprintf("%s\n", exception);
178-
return NULL;
187+
return wasm_runtime_get_exception(module_inst);
179188
}
180189

181190
rt_uint8_t *
@@ -214,28 +223,36 @@ void
214223
iwasm_help(void)
215224
{
216225
#ifdef WAMR_ENABLE_IWASM_PARAMS
217-
rt_kputs("wrong input: iwasm [-t] [-m] [-s] <*.wasm> <wasm_args ...>\n"
218-
" iwasm [-h]\n");
219-
rt_kputs("\t -h: show this tips.\n");
220-
rt_kputs("\t -t: show time taking to run this app.\n");
221-
rt_kputs("\t -m: show memory taking to run this app\n");
222-
rt_kputs("\t wasm file name and exec params must behind of all vm-param\n");
226+
rt_kputs("Usage: iwasm [-options] wasm_file [args...]\n");
227+
rt_kputs("options:\n");
228+
rt_kputs(" -t Show time taking to run this app.\n");
229+
rt_kputs(" -m Show memory taking to run this app\n");
230+
rt_kputs(" -f|--function name Specify a function name of the module "
231+
"to run rather than main\n");
232+
rt_kputs(" --max-threads=n Set maximum thread number per "
233+
"cluster, default is 4\n");
223234
#else
224-
rt_kputs("wrong input: iwasm <*.wasm> <wasm_args ...>\n");
235+
rt_kputs("Usage: iwasm wasm_file [args...]\n");
225236
#endif /* WAMR_ENABLE_PARAMS */
226237
}
227238

228239
int
229240
iwasm(int argc, char **argv)
230241
{
242+
const char *exception = NULL;
243+
const char *func_name = NULL;
231244
rt_uint8_t *wasm_file_buf = NULL;
232245
rt_uint32_t wasm_file_size;
233-
rt_uint32_t stack_size = 4 * 1024, heap_size = 4 * 1024;
246+
rt_uint32_t stack_size = 64 * 1024, heap_size = 256 * 1024;
234247
wasm_module_t wasm_module = NULL;
235248
wasm_module_inst_t wasm_module_inst = NULL;
236249
RuntimeInitArgs init_args;
237250
static char error_buf[128] = { 0 };
238251
/* avoid stack overflow */
252+
#if WASM_ENABLE_LIBC_WASI != 0
253+
libc_wasi_parse_context_t wasi_parse_ctx;
254+
memset(&wasi_parse_ctx, 0, sizeof(wasi_parse_ctx));
255+
#endif
239256

240257
#ifdef WAMR_ENABLE_IWASM_PARAMS
241258
int i_arg_begin;
@@ -260,6 +277,17 @@ iwasm(int argc, char **argv)
260277
iwasm_help();
261278
return 0;
262279
}
280+
else if (argv[i_arg_begin][1] == 'f') {
281+
func_name = argv[++i_arg_begin];
282+
}
283+
else if (!strncmp(argv[i_arg_begin], "--max-threads=", 14)) {
284+
if (argv[0][14] != '\0')
285+
wasm_runtime_set_max_thread_num(atoi(argv[0] + 14));
286+
else {
287+
iwasm_help();
288+
return 0;
289+
}
290+
}
263291
else if (argv[i_arg_begin][1] == 0x00) {
264292
continue;
265293
}
@@ -303,8 +331,8 @@ iwasm(int argc, char **argv)
303331
rt_thread_t tid;
304332
if (show_stack) {
305333
tid = rt_thread_self();
306-
printf("thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr,
307-
tid->stack_size, tid->sp);
334+
rt_kprintf("thread stack addr: %p, size: %u, sp: %p\n", tid->stack_addr,
335+
tid->stack_size, tid->sp);
308336
}
309337
#endif /* WAMR_ENABLE_PARAMS */
310338

@@ -326,6 +354,10 @@ iwasm(int argc, char **argv)
326354
rt_kprintf("%s\n", error_buf);
327355
goto fail2;
328356
}
357+
#if WASM_ENABLE_LIBC_WASI != 0
358+
libc_wasi_init(wasm_module, argc, argv, &wasi_parse_ctx);
359+
#endif
360+
329361
rt_memset(error_buf, 0x00, sizeof(error_buf));
330362
wasm_module_inst = wasm_runtime_instantiate(
331363
wasm_module, stack_size, heap_size, error_buf, sizeof(error_buf));
@@ -341,13 +373,31 @@ iwasm(int argc, char **argv)
341373
}
342374
#endif /* WAMR_ENABLE_PARAMS */
343375

344-
app_instance_main(wasm_module_inst, argc - i_arg_begin, &argv[i_arg_begin]);
376+
if (func_name) {
377+
exception = app_instance_func(wasm_module_inst, func_name,
378+
argc - i_arg_begin, &argv[i_arg_begin]);
379+
}
380+
else {
381+
exception = app_instance_main(wasm_module_inst, argc - i_arg_begin,
382+
&argv[i_arg_begin]);
383+
rt_kprintf("finshed run app_instance_main\n");
384+
}
385+
386+
if (exception)
387+
rt_kprintf("%s\n", exception);
388+
389+
#if WASM_ENABLE_LIBC_WASI != 0
390+
if (!exception) {
391+
/* propagate wasi exit code. */
392+
wasm_runtime_get_wasi_exit_code(wasm_module_inst);
393+
}
394+
#endif
345395

346396
#ifdef WAMR_ENABLE_IWASM_PARAMS
347397
if (show_time_exec) {
348398
ticks_exec = rt_tick_get() - ticks_exec;
349-
printf("[iwasm] execute ticks took: %u [ticks/s = %u]\n", ticks_exec,
350-
RT_TICK_PER_SECOND);
399+
rt_kprintf("[iwasm] execute ticks took: %u [ticks/s = %u]\n",
400+
ticks_exec, RT_TICK_PER_SECOND);
351401
}
352402
#if defined(RT_USING_HEAP) && defined(RT_USING_MEMHEAP_AS_HEAP)
353403
if (show_mem) {
@@ -361,8 +411,8 @@ iwasm(int argc, char **argv)
361411
}
362412
#endif
363413
if (show_stack) {
364-
printf("[iwasm] thread stack addr: %p, size: %u, sp: %p\n",
365-
tid->stack_addr, tid->stack_size, tid->sp);
414+
rt_kprintf("[iwasm] thread stack addr: %p, size: %u, sp: %p\n",
415+
tid->stack_addr, tid->stack_size, tid->sp);
366416
}
367417

368418
#endif /* WAMR_ENABLE_PARAMS */

0 commit comments

Comments
 (0)
Please sign in to comment.