简体   繁体   English

这是如何使用 klee 符号执行测试有状态 API 的吗?

[英]Is this how to test a stateful API with klee symbolic execution?

I'm currently testing out a few approaches on how to test and fuzz a C API.我目前正在测试一些关于如何测试和模糊 C API 的方法。 In the process thereof I found KLEE which runs the code symbolically, meaning that it tries to cover all branches that depend on some symbolic input and checks for all sorts of errors.在此过程中,我发现KLEE以符号方式运行代码,这意味着它试图覆盖所有依赖于某些符号输入的分支并检查各种错误。 I managed to get it to work and am now asking if my approach is good or if it has some major drawbacks or problems.我设法让它工作,现在问我的方法是否好,或者它是否有一些主要的缺点或问题。

Let's suppose we have following simple but buggy API:假设我们有以下简单但有缺陷的 API:

#include <assert.h>

static int g_state;

void setState(int state) {
    g_state = state;
}

void run(void) {
    if (g_state == 123) {
        assert(0);
    }
}

If the state is set to 123 and then run() is invoked the placed assertion fails.如果状态设置为123 ,然后调用run() ,则放置的断言失败。

For this I have written following symbolic test with KLEE:为此,我用 KLEE 编写了以下符号测试:

#include "klee/klee.h"
#include "buggy_api.h"

int main(void) {
    for (int i = 0; i < 2; ++i) { // sequentially call 2 APIs
        int f_select = klee_choose(2); // what API to call
        if (f_select == 0) {
            int state = 0;
            klee_make_symbolic(&state, sizeof(state), "state");
            setState(state);
        } else if (f_select == 1) {
            run();
        }
    }
    return 0;
}

When running with KLEE, the sequence of calls necessary to trigger the assertion is found almost immediately.使用 KLEE 运行时,几乎可以立即找到触发断言所需的调用序列。 I then tried to extend the API with a few other functions and hid the assertion behind a combination of states.然后,我尝试使用其他一些函数来扩展 API,并将断言隐藏在状态组合之后。 KLEE again found the planted bug but naturally took a bit longer. KLEE再​​次找到了种植的虫子,但自然需要更长的时间。

Is this how I can efficiently use KLEE for checking an API?这就是我可以有效地使用 KLEE 检查 API 的方法吗? Or is there documentation about a better approach?或者是否有关于更好方法的文档?

In order to test an API with KLEE, you indeed need to write a driver that calls it.为了使用 KLEE 测试 API,您确实需要编写一个调用它的驱动程序。 Yours works well, I'm not sure why you're using a for loop though?您的效果很好,但我不确定您为什么要使用for循环? This smaller example should work:这个较小的示例应该可以工作:

#include "klee/klee.h"
#include "buggy_api.h"

int main(void) {
    int state;
    klee_make_symbolic(&state, sizeof(state), "state");
    setState(state);
    run();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM