iPXE
fault.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <errno.h>
00028 #include <ipxe/fault.h>
00029 
00030 /** @file
00031  *
00032  * Fault injection
00033  *
00034  */
00035 
00036 /**
00037  * Inject fault with a specified probability
00038  *
00039  * @v rate              Reciprocal of fault probability (must be non-zero)
00040  * @ret rc              Return status code
00041  */
00042 int inject_fault_nonzero ( unsigned int rate ) {
00043 
00044         /* Do nothing unless we want to inject a fault now */
00045         if ( ( random() % rate ) != 0 )
00046                 return 0;
00047 
00048         /* Generate error number here so that faults can be injected
00049          * into files that don't themselves have error file
00050          * identifiers (via errfile.h).
00051          */
00052         return -EFAULT;
00053 }
00054 
00055 /**
00056  * Corrupt data with a specified probability
00057  *
00058  * @v rate              Reciprocal of fault probability (must be non-zero)
00059  * @v data              Data
00060  * @v len               Length of data
00061  * @ret rc              Return status code
00062  */
00063 void inject_corruption_nonzero ( unsigned int rate, const void *data,
00064                                  size_t len ) {
00065         uint8_t *writable;
00066         size_t offset;
00067 
00068         /* Do nothing if we have no data to corrupt */
00069         if ( ! len )
00070                 return;
00071 
00072         /* Do nothing unless we want to inject a fault now */
00073         if ( ! inject_fault_nonzero ( rate ) )
00074                 return;
00075 
00076         /* Get a writable pointer to the nominally read-only data */
00077         writable = ( ( uint8_t * ) data );
00078 
00079         /* Pick a random victim byte and zap it */
00080         offset = ( random() % len );
00081         writable[offset] ^= random();
00082 }