11cb0ef41Sopenharmony_ci#ifndef __UVWASI_SERDES_H__
21cb0ef41Sopenharmony_ci#define __UVWASI_SERDES_H__
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci#include "wasi_types.h"
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci/* Basic uint{8,16,32,64}_t read/write functions. */
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#define BASIC_TYPE(name, type)                                                \
91cb0ef41Sopenharmony_ci  void uvwasi_serdes_write_##name(void* ptr, size_t offset, type value);      \
101cb0ef41Sopenharmony_ci  type uvwasi_serdes_read_##name(const void* ptr, size_t offset);             \
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#define BASIC_TYPE_UVWASI(type) BASIC_TYPE(type, uvwasi_##type)
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_uint8_t sizeof(uint8_t)
151cb0ef41Sopenharmony_ciBASIC_TYPE(uint8_t, uint8_t)
161cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_uint16_t sizeof(uint16_t)
171cb0ef41Sopenharmony_ciBASIC_TYPE(uint16_t, uint16_t)
181cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_uint32_t sizeof(uint32_t)
191cb0ef41Sopenharmony_ciBASIC_TYPE(uint32_t, uint32_t)
201cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_uint64_t sizeof(uint64_t)
211cb0ef41Sopenharmony_ciBASIC_TYPE(uint64_t, uint64_t)
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_advice_t sizeof(uvwasi_advice_t)
241cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(advice_t)
251cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_clockid_t sizeof(uvwasi_clockid_t)
261cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(clockid_t)
271cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_device_t sizeof(uvwasi_device_t)
281cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(device_t)
291cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_dircookie_t sizeof(uvwasi_dircookie_t)
301cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(dircookie_t)
311cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_eventrwflags_t sizeof(uvwasi_eventrwflags_t)
321cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(eventrwflags_t)
331cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_eventtype_t sizeof(uvwasi_eventtype_t)
341cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(eventtype_t)
351cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_exitcode_t sizeof(uvwasi_exitcode_t)
361cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(exitcode_t)
371cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_fd_t sizeof(uvwasi_fd_t)
381cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(fd_t)
391cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_fdflags_t sizeof(uvwasi_fdflags_t)
401cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(fdflags_t)
411cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_filesize_t sizeof(uvwasi_filesize_t)
421cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(filesize_t)
431cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_fstflags_t sizeof(uvwasi_fstflags_t)
441cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(fstflags_t)
451cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_inode_t sizeof(uvwasi_inode_t)
461cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(inode_t)
471cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_linkcount_t sizeof(uvwasi_linkcount_t)
481cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(linkcount_t)
491cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_lookupflags_t sizeof(uvwasi_lookupflags_t)
501cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(lookupflags_t)
511cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_oflags_t sizeof(uvwasi_oflags_t)
521cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(oflags_t)
531cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_preopentype_t sizeof(uvwasi_preopentype_t)
541cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(preopentype_t)
551cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_riflags_t sizeof(uvwasi_riflags_t)
561cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(riflags_t)
571cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_rights_t sizeof(uvwasi_rights_t)
581cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(rights_t)
591cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_roflags_t sizeof(uvwasi_roflags_t)
601cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(roflags_t)
611cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_sdflags_t sizeof(uvwasi_sdflags_t)
621cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(sdflags_t)
631cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_siflags_t sizeof(uvwasi_siflags_t)
641cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(siflags_t)
651cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_size_t sizeof(uvwasi_size_t)
661cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(size_t)
671cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_inode_t sizeof(uvwasi_inode_t)
681cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(inode_t)
691cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_signal_t sizeof(uvwasi_signal_t)
701cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(signal_t)
711cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_subclockflags_t sizeof(uvwasi_subclockflags_t)
721cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(subclockflags_t)
731cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_timestamp_t sizeof(uvwasi_timestamp_t)
741cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(timestamp_t)
751cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_userdata_t sizeof(uvwasi_userdata_t)
761cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(userdata_t)
771cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_whence_t sizeof(uvwasi_whence_t)
781cb0ef41Sopenharmony_ciBASIC_TYPE_UVWASI(whence_t)
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci#undef BASIC_TYPE_UVWASI
811cb0ef41Sopenharmony_ci#undef BASIC_TYPE
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci/* WASI structure read/write functions. */
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci#define STRUCT(name)                                                          \
861cb0ef41Sopenharmony_ci  void uvwasi_serdes_write_##name(void* ptr,                                  \
871cb0ef41Sopenharmony_ci                                  size_t offset,                              \
881cb0ef41Sopenharmony_ci                                  const uvwasi_##name* value);                \
891cb0ef41Sopenharmony_ci  void uvwasi_serdes_read_##name(const void* ptr,                             \
901cb0ef41Sopenharmony_ci                                 size_t offset,                               \
911cb0ef41Sopenharmony_ci                                 uvwasi_##name* value);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci/* iovs currently only need to be read from WASM memory. */
941cb0ef41Sopenharmony_ci#define IOVS_STRUCT(name)                                                     \
951cb0ef41Sopenharmony_ci  uvwasi_errno_t uvwasi_serdes_read_##name(const void* ptr,                   \
961cb0ef41Sopenharmony_ci                                           size_t end,                        \
971cb0ef41Sopenharmony_ci                                           size_t offset,                     \
981cb0ef41Sopenharmony_ci                                           uvwasi_##name* value);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_ciovec_t 8
1011cb0ef41Sopenharmony_ciIOVS_STRUCT(ciovec_t)
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_iovec_t 8
1041cb0ef41Sopenharmony_ciIOVS_STRUCT(iovec_t)
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_dirent_t 24
1071cb0ef41Sopenharmony_ciSTRUCT(dirent_t)
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_fdstat_t 24
1101cb0ef41Sopenharmony_ciSTRUCT(fdstat_t)
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_filestat_t 64
1131cb0ef41Sopenharmony_ciSTRUCT(filestat_t)
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_prestat_t 8
1161cb0ef41Sopenharmony_ciSTRUCT(prestat_t)
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_event_t 32
1191cb0ef41Sopenharmony_ciSTRUCT(event_t)
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci#define UVWASI_SERDES_SIZE_subscription_t 48
1221cb0ef41Sopenharmony_ciSTRUCT(subscription_t)
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci#undef STRUCT
1251cb0ef41Sopenharmony_ci#undef IOVS_STRUCT
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciuvwasi_errno_t uvwasi_serdes_readv_ciovec_t(const void* ptr,
1281cb0ef41Sopenharmony_ci                                            size_t end,
1291cb0ef41Sopenharmony_ci                                            size_t offset,
1301cb0ef41Sopenharmony_ci                                            uvwasi_ciovec_t* iovs,
1311cb0ef41Sopenharmony_ci                                            uvwasi_size_t iovs_len);
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciuvwasi_errno_t uvwasi_serdes_readv_iovec_t(const void* ptr,
1341cb0ef41Sopenharmony_ci                                           size_t end,
1351cb0ef41Sopenharmony_ci                                           size_t offset,
1361cb0ef41Sopenharmony_ci                                           uvwasi_iovec_t* iovs,
1371cb0ef41Sopenharmony_ci                                           uvwasi_size_t iovs_len);
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci/* Helper functions for memory bounds checking. */
1401cb0ef41Sopenharmony_ciint uvwasi_serdes_check_bounds(size_t offset, size_t end, size_t size);
1411cb0ef41Sopenharmony_ciint uvwasi_serdes_check_array_bounds(size_t offset,
1421cb0ef41Sopenharmony_ci                                     size_t end,
1431cb0ef41Sopenharmony_ci                                     size_t size,
1441cb0ef41Sopenharmony_ci                                     size_t count);
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci#endif /* __UVWASI_SERDES_H__ */
147