Mkrasimage segmentation fault

Compiling a snapshot firmware for my Armor Z2 I am getting a segmentation fault when it attempts to build the factory image.

The command it is running appears to be ...

/home/mtf/openwrt-armor-z2/staging_dir/host/bin/mkrasimage -b NBG6817 -v "V1.99(OWRT.9999)C0" -r /home/mtf/openwrt-armor-z2/build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-ipq806x_generic/tmp/openwrt-ipq806x-generic-zyxel_nbg6817-squashfs-factory.bin -s 21436416 -o /home/mtf/openwrt-armor-z2/build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-ipq806x_generic/tmp/openwrt-ipq806x-generic-zyxel_nbg6817-squashfs-factory.bin.new -k /home/mtf/openwrt-armor-z2/build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-ipq806x_generic/zyxel_nbg6817-uImage && mv /home/mtf/openwrt-armor-z2/build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-ipq806x_generic/tmp/openwrt-ipq806x-generic-zyxel_nbg6817-squashfs-factory.bin.new /home/mtf/openwrt-armor-z2/build_dir/target-arm_cortex-a15+neon-vfpv4_musl_eabi/linux-ipq806x_generic/tmp/openwrt-ipq806x-generic-zyxel_nbg6817-squashfs-factory.bin

I have put this command into gdb and I get ...

Program received signal SIGSEGV, Segmentation fault.
__memmove_sse2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:405
405	../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory.
(gdb) bt
#0  __memmove_sse2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:405
#1  0x000055555555549e in build_image ()
#2  0x00007ffff7a05b97 in __libc_start_main (main=0x555555554c70 <main>, argc=13, argv=0x7fffffffe2a8, init=<optimised out>, fini=<optimised out>, rtld_fini=<optimised out>, stack_end=0x7fffffffe298) at ../csu/libc-start.c:310
#3  0x0000555555554dda in _start ()
(gdb) 

I believe the error is in tools/firmware-utils/src/mkrasimage.c it attempts to ensure that the output file is big enough at line 462 which reads ...

    rootfs_out.size = rootfs_size < rootfs.size ? rootfs.size : rootfs_size;

However the value of rootfs.size isn't known at that point, so it always picks rootfs_size instead, even though in my case rootfs.size should be larger, consequently when it reaches line 330 of this file and does:

    memcpy(rootfs_out.data, rootfs.data, rootfs.size);

it causes the segmentation fault.

Moving the code from line 462 to line 320 fixes the problem.

Not sure how to have this fix added, but hopefully somebody on this forum will be happy to do it?

I believe this is the right patch ...

diff --git a/tools/firmware-utils/src/mkrasimage.c b/tools/firmware-utils/src/mkrasimage.c
index cff3a533d1..d8cec527fb 100644
--- a/tools/firmware-utils/src/mkrasimage.c
+++ b/tools/firmware-utils/src/mkrasimage.c
@@ -317,6 +317,16 @@ int build_image()
         map_file(&kernel);
     map_file(&rootfs);
 
+    /* As ZyXEL Web-GUI only accept images with a rootfs equal or larger than the first firmware shipped
+     * for the device, we need to pad rootfs partition to this size. To perform further calculations, we
+     * decide the size of this part here. In case the rootfs we want to integrate in our image is larger,
+     * take it's size, otherwise the supplied size.
+     *
+     * Be careful! We rely on assertion of correct size to be performed beforehand. It is unknown if images
+     * with a to large rootfs are accepted or not.
+     */
+    rootfs_out.size = rootfs_size < rootfs.size ? rootfs.size : rootfs_size;
+
     /*
      * Allocate memory and copy input rootfs for temporary output rootfs.
      * This is important as we have to generate the rootfs checksum over the
@@ -451,14 +461,5 @@ int main(int argc, char *argv[])
     if (ret)
         usage(EXIT_FAILURE);
 
-    /* As ZyXEL Web-GUI only accept images with a rootfs equal or larger than the first firmware shipped
-     * for the device, we need to pad rootfs partition to this size. To perform further calculations, we
-     * decide the size of this part here. In case the rootfs we want to integrate in our image is larger,
-     * take it's size, otherwise the supplied size.
-     *
-     * Be careful! We rely on assertion of correct size to be performed beforehand. It is unknown if images
-     * with a to large rootfs are accepted or not.
-     */
-    rootfs_out.size = rootfs_size < rootfs.size ? rootfs.size : rootfs_size;
     return build_image();
 }

Just read http://openwrt.org/submitting-patches and feel free to ask if something is not clear.

Looking at the documentation it appears to be a remarkably good way of obscuring how to submit a patch!

I eventually worked out that I needed to send an email to the mailing list and took a look at it to try to work out roughly what it should look like. Hopefully I have sent something that meets the requirements!