Skip to content

Instantly share code, notes, and snippets.

@mukeshtiwari
Created August 24, 2019 09:40
Show Gist options
  • Save mukeshtiwari/c4a7c471b866dc57c0e14f557d567fe7 to your computer and use it in GitHub Desktop.
Save mukeshtiwari/c4a7c471b866dc57c0e14f557d567fe7 to your computer and use it in GitHub Desktop.
(import random os tempfile subprocess)
(defn fuzzer [&optional [max-length 100] [char-start 32] [char-range 32]]
(->>
(.randrange random 0 (+ 1 max-length))
((fn [x] (lfor _ (range x)
(->
(.randrange random char-start (+ char-start char-range))
chr))))
(.join "")))
;(print (fuzzer 100 32 32))
;(print (fuzzer 1000 (ord "a") 26))
;(print (fuzzer))
(defn prepare-fuzz []
(do
(setv basename "input.txt")
(setv tempdir (.mkdtemp tempfile))
(setv FILE (.join os.path tempdir basename))
(setv data (fuzzer))
(with [outf (open FILE "w")]
(.write outf data))))
;; How Can I make it more abstract
;; Also, it's good idea to avoid os.system call
(defn fuzz-unix-command [command]
(lfor _ (range 100)
(as->
;(fuzzer 100 (ord "0") 10) it ;; This would never crash
(fuzzer) it ;; This would may crash
(.system os (+ "echo " it " + " it " | " command)))))
;; Using subprocess
(defn fuzz-bc [command]
(lfor _ (range 100)
(as->
;(fuzzer 100 (ord "0") 10) it ;; This would never crash
(fuzzer) it ;; This would may crash
(.run subprocess ["echo" it "+" it "|" command]))))
;; However, this would generate a random garbage data, and
;; and it might lead to parse error
(fuzz-unix-command "bc")
(fuzz-bc "bc")
(defn fuzz-unix-from-file [command]
(setv basename "input.txt")
(setv tempdir (.mkdtemp tempfile))
(setv FILE (.join os.path tempdir basename))
(setv runs [])
(lfor _ (range 10)
(do
(setv data (fuzzer))
(with [ofile (open FILE "w")]
(.write ofile data))
(as->
(.run subprocess [command FILE]
:stdin subprocess.DEVNULL
:stdout subprocess.PIPE
:stderr subprocess.PIPE
:universal_newlines True) it
(.append runs (, data it)))))
(print runs))
(fuzz-unix-from-file "bc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment