From: Bart Van Assche <bvanassche@acm.org>
To: Jaegeuk Kim <jaegeuk@kernel.org>
Cc: Bart Van Assche <bvanassche@acm.org>,
linux-f2fs-devel@lists.sourceforge.net
Subject: [f2fs-dev] [PATCH 10/31] Verify structure sizes at compile time
Date: Thu, 21 Apr 2022 15:18:15 -0700 [thread overview]
Message-ID: <20220421221836.3935616-11-bvanassche@acm.org> (raw)
In-Reply-To: <20220421221836.3935616-1-bvanassche@acm.org>
Before modifying the __attribute__((packed)) annotations, let the
compiler verify the sizes of on-disk data structures.
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
fsck/quotaio.c | 2 ++
fsck/quotaio_tree.h | 2 ++
fsck/quotaio_v2.h | 6 +++++
fsck/xattr.h | 2 ++
include/f2fs_fs.h | 54 +++++++++++++++++++++++++++++++++++++++++++++
include/quota.h | 7 ++++++
tools/f2fscrypt.c | 4 ++++
7 files changed, 77 insertions(+)
diff --git a/fsck/quotaio.c b/fsck/quotaio.c
index 51abbb7b2c57..52e962432c72 100644
--- a/fsck/quotaio.c
+++ b/fsck/quotaio.c
@@ -33,6 +33,8 @@ struct disk_dqheader {
__le32 dqh_version;
} __attribute__ ((packed));
+static_assert(sizeof(struct disk_dqheader) == 8, "");
+
int cur_qtype = -1;
u32 qf_last_blkofs[MAXQUOTAS] = {0, 0, 0};
enum qf_szchk_type_t qf_szchk_type[MAXQUOTAS] =
diff --git a/fsck/quotaio_tree.h b/fsck/quotaio_tree.h
index 8f4dae054691..b88c55c01d5c 100644
--- a/fsck/quotaio_tree.h
+++ b/fsck/quotaio_tree.h
@@ -35,6 +35,8 @@ struct qt_disk_dqdbheader {
__le32 dqdh_pad2;
} __attribute__ ((packed));
+static_assert(sizeof(struct qt_disk_dqdbheader) == 16, "");
+
struct dquot;
struct quota_handle;
diff --git a/fsck/quotaio_v2.h b/fsck/quotaio_v2.h
index de2db2785cb0..a37300d78bd0 100644
--- a/fsck/quotaio_v2.h
+++ b/fsck/quotaio_v2.h
@@ -20,6 +20,8 @@ struct v2_disk_dqheader {
__le32 dqh_version; /* File version */
} __attribute__ ((packed));
+static_assert(sizeof(struct v2_disk_dqheader) == 8, "");
+
/* Flags for version specific files */
#define V2_DQF_MASK 0x0000 /* Mask for all valid ondisk flags */
@@ -36,6 +38,8 @@ struct v2_disk_dqinfo {
* free entry */
} __attribute__ ((packed));
+static_assert(sizeof(struct v2_disk_dqinfo) == 24, "");
+
struct v2r1_disk_dqblk {
__le32 dqb_id; /* id this quota applies to */
__le32 dqb_pad;
@@ -51,4 +55,6 @@ struct v2r1_disk_dqblk {
__le64 dqb_itime; /* time limit for excessive inode use */
} __attribute__ ((packed));
+static_assert(sizeof(struct v2r1_disk_dqblk) == 72, "");
+
#endif
diff --git a/fsck/xattr.h b/fsck/xattr.h
index 579ab6c42585..5709a7df7adb 100644
--- a/fsck/xattr.h
+++ b/fsck/xattr.h
@@ -47,6 +47,8 @@ struct fscrypt_context {
u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
} __attribute__((packed));
+static_assert(sizeof(struct fscrypt_context) == 28, "");
+
#define F2FS_ACL_VERSION 0x0001
struct f2fs_acl_entry {
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index e6fc8a0e08f6..7804dd158de4 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -68,6 +68,10 @@
# define UNUSED(x) x
#endif
+#ifndef static_assert
+#define static_assert _Static_assert
+#endif
+
#ifdef ANDROID_WINDOWS_HOST
#undef HAVE_LINUX_TYPES_H
#endif
@@ -737,6 +741,8 @@ struct f2fs_device {
__le32 total_segments;
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_device) == 68, "");
+
struct f2fs_super_block {
__le32 magic; /* Magic Number */
__le16 major_ver; /* Major Version */
@@ -784,6 +790,8 @@ struct f2fs_super_block {
__le32 crc; /* checksum of superblock */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_super_block) == 3072, "");
+
/*
* For checkpoint
*/
@@ -835,6 +843,8 @@ struct f2fs_checkpoint {
unsigned char sit_nat_version_bitmap[];
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_checkpoint) == 192, "");
+
#define CP_BITMAP_OFFSET \
(offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap))
#define CP_MIN_CHKSUM_OFFSET CP_BITMAP_OFFSET
@@ -859,6 +869,8 @@ struct f2fs_orphan_block {
__le32 check_sum; /* CRC32 for orphan inode block */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_orphan_block) == 4096, "");
+
/*
* For NODE structure
*/
@@ -868,6 +880,8 @@ struct f2fs_extent {
__le32 len; /* lengh of the extent */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_extent) == 12, "");
+
#define F2FS_NAME_LEN 255
/* max output length of pretty_print_filename() including null terminator */
@@ -1013,15 +1027,20 @@ struct f2fs_inode {
double_indirect(1) node id */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_inode) == 4072, "");
struct direct_node {
__le32 addr[DEF_ADDRS_PER_BLOCK]; /* array of data block address */
} __attribute__((packed));
+static_assert(sizeof(struct direct_node) == 4072, "");
+
struct indirect_node {
__le32 nid[NIDS_PER_BLOCK]; /* array of data block address */
} __attribute__((packed));
+static_assert(sizeof(struct indirect_node) == 4072, "");
+
enum {
COLD_BIT_SHIFT = 0,
FSYNC_BIT_SHIFT,
@@ -1039,6 +1058,8 @@ struct node_footer {
__le32 next_blkaddr; /* next node page block address */
} __attribute__((packed));
+static_assert(sizeof(struct node_footer) == 24, "");
+
struct f2fs_node {
/* can be one of three types: inode, direct, and indirect types */
union {
@@ -1049,6 +1070,8 @@ struct f2fs_node {
struct node_footer footer;
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_node) == 4096, "");
+
/*
* For NAT entries
*/
@@ -1063,10 +1086,14 @@ struct f2fs_nat_entry {
__le32 block_addr; /* block address */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_nat_entry) == 9, "");
+
struct f2fs_nat_block {
struct f2fs_nat_entry entries[NAT_ENTRY_PER_BLOCK];
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_nat_block) == 4095, "");
+
/*
* For SIT entries
*
@@ -1106,10 +1133,14 @@ struct f2fs_sit_entry {
__le64 mtime; /* segment age for cleaning */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_sit_entry) == 74, "");
+
struct f2fs_sit_block {
struct f2fs_sit_entry entries[SIT_ENTRY_PER_BLOCK];
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_sit_block) == 4070, "");
+
/*
* For segment summary
*
@@ -1142,6 +1173,8 @@ struct f2fs_summary {
};
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_summary) == 7, "");
+
/* summary block type, node or data, is stored to the summary_footer */
#define SUM_TYPE_NODE (1)
#define SUM_TYPE_DATA (0)
@@ -1151,6 +1184,8 @@ struct summary_footer {
__le32 check_sum; /* summary checksum */
} __attribute__((packed));
+static_assert(sizeof(struct summary_footer) == 5, "");
+
#define SUM_JOURNAL_SIZE (F2FS_BLKSIZE - SUM_FOOTER_SIZE -\
SUM_ENTRIES_SIZE)
#define NAT_JOURNAL_ENTRIES ((SUM_JOURNAL_SIZE - 2) /\
@@ -1182,26 +1217,36 @@ struct nat_journal_entry {
struct f2fs_nat_entry ne;
} __attribute__((packed));
+static_assert(sizeof(struct nat_journal_entry) == 13, "");
+
struct nat_journal {
struct nat_journal_entry entries[NAT_JOURNAL_ENTRIES];
__u8 reserved[NAT_JOURNAL_RESERVED];
} __attribute__((packed));
+static_assert(sizeof(struct nat_journal) == 505, "");
+
struct sit_journal_entry {
__le32 segno;
struct f2fs_sit_entry se;
} __attribute__((packed));
+static_assert(sizeof(struct sit_journal_entry) == 78, "");
+
struct sit_journal {
struct sit_journal_entry entries[SIT_JOURNAL_ENTRIES];
__u8 reserved[SIT_JOURNAL_RESERVED];
} __attribute__((packed));
+static_assert(sizeof(struct sit_journal) == 505, "");
+
struct f2fs_extra_info {
__le64 kbytes_written;
__u8 reserved[EXTRA_INFO_RESERVED];
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_extra_info) == 505, "");
+
struct f2fs_journal {
union {
__le16 n_nats;
@@ -1215,6 +1260,8 @@ struct f2fs_journal {
};
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_journal) == 507, "");
+
/* 4KB-sized summary block structure */
struct f2fs_summary_block {
struct f2fs_summary entries[ENTRIES_IN_SUM];
@@ -1222,6 +1269,8 @@ struct f2fs_summary_block {
struct summary_footer footer;
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_summary_block) == 4096, "");
+
/*
* For directory operations
*/
@@ -1263,6 +1312,8 @@ struct f2fs_dir_entry {
__u8 file_type; /* file type */
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_dir_entry) == 11, "");
+
/* 4KB-sized directory entry block */
struct f2fs_dentry_block {
/* validity bitmap for directory entries in each block */
@@ -1271,6 +1322,9 @@ struct f2fs_dentry_block {
struct f2fs_dir_entry dentry[NR_DENTRY_IN_BLOCK];
__u8 filename[NR_DENTRY_IN_BLOCK][F2FS_SLOT_LEN];
} __attribute__((packed));
+
+static_assert(sizeof(struct f2fs_dentry_block) == 4096, "");
+
#pragma pack(pop)
/* for inline stuff */
diff --git a/include/quota.h b/include/quota.h
index 627a86f6421f..5b7aaa891d28 100644
--- a/include/quota.h
+++ b/include/quota.h
@@ -50,6 +50,8 @@ struct v2_disk_dqheader {
uint32_t dqh_version; /* File version */
} __attribute__ ((packed));
+static_assert(sizeof(struct v2_disk_dqheader) == 8, "");
+
/* Header with type and version specific information */
struct v2_disk_dqinfo {
uint32_t dqi_bgrace; /* Time before block soft limit becomes hard limit */
@@ -60,6 +62,8 @@ struct v2_disk_dqinfo {
uint32_t dqi_free_entry; /* Number of block with at least one free entry */
} __attribute__ ((packed));
+static_assert(sizeof(struct v2_disk_dqinfo) == 24, "");
+
struct v2r1_disk_dqblk {
__le32 dqb_id; /* id this quota applies to */
__le32 dqb_pad;
@@ -74,6 +78,9 @@ struct v2r1_disk_dqblk {
__le64 dqb_btime; /* time limit for excessive disk use */
__le64 dqb_itime; /* time limit for excessive inode use */
} __attribute__ ((packed));
+
+static_assert(sizeof(struct v2r1_disk_dqblk) == 72, "");
+
#pragma pack(pop)
#endif
diff --git a/tools/f2fscrypt.c b/tools/f2fscrypt.c
index fe3e0ff3c1a9..97624ba5e185 100644
--- a/tools/f2fscrypt.c
+++ b/tools/f2fscrypt.c
@@ -104,6 +104,8 @@ struct f2fs_fscrypt_policy {
__u8 master_key_descriptor[F2FS_KEY_DESCRIPTOR_SIZE];
} __attribute__((packed));
+static_assert(sizeof(struct f2fs_fscrypt_policy) == 12, "");
+
#define F2FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct f2fs_fscrypt_policy)
#define F2FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
#define F2FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct f2fs_fscrypt_policy)
@@ -121,6 +123,8 @@ struct f2fs_encryption_key {
__u32 size;
} __attribute__((__packed__));
+static_assert(sizeof(struct f2fs_encryption_key) == 72, "");
+
int options;
extern void f2fs_sha512(const unsigned char *in, unsigned long in_size,
_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
next prev parent reply other threads:[~2022-04-21 22:19 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-21 22:18 [f2fs-dev] [PATCH 00/31] Make f2fs-tools easier to maintain Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 01/31] configure.ac: Stop using obsolete macros Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 02/31] configure.ac: Remove two prototype tests Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 03/31] configure.ac: Enable the automake -Wall option Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 04/31] configure.ac: Sort header file names alphabetically Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 05/31] configure.ac: Enable cross-compilation Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 06/31] Switch from the u_int to the uint types Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 07/31] Change the ANDROID_WINDOWS_HOST macro into _WIN32 Bart Van Assche
2022-04-22 17:53 ` Jaegeuk Kim
2022-04-21 22:18 ` [f2fs-dev] [PATCH 08/31] ci: Build f2fstools upon push and pull requests Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 09/31] Change one array member into a flexible array member Bart Van Assche
2022-04-21 22:18 ` Bart Van Assche [this message]
2022-06-10 2:05 ` [f2fs-dev] [PATCH 10/31] Verify structure sizes at compile time Peter Collingbourne via Linux-f2fs-devel
2022-06-10 17:12 ` Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 11/31] Suppress a compiler warning Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 12/31] f2fs_fs.h: Use standard fixed width integer types Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 13/31] Remove unnecessary __attribute__((packed)) annotations Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 14/31] Move the be32_to_cpu() definition Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 15/31] Include <stddef.h> instead of defining offsetof() Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 16/31] Use %zu to format size_t Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 17/31] Fix the MinGW build Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 18/31] configure.ac: Detect the sparse/sparse.h header Bart Van Assche
2022-04-22 19:01 ` Jaegeuk Kim
2022-04-21 22:18 ` [f2fs-dev] [PATCH 19/31] configure.ac: Detect selinux/android.h Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 20/31] mkfs/f2fs_format.c: Suppress a compiler warning Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 21/31] fsck: Remove a superfluous include directive Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 22/31] tools/f2fscrypt.c: Fix build without uuid/uuid.h header file Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 23/31] fsck/main.c: Suppress a compiler warning Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 24/31] Change #ifdef _WIN32 checks into #ifdef HAVE_.* Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 25/31] fsck/segment.c: Remove dead code Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 26/31] tools/f2fs_io: Fix the type of 'ret' Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 27/31] Annotate switch/case fallthrough Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 28/31] Suppress a compiler warning about integer truncation Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 29/31] Support cross-compiliation for PowerPC Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 30/31] Fix PowerPC format string warnings Bart Van Assche
2022-04-21 22:18 ` [f2fs-dev] [PATCH 31/31] ci: Enable -Wall, -Wextra and -Werror Bart Van Assche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220421221836.3935616-11-bvanassche@acm.org \
--to=bvanassche@acm.org \
--cc=jaegeuk@kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).