gpt4 book ai didi

c - 带电围栏库的gcc不生效

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:32 25 4
gpt4 key购买 nike

我写了一个片段,在动态分配时有内存问题;当用-lefence 选项编译时,似乎没有效果。这是代码段:

int main(int argc, char *argv[])
{
int *a = (int *)malloc(2*sizeof(int));
for(int i = 0; i <=2; ++i){
a[i] = i;
printf ("%d\n",a[i]);
}

free(a);
return 0;
}

以及编译选项:

gcc -g3 -Wall -std=c99 outOfBound.c -lefence

预期的结果是,当a.out 被执行时,i 被分配给2 并且a[i]=i 之后会有一个核心转储 被调用。

那么为什么 -lefence 没有效果?

我还将循环中的上限增加到 9,但仍然没有 electric-fence 调用的核心转储。 (实际上默认情况下确实有一个核心转储,但这可能是由于 MALLOC_CHECK_ env virable 因为当我 export MALLOC_CHECK_=0 时,将不再有核心转储) .

更新:nm -A a.out 的全部结果如下:

a.out:08049f28 d _DYNAMIC
a.out:08049ff4 d _GLOBAL_OFFSET_TABLE_
a.out:0804864c R _IO_stdin_used
a.out: w _Jv_RegisterClasses
a.out:08049f18 d __CTOR_END__
a.out:08049f14 d __CTOR_LIST__
a.out:08049f20 d __DTOR_END__
a.out:08049f1c d __DTOR_LIST__
a.out:08048718 r __FRAME_END__
a.out:08049f24 d __JCR_END__
a.out:08049f24 d __JCR_LIST__
a.out:0804a01c A __bss_start
a.out:0804a014 D __data_start
a.out:08048600 t __do_global_ctors_aux
a.out:08048480 t __do_global_dtors_aux
a.out:0804a018 d __dso_handle
a.out: w __gmon_start__
a.out:080485f2 t __i686.get_pc_thunk.bx
a.out:00000000 a __init_array_end
a.out:00000000 a __init_array_start
a.out:080485f0 T __libc_csu_fini
a.out:08048580 T __libc_csu_init
a.out: U __libc_start_main
a.out:0804a01c A _edata
a.out:0804a024 A _end
a.out:0804862c T _fini
a.out:08048648 R _fp_hw
a.out:080483b4 T _init
a.out:08048450 T _start
a.out:0804a01c b completed.6159
a.out:0804a014 W data_start
a.out:0804a020 b dtor_idx.6161
a.out:080484e0 t frame_dummy
a.out: U free
a.out:08048504 T main
a.out: U malloc
a.out: U printf

(我在 Ubuntu 12.04 32 位,gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 上使用 debian 包 electric-fence)

更新(20140801):

对于debian打包的2.2.4版本的electric-fence(testing分支,即jessie),是可以的。

最佳答案

您可能遇到了这个问题。

... it must increase the size of the allocation to a multiple of the word size. In addition, the functions memalign() and valloc() must honor explicit specifications on the alignment of the memory allocation, and this, as well can only be implemented by increasing the size of the allocation. Thus, there will be situations in which the end of a memory allocation contains some padding space, and accesses of that padding space will not be detected, even if they are overruns.

再尝试超出边界一点,看看在什么时候开始进行超限检测。

关于c - 带电围栏库的gcc不生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com