iPXE
fault.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26#include <stdlib.h>
27#include <errno.h>
28#include <ipxe/fault.h>
29
30/** @file
31 *
32 * Fault injection
33 *
34 */
35
36/**
37 * Inject fault with a specified probability
38 *
39 * @v rate Reciprocal of fault probability (must be non-zero)
40 * @ret rc Return status code
41 */
42int inject_fault_nonzero ( unsigned int rate ) {
43
44 /* Do nothing unless we want to inject a fault now */
45 if ( ( random() % rate ) != 0 )
46 return 0;
47
48 /* Generate error number here so that faults can be injected
49 * into files that don't themselves have error file
50 * identifiers (via errfile.h).
51 */
52 return -EFAULT;
53}
54
55/**
56 * Corrupt data with a specified probability
57 *
58 * @v rate Reciprocal of fault probability (must be non-zero)
59 * @v data Data
60 * @v len Length of data
61 * @ret rc Return status code
62 */
63void inject_corruption_nonzero ( unsigned int rate, const void *data,
64 size_t len ) {
65 uint8_t *writable;
66 size_t offset;
67
68 /* Do nothing if we have no data to corrupt */
69 if ( ! len )
70 return;
71
72 /* Do nothing unless we want to inject a fault now */
73 if ( ! inject_fault_nonzero ( rate ) )
74 return;
75
76 /* Get a writable pointer to the nominally read-only data */
77 writable = ( ( uint8_t * ) data );
78
79 /* Pick a random victim byte and zap it */
80 offset = ( random() % len );
81 writable[offset] ^= random();
82}
unsigned char uint8_t
Definition stdint.h:10
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
int inject_fault_nonzero(unsigned int rate)
Inject fault with a specified probability.
Definition fault.c:42
void inject_corruption_nonzero(unsigned int rate, const void *data, size_t len)
Corrupt data with a specified probability.
Definition fault.c:63
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EFAULT
Bad address.
Definition errno.h:394
Fault injection.
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32