Skip to content

Commit 071b8c2

Browse files
authored
esp-idf: Make esp-idf support Libc WASI (#1356)
esp-idf: Make esp-idf support Libc WASI 1. Support to get WASM APP libs' DIR from upper layer 2. Add SSP support for esp-idf platform 3. Change the errno of readlinkat
1 parent 3b641b1 commit 071b8c2

File tree

4 files changed

+265
-7
lines changed

4 files changed

+265
-7
lines changed

core/app-framework/app_framework.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function (add_module_native arg)
3333
${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native/*.h
3434
${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native/*.inl
3535
)
36+
37+
LIST (APPEND WASM_APP_LIBS_DIR ${APP_FRAMEWORK_ROOT_DIR}/${ARGV0}/native)
38+
set (WASM_APP_LIBS_DIR ${WASM_APP_LIBS_DIR} PARENT_SCOPE)
39+
3640
LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})
3741
set (RUNTIME_LIB_HEADER_LIST ${RUNTIME_LIB_HEADER_LIST} PARENT_SCOPE)
3842

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/ssp_config.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
// On Linux, prefer to use getrandom, though it isn't available in
2727
// GLIBC before 2.25.
28-
#if defined(__linux__) \
29-
&& (!defined(__GLIBC__) || __GLIBC__ > 2 \
28+
#if (defined(__linux__) || defined(ESP_PLATFORM)) \
29+
&& (!defined(__GLIBC__) || __GLIBC__ > 2 \
3030
|| (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
3131
#define CONFIG_HAS_GETRANDOM 1
3232
#else
@@ -39,13 +39,14 @@
3939
#define CONFIG_HAS_CAP_ENTER 0
4040
#endif
4141

42-
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__)
42+
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__EMSCRIPTEN__) \
43+
&& !defined(ESP_PLATFORM)
4344
#define CONFIG_HAS_CLOCK_NANOSLEEP 1
4445
#else
4546
#define CONFIG_HAS_CLOCK_NANOSLEEP 0
4647
#endif
4748

48-
#if !defined(__APPLE__) && !defined(__FreeBSD__)
49+
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(ESP_PLATFORM)
4950
#define CONFIG_HAS_FDATASYNC 1
5051
#else
5152
#define CONFIG_HAS_FDATASYNC 0
@@ -63,13 +64,13 @@
6364
#endif
6465
#endif
6566

66-
#ifndef __APPLE__
67+
#if !defined(__APPLE__) && !defined(ESP_PLATFORM)
6768
#define CONFIG_HAS_POSIX_FALLOCATE 1
6869
#else
6970
#define CONFIG_HAS_POSIX_FALLOCATE 0
7071
#endif
7172

72-
#ifndef __APPLE__
73+
#if !defined(__APPLE__) && !defined(ESP_PLATFORM)
7374
#define CONFIG_HAS_PREADV 1
7475
#else
7576
#define CONFIG_HAS_PREADV 0
@@ -87,7 +88,7 @@
8788
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0
8889
#endif
8990

90-
#ifndef __APPLE__
91+
#if !defined(__APPLE__) && !defined(ESP_PLATFORM)
9192
#define CONFIG_HAS_PWRITEV 1
9293
#else
9394
#define CONFIG_HAS_PWRITEV 0

core/shared/platform/esp-idf/espidf_platform.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,193 @@ os_usleep(uint32 usec)
5858
{
5959
return usleep(usec);
6060
}
61+
62+
/* Below parts of readv & writev are ported from Nuttx, under Apache License
63+
* v2.0 */
64+
65+
ssize_t
66+
readv(int fildes, const struct iovec *iov, int iovcnt)
67+
{
68+
ssize_t ntotal;
69+
ssize_t nread;
70+
size_t remaining;
71+
uint8_t *buffer;
72+
int i;
73+
74+
/* Process each entry in the struct iovec array */
75+
76+
for (i = 0, ntotal = 0; i < iovcnt; i++) {
77+
/* Ignore zero-length reads */
78+
79+
if (iov[i].iov_len > 0) {
80+
buffer = iov[i].iov_base;
81+
remaining = iov[i].iov_len;
82+
83+
/* Read repeatedly as necessary to fill buffer */
84+
85+
do {
86+
/* NOTE: read() is a cancellation point */
87+
88+
nread = read(fildes, buffer, remaining);
89+
90+
/* Check for a read error */
91+
92+
if (nread < 0) {
93+
return nread;
94+
}
95+
96+
/* Check for an end-of-file condition */
97+
98+
else if (nread == 0) {
99+
return ntotal;
100+
}
101+
102+
/* Update pointers and counts in order to handle partial
103+
* buffer reads.
104+
*/
105+
106+
buffer += nread;
107+
remaining -= nread;
108+
ntotal += nread;
109+
} while (remaining > 0);
110+
}
111+
}
112+
113+
return ntotal;
114+
}
115+
116+
ssize_t
117+
writev(int fildes, const struct iovec *iov, int iovcnt)
118+
{
119+
ssize_t ntotal;
120+
ssize_t nwritten;
121+
size_t remaining;
122+
uint8_t *buffer;
123+
int i;
124+
125+
/* Process each entry in the struct iovec array */
126+
127+
for (i = 0, ntotal = 0; i < iovcnt; i++) {
128+
/* Ignore zero-length writes */
129+
130+
if (iov[i].iov_len > 0) {
131+
buffer = iov[i].iov_base;
132+
remaining = iov[i].iov_len;
133+
134+
/* Write repeatedly as necessary to write the entire buffer */
135+
136+
do {
137+
/* NOTE: write() is a cancellation point */
138+
139+
nwritten = write(fildes, buffer, remaining);
140+
141+
/* Check for a write error */
142+
143+
if (nwritten < 0) {
144+
return ntotal ? ntotal : -1;
145+
}
146+
147+
/* Update pointers and counts in order to handle partial
148+
* buffer writes.
149+
*/
150+
151+
buffer += nwritten;
152+
remaining -= nwritten;
153+
ntotal += nwritten;
154+
} while (remaining > 0);
155+
}
156+
}
157+
158+
return ntotal;
159+
}
160+
161+
int
162+
openat(int fd, const char *path, int oflags, ...)
163+
{
164+
errno = ENOSYS;
165+
return -1;
166+
}
167+
168+
int
169+
fstatat(int fd, const char *path, struct stat *buf, int flag)
170+
{
171+
errno = ENOSYS;
172+
return -1;
173+
}
174+
175+
int
176+
mkdirat(int fd, const char *path, mode_t mode)
177+
{
178+
errno = ENOSYS;
179+
return -1;
180+
}
181+
182+
ssize_t
183+
readlinkat(int fd, const char *path, char *buf, size_t bufsize)
184+
{
185+
errno = EINVAL;
186+
return -1;
187+
}
188+
189+
int
190+
linkat(int fd1, const char *path1, int fd2, const char *path2, int flag)
191+
{
192+
errno = ENOSYS;
193+
return -1;
194+
}
195+
196+
int
197+
renameat(int fromfd, const char *from, int tofd, const char *to)
198+
{
199+
errno = ENOSYS;
200+
return -1;
201+
}
202+
203+
int
204+
symlinkat(const char *target, int fd, const char *path)
205+
{
206+
errno = ENOSYS;
207+
return -1;
208+
}
209+
210+
int
211+
unlinkat(int fd, const char *path, int flag)
212+
{
213+
errno = ENOSYS;
214+
return -1;
215+
}
216+
217+
int
218+
utimensat(int fd, const char *path, const struct timespec ts[2], int flag)
219+
{
220+
errno = ENOSYS;
221+
return -1;
222+
}
223+
224+
DIR *
225+
fdopendir(int fd)
226+
{
227+
errno = ENOSYS;
228+
return NULL;
229+
}
230+
231+
int
232+
ftruncate(int fd, off_t length)
233+
{
234+
errno = ENOSYS;
235+
return -1;
236+
}
237+
238+
int
239+
futimens(int fd, const struct timespec times[2])
240+
{
241+
errno = ENOSYS;
242+
return -1;
243+
}
244+
245+
int
246+
nanosleep(const struct timespec *req, struct timespec *rem)
247+
{
248+
errno = ENOSYS;
249+
return -1;
250+
}

core/shared/platform/esp-idf/platform_internal.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include <unistd.h>
1919
#include <pthread.h>
2020
#include <arpa/inet.h>
21+
#include <sys/socket.h>
22+
#include <sys/uio.h>
23+
#include <dirent.h>
2124

2225
#include "esp_pthread.h"
2326
#include "esp_timer.h"
@@ -43,6 +46,66 @@ typedef unsigned int korp_sem;
4346
/* Default thread priority */
4447
#define BH_THREAD_DEFAULT_PRIORITY 5
4548

49+
/* Special value for tv_nsec field of timespec */
50+
51+
#define UTIME_NOW ((1l << 30) - 1l)
52+
#ifndef __cplusplus
53+
#define UTIME_OMIT ((1l << 30) - 2l)
54+
#endif
55+
56+
#ifdef DT_UNKNOWN
57+
#undef DT_UNKNOWN
58+
#endif
59+
60+
#ifdef DT_REG
61+
#undef DT_REG
62+
#endif
63+
64+
#ifdef DT_DIR
65+
#undef DT_DIR
66+
#endif
67+
68+
/* Below parts of d_type define are ported from Nuttx, under Apache License v2.0
69+
*/
70+
71+
/* File type code for the d_type field in dirent structure.
72+
* Note that because of the simplified filesystem organization of the NuttX,
73+
* top-level, pseudo-file system, an inode can be BOTH a file and a directory
74+
*/
75+
76+
#define DTYPE_UNKNOWN 0
77+
#define DTYPE_FIFO 1
78+
#define DTYPE_CHR 2
79+
#define DTYPE_SEM 3
80+
#define DTYPE_DIRECTORY 4
81+
#define DTYPE_MQ 5
82+
#define DTYPE_BLK 6
83+
#define DTYPE_SHM 7
84+
#define DTYPE_FILE 8
85+
#define DTYPE_MTD 9
86+
#define DTYPE_LINK 10
87+
#define DTYPE_SOCK 12
88+
89+
/* The d_type field of the dirent structure is not specified by POSIX. It
90+
* is a non-standard, 4.5BSD extension that is implemented by most OSs. A
91+
* POSIX compliant OS may not implement the d_type field at all. Many OS's
92+
* (including glibc) may use the following alternative naming for the file
93+
* type names:
94+
*/
95+
96+
#define DT_UNKNOWN DTYPE_UNKNOWN
97+
#define DT_FIFO DTYPE_FIFO
98+
#define DT_CHR DTYPE_CHR
99+
#define DT_SEM DTYPE_SEM
100+
#define DT_DIR DTYPE_DIRECTORY
101+
#define DT_MQ DTYPE_MQ
102+
#define DT_BLK DTYPE_BLK
103+
#define DT_SHM DTYPE_SHM
104+
#define DT_REG DTYPE_FILE
105+
#define DT_MTD DTYPE_MTD
106+
#define DT_LNK DTYPE_LINK
107+
#define DT_SOCK DTYPE_SOCK
108+
46109
#ifdef __cplusplus
47110
}
48111
#endif

0 commit comments

Comments
 (0)