用libfuzzer找到slow-input的文章见我的上一篇
libfuzzer初学者(一):mjson–slowinput的模糊测试
仿照readme中给出的示例,编写程序:
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
int main() {
const char *s = "88888888E888888888888888888888"; // {"a":1,"b":[2,false]}
printf("slow input test begin\n");
double val; // Get `a` attribute
clock_t begin = clock();
if (mjson_get_number(s, strlen(s), "$.a", &val)) // into C variable `val`
printf("a: %g\n", val); // a: 1
clock_t test1 = clock();
double time1 = (double)(test1 - begin) / CLOCKS_PER_SEC;
printf("test1: %fs\n", time1);
const char *buf; // Get `b` sub-object
int len; // into C variables `buf,len`
if (mjson_find(s, strlen(s), "$.b", &buf, &len)) // And print it
printf("%.*s\n", len, buf); // [2,false]
clock_t test2 = clock();
double time2 = (double)(test2 - test1) / CLOCKS_PER_SEC;
printf("test2: %fs\n", time2);
int v; // Extract `false`
if (mjson_get_bool(s, strlen(s), "$.b[1]", &v)) // into C variable `v`
printf("boolean: %d\n", v);
clock_t test3 = clock();
double time3 = (double)(test3 - test2) / CLOCKS_PER_SEC;
printf("test3: %fs\n", time3);
}
编译命令
gcc -o slow_input slow_input.c /home/xjy/fuzz/workspace/mjson/src/mjson.c
得到输出:

三个mjson函数调用均触发了SLOW_INPUT
我们不妨就针对mjson_get_number来在不依赖大模型的情况下判断。
由于slow_input这个错误的特性是在执行时在时间上有直接的放慢,所以寻找问题所在变得非常容易。记得修改路径里的yourpath
gcc -o -g slow_input slow_input.c /yourpath/fuzz/workspace/mjson/src/mjson.c
启用gdb调试
使用n单步调试,在14行之后开始超过3s未响应,此时在键盘输入ctrl c打断进程。
在这里插入图片描述

gdb输出当前所在位置,继续执行,继续打断,gdb提示仍在主函数的805行。
我们可以断定是805行附近触发该slow-input。
for (i = 0; i < e; i++) d *= 10;
for (i = 0; i < -e; i++) d /= 10;
再结合我们的输入8888888888e888888888,很容易发现问题所在。
感觉这个是由于slow-input的性质特殊,所以查找问题来源特别容易。
307

被折叠的 条评论
为什么被折叠?



