无效的读/写会导致SIGBUS错误吗?

时间:2019-06-25 17:06:59

标签: c gdb valgrind sigsegv sigbus

编辑1 :示例程序的平台为x86_64。

编辑2:为了更好的理解,我正在对其进行编辑。以下是两个不同的问题。第一个问题是无效的读写会导致SIGBUS吗?第二个问题是Valgrind对SIGBUS分析有用吗?示例代码是第二个问题,它支持我的观点,即在SIGBUS错误的情况下Valgrind根本没有用。我可能在这里错了。

实际情况::我们有一个屏幕阅读器应用,该应用在连续测试2天后崩溃(一次由于SIGBUS而崩溃)。我有一个coredump文件,但是我没有正确的二进制文件和调试包。因此,基本上我必须在不同的二进制文件中对此进行测试,并且由于调试程序包不匹配,因此coredump在gdb中无法正常工作。在Valgrind分析期间,我可以在屏幕阅读器模块中看到一些无效的读/写操作。我的队友建议,通过解决这些无效的读/写操作可以解决此问题,但我认为不会解决。以下是我对这两种信号的理解。

SIGSEGV::地址有效,但没有读/写权限。

SIGBUS:地址本身无效(由于不正确的匹配等原因,CPU无法找到地址)

我有一个与SIGBUS信号有关的问题。我已经在堆栈溢出中搜索了类似的问题,但是没有找到明确的答案。

无效的读/写是否会导致总线错误(SIGBUS)?

我的理解是,无效的读/写总是会导致分段错误(SIGSEGV),而修复总线错误的最佳方法是在应用程序上运行gdb。如果发生总线错误,Valgrind分析将完全没有帮助。下面的代码对此进行了更详细的说明。

#include<stdlib.h>
#include<stdio.h>

typedef struct {
char *name;
int val;
}data;

void fun1()
{
    data *ptr = malloc(sizeof(data));
    ptr->val = 100;
    ptr->name = "name in structure";

    printf("val:%d name:%s\n",ptr->val,ptr->name);
    free(ptr);
    ptr = NULL;
    printf("val:%d name:%s\n",ptr->val,ptr->name); //SIGSEGV
    return;
}

int fun2()
{
    #if defined(__GNUC__) 
    # if defined(__i386__) 
    /* Enable Alignment Checking on x86 */
    __asm__("pushf\norl $0x40000,(%esp)\npopf"); 
    # elif defined(__x86_64__)  
    /* Enable Alignment Checking on x86_64 */
    __asm__("pushf\norl $0x40000,(%rsp)\npopf"); 
    # endif 
    #endif 

    char *cptr = malloc(sizeof(int) + 1);
    char *optr = cptr;
    int *iptr = (int *) ++cptr; 
    *iptr = 42; //SIGBUS
    free(optr);

    return 0; 
}

void fun()
{
    fun2();
    //fun1();
}

int main()
{
    fun();
    return 0;
}

万一出现分段错误,Valgrind报告将包含导致崩溃的代码的详细信息,但如果SIGBUS崩溃,我在Valgrind报告中找不到任何此类详细信息。

SIGSEGV的Valgrind报告:

==28128== Memcheck, a memory error detector
==28128== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==28128== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==28128== Command: ./a.out
==28128== Parent PID: 27953
==28128== 
==28128== Invalid read of size 8
==28128==    at 0x400619: fun1 (tmp.c:18)
==28128==    by 0x400695: fun (tmp.c:46)
==28128==    by 0x4006A6: main (tmp.c:51)
==28128==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==28128== 
==28128== 
==28128== Process terminating with default action of signal 11 (SIGSEGV)
==28128==  Access not within mapped region at address 0x0
==28128==    at 0x400619: fun1 (tmp.c:18)
==28128==    by 0x400695: fun (tmp.c:46)
==28128==    by 0x4006A6: main (tmp.c:51)
==28128==  If you believe this happened as a result of a stack
==28128==  overflow in your program's main thread (unlikely but
==28128==  possible), you can try to increase the size of the
==28128==  main thread stack using the --main-stacksize= flag.
==28128==  The main thread stack size used in this run was 8388608.
==28128== 
==28128== HEAP SUMMARY:
==28128==     in use at exit: 0 bytes in 0 blocks
==28128==   total heap usage: 2 allocs, 2 frees, 1,040 bytes allocated
==28128== 
==28128== All heap blocks were freed -- no leaks are possible
==28128== 
==28128== For counts of detected and suppressed errors, rerun with: -v
==28128== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

SIGBUS的Valgrind报告:

==28176== Memcheck, a memory error detector
==28176== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==28176== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==28176== Command: ./a.out
==28176== Parent PID: 27953
==28176== 
==28176== 
==28176== HEAP SUMMARY:
==28176==     in use at exit: 0 bytes in 0 blocks
==28176==   total heap usage: 1 allocs, 1 frees, 5 bytes allocated
==28176== 
==28176== All heap blocks were freed -- no leaks are possible
==28176== 
==28176== For counts of detected and suppressed errors, rerun with: -v
==28176== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

1 个答案:

答案 0 :(得分:2)

int *iptr = (int *) ++cptr; 
*iptr = 42; //SIGBUS

违反了C标准的多个部分。

您遇到6.3.2.3 Pointers, paragraph 7的违规行为:

  

指向对象类型的指针可以转换为指向不同对象类型的指针。如果结果指针未针对引用的类型正确对齐,则行为未定义。

并违反了6.5 Expressions, paragraph 7的严格混叠规则:

  

只能通过具有以下类型之一的左值表达式访问对象的存储值:

     
      
  • 与对象的有效类型兼容的类型
  •   
  • 与对象的有效类型兼容的类型的限定版本,
  •   
  • 一种类型,它是与对象的有效类型相对应的有符号或无符号类型,
  •   
  • 一种类型,它是与对象的有效类型的限定版本相对应的有符号或无符号类型,
  •   
  • 在其成员(包括递归地包括子集合或包含的联盟的成员)中包括上述类型之一的集合或联合类型,或
  •   
  • 一种字符类型。
  •   

the Valgrind documentation for Memcheck

  

4.1。概述

     

Memcheck是内存错误检测器。它可以检测以下   C和C ++程序中常见的问题。

     
      
  • 您不应该访问的内存,例如溢出和溢出堆块,溢出栈顶和访问内存   在释放之后。

  •   
  • 使用未定义的值,即未初始化的值或从其他未定义的值派生的值。

  •   
  • 不正确地释放堆内存,例如两次释放堆块,或者对malloc / new / new []的使用不匹配   免费/删除/删除[]

  •   
  • 在memcpy和相关函数中重叠src和dst指针。

  •   
  • 将模糊的(可能是负的)值传递给内存分配函数的size参数。

  •   
  • 内存泄漏。

  •   

请注意,您的代码

int *iptr = (int *) ++cptr; 
*iptr = 42; //SIGBUS

没有任何Valgrind声称要检测的事情。您不是在访问没有权限的内存,也不是在使用malloc()创建的区域范围之外的内存。您还没有free()拥有记忆。您没有未初始化的变量,没有两倍free()的内存,也没有在重叠的源区域和目标区域中不正确地使用memcpy()。而且您没有将负数/“鱼腥”大小传递给分配函数。而且您不会泄漏任何内存。

因此,不,Valgrind甚至没有声称能够检测到将导致SIGBUS的代码。