OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/env python |
| 2 # Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Script for updating AFL. Also updates AFL version in README.chromium. |
| 7 """ |
| 8 |
| 9 import argparse |
| 10 import cStringIO |
| 11 import datetime |
| 12 import os |
| 13 import re |
| 14 import sys |
| 15 import tarfile |
| 16 import urllib2 |
| 17 |
| 18 |
| 19 VERSION_REGEX = r'(?P<version>([0-9]*[.])?[0-9]+b)' |
| 20 PATH_REGEX = r'(afl-)' + VERSION_REGEX |
| 21 |
| 22 |
| 23 class ChromiumReadme(object): |
| 24 """Class that handles reading from and updating the README.chromium""" |
| 25 |
| 26 README_FILE_PATH = 'third_party/afl/README.chromium' |
| 27 README_VERSION_REGEX = r'Version: ' + VERSION_REGEX |
| 28 |
| 29 def __init__(self): |
| 30 """ |
| 31 Inits the ChromiumReadme. |
| 32 """ |
| 33 with open(self.README_FILE_PATH) as readme_file_handle: |
| 34 self.readme_contents = readme_file_handle.read() |
| 35 |
| 36 def get_current_version(self): |
| 37 """ |
| 38 Get the current version of AFL according to the README.chromium |
| 39 """ |
| 40 match = re.search(self.README_VERSION_REGEX, self.readme_contents) |
| 41 if not match: |
| 42 raise Exception('Could not determine current AFL version') |
| 43 return match.groupdict()['version'] |
| 44 |
| 45 def update(self, new_version): |
| 46 """ |
| 47 Update the readme to reflect the new version that has been downloaded. |
| 48 """ |
| 49 new_readme = self.readme_contents |
| 50 subsitutions = [(VERSION_REGEX, new_version), # Update the version. |
| 51 (r'Date: .*', |
| 52 'Date: ' + datetime.date.today().strftime("%B %d, %Y")), |
| 53 # Update the Local Modifications. |
| 54 (PATH_REGEX + r'/', 'afl-' + new_version + '/')] |
| 55 |
| 56 for regex, replacement in subsitutions: |
| 57 new_readme = re.subn(regex, replacement, new_readme, 1)[0] |
| 58 |
| 59 self.readme_contents = new_readme |
| 60 with open(self.README_FILE_PATH, 'w+') as readme_file_handle: |
| 61 readme_file_handle.write(self.readme_contents) |
| 62 |
| 63 |
| 64 class AflTarball(object): |
| 65 """ |
| 66 Class that handles the afl-latest.tgz tarball. |
| 67 """ |
| 68 # Regexes that match files that we don't want to extract. |
| 69 # Note that you should add these removals to "Local Modifications" in |
| 70 # the README.chromium. |
| 71 UNWANTED_FILE_REGEX = '|'.join([ |
| 72 r'(.*\.elf)', # presubmit complains these aren't marked executable. |
| 73 r'(.*others/elf)', # We don't need this if we have no elfs. |
| 74 # checkdeps complains about #includes. |
| 75 r'(.*afl-llvm-pass\.so\.cc)', |
| 76 r'(.*argv.*)', # Delete the demo's directory as well. |
| 77 |
| 78 r'(.*dictionaries.*)', # Including these make builds fail. |
| 79 ]) |
| 80 AFL_SRC_DIR = 'third_party/afl/src' |
| 81 |
| 82 def __init__(self, version): |
| 83 """ |
| 84 Init this AFL tarball. |
| 85 """ |
| 86 release_name = 'afl-{0}'.format(version) |
| 87 filename = '{0}.tgz'.format(release_name) |
| 88 |
| 89 # Note: lcamtuf.coredump.cx does not support TLS connections. The "http://" |
| 90 # protocol is intentional. |
| 91 self.url = "http://lcamtuf.coredump.cx/afl/releases/{0}".format(filename) |
| 92 self.tarball = None |
| 93 self.real_version = version if version != 'latest' else None |
| 94 |
| 95 def download(self): |
| 96 """Download the tarball version from |
| 97 http://lcamtuf.coredump.cx/afl/releases/ |
| 98 """ |
| 99 tarball_contents = urllib2.urlopen(self.url).read() |
| 100 tarball_file = cStringIO.StringIO(tarball_contents) |
| 101 self.tarball = tarfile.open(fileobj=tarball_file, mode="r:gz") |
| 102 if self.real_version is None: |
| 103 regex_match = re.search(VERSION_REGEX, self.tarball.members[0].path) |
| 104 self.real_version = regex_match.groupdict()['version'] |
| 105 |
| 106 def extract(self): |
| 107 """ |
| 108 Extract the files and folders from the tarball we have downloaded while |
| 109 skipping unwanted ones. |
| 110 """ |
| 111 |
| 112 for member in self.tarball.getmembers(): |
| 113 member.path = re.sub(PATH_REGEX, self.AFL_SRC_DIR, member.path) |
| 114 if re.match(self.UNWANTED_FILE_REGEX, member.path): |
| 115 print 'skipping unwanted file: {0}'.format(member.path) |
| 116 continue |
| 117 self.tarball.extract(member) |
| 118 |
| 119 |
| 120 def version_to_float(version): |
| 121 """ |
| 122 Convert version string to float. |
| 123 """ |
| 124 if version.endswith('b'): |
| 125 return float(version[:-1]) |
| 126 |
| 127 return float(version) |
| 128 |
| 129 |
| 130 def update_afl(new_version): |
| 131 """ |
| 132 Update this version of AFL to newer version, new_version. |
| 133 """ |
| 134 readme = ChromiumReadme() |
| 135 old_version = readme.get_current_version() |
| 136 if new_version != 'latest': |
| 137 new_float = version_to_float(new_version) |
| 138 assert version_to_float(old_version) < new_float, ( |
| 139 'Trying to update from version {0} to {1}'.format(old_version, |
| 140 new_version)) |
| 141 |
| 142 # Extract the tarball. |
| 143 tarball = AflTarball(new_version) |
| 144 tarball.download() |
| 145 current_less_than_new = version_to_float(old_version) < version_to_float( |
| 146 tarball.real_version) |
| 147 assert current_less_than_new, ( |
| 148 'Trying to update from version {0} to {1}'.format(old_version, |
| 149 tarball.real_version)) |
| 150 |
| 151 tarball.extract() |
| 152 |
| 153 readme.update(tarball.real_version) |
| 154 |
| 155 |
| 156 def main(): |
| 157 """ |
| 158 Update AFL if possible. |
| 159 """ |
| 160 parser = argparse.ArgumentParser('Update AFL.') |
| 161 parser.add_argument('version', metavar='version', default='latest', nargs='?', |
| 162 help='(optional) Version to update AFL to.') |
| 163 |
| 164 args = parser.parse_args() |
| 165 version = args.version |
| 166 if version != 'latest' and not version.endswith('b'): |
| 167 version += 'b' |
| 168 |
| 169 in_correct_directory = (os.path.basename(os.getcwd()) == 'src' and |
| 170 os.path.exists('third_party')) |
| 171 |
| 172 assert in_correct_directory, ( |
| 173 '{0} must be run from the repo\'s root'.format(sys.argv[0])) |
| 174 |
| 175 update_afl(version) |
| 176 print ("Run git diff third_party/afl/src/docs/ChangeLog to see changes to AFL" |
| 177 " since the last roll") |
| 178 |
| 179 |
| 180 if __name__ == '__main__': |
| 181 main() |
OLD | NEW |