@@ -39,29 +39,10 @@ using namespace double_conversion;
3939#include " stage2_flatten.h"
4040#include " stage3_ape_machine.h"
4141#include " stage4_shovel_machine.h"
42-
42+ # include " jsonioutil.h "
4343using namespace std ;
4444
45- // get a corpus; pad out to cache line so we can always use SIMD
46- pair<u8 *, size_t > get_corpus (string filename) {
47- ifstream is (filename, ios::binary);
48- if (is) {
49- stringstream buffer;
50- buffer << is.rdbuf ();
51- size_t length = buffer.str ().size ();
52- char * aligned_buffer;
53- if (posix_memalign ( (void **)&aligned_buffer, 64 , ROUNDUP_N (length, 64 ))) {
54- cerr << " Could not allocate memory\n " ;
55- exit (1 );
56- };
57- memset (aligned_buffer, 0x20 , ROUNDUP_N (length, 64 ));
58- memcpy (aligned_buffer, buffer.str ().c_str (), length);
59- is.close ();
60- return make_pair ((u8 *)aligned_buffer, length);
61- }
62- throw " No corpus" ;
63- return make_pair ((u8 *)0 , (size_t )0 );
64- }
45+
6546
6647
6748// https://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
@@ -115,88 +96,20 @@ void colorfuldisplay(ParsedJson & pj, const u8 * buf) {
11596}
11697
11798
118- /* *
119- * Does the file filename ends with the given extension.
120- */
121- static bool hasExtension (const char *filename, const char *extension) {
122- const char *ext = strrchr (filename, ' .' );
123- return (ext && !strcmp (ext, extension));
124- }
12599
126- bool startsWith (const char *pre , const char *str) {
127- size_t lenpre = strlen (pre ),
128- lenstr = strlen (str);
129- return lenstr < lenpre ? false : strncmp (pre , str, lenpre) == 0 ;
130- }
131100
132- void validate () {
133- init_state_machine ();// to be safe
134- const char *dirname = " jsonchecker/" ; // ugly, hardcoded, brittle
135- const char *extension = " .json" ;
136- size_t dirlen = strlen (dirname);
137- struct dirent **entry_list;
138- int c = scandir (dirname, &entry_list, 0 , alphasort);
139- if (c < 0 ) {
140- printf (" error accessing %s \n " , dirname);
141- return ;
142- }
143- if (c == 0 ) {
144- printf (" nothing in dir %s \n " , dirname);
145- return ;
146- }
147- for (int i = 0 ; i < c; i++) {
148- const char *name = entry_list[i]->d_name ;
149- if (hasExtension (name, extension)) {
150- printf (" validating: file %s \n " ,name);
151- size_t filelen = strlen (name);
152- char *fullpath = (char *)malloc (dirlen + filelen + 1 );
153- strcpy (fullpath, dirname);
154- strcpy (fullpath + dirlen, name);
155- pair<u8 *, size_t > p = get_corpus (fullpath);
156- // terrible hack but just to get it working
157- ParsedJson * pj_ptr = new ParsedJson;
158- ParsedJson & pj (*pj_ptr);
159- if (posix_memalign ( (void **)&pj.structurals , 8 , ROUNDUP_N (p.second , 64 )/8 )) {
160- cerr << " Could not allocate memory\n " ;
161- return ;
162- };
163- pj.n_structural_indexes = 0 ;
164- u32 max_structures = ROUNDUP_N (p.second , 64 ) + 2 + 7 ;
165- pj.structural_indexes = new u32 [max_structures];
166- find_structural_bits (p.first , p.second , pj);
167- flatten_indexes (p.second , pj);
168- bool isok = ape_machine (p.first , p.second , pj);
169- if (isok)
170- isok = shovel_machine (p.first , p.second , pj);
171- if (startsWith (" pass" ,name)) {
172- if (!isok) printf (" warning: file %s should pass but it fails.\n " ,name);
173- }
174- if (startsWith (" fail" ,name)) {
175- if (isok) printf (" warning: file %s should fail but it passes.\n " ,name);
176- }
177- free (pj.structurals );
178- free (p.first );
179- delete[] pj.structural_indexes ;
180- free (fullpath);
181- }
182- }
183- for (int i = 0 ; i < c; ++i) free (entry_list[i]);
184- free (entry_list);
185- }
186101
187102int main (int argc, char * argv[]) {
188103 if (argc != 2 ) {
189- cerr << " Usage: " << argv[0 ] << " <jsonfile>\n " ;
190- cout << " We are going to validate:\n " << std::endl;
191- validate ();
104+ cerr << " Usage: " << argv[0 ] << " <jsonfile>" << endl;
192105 exit (1 );
193106 }
194107 pair<u8 *, size_t > p = get_corpus (argv[1 ]);
195108 ParsedJson * pj_ptr = new ParsedJson;
196109 ParsedJson & pj (*pj_ptr);
197110
198111 if (posix_memalign ( (void **)&pj.structurals , 8 , ROUNDUP_N (p.second , 64 )/8 )) {
199- cerr << " Could not allocate memory\n " ;
112+ cerr << " Could not allocate memory" << endl ;
200113 exit (1 );
201114 };
202115
@@ -237,38 +150,44 @@ int main(int argc, char * argv[]) {
237150 unsigned long cy1 = 0 , cy2 = 0 , cy3 = 0 , cy4 = 0 ;
238151 unsigned long cl1 = 0 , cl2 = 0 , cl3 = 0 , cl4 = 0 ;
239152#endif
153+ bool isok = true ;
240154 for (u32 i = 0 ; i < iterations; i++) {
241155 auto start = std::chrono::steady_clock::now ();
242156#ifndef SQUASH_COUNTERS
243157 unified.start ();
244158#endif
245- find_structural_bits (p.first , p.second , pj);
159+ isok = find_structural_bits (p.first , p.second , pj);
246160#ifndef SQUASH_COUNTERS
247161 unified.end (results);
248162 cy1 += results[0 ]; cl1 += results[1 ];
163+ if (! isok ) break ;
249164 unified.start ();
250165#endif
251- flatten_indexes (p.second , pj);
166+ isok = flatten_indexes (p.second , pj);
252167#ifndef SQUASH_COUNTERS
253168 unified.end (results);
254169 cy2 += results[0 ]; cl2 += results[1 ];
170+ if (! isok ) break ;
255171 unified.start ();
256172#endif
257- ape_machine (p.first , p.second , pj);
173+ isok = ape_machine (p.first , p.second , pj);
258174#ifndef SQUASH_COUNTERS
259175 unified.end (results);
260176 cy3 += results[0 ]; cl3 += results[1 ];
177+ if (! isok ) break ;
261178 unified.start ();
262179#endif
263- shovel_machine (p.first , p.second , pj);
180+ isok = shovel_machine (p.first , p.second , pj);
264181#ifndef SQUASH_COUNTERS
265182 unified.end (results);
266183 cy4 += results[0 ]; cl4 += results[1 ];
267184#endif
185+ if (! isok ) break ;
268186 auto end = std::chrono::steady_clock::now ();
269187 std::chrono::duration<double > secs = end - start;
270188 res[i] = secs.count ();
271189 }
190+
272191#ifndef SQUASH_COUNTERS
273192 printf (" number of bytes %ld number of structural chars %d ratio %.3f\n " , p.second , pj.n_structural_indexes ,
274193 (double ) pj.n_structural_indexes / p.second );
@@ -302,6 +221,10 @@ int main(int argc, char * argv[]) {
302221 free (pj.structurals );
303222 free (p.first );
304223 delete[] pj.structural_indexes ;
305- delete pj_ptr;
306- return 0 ;
224+ delete pj_ptr;
225+ if (! isok ) {
226+ printf (" Parsing failed. \n " );
227+ return EXIT_FAILURE;
228+ }
229+ return EXIT_SUCCESS;
307230}
0 commit comments