How to catch data-alignment faults on x86 (aka SIGBUS on Sparc)

前端 未结 6 1000
耶瑟儿~
耶瑟儿~ 2020-12-25 15:30

Is it somehow possible to catch data-alignment faults even on i386? Maybe by setting a i386 specific machine register or something like that.

On Solaris-Sparc I am r

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-25 16:10

    I have found a very simple solution on SOF! See: Mis-aligned pointers on x86.

    int main(int argc, char **argv)
    {
    # if defined i386
        /* EDIT: enable AC check */
        asm("pushf; "
        "orl $(1<<18), (%esp); "
        "popf;");
    # endif
    
        char d[] = "12345678";  /* yep! - causes SIGBUS even on Linux-i386 */
        return 0;
    }
    

    But I must confess that I do not understand why the assignment

    char d[] = "12345678";

    is assumed to be mis-aligned?

    EDIT:

    on the SPARC machine there is no SIGBUS on the line of assignment to char d[].

提交回复
热议问题