2

Consider the following code, I compile and run on aarch64:

struct test_msg {
    uint64_t seq;
    char data[4096];
};

static inline void serialize(void *dest, const struct test_msg *src)
{
    memcpy(dest, &src->seq, sizeof(src->seq));
    strcpy((char*)dest + sizeof(src->seq), src->data);
}

It will break at strcpy((char*)dest + sizeof(src->seq), src->data); I used strcpy() as in following link But if I change it to

static inline void serialize(void *dest, const struct test_msg *src)
{
    memcpy(dest, &src->seq, sizeof(src->seq));
    // strcpy((char*)dest + sizeof(src->seq), src->data);
    memcpy((char*)dest + sizeof(src->seq), src->data, sizeof(src->data));
}

The code will run as normal.

I tried to implement strcpy() myself as:

void heo_strcpy(char *s, char *t)
{
    while(*s++ = *t++)
    ;
}

And it will work also. I think there will be no problem with my src->data as it was like this before strcpy(), zero terminated:

data = "shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_", '\000' <repeats 4024 times>}

The question is what happened to strcpy()? Is there any misaligned access to the memory?

I performed a debug session and got the output as following:

In the last line before it jump to Bus error, the MCU trying to store the q1 register to [x0]. I don't understand this part as why the q1 register (which is 128 bits wide) appear here, and which part of C code did I wrong to cause this problem.

