iPXE
Functions
fault.c File Reference

Fault injection. More...

#include <stdlib.h>
#include <errno.h>
#include <ipxe/fault.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int inject_fault_nonzero (unsigned int rate)
 Inject fault with a specified probability. More...
 
void inject_corruption_nonzero (unsigned int rate, const void *data, size_t len)
 Corrupt data with a specified probability. More...
 

Detailed Description

Fault injection.

Definition in file fault.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ inject_fault_nonzero()

int inject_fault_nonzero ( unsigned int  rate)

Inject fault with a specified probability.

Parameters
rateReciprocal of fault probability (must be non-zero)
Return values
rcReturn status code

Definition at line 42 of file fault.c.

42  {
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 }
#define EFAULT
Bad address.
Definition: errno.h:393
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:30

References EFAULT, and random().

Referenced by __attribute__(), and inject_corruption_nonzero().

◆ inject_corruption_nonzero()

void inject_corruption_nonzero ( unsigned int  rate,
const void *  data,
size_t  len 
)

Corrupt data with a specified probability.

Parameters
rateReciprocal of fault probability (must be non-zero)
dataData
lenLength of data
Return values
rcReturn status code

Definition at line 63 of file fault.c.

64  {
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 }
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:30
unsigned char uint8_t
Definition: stdint.h:10
int inject_fault_nonzero(unsigned int rate)
Inject fault with a specified probability.
Definition: fault.c:42
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References data, inject_fault_nonzero(), len, offset, and random().