Skip to content

Commit

Permalink
sim: add assertions on hostfs copy of structures
Browse files Browse the repository at this point in the history
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. apache#11445
this commit introduces some assertions to detect that kind of
mistakes.
  • Loading branch information
yamt committed Dec 28, 2023
1 parent 087c519 commit 13f3398
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
1 change: 1 addition & 0 deletions arch/sim/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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_checknuttxtypes.c

ifeq ($(CONFIG_SCHED_BACKTRACE),y)
CSRCS += sim_backtrace.c
Expand Down
3 changes: 2 additions & 1 deletion arch/sim/src/sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ list(
sim_registerdump.c
sim_saveusercontext.c
sim_tcbinfo.c
sim_textheap.c)
sim_textheap.c
sim_checknuttxtypes.c)

if(CONFIG_HOST_X86_64)
if(CONFIG_SIM_M32)
Expand Down
100 changes: 100 additions & 0 deletions arch/sim/src/sim/sim_checknuttxtypes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/****************************************************************************
* arch/sim/src/sim/sim_checknuttxtypes.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>

/* nuttx types */

#include <dirent.h>
#include <sys/statfs.h>
#include <sys/stat.h>

/* hostfs types */

#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 FIELD(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 */

FIELD(nuttx_dirent_s, dirent, d_type);
FIELD(nuttx_dirent_s, dirent, d_name);

STATIC_ASSERT(sizeof(struct nuttx_dirent_s) == sizeof(struct dirent));

/* stat */

FIELD(nuttx_stat_s, stat, st_dev);
FIELD(nuttx_stat_s, stat, st_ino);
FIELD(nuttx_stat_s, stat, st_mode);
FIELD(nuttx_stat_s, stat, st_nlink);
FIELD(nuttx_stat_s, stat, st_uid);
FIELD(nuttx_stat_s, stat, st_gid);
FIELD(nuttx_stat_s, stat, st_rdev);
FIELD(nuttx_stat_s, stat, st_size);
FIELD(nuttx_stat_s, stat, st_atim);
FIELD(nuttx_stat_s, stat, st_mtim);
FIELD(nuttx_stat_s, stat, st_ctim);
FIELD(nuttx_stat_s, stat, st_blksize);
FIELD(nuttx_stat_s, stat, st_blocks);
FIELD(nuttx_stat_s, stat, st_size);

STATIC_ASSERT(sizeof(struct nuttx_stat_s) == sizeof(struct stat));

/* statfs */

FIELD(nuttx_statfs_s, statfs, f_type);
FIELD(nuttx_statfs_s, statfs, f_namelen);
FIELD(nuttx_statfs_s, statfs, f_bsize);
FIELD(nuttx_statfs_s, statfs, f_blocks);
FIELD(nuttx_statfs_s, statfs, f_bfree);
FIELD(nuttx_statfs_s, statfs, f_bavail);
FIELD(nuttx_statfs_s, statfs, f_files);
FIELD(nuttx_statfs_s, statfs, f_ffree);
FIELD(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_nuttx_types_dummy(void);

void check_nuttx_types_dummy(void)
{
}

0 comments on commit 13f3398

Please sign in to comment.