用于FUZZ测试的程序及其详解

来源:互联网 发布:单片机原理及应用pdf 编辑:程序博客网 时间:2024/06/01 20:56
//代码摘抄自模糊测试[...]    if (!(pid  = fork ()))    {/*child*/        ptrace(PTRACE_TRACEME,0,NULL,NULL)            //long ptrace(enum_ptrace_request requese,pid_t_ pid, void *addr,void *data);            //1).enum_ptrace_request 执行了ptrace要执行的命令            //2).pid_t 进程ID            //3).void *addr 指示了要监控的内存地址            //4).void *data 存放读取出的或者要写入的数据        execve(argv[0],argv,envp);//内核级系统调用,成功不返回,失败返回-1    }    else    {/*parent*/        c_pid  = pid;    monitor:        waitpid(pid,&status,0);//等待京城        if(WIFEXITED(status))        {/*程序退出信号*/            if(!quiet)                printf("process %d exited with code %d\n",pid,WEXITSTATUS(status));            return(ERR_OK);        }        else if(WIFSIGNALED(status))        {/*program ended because of a signal*/            printf("process %d terminated by unhandled signal %d\n",pid,WIERMSIG(status));            return(ERR_OK);        }        else if(WIFSIGNALED(status))        //看官方介绍        //The status field that was filled in by the wait or waitpid function        /*The WIFSIGNALED macro is always successful.If the child process for which status was returned by the wait or waitpid function exited because it raised a signal that caused it to exit, the WIFSIGNALED macro evaluates to TRUE and the WTERMSIG macro can be used to determine which signal was raised by the child process. Otherwise, the WIFSIGNALED macro evaluates to FALSE.*/        /*意思就是说这个函数用于响应waitpid函数,如果进行是正常的通过waitpid返回状态子进程的话,那么就为TRUE,并且wtermsig可以用于子进程引发的信号*/        {            if(!quiet)                fprintf(stderr,"process %d stopped due to signal %d (%s)",pid,WSTOPSIG(status),F_signum2ascii(WSTOPSIG(status)));                switch(WSTOPSIG(status))                {                    case SIGILL:                    case SIGBUS:                    case SIGSEGV:                    case SIGSYS:                        printf("program got interesting signal...\n");                        if((ptrace(PTRACE_CONT,pid,NULL,(WSTOPSIG(status)==SIGTRAP)?0:WSTOPSIG(status)))==-1)                        {                            perror("ptrace");                        }                        ptrace(PTRACE_DETACH,pid,NULL,NULL);                        fclose(fp);                        return(ERR_CRASH);/*it crashed*/                }*/deliver the signal through and keep traction*/                if((ptrace(PTRACE_CONT,pid,NULL,(WSTOPSIG(status)==SIGTRAP)?0:WSTOPSIG(status)))==-1)                {                    perror("ptrace");                }                goto monitor;        }        return(ERR_OK);    }
0 0