(gdb) display/i $pc
1: x/i $pc
=> 0x7ff7eaab40 <strcpy>:       bti     c
(gdb) si
0x0000007ff7eaab44 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab44 <strcpy+4>:     and     x2, x1, #0xfffffffffffffff0
(gdb)
0x0000007ff7eaab48 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab48 <strcpy+8>:     mov     w5, #0xf00f                     // #61455
(gdb)
0x0000007ff7eaab4c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab4c <strcpy+12>:    ld1     {v0.16b}, [x2]
(gdb)
0x0000007ff7eaab50 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab50 <strcpy+16>:    dup     v2.8h, w5
(gdb)
0x0000007ff7eaab54 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab54 <strcpy+20>:    cmeq    v1.16b, v0.16b, #0
(gdb)
0x0000007ff7eaab58 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab58 <strcpy+24>:    lsl     x5, x1, #2
(gdb)
0x0000007ff7eaab5c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab5c <strcpy+28>:    and     v1.16b, v1.16b, v2.16b
(gdb)
0x0000007ff7eaab60 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab60 <strcpy+32>:    addp    v3.16b, v1.16b, v1.16b
(gdb)
0x0000007ff7eaab64 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab64 <strcpy+36>:    fmov    x4, d3
(gdb)
0x0000007ff7eaab68 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab68 <strcpy+40>:    lsr     x4, x4, x5
(gdb)
0x0000007ff7eaab6c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab6c <strcpy+44>:    cbnz    x4, 0x7ff7eaabb4 <strcpy+116>
(gdb)
0x0000007ff7eaab70 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab70 <strcpy+48>:    ldr     q0, [x2, #16]!
(gdb)
0x0000007ff7eaab74 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab74 <strcpy+52>:    cmeq    v1.16b, v0.16b, #0
(gdb)
0x0000007ff7eaab78 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab78 <strcpy+56>:    and     v1.16b, v1.16b, v2.16b
(gdb)
0x0000007ff7eaab7c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab7c <strcpy+60>:    addp    v3.16b, v1.16b, v1.16b
(gdb)
0x0000007ff7eaab80 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab80 <strcpy+64>:    fmov    x4, d3
(gdb)
0x0000007ff7eaab84 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaab84 <strcpy+68>:    cbz     x4, 0x7ff7eaac10 <strcpy+208>
(gdb)
0x0000007ff7eaac10 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaac10 <strcpy+208>:   sub     x4, x2, x1
(gdb)
0x0000007ff7eaac14 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaac14 <strcpy+212>:   ldr     q1, [x1]
(gdb)
0x0000007ff7eaac18 in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaac18 <strcpy+216>:   add     x3, x0, x4
(gdb)
0x0000007ff7eaac1c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaac1c <strcpy+220>:   str     q1, [x0]
(gdb)

Thread 1 "shm_sys_ts_004." received signal SIGBUS, Bus error.
0x0000007ff7eaac1c in strcpy () from /lib/libc.so.6
1: x/i $pc
=> 0x7ff7eaac1c <strcpy+220>:   str     q1, [x0]
(gdb) info all-registers
x0             0x7ff7d2c018        549618630680
x1             0x555557c958        366504036696
x2             0x555557c960        366504036704
x3             0x7ff7d2c020        549618630688
x4             0x8                 8
x5             0x155555f2560       1466016146784
x6             0xff1f31302f5e6273  -63277786736991629
x7             0x7f7f7f7f7f7f7f7f  9187201950435737471
x8             0x101010101010101   72340172838076673
x9             0x5555567be8        366503951336
x10            0x20                32
x11            0x0                 0
x12            0x74696e69206d6873  8388357178748921971
x13            0xa64657a696c6169   748835014479470953
x14            0x7ff7f4cef0        549620862704
x15            0x0                 0
x16            0x5555579ee0        366504025824
x17            0x7ff7eaab40        549620198208
x18            0x0                 0
x19            0x55555665e0        366503945696
x20            0x0                 0
x21            0x5555558b40        366503889728
x22            0x0                 0
x23            0x0                 0
x24            0x0                 0
x25            0x0                 0
x26            0x0                 0
x27            0x0                 0
x28            0x0                 0
x29            0x7ffffffaa0        549755812512
x30            0x5555559074        366503891060
sp             0x7ffffffaa0        0x7ffffffaa0
pc             0x7ff7eaac1c        0x7ff7eaac1c <strcpy+220>
cpsr           0x60200000          [ EL=0 SS C Z ]
v0             {d = {f = {0x7fffffffffffffff, 0x7fffffffffffffff}, u = {0x73203231305f6374, 0x745f7379735f6d68}, s = {0x73203231305f6374, 0x745f7379735f6d68}}, s = {f = {0x0, 0xffffffff, 0xffffffff, 0xffffffff}, u = {0x305f6374, 0x73203231, 0x735f6d68, 0x745f7379}, s = {0x305f6374, 0x73203231, 0x735f6d68, 0x745f7379}}, h = {f = {0x3ba, 0x0, 0x0, 0x3900, 0x15a0, 0x3af8, 0x3bc8, 0x45f0}, u = {0x6374, 0x305f, 0x3231, 0x7320, 0x6d68, 0x735f, 0x7379, 0x745f}, s = {0x6374, 0x305f, 0x3231, 0x7320, 0x6d68, 0x735f, 0x7379, 0x745f}}, b = {u = {0x74, 0x63, 0x5f, 0x30, 0x31, 0x32, 0x20, 0x73, 0x68, 0x6d, 0x5f, 0x73, 0x79, 0x73, 0x5f, 0x74}, s = {0x74, 0x63, 0x5f, 0x30, 0x31, 0x32, 0x20, 0x73, 0x68, 0x6d, 0x5f, 0x73, 0x79, 0x73, 0x5f, 0x74}}, q = {u = {0x745f7379735f6d6873203231305f6374}, s = {0x745f7379735f6d6873203231305f6374}}}
v1             {d = {f = {0x7fffffffffffffff, 0x7fffffffffffffff}, u = {0x5f7379735f6d6873, 0x73203231305f6374}, s = {0x5f7379735f6d6873, 0x73203231305f6374}}, s = {f = {0xffffffff, 0xffffffff, 0x0, 0xffffffff}, u = {0x5f6d6873, 0x5f737973, 0x305f6374, 0x73203231}, s = {0x5f6d6873, 0x5f737973, 0x305f6374, 0x73203231}}, h = {f = {0x8e6, 0x1db, 0xae60, 0x1dc, 0x3ba, 0x0, 0x0, 0x3900}, u = {0x6873, 0x5f6d, 0x7973, 0x5f73, 0x6374, 0x305f, 0x3231, 0x7320}, s = {0x6873, 0x5f6d, 0x7973, 0x5f73, 0x6374, 0x305f, 0x3231, 0x7320}}, b = {u = {0x73, 0x68, 0x6d, 0x5f, 0x73, 0x79, 0x73, 0x5f, 0x74, 0x63, 0x5f, 0x30, 0x31, 0x32, 0x20, 0x73}, s = {0x73, 0x68, 0x6d, 0x5f, 0x73, 0x79, 0x73, 0x5f, 0x74, 0x63, 0x5f, 0x30, 0x31, 0x32, 0x20, 0x73}}, q = {u = {0x73203231305f63745f7379735f6d6873}, s = {0x73203231305f63745f7379735f6d6873}}}
v2             {d = {f = {0x8000000000000000, 0x8000000000000000}, u = {0xf00ff00ff00ff00f, 0xf00ff00ff00ff00f}, s = {0xf00ff00ff00ff00f, 0xf00ff00ff00ff00f}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0xf00ff00f, 0xf00ff00f, 0xf00ff00f, 0xf00ff00f}, s = {0xf00ff00f, 0xf00ff00f, 0xf00ff00f, 0xf00ff00f}}, h = {f = {0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88}, u = {0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f}, s = {0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f}}, b = {u = {0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0}, s = {0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0}}, q = {u = {0xf00ff00ff00ff00ff00ff00ff00ff00f}, s = {0xf00ff00ff00ff00ff00ff00ff00ff00f}}}
v3             {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v4             {d = {f = {0x8000000000000000, 0x8000000000000000}, u = {0xf00ff00ff00ff00f, 0xf00ff00ff00ff00f}, s = {0xf00ff00ff00ff00f, 0xf00ff00ff00ff00f}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0xf00ff00f, 0xf00ff00f, 0xf00ff00f, 0xf00ff00f}, s = {0xf00ff00f, 0xf00ff00f, 0xf00ff00f, 0xf00ff00f}}, h = {f = {0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88, 0xdf88}, u = {0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f}, s = {0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f, 0xf00f}}, b = {u = {0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0}, s = {0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0, 0xf, 0xf0}}, q = {u = {0xf00ff00ff00ff00ff00ff00ff00ff00f}, s = {0xf00ff00ff00ff00ff00ff00ff00ff00f}}}
v5             {d = {f = {0x0, 0x0}, u = {0xffffff00, 0xffffff00}, s = {0xffffff00, 0xffffff00}}, s = {f = {0xffffffff, 0x0, 0xffffffff, 0x0}, u = {0xffffff00, 0x0, 0xffffff00, 0x0}, s = {0xffffff00, 0x0, 0xffffff00, 0x0}}, h = {f = {0xffff, 0xffff, 0x0, 0x0, 0xffff, 0xffff, 0x0, 0x0}, u = {0xff00, 0xffff, 0x0, 0x0, 0xff00, 0xffff, 0x0, 0x0}, s = {0xff00, --Type <RET> for more, q to quit, c to continue without paging--c
0xffff, 0x0, 0x0, 0xff00, 0xffff, 0x0, 0x0}}, b = {u = {0x0, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0}}, q = {u = {0xffffff0000000000ffffff00}, s = {0xffffff0000000000ffffff00}}}
v6             {d = {f = {0x0, 0x0}, u = {0xc0000000000, 0xc0000000000}, s = {0xc0000000000, 0xc0000000000}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0xc00, 0x0, 0xc00}, s = {0x0, 0xc00, 0x0, 0xc00}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0xc00, 0x0, 0x0, 0x0, 0xc00, 0x0}, s = {0x0, 0x0, 0xc00, 0x0, 0x0, 0x0, 0xc00, 0x0}}, b = {u = {0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0}}, q = {u = {0xc000000000000000c0000000000}, s = {0xc000000000000000c0000000000}}}
v7             {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v8             {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v9             {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v10            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v11            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v12            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v13            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v14            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v15            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v16            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v17            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v18            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v19            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v20            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v21            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v22            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v23            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v24            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v25            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v26            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v27            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v28            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v29            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v30            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
v31            {d = {f = {0x0, 0x0}, u = {0x0, 0x0}, s = {0x0, 0x0}}, s = {f = {0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0}}, h = {f = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, u = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, s = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, b = {u = {0x0 <repeats 16 times>}, s = {0x0 <repeats 16 times>}}, q = {u = {0x0}, s = {0x0}}}
fpsr           0x0                 0
fpcr           0x0                 0
q0             {u = 0x745f7379735f6d6873203231305f6374, s = 0x745f7379735f6d6873203231305f6374} {u = 154686057812029274796657393729043129204, s = 154686057812029274796657393729043129204}
q1             {u = 0x73203231305f63745f7379735f6d6873, s = 0x73203231305f63745f7379735f6d6873} {u = 153028391032369183735794571568917342323, s = 153028391032369183735794571568917342323}

Jens
  • 69,818
  • 15
  • 125
  • 179
Minh Do
  • 43
  • 7
  • 1
    You really should be casting to a structure pointer rather than making wild assumptions about the structure layout. What is `dest`? Is that just a raw buffer? What format do you want the resulting "serialized" structure in? It's not clear how this code differs from just dumping the structure as-is. – tadman Mar 19 '21 at 08:31
  • So.. is `src->data` a zero terminated string? What standard library are you using (newlib? glibc?)? – KamilCuk Mar 19 '21 at 08:33
  • 2
    `strcpy()` will *only* work on well-formed C strings. Tip: **DO NOT USE `strcpy()`**. Use `strncpy()` which will not overflow your buffer by accident. You likely have a malformed, corrupted, or unterminated C string here that's causing `strcpy()` to run off the rails and crash. This function is extremely dangerous to use, *always* steer towards length-limited versions like `strncpy()`. – tadman Mar 19 '21 at 08:33
  • I think there will be no problem with my src->data as it was like this before strcpy(), zero terminated: ``` ata = "shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_012 shm_sys_tc_", '\000' } ``` – Minh Do Mar 19 '21 at 08:40
  • You *assume* there will be no problem, and that's where mistakes happen. `strcpy()` only malfunctions this way when it can't find the NUL terminator and crashes outside of bounds. You're showing `ata`. How did that get into your struct? Or is that short for `data`? – tadman Mar 19 '21 at 08:42
  • 1
    When building in debug mode or running in a debugger, the run-time or the debugger could set some data to zero, which doesn't happen when running a release build or outside of a debugger. Treat all uninitialized data as truly random or garbage (it's really *indeterminate*) and you will be fine. – Some programmer dude Mar 19 '21 at 08:43
  • Are you also *sure* that `data` is the correct size? `void*` pointers provide absolutely no assurances. – tadman Mar 19 '21 at 08:43
  • @MinhDo please note that `memcpy` copies only the specified number of bytes, contrary to `strcpy`, which calculates the buffer size based on the first occurence of '\0'. Given that the string is not null-terminated, too much data will be copied effectively leading to this kind of error. But this just the assumption based on the the symptoms. – alagner Mar 19 '21 at 08:44
  • @tadman @KamilCuk Thank you for your reply. `dest` is the share memory I allocated in shared memory, I can't share the code as it complicated (I don't really understand) and confidential. But I think It's fine because it worked with memcpy(). – Minh Do Mar 19 '21 at 08:45
  • I'm almost certain that this is a problem with using `strcpy()`. Step 1: Switch to `strncpy()`. Step 2: Verify that the string is in fact *actually* `NUL` terminated. – tadman Mar 19 '21 at 08:46
  • 1
    As for why it's using a 128 bit register? Code like `strcpy()` and `memcpy()` tend to be highly optimized and will use the fastest possible copying method. That's probably involving the largest possible register to move as much data per iteration in the loop. – tadman Mar 19 '21 at 08:47
  • @tadman I tried with strncpy(). ```strncpy((char*)dest + sizeof(src->seq), src->data, sizeof(src->data));```` Still the same error – Minh Do Mar 19 '21 at 09:08
  • 1
    None of the theories above seem consistent with the debug output. The SIGBUS occurs on the very first store to `[x0]`, so it's not an overrun. The address in `x0` isn't at the beginning of a page, and the `memcpy` which would have written the address 8 bytes previous didn't crash, so the page being written must be writable. And two instructions before the crash, we had `ldr q1, [x1]` from an address which is only 8-byte aligned, so unaligned 16-byte memory access must be enabled. Unless there is some mode in which unaligned SIMD loads succeed but unaligned stores crash? – Nate Eldredge Mar 20 '21 at 05:45
  • 1
    What kind of device is this running on? Does it have an OS, and if so what? What do you know about its settings as far as alignment checking? Oh! Is `dest` by some chance marked as device memory? That could explain why unaligned accesses fail there but succeed elsewhere. – Nate Eldredge Mar 20 '21 at 06:11
  • 1
    It's possible in theory that the `memcpy` and `strcpy` were reordered, if the compiler could prove when inlining that `dest` and `src->data` don't alias. If so then the `strcpy` might actually be the first write to `dest`, and so maybe the memory where `dest` points isn't accessible/writable after all. Disassembly of the surrounding code could show whether that's the case. I think it's unlikely though - I like my "device memory" theory better. – Nate Eldredge Mar 20 '21 at 13:47
  • I used strcpy() as in following [link](https://github.com/bminor/glibc/blob/master/sysdeps/aarch64/strcpy.S) I tried to implement strcpy() myself as: ` void heo_strcpy(char *s, char *t) { while(*s++ = *t++) ; } ` And it also work. – Minh Do Mar 24 '21 at 06:14
  • @NateEldredge this code run on Linux aarch64. `dest` is memory acquired on SRAM. – Minh Do Mar 24 '21 at 06:24
  • So, everything seems to be pointing to the unaligned access being the problem, and only for `dest` but not for other areas of memory. The only way I know for this to happen is if `dest` points to device memory (unless someone else knows another possibility?). What's not clear to me is whether that should be the case or not in your environment. If it is correctly marked as device memory, then I think your only option is copying with a loop as you have done [...] – Nate Eldredge Mar 24 '21 at 14:08
  • [...] but you probably also need to make it a `volatile` pointer for safety, else the compiler may optimize it back to using unaligned multi-byte accesses. If it is incorrectly marked as device memory, you have to figure out why. It would be helpful to see the exact code which allocates this memory gets allocated as I can't tell from your notes so far how you're doing this. – Nate Eldredge Mar 24 '21 at 14:10

0 Answers0