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.
void inject_corruption_nonzero (unsigned int rate, const void *data, size_t len)
 Corrupt data with a specified probability.

Detailed Description

Fault injection.

Definition in file fault.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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.

References EFAULT, and random().

Referenced by __attribute__(), and inject_corruption_nonzero().

                                               {

        /* Do nothing unless we want to inject a fault now */
        if ( ( random() % rate ) != 0 )
                return 0;

        /* Generate error number here so that faults can be injected
         * into files that don't themselves have error file
         * identifiers (via errfile.h).
         */
        return -EFAULT;
}
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.

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

                                              {
        uint8_t *writable;
        size_t offset;

        /* Do nothing if we have no data to corrupt */
        if ( ! len )
                return;

        /* Do nothing unless we want to inject a fault now */
        if ( ! inject_fault_nonzero ( rate ) )
                return;

        /* Get a writable pointer to the nominally read-only data */
        writable = ( ( uint8_t * ) data );

        /* Pick a random victim byte and zap it */
        offset = ( random() % len );
        writable[offset] ^= random();
}