error.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <linux/interrupt.h>
  2. #include <linux/kdebug.h>
  3. #include <linux/kmemcheck.h>
  4. #include <linux/kernel.h>
  5. #include <linux/types.h>
  6. #include <linux/ptrace.h>
  7. #include <linux/stacktrace.h>
  8. #include <linux/string.h>
  9. #include "error.h"
  10. #include "shadow.h"
  11. enum kmemcheck_error_type {
  12. KMEMCHECK_ERROR_INVALID_ACCESS,
  13. KMEMCHECK_ERROR_BUG,
  14. };
  15. #define SHADOW_COPY_SIZE (1 << CONFIG_KMEMCHECK_SHADOW_COPY_SHIFT)
  16. struct kmemcheck_error {
  17. enum kmemcheck_error_type type;
  18. union {
  19. /* KMEMCHECK_ERROR_INVALID_ACCESS */
  20. struct {
  21. /* Kind of access that caused the error */
  22. enum kmemcheck_shadow state;
  23. /* Address and size of the erroneous read */
  24. unsigned long address;
  25. unsigned int size;
  26. };
  27. };
  28. struct pt_regs regs;
  29. struct stack_trace trace;
  30. unsigned long trace_entries[32];
  31. /* We compress it to a char. */
  32. unsigned char shadow_copy[SHADOW_COPY_SIZE];
  33. unsigned char memory_copy[SHADOW_COPY_SIZE];
  34. };
  35. /*
  36. * Create a ring queue of errors to output. We can't call printk() directly
  37. * from the kmemcheck traps, since this may call the console drivers and
  38. * result in a recursive fault.
  39. */
  40. static struct kmemcheck_error error_fifo[CONFIG_KMEMCHECK_QUEUE_SIZE];
  41. static unsigned int error_count;
  42. static unsigned int error_rd;
  43. static unsigned int error_wr;
  44. static unsigned int error_missed_count;
  45. static struct kmemcheck_error *error_next_wr(void)
  46. {
  47. struct kmemcheck_error *e;
  48. if (error_count == ARRAY_SIZE(error_fifo)) {
  49. ++error_missed_count;
  50. return NULL;
  51. }
  52. e = &error_fifo[error_wr];
  53. if (++error_wr == ARRAY_SIZE(error_fifo))
  54. error_wr = 0;
  55. ++error_count;
  56. return e;
  57. }
  58. static struct kmemcheck_error *error_next_rd(void)
  59. {
  60. struct kmemcheck_error *e;
  61. if (error_count == 0)
  62. return NULL;
  63. e = &error_fifo[error_rd];
  64. if (++error_rd == ARRAY_SIZE(error_fifo))
  65. error_rd = 0;
  66. --error_count;
  67. return e;
  68. }
  69. void kmemcheck_error_recall(void)
  70. {
  71. static const char *desc[] = {
  72. [KMEMCHECK_SHADOW_UNALLOCATED] = "unallocated",
  73. [KMEMCHECK_SHADOW_UNINITIALIZED] = "uninitialized",
  74. [KMEMCHECK_SHADOW_INITIALIZED] = "initialized",
  75. [KMEMCHECK_SHADOW_FREED] = "freed",
  76. };
  77. static const char short_desc[] = {
  78. [KMEMCHECK_SHADOW_UNALLOCATED] = 'a',
  79. [KMEMCHECK_SHADOW_UNINITIALIZED] = 'u',
  80. [KMEMCHECK_SHADOW_INITIALIZED] = 'i',
  81. [KMEMCHECK_SHADOW_FREED] = 'f',
  82. };
  83. struct kmemcheck_error *e;
  84. unsigned int i;
  85. e = error_next_rd();
  86. if (!e)
  87. return;
  88. switch (e->type) {
  89. case KMEMCHECK_ERROR_INVALID_ACCESS:
  90. printk(KERN_WARNING "WARNING: kmemcheck: Caught %d-bit read from %s memory (%p)\n",
  91. 8 * e->size, e->state < ARRAY_SIZE(desc) ?
  92. desc[e->state] : "(invalid shadow state)",
  93. (void *) e->address);
  94. printk(KERN_WARNING);
  95. for (i = 0; i < SHADOW_COPY_SIZE; ++i)
  96. printk(KERN_CONT "%02x", e->memory_copy[i]);
  97. printk(KERN_CONT "\n");
  98. printk(KERN_WARNING);
  99. for (i = 0; i < SHADOW_COPY_SIZE; ++i) {
  100. if (e->shadow_copy[i] < ARRAY_SIZE(short_desc))
  101. printk(KERN_CONT " %c", short_desc[e->shadow_copy[i]]);
  102. else
  103. printk(KERN_CONT " ?");
  104. }
  105. printk(KERN_CONT "\n");
  106. printk(KERN_WARNING "%*c\n", 2 + 2
  107. * (int) (e->address & (SHADOW_COPY_SIZE - 1)), '^');
  108. break;
  109. case KMEMCHECK_ERROR_BUG:
  110. printk(KERN_EMERG "ERROR: kmemcheck: Fatal error\n");
  111. break;
  112. }
  113. __show_regs(&e->regs, 1);
  114. print_stack_trace(&e->trace, 0);
  115. }
  116. static void do_wakeup(unsigned long data)
  117. {
  118. while (error_count > 0)
  119. kmemcheck_error_recall();
  120. if (error_missed_count > 0) {
  121. printk(KERN_WARNING "kmemcheck: Lost %d error reports because "
  122. "the queue was too small\n", error_missed_count);
  123. error_missed_count = 0;
  124. }
  125. }
  126. static DECLARE_TASKLET(kmemcheck_tasklet, &do_wakeup, 0);
  127. /*
  128. * Save the context of an error report.
  129. */
  130. void kmemcheck_error_save(enum kmemcheck_shadow state,
  131. unsigned long address, unsigned int size, struct pt_regs *regs)
  132. {
  133. static unsigned long prev_ip;
  134. struct kmemcheck_error *e;
  135. void *shadow_copy;
  136. void *memory_copy;
  137. /* Don't report several adjacent errors from the same EIP. */
  138. if (regs->ip == prev_ip)
  139. return;
  140. prev_ip = regs->ip;
  141. e = error_next_wr();
  142. if (!e)
  143. return;
  144. e->type = KMEMCHECK_ERROR_INVALID_ACCESS;
  145. e->state = state;
  146. e->address = address;
  147. e->size = size;
  148. /* Save regs */
  149. memcpy(&e->regs, regs, sizeof(*regs));
  150. /* Save stack trace */
  151. e->trace.nr_entries = 0;
  152. e->trace.entries = e->trace_entries;
  153. e->trace.max_entries = ARRAY_SIZE(e->trace_entries);
  154. e->trace.skip = 0;
  155. save_stack_trace_regs(&e->trace, regs);
  156. /* Round address down to nearest 16 bytes */
  157. shadow_copy = kmemcheck_shadow_lookup(address
  158. & ~(SHADOW_COPY_SIZE - 1));
  159. BUG_ON(!shadow_copy);
  160. memcpy(e->shadow_copy, shadow_copy, SHADOW_COPY_SIZE);
  161. kmemcheck_show_addr(address);
  162. memory_copy = (void *) (address & ~(SHADOW_COPY_SIZE - 1));
  163. memcpy(e->memory_copy, memory_copy, SHADOW_COPY_SIZE);
  164. kmemcheck_hide_addr(address);
  165. tasklet_hi_schedule_first(&kmemcheck_tasklet);
  166. }
  167. /*
  168. * Save the context of a kmemcheck bug.
  169. */
  170. void kmemcheck_error_save_bug(struct pt_regs *regs)
  171. {
  172. struct kmemcheck_error *e;
  173. e = error_next_wr();
  174. if (!e)
  175. return;
  176. e->type = KMEMCHECK_ERROR_BUG;
  177. memcpy(&e->regs, regs, sizeof(*regs));
  178. e->trace.nr_entries = 0;
  179. e->trace.entries = e->trace_entries;
  180. e->trace.max_entries = ARRAY_SIZE(e->trace_entries);
  181. e->trace.skip = 1;
  182. save_stack_trace(&e->trace);
  183. tasklet_hi_schedule_first(&kmemcheck_tasklet);
  184. }