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, _DBUS_INT_MAX) ||
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));
156 const char *filename_c;
159 _dbus_string_append (&filename, ".message-raw");
161 _dbus_string_get_const_data (&filename, &filename_c);
162 printf ("Child failed, writing %s\n",
165 dbus_error_init (&error);
166 if (!_dbus_string_save_to_file (data, &filename, &error))
168 fprintf (stderr, "Failed to save failed message data: %s\n",
170 dbus_error_free (&error);
171 exit (1); /* so we can see the seed that was printed out */
174 failures_this_iteration += 1;
176 _dbus_string_free (&filename);
182 _dbus_string_free (&filename);
187 _dbus_assert_not_reached ("should not be reached");
192 randomly_shorten_or_lengthen (const DBusString *orig_data,
197 if (orig_data != mutated)
199 _dbus_string_set_length (mutated, 0);
201 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
202 _dbus_assert_not_reached ("out of mem");
205 if (_dbus_string_get_length (mutated) == 0)
206 delta = random_int_in_range (0, 10);
208 delta = random_int_in_range (- _dbus_string_get_length (mutated),
209 _dbus_string_get_length (mutated) * 3);
212 _dbus_string_shorten (mutated, - delta);
217 i = _dbus_string_get_length (mutated);
218 if (!_dbus_string_lengthen (mutated, delta))
219 _dbus_assert_not_reached ("couldn't lengthen string");
221 while (i < _dbus_string_get_length (mutated))
223 _dbus_string_set_byte (mutated,
225 random_int_in_range (0, 256));
232 randomly_change_one_byte (const DBusString *orig_data,
237 if (orig_data != mutated)
239 _dbus_string_set_length (mutated, 0);
241 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
242 _dbus_assert_not_reached ("out of mem");
245 if (_dbus_string_get_length (mutated) == 0)
248 i = random_int_in_range (0, _dbus_string_get_length (mutated));
250 _dbus_string_set_byte (mutated, i,
251 random_int_in_range (0, 256));
255 randomly_remove_one_byte (const DBusString *orig_data,
260 if (orig_data != mutated)
262 _dbus_string_set_length (mutated, 0);
264 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
265 _dbus_assert_not_reached ("out of mem");
268 if (_dbus_string_get_length (mutated) == 0)
271 i = random_int_in_range (0, _dbus_string_get_length (mutated));
273 _dbus_string_delete (mutated, i, 1);
278 randomly_add_one_byte (const DBusString *orig_data,
283 if (orig_data != mutated)
285 _dbus_string_set_length (mutated, 0);
287 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
288 _dbus_assert_not_reached ("out of mem");
291 i = random_int_in_range (0, _dbus_string_get_length (mutated));
293 _dbus_string_insert_byte (mutated, i,
294 random_int_in_range (0, 256));
298 randomly_modify_length (const DBusString *orig_data,
307 if (orig_data != mutated)
309 _dbus_string_set_length (mutated, 0);
311 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
312 _dbus_assert_not_reached ("out of mem");
315 if (_dbus_string_get_length (mutated) < 12)
318 _dbus_string_get_const_data (mutated, &d);
320 if (!(*d == DBUS_LITTLE_ENDIAN ||
321 *d == DBUS_BIG_ENDIAN))
326 i = random_int_in_range (4, _dbus_string_get_length (mutated) - 8);
327 i = _DBUS_ALIGN_VALUE (i, 4);
329 orig = _dbus_demarshal_uint32 (mutated, byte_order, i, NULL);
331 delta = random_int_in_range (-10, 10);
333 _dbus_marshal_set_uint32 (mutated, byte_order, i,
334 (unsigned) (orig + delta));
338 randomly_set_extreme_ints (const DBusString *orig_data,
345 static int which = 0;
346 unsigned int extreme_ints[] = {
353 (unsigned int) (_DBUS_INT_MAX + 1),
354 (unsigned int) (_DBUS_UINT_MAX + 1),
363 if (orig_data != mutated)
365 _dbus_string_set_length (mutated, 0);
367 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
368 _dbus_assert_not_reached ("out of mem");
371 if (_dbus_string_get_length (mutated) < 12)
374 _dbus_string_get_const_data (mutated, &d);
376 if (!(*d == DBUS_LITTLE_ENDIAN ||
377 *d == DBUS_BIG_ENDIAN))
382 i = random_int_in_range (4, _dbus_string_get_length (mutated) - 8);
383 i = _DBUS_ALIGN_VALUE (i, 4);
385 orig = _dbus_demarshal_uint32 (mutated, byte_order, i, NULL);
387 which = random_int_in_range (0, _DBUS_N_ELEMENTS (extreme_ints));
389 _dbus_assert (which >= 0);
390 _dbus_assert (which < _DBUS_N_ELEMENTS (extreme_ints));
392 _dbus_marshal_set_uint32 (mutated, byte_order, i,
393 extreme_ints[which]);
396 static int times_we_did_each_thing[6] = { 0, };
399 randomly_do_n_things (const DBusString *orig_data,
404 void (* functions[]) (const DBusString *orig_data,
405 DBusString *mutated) =
407 randomly_shorten_or_lengthen,
408 randomly_change_one_byte,
409 randomly_add_one_byte,
410 randomly_remove_one_byte,
411 randomly_modify_length,
412 randomly_set_extreme_ints
415 _dbus_string_set_length (mutated, 0);
417 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
418 _dbus_assert_not_reached ("out of mem");
425 which = random_int_in_range (0, _DBUS_N_ELEMENTS (functions));
427 (* functions[which]) (mutated, mutated);
428 times_we_did_each_thing[which] += 1;
435 find_breaks_based_on (const DBusString *filename,
437 DBusMessageValidity expected_validity,
440 DBusString orig_data;
442 const char *filename_c;
446 _dbus_string_get_const_data (filename, &filename_c);
450 if (!_dbus_string_init (&orig_data, _DBUS_INT_MAX))
451 _dbus_assert_not_reached ("could not allocate string\n");
453 if (!_dbus_string_init (&mutated, _DBUS_INT_MAX))
454 _dbus_assert_not_reached ("could not allocate string\n");
456 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
459 fprintf (stderr, "could not load file %s\n", filename_c);
466 randomly_change_one_byte (&orig_data, &mutated);
467 try_mutated_data (&mutated);
475 randomly_modify_length (&orig_data, &mutated);
476 try_mutated_data (&mutated);
484 randomly_remove_one_byte (&orig_data, &mutated);
485 try_mutated_data (&mutated);
493 randomly_add_one_byte (&orig_data, &mutated);
494 try_mutated_data (&mutated);
502 randomly_set_extreme_ints (&orig_data, &mutated);
503 try_mutated_data (&mutated);
511 randomly_shorten_or_lengthen (&orig_data, &mutated);
512 try_mutated_data (&mutated);
520 randomly_do_n_things (&orig_data, &mutated, 2);
521 try_mutated_data (&mutated);
529 randomly_do_n_things (&orig_data, &mutated, 3);
530 try_mutated_data (&mutated);
538 randomly_do_n_things (&orig_data, &mutated, 4);
539 try_mutated_data (&mutated);
548 _dbus_string_free (&orig_data);
549 _dbus_string_free (&mutated);
551 /* FALSE means end the whole process */
556 get_random_seed (void)
565 if (!_dbus_string_init (&bytes, _DBUS_INT_MAX))
568 fd = open ("/dev/urandom", O_RDONLY);
572 if (_dbus_read (fd, &bytes, 4) != 4)
577 _dbus_string_get_const_data (&bytes, &s);
579 seed = * (unsigned int*) s;
586 fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n");
588 _dbus_get_current_time (NULL, &tv_usec);
594 _dbus_string_free (&bytes);
603 const char *test_data_dir;
604 const char *failure_dir_c;
605 int total_failures_found;
608 test_data_dir = argv[1];
611 fprintf (stderr, "Must specify a top_srcdir/test/data directory\n");
615 total_failures_found = 0;
618 if (!_dbus_string_init (&failure_dir, _DBUS_INT_MAX))
621 /* so you can leave it overnight safely */
622 #define MAX_FAILURES 1000
624 while (total_failures_found < MAX_FAILURES)
628 failures_this_iteration = 0;
630 seed = get_random_seed ();
632 _dbus_string_set_length (&failure_dir, 0);
634 if (!_dbus_string_append (&failure_dir, "failures-"))
637 if (!_dbus_string_append_uint (&failure_dir, seed))
640 _dbus_string_get_const_data (&failure_dir, &failure_dir_c);
642 if (mkdir (failure_dir_c, 0700) < 0)
645 fprintf (stderr, "didn't mkdir %s: %s\n",
646 failure_dir_c, strerror (errno));
649 printf ("next seed = %u \ttotal failures %d of %d attempts\n",
650 seed, total_failures_found, total_attempts);
654 if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
655 find_breaks_based_on,
658 fprintf (stderr, "fatal error iterating over message files\n");
659 rmdir (failure_dir_c);
663 printf (" did %d random mutations: %d %d %d %d %d %d\n",
664 _DBUS_N_ELEMENTS (times_we_did_each_thing),
665 times_we_did_each_thing[0],
666 times_we_did_each_thing[1],
667 times_we_did_each_thing[2],
668 times_we_did_each_thing[3],
669 times_we_did_each_thing[4],
670 times_we_did_each_thing[5]);
672 printf ("Found %d failures with seed %u stored in %s\n",
673 failures_this_iteration, seed, failure_dir_c);
675 total_failures_found += failures_this_iteration;
677 rmdir (failure_dir_c); /* does nothing if non-empty */