Thanks a lot Yuri for replying !
I am not so familiar with assembly and when i try to look into assembly
code generated  from both optimisation its not clear to me
I am pasting assembly code for main function below (for -O1and -O0) , could
you please point me exactly  what is happening ? here

With optimisation "-01"

 22         .cfi_startproc
 23         pushq   %rbx
 24         .cfi_def_cfa_offset 16
 25         .cfi_offset 3, -16
 26         movl    $25, %edi
 27         call    malloc
 28         movq    %rax, %rbx
 29         movl    $11, %edx
 30         movl    $.LC0, %esi
 31         movq    %rax, %rdi
 32         call    memcpy
 33         movq    %rbx, %rdx
 34         movq    %rbx, %rsi
 35         movl    $.LC1, %edi
 36         movl    $0, %eax
 37         call    printf
 38         movq    %rbx, %rdi
 39         call    strlen
 40         leaq    (%rbx,%rax), %rdi
 41         movl    $7, %edx
 42         movl    $.LC2, %esi
 43         call    memcpy
 44         movq    %rbx, %rdx
 45         movq    %rbx, %rsi
 46         movl    $.LC1, %edi
 47         movl    $0, %eax
 48         call    printf
 49         movl    $0, %eax
 50         popq    %rbx
 51         .cfi_def_cfa_offset 8
 52         ret
 53         .cfi_endproc
 54 .LFE20:

With optimization -O0

 28         subq    $16, %rsp
 29         movl    $25, %edi
 30         call    malloc
 31         movq    %rax, -8(%rbp)
 32         movq    -8(%rbp), %rax
 33         movl    $11, %edx
 34         movl    $.LC0, %esi
 35         movq    %rax, %rdi
 36         call    memcpy
 37         movq    -8(%rbp), %rdx
 38         movq    -8(%rbp), %rax
 39         movq    %rax, %rsi
 40         movl    $.LC1, %edi
 41         movl    $0, %eax
 42         call    printf
 43         movq    -8(%rbp), %rax
 44         movq    %rax, %rdi
 45         call    strlen
 46         movq    %rax, %rdx
 47         movq    -8(%rbp), %rax
 48         addq    %rdx, %rax
 49         movl    $7, %edx
 50         movl    $.LC2, %esi
 51         movq    %rax, %rdi
 52         call    memcpy
 53         movq    -8(%rbp), %rdx
 54         movq    -8(%rbp), %rax
 55         movq    %rax, %rsi
 56         movl    $.LC1, %edi
 57         movl    $0, %eax
 58         call    printf
 59         movl    $0, %eax
 60         leave
 61         .cfi_def_cfa 7, 8
 62         ret




Regards
Ankur raj
Oracle India pvt ltd:


On Fri, Feb 15, 2019 at 12:05 PM Yuri Gribov <[email protected]> wrote:

> Hi Ankur,
>
> If pointer to allocated buffer manages to survive in one of the
> registers or on stack (after program returns from main), LSan would
> consider buffer to be reachable and won't report it. This behavior is
> very sensitive to compiler/Glibc version and compilation flags.
>
> On 2/15/19, Ankur Raj <[email protected]> wrote:
> > Hi Folks ,
> > Wondering if someone can explain this
> > Adress sanitizer does not report and leak in this code below when
> compiled
> > with O1 , O2 ,,,
> >
> > But it works as expected when compiled with O0
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main () {
> >    volatile char *str;
> >
> >    /* Initial memory allocation */
> >    str = (char *) malloc(25);
> >    strcpy(str, "sameple st");
> >    printf("String = %s,  Address = %u\n", str, str);
> >
> >    strcat(str, "append");
> >    printf("String = %s,  Address = %u\n", str, str);
> >
> >
> >    return(0);
> > }
> >
> > I am compiling this code as
> >
> > gcc -fsanitize=address -O0 a.c
> > gcc --version
> > gcc (GCC) 8.2.1 20190102
> >
> > I looked at asm code also and clearly malloc has not been removed by
> > optimizers
> >
> >  22         .cfi_startproc
> >  23         pushq   %rbp
> >  24         .cfi_def_cfa_offset 16
> >  25         .cfi_offset 6, -16
> >  26         movq    %rsp, %rbp
> >  27         .cfi_def_cfa_register 6
> >  28         subq    $16, %rsp
> >  29         movl    $25, %edi
> >  30         *call    malloc*
> >  31         movq    %rax, -8(%rbp)
> >  32         movq    -8(%rbp), %rax
> >  33         movl    $11, %edx
> >  34         movl    $.LC0, %esi
> >  35         movq    %rax, %rdi
> >  36         call    memcpy
> >
> > Any clue what is happening here ?
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "address-sanitizer" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
> > For more options, visit https://groups.google.com/d/optout.
> >
>
>
> --
> Best regards,
> Yuri
>
> --
> You received this message because you are subscribed to the Google Groups
> "address-sanitizer" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"address-sanitizer" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to