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 <stdio.h>
#include <stdlib.h>

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 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章