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;
157 DBusResultCode result;
159 _dbus_string_append (&filename, ".message-raw");
161 _dbus_string_get_const_data (&filename, &filename_c);
162 printf ("Child failed, writing %s\n",
165 result = _dbus_string_save_to_file (data, &filename);
167 if (result != DBUS_RESULT_SUCCESS)
169 fprintf (stderr, "Failed to save failed message data: %s\n",
170 dbus_result_to_string (result));
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_do_n_things (const DBusString *orig_data,
343 void (* functions[]) (const DBusString *orig_data,
344 DBusString *mutated) =
346 randomly_shorten_or_lengthen,
347 randomly_change_one_byte,
348 randomly_add_one_byte,
349 randomly_remove_one_byte,
350 randomly_modify_length
353 _dbus_string_set_length (mutated, 0);
355 if (!_dbus_string_copy (orig_data, 0, mutated, 0))
356 _dbus_assert_not_reached ("out of mem");
363 which = random_int_in_range (0, _DBUS_N_ELEMENTS (functions));
365 (* functions[which]) (mutated, mutated);
372 find_breaks_based_on (const DBusString *filename,
374 DBusMessageValidity expected_validity,
377 DBusString orig_data;
379 const char *filename_c;
383 _dbus_string_get_const_data (filename, &filename_c);
387 if (!_dbus_string_init (&orig_data, _DBUS_INT_MAX))
388 _dbus_assert_not_reached ("could not allocate string\n");
390 if (!_dbus_string_init (&mutated, _DBUS_INT_MAX))
391 _dbus_assert_not_reached ("could not allocate string\n");
393 if (!dbus_internal_do_not_use_load_message_file (filename, is_raw,
396 fprintf (stderr, "could not load file %s\n", filename_c);
403 randomly_change_one_byte (&orig_data, &mutated);
404 try_mutated_data (&mutated);
412 randomly_modify_length (&orig_data, &mutated);
413 try_mutated_data (&mutated);
421 randomly_remove_one_byte (&orig_data, &mutated);
422 try_mutated_data (&mutated);
430 randomly_add_one_byte (&orig_data, &mutated);
431 try_mutated_data (&mutated);
439 randomly_shorten_or_lengthen (&orig_data, &mutated);
440 try_mutated_data (&mutated);
448 randomly_do_n_things (&orig_data, &mutated, 2);
449 try_mutated_data (&mutated);
457 randomly_do_n_things (&orig_data, &mutated, 3);
458 try_mutated_data (&mutated);
466 randomly_do_n_things (&orig_data, &mutated, 4);
467 try_mutated_data (&mutated);
476 _dbus_string_free (&orig_data);
477 _dbus_string_free (&mutated);
479 /* FALSE means end the whole process */
484 get_random_seed (void)
493 if (!_dbus_string_init (&bytes, _DBUS_INT_MAX))
496 fd = open ("/dev/urandom", O_RDONLY);
500 if (_dbus_read (fd, &bytes, 4) != 4)
505 _dbus_string_get_const_data (&bytes, &s);
507 seed = * (unsigned int*) s;
514 fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n");
516 _dbus_get_current_time (NULL, &tv_usec);
522 _dbus_string_free (&bytes);
531 const char *test_data_dir;
532 const char *failure_dir_c;
533 int total_failures_found;
536 test_data_dir = argv[1];
539 fprintf (stderr, "Must specify a top_srcdir/test/data directory\n");
543 total_failures_found = 0;
546 if (!_dbus_string_init (&failure_dir, _DBUS_INT_MAX))
549 /* so you can leave it overnight safely */
550 #define MAX_FAILURES 1000
552 while (total_failures_found < MAX_FAILURES)
556 failures_this_iteration = 0;
558 seed = get_random_seed ();
560 _dbus_string_set_length (&failure_dir, 0);
562 if (!_dbus_string_append (&failure_dir, "failures-"))
565 if (!_dbus_string_append_uint (&failure_dir, seed))
568 _dbus_string_get_const_data (&failure_dir, &failure_dir_c);
570 if (mkdir (failure_dir_c, 0700) < 0)
573 fprintf (stderr, "didn't mkdir %s: %s\n",
574 failure_dir_c, strerror (errno));
577 printf ("next seed = %u \ttotal failures %d of %d attempts\n",
578 seed, total_failures_found, total_attempts);
582 if (!dbus_internal_do_not_use_foreach_message_file (test_data_dir,
583 find_breaks_based_on,
586 fprintf (stderr, "fatal error iterating over message files\n");
587 rmdir (failure_dir_c);
591 printf ("Found %d failures with seed %u stored in %s\n",
592 failures_this_iteration, seed, failure_dir_c);
594 total_failures_found += failures_this_iteration;
596 rmdir (failure_dir_c); /* does nothing if non-empty */