Skip to content

Commit 8f902fa

Browse files
JavanZhuwenyongh
andauthored
Enable Android libc wasi support. (#590)
Some libc APIs required by wasi native lib are missing in some Android API versions, only when the version >= 24, all APIs are supported. Add the missing APIs in android platform layer, leave them empty, report error and return failed if they are called. Also update CMakeLists.txt to enable libc wasi by default. Co-authored-by: Wenyong Huang <[email protected]>
1 parent be54b4c commit 8f902fa

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <stdlib.h>
1616

17-
#if defined(__FreeBSD__) || defined(__APPLE__)
17+
#if defined(__FreeBSD__) || defined(__APPLE__) || (defined(ANDROID) && __ANDROID_API__ < 28)
1818
#define CONFIG_HAS_ARC4RANDOM_BUF 1
1919
#else
2020
#define CONFIG_HAS_ARC4RANDOM_BUF 0

core/shared/platform/android/platform_init.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
#include "platform_api_vmcore.h"
77
#include "platform_api_extension.h"
8+
9+
#define API_NOT_SUPPORT_ERROR(API, VERSION) \
10+
__android_log_print(ANDROID_LOG_ERROR, "wasm_runtime::", \
11+
"%s() is only supported when __ANDROID_API__ >= %s.", \
12+
#API, #VERSION);
13+
814
int
915
bh_platform_init()
1016
{
@@ -33,3 +39,78 @@ int os_vprintf(const char *fmt, va_list ap)
3339
return __android_log_vprint(ANDROID_LOG_INFO, "wasm_runtime::", fmt, ap);
3440
}
3541

42+
#if __ANDROID_API__ < 19
43+
44+
int futimens(int __dir_fd, const struct timespec __times[2])
45+
{
46+
API_NOT_SUPPORT_ERROR(futimens, 19);
47+
return -1;
48+
}
49+
50+
#endif
51+
52+
#if __ANDROID_API__ < 21
53+
54+
int posix_fallocate(int __fd, off_t __offset, off_t __length)
55+
{
56+
API_NOT_SUPPORT_ERROR(posix_fallocate, 21);
57+
return -1;
58+
}
59+
60+
int posix_fadvise(int fd, off_t offset, off_t len, int advice)
61+
{
62+
API_NOT_SUPPORT_ERROR(posix_fadvise, 21);
63+
return -1;
64+
}
65+
66+
int linkat(int __old_dir_fd, const char *__old_path,
67+
int __new_dir_fd, const char *__new_path, int __flags)
68+
{
69+
API_NOT_SUPPORT_ERROR(linkat, 21);
70+
return -1;
71+
}
72+
73+
int symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path)
74+
{
75+
API_NOT_SUPPORT_ERROR(symlinkat, 21);
76+
return -1;
77+
}
78+
79+
ssize_t readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size)
80+
{
81+
API_NOT_SUPPORT_ERROR(readlinkat, 21);
82+
return -1;
83+
}
84+
85+
#endif
86+
87+
#if __ANDROID_API__ < 23
88+
89+
long telldir(DIR *__dir)
90+
{
91+
API_NOT_SUPPORT_ERROR(telldir, 23);
92+
return -1;
93+
}
94+
95+
void seekdir(DIR *__dir, long __location)
96+
{
97+
API_NOT_SUPPORT_ERROR(seekdir, 23);
98+
}
99+
100+
#endif
101+
102+
#if __ANDROID_API__ < 24
103+
104+
ssize_t preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset)
105+
{
106+
API_NOT_SUPPORT_ERROR(preadv, 24);
107+
return -1;
108+
}
109+
110+
ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset)
111+
{
112+
API_NOT_SUPPORT_ERROR(pwritev, 24);
113+
return -1;
114+
}
115+
116+
#endif

core/shared/platform/android/platform_internal.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,45 @@ void os_sigreturn();
8787
#endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64 */
8888
#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */
8989

90+
typedef long int __syscall_slong_t;
91+
92+
#if __ANDROID_API__ < 19
93+
94+
int futimens(int __dir_fd, const struct timespec __times[2]);
95+
96+
#endif
97+
98+
#if __ANDROID_API__ < 21
99+
100+
int posix_fallocate(int __fd, off_t __offset, off_t __length);
101+
102+
int posix_fadvise(int fd, off_t offset, off_t len, int advice);
103+
104+
int linkat(int __old_dir_fd, const char *__old_path,
105+
int __new_dir_fd, const char *__new_path, int __flags);
106+
107+
int symlinkat(const char *__old_path, int __new_dir_fd, const char *__new_path);
108+
109+
ssize_t readlinkat(int __dir_fd, const char *__path, char *__buf, size_t __buf_size);
110+
111+
#endif
112+
113+
#if __ANDROID_API__ < 23
114+
115+
long telldir(DIR *__dir);
116+
117+
void seekdir(DIR *__dir, long __location);
118+
119+
#endif
120+
121+
#if __ANDROID_API__ < 24
122+
123+
ssize_t preadv(int __fd, const struct iovec *__iov, int __count, off_t __offset);
124+
125+
ssize_t pwritev(int __fd, const struct iovec *__iov, int __count, off_t __offset);
126+
127+
#endif
128+
90129
#ifdef __cplusplus
91130
}
92131
#endif

product-mini/platforms/android/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ set (ANDROID_NDK $ENV{ANDROID_NDK_HOME})
1111
set (ANDROID_SDK $ENV{ANDROID_SDK_HOME})
1212
set (ANDROID_ABI "x86")
1313
set (ANDROID_LD lld)
14+
if (NOT DEFINED ANDROID_PLATFORM)
15+
set (ANDROID_PLATFORM 24)
16+
endif ()
1417

1518
project (iwasm)
1619

@@ -20,7 +23,7 @@ set (WAMR_BUILD_TYPE Release)
2023
set (WAMR_BUILD_INTERP 1)
2124
set (WAMR_BUILD_AOT 0)
2225
set (WAMR_BUILD_LIBC_BUILTIN 1)
23-
set (WAMR_BUILD_LIBC_WASI 0)
26+
set (WAMR_BUILD_LIBC_WASI 1)
2427

2528
# Reset default linker flags
2629
set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")

0 commit comments

Comments
 (0)