Re: How is NULL pointer dereference handled inside kernel?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> > I remember a big discussion happening on LKML on this
> > topic (i.e., NULL vs 0) a couple of months back. It must
> > be still available in their mail archives. Try googling for
> > "Use NULL instead of integer 0" & you must find it. Must
> > say it was a pretty heated discussion. Dont know what
> > the outcome was. I stopped following it after sometime.
> Tried some thing and pasting a copy here...which tells clearly abt NULL
> pointer and using zero(integer)
Tried some thing and pasting a copy here...which tells clearly abt NULL
pointer and using zero(integer)
C code -- used
file name : null.c
int main()
{
        int *p= NULL;
        return 0;
}

file name : zero.c
int main()
{
        int p = 0;
        return 0;
}

Assembly snippet on 64 bit machine for null.c
main:
.LFB3:
        pushq   %rbp
.LCFI0:
        movq    %rsp, %rbp
.LCFI1:
        movq    $0, -8(%rbp)   <------------
        movl    $0, %eax
        leave
        ret

Assembly snippet on  64 bit machine for zero.c
main:
.LFB3:
        pushq   %rbp
.LCFI0:
        movq    %rsp, %rbp
.LCFI1:
        movl    $0, -4(%rbp)    <-------------
        movl    $0, %eax
        leave
        ret
Check out the 7th line..... of both the snippets
If we see here 8 bytes are deducted from base pointer(in 64 bit machine -- 
when variable p was NULL pointer)
and 4 bytes are deducted from rbp when p was integer....(obvious... Right
!!!!)
and thus the code differs.......

Whereas in 32 bit machine, assembly snippets for null.c and zero.c are
similar.
main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        addl    $15, %eax
        addl    $15, %eax
        shrl    $4, %eax
        sall    $4, %eax
        subl    %eax, %esp
        movl    $0, -4(%ebp)
        movl    $0, %eax
        leave
        ret

Interesting conclusions and concept to imbibe :)
Regards,
Vishal.


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux