OS X - x64: stack not 16 byte aligned error

后端 未结 1 1791
臣服心动
臣服心动 2021-01-13 02:10

I know that OS X is 16 byte stack align, but I don\'t really understand why it is causing an error here.

All I am doing here is to pass an object size (whic

相关标签:
1条回答
  • 2021-01-13 02:48

    Like you said, MacOS X has a 16 byte stack alignment, which means that the machine expects each variable on the stack to start on a byte that is a multiple of 16 from the current stack pointer.

    When the stack is misaligned, it means we start trying to read variables from the middle of that 16 byte window and usually end up with a segmentation fault.

    Before you call a routine in your code, you need to make sure that your stack is aligned correctly; in this case, meaning that the base pointer register is divisible by 16.

    subq $8, %rsp               # stack is misaligned by 8 bytes
    movq %rdi, 8(%rsp)          #
    movq obj_size(%rdi), %rax   #
    imul $8, %rax               #
    movq %rax, %rdi             #
    callq _malloc               # stack is still misaligned when this is called
    

    To fix this, you can subq the %rsp by something like 16 instead of 8.

    subq $16, %rsp               # stack is still aligned
    movq %rdi, 16(%rsp)          #
    ...                          #
    callq _malloc                # stack is still aligned when this is called, good
    
    0 讨论(0)
提交回复
热议问题