3

I'm doing some experimentation with editing a process memory while it's running, and I noticed when I call calloc in a gdb'd process, the call seems to work and return the original passed pointer, but the memory does not appear to be initialized to 0:

(gdb) call calloc(1, 32)
$88 = (void *) 0x8d9d50
(gdb) x/8xw 0x8d9d50
0x8d9d50:       0xf74a87d8      0x00007fff      0xf74a87d8      0x00007fff
0x8d9d60:       0xfbfbfbfb      0xfbfbfbfb      0x00000000      0x9b510000

If I call memset on the resulting pointer, however, the initialization works just fine:

(gdb) call memset(0x8d9d50, 0, 32)
$89 = 9280848
(gdb) x/8xw 0x8d9d50
0x8d9d50:       0x00000000      0x00000000      0x00000000      0x00000000
0x8d9d60:       0x00000000      0x00000000      0x00000000      0x00000000
Dan
  • 1,925
  • 3
  • 22
  • 28

1 Answers1

9

Interesting question. The answer is: on Linux (where I assume you ran your program) this:

(gdb) call calloc(1, 32)

doesn't call calloc from libc.so.6.

Rather it calls calloc from ld-linux.so.2. And that calloc is very minimal. It expects to be called only from ld-linux.so.2 itself, and it assumes that whatever pages it has access to are "clean" and do not require a memset. (That said, I could not reproduce the unclean calloc using glibc-2.19).

You can confirm this like so:

#include <stdlib.h>
int main()
{
  void *p = calloc(1, 10);
  return p == 0;
}

gcc -g foo.c -m32 && gdb -q ./a.out

Reading symbols from ./a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x8048426: file foo.c, line 4.
Starting program: /tmp/a.out 

Temporary breakpoint 1, main () at foo.c:4
warning: Source file is more recent than executable.
4         void *p = calloc(1, 10);
(gdb) b __libc_calloc
Breakpoint 2 at 0xf7e845a0
(gdb) n

Breakpoint 2, 0xf7e845a0 in calloc () from /lib32/libc.so.6
(gdb) fin
Run till exit from #0  0xf7e845a0 in calloc () from /lib32/libc.so.6
0x0804843a in main () at foo.c:4
4         void *p = calloc(1, 10);

Note how the call from the program to calloc hit breakpoint #2.

(gdb) n
5         return p == 0;
(gdb) call calloc(1,32)
$1 = 134524952

Note that above call from GDB did not hit breakpoint #2.

Let's try again with:

(gdb) info func calloc
All functions matching regular expression "calloc":

Non-debugging symbols:
0x08048310  calloc@plt
0xf7fdc820  calloc@plt
0xf7ff16a0  calloc
0xf7e25450  calloc@plt
0xf7e845a0  __libc_calloc
0xf7e845a0  calloc

(gdb) info sym 0xf7ff16a0
calloc in section .text of /lib/ld-linux.so.2    ## this is the wrong one!

(gdb) break *0xf7ff16a0
Breakpoint 3, 0xf7ff16a0 in calloc () from /lib/ld-linux.so.2

(gdb) disable    
(gdb) start
Temporary breakpoint 7 at 0x8048426: file foo.c, line 4.
Starting program: /tmp/a.out 

Temporary breakpoint 7, main () at foo.c:4
4         void *p = calloc(1, 10);
(gdb) ena 3
(gdb) n
5         return p == 0;

Note that breakpoint #3 did not fire above (because the "real" __libc_calloc was called).

(gdb) call calloc(1,32)

Breakpoint 3, 0xf7ff16a0 in calloc () from /lib/ld-linux.so.2
The program being debugged stopped while in a function called from GDB.
Evaluation of the expression containing the function
(calloc) will be abandoned.
When the function is done executing, GDB will silently stop.
(gdb) bt
#0  0xf7ff16a0 in calloc () from /lib/ld-linux.so.2
#1  <function called from gdb>
#2  main () at foo.c:5

QED.

Update:

I don't see the ld-linux version in the output of "info func calloc"

I think what you see in info func depends on whether you have debug symbols installed. For a (64-bit) glibc with debug symbols, here is what I see:

(gdb) info func calloc
All functions matching regular expression "calloc":

File dl-minimal.c:
void *calloc(size_t, size_t);         <<< this is the wrong one!

File malloc.c:
void *__libc_calloc(size_t, size_t);  <<< this is the one you want!

Non-debugging symbols:
0x0000000000400440  calloc@plt
0x00007ffff7ddaab0  calloc@plt
0x00007ffff7a344e0  calloc@plt

Here is another way to figure out what calloc GDB thinks it should be calling:

(gdb) start
Temporary breakpoint 1 at 0x8048426: file foo.c, line 4.
Starting program: /tmp/a.out

Temporary breakpoint 1, main () at foo.c:4
warning: Source file is more recent than executable.
4     void *p = calloc(1, 10);
(gdb) p &calloc
$1 = (<text variable, no debug info> *) 0xf7ff16a0 <calloc>
(gdb) info sym 0xf7ff16a0
calloc in section .text of /lib/ld-linux.so.2

Or, for completness, using 64-bit glibc with debug symbols:

(gdb) start
Temporary breakpoint 1 at 0x400555: file foo.c, line 4.
Starting program: /tmp/a.out

Temporary breakpoint 1, main () at foo.c:4
4     void *p = calloc(1, 10);
(gdb) p &calloc
$1 = (void *(*)(size_t, size_t)) 0x7ffff7df1bc0 <calloc>
(gdb) info sym 0x7ffff7df1bc0
calloc in section .text of /lib64/ld-linux-x86-64.so.2
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Though the explanation makes sense, I don't see the ld-linux version in the output of "info func calloc". I've tried putting breakpoints on everything it does list there, but the call within gdb still doesn't hit any of them. – Dan Sep 29 '16 at 04:46
  • Amazing! Accepting the answer now. – Dan Sep 29 '16 at 04:55
  • I've just had a fight with this: https://www.linuxquestions.org/questions/programming-9/debugging-dlopen-4175668676/ I wish I've found this earlier;) Well done and thank you very much. – Lorinczy Zsigmond Jan 30 '20 at 17:51