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_ERR "WARNING: kmemcheck: Caught %d-bit read "
  91. "from %s memory (%p)\n",
  92. 8 * e->size, e->state < ARRAY_SIZE(desc) ?
  93. desc[e->state] : "(invalid shadow state)",
  94. (void *) e->address);
  95. printk(KERN_INFO);
  96. for (i = 0; i < SHADOW_COPY_SIZE; ++i)
  97. printk("%02x", e->memory_copy[i]);
  98. printk("\n");
  99. printk(KERN_INFO);
  100. for (i = 0; i < SHADOW_COPY_SIZE; ++i) {
  101. if (e->shadow_copy[i] < ARRAY_SIZE(short_desc))
  102. printk(" %c", short_desc[e->shadow_copy[i]]);
  103. else
  104. printk(" ?");
  105. }
  106. printk("\n");
  107. printk(KERN_INFO "%*c\n", 2 + 2
  108. * (int) (e->address & (SHADOW_COPY_SIZE - 1)), '^');
  109. break;
  110. case KMEMCHECK_ERROR_BUG:
  111. printk(KERN_EMERG "ERROR: kmemcheck: Fatal error\n");
  112. break;
  113. }
  114. __show_regs(&e->regs, 1);
  115. print_stack_trace(&e->trace, 0);
  116. }
  117. static void do_wakeup(unsigned long data)
  118. {
  119. while (error_count > 0)
  120. kmemcheck_error_recall();
  121. if (error_missed_count > 0) {
  122. printk(KERN_WARNING "kmemcheck: Lost %d error reports because "
  123. "the queue was too small\n", error_missed_count);
  124. error_missed_count = 0;
  125. }
  126. }
  127. static DECLARE_TASKLET(kmemcheck_tasklet, &do_wakeup, 0);
  128. /*
  129. * Save the context of an error report.
  130. */
  131. void kmemcheck_error_save(enum kmemcheck_shadow state,
  132. unsigned long address, unsigned int size, struct pt_regs *regs)
  133. {
  134. static unsigned long prev_ip;
  135. struct kmemcheck_error *e;
  136. void *shadow_copy;
  137. void *memory_copy;
  138. /* Don't report several adjacent errors from the same EIP. */
  139. if (regs->ip == prev_ip)
  140. return;
  141. prev_ip = regs->ip;
  142. e = error_next_wr();
  143. if (!e)
  144. return;
  145. e->type = KMEMCHECK_ERROR_INVALID_ACCESS;
  146. e->state = state;
  147. e->address = address;
  148. e->size = size;
  149. /* Save regs */
  150. memcpy(&e->regs, regs, sizeof(*regs));
  151. /* Save stack trace */
  152. e->trace.nr_entries = 0;
  153. e->trace.entries = e->trace_entries;
  154. e->trace.max_entries = ARRAY_SIZE(e->trace_entries);
  155. e->trace.skip = 0;
  156. save_stack_trace_bp(&e->trace, regs->bp);
  157. /* Round address down to nearest 16 bytes */
  158. shadow_copy = kmemcheck_shadow_lookup(address
  159. & ~(SHADOW_COPY_SIZE - 1));
  160. BUG_ON(!shadow_copy);
  161. memcpy(e->shadow_copy, shadow_copy, SHADOW_COPY_SIZE);
  162. kmemcheck_show_addr(address);
  163. memory_copy = (void *) (address & ~(SHADOW_COPY_SIZE - 1));
  164. memcpy(e->memory_copy, memory_copy, SHADOW_COPY_SIZE);
  165. kmemcheck_hide_addr(address);
  166. tasklet_hi_schedule_first(&kmemcheck_tasklet);
  167. }
  168. /*
  169. * Save the context of a kmemcheck bug.
  170. */
  171. void kmemcheck_error_save_bug(struct pt_regs *regs)
  172. {
  173. struct kmemcheck_error *e;
  174. e = error_next_wr();
  175. if (!e)
  176. return;
  177. e->type = KMEMCHECK_ERROR_BUG;
  178. memcpy(&e->regs, regs, sizeof(*regs));
  179. e->trace.nr_entries = 0;
  180. e->trace.entries = e->trace_entries;
  181. e->trace.max_entries = ARRAY_SIZE(e->trace_entries);
  182. e->trace.skip = 1;
  183. save_stack_trace(&e->trace);
  184. tasklet_hi_schedule_first(&kmemcheck_tasklet);
  185. }