Skip to contentSkip to conversation

mmc-utils: mmc_cmds: fix type-punned warning on &ext_csd[] casting

PATCH Superseded Giulio Benetti 3 participants 7 messages Mar 5, 2024 – Mar 7, 2024 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc-utils: mmc_cmds: fix type-punned warning on &ext_csd[] casting

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Let's fix type-punned breaking strict-aliasing by memcpy() the variable.

---
 mmc_cmds.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mmc_cmds.c +2 -1
index ae7b876..dcd60aa 100644
@@ -2968,7 +2968,8 @@ do_retry:
 	}
 
 	/* Test if we need to restart the download */
-	sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+	memcpy(&sect_done, &ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0], sizeof(__u32));
+	sect_done = htole32(sect_done);
 	/* By spec, host should re-start download from the first sector if sect_done is 0 */
 	if (sect_done == 0) {
 		if (retry--) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 11 lines

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict- aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32
*)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Quoted 1 line

Let's fix type-punned breaking strict-aliasing by memcpy() the variable.

If any, I would just revert commit a1b594ca735e.

Thanks,
Avri

Quoted 26 lines
---
 mmc_cmds.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mmc_cmds.c +2 -1
index ae7b876..dcd60aa 100644
@@ -2968,7 +2968,8 @@ do_retry:
        }
 
        /* Test if we need to restart the download */
