summaryrefslogtreecommitdiffstats
path: root/src/core/fault.c
diff options
context:
space:
mode:
authorMichael Brown2015-07-22 15:29:20 +0200
committerMichael Brown2015-07-22 22:17:47 +0200
commit89816af2a4b7dfc42aef880912dad8fad28e974b (patch)
tree9014cc1eb6accdd79b77c4dbdb0b8bb3523694fc /src/core/fault.c
parent[fault] Generalise NETDEV_DISCARD_RATE fault injection mechanism (diff)
downloadipxe-89816af2a4b7dfc42aef880912dad8fad28e974b.tar.gz
ipxe-89816af2a4b7dfc42aef880912dad8fad28e974b.tar.xz
ipxe-89816af2a4b7dfc42aef880912dad8fad28e974b.zip
[fault] Add inject_corruption() to randomly corrupt data
Provide an inject_corruption() function that can be used to randomly corrupt data bytes with configurable probabilities. Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/fault.c')
-rw-r--r--src/core/fault.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/core/fault.c b/src/core/fault.c
index 91289fde..63d3ccac 100644
--- a/src/core/fault.c
+++ b/src/core/fault.c
@@ -51,3 +51,32 @@ int inject_fault_nonzero ( unsigned int rate ) {
*/
return -EFAULT;
}
+
+/**
+ * Corrupt data with a specified probability
+ *
+ * @v rate Reciprocal of fault probability (must be non-zero)
+ * @v data Data
+ * @v len Length of data
+ * @ret rc Return status code
+ */
+void inject_corruption_nonzero ( unsigned int rate, const void *data,
+ size_t len ) {
+ 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();
+}