You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by as...@apache.org on 2022/06/17 15:08:28 UTC

[qpid-proton] branch main updated: PROTON-2567: Fix fuzz-proactor-receive to work with response file

This is an automated email from the ASF dual-hosted git repository.

astitcher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-proton.git


The following commit(s) were added to refs/heads/main by this push:
     new 0dc72eb59 PROTON-2567: Fix fuzz-proactor-receive to work with response file
0dc72eb59 is described below

commit 0dc72eb59ff875c7380aaca17b633d7ef33ebb95
Author: Andrew Stitcher <as...@apache.org>
AuthorDate: Thu Jun 16 19:05:33 2022 +0100

    PROTON-2567: Fix fuzz-proactor-receive to work with response file
---
 c/tests/fuzz/StandaloneFuzzTargetInit.c | 68 --------------------------------
 c/tests/fuzz/StandaloneFuzzTargetMain.c | 70 +++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 68 deletions(-)

diff --git a/c/tests/fuzz/StandaloneFuzzTargetInit.c b/c/tests/fuzz/StandaloneFuzzTargetInit.c
index e1bd6bd4c..69c110494 100644
--- a/c/tests/fuzz/StandaloneFuzzTargetInit.c
+++ b/c/tests/fuzz/StandaloneFuzzTargetInit.c
@@ -19,78 +19,10 @@
  *
  */
 
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include "libFuzzingEngine.h"
 
-/*
- * Use this to implement response file:
- * - Check if there is one file mentioned and its name starts with '@'
- * - If so then read the file line by line making up the new argv
- * - Modify argc/argv then return.
- *
- * Problem: Somehow need to free buf and nargv to avoid sanitizer warnings
- */
-
-/* Free allocated memory at program exit to avoid the leak sanitizer complaining  */
-static char *buf = 0;
-static char **nargv = 0;
-
-static void freeall(void)
-{
-  free(buf);
-  free(nargv);
-}
-
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-  if (*argc==2 && (*argv)[1][0]=='@') {
-    const char* rfilename = (*argv)[1]+1;
-
-    /* Read entire file into memory */
-    fprintf(stderr, "Reading response file: %s\n", rfilename);
-    FILE *f = fopen(rfilename, "rb");
-    assert(f);
-    fseek(f, 0, SEEK_END);
-    size_t len = ftell(f);
-    fseek(f, 0, SEEK_SET);
-    buf = (char*)malloc(len+1);
-    size_t n_read = fread(buf, 1, len, f);
-    fclose(f);
-    assert(n_read == len);
-    buf[len] = '\0';
-
-    /* scan file counting lines and replacing line ends with \0 */
-    int line = 0;
-    char *p = buf;
-    while (p<&buf[len]) {
-      p += strcspn(p, "\n\r ");
-      *p++ = '\0';
-      line +=1;
-    };
-
-    fprintf(stderr, "        response file: (%zd bytes, %d lines)\n", n_read, line);
-
-    /* scan again putting each line into the argv array */
-    nargv = (char**) calloc(line+1, sizeof(p));
-
-    p = buf;
-    line = 1;
-    do {
-        char* s = p;
-        int l = strlen(p);
-        p += l+1;
-        if (l>0) nargv[line++] = s;
-    } while (p<&buf[len]);
-
-    int nargc = line;
-    *argc = nargc;
-    *argv = nargv;
-    atexit(&freeall);
-  }
   return 0;
 }
 
diff --git a/c/tests/fuzz/StandaloneFuzzTargetMain.c b/c/tests/fuzz/StandaloneFuzzTargetMain.c
index 38d007e35..21a59d31b 100644
--- a/c/tests/fuzz/StandaloneFuzzTargetMain.c
+++ b/c/tests/fuzz/StandaloneFuzzTargetMain.c
@@ -17,13 +17,82 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "libFuzzingEngine.h"
 
+/*
+ * Use this to implement response file:
+ * - Check if there is one file mentioned and its name starts with '@'
+ * - If so then read the file line by line making up the new argv
+ * - Modify argc/argv then return.
+ *
+ */
+
+/* Free allocated memory at program exit to avoid the leak sanitizer complaining  */
+static char *buf = 0;
+static char **nargv = 0;
+
+static void freeall(void)
+{
+  free(buf);
+  free(nargv);
+}
+
+int ProcessResponseFile(int *argc, char ***argv) {
+  if (*argc==2 && (*argv)[1][0]=='@') {
+    const char* rfilename = (*argv)[1]+1;
+
+    /* Read entire file into memory */
+    fprintf(stderr, "Reading response file: %s\n", rfilename);
+    FILE *f = fopen(rfilename, "rb");
+    assert(f);
+    fseek(f, 0, SEEK_END);
+    size_t len = ftell(f);
+    fseek(f, 0, SEEK_SET);
+    buf = (char*)malloc(len+1);
+    size_t n_read = fread(buf, 1, len, f);
+    fclose(f);
+    assert(n_read == len);
+    buf[len] = '\0';
+
+    /* scan file counting lines and replacing line ends with \0 */
+    int line = 0;
+    char *p = buf;
+    while (p<&buf[len]) {
+      p += strcspn(p, "\n\r ");
+      *p++ = '\0';
+      line +=1;
+    };
+
+    fprintf(stderr, "        response file: (%zd bytes, %d lines)\n", n_read, line);
+
+    /* scan again putting each line into the argv array */
+    nargv = (char**) calloc(line+1, sizeof(p));
+
+    p = buf;
+    line = 1;
+    do {
+      char* s = p;
+      int l = strlen(p);
+      p += l+1;
+      if (l>0) nargv[line++] = s;
+    } while (p<&buf[len]);
+
+    int nargc = line;
+    *argc = nargc;
+    *argv = nargv;
+  }
+  return 0;
+}
+
 int main(int argc, char **argv) {
   fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1);
   LLVMFuzzerInitialize(&argc, &argv);
 
+  // Process response file
+  ProcessResponseFile(&argc, &argv);
+
   for (int i = 1; i < argc; i++) {
     fprintf(stderr, "Running: %s\n", argv[i]);
     FILE *f = fopen(argv[i], "rb");
@@ -39,4 +108,5 @@ int main(int argc, char **argv) {
     free(buf);
     fprintf(stderr, "Done:    %s: (%zd bytes)\n", argv[i], n_read);
   }
+  freeall();
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org