Search code examples
linuxubuntuassemblystrchr

Preloading custom strchr() - ubuntu crashes


I implemented that strchr()

        global  strchr
strchr:
        cmp     byte[rdi], 0
        je      end
        cmp     [rdi], sil
        je      end
        add     rdi, 1
        jmp     strchr
end:    mov     rax, rdi
        ret

When I preload it as .so using,

export LD_PRELOAD=abs/path/to/lib.so

Ubuntu 16.04 crashes. Sometimes it completly crahses, sometimes it displays SIGILL (corrupted data ?).

When I preload it using opensuse 4, it works.

Any idea why ?


Solution

  • Thanks to Michael Petch :

    That strchr() doesn't conform to manual for it doesn't return NULL when character is not found.

    Fixed strchr() :

    global  strchr
    strchr:
            cmp     [rdi], sil;first check for character (useful if user searches '\0')
            je      end
            cmp     byte[rdi], 0;then if it is EoS and the character is not in the string, return NULL
            je      eos
            add     rdi, 1
            jmp     strchr
    eos:    mov     rax, 0
            ret
    end:    mov     rax, rdi
            ret