crash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * crash.c
  31. *
  32. * Commit a variety of exceptions, primarily address faults.
  33. *
  34. * Once the basic system calls assignment is complete, none of these
  35. * should crash the kernel.
  36. *
  37. * They should all, however, terminate this program, except for the
  38. * one that writes to the code segment. (That one won't cause program
  39. * termination until/unless you implement read-only segments in your
  40. * VM system.)
  41. */
  42. #include <stdio.h>
  43. #include <stdint.h>
  44. #include <unistd.h>
  45. #include <err.h>
  46. #if defined(__mips__)
  47. #define KERNEL_ADDR 0x80000000
  48. #define INVAL_ADDR 0x40000000
  49. #define INSN_TYPE uint32_t
  50. #define INVAL_INSN 0x0000003f
  51. #else
  52. #error "Please fix this"
  53. #endif
  54. typedef void (*func)(void);
  55. static
  56. void
  57. read_from_null(void)
  58. {
  59. int *null = NULL;
  60. volatile int x;
  61. x = *null;
  62. }
  63. static
  64. void
  65. read_from_inval(void)
  66. {
  67. int *ptr = (int *) INVAL_ADDR;
  68. volatile int x;
  69. x = *ptr;
  70. }
  71. static
  72. void
  73. read_from_kernel(void)
  74. {
  75. int *ptr = (int *) KERNEL_ADDR;
  76. volatile int x;
  77. x = *ptr;
  78. }
  79. static
  80. void
  81. write_to_null(void)
  82. {
  83. int *null = NULL;
  84. *null = 6;
  85. }
  86. static
  87. void
  88. write_to_inval(void)
  89. {
  90. int *ptr = (int *) INVAL_ADDR;
  91. *ptr = 8;
  92. }
  93. static
  94. void
  95. write_to_code(void)
  96. {
  97. INSN_TYPE *x = (INSN_TYPE *)write_to_code;
  98. *x = INVAL_INSN;
  99. }
  100. static
  101. void
  102. write_to_kernel(void)
  103. {
  104. int *ptr = (int *) KERNEL_ADDR;
  105. *ptr = 8;
  106. }
  107. static
  108. void
  109. jump_to_null(void)
  110. {
  111. func f = NULL;
  112. f();
  113. }
  114. static
  115. void
  116. jump_to_inval(void)
  117. {
  118. func f = (func) INVAL_ADDR;
  119. f();
  120. }
  121. static
  122. void
  123. jump_to_kernel(void)
  124. {
  125. func f = (func) KERNEL_ADDR;
  126. f();
  127. }
  128. static
  129. void
  130. illegal_instruction(void)
  131. {
  132. #if defined(__mips__)
  133. asm(".long 0x0000003f");
  134. #else
  135. #error "Please fix this"
  136. #endif
  137. }
  138. static
  139. void
  140. alignment_error(void)
  141. {
  142. int x;
  143. int *ptr = &x;
  144. int *badptr = (int *)(((char *)ptr)+1);
  145. volatile int j;
  146. j = *badptr;
  147. }
  148. static
  149. void
  150. divide_by_zero(void)
  151. {
  152. volatile int x = 6;
  153. volatile int z = 0;
  154. volatile int a;
  155. a = x/z;
  156. }
  157. static
  158. void
  159. mod_by_zero(void)
  160. {
  161. volatile int x = 6;
  162. volatile int z = 0;
  163. volatile int a;
  164. a = x%z;
  165. }
  166. static
  167. void
  168. recurse_inf(void)
  169. {
  170. volatile char buf[16];
  171. buf[0] = 0;
  172. recurse_inf();
  173. buf[0] = 1;
  174. }
  175. static
  176. struct {
  177. int ch;
  178. const char *name;
  179. func f;
  180. } ops[] = {
  181. { 'a', "read from NULL", read_from_null },
  182. { 'b', "read from invalid address", read_from_inval },
  183. { 'c', "read from kernel address", read_from_kernel },
  184. { 'd', "write to NULL", write_to_null },
  185. { 'e', "write to invalid address", write_to_inval },
  186. { 'f', "write to code segment", write_to_code },
  187. { 'g', "write to kernel address", write_to_kernel },
  188. { 'h', "jump to NULL", jump_to_null },
  189. { 'i', "jump to invalid address", jump_to_inval },
  190. { 'j', "jump to kernel address", jump_to_kernel },
  191. { 'k', "alignment error", alignment_error },
  192. { 'l', "illegal instruction", illegal_instruction },
  193. { 'm', "divide by zero", divide_by_zero },
  194. { 'n', "mod by zero", mod_by_zero },
  195. { 'o', "Recurse infinitely", recurse_inf },
  196. { 0, NULL, NULL }
  197. };
  198. int
  199. main(int argc, char **argv)
  200. {
  201. int op, i, status;
  202. pid_t pid;
  203. if (argc == 2) {
  204. op = argv[1][0];
  205. }
  206. else {
  207. for (i=0; ops[i].name; i++) {
  208. printf("[%c] %s\n", ops[i].ch, ops[i].name);
  209. }
  210. printf("[*] Run everything (in subprocesses)\n");
  211. printf("Note: [f] may not cause an exception on some "
  212. "platforms, in which\ncase it'll appear to fail.\n");
  213. printf("Choose: ");
  214. op = getchar();
  215. }
  216. if (op=='*') {
  217. for (i=0; ops[i].name; i++) {
  218. printf("Running: [%c] %s\n", ops[i].ch, ops[i].name);
  219. pid = fork();
  220. if (pid<0) {
  221. /* error */
  222. warn("fork");
  223. }
  224. else if (pid==0) {
  225. /* child */
  226. ops[i].f();
  227. printf("I wasn't killed - test fails!\n");
  228. _exit(1);
  229. }
  230. waitpid(pid, &status, 0);
  231. if (WIFSIGNALED(status)) {
  232. printf("Signal %d\n", WTERMSIG(status));
  233. }
  234. else {
  235. printf("Exit %d\n", WEXITSTATUS(status));
  236. }
  237. }
  238. }
  239. else {
  240. /* intentionally don't check if op is in bounds :) */
  241. ops[op-'a'].f();
  242. printf("I wasn't killed - test fails!\n");
  243. }
  244. return 0;
  245. }