Search code examples
macosassemblyapple-m1arm64

Why does strlen give segfault in assembly?


I'm trying to make a request to localhost. I want to know length of my message, but can't figure out what am I doing wrong. Output is like:

    helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS  helloS./run.sh: line 1: 10066 Segmentation fault: 11

This is what lldb gives: This is what lldb gives

Code is here. But when replace

adr x0, _msg
bl _strlen

to

mov x0, #5

everything works fine.


Solution

  • The issue is that your _send implementation doesn't preserve x30.

    When you hardcode mov x0, #5, then there is nothing in that function that corrupts x30. But when you call _strlen, then that sets x30 to the address of str x0, [sp, #4], which means that the part of _send after the call to _strlen will run over and over again, each time increasing sp by 16 bytes until you reach the top of the stack and hit unmapped memory.

    The fix is to spill x30 to the stack before the call to _strlen, and to reload it from there afterwards.