changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/lib/rt/fuzz.lisp

changeset 698: 96958d3eb5b0
parent: 74e563ed4537
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; fuzz.lisp --- RT Fuzz
2 
3 ;; FUZZER API
4 
5 ;;; Commentary:
6 
7 ;;
8 ;; wiki: https://en.wikipedia.org/wiki/Fuzzing
9 
10 ;;; Code:
11 (in-package :rt/fuzz)
12 
13 (defvar *default-fuzz-generator*
14  (lambda (state)
15  (random most-positive-fixnum state)))
16 
17 (defclass fuzzer ()
18  ((state :initform (make-random-state t)
19  :initarg :state
20  :accessor fuzz-state)
21  (generator :initform *default-fuzz-generator*
22  :initarg :generator
23  :type function
24  :accessor fuzz-generator))
25  (:documentation "An object which provides invalid, unexpected or random data as inputs to some
26 program."))
27 
28 (defgeneric fuzz (self &key &allow-other-keys)
29  (:method ((self fuzzer) &key &allow-other-keys)
30  (funcall (the function (fuzz-generator self)) (fuzz-state self)))
31  (:method ((self fuzzer) &key count)
32  (if count
33  (let ((ret))
34  (dotimes (i count ret)
35  (push (funcall (the function (fuzz-generator self)) (fuzz-state self)) ret)))
36  (fuzz self))))
37 
38 (defgeneric fuzz* (state generator &key &allow-other-keys)
39  (:method ((state list) (generator function) &key (count 1))
40  (let ((ret))
41  (dotimes (i count ret)
42  (push (funcall generator state) ret))))
43  (:method ((state vector) (generator function) &key (count 1))
44  (let ((ret (make-array count :fill-pointer 0)))
45  (dotimes (i count ret)
46  (setf (aref ret i) (funcall generator state)))))
47  (:method ((state hash-table) (generator function) &key (count 1))
48  (let ((ret (make-hash-table)))
49  (dotimes (i count ret)
50  (destructuring-bind (k v) (funcall generator state)
51  (setf (gethash k ret) v)))))
52  (:method ((state random-state) (generator function) &key (count 1))
53  (let ((ret))
54  (dotimes (i count ret)
55  (push (funcall generator state) ret)))))