Skip to contentSkip to conversation

zfs: fix compilation failure with clang due to alignment

PATCH In review Andrei Borzenkov 3 participants 11 messages Jul 3, 2015 – Jul 16, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 zfs: fix compilation failure with clang due to alignment

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
#1 lore raw
Quoted 6 lines · Andrei Borzenkov

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +0 -0
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,

grub_disk_addr_t fileblock)
if (node->inode.format == XFS_INODE_FORMAT_BTREE)

     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 32 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 6 lines
tg: (7a21030..) u/xfs-clang-align (depends on: master)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

Go ahead

Quoted 101 lines · Leif Lindholm
Quoted 6 lines · Andrei Borzenkov

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 32 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 6 lines
tg: (7a21030..) u/xfs-clang-align (depends on: master)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Go ahead

The below was more of an RFC than something committable - are you OK with me splitting the types.h changes out as a separate patch?

Quoted 111 lines · Leif Lindholm
Quoted 91 lines · Andrei Borzenkov
Quoted 6 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 28 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
Quoted 10 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 6 lines · Leif Lindholm
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Go ahead

The below was more of an RFC than something committable - are you OK with me splitting the types.h changes out as a separate patch?

Yes

Quoted 118 lines
Quoted 107 lines · Leif Lindholm
Quoted 91 lines · Andrei Borzenkov
Quoted 6 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 28 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
Quoted 10 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
In reply to Leif Lindholm
Quoted 6 lines · Leif Lindholm
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Go ahead

The below was more of an RFC than something committable - are you OK with me splitting the types.h changes out as a separate patch?

Actually I think this patch doesn't work as __attribute__((aligned(X))) can only increase alignment, not decrease it. I'm going to fix it by using char * and grub_get_unaligned64

Quoted 118 lines
Quoted 107 lines · Leif Lindholm
Quoted 91 lines · Andrei Borzenkov
Quoted 6 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 28 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
Quoted 10 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw
Quoted 10 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 6 lines · Leif Lindholm
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Go ahead

The below was more of an RFC than something committable - are you OK with me splitting the types.h changes out as a separate patch?

Actually I think this patch doesn't work as __attribute__((aligned(X))) can only increase alignment, not decrease it. I'm going to fix it by using char * and grub_get_unaligned64

Yeah, you're right - it should actually have been (packed) again.

Anyway, you already contributed a fix.

/
Leif

Quoted 127 lines
Quoted 113 lines
Quoted 107 lines · Leif Lindholm
Quoted 91 lines · Andrei Borzenkov
Quoted 6 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 28 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
Quoted 10 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw
In reply to Leif Lindholm
Quoted 6 lines · Leif Lindholm
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Go ahead

The below was more of an RFC than something committable - are you OK with me splitting the types.h changes out as a separate patch?

I've fixed this and several other bad alignments in xfs.c

Quoted 118 lines
Quoted 107 lines · Leif Lindholm
Quoted 91 lines · Andrei Borzenkov
Quoted 6 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

Quoted 8 lines
  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

Well, the problem is that struct grub_xfs_btree_node is defined with GRUB_PACKED - forcing a 1-byte alignment requirement as opposed to the 8-byte requirement that would naturally be enforced by the struct contents. And apparently clang objects to this, whereas gcc thinks everything is fine ... even though -Wcast-align is explicitly used.

Now, grub_xfs_btree_keys() is only called by grub_xfs_read_block(), where it is immediately stuffed back into another 8-byte aligned pointer. And then the alignment is immediately discarded again by casting it to a (char *) for an arithmetic operation.

If the alignment is indeed not required, it may be worth explicitly marking that pointer as one to a potentially unaligned location. But we don't currently appear to have a GRUB_UNALIGNED macro, to match the GRUB_PACKED for structs. Should we?

If so, something like the following could be added to your patch for a more complete fix:

grub-core/fs/xfs.c +1 -1
@@ -488,7 +488,7 @@ grub_xfs_read_block (grub_fshelp_node_t node,
 grub_disk_addr_t fileblock)
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
       struct grub_xfs_btree_root *root;
