failure injection

(1)malloc穷尽的情况:

假设下面的代码是测试代码,里面含有被测函数fmalloc,其中含有一个malloc语句,在一般情况下,是很难走到malloc失败的分支的,因为很难模拟系统内存耗尽的情况。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1024
void fmalloc();
int main(){
fmalloc();
}
void fmalloc(){
    char *p=NULL;
    p=(char*)malloc(SIZE*sizeof(char));
    if(p==NULL)
    {   
        fprintf(stdout,"zero alocated!
");
    }   
    else{
        fprintf(stdout,"malloc success!
");
    }   
}

运行

./test2              
malloc success!

使用injection则有

fiu-run -x -c 'enable  name=posix/io/*,probability=0.25'  -c 'enable name=libc/mm/*,probability=0.05' ./test2
zero alocated!

原文地址:https://www.cnblogs.com/laodageblog/p/3769802.html