Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / test / stress / stress2 / testcases / badcode / badcode.c
1 /*
2  *             COPYRIGHT (c) 1990 BY             *
3  *  GEORGE J. CARRETTE, CONCORD, MASSACHUSETTS.  *
4  *             ALL RIGHTS RESERVED               *
5
6 Permission to use, copy, modify, distribute and sell this software
7 and its documentation for any purpose and without fee is hereby
8 granted, provided that the above copyright notice appear in all copies
9 and that both that copyright notice and this permission notice appear
10 in supporting documentation, and that the name of the author
11 not be used in advertising or publicity pertaining to distribution
12 of the software without specific, written prior permission.
13
14 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16 HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 SOFTWARE.
21
22 This code is based on crashme.c
23
24 */
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <string.h>
36 #include <err.h>
37
38 #include "stress.h"
39
40 #define BUFS 64
41
42 char buf [BUFS];
43 pid_t pid;
44
45 void (*sub)();
46
47 void
48 proto(void) {
49         int i = 2;
50         printf("Hello, world (%d)\n", i);
51         return;
52 }
53
54 void
55 alter(void) {   /* Change one byte in the code */
56         int i;
57         i = random() % BUFS;
58         buf[i] = random() & 0xff;
59 }
60
61 void
62 hand(int i) {   /* alarm handler */
63         if (pid != 0) {
64                 kill(pid, SIGHUP);
65                 kill(pid, SIGKILL);
66         }
67         exit(1);
68 }
69
70 int
71 setup(int nb)
72 {
73         return (0);
74 }
75
76 void
77 cleanup(void)
78 {
79 }
80
81 int
82 test(void)
83 {
84         pid_t pid;
85         int i, status;
86
87         for (i = 0; i < 512; i++) {
88                 if (i % 10 == 0)
89                         bcopy(proto, buf, BUFS);
90                 alter();
91
92                 if ((pid = fork()) == 0) {
93                         signal(SIGALRM, hand);
94 #if 0
95                         signal(SIGILL,  hand);
96                         signal(SIGFPE,  hand);
97                         signal(SIGSEGV, hand);
98                         signal(SIGBUS,  hand);
99                         signal(SIGURG,  hand);
100                         signal(SIGSYS,  hand);
101                         signal(SIGTRAP, hand);
102 #endif
103                         alarm(2);
104
105                         (*sub)();
106
107                         exit(EXIT_SUCCESS);
108
109                 } else if (pid > 0) {
110                         signal(SIGALRM, hand);
111                         alarm(3);
112                         if (waitpid(pid, &status, 0) == -1)
113                                 warn("waitpid(%d)", pid);
114                         alarm(0);
115                         kill(pid, SIGINT);
116                 } else
117                         err(1, "fork(), %s:%d",  __FILE__, __LINE__);
118         }
119
120         return (0);
121 }