-      const grub_uint64_t *keys;
+      const grub_uint64_t *keys GRUB_UNALIGNED;
       int recoffset;
 
       leaf = grub_malloc (node->data->bsize);
include/grub/types.h +2 -0
index e732efb..720e236 100644
@@ -30,6 +30,8 @@
 #define GRUB_PACKED __attribute__ ((packed))
 #endif
 
+#define GRUB_UNALIGNED __attribute__ ((aligned (1)))
+
 #ifdef GRUB_BUILD
 # define GRUB_CPU_SIZEOF_VOID_P        BUILD_SIZEOF_VOID_P
 # define GRUB_CPU_SIZEOF_LONG  BUILD_SIZEOF_LONG

/
Leif

Quoted 28 lines
 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +3 -3
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 
Signature · 1 line
tg: (7a21030..) u/xfs-clang-align (depends on: master)
Quoted 10 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw
In reply to Andrei Borzenkov
Quoted 30 lines · Andrei Borzenkov

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +1 -1
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)

grub-core/fs/xfs.c grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de) · line 458

Quoted 10 lines
grub-core/fs/xfs.c +2 -2
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 

This would only hide the problem behind void*. Leif's patch solves the problem. Another possibility is to analyze if packed is really required but most likely it is.

Quoted 1 line
#9 lore raw

В Wed, 15 Jul 2015 17:49:30 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 36 lines · Andrei Borzenkov
Quoted 34 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +2 -2
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
Quoted 12 lines

grub-core/fs/xfs.c grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de) · line 458

Quoted 6 lines
grub-core/fs/xfs.c +1 -1
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 

This would only hide the problem behind void*. Leif's patch solves the problem. Another possibility is to analyze if packed is really required but most likely it is.

Quoted 1 line

grub_uint64_t is not really required at all. The whole code is used to compute bite offset. So in reality this should simply be replaced by char *. I would rather avoid making code even more complicated.

Jan, do I miss something?

#10 lore raw
Quoted 52 lines · Andrei Borzenkov

В Wed, 15 Jul 2015 17:49:30 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:

Quoted 36 lines · Andrei Borzenkov
Quoted 34 lines

I do not claim I understand why clang complains, but this patch does fix it.

fs/xfs.c:452:25: error: cast from 'struct grub_xfs_btree_node *' to
'grub_uint64_t *' (aka 'unsigned long long *') increases required alignment from 1 to 8 [-Werror,-Wcast-align]

  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error generated.

---

Jan, do you have any idea what's wrong and whether this is proper fix? Or should I raise it with clang?

 grub-core/fs/xfs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
grub-core/fs/xfs.c +2 -2
index 7249291..ea8cf7e 100644
@@ -445,14 +445,14 @@ grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de)
   return (struct grub_xfs_dir2_entry *)(((char *)de) + ALIGN_UP(size, 8));
 }
 
-static grub_uint64_t *
+static void *
 grub_xfs_btree_keys(struct grub_xfs_data *data,
 		    struct grub_xfs_btree_node *leaf)
 {
-  grub_uint64_t *keys = (grub_uint64_t *)(leaf + 1);
+  char *keys = (char *)leaf + sizeof (*leaf);
 
Quoted 12 lines

grub-core/fs/xfs.c grub_xfs_next_de(struct grub_xfs_data *data, struct grub_xfs_dir2_entry *de) · line 458

Quoted 6 lines
grub-core/fs/xfs.c +1 -1
   if (data->hascrc)
-    keys += 6;	/* skip crc, uuid, ... */
+    keys += 6 * sizeof (grub_uint64_t);	/* skip crc, uuid, ... */
   return keys;
 }
 

This would only hide the problem behind void*. Leif's patch solves the problem. Another possibility is to analyze if packed is really required but most likely it is.

Quoted 1 line
Quoted 6 lines

grub_uint64_t is not really required at all. The whole code is used to compute bite offset. So in reality this should simply be replaced by char *. I would rather avoid making code even more complicated.

Jan, do I miss something?

We have exactly the same problem at line 500:

      keys = &root->keys[0];

and root is in packed struct. I think it's better to solve both in the same way.