Code format

This commit is contained in:
Edznux
2020-10-04 16:11:05 +02:00
parent 1e0bc2e5c3
commit 1a12db1b59
4 changed files with 133 additions and 130 deletions

View File

@ -635,6 +635,7 @@ typedef struct afl_state {
u64 stats_last_stats_ms, stats_last_plot_ms, stats_last_ms, stats_last_execs; u64 stats_last_stats_ms, stats_last_plot_ms, stats_last_ms, stats_last_execs;
// StatsD // StatsD
u64 statsd_last_send_ms; u64 statsd_last_send_ms;
double stats_avg_exec; double stats_avg_exec;
u8 *clean_trace; u8 *clean_trace;

View File

@ -903,6 +903,7 @@ common_fuzz_stuff(afl_state_t *afl, u8 *out_buf, u32 len) {
afl->stage_cur + 1 == afl->stage_max) { afl->stage_cur + 1 == afl->stage_max) {
show_stats(afl); show_stats(afl);
} }
return 0; return 0;

View File

@ -423,15 +423,16 @@ void show_stats(afl_state_t *afl) {
} }
#ifdef USE_STATSD #ifdef USE_STATSD
if (cur_ms - afl->statsd_last_send_ms > STATSD_UPDATE_SEC * 1000) { if (cur_ms - afl->statsd_last_send_ms > STATSD_UPDATE_SEC * 1000) {
/* reset counter, even if send failed. */ /* reset counter, even if send failed. */
afl->statsd_last_send_ms = cur_ms; afl->statsd_last_send_ms = cur_ms;
if(statsd_send_metric(afl)){ if (statsd_send_metric(afl)) { WARNF("coundln't send statsd metric."); }
WARNF("coundln't send statsd metric.");
} }
}
#endif #endif
/* Every now and then, write plot data. */ /* Every now and then, write plot data. */

View File

