This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new c1c723b162 sim: add assertions on hostfs copy of structures
c1c723b162 is described below
commit c1c723b16260225939382b63bdb64f16e9c64904
Author: YAMAMOTO Takashi <[email protected]>
AuthorDate: Tue Dec 26 18:31:52 2023 +0900
sim: add assertions on hostfs copy of structures
hostfs has its copies of some of nuttx definitions with different
names to avoid conflicting with the host OS definitions.
sometimes people only modifies one of them and forgets updating
another. eg. https://github.com/apache/nuttx/pull/11445
this commit introduces some assertions to detect that kind of
mistakes.
---
arch/sim/src/Makefile | 1 +
arch/sim/src/sim/CMakeLists.txt | 5 +-
arch/sim/src/sim/sim_checkhostfstypes.c | 113 ++++++++++++++++++++++++++++++++
3 files changed, 118 insertions(+), 1 deletion(-)
diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile
index 5750fd5fe7..a1328ad90c 100644
--- a/arch/sim/src/Makefile
+++ b/arch/sim/src/Makefile
@@ -65,6 +65,7 @@ CSRCS += sim_createstack.c sim_usestack.c sim_releasestack.c
sim_stackframe.c
CSRCS += sim_exit.c sim_schedulesigaction.c sim_switchcontext.c sim_heap.c
CSRCS += sim_uart.c sim_copyfullstate.c sim_sigdeliver.c sim_tcbinfo.c
sim_cpuinfo.c
CSRCS += sim_registerdump.c sim_saveusercontext.c sim_textheap.c
+CSRCS += sim_checkhostfstypes.c
ifeq ($(CONFIG_SCHED_BACKTRACE),y)
CSRCS += sim_backtrace.c
diff --git a/arch/sim/src/sim/CMakeLists.txt b/arch/sim/src/sim/CMakeLists.txt
index a01d685edf..44390ae226 100644
--- a/arch/sim/src/sim/CMakeLists.txt
+++ b/arch/sim/src/sim/CMakeLists.txt
@@ -57,7 +57,8 @@ list(
sim_registerdump.c
sim_saveusercontext.c
sim_tcbinfo.c
- sim_textheap.c)
+ sim_textheap.c
+ sim_checkhostfstypes.c)
if(CONFIG_HOST_X86_64)
if(CONFIG_SIM_M32)
@@ -237,6 +238,8 @@ target_include_directories(nuttx PRIVATE
${CMAKE_CURRENT_LIST_DIR})
target_include_directories(sim_head PUBLIC ${NUTTX_DIR}/sched)
target_sources(sim_head PUBLIC sim_head.c sim_doirq.c)
+
+target_include_directories(arch PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_sources(arch PRIVATE ${SRCS})
if(WIN32)
diff --git a/arch/sim/src/sim/sim_checkhostfstypes.c
b/arch/sim/src/sim/sim_checkhostfstypes.c
new file mode 100644
index 0000000000..49145ab346
--- /dev/null
+++ b/arch/sim/src/sim/sim_checkhostfstypes.c
@@ -0,0 +1,113 @@
+/****************************************************************************
+ * arch/sim/src/sim/sim_checkhostfstypes.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 <assert.h>
+
+/* include headers for NuttX types
+ * eg. struct stat
+ *
+ * Note: This file is not HOSTSRCS. Thus, these files are not
+ * the host OS version of them.
+ */
+
+#include <dirent.h>
+#include <sys/statfs.h>
+#include <sys/stat.h>
+
+/* include nuttx/fs/hostfs.h for hostfs types
+ * eg. struct nuttx_stat_s
+ *
+ * define __SIM__ to make them visible.
+ *
+ * Note: For HOSTSRCS, __SIM__ is automatically defined by make files.
+ * However, this file is not HOSTSRCS. Thus we need to define it
+ * by ourselves here.
+ */
+
+#define __SIM__
+#include <nuttx/fs/hostfs.h>
+#undef __SIM__
+
+/* Here we make static assertions to ensure NuttX types (eg. struct stat)
+ * match the corresponding hostfs types. (eg. struct nuttx_stat_s)
+ *
+ * TODO: check the alignment as well. (_Alignof)
+ */
+
+#define STATIC_ASSERT(c) static_assert(c, #c)
+#define STATIC_ASSERT_FILED(a, b, f) \
+ STATIC_ASSERT(offsetof(struct a, f) == offsetof(struct b, f)); \
+ STATIC_ASSERT(sizeof(((struct a *)0)->f) == sizeof(((struct b *)0)->f))
+
+/* dirent */
+
+STATIC_ASSERT_FILED(nuttx_dirent_s, dirent, d_type);
+STATIC_ASSERT_FILED(nuttx_dirent_s, dirent, d_name);
+
+STATIC_ASSERT(sizeof(struct nuttx_dirent_s) == sizeof(struct dirent));
+
+/* stat */
+
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_dev);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_ino);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_mode);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_nlink);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_uid);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_gid);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_rdev);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_size);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_atim);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_mtim);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_ctim);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_blksize);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_blocks);
+STATIC_ASSERT_FILED(nuttx_stat_s, stat, st_size);
+
+STATIC_ASSERT(sizeof(struct nuttx_stat_s) == sizeof(struct stat));
+
+/* statfs */
+
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_type);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_namelen);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_bsize);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_blocks);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_bfree);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_bavail);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_files);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_ffree);
+STATIC_ASSERT_FILED(nuttx_statfs_s, statfs, f_fsid);
+
+STATIC_ASSERT(sizeof(struct nuttx_statfs_s) == sizeof(struct statfs));
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* dummy function to suppress nxstyle and compiler warnings */
+
+void check_hostfs_types_dummy(void);
+
+void check_hostfs_types_dummy(void)
+{
+}