1

我使用 pin 来分析我的简单程序。

它有 4 个 malloc 函数,但是,当我将 pin 与名为 malloctrace 的 pintool 一起使用时,它会显示超过 4 个 malloc。

这些 malloc 函数是什么?

我的操作系统是 Ubuntu 12.04 64 位。以下项目是我制作的代码和 pintool 的结果。

#include <stdio.h>
#define SIZE 100

int main()
{
    int *test1 = (int*)malloc(SIZE* sizeof(int));
    int *test2 = (int*)malloc(SIZE* sizeof(int));
    int i, j;
    int *test3 = (int*)malloc(16*sizeof(int));
    int *test4 = (char*)malloc(SIZE* sizeof(int));

    test1[0] = 2;
    test1[2] = 3;
    test2[0] = 5;

    printf("test1's addr : %p , test1's val = %d    \n", test1, test1[0]);
    printf("test1's addr : %p , test1's val = %d    \n", &test1[1], test1[1]);
    printf("test2's addr : %p , test2's val = %d    \n", &test1[9], test1[9]);


    return 0;
}

       name           size          start_addr           malloc_IP          access_cnt
     malloc          0x589                   0      0x7ffff7de557e                   0
     malloc          0x489                   0      0x7ffff7de557e                   0
     malloc            0xf                   0      0x7ffff7ddd29e                   0
     malloc           0x4b                   0      0x7ffff7df01b2                   0
     malloc           0x28                   0      0x7ffff7de1fe7                   0
     malloc         0x14a0                   0      0x7ffff7de202c                   0
     malloc           0x28                   0      0x7ffff7de22ad                   0
     malloc           0x48                   0      0x7ffff7ddf9d3                   0
     malloc           0x5c                   0      0x7ffff7ddf9d3                   0
     malloc           0x5c                   0      0x7ffff7ddf9d3                   0
     malloc           0x39                   0      0x7ffff7ddf9d3                   0
     malloc           0x20                   0      0x7ffff7de294e                   0
     malloc          0x492                   0      0x7ffff7de557e                   0
     malloc           0x20                   0      0x7ffff7de57ed                   0
     malloc           0x28                   0      0x7ffff7de776f                   0
     malloc           0x38                   0      0x7ffff7de7900                   0
     malloc           0x48                   0      0x7ffff7deab5a                   0
     malloc           0x48                   0      0x7ffff7deab5a                   0
     malloc          0x228                   0      0x7ffff7deab5a                   0
     malloc           0x90                   0      0x7ffff7deab5a                   0
     malloc          0x410                   0      0x7ffff7ddaf22                   0
     malloc          0x110                   0      0x7ffff7debd52                   0
     malloc          0x190                   0            0x4013d2                   0
     malloc          0x190            0x603010            0x4013d2                   0
     malloc          0x190            0x6031b0            0x4013e0                   0
     malloc           0x40            0x603350            0x4013ee                   0
     malloc          0x190            0x6033a0            0x4013fc                   0
       free              0                   0            0x401688                   0
       free              0                   0            0x401688                   0
       free              0                   0            0x401688                   0
       free              0                   0            0x401688                   0
       free              0                   0            0x4016b0                   0
       free              0                   0            0x4016b0                   0
       free              0                   0            0x4016d7                   0
       free              0                   0            0x4016d7                   0
       free              0                   0            0x4016d7                   0
       free              0                   0            0x4016d7                   0
       free              1                   0            0x4016e8                   0
       free              0                   0            0x4016e8                   0
       free              0                   0            0x401718                   0
       free              0                   0            0x401718                   0
4

3 回答 3

1

其他东西很可能在幕后调用malloc,包括 C 运行时代码(例如,用于诸如 之类的东西使用的特定于线程的数据strtok),甚至是您的分析工具本身。

如果您检查所有这些内存块的起始地址,您会注意到除了您所做的之外,所有的都是0(100 个 4 字节int变量占用 op4000x190字节,其中 16 个占用640x40字节)。

可能与这里有关,尽管也可能是您是唯一一个不自己清理的人:-)


顺便说一句,你不应该malloc在 C 中转换返回值,因为它可以隐藏某些细微的错误,例如当你的int和指针大小不同的宽度并且你忘记包含stdlib.h时,这两种情况都可能在这里给你在 64 位环境中运行。

C 完全能够将void *返回的 from隐式转换malloc为任何其他指针类型。

于 2013-11-27T07:34:23.763 回答
1

您可以使用 gdb 找出发生了什么以及谁调用了malloc(). 在您的情况下,您会在 ld-linux.so 中看到很多中断:

Breakpoint 3, malloc (n=n@entry=136) at dl-minimal.c:93
93  in dl-minimal.c
(gdb) where
#0  malloc (n=n@entry=136) at dl-minimal.c:93
#1  0xb7ff3baa in calloc (nmemb=nmemb@entry=17, size=size@entry=8) at dl-minimal.c:113
#2  0xb7fef628 in allocate_dtv (result=result@entry=0xb7e00900) at dl-tls.c:296
#3  0xb7fefaf8 in _dl_allocate_tls_storage () at dl-tls.c:364
#4  0xb7fdecc7 in init_tls () at rtld.c:771
#5  0xb7fe0fcd in dl_main (phdr=0x8048034, phnum=9, user_entry=0xbfffedbc, auxv=0xbfffef5c) at rtld.c:1819
#6  0xb7ff33b6 in _dl_sysdep_start (start_argptr=start_argptr@entry=0xbfffee50, dl_main=dl_main@entry=0xb7fdf720 <dl_main>) at ../elf/dl-sysdep.c:241
#7  0xb7fe2dd4 in _dl_start_final (arg=0xbfffee50) at rtld.c:337
#8  _dl_start (arg=0xbfffee50) at rtld.c:563
#9  0xb7fdf197 in _start () from /lib/ld-linux.so.2

_start函数是程序的入口点,它在 main() 函数之前被调用。所以它在程序文本中不可见,但它存在,并且在启动时调用了不同的函数。
还有一个ldd实用程序可以显示可执行文件使用的所有动态库:

ldd main
    linux-gate.so.1 =>  (0xb7724000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7547000)
    /lib/ld-linux.so.2 (0xb7725000)

因此任何链接到可执行文件库的都可以使用 malloc()。

于 2013-11-27T07:44:34.783 回答
1

对你很重要的 malloc 调用是这个

 malloc          0x190            0x603010            0x4013d2                   0
 malloc          0x190            0x6031b0            0x4013e0                   0
 malloc           0x40            0x603350            0x4013ee                   0
 malloc          0x190            0x6033a0            0x4013fc    

一些系统调用在内部调用了 Rest。

于 2013-11-28T14:40:56.077 回答