Skip to content

Commit 43ad0c9

Browse files
committed
shmem: pad the end of header up to a multiple of pointer size
So that the following topology is well aligned on 128 bits architectures. No change on 32/64bits architectures. Signed-off-by: Brice Goglin <[email protected]>
1 parent 2e40224 commit 43ad0c9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

hwloc/shmem.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct hwloc_shmem_header {
2323
uint32_t header_length; /* where the actual topology starts in the file/mapping */
2424
uint64_t mmap_address; /* virtual address to pass to mmap */
2525
uint64_t mmap_length; /* length to pass to mmap (includes the header) */
26+
/* we will pad the end to a multiple of pointer size so that the topology is well aligned */
2627
};
2728

2829
#define HWLOC_SHMEM_MALLOC_ALIGN 8UL
@@ -85,6 +86,7 @@ hwloc_shmem_topology_write(hwloc_topology_t topology,
8586
hwloc_topology_t new;
8687
struct hwloc_tma tma;
8788
struct hwloc_shmem_header header;
89+
uint32_t header_length = (sizeof(header) + sizeof(void*) - 1) & ~(sizeof(void*) - 1); /* pad to a multiple of pointer size */
8890
void *mmap_res;
8991
int err;
9092

@@ -100,7 +102,7 @@ hwloc_shmem_topology_write(hwloc_topology_t topology,
100102
hwloc_internal_memattrs_refresh(topology);
101103

102104
header.header_version = HWLOC_SHMEM_HEADER_VERSION;
103-
header.header_length = sizeof(header);
105+
header.header_length = header_length;
104106
header.mmap_address = (uintptr_t) mmap_address;
105107
header.mmap_length = length;
106108

@@ -127,7 +129,7 @@ hwloc_shmem_topology_write(hwloc_topology_t topology,
127129

128130
tma.malloc = tma_shmem_malloc;
129131
tma.dontfree = 1;
130-
tma.data = (char *)mmap_res + sizeof(header);
132+
tma.data = (char *)mmap_res + header_length;
131133
err = hwloc__topology_dup(&new, topology, &tma);
132134
if (err < 0)
133135
return err;
@@ -154,6 +156,7 @@ hwloc_shmem_topology_adopt(hwloc_topology_t *topologyp,
154156
{
155157
hwloc_topology_t new, old;
156158
struct hwloc_shmem_header header;
159+
uint32_t header_length = (sizeof(header) + sizeof(void*) - 1) & ~(sizeof(void*) - 1); /* pad to a multiple of pointer size */
157160
void *mmap_res;
158161
int err;
159162

@@ -171,7 +174,7 @@ hwloc_shmem_topology_adopt(hwloc_topology_t *topologyp,
171174
return -1;
172175

173176
if (header.header_version != HWLOC_SHMEM_HEADER_VERSION
174-
|| header.header_length != sizeof(header)
177+
|| header.header_length != header_length
175178
|| header.mmap_address != (uintptr_t) mmap_address
176179
|| header.mmap_length != length) {
177180
errno = EINVAL;
@@ -186,7 +189,7 @@ hwloc_shmem_topology_adopt(hwloc_topology_t *topologyp,
186189
goto out_with_mmap;
187190
}
188191

189-
old = (hwloc_topology_t)((char*)mmap_address + sizeof(header));
192+
old = (hwloc_topology_t)((char*)mmap_address + header_length);
190193
if (hwloc_topology_abi_check(old) < 0) {
191194
errno = EINVAL;
192195
goto out_with_mmap;

0 commit comments

Comments
 (0)