Refactor global var into afl_state_t struct
MODIFY
include/afl-fuzz.h
include/afl-fuzz.h
@@ -633,8 +633,11 @@ typedef struct afl_state {
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;
- // StatsD
+
+ /* StatsD */
u64 statsd_last_send_ms;
+ struct sockaddr_in statsd_server;
+ int statsd_sock;
double stats_avg_exec;
@@ -958,7 +961,7 @@ void show_init_stats(afl_state_t *);
/* StatsD */
-int statsd_socket_init(char *host, int port);
+int statsd_socket_init(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);

MODIFY
src/afl-fuzz-statsd.c
src/afl-fuzz-statsd.c
#define MAX_TAG_LEN 200
#define METRIC_PREFIX "fuzzing"
-struct sockaddr_in server;
-int error = 0;
-int statds_sock = 0;
+int statsd_socket_init(afl_state_t *afl) {
+ /* Default port and host.
+ Will be overwritten by AFL_STATSD_PORT and AFL_STATSD_HOST environment
+ variable, if they exists.
+ */
+ u16 port = STATSD_DEFAULT_PORT;
+ char *host = STATSD_DEFAULT_HOST;
-int statsd_socket_init(char *host, int port) {
+ if (afl->afl_env.afl_statsd_port) { port = atoi(afl->afl_env.afl_statsd_port); }
+ if (afl->afl_env.afl_statsd_host) { host = afl->afl_env.afl_statsd_host; }
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
@@ -25,9 +30,9 @@ int statsd_socket_init(char *host, int port) {
}
- memset(&server, 0, sizeof(server));
- server.sin_family = AF_INET;
- server.sin_port = htons(port);
+ memset(&afl->statsd_server, 0, sizeof(afl->statsd_server));
+ afl->statsd_server.sin_family = AF_INET;
+ afl->statsd_server.sin_port = htons(port);
struct addrinfo *result;
struct addrinfo hints;
@@ -36,13 +41,13 @@ int statsd_socket_init(char *host, int port) {
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
- if ((error = getaddrinfo(host, NULL, &hints, &result))) {
+ if ((getaddrinfo(host, NULL, &hints, &result))) {
FATAL("Fail to getaddrinfo");
}
- memcpy(&(server.sin_addr), &((struct sockaddr_in *)result->ai_addr)->sin_addr,
+ memcpy(&(afl->statsd_server.sin_addr), &((struct sockaddr_in *)result->ai_addr)->sin_addr,
sizeof(struct in_addr));
freeaddrinfo(result);
@@ -53,24 +58,14 @@ int statsd_socket_init(char *host, int port) {
int statsd_send_metric(afl_state_t *afl) {
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.
- */
- u16 port = STATSD_DEFAULT_PORT;
- char *host = STATSD_DEFAULT_HOST;
-
- if (afl->afl_env.afl_statsd_port) { port = atoi(afl->afl_env.afl_statsd_port); }
- if (afl->afl_env.afl_statsd_host) { host = afl->afl_env.afl_statsd_host; }
- /* 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.
+ /* afl->statsd_sock is set once in the initialisation of afl-fuzz and reused each time
+ If the sendto later fail, we reset it to 0 to be able to recreates it.
*/
- if (!statds_sock) {
+ if (!afl->statsd_sock) {
- statds_sock = statsd_socket_init(host, port);
- if (!statds_sock) {
+ afl->statsd_sock = statsd_socket_init(afl);
+ if (!afl->statsd_sock) {
WARNF("Cannot create socket");
return -1;
@@ -80,11 +75,11 @@ int statsd_send_metric(afl_state_t *afl) {
}
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(afl->statsd_sock, buff, strlen(buff), 0, (struct sockaddr *)&afl->statsd_server,
+ sizeof(afl->statsd_server)) == -1) {
- if (!close(statds_sock)) { perror("Cannot close socket"); }
- statds_sock = 0;
+ if (!close(afl->statsd_sock)) { perror("Cannot close socket"); }
+ afl->statsd_sock = 0;
WARNF("Cannot sendto");
return -1;

Refactor global var into afl_state_t struct