output_handler.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from __future__ import print_function, unicode_literals
  5. import logging
  6. import re
  7. class OutputHandler(object):
  8. '''
  9. A class for handling Valgrind output.
  10. Valgrind errors look like this:
  11. ==60741== 40 (24 direct, 16 indirect) bytes in 1 blocks are definitely lost in loss record 2,746 of 5,235
  12. ==60741== at 0x4C26B43: calloc (vg_replace_malloc.c:593)
  13. ==60741== by 0x63AEF65: PR_Calloc (prmem.c:443)
  14. ==60741== by 0x69F236E: PORT_ZAlloc_Util (secport.c:117)
  15. ==60741== by 0x69F1336: SECITEM_AllocItem_Util (secitem.c:28)
  16. ==60741== by 0xA04280B: ffi_call_unix64 (in /builds/slave/m-in-l64-valgrind-000000000000/objdir/toolkit/library/libxul.so)
  17. ==60741== by 0xA042443: ffi_call (ffi64.c:485)
  18. For each such error, this class extracts most or all of the first (error
  19. kind) line, plus the function name in each of the first few stack entries.
  20. With this data it constructs and prints a TEST-UNEXPECTED-FAIL message that
  21. TBPL will highlight.
  22. It buffers these lines from which text is extracted so that the
  23. TEST-UNEXPECTED-FAIL message can be printed before the full error.
  24. Parsing the Valgrind output isn't ideal, and it may break in the future if
  25. Valgrind changes the format of the messages, or introduces new error kinds.
  26. To protect against this, we also count how many lines containing
  27. "<insert_a_suppression_name_here>" are seen. Thanks to the use of
  28. --gen-suppressions=yes, exactly one of these lines is present per error. If
  29. the count of these lines doesn't match the error count found during
  30. parsing, then the parsing has missed one or more errors and we can fail
  31. appropriately.
  32. '''
  33. def __init__(self, logger):
  34. # The regexps in this list match all of Valgrind's errors. Note that
  35. # Valgrind is English-only, so we don't have to worry about
  36. # localization.
  37. self.logger = logger
  38. self.re_error = \
  39. r'==\d+== (' + \
  40. r'(Use of uninitialised value of size \d+)|' + \
  41. r'(Conditional jump or move depends on uninitialised value\(s\))|' + \
  42. r'(Syscall param .* contains uninitialised byte\(s\))|' + \
  43. r'(Syscall param .* points to (unaddressable|uninitialised) byte\(s\))|' + \
  44. r'((Unaddressable|Uninitialised) byte\(s\) found during client check request)|' + \
  45. r'(Invalid free\(\) / delete / delete\[\] / realloc\(\))|' + \
  46. r'(Mismatched free\(\) / delete / delete \[\])|' + \
  47. r'(Invalid (read|write) of size \d+)|' + \
  48. r'(Jump to the invalid address stated on the next line)|' + \
  49. r'(Source and destination overlap in .*)|' + \
  50. r'(.* bytes in .* blocks are .* lost)' + \
  51. r')'
  52. # Match identifer chars, plus ':' for namespaces, and '\?' in order to
  53. # match "???" which Valgrind sometimes produces.
  54. self.re_stack_entry = r'^==\d+==.*0x[A-Z0-9]+: ([A-Za-z0-9_:\?]+)'
  55. self.re_suppression = r' *<insert_a_suppression_name_here>'
  56. self.error_count = 0
  57. self.suppression_count = 0
  58. self.number_of_stack_entries_to_get = 0
  59. self.curr_error = None
  60. self.curr_location = None
  61. self.buffered_lines = None
  62. def log(self, line):
  63. self.logger(logging.INFO, 'valgrind-output', {'line': line}, '{line}')
  64. def __call__(self, line):
  65. if self.number_of_stack_entries_to_get == 0:
  66. # Look for the start of a Valgrind error.
  67. m = re.search(self.re_error, line)
  68. if m:
  69. self.error_count += 1
  70. self.number_of_stack_entries_to_get = 4
  71. self.curr_error = m.group(1)
  72. self.curr_location = ""
  73. self.buffered_lines = [line]
  74. else:
  75. self.log(line)
  76. else:
  77. # We've recently found a Valgrind error, and are now extracting
  78. # details from the first few stack entries.
  79. self.buffered_lines.append(line)
  80. m = re.match(self.re_stack_entry, line)
  81. if m:
  82. self.curr_location += m.group(1)
  83. else:
  84. self.curr_location += '?!?'
  85. self.number_of_stack_entries_to_get -= 1
  86. if self.number_of_stack_entries_to_get != 0:
  87. self.curr_location += ' / '
  88. else:
  89. # We've finished getting the first few stack entries. Print the
  90. # failure message and the buffered lines, and then reset state.
  91. self.logger(logging.ERROR, 'valgrind-error-msg',
  92. {'error': self.curr_error,
  93. 'location': self.curr_location},
  94. 'TEST-UNEXPECTED-FAIL | valgrind-test | {error} at {location}')
  95. for b in self.buffered_lines:
  96. self.log(b)
  97. self.curr_error = None
  98. self.curr_location = None
  99. self.buffered_lines = None
  100. if re.match(self.re_suppression, line):
  101. self.suppression_count += 1