1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15-
1615#define _GNU_SOURCE
1716
1817#define __STDC_FORMAT_MACROS
1918#include <inttypes.h>
2019
2120#define KXVER 3
21+ #include "k.h"
22+ #include "libchronicle.h"
23+ #include "mock_k.h"
24+ #include "serdes_k.h"
25+ #include <ctype.h>
26+ #include <errno.h>
27+ #include <fcntl.h>
28+ #include <stdarg.h>
2229#include <stdio.h>
2330#include <stdlib.h>
24- #include <errno.h>
2531#include <string.h>
26- #include <fcntl.h>
2732#include <unistd.h>
28- #include <stdarg.h>
29- #include <ctype.h>
30- #include "k.h"
31- #include "mock_k.h"
32- #include "serdes_k.h"
33- #include "libchronicle.h"
3433
3534// This is a stand-alone tool for replaying a queue for use with fuzzer
3635int print_data = 0 ;
3736int print_meta = 0 ;
3837
39- int print_msg (void * ctx , uint64_t index , void * msg ) {
38+ int print_msg (void * ctx , uint64_t index , void * msg ) {
4039 K x = (K )msg ;
4140 if (print_data ) {
4241 printf ("[%" PRIu64 "] KC " , index );
@@ -53,7 +52,9 @@ uint32_t xorshift128(uint32_t state[static 4]) {
5352 uint32_t s , t = state [3 ];
5453 t ^= t << 11 ;
5554 t ^= t >> 8 ;
56- state [3 ] = state [2 ]; state [2 ] = state [1 ]; state [1 ] = s = state [0 ];
55+ state [3 ] = state [2 ];
56+ state [2 ] = state [1 ];
57+ state [1 ] = s = state [0 ];
5758 t ^= s ;
5859 t ^= s >> 19 ;
5960 state [0 ] = t ;
@@ -68,13 +69,13 @@ uint32_t xorshift128(uint32_t state[static 4]) {
6869// Reported but unresolved https://www.mail-archive.com/[email protected] /msg209613.html 6970static inline uint64_t rdtsc (void ) {
7071 uint32_t a , d ;
71- __asm__ __volatile__("rdtscp" : "=a" (a ), "=d" (d ));
72- return (((uint64_t ) d << 32 ) | a );
72+ __asm__ __volatile__("rdtscp" : "=a" (a ), "=d" (d ));
73+ return (((uint64_t )d << 32 ) | a );
7374}
74- int c_mkstemp (char * pattern ) {
75+ int c_mkstemp (char * pattern ) {
7576 uint32_t xor_state [4 ];
7677 xor_state [0 ] = xor_state [1 ] = xor_state [2 ] = xor_state [3 ] = (uint32_t )rdtsc ();
77- char * rep = strstr (pattern , "XXXXXX" );
78+ char * rep = strstr (pattern , "XXXXXX" );
7879 if (rep ) {
7980 for (int i = 0 ; i < 6 ; i ++ ) {
8081 uint8_t incre = xorshift128 (xor_state ) & 0x1F ;
@@ -93,11 +94,11 @@ int c_mkstemp(char* pattern) {
9394int main (const int argc , char * * argv ) {
9495 int c ;
9596 opterr = 0 ;
96- int verboseflag = 0 ;
97- FILE * fuzzfid = NULL ;
97+ int verboseflag = 0 ;
98+ FILE * fuzzfid = NULL ;
9899
99100 while ((c = getopt (argc , argv , "dmv:" )) != -1 )
100- switch (c ) {
101+ switch (c ) {
101102 case 'd' :
102103 print_data = 1 ;
103104 break ;
@@ -113,7 +114,7 @@ int main(const int argc, char **argv) {
113114 default :
114115 fprintf (stderr , "%c??" , c );
115116 exit (3 );
116- }
117+ }
117118
118119 if (optind + 2 > argc ) {
119120 printf ("Missing mandatory argument.\n Expected: %s [-d] [-m] [-v] QUEUE FUZZFILE\n" , argv [0 ]);
@@ -128,68 +129,70 @@ int main(const int argc, char **argv) {
128129 printf (" the second `bytes` appends that many random bytes as a new entry to the queue\n" );
129130 printf (" As the script is played, the random number generator seed, recieved index and byte count\n" );
130131 printf (" are written to a log. This is then re-opened to verify the data in the queue matches.\n" );
131-
132+
132133 exit (1 );
133134 }
134135
135136 // mandatory arguments
136- char * dir = argv [optind ];
137- char * fuzz = argv [optind + 1 ];
137+ char * dir = argv [optind ];
138+ char * fuzz = argv [optind + 1 ];
138139
139140 fuzzfid = stdin ;
140141 if (strcmp (fuzz , "-" ) == 0 ) {
141142 fprintf (stderr , "fuzzing from stdin\n" );
142143 } else {
143144 fuzzfid = fopen (fuzz , "r" );
144- if (fuzzfid == NULL ) {
145+ if (fuzzfid == NULL ) {
145146 fprintf (stderr , "Unable to open %s\n" , fuzz );
146147 exit (4 );
147148 }
148149 }
149150
150- queue_t * queue = chronicle_init (dir );
151+ queue_t * queue = chronicle_init (dir );
151152 chronicle_set_decoder (queue , & parse_kx , & free_kx );
152153 chronicle_set_encoder (queue , & sizeof_kx , & append_kx );
153154 chronicle_set_version (queue , 5 );
154155 chronicle_set_roll_scheme (queue , "FAST_HOURLY" );
155156 chronicle_set_create (queue , 1 );
156- if (chronicle_open (queue ) != 0 ) exit (-1 );
157-
158- tailer_t * tailer = chronicle_tailer (queue , print_msg , NULL , 0 );
157+ if (chronicle_open (queue ) != 0 )
158+ exit (-1 );
159+
160+ tailer_t * tailer = chronicle_tailer (queue , print_msg , NULL , 0 );
159161 chronicle_peek_queue (queue );
160162
161163 if (verboseflag ) {
162164 chronicle_debug ();
163165 }
164-
165- size_t linecap = 0 ;
166- ssize_t linelen = 0 ;
167- char * msgp = NULL ;
168- char * parsep = NULL ;
169166
170- int line = 0 ;
167+ size_t linecap = 0 ;
168+ ssize_t linelen = 0 ;
169+ char * msgp = NULL ;
170+ char * parsep = NULL ;
171+
172+ int line = 0 ;
171173 long long bytes ;
172174 long long time ;
173- uint64_t clock = 0 ;
174- uint64_t index = 0 ;
175+ uint64_t clock = 0 ;
176+ uint64_t index = 0 ;
175177
176- uint32_t xor_state [4 ];
178+ uint32_t xor_state [4 ];
177179
178- char * tmpfile = strdup ("/tmp/shmmain.XXXXXX" );
179- int tmpfid = c_mkstemp (tmpfile );
180+ char * tmpfile = strdup ("/tmp/shmmain.XXXXXX" );
181+ int tmpfid = c_mkstemp (tmpfile );
180182 printf ("logging fuzz expectations to %s fid %d msg %s\n" , tmpfile , tmpfid , strerror (errno ));
181- FILE * tmp = fdopen (tmpfid , "w+" );
183+ FILE * tmp = fdopen (tmpfid , "w+" );
182184 printf (" tmp is %p %s\n" , tmp , strerror (errno ));
183185 while ((linelen = getline (& msgp , & linecap , fuzzfid )) > 0 ) {
184186 line ++ ;
185187 parsep = msgp ;
186188 time = strtoll (parsep , & parsep , 0 );
187- if (* parsep == ' ' ) parsep ++ ;
189+ if (* parsep == ' ' )
190+ parsep ++ ;
188191 bytes = strtoll (parsep , & parsep , 0 );
189192
190193 printf (" FUZ: %lld millis, %lld bytes\n" , time , bytes );
191194 clock += time ;
192- xor_state [0 ] = xor_state [1 ] = xor_state [2 ] = xor_state [3 ] = (uint32_t )line + 1 ;
195+ xor_state [0 ] = xor_state [1 ] = xor_state [2 ] = xor_state [3 ] = (uint32_t )line + 1 ;
193196 K x = ktn (KC , bytes );
194197 for (long long b = 0 ; b < bytes ; b ++ ) {
195198 kG (x )[b ] = (uint8_t )xorshift128 (xor_state );
@@ -199,11 +202,14 @@ int main(const int argc, char **argv) {
199202 r0 (x );
200203 }
201204 // we're done parsing input, now replay checking using temp file
202- //fclose(tmp);
203- //close(tmpfid);
205+ // fclose(tmp);
206+ // close(tmpfid);
204207
205208 fflush (tmp );
206- if (fseek (tmp , 0L , SEEK_SET ) != 0 ) { printf ("abort not fseek" ); abort (); };
209+ if (fseek (tmp , 0L , SEEK_SET ) != 0 ) {
210+ printf ("abort not fseek" );
211+ abort ();
212+ };
207213
208214 int rline = 0 ;
209215 int r ;
@@ -216,7 +222,7 @@ int main(const int argc, char **argv) {
216222
217223 // TODO: verify!!
218224 } else if (rline != line ) {
219- printf ("error, %s r=%d at line %d!\n\n" , tmpfile , r , rline );
225+ printf ("error, %s r=%d at line %d!\n\n" , tmpfile , r , rline );
220226 }
221227 rline ++ ;
222228 } while (r != EOF );
@@ -225,7 +231,8 @@ int main(const int argc, char **argv) {
225231
226232 // unlink(tmpfile);
227233 free (tmpfile );
228- if (msgp ) free (msgp );
234+ if (msgp )
235+ free (msgp );
229236 printf ("parse finished\n" );
230237
231238 exit (rline == line ? 0 : 5 ); // exit with fail
0 commit comments