# Linux Kernel Module Cheat

##### 38.16.1.5. Magic failure string

We do not know of any way to set the emulator exit status in QEMU arm full system.

For other arch / emulator combinations, we know how to do it:

- aarch64: aarch64 semihosting supports exit status

- gem5: m5 fail works on all archs

- user mode: QEMU forwards exit status, for gem5 we do some log parsing as described at: Section 11.7.2, “gem5 syscall emulation exit status”

Since we can’t do it for QEMU arm, the only reliable solution is to just parse the guest serial output for a magic failure string to check if tests failed.

Our run scripts parse the serial output looking for a line line containing only exactly the magic regular expression:

    lkmc_exit_status_(\d+)

and then exit with the given regular expression, e.g.:

    ./run --arch aarch64 baremetal/return2.c
    echo $?

should output:

    2

This magic output string is notably generated by:

- rootfs_overlay/lkmc/test_fail.sh, which is used by Test userland in full system

- the `exit()` baremetal function when `status != 1`.

  Unfortunately the only way we found to set this up was with `on_exit`: https://github.com/cirosantilli/linux-kernel-module-cheat/issues/59.

  Trying to patch `_exit` directly fails since at that point some de-initialization has already happened which prevents the print.

  So setup this `on_exit` automatically from all our Baremetal bootloaders, so it just works automatically for the examples that use the bootloaders: https://stackoverflow.com/questions/44097610/pass-parameter-to-atexit/49659697#49659697

  The following examples end up testing that our setup is working:

- userland/c/assert_fail.c

- userland/c/return0.c

- userland/c/return1.c

- userland/c/return2.c

- userland/c/exit0.c

- userland/c/exit1.c

- userland/c/exit2.c

- userland/posix/kill.c

Beware that on Linux kernel simulations, you cannot even echo that string from userland, since userland stdout shows up on the serial.