-       sect_done = htole32(*((__u32
 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+       memcpy(&sect_done, &ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0],
 sizeof(__u32));
+       sect_done = htole32(sect_done);
        /* By spec, host should re-start download from the first sector if sect_done

is 0 */

        if (sect_done == 0) {
                if (retry--) {

--
2.34.1

#2 lore raw
Quoted 10 lines
Quoted 10 lines

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict- aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32
*)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Quoted 5 lines
Quoted 1 line

Let's fix type-punned breaking strict-aliasing by memcpy() the variable.

If any, I would just revert commit a1b594ca735e.

Thanks,
Avri

Or, how about this untested option.

Thanks,
Avri

index ae7b876..10f7532 100644

mmc_cmds.c +9 -3
@@ -58,6 +58,11 @@
 #define WPTYPE_PWRON 2
 #define WPTYPE_PERM 3
 
+static inline __u32 strict_htole32(__u8 *arr)
+{
+       return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
+}
+
 int read_extcsd(int fd, __u8 *ext_csd)
 {
        int ret = 0;
@@ -2807,7 +2812,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
                               __u8 *ext_csd, unsigned int bytes, __u8 *buf,
                               off_t offset)
 {
-       __u32 arg = htole32(*((__u32 *)&ext_csd[EXT_CSD_FFU_ARG_0]));
+       __u32 arg = safe_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
 
        /* send block count */
        set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0,
@@ -2827,7 +2832,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
 int do_ffu(int nargs, char **argv)
 {
        int dev_fd, img_fd;
-       int sect_done = 0, retry = 3, ret = -EINVAL;
+       int retry = 3, ret = -EINVAL;
        unsigned int sect_size;
        __u8 ext_csd[512];
        __u8 *buf = NULL;
@@ -2835,6 +2840,7 @@ int do_ffu(int nargs, char **argv)
        char *device;
        struct mmc_ioc_multi_cmd *multi_cmd = NULL;
        unsigned int default_chunk = MMC_IOC_MAX_BYTES;
+       __u32 sect_done = 0;
 
        assert (nargs == 3 || nargs == 4);
 
@@ -2968,7 +2974,7 @@ do_retry:
        }
 
        /* Test if we need to restart the download */
-       sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+       sect_done = strict_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]);
        /* By spec, host should re-start download from the first sector if sect_done is 0 */
        if (sect_done == 0) {
                if (retry--) {
#3 lore raw
Quoted 10 lines · Avri Altman
Quoted 10 lines
Quoted 10 lines

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict- aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32

*)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
   ^

cc1: all warnings being treated as errors

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Let's fix type-punned breaking strict-aliasing by memcpy() the variable.

If any, I would just revert commit a1b594ca735e.

Thanks,
Avri

Or, how about this untested option.

This is a good solution, just re-spin and renamed to per_byte_htole32(__u8 *). Hope it sounds good the name to you.

Thank you
Best regards

Signature · 2 lines
Giulio Benetti
CEO&CTO@Benetti Engineering sas
Quoted 54 lines

Thanks,
Avri

index ae7b876..10f7532 100644

mmc_cmds.c +9 -3
@@ -58,6 +58,11 @@
  #define WPTYPE_PWRON 2
  #define WPTYPE_PERM 3
 
+static inline __u32 strict_htole32(__u8 *arr)
+{
+       return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
+}
+
  int read_extcsd(int fd, __u8 *ext_csd)
  {
         int ret = 0;
@@ -2807,7 +2812,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
                                __u8 *ext_csd, unsigned int bytes, __u8 *buf,
                                off_t offset)
  {
-       __u32 arg = htole32(*((__u32 *)&ext_csd[EXT_CSD_FFU_ARG_0]));
+       __u32 arg = safe_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
 
         /* send block count */
         set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0,
@@ -2827,7 +2832,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
  int do_ffu(int nargs, char **argv)
  {
         int dev_fd, img_fd;
-       int sect_done = 0, retry = 3, ret = -EINVAL;
+       int retry = 3, ret = -EINVAL;
         unsigned int sect_size;
         __u8 ext_csd[512];
         __u8 *buf = NULL;
@@ -2835,6 +2840,7 @@ int do_ffu(int nargs, char **argv)
         char *device;
         struct mmc_ioc_multi_cmd *multi_cmd = NULL;
         unsigned int default_chunk = MMC_IOC_MAX_BYTES;
+       __u32 sect_done = 0;
 
         assert (nargs == 3 || nargs == 4);
 
@@ -2968,7 +2974,7 @@ do_retry:
         }
 
         /* Test if we need to restart the download */
-       sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+       sect_done = strict_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]);
         /* By spec, host should re-start download from the first sector if sect_done is 0 */
         if (sect_done == 0) {
                 if (retry--) {
#4 lore raw

PATCH v2 1/1 mmc-utils: mmc_cmds: fix type-punned warning on &ext_csd[] casting

In reply to Avri Altman

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Let's fix type-punned breaking strict-aliasing by implementing local per_byte_htole32(__u8 *) and use it for the 2 present occurences of htole32(). Let's also change sect_done type to __u32 for consistency.

Fixes a1b594ca735e ("mmc-utils: ffu: Simplify ext_csd bytes parsing")

---
V1->V2:
* implement local per_byte_htole32() as suggested by Avril Altman

---
 mmc_cmds.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
mmc_cmds.c +9 -3
index ae7b876..936e0c5 100644
@@ -58,6 +58,11 @@
 #define WPTYPE_PWRON 2
 #define WPTYPE_PERM 3
 
+static inline __u32 per_byte_htole32(__u8 *arr)
+{
+	return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
+}
+
 int read_extcsd(int fd, __u8 *ext_csd)
 {
 	int ret = 0;
@@ -2807,7 +2812,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
 			       __u8 *ext_csd, unsigned int bytes, __u8 *buf,
 			       off_t offset)
 {
-	__u32 arg = htole32(*((__u32 *)&ext_csd[EXT_CSD_FFU_ARG_0]));
+	__u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
 
 	/* send block count */
 	set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0,
@@ -2827,7 +2832,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
 int do_ffu(int nargs, char **argv)
 {
 	int dev_fd, img_fd;
-	int sect_done = 0, retry = 3, ret = -EINVAL;
+	int retry = 3, ret = -EINVAL;
 	unsigned int sect_size;
 	__u8 ext_csd[512];
 	__u8 *buf = NULL;
@@ -2835,6 +2840,7 @@ int do_ffu(int nargs, char **argv)
 	char *device;
 	struct mmc_ioc_multi_cmd *multi_cmd = NULL;
 	unsigned int default_chunk = MMC_IOC_MAX_BYTES;
+	__u32 sect_done = 0;
 
 	assert (nargs == 3 || nargs == 4);
 
@@ -2968,7 +2974,7 @@ do_retry:
 	}
 
 	/* Test if we need to restart the download */
-	sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+	sect_done = per_byte_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]);
 	/* By spec, host should re-start download from the first sector if sect_done is 0 */
 	if (sect_done == 0) {
 		if (retry--) {
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#5 lore raw
Quoted 10 lines

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict- aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32
*)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Quoted 7 lines

Let's fix type-punned breaking strict-aliasing by implementing local per_byte_htole32(__u8 *) and use it for the 2 present occurences of htole32(). Let's also change sect_done type to __u32 for consistency.

Fixes a1b594ca735e ("mmc-utils: ffu: Simplify ext_csd bytes parsing")
Quoted 65 lines

---
V1->V2:
* implement local per_byte_htole32() as suggested by Avril Altman

---
 mmc_cmds.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
mmc_cmds.c +6 -1
index ae7b876..936e0c5 100644
@@ -58,6 +58,11 @@
 #define WPTYPE_PWRON 2
 #define WPTYPE_PERM 3
 
+static inline __u32 per_byte_htole32(__u8 *arr)
+{
+       return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
+}
+
 int read_extcsd(int fd, __u8 *ext_csd)
 {
        int ret = 0;
@@ -2807,7 +2812,7 @@ static void set_ffu_single_cmd(struct
 mmc_ioc_multi_cmd *multi_cmd,
                               __u8 *ext_csd, unsigned int bytes, __u8 *buf,
                               off_t offset)
 {
-       __u32 arg = htole32(*((__u32 *)&ext_csd[EXT_CSD_FFU_ARG_0]));
+       __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
 
        /* send block count */
        set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0,
mmc_cmds.c +1 -1
@@ -2827,7 +2832,7 @@ static void set_ffu_single_cmd(struct
 mmc_ioc_multi_cmd *multi_cmd,
 int do_ffu(int nargs, char **argv)
 {
        int dev_fd, img_fd;
-       int sect_done = 0, retry = 3, ret = -EINVAL;
+       int retry = 3, ret = -EINVAL;
        unsigned int sect_size;
        __u8 ext_csd[512];
        __u8 *buf = NULL;
mmc_cmds.c +2 -1
@@ -2835,6 +2840,7 @@ int do_ffu(int nargs, char **argv)
        char *device;
        struct mmc_ioc_multi_cmd *multi_cmd = NULL;
        unsigned int default_chunk = MMC_IOC_MAX_BYTES;
+       __u32 sect_done = 0;
 
        assert (nargs == 3 || nargs == 4);
 
@@ -2968,7 +2974,7 @@ do_retry:
        }
 
        /* Test if we need to restart the download */
-       sect_done = htole32(*((__u32
 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+       sect_done =
 per_byte_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]);
        /* By spec, host should re-start download from the first sector if sect_done

is 0 */

        if (sect_done == 0) {
                if (retry--) {

--
2.34.1

Applied
#6 lore raw
In reply to Giulio Benetti
Quoted 9 lines · Giulio Benetti

When building with -Werror=strict-aliasing error is thrown:

mmc_cmds.c: In function 'do_ffu':
mmc_cmds.c:2972:2: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
^
cc1: all warnings being treated as errors

Quoted 7 lines

Let's fix type-punned breaking strict-aliasing by implementing local per_byte_htole32(__u8 *) and use it for the 2 present occurences of htole32(). Let's also change sect_done type to __u32 for consistency.

Fixes a1b594ca735e ("mmc-utils: ffu: Simplify ext_csd bytes parsing")

Applied to git.kernel.org/pub/scm/utils/mmc/mmc-utils.git master, thanks!

Kind regards
Uffe

Quoted 62 lines

---
V1->V2:
* implement local per_byte_htole32() as suggested by Avril Altman

---
 mmc_cmds.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
mmc_cmds.c +9 -4
index ae7b876..936e0c5 100644
@@ -58,6 +58,11 @@
 #define WPTYPE_PWRON 2
 #define WPTYPE_PERM 3
 
+static inline __u32 per_byte_htole32(__u8 *arr)
+{
+       return arr[0] | arr[1] << 8 | arr[2] << 16 | arr[3] << 24;
+}
+
 int read_extcsd(int fd, __u8 *ext_csd)
 {
        int ret = 0;
@@ -2807,7 +2812,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
                               __u8 *ext_csd, unsigned int bytes, __u8 *buf,
                               off_t offset)
 {
-       __u32 arg = htole32(*((__u32 *)&ext_csd[EXT_CSD_FFU_ARG_0]));
+       __u32 arg = per_byte_htole32(&ext_csd[EXT_CSD_FFU_ARG_0]);
 
        /* send block count */
        set_single_cmd(&multi_cmd->cmds[1], MMC_SET_BLOCK_COUNT, 0, 0,
@@ -2827,7 +2832,7 @@ static void set_ffu_single_cmd(struct mmc_ioc_multi_cmd *multi_cmd,
 int do_ffu(int nargs, char **argv)
 {
        int dev_fd, img_fd;
-       int sect_done = 0, retry = 3, ret = -EINVAL;
+       int retry = 3, ret = -EINVAL;
        unsigned int sect_size;
        __u8 ext_csd[512];
        __u8 *buf = NULL;
@@ -2835,6 +2840,7 @@ int do_ffu(int nargs, char **argv)
        char *device;
        struct mmc_ioc_multi_cmd *multi_cmd = NULL;
        unsigned int default_chunk = MMC_IOC_MAX_BYTES;
+       __u32 sect_done = 0;
 
        assert (nargs == 3 || nargs == 4);
 
@@ -2968,7 +2974,7 @@ do_retry:
        }
 
        /* Test if we need to restart the download */
-       sect_done = htole32(*((__u32 *)&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]));
+       sect_done = per_byte_htole32(&ext_csd[EXT_CSD_NUM_OF_FW_SEC_PROG_0]);
        /* By spec, host should re-start download from the first sector if sect_done is 0 */
        if (sect_done == 0) {
                if (retry--) {
--

2.34.1