1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-break-loader.c Program to find byte streams that break the message loader
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <dbus/dbus.h>
26 #include <sys/types.h>
35 #define DBUS_COMPILATION
36 #include <dbus/dbus-string.h>
37 #include <dbus/dbus-internals.h>
38 #include <dbus/dbus-test.h>
39 #include <dbus/dbus-marshal.h>
40 #undef DBUS_COMPILATION
42 static DBusString failure_dir;
43 static int total_attempts;
44 static int failures_this_iteration;
47 random_int_in_range (int start,
50 /* such elegant math */
58 _dbus_assert (end > start);
60 gap = end - start - 1; /* -1 to not include "end" */
61 v_double = ((double)start) + (((double)rand ())/RAND_MAX) * gap;
69 fprintf (stderr, "random_int_in_range() generated %d for range [%d,%d)\n",
75 fprintf (stderr, "random_int_in_range() generated %d for range [%d,%d)\n",
80 /* printf (" %d of [%d,%d)\n", v, start, end); */
86 try_mutated_data (const DBusString *data)
91 /* printf (" attempt %d\n", total_attempts); */
97 fprintf (stderr, "fork() failed: %s\n",
105 /* Child, try loading the data */
106 if (!dbus_internal_do_not_use_try_message_data (data, _DBUS_MESSAGE_UNKNOWN))
113 /* Parent, wait for child */
118 if (waitpid (pid, &status, 0) < 0)
120 fprintf (stderr, "waitpid() failed: %s\n", strerror (errno));
127 if (!_dbus_string_init (&filename) ||
128 !_dbus_string_copy (&failure_dir, 0,
130 !_dbus_string_append_byte (&filename, '/'))
132 fprintf (stderr, "out of memory\n");
136 _dbus_string_append_int (&filename, total_attempts);
138 if (WIFEXITED (status))
140 if (WEXITSTATUS (status) != 0)
142 _dbus_string_append (&filename, "-exited-");
143 _dbus_string_append_int (&filename, WEXITSTATUS (status));
147 else if (WIFSIGNALED (status))
149 _dbus_string_append (&filename, "signaled-");
150 _dbus_string_append_int (&filename, WTERMSIG (status));
158 _dbus_string_append (&filename, ".message-raw");
160 printf ("Child failed, writing %s\n", _dbus_string_get_const_data (&filename));
162 dbus_error_init (&error);
163 if (!_dbus_string_save_to_file (data, &filename, &error))
165 fprintf (stderr, "Failed to save failed message data: %s\n",
167 dbus_error_free (&error);
168 exit (1); /* so we can see the seed that was printed out */
171 failures_this_iteration += 1;
173 _dbus_string_free (&filename);
179 _dbus_string_free (&filename);
184 _dbus_assert_not_reached ("should not be reached");
189 randomly_shorten_or_lengthen (const DBusString *orig_data,
194 if (orig_data != mutated)
196 _dbus_string_set_length (mutated, 0);
198 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
199 _dbus_assert_not_reached ("out of mem");
202 if (_dbus_string_get_length (mutated) == 0)
203 delta = random_int_in_range (0, 10);
205 delta = random_int_in_range (- _dbus_string_get_length (mutated),
206 _dbus_string_get_length (mutated) * 3);
209 _dbus_string_shorten (mutated, - delta);
214 i = _dbus_string_get_length (mutated);
215 if (!_dbus_string_lengthen (mutated, delta))
216 _dbus_assert_not_reached ("couldn't lengthen string");
218 while (i < _dbus_string_get_length (mutated))
220 _dbus_string_set_byte (mutated,
222 random_int_in_range (0, 256));
229 randomly_change_one_byte (const DBusString *orig_data,
234 if (orig_data != mutated)
236 _dbus_string_set_length (mutated, 0);
238 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
239 _dbus_assert_not_reached ("out of mem");
242 if (_dbus_string_get_length (mutated) == 0)
245 i = random_int_in_range (0, _dbus_string_get_length (mutated));
247 _dbus_string_set_byte (mutated, i,
248 random_int_in_range (0, 256));
252 randomly_remove_one_byte (const DBusString *orig_data,
257 if (orig_data != mutated)
259 _dbus_string_set_length (mutated, 0);
261 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
262 _dbus_assert_not_reached ("out of mem");
265 if (_dbus_string_get_length (mutated) == 0)
268 i = random_int_in_range (0, _dbus_string_get_length (mutated));
270 _dbus_string_delete (mutated, i, 1);
275 randomly_add_one_byte (const DBusString *orig_data,
280 if (orig_data != mutated)
282 _dbus_string_set_length (mutated, 0);
284 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
285 _dbus_assert_not_reached ("out of mem");
288 i = random_int_in_range (0, _dbus_string_get_length (mutated));
290 _dbus_string_insert_byte (mutated, i,
291 random_int_in_range (0, 256));
295 randomly_modify_length (const DBusString *orig_data,
304 if (orig_data != mutated)
306 _dbus_string_set_length (mutated, 0);
308 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
309 _dbus_assert_not_reached ("out of mem");
312 if (_dbus_string_get_length (mutated) < 12)
315 d = _dbus_string_get_const_data (mutated);
317 if (!(*d == DBUS_LITTLE_ENDIAN ||
318 *d == DBUS_BIG_ENDIAN))
323 i = random_int_in_range (4, _dbus_string_get_length (mutated) - 8);
324 i = _DBUS_ALIGN_VALUE (i, 4);
326 orig = _dbus_demarshal_uint32 (mutated, byte_order, i, NULL);
328 delta = random_int_in_range (-10, 10);
330 _dbus_marshal_set_uint32 (mutated, byte_order, i,
331 (unsigned) (orig + delta));
335 randomly_set_extreme_ints (const DBusString *orig_data,
342 static int which = 0;
343 unsigned int extreme_ints[] = {
350 (unsigned int) (_DBUS_INT_MAX + 1),
351 (unsigned int) (_DBUS_UINT_MAX + 1),
360 if (orig_data != mutated)
362 _dbus_string_set_length (mutated, 0);
364 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
365 _dbus_assert_not_reached ("out of mem");
368 if (_dbus_string_get_length (mutated) < 12)
371 d = _dbus_string_get_const_data (mutated);
373 if (!(*d == DBUS_LITTLE_ENDIAN ||
374 *d == DBUS_BIG_ENDIAN))
379 i = random_int_in_range (4, _dbus_string_get_length (mutated) - 8);
380 i = _DBUS_ALIGN_VALUE (i, 4);
382 orig = _dbus_demarshal_uint32 (mutated, byte_order, i, NULL);
384 which = random_int_in_range (0, _DBUS_N_ELEMENTS (extreme_ints));
386 _dbus_assert (which >= 0);
387 _dbus_assert (which < _DBUS_N_ELEMENTS (extreme_ints));
389 _dbus_marshal_set_uint32 (mutated, byte_order, i,
390 extreme_ints[which]);
393 static int times_we_did_each_thing[6] = { 0, };
396 randomly_do_n_things (const DBusString *orig_data,
401 void (* functions[]) (const DBusString *orig_data,
402 DBusString *mutated) =
404 randomly_shorten_or_lengthen,
405 randomly_change_one_byte,
406 randomly_add_one_byte,
407 randomly_remove_one_byte,
408 randomly_modify_length,
409 randomly_set_extreme_ints
412 _dbus_string_set_length (mutated, 0);
414 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
415 _dbus_assert_not_reached ("out of mem");
422 which = random_int_in_range (0, _DBUS_N_ELEMENTS (functions));
424 (* functions[which]) (mutated, mutated);
425 times_we_did_each_thing[which] += 1;
432 find_breaks_based_on (const DBusString *filename,
434 DBusMessageValidity expected_validity,
437 DBusString orig_data;
439 const char *filename_c;
443 filename_c = _dbus_string_get_const_data (filename);
447 if (!_dbus_string_init (&orig_data))
448 _dbus_assert_not_reached ("could not allocate string\n");
450 if (!_dbus_string_init (&mutated))
451 _dbus_assert_not_reached ("could not allocate string\n");
453 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
456 fprintf (stderr, "could not load file %s\n", filename_c);
463 randomly_change_one_byte (&orig_data, &mutated);
464 try_mutated_data (&mutated);
472 randomly_modify_length (&orig_data, &mutated);
473 try_mutated_data (&mutated);
481 randomly_remove_one_byte (&orig_data, &mutated);
482 try_mutated_data (&mutated);
490 randomly_add_one_byte (&orig_data, &mutated);
491 try_mutated_data (&mutated);
499 randomly_set_extreme_ints (&orig_data, &mutated);
500 try_mutated_data (&mutated);
508 randomly_shorten_or_lengthen (&orig_data, &mutated);
509 try_mutated_data (&mutated);
517 randomly_do_n_things (&orig_data, &mutated, 2);
518 try_mutated_data (&mutated);
526 randomly_do_n_things (&orig_data, &mutated, 3);
527 try_mutated_data (&mutated);
535 randomly_do_n_things (&orig_data, &mutated, 4);
536 try_mutated_data (&mutated);
545 _dbus_string_free (&orig_data);
546 _dbus_string_free (&mutated);
548 /* FALSE means end the whole process */
553 get_random_seed (void)
562 if (!_dbus_string_init (&bytes))
565 fd = open ("/dev/urandom", O_RDONLY);
569 if (_dbus_read (fd, &bytes, 4) != 4)
574 s = _dbus_string_get_const_data (&bytes);
576 seed = * (unsigned int*) s;
583 fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n");
585 _dbus_get_current_time (NULL, &tv_usec);
591 _dbus_string_free (&bytes);
600 const char *test_data_dir;
601 const char *failure_dir_c;
602 int total_failures_found;
605 test_data_dir = argv[1];
608 fprintf (stderr, "Must specify a top_srcdir/test/data directory\n");
612 total_failures_found = 0;
615 if (!_dbus_string_init (&failure_dir))
618 /* so you can leave it overnight safely */
619 #define MAX_FAILURES 1000
621 while (total_failures_found < MAX_FAILURES)
625 failures_this_iteration = 0;
627 seed = get_random_seed ();
629 _dbus_string_set_length (&failure_dir, 0);
631 if (!_dbus_string_append (&failure_dir, "failures-"))
634 if (!_dbus_string_append_uint (&failure_dir, seed))
637 failure_dir_c = _dbus_string_get_const_data (&failure_dir);
639 if (mkdir (failure_dir_c, 0700) < 0)
642 fprintf (stderr, "didn't mkdir %s: %s\n",
643 failure_dir_c, strerror (errno));
646 printf ("next seed = %u \ttotal failures %d of %d attempts\n",
647 seed, total_failures_found, total_attempts);
651 if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
652 find_breaks_based_on,
655 fprintf (stderr, "fatal error iterating over message files\n");
656 rmdir (failure_dir_c);
660 printf (" did %d random mutations: %d %d %d %d %d %d\n",
661 _DBUS_N_ELEMENTS (times_we_did_each_thing),
662 times_we_did_each_thing[0],
663 times_we_did_each_thing[1],
664 times_we_did_each_thing[2],
665 times_we_did_each_thing[3],
666 times_we_did_each_thing[4],
667 times_we_did_each_thing[5]);
669 printf ("Found %d failures with seed %u stored in %s\n",
670 failures_this_iteration, seed, failure_dir_c);
672 total_failures_found += failures_this_iteration;
674 rmdir (failure_dir_c); /* does nothing if non-empty */