<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>

#define MAX_NETSTRING 1024*1024*1024

// globals
static FILE* sockf = NULL;

void die(const char* msg) __attribute__ ((noreturn));
void setup (void) __attribute__ ((constructor));
const unsigned char* afl_postprocess(const unsigned char* in_buf, unsigned int* inout_len);

void 
die(const char* msg) 
{
  fprintf(stderr, "[SHIM] Fatal error: %s\n", msg);
  exit(-1);
}

// set up our socket on load
void 
setup(void) 
{ 

  int sock;
  socklen_t saddr_len;
  struct sockaddr_un saddr;
  const char* sock_fname;

  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    die("failed to create socket");
  }

  sock_fname = getenv("AFL_FIX_SOCK");
  if (!sock_fname) {
    die("couldn't find AFL_FIX_SOCK in ENV\n");
  }

  memset(&saddr, 0, sizeof(struct sockaddr_un));
  saddr.sun_family = AF_UNIX;
  strlcpy(saddr.sun_path, sock_fname, sizeof(saddr.sun_path));
  saddr_len = (socklen_t)SUN_LEN(&saddr);
  if (connect(sock, (struct sockaddr*)&saddr, saddr_len) < 0) {
    die("error in connect\n - is the listener \
running?\n - does the sockfile have correct permissions?");
  }

  // wrap the fd as a FILE* so we can use stdio functions on it
  // this var is available to afl_postprocess as a global
  sockf = fdopen(sock, "w+");

}

const unsigned char* 
afl_postprocess(const unsigned char* in_buf, unsigned int* inout_len)
{

  static unsigned char* sock_buf = NULL;
  static size_t sock_buf_sz = 0;
  static unsigned char scratch; // one byte buffer for : and , tokens
  static unsigned int recv_len;

  // send netstring directly from input buffer
  if (*inout_len == 0 || *inout_len > MAX_NETSTRING) {
    die("input > 1GB");
  }
  if (fprintf(sockf, "%u:", *inout_len) < 0) {
    die("failed to write to socket");
  }
  if (fwrite(in_buf, 1, *inout_len, sockf) < *inout_len) {
    die("failed to write to socket");
  }
  if (fwrite(",", 1, 1, sockf) < 1) {
    die("failed to write to socket");
  }

  // read back the (possibly) modified test
  if (fscanf(sockf, "%u", &recv_len) < 1) {<commit_msg>missed a setup check<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/types.h>

#define MAX_NETSTRING 1024*1024*1024

// globals
static FILE* sockf = NULL;

void die(const char* msg) __attribute__ ((noreturn));
void setup (void) __attribute__ ((constructor));
const unsigned char* afl_postprocess(const unsigned char* in_buf, unsigned int* inout_len);

void 
die(const char* msg) 
{
  fprintf(stderr, "[SHIM] Fatal error: %s\n", msg);
  exit(-1);
}

// set up our socket on load
void 
setup(void) 
{ 

  int sock;
  socklen_t saddr_len;
  struct sockaddr_un saddr;
  const char* sock_fname;

  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    die("failed to create socket");
  }

  sock_fname = getenv("AFL_FIX_SOCK");
  if (!sock_fname) {
    die("couldn't find AFL_FIX_SOCK in ENV\n");
  }

  memset(&saddr, 0, sizeof(struct sockaddr_un));
  saddr.sun_family = AF_UNIX;
  strlcpy(saddr.sun_path, sock_fname, sizeof(saddr.sun_path));
  saddr_len = (socklen_t)SUN_LEN(&saddr);
  if (connect(sock, (struct sockaddr*)&saddr, saddr_len) < 0) {
    die("error in connect\n - is the listener \
running?\n - does the sockfile have correct permissions?");
  }

  // wrap the fd as a FILE* so we can use stdio functions on it
  // this var is available to afl_postprocess as a global
  sockf = fdopen(sock, "w+");
  if (!sockf) {
    die("failed to fdopen");
  }

}

const unsigned char* 
afl_postprocess(const unsigned char* in_buf, unsigned int* inout_len)
{

  static unsigned char* sock_buf = NULL;
  static size_t sock_buf_sz = 0;
  static unsigned char scratch; // one byte buffer for : and , tokens
  static unsigned int recv_len;

  // send netstring directly from input buffer
  if (*inout_len == 0 || *inout_len > MAX_NETSTRING) {
    die("input > 1GB");
  }
  if (fprintf(sockf, "%u:", *inout_len) < 0) {
    die("failed to write to socket");
  }
  if (fwrite(in_buf, 1, *inout_len, sockf) < *inout_len) {
    die("failed to write to socket");
  }
  if (fwrite(",", 1, 1, sockf) < 1) {
    die("failed to write to socket");
  }

  // read back the (possibly) modified test
  if (fscanf(sockf, "%u", &recv_len) < 1) {
{'fraction_non_alphanumeric': 0.14012738853503184, 'fraction_numerical': 0.011828935395814377, 'mean_word_length': 3.9018952062430325, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5129847', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1632, 'n_words': 480}