Skip to content

Commit af61ae8

Browse files
wujiangangdonghengqaz
authored andcommitted
esp-idf: Add SSP support for esp-idf platform
1 parent cd2a39b commit af61ae8

File tree

3 files changed

+261
-7
lines changed

3 files changed

+261
-7
lines changed

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 = ENOSYS;
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"
@@ -42,6 +45,66 @@ typedef pthread_t korp_thread;
4245
/* Default thread priority */
4346
#define BH_THREAD_DEFAULT_PRIORITY 5
4447

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

0 commit comments

Comments
 (0)