xref: /freebsd/tools/test/stress2/tools/flip.c (revision abd87254)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Flip one or more bits in a file.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 
36 #include <err.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 extern char *optarg;
44 extern int optind;
45 
46 static long
47 random_long(long mi, long ma)
48 {
49         return (arc4random()  % (ma - mi + 1) + mi);
50 }
51 
52 static void
53 flip(void *ap, size_t len)
54 {
55 	unsigned char *cp;
56 	int byte;
57 	unsigned char bit, buf, mask, old __unused;
58 
59 	cp = (unsigned char *)ap;
60 	byte = random_long(0, len - 1);
61 	bit = random_long(0,7);
62 	mask = ~(1 << bit);
63 	buf = cp[byte];
64 	old = cp[byte];
65 	buf = (buf & mask) | (~buf & ~mask);
66 	cp[byte] = buf;
67 #if defined(DEBUG)
68 	printf("Change %2x to %2x at %d by flipping bit %d\n",
69 	    old, buf, byte, bit);
70 #endif
71 }
72 
73 static void
74 trash(char *c)
75 {
76 	if (arc4random() % 2 == 1)
77 		*c = 0;
78 	else
79 		arc4random_buf(c, sizeof(c));
80 }
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	struct stat st;
86 	off_t pos;
87 	size_t size;
88 	int fd, i, times;
89 	char c;
90 
91 	times = 1;
92 	size = 0;
93 	while ((c = getopt(argc, argv, "n:s:")) != -1) {
94 		switch (c) {
95 			case 'n':
96 				times = atoi(optarg);
97 				break;
98 			case 's':
99 				size = atol(optarg);
100 				break;
101 			case '?':
102 			default:
103 				fprintf(stderr,
104 				    "Usage: %s [ -n <num> <file>]\n",
105 				    argv[0]);
106 				exit(1);
107 		}
108 	}
109 	argc -= optind;
110 	argv += optind;
111 
112 	if (argc != 1) {
113 		fprintf(stderr, "Missing file name\n");
114 		exit(1);
115 	}
116 
117 	if ((fd = open(argv[0], O_RDWR)) == -1)
118 		err(1, "open(%s)", argv[0]);
119 
120 	if (size == 0) {
121 		if (fstat(fd, &st) == -1)
122 			err(1, "stat %s", argv[0]);
123 		if ((st.st_mode & S_IFREG) == 0)
124 			errx(1, "%s must be a regular file\n", argv[0]);
125 		size = st.st_size;
126 	}
127 
128 	for (i = 0; i < times; i++) {
129 		pos = arc4random() % size;
130 		if (lseek(fd, pos, SEEK_SET) == -1)
131 			err(1, "lseek()");
132 		if (read(fd, &c, 1) != 1)
133 			err(1, "read()");
134 		if (arc4random() % 100 < 98)
135 			flip(&c, 1);
136 		else
137 			trash(&c);
138 		if (lseek(fd, pos, SEEK_SET) == -1)
139 			err(1, "lseek()");
140 		if (write(fd, &c, 1) != 1)
141 			err(1, "write()");
142 	}
143 
144 	return (0);
145 }
146