ubsan gcc -fsanitize=undefined 检测栈溢出

The Undefined Behavior Sanitizer - UBSAN

UBSAN is a runtime undefined behaviour checker.

UBSAN uses compile-time instrumentation to catch undefined behavior (UB). Compiler inserts code that perform certain kinds of checks before operations that may cause UB. If check fails (i.e. UB detected) __ubsan_handle_* function called to print error message.

GCC has that feature since 4.9.x [1] (see -fsanitize=undefined option and its suboptions). GCC 5.x has more checkers implemented [2].

 

UBSAN在编译时插入代码,进行检查访问越界等操作。

例子:

 

#include 
#include 

void func2(int c, int d)
{
    c = c -d;
}

void func(int a, int b)
{
   a = a + b;

   char t[256] = {};
   t[280] = 0;
   func2(a, b);
}


int main()
{
    char t[256] = {};
    int a,b;
    char c = 100;
    a = 2;
    b =3;
    func(a, 3);
    return 0;
}

gcc stack_test.c  -o stack_test 

你可能感兴趣的:(基础)