no1wudi commented on code in PR #13245:
URL: https://github.com/apache/nuttx/pull/13245#discussion_r1779497091


##########
libs/libc/librust/lib_rust.c:
##########
@@ -0,0 +1,944 @@
+/****************************************************************************
+ * libs/libc/librust/lib_rust.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <dlfcn.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <locale.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <sys/statvfs.h>
+#include <sys/poll.h>
+#include <sys/resource.h>
+#include <pwd.h>
+#include <time.h>
+#include <netdb.h>
+#include <termios.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <unistd.h>
+#include <signal.h>
+#include <dirent.h>
+#include <sys/un.h>
+
+#include <nuttx/fs/ioctl.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/**
+ * This file contains definitions and assertions to ensure compatibility
+ * between C and Rust data structures. It defines the sizes of various
+ * POSIX structures and asserts that their sizes are compatible with Rust.
+ *
+ * Most of these sizes are defined in terms of the size of a pointer, which
+ * can be either 4 or 8 bytes depending on the architecture. The sizes are
+ * defined in terms of the number of pointer-sized elements they contain.
+ *
+ * Most of these structures have a reserved space for future use, and
+ * the current actual size of them are calculated on riscv32 platform.
+ */
+
+#undef NP
+#define NP(x) (sizeof(void *) * (x))
+
+#define __PTHREAD_ATTR_SIZE__     (NP(5))  /* Actual size is 3 */
+#define __PTHREAD_MUTEX_SIZE__    (NP(9))  /* Actual size is 7 */
+#define __PTHREAD_COND_SIZE__     (NP(7))  /* Actual size is 5 */
+#define __PTHREAD_CONDATTR_SIZE__ (NP(5))  /* Actual size is 3 */
+#define __PTHREAD_RWLOCK_SIZE__   (NP(17)) /* Actual size is 15 */
+#define __SEM_SIZE__              (NP(6))  /* Actual size is 4 */
+#define __STAT_SIZE__             (NP(28)) /* Actual size is 26 */
+#define __DIRENT_SIZE__           (NP(20)) /* Actual size is 17.5 */
+#define __STATVFS_SIZE__          (NP(20)) /* Actual size is 18 */
+#define __PASSWD_SIZE__           (NP(16)) /* Actual size is 14 */
+#define __DL_INFO_SIZE__          (NP(4))  /* Actual size is 4 */
+#define __LCONV_SIZE__            (NP(40)) /* Actual size is 38 */
+#define __TM_SIZE__               (NP(13)) /* Actual size is 11 */
+#define __ADDRINFO_SIZE__         (NP(10)) /* Actual size is 8 */
+#define __FDSET_SIZE__            (NP(10)) /* Actual size is 8 */
+#define __SIGSET_SIZE__           (NP(4))  /* Actual size is 2 */
+#define __SIGACTION_SIZE__        (NP(7))  /* Actual size is 5 */
+#define __TERMIOS_SIZE__          (NP(10)) /* Actual size is 8 */
+#define __SOCKADDR_STORAGE_SIZE__ (NP(36)) /* Actual size is 34*/
+#define __NAME_MAX_SIZE__         (64)
+
+#define ALIGNUP_TO(x, align) (((x) + (align) - 1) & ~((align) - 1))
+
+/**
+ * This section contains static assertions to ensure that the sizes of
+ * various POSIX data types and structures are compatible with Rust.
+ * These assertions are crucial for maintaining interoperability between
+ * C and Rust code.
+ *
+ * The assertions check the sizes of common POSIX types such as pid_t,
+ * pthread_t, and various structures like pthread_attr_t, pthread_mutex_t,
+ * etc. They also verify the sizes of structures used in file system
+ * operations, networking, and threading.
+ *
+ * If any of these assertions fail, it indicates a mismatch between
+ * the expected sizes in Rust and the actual sizes in C, which could lead
+ * to runtime errors or undefined behavior when passing data between
+ * the Rust side and NuttX side.
+ */
+
+static_assert(sizeof(pid_t) == sizeof(int), "pid_t size mismatch with Rust");
+static_assert(sizeof(pthread_t) == sizeof(int),
+              "pthread_t size mismatch with Rust");
+static_assert(sizeof(pthread_attr_t) <= __PTHREAD_ATTR_SIZE__,
+              "pthread_attr_t too large for Rust");
+static_assert(sizeof(pthread_mutex_t) <= __PTHREAD_MUTEX_SIZE__,
+              "pthread_mutex_t too large for Rust");
+static_assert(sizeof(pthread_cond_t) <= __PTHREAD_COND_SIZE__,
+              "pthread_cond_t too large for Rust");
+static_assert(sizeof(pthread_condattr_t) <= __PTHREAD_CONDATTR_SIZE__,
+              "pthread_condattr_t too large for Rust");
+static_assert(sizeof(pthread_rwlock_t) <= __PTHREAD_RWLOCK_SIZE__,
+              "pthread_rwlock_t too large for Rust");
+static_assert(sizeof(sem_t) <= __SEM_SIZE__, "sem_t too large for Rust");
+static_assert(sizeof(struct stat) <= __STAT_SIZE__,
+              "struct stat size mismatch with Rust");
+static_assert(sizeof(struct statvfs) <= __STATVFS_SIZE__,
+              "struct statvfs too large for Rust");
+static_assert(sizeof(struct passwd) <= __PASSWD_SIZE__,
+              "struct passwd size mismatch with Rust");
+static_assert(sizeof(Dl_info) == __DL_INFO_SIZE__,
+              "Dl_info size mismatch with Rust");
+static_assert(sizeof(struct lconv) <= __LCONV_SIZE__,
+              "__Lconv size mismatch with Rust");
+static_assert(sizeof(struct tm) <= __TM_SIZE__,
+              "struct tm too large for Rust");
+static_assert(sizeof(struct addrinfo) <= __ADDRINFO_SIZE__,
+              "struct addrinfo too large for Rust");
+static_assert(sizeof(fd_set) <= __FDSET_SIZE__,
+              "struct fd_set too large for Rust");
+static_assert(sizeof(sigset_t) <= __SIGSET_SIZE__,
+              "sigset_t too large for Rust");
+static_assert(sizeof(struct sigaction) <= __SIGACTION_SIZE__,
+              "struct sigaction too large for Rust");
+static_assert(sizeof(struct termios) <= __TERMIOS_SIZE__,
+              "struct termios too large for Rust");
+
+static_assert(NAME_MAX <= __NAME_MAX_SIZE__,
+              "NAME_MAX size mismatch with Rust");
+
+static_assert(sizeof(blkcnt_t) == 8, "blkcnt_t size mismatch with Rust");
+static_assert(sizeof(blksize_t) == 2, "blksize_t size mismatch with Rust");
+static_assert(sizeof(cc_t) == 1, "cc_t size mismatch with Rust");
+static_assert(sizeof(clock_t) == 8, "clock_t size mismatch with Rust");
+static_assert(sizeof(dev_t) == 4, "dev_t size mismatch with Rust");
+static_assert(sizeof(fsblkcnt_t) == 8, "fsblkcnt_t size mismatch with Rust");
+static_assert(sizeof(locale_t) == sizeof(void *),
+              "locale_t size mismatch with Rust");
+static_assert(sizeof(mode_t) == 4, "mode_t size mismatch with Rust");
+static_assert(sizeof(nfds_t) == 4, "nfds_t size mismatch with Rust");
+static_assert(sizeof(off_t) == 8, "off_t size mismatch with Rust");
+static_assert(sizeof(pthread_key_t) == 4,
+              "pthread_key_t size mismatch with Rust");
+static_assert(sizeof(pthread_mutexattr_t) == 1,
+              "pthread_mutexattr_t size mismatch with Rust");
+static_assert(sizeof(pthread_rwlockattr_t) == 4,
+              "pthread_rwlockattr_t size mismatch with Rust");
+static_assert(sizeof(pthread_t) == 4, "pthread_t size mismatch with Rust");
+static_assert(sizeof(rlim_t) == 8, "rlim_t size mismatch with Rust");
+static_assert(sizeof(sa_family_t) == 2,
+              "sa_family_t size mismatch with Rust");
+static_assert(sizeof(socklen_t) == 4, "socklen_t size mismatch with Rust");
+static_assert(sizeof(speed_t) == sizeof(unsigned long),
+              "speed_t size mismatch with Rust");
+static_assert(sizeof(suseconds_t) == 4,
+              "suseconds_t size mismatch with Rust");
+static_assert(sizeof(tcflag_t) == 4, "tcflag_t size mismatch with Rust");
+static_assert(sizeof(time_t) == 8, "time_t size mismatch with Rust");
+static_assert(sizeof(wchar_t) == 4, "wchar_t size mismatch with Rust");
+
+/* Field ordering checks */
+
+/**
+ * This section contains static assertions to ensure that the field offsets
+ * for compatibility with Rust, ensuring that the memory layout is consistent
+ * across both languages.
+ */
+
+/**
+ * This section contains static assertions to ensure that the field offsets
+ * of the `struct stat` match the expected values.
+ */
+
+static_assert(offsetof(struct stat, st_dev) == 0, "st_dev offset mismatch");
+static_assert(offsetof(struct stat, st_ino) ==
+                  offsetof(struct stat, st_dev) + sizeof(dev_t),
+              "st_ino offset mismatch");
+static_assert(offsetof(struct stat, st_mode) ==
+                  offsetof(struct stat, st_ino) + sizeof(mode_t),

Review Comment:
   Some members have memory align issue:
   
   * dev_t is 4 byte
   * ino_t is 2 byte
   * mode_t is 4 byte
   
   so if use  offsetof(struct stat, st_ino) + sizeof(ino_t) = 6, but offset of 
mode_t will be aligned to 8.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to