OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import re |
| 7 |
| 8 import utils |
| 9 |
| 10 |
| 11 VALIDATORS = ['nc', 'dfa'] |
| 12 |
| 13 |
| 14 ncval32 = None |
| 15 ncval64 = None |
| 16 rdfaval = None |
| 17 |
| 18 |
| 19 def AddValidatorsOptions(option_parser): |
| 20 option_parser.add_option('--ncval32', dest='ncval32', type='string') |
| 21 option_parser.add_option('--ncval64', dest='ncval64', type='string') |
| 22 option_parser.add_option('--rdfaval', dest='rdfaval', type='string') |
| 23 |
| 24 |
| 25 def ProcessValidatorsOptions(options): |
| 26 global ncval32 |
| 27 global ncval64 |
| 28 global rdfaval |
| 29 |
| 30 if options.ncval32 is not None: |
| 31 ncval32 = options.ncval32 |
| 32 |
| 33 if options.ncval64 is not None: |
| 34 ncval64 = options.ncval64 |
| 35 |
| 36 if options.rdfaval is not None: |
| 37 rdfaval = options.rdfaval |
| 38 |
| 39 |
| 40 def SplitToLines(content): |
| 41 return [line.rstrip() for line in content.rstrip().split('\n')] |
| 42 |
| 43 |
| 44 def RunNcVal32(hex_name): |
| 45 args = [ |
| 46 '--hex_text=-', |
| 47 '--max_errors=-1', |
| 48 '--detailed=false', |
| 49 '--cpuid-all' |
| 50 ] |
| 51 |
| 52 with open(hex_name) as input: |
| 53 result = utils.CheckOutput([ncval32] + args, stdin=input) |
| 54 |
| 55 return SplitToLines(result) |
| 56 |
| 57 |
| 58 def RunNcVal64(hex_name): |
| 59 args = [ |
| 60 '--hex_text=-', |
| 61 '--max_errors=-1', |
| 62 '--readwrite_sfi', |
| 63 '--annotate=false', |
| 64 '--cpuid-all', |
| 65 '--detailed=false', |
| 66 ] |
| 67 |
| 68 with open(hex_name) as input: |
| 69 result = utils.CheckOutput([ncval64] + args, stdin=input) |
| 70 |
| 71 return SplitToLines(result) |
| 72 |
| 73 |
| 74 def ParseNcValVerdict(last_line): |
| 75 m = re.match(r'\*\*\* <input> (is safe|IS UNSAFE) \*\*\*$', last_line) |
| 76 assert m is not None, 'unexpected ncval output "%s"' % last_line |
| 77 return m.group(1) == 'is safe' |
| 78 |
| 79 |
| 80 def ParseNcVal32(lines): |
| 81 assert len(lines) > 0, 'ncval output is empty' |
| 82 |
| 83 errors = [] |
| 84 for line in lines[:-1]: |
| 85 line = line.strip() |
| 86 if line == '': |
| 87 continue |
| 88 |
| 89 if re.match(r'.+ > .+ \(read overflow of .+ bytes\)', line): |
| 90 errors.append((0, line)) |
| 91 continue |
| 92 if line == 'ErrorSegmentation': |
| 93 errors.append((0, line)) |
| 94 continue |
| 95 |
| 96 # Parse error message of the form |
| 97 # VALIDATOR: 4: Bad prefix usage |
| 98 m = re.match(r'VALIDATOR: ([0-9a-f]+): (.*)$', line, re.IGNORECASE) |
| 99 |
| 100 assert m is not None, "can't parse '%s'" % line |
| 101 |
| 102 offset = int(m.group(1), 16) |
| 103 message = m.group(2) |
| 104 errors.append((offset, message)) |
| 105 safe = ParseNcValVerdict(lines[-1]) |
| 106 return safe, errors |
| 107 |
| 108 |
| 109 def ParseNcVal64(lines): |
| 110 assert len(lines) > 0, 'ncval output is empty' |
| 111 |
| 112 errors = [] |
| 113 for i, line in enumerate(lines[:-1]): |
| 114 if line.startswith('VALIDATOR: Checking jump targets:'): |
| 115 continue |
| 116 if line.startswith('VALIDATOR: Checking that basic blocks are aligned'): |
| 117 continue |
| 118 |
| 119 # Skip disassembler output of the form |
| 120 # VALIDATOR: 0000000000000003: 49 89 14 07 mov [%r15+%rax*1], %rdx |
| 121 m = re.match(r'VALIDATOR: ([0-9a-f]+):', line, re.IGNORECASE) |
| 122 if m is not None: |
| 123 continue |
| 124 |
| 125 # Parse error message of the form |
| 126 # VALIDATOR: ERROR: 20: Bad basic block alignment. |
| 127 m = re.match(r'VALIDATOR: ERROR: ([0-9a-f]+): (.*)', line, re.IGNORECASE) |
| 128 if m is not None: |
| 129 offset = int(m.group(1), 16) |
| 130 errors.append((offset, m.group(2))) |
| 131 continue |
| 132 |
| 133 # Parse two-line error messages of the form |
| 134 # VALIDATOR: 0000000000000003: 49 89 14 07 mov [%r15+%rax*1], %rdx |
| 135 # VALIDATOR: ERROR: Invalid index register in memory offset |
| 136 m = re.match(r'VALIDATOR: ((?:ERROR|WARNING): .*)$', line, re.IGNORECASE) |
| 137 if m is not None: |
| 138 m2 = re.match(r'VALIDATOR: ([0-9a-f]+):', lines[i-1], re.IGNORECASE) |
| 139 assert m2 is not None, "can't parse line '%s' preceding line '%s'" % ( |
| 140 lines[i-1], |
| 141 line |
| 142 ) |
| 143 offset = int(m2.group(1), 16) |
| 144 errors.append((offset, m.group(1))) |
| 145 continue |
| 146 |
| 147 raise AssertionError("can't parse line '%s'" % line) |
| 148 |
| 149 safe = ParseNcValVerdict(lines[-1]) |
| 150 return safe, errors |
| 151 |
| 152 |
| 153 ValidationResults = collections.namedtuple('ValidationResults', 'safe, errors') |
| 154 |
| 155 |
| 156 def RunValidator(validator, bits, data): |
| 157 assert validator in VALIDATORS |
| 158 assert bits in [32, 64] |
| 159 assert len(data) % 32 == 0 |
| 160 |
| 161 if validator == 'nc': |
| 162 with utils.TempFile(mode='w') as hex_file: |
| 163 hex_file.write('%s\n' % utils.DataToReadableHex(data)) |
| 164 hex_file.flush() |
| 165 |
| 166 if bits == 32: |
| 167 safe, errors = ParseNcVal32(RunNcVal32(hex_file.name)) |
| 168 elif bits == 64: |
| 169 safe, errors = ParseNcVal64(RunNcVal64(hex_file.name)) |
| 170 |
| 171 elif validator == 'dfa': |
| 172 raise NotImplementedError('Code for running rdfa is temporarily removed') |
| 173 |
| 174 return ValidationResults(safe=safe, errors=errors) |
OLD | NEW |