【问题标题】:there is strange malloc functions which I not used有一些我没有使用过的奇怪 malloc 函数
【发布时间】:2013-12-12 17:34:05
【问题描述】:

我使用 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

【问题讨论】:

    标签: c memory-management malloc intel-pin


    【解决方案1】:

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

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

    可能在这里是相关的,尽管也可能是你是唯一一个不清理自己的人:-)


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

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

    【讨论】:

      【解决方案2】:

      您可以使用 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()。

      【讨论】:

        【解决方案3】:

        对你来说重要的 malloc 调用是这样的

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多