mirror of
https://github.com/AFLplusplus/AFLplusplus.git
synced 2025-06-13 18:48:08 +00:00
Refactor
This commit is contained in:
@ -635,6 +635,8 @@ typedef struct afl_state {
|
|||||||
u64 plot_prev_qc, plot_prev_uc, plot_prev_uh, plot_prev_ed;
|
u64 plot_prev_qc, plot_prev_uc, plot_prev_uh, plot_prev_ed;
|
||||||
|
|
||||||
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
|
||||||
|
u64 statsd_last_send_ms;
|
||||||
double stats_avg_exec;
|
double stats_avg_exec;
|
||||||
|
|
||||||
u8 *clean_trace;
|
u8 *clean_trace;
|
||||||
@ -957,7 +959,7 @@ void show_init_stats(afl_state_t *);
|
|||||||
/* StatsD */
|
/* StatsD */
|
||||||
|
|
||||||
int statsd_socket_init(char *host, int port);
|
int statsd_socket_init(char *host, int port);
|
||||||
int send_statsd_metric(afl_state_t *afl);
|
int statsd_send_metric(afl_state_t *afl);
|
||||||
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);
|
||||||
|
|
||||||
/* Run */
|
/* Run */
|
||||||
|
@ -47,6 +47,8 @@ Server config can be adjusted with AFL_STATSD_HOST and AFL_STATSD_PORT env var.
|
|||||||
|
|
||||||
#define USE_STATSD
|
#define USE_STATSD
|
||||||
#define STATSD_UPDATE_SEC 1
|
#define STATSD_UPDATE_SEC 1
|
||||||
|
#define STATSD_DEFAULT_PORT 8125
|
||||||
|
#define STATSD_DEFAULT_HOST "127.0.0.1"
|
||||||
|
|
||||||
/* If you want to have the original afl internal memory corruption checks.
|
/* If you want to have the original afl internal memory corruption checks.
|
||||||
Disabled by default for speed. it is better to use "make ASAN_BUILD=1". */
|
Disabled by default for speed. it is better to use "make ASAN_BUILD=1". */
|
||||||
|
@ -423,8 +423,10 @@ void show_stats(afl_state_t *afl) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_STATSD
|
#ifdef USE_STATSD
|
||||||
if (cur_ms - afl->stats_last_stats_ms > STATSD_UPDATE_SEC * 1000) {
|
if (cur_ms - afl->statsd_last_send_ms > STATSD_UPDATE_SEC * 1000) {
|
||||||
if(send_statsd_metric(afl)){
|
/* reset counter, even if send failed. */
|
||||||
|
afl->statsd_last_send_ms = cur_ms;
|
||||||
|
if(statsd_send_metric(afl)){
|
||||||
WARNF("coundln't send statsd metric.");
|
WARNF("coundln't send statsd metric.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,11 +13,12 @@
|
|||||||
#define MAX_TAG_LEN 200
|
#define MAX_TAG_LEN 200
|
||||||
#define METRIC_PREFIX "fuzzing"
|
#define METRIC_PREFIX "fuzzing"
|
||||||
|
|
||||||
int sock = 0;
|
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
int statds_sock = 0;
|
||||||
|
|
||||||
int statsd_socket_init(char *host, int port){
|
int statsd_socket_init(char *host, int port){
|
||||||
|
int sock;
|
||||||
if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
|
if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
|
||||||
perror("socket");
|
perror("socket");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -42,72 +43,72 @@ int statsd_socket_init(char *host, int port){
|
|||||||
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 0;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_statsd_metric(afl_state_t *afl){
|
int statsd_send_metric(afl_state_t *afl){
|
||||||
/* default port and host.
|
|
||||||
|
char buff[MAX_STATSD_PACKET_SIZE] = {0};
|
||||||
|
/* 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 = 8125;
|
u16 port = STATSD_DEFAULT_PORT;
|
||||||
char* host = "127.0.0.1";
|
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) {
|
||||||
// sanitization check ?
|
|
||||||
port = atoi(port_env);
|
port = atoi(port_env);
|
||||||
}
|
}
|
||||||
if ((host_env = getenv("AFL_STATSD_HOST")) != NULL) {
|
if ((host_env = getenv("AFL_STATSD_HOST")) != NULL) {
|
||||||
// sanitization check ?
|
|
||||||
host = host_env;
|
host = host_env;
|
||||||
}
|
}
|
||||||
|
|
||||||
error = statsd_socket_init(host, port);
|
|
||||||
if (error){
|
|
||||||
perror("Failed to init statsd client. Aborting");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!sock){
|
|
||||||
perror("sock");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buff[MAX_STATSD_PACKET_SIZE] = {0};
|
/* statds_sock is a global variable. We set it once in the beginning and reuse the socket.
|
||||||
|
If the sendto later fail, we reset it to 0 to be able to recreate it.
|
||||||
|
*/
|
||||||
|
if(!statds_sock){
|
||||||
|
statds_sock = statsd_socket_init(host, port);
|
||||||
|
if(!statds_sock){
|
||||||
|
perror("Cannot create socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE);
|
statsd_format_metric(afl, buff, MAX_STATSD_PACKET_SIZE);
|
||||||
if (sendto(sock, buff, strlen(buff), 0,
|
if (sendto(statds_sock, buff, strlen(buff), 0, (struct sockaddr *)&server, sizeof(server)) == -1) {
|
||||||
(struct sockaddr *)&server, sizeof(server)) == -1) {
|
if(!close(statds_sock)){
|
||||||
perror("sendto");
|
perror("Cannot close socket");
|
||||||
return -1;
|
}
|
||||||
|
statds_sock = 0;
|
||||||
|
perror("Cannot sendto");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sock);
|
|
||||||
sock=0;
|
|
||||||
|
|
||||||
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:
|
||||||
metric format:
|
<some.namespaced.name>:<value>|<type>
|
||||||
<some.namespaced.name>:<value>|<type>|<tags>
|
|
||||||
tags format:
|
|
||||||
*/
|
*/
|
||||||
#ifdef USE_STATSD_TAGS
|
#ifdef USE_STATSD_TAGS
|
||||||
/* #key:value,key:value,key
|
/* Tags format: DogStatsD
|
||||||
|
<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",
|
"|#banner:%s,afl_version:%s",
|
||||||
afl->use_banner,
|
afl->use_banner,
|
||||||
VERSION);
|
VERSION);
|
||||||
#else
|
#else
|
||||||
|
/* 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".cycles_wo_finds:%llu|g%s\n"
|
METRIC_PREFIX".cycles_wo_finds:%llu|g%s\n"
|
||||||
|
Reference in New Issue
Block a user