You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fault.c 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <ipxe/fault.h>
  27. /** @file
  28. *
  29. * Fault injection
  30. *
  31. */
  32. /**
  33. * Inject fault with a specified probability
  34. *
  35. * @v rate Reciprocal of fault probability (must be non-zero)
  36. * @ret rc Return status code
  37. */
  38. int inject_fault_nonzero ( unsigned int rate ) {
  39. /* Do nothing unless we want to inject a fault now */
  40. if ( ( random() % rate ) != 0 )
  41. return 0;
  42. /* Generate error number here so that faults can be injected
  43. * into files that don't themselves have error file
  44. * identifiers (via errfile.h).
  45. */
  46. return -EFAULT;
  47. }
  48. /**
  49. * Corrupt data with a specified probability
  50. *
  51. * @v rate Reciprocal of fault probability (must be non-zero)
  52. * @v data Data
  53. * @v len Length of data
  54. * @ret rc Return status code
  55. */
  56. void inject_corruption_nonzero ( unsigned int rate, const void *data,
  57. size_t len ) {
  58. uint8_t *writable;
  59. size_t offset;
  60. /* Do nothing if we have no data to corrupt */
  61. if ( ! len )
  62. return;
  63. /* Do nothing unless we want to inject a fault now */
  64. if ( ! inject_fault_nonzero ( rate ) )
  65. return;
  66. /* Get a writable pointer to the nominally read-only data */
  67. writable = ( ( uint8_t * ) data );
  68. /* Pick a random victim byte and zap it */
  69. offset = ( random() % len );
  70. writable[offset] ^= random();
  71. }