- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下非常简单的 C 程序 (test.c
):
int f(int i)
{
return i;
}
int main(int argC, char* argV[])
{
int x = f(12);
return 1;
}
我用 valgrind 做了一些测试,在两个不同的系统中使用 -fstack-check
编译器标志。
使用 gcc 4.9.2 和 valgrind 3.10.0 在 Debian 8.6 上完成测试。
没有-fstack-check
:
$ gcc test.c -o test
$ valgrind ./test
==103703== Memcheck, a memory error detector
==103703== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==103703== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==103703== Command: ./test
==103703==
==103703==
==103703== HEAP SUMMARY:
==103703== in use at exit: 0 bytes in 0 blocks
==103703== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==103703==
==103703== All heap blocks were freed -- no leaks are possible
==103703==
==103703== For counts of detected and suppressed errors, rerun with: -v
==103703== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
使用-fstack-check
:
$ gcc -fstack-check test.c -o test
$ valgrind ./test
==103726== Memcheck, a memory error detector
==103726== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==103726== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==103726== Command: ./test
==103726==
==103726==
==103726== HEAP SUMMARY:
==103726== in use at exit: 0 bytes in 0 blocks
==103726== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==103726==
==103726== All heap blocks were freed -- no leaks are possible
==103726==
==103726== For counts of detected and suppressed errors, rerun with: -v
==103726== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
在 CentOS 6.6 中使用 gcc 4.4.7 和 valgrind 3.8.1 进行的测试:
没有-fstack-check
:
$ gcc test.c -o test
$ valgrind ./test
==16390== Memcheck, a memory error detector
==16390== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==16390== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==16390== Command: ./test
==16390==
==16390==
==16390== HEAP SUMMARY:
==16390== in use at exit: 0 bytes in 0 blocks
==16390== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==16390==
==16390== All heap blocks were freed -- no leaks are possible
==16390==
==16390== For counts of detected and suppressed errors, rerun with: -v
==16390== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
使用-fstack-check
:
$ gcc -fstack-check test.c -o test
$ valgrind ./test
==16441== Memcheck, a memory error detector
==16441== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==16441== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==16441== Command: ./test
==16441==
==16441== Invalid write of size 8
==16441== at 0x400497: main (in /home/fermin/valgrindtest/test)
==16441== Address 0x7feffd058 is not stack'd, malloc'd or (recently) free'd
==16441==
==16441==
==16441== Process terminating with default action of signal 11 (SIGSEGV)
==16441== Access not within mapped region at address 0x7FEFFD058
==16441== at 0x400497: main (in /home/fermin/valgrindtest/test)
==16441== If you believe this happened as a result of a stack
==16441== overflow in your program's main thread (unlikely but
==16441== possible), you can try to increase the size of the
==16441== main thread stack using the --main-stacksize= flag.
==16441== The main thread stack size used in this run was 10485760.
==16441==
==16441== HEAP SUMMARY:
==16441== in use at exit: 0 bytes in 0 blocks
==16441== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==16441==
==16441== All heap blocks were freed -- no leaks are possible
==16441==
==16441== For counts of detected and suppressed errors, rerun with: -v
==16441== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
在这种情况下,此外,程序以段错误结束。
总结,虽然在 Debian 中使用 -fstack-check
似乎一切正常,但在 CentOS 中我收到“无效写入”错误无法根据我的程序代码进行解释。
正在阅读 the -fstack-check
flag也许错误跟踪是由于堆栈检查机制在堆空间中为 x
变量分配了 8 个字节,但 valgrind(由于某种原因)无法意识到这一点,因此将其标记为作为无效写入。也许这是 valgrind 3.8.1 中的限制,已在 3.10.0 中解决(考虑到在 Debian 设置中,使用 3.10.0,我没有得到该跟踪)?
此外,我还查看了valgring FAQ “-fomit-frame-pointer
和 -fstack-check
会使堆栈跟踪变得更糟” 但它们没有提供太多关于这样做的原因所以我不知道问题是否相关...
非常感谢任何有助于解释这一点的帮助或提示。
编辑:正如一条评论中所建议的,我包括了 CentOS 6.x 案例的反汇编代码,使用 -g3
编译并使用 objdump --源测试
。我在这里包含了与 main() 和 f() 函数对应的部分,但是可以找到完整的转储 here .
使用 -fstack-check
编译的二进制文件:
0000000000400474 <f>:
int f(int i)
{
400474: 55 push %rbp
400475: 48 89 e5 mov %rsp,%rbp
400478: 89 7d fc mov %edi,-0x4(%rbp)
return i;
40047b: 8b 45 fc mov -0x4(%rbp),%eax
}
40047e: c9 leaveq
40047f: c3 retq
0000000000400480 <main>:
int main(int argC, char* argV[])
{
400480: 55 push %rbp
400481: 48 89 e5 mov %rsp,%rbp
400484: 48 83 ec 20 sub $0x20,%rsp
400488: 89 7d ec mov %edi,-0x14(%rbp)
40048b: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int x = f(12);
return 0;
40048f: 48 8d 84 24 08 d0 ff lea -0x2ff8(%rsp),%rax
400496: ff
400497: 48 c7 00 00 00 00 00 movq $0x0,(%rax)
return i;
}
int main(int argC, char* argV[])
{
int x = f(12);
40049e: bf 0c 00 00 00 mov $0xc,%edi
4004a3: e8 cc ff ff ff callq 400474 <f>
4004a8: 89 45 fc mov %eax,-0x4(%rbp)
return 0;
4004ab: b8 00 00 00 00 mov $0x0,%eax
}
4004b0: c9 leaveq
4004b1: c3 retq
没有 -fstack-check
编译的二进制文件:
0000000000400474 <f>:
int f(int i)
{
400474: 55 push %rbp
400475: 48 89 e5 mov %rsp,%rbp
400478: 89 7d fc mov %edi,-0x4(%rbp)
return i;
40047b: 8b 45 fc mov -0x4(%rbp),%eax
}
40047e: c9 leaveq
40047f: c3 retq
0000000000400480 <main>:
int main(int argC, char* argV[])
{
400480: 55 push %rbp
400481: 48 89 e5 mov %rsp,%rbp
400484: 48 83 ec 20 sub $0x20,%rsp
400488: 89 7d ec mov %edi,-0x14(%rbp)
40048b: 48 89 75 e0 mov %rsi,-0x20(%rbp)
int x = f(12);
40048f: bf 0c 00 00 00 mov $0xc,%edi
400494: e8 db ff ff ff callq 400474 <f>
400499: 89 45 fc mov %eax,-0x4(%rbp)
return 0;
40049c: b8 00 00 00 00 mov $0x0,%eax
}
4004a1: c9 leaveq
4004a2: c3 retq
EDIT2:我目前在 CentOS 6.8 中使用最新的 valgrind 版本 (3.13.0) 进行了测试,我遇到了同样的问题。
最佳答案
我已经在我的 CentOS 6.6 系统中安装了 gcc 4.7.2(使用 this procedure )并使用 -fstack-check
重新编译测试:
$ /opt/centos/devtoolset-1.1/root/usr/bin/gcc --version
gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ /opt/centos/devtoolset-1.1/root/usr/bin/gcc -fstack-check test.c -o test
$ valgrind ./test
==19374== Memcheck, a memory error detector
==19374== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==19374== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==19374== Command: ./test
==19374==
==19374==
==19374== HEAP SUMMARY:
==19374== in use at exit: 0 bytes in 0 blocks
==19374== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==19374==
==19374== All heap blocks were freed -- no leaks are possible
==19374==
==19374== For counts of detected and suppressed errors, rerun with: -v
==19374== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
请注意,现在“无效写入”错误未显示。因此,这似乎是 gcc 编译器中的一个问题,某种在 4.4.7 和 4.7.2 之间修复的错误,如@n.m。在评论中建议。
关于c - 使用 fstack-check 时出现意外的 valgrind "invalid write",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44758939/
这个问题在这里已经有了答案: What's the proper value for a checked attribute of an HTML checkbox? (10 个答案) 关闭 8 年
我使用这个制作了自定义复选框: enter link description here 也可在 stackoverflow 上获得:enter link description here 但我正在尝试
我需要使用 CSS“checkbox-hack”来实现滑动菜单指示器效果,我唯一的方法是通过 JavaScript 附加输入元素。我被迫通过在线工具 MonoSolutions 执行此操作,并且我受到
此代码运行良好,但缺少一些我需要的东西。基本上,如果输入有一个 checked="checked" 属性,它应该使其他两个元素保持禁用状态。如果未选中,则元素已启用。 这是我在 jsFiddle 上的
当我的人 checkout 文件时,我希望他们将其锁定,以便其他人也无法进行更改,我从这篇文章中看到:http://msdn.microsoft.com/en-us/library/jj155783.
请告诉我这些函数的作用。 最佳答案 这些是基于框架的、与语言无关的方法,用于在 .NET 中定义代码契约。虽然某些语言(如 spec# 和 Delphi Prism)对代码契约具有一流的语言支持,但这
假设以下场景:您有 2 个单选按钮,它们具有相同的名称,并且都被选中(我知道这是无效的): 为什么下面两个选择器的行为不同? $('.input:checked').size(); // retu
我正在尝试收听广播。以下均不起作用: [编辑] $('selector').attr('checked','checked'); $('selector').attr('checked',true);
我实际上在努力理解此类型错误。 任何人都知道我如何更正代码?谢谢 CheckIn checkin1 = new CheckIn(location1, dt); CheckInMonths checkI
我有这段代码,但不起作用。 .on("click","span.name", function selectThisName(e) { if (e.altKey) {
我现在是 Espresso 的新手,我遇到了这个异常: android.support.test.espresso.AmbiguousViewMatcherException: 'with id: a
我已经创建了一个基本的 2 单选按钮表单,如下面的示例所示。 观察浏览器渲染,我们看到元素 1 被选中。我们检查元素 1 和元素 2。 当我点击元素 2 时,我希望元素 1 的 checked=che
我在查找以下 jquery/checkbox 行为的原因时遇到问题。 $( this.obj + ' table.sgrid-content > thead > tr > th > input.sel
以下逻辑应用在上午 10 点触发并运行 SQL Server 查询。从图片中可以看出,结果集是空的。 条件检查检查查询的结果集是否为空。 (第二张图) 这仍然如何转化为 True?结果显然是空的。 最
我想知道哪种操作更快: int c = version1.compareTo(version2); 这个 if (c == 1) 或者这个 if (c > 0) 符号比较是否只使用一位检查,而相等比较
我有一个包含大约 100 个问题的表单,每个问题都有一个单选按钮和一些复选框,因此我需要用户能够保存表单并在以后加载它。我还需要检查用户在此 session 中更改了哪些。 本题解决问题:How ca
我正在编写一个小程序,需要用户决定一些 bool 值。我已经制作了复选框来处理这一部分,但问题是每次我选中或取消选中一个复选框时,所有其他复选框都会跟随。 我在网上搜索过,但我找到的唯一解释( pyt
我有以下代码片段(我使用的是 jQuery 1.4.2): $.post('/Ads/GetAdStatsRow/', { 'ad_id': id }, function(result) {
我的代码发生了一些奇怪的事情。我有两个按钮,其中一个带有 .add 和 .remove 类,有一个复选框会根据按下哪个按钮而打开和关闭,因此如果您使用删除按钮删除,则选中的复选框将被选中,否则复选框将
我陷入了一种情况,我必须通过“选中”工具栏中的复选框来“选中”列表中存在的所有复选框。 这是创建复选框列表的代码:- itemTpl: 'checked="checked" /> {groupName
我是一名优秀的程序员,十分优秀!