0

我现在正在学习 KLEE,我写了一个简单的代码:

#include "klee/klee.h"
#include <stdio.h>
#include <stdlib.h>

int test(int *p)
{
    int *q = (int *) malloc(sizeof(int));

    if ((*p) == (*q)) {
       printf("reading uninitialized heap memory");
    }
    return 0;
}


int main()
{
    int *p = (int *) malloc(sizeof(int));
    test(p);
    return 0;
}

首先,我生成 LLVM 位码,然后对位码执行 KLEE。以下是所有输出:

KLEE: output directory is "/Users/yjy/WorkSpace/Test/klee-out-13"
Using STP solver backend
KLEE: WARNING: undefined reference to function: printf
KLEE: WARNING ONCE: calling external: printf(140351601907424)
reading uninitialized heap memory
KLEE: done: total instructions = 61
KLEE: done: completed paths = 4
KLEE: done: generated tests = 4

我想 KLEE 应该给我一个错误,即 q 指针未初始化,但事实并非如此。为什么 KLEE 没有给我一个错误或警告?KLEE不能检测到这个错误吗?提前致谢!

4

1 回答 1

1

TLTR : KLEE 没有实现这个功能。Clang 可以直接检查这个。

KLEE 目前支持 add/sub/mul/div 溢出检查。要使用此功能,您必须使用 clang -fsanitize=signed-integer-overflow 或 clang -fsanitize=unsigned-integer-overflow 编译源代码。

这个想法是在您使用 clang sanitizer 时将函数调用插入到字节码中(例如 __ubsan_handle_add_overflow)。然后 KLEE 将在遇到函数调用时处理溢出检查。

Clang 支持 MemorySanitizerAddressSanitizer UndefinedBehaviorSanitizer。它们定义在 projects/compiler-rt/lib 目录中。MemorySanitizer 是您正在寻找的那个,它是未初始化读取的检测器。

您可以删除 KLEE 函数调用并直接使用 clang 检查。

➜  ~ clang -g -fsanitize=memory st.cpp
➜  ~ ./a.out
==16031==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x490954  (/home/hailin/a.out+0x490954)
    #1 0x7f21b72f382f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #2 0x41a1d8  (/home/hailin/a.out+0x41a1d8)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/home/hailin/a.out+0x490954)
Exiting
于 2017-04-21T07:26:18.960 回答