Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | """ |
michael@0 | 6 | Classes in this file define additional actions that need to be taken to run a |
michael@0 | 7 | test under some kind of runtime error detection tool. |
michael@0 | 8 | |
michael@0 | 9 | The interface is intended to be used as follows. |
michael@0 | 10 | |
michael@0 | 11 | 1. For tests that simply run a native process (i.e. no activity is spawned): |
michael@0 | 12 | |
michael@0 | 13 | Call tool.CopyFiles(). |
michael@0 | 14 | Prepend test command line with tool.GetTestWrapper(). |
michael@0 | 15 | |
michael@0 | 16 | 2. For tests that spawn an activity: |
michael@0 | 17 | |
michael@0 | 18 | Call tool.CopyFiles(). |
michael@0 | 19 | Call tool.SetupEnvironment(). |
michael@0 | 20 | Run the test as usual. |
michael@0 | 21 | Call tool.CleanUpEnvironment(). |
michael@0 | 22 | """ |
michael@0 | 23 | |
michael@0 | 24 | import os.path |
michael@0 | 25 | import sys |
michael@0 | 26 | |
michael@0 | 27 | from constants import CHROME_DIR |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | def SetChromeTimeoutScale(adb, scale): |
michael@0 | 31 | """Sets the timeout scale in /data/local/tmp/chrome_timeout_scale to scale.""" |
michael@0 | 32 | path = '/data/local/tmp/chrome_timeout_scale' |
michael@0 | 33 | if not scale or scale == 1.0: |
michael@0 | 34 | # Delete if scale is None/0.0/1.0 since the default timeout scale is 1.0 |
michael@0 | 35 | adb.RunShellCommand('rm %s' % path) |
michael@0 | 36 | else: |
michael@0 | 37 | adb.SetFileContents(path, '%f' % scale) |
michael@0 | 38 | |
michael@0 | 39 | |
michael@0 | 40 | class BaseTool(object): |
michael@0 | 41 | """A tool that does nothing.""" |
michael@0 | 42 | |
michael@0 | 43 | def GetTestWrapper(self): |
michael@0 | 44 | """Returns a string that is to be prepended to the test command line.""" |
michael@0 | 45 | return '' |
michael@0 | 46 | |
michael@0 | 47 | def GetUtilWrapper(self): |
michael@0 | 48 | """Returns the wrapper name for the utilities. |
michael@0 | 49 | |
michael@0 | 50 | Returns: |
michael@0 | 51 | A string that is to be prepended to the command line of utility |
michael@0 | 52 | processes (forwarder, etc.). |
michael@0 | 53 | """ |
michael@0 | 54 | return '' |
michael@0 | 55 | |
michael@0 | 56 | def CopyFiles(self): |
michael@0 | 57 | """Copies tool-specific files to the device, create directories, etc.""" |
michael@0 | 58 | pass |
michael@0 | 59 | |
michael@0 | 60 | def SetupEnvironment(self): |
michael@0 | 61 | """Sets up the system environment for a test. |
michael@0 | 62 | |
michael@0 | 63 | This is a good place to set system properties. |
michael@0 | 64 | """ |
michael@0 | 65 | pass |
michael@0 | 66 | |
michael@0 | 67 | def CleanUpEnvironment(self): |
michael@0 | 68 | """Cleans up environment.""" |
michael@0 | 69 | pass |
michael@0 | 70 | |
michael@0 | 71 | def GetTimeoutScale(self): |
michael@0 | 72 | """Returns a multiplier that should be applied to timeout values.""" |
michael@0 | 73 | return 1.0 |
michael@0 | 74 | |
michael@0 | 75 | def NeedsDebugInfo(self): |
michael@0 | 76 | """Whether this tool requires debug info. |
michael@0 | 77 | |
michael@0 | 78 | Returns: |
michael@0 | 79 | True if this tool can not work with stripped binaries. |
michael@0 | 80 | """ |
michael@0 | 81 | return False |
michael@0 | 82 | |
michael@0 | 83 | |
michael@0 | 84 | class AddressSanitizerTool(BaseTool): |
michael@0 | 85 | """AddressSanitizer tool.""" |
michael@0 | 86 | |
michael@0 | 87 | WRAPPER_PATH = '/system/bin/asanwrapper' |
michael@0 | 88 | |
michael@0 | 89 | def __init__(self, adb): |
michael@0 | 90 | self._adb = adb |
michael@0 | 91 | self._wrap_properties = ['wrap.com.google.android.apps.ch', |
michael@0 | 92 | 'wrap.org.chromium.native_test'] |
michael@0 | 93 | |
michael@0 | 94 | def CopyFiles(self): |
michael@0 | 95 | """Copies ASan tools to the device.""" |
michael@0 | 96 | files = ['system/lib/libasan_preload.so', |
michael@0 | 97 | 'system/bin/asanwrapper', |
michael@0 | 98 | 'system/bin/asan/app_process', |
michael@0 | 99 | 'system/bin/linker'] |
michael@0 | 100 | android_product_out = os.environ['ANDROID_PRODUCT_OUT'] |
michael@0 | 101 | self._adb.MakeSystemFolderWritable() |
michael@0 | 102 | for f in files: |
michael@0 | 103 | self._adb.PushIfNeeded(os.path.join(android_product_out, f), |
michael@0 | 104 | os.path.join('/', f)) |
michael@0 | 105 | |
michael@0 | 106 | def GetTestWrapper(self): |
michael@0 | 107 | return AddressSanitizerTool.WRAPPER_PATH |
michael@0 | 108 | |
michael@0 | 109 | def GetUtilWrapper(self): |
michael@0 | 110 | """Returns the wrapper for utilities, such as forwarder. |
michael@0 | 111 | |
michael@0 | 112 | AddressSanitizer wrapper must be added to all instrumented binaries, |
michael@0 | 113 | including forwarder and the like. This can be removed if such binaries |
michael@0 | 114 | were built without instrumentation. """ |
michael@0 | 115 | return AddressSanitizerTool.WRAPPER_PATH |
michael@0 | 116 | |
michael@0 | 117 | def SetupEnvironment(self): |
michael@0 | 118 | for prop in self._wrap_properties: |
michael@0 | 119 | self._adb.RunShellCommand('setprop %s "logwrapper %s"' % ( |
michael@0 | 120 | prop, self.GetTestWrapper())) |
michael@0 | 121 | SetChromeTimeoutScale(self._adb, self.GetTimeoutScale()) |
michael@0 | 122 | |
michael@0 | 123 | def CleanUpEnvironment(self): |
michael@0 | 124 | for prop in self._wrap_properties: |
michael@0 | 125 | self._adb.RunShellCommand('setprop %s ""' % (prop,)) |
michael@0 | 126 | SetChromeTimeoutScale(self._adb, None) |
michael@0 | 127 | |
michael@0 | 128 | def GetTimeoutScale(self): |
michael@0 | 129 | # Very slow startup. |
michael@0 | 130 | return 20.0 |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | class ValgrindTool(BaseTool): |
michael@0 | 134 | """Base abstract class for Valgrind tools.""" |
michael@0 | 135 | |
michael@0 | 136 | VG_DIR = '/data/local/tmp/valgrind' |
michael@0 | 137 | VGLOGS_DIR = '/data/local/tmp/vglogs' |
michael@0 | 138 | |
michael@0 | 139 | def __init__(self, adb): |
michael@0 | 140 | self._adb = adb |
michael@0 | 141 | # exactly 31 chars, SystemProperties::PROP_NAME_MAX |
michael@0 | 142 | self._wrap_properties = ['wrap.com.google.android.apps.ch', |
michael@0 | 143 | 'wrap.org.chromium.native_test'] |
michael@0 | 144 | |
michael@0 | 145 | def CopyFiles(self): |
michael@0 | 146 | """Copies Valgrind tools to the device.""" |
michael@0 | 147 | self._adb.RunShellCommand('rm -r %s; mkdir %s' % |
michael@0 | 148 | (ValgrindTool.VG_DIR, ValgrindTool.VG_DIR)) |
michael@0 | 149 | self._adb.RunShellCommand('rm -r %s; mkdir %s' % |
michael@0 | 150 | (ValgrindTool.VGLOGS_DIR, |
michael@0 | 151 | ValgrindTool.VGLOGS_DIR)) |
michael@0 | 152 | files = self.GetFilesForTool() |
michael@0 | 153 | for f in files: |
michael@0 | 154 | self._adb.PushIfNeeded(os.path.join(CHROME_DIR, f), |
michael@0 | 155 | os.path.join(ValgrindTool.VG_DIR, |
michael@0 | 156 | os.path.basename(f))) |
michael@0 | 157 | |
michael@0 | 158 | def SetupEnvironment(self): |
michael@0 | 159 | """Sets up device environment.""" |
michael@0 | 160 | self._adb.RunShellCommand('chmod 777 /data/local/tmp') |
michael@0 | 161 | for prop in self._wrap_properties: |
michael@0 | 162 | self._adb.RunShellCommand('setprop %s "logwrapper %s"' % ( |
michael@0 | 163 | prop, self.GetTestWrapper())) |
michael@0 | 164 | SetChromeTimeoutScale(self._adb, self.GetTimeoutScale()) |
michael@0 | 165 | |
michael@0 | 166 | def CleanUpEnvironment(self): |
michael@0 | 167 | """Cleans up device environment.""" |
michael@0 | 168 | for prop in self._wrap_properties: |
michael@0 | 169 | self._adb.RunShellCommand('setprop %s ""' % (prop,)) |
michael@0 | 170 | SetChromeTimeoutScale(self._adb, None) |
michael@0 | 171 | |
michael@0 | 172 | def GetFilesForTool(self): |
michael@0 | 173 | """Returns a list of file names for the tool.""" |
michael@0 | 174 | raise NotImplementedError() |
michael@0 | 175 | |
michael@0 | 176 | def NeedsDebugInfo(self): |
michael@0 | 177 | """Whether this tool requires debug info. |
michael@0 | 178 | |
michael@0 | 179 | Returns: |
michael@0 | 180 | True if this tool can not work with stripped binaries. |
michael@0 | 181 | """ |
michael@0 | 182 | return True |
michael@0 | 183 | |
michael@0 | 184 | |
michael@0 | 185 | class MemcheckTool(ValgrindTool): |
michael@0 | 186 | """Memcheck tool.""" |
michael@0 | 187 | |
michael@0 | 188 | def __init__(self, adb): |
michael@0 | 189 | super(MemcheckTool, self).__init__(adb) |
michael@0 | 190 | |
michael@0 | 191 | def GetFilesForTool(self): |
michael@0 | 192 | """Returns a list of file names for the tool.""" |
michael@0 | 193 | return ['tools/valgrind/android/vg-chrome-wrapper.sh', |
michael@0 | 194 | 'tools/valgrind/memcheck/suppressions.txt', |
michael@0 | 195 | 'tools/valgrind/memcheck/suppressions_android.txt'] |
michael@0 | 196 | |
michael@0 | 197 | def GetTestWrapper(self): |
michael@0 | 198 | """Returns a string that is to be prepended to the test command line.""" |
michael@0 | 199 | return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper.sh' |
michael@0 | 200 | |
michael@0 | 201 | def GetTimeoutScale(self): |
michael@0 | 202 | """Returns a multiplier that should be applied to timeout values.""" |
michael@0 | 203 | return 30 |
michael@0 | 204 | |
michael@0 | 205 | |
michael@0 | 206 | class TSanTool(ValgrindTool): |
michael@0 | 207 | """ThreadSanitizer tool. See http://code.google.com/p/data-race-test .""" |
michael@0 | 208 | |
michael@0 | 209 | def __init__(self, adb): |
michael@0 | 210 | super(TSanTool, self).__init__(adb) |
michael@0 | 211 | |
michael@0 | 212 | def GetFilesForTool(self): |
michael@0 | 213 | """Returns a list of file names for the tool.""" |
michael@0 | 214 | return ['tools/valgrind/android/vg-chrome-wrapper-tsan.sh', |
michael@0 | 215 | 'tools/valgrind/tsan/suppressions.txt', |
michael@0 | 216 | 'tools/valgrind/tsan/suppressions_android.txt', |
michael@0 | 217 | 'tools/valgrind/tsan/ignores.txt'] |
michael@0 | 218 | |
michael@0 | 219 | def GetTestWrapper(self): |
michael@0 | 220 | """Returns a string that is to be prepended to the test command line.""" |
michael@0 | 221 | return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper-tsan.sh' |
michael@0 | 222 | |
michael@0 | 223 | def GetTimeoutScale(self): |
michael@0 | 224 | """Returns a multiplier that should be applied to timeout values.""" |
michael@0 | 225 | return 30.0 |
michael@0 | 226 | |
michael@0 | 227 | |
michael@0 | 228 | TOOL_REGISTRY = { |
michael@0 | 229 | 'memcheck': lambda x: MemcheckTool(x), |
michael@0 | 230 | 'memcheck-renderer': lambda x: MemcheckTool(x), |
michael@0 | 231 | 'tsan': lambda x: TSanTool(x), |
michael@0 | 232 | 'tsan-renderer': lambda x: TSanTool(x), |
michael@0 | 233 | 'asan': lambda x: AddressSanitizerTool(x), |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | |
michael@0 | 237 | def CreateTool(tool_name, adb): |
michael@0 | 238 | """Creates a tool with the specified tool name. |
michael@0 | 239 | |
michael@0 | 240 | Args: |
michael@0 | 241 | tool_name: Name of the tool to create. |
michael@0 | 242 | adb: ADB interface the tool will use. |
michael@0 | 243 | Returns: |
michael@0 | 244 | A tool for the specified tool_name. |
michael@0 | 245 | """ |
michael@0 | 246 | if not tool_name: |
michael@0 | 247 | return BaseTool() |
michael@0 | 248 | |
michael@0 | 249 | ctor = TOOL_REGISTRY.get(tool_name) |
michael@0 | 250 | if ctor: |
michael@0 | 251 | return ctor(adb) |
michael@0 | 252 | else: |
michael@0 | 253 | print 'Unknown tool %s, available tools: %s' % ( |
michael@0 | 254 | tool_name, ', '.join(sorted(TOOL_REGISTRY.keys()))) |
michael@0 | 255 | sys.exit(1) |