@ -8,7 +8,6 @@
#include <unistd.h> #include <unistd.h>
#include "afl-fuzz.h" #include "afl-fuzz.h"
#define MAX_STATSD_PACKET_SIZE 4096 #define MAX_STATSD_PACKET_SIZE 4096
#define MAX_TAG_LEN 200 #define MAX_TAG_LEN 200
#define METRIC_PREFIX "fuzzing" #define METRIC_PREFIX "fuzzing"
@ -17,11 +16,13 @@ struct sockaddr_in server;
int error = 0; int error = 0;
int statds_sock = 0; int statds_sock = 0;
int statsd_socket_init(char *host, int port){ int statsd_socket_init(char *host, int port) {
int sock; int sock;
if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket");
exit(1); FATAL("Failed to create socket");
} }
memset(&server, 0, sizeof(server)); memset(&server, 0, sizeof(server));
@ -35,122 +36,121 @@ int statsd_socket_init(char *host, int port){
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
if ( (error = getaddrinfo(host, NULL, &hints, &result)) ) { if ((error = getaddrinfo(host, NULL, &hints, &result))) {
perror("getaddrinfo");
exit(1); FATAL("Fail to getaddrinfo");
} }
memcpy(&(server.sin_addr), &((struct sockaddr_in*)result->ai_addr)->sin_addr, sizeof(struct in_addr)); memcpy(&(server.sin_addr), &((struct sockaddr_in *)result->ai_addr)->sin_addr,
sizeof(struct in_addr));
freeaddrinfo(result); freeaddrinfo(result);
return sock; return sock;
} }
int statsd_send_metric(afl_state_t *afl){ int statsd_send_metric(afl_state_t *afl) {
char buff[MAX_STATSD_PACKET_SIZE] = {0}; char buff[MAX_STATSD_PACKET_SIZE] = {0};
/* Default port and host. /* Default port and host.
Will be overwritten by AFL_STATSD_PORT and AFL_STATSD_HOST environment variable, if they exists. Will be overwritten by AFL_STATSD_PORT and AFL_STATSD_HOST environment
variable, if they exists.
*/ */
u16 port = STATSD_DEFAULT_PORT; u16 port = STATSD_DEFAULT_PORT;
char* host = STATSD_DEFAULT_HOST; char *host = STATSD_DEFAULT_HOST;
char* port_env; char *port_env;
char* host_env; char *host_env;
if ((port_env = getenv("AFL_STATSD_PORT")) != NULL) { if ((port_env = getenv("AFL_STATSD_PORT")) != NULL) { port = atoi(port_env); }
port = atoi(port_env); if ((host_env = getenv("AFL_STATSD_HOST")) != NULL) { host = host_env; }
}
if ((host_env = getenv("AFL_STATSD_HOST")) != NULL) {
host = host_env;
}
/* statds_sock is a global variable. We set it once in the beginning and reuse the socket. /* statds_sock is a global variable. We set it once in the beginning and reuse
If the sendto later fail, we reset it to 0 to be able to recreate it. the socket. If the sendto later fail, we reset it to 0 to be able to recreate
it.
*/ */
if(!statds_sock){ if (!statds_sock) {
statds_sock = statsd_socket_init(host, port); statds_sock = statsd_socket_init(host, port);
if(!statds_sock){ if (!statds_sock) {
perror("Cannot create socket");
WARNF("Cannot create socket");
return -1; return -1;
} }
} }
statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE); statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE);
if (sendto(statds_sock, buff, strlen(buff), 0, (struct sockaddr *)&server, sizeof(server)) == -1) { if (sendto(statds_sock, buff, strlen(buff), 0, (struct sockaddr *)&server,
if(!close(statds_sock)){ sizeof(server)) == -1) {
perror("Cannot close socket");
} if (!close(statds_sock)) { perror("Cannot close socket"); }
statds_sock = 0; statds_sock = 0;
perror("Cannot sendto"); WARNF("Cannot sendto");
return -1; return -1;
} }
return 0; return 0;
} }
int statsd_format_metric(afl_state_t *afl, char *buff, size_t bufflen){ int statsd_format_metric(afl_state_t *afl, char *buff, size_t bufflen) {
/* Metric format:
<some.namespaced.name>:<value>|<type> /* Metric format:
*/ <some.namespaced.name>:<value>|<type>
#ifdef USE_DOGSTATSD_TAGS */
#ifdef USE_DOGSTATSD_TAGS
/* Tags format: DogStatsD /* Tags format: DogStatsD
<some.namespaced.name>:<value>|<type>|#key:value,key:value,key <some.namespaced.name>:<value>|<type>|#key:value,key:value,key
*/ */
char tags[MAX_TAG_LEN * 2] = {0}; char tags[MAX_TAG_LEN * 2] = {0};
snprintf(tags, MAX_TAG_LEN * 2, snprintf(tags, MAX_TAG_LEN * 2, "|#banner:%s,afl_version:%s", afl->use_banner,
"|#banner:%s,afl_version:%s",
afl->use_banner,
VERSION); VERSION);
#else #else
/* No tags. /* No tags.
*/ */
char *tags = ""; char *tags = "";
#endif #endif
/* Sends multiple metrics with one UDP Packet. /* Sends multiple metrics with one UDP Packet.
bufflen will limit to the max safe size. bufflen will limit to the max safe size.
*/ */
snprintf(buff, bufflen, snprintf(buff, bufflen,
METRIC_PREFIX".cycle_done:%llu|g%s\n" METRIC_PREFIX ".cycle_done:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".cycles_wo_finds:%llu|g%s\n" ".cycles_wo_finds:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".execs_done:%llu|g%s\n" ".execs_done:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".execs_per_sec:%0.02f|g%s\n" ".execs_per_sec:%0.02f|g%s\n" METRIC_PREFIX
METRIC_PREFIX".paths_total:%u|g%s\n" ".paths_total:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".paths_favored:%u|g%s\n" ".paths_favored:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".paths_found:%u|g%s\n" ".paths_found:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".paths_imported:%u|g%s\n" ".paths_imported:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".max_depth:%u|g%s\n" ".max_depth:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".cur_path:%u|g%s\n" ".cur_path:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".pending_favs:%u|g%s\n" ".pending_favs:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".pending_total:%u|g%s\n" ".pending_total:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".variable_paths:%u|g%s\n" ".variable_paths:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".unique_crashes:%llu|g%s\n" ".unique_crashes:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".unique_hangs:%llu|g%s\n" ".unique_hangs:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".total_crashes:%llu|g%s\n" ".total_crashes:%llu|g%s\n" METRIC_PREFIX
METRIC_PREFIX".slowest_exec_ms:%u|g%s\n" ".slowest_exec_ms:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".edges_found:%u|g%s\n" ".edges_found:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".var_byte_count:%u|g%s\n" ".var_byte_count:%u|g%s\n" METRIC_PREFIX
METRIC_PREFIX".havoc_expansion:%u|g%s\n", ".havoc_expansion:%u|g%s\n",
afl->queue_cycle ? (afl->queue_cycle - 1) : 0, tags, afl->queue_cycle ? (afl->queue_cycle - 1) : 0, tags,
afl->cycles_wo_finds, tags, afl->cycles_wo_finds, tags, afl->fsrv.total_execs, tags,
afl->fsrv.total_execs, tags, afl->fsrv.total_execs /
afl->fsrv.total_execs / ((double)(get_cur_time() - afl->start_time) / 1000), tags, ((double)(get_cur_time() - afl->start_time) / 1000),
afl->queued_paths, tags, tags, afl->queued_paths, tags, afl->queued_favored, tags,
afl->queued_favored, tags, afl->queued_discovered, tags, afl->queued_imported, tags,
afl->queued_discovered, tags, afl->max_depth, tags, afl->current_entry, tags, afl->pending_favored,
afl->queued_imported, tags, tags, afl->pending_not_fuzzed, tags, afl->queued_variable, tags,
afl->max_depth, tags, afl->unique_crashes, tags, afl->unique_hangs, tags,
afl->current_entry, tags, afl->total_crashes, tags, afl->slowest_exec_ms, tags,
afl->pending_favored, tags,
afl->pending_not_fuzzed, tags,
afl->queued_variable, tags,
afl->unique_crashes, tags,
afl->unique_hangs, tags,
afl->total_crashes, tags,
afl->slowest_exec_ms, tags,
count_non_255_bytes(afl, afl->virgin_bits), tags, count_non_255_bytes(afl, afl->virgin_bits), tags,
afl->var_byte_count, tags, afl->var_byte_count, tags, afl->expand_havoc, tags);
afl->expand_havoc, tags
);
return 0; return 0;
} }