1 # Copyright (c) 2012 The Chromium 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.
6 Classes in this file define additional actions that need to be taken to run a
7 test under some kind of runtime error detection tool.
9 The interface is intended to be used as follows.
11 1. For tests that simply run a native process (i.e. no activity is spawned):
13 Call tool.CopyFiles().
14 Prepend test command line with tool.GetTestWrapper().
16 2. For tests that spawn an activity:
18 Call tool.CopyFiles().
19 Call tool.SetupEnvironment().
20 Run the test as usual.
21 Call tool.CleanUpEnvironment().
23 # pylint: disable=R0201
30 from pylib.constants import DIR_SOURCE_ROOT
33 def SetChromeTimeoutScale(device, scale):
34 """Sets the timeout scale in /data/local/tmp/chrome_timeout_scale to scale."""
35 path = '/data/local/tmp/chrome_timeout_scale'
36 if not scale or scale == 1.0:
37 # Delete if scale is None/0.0/1.0 since the default timeout scale is 1.0
38 device.old_interface.RunShellCommand('rm %s' % path)
40 device.old_interface.SetProtectedFileContents(path, '%f' % scale)
43 class BaseTool(object):
44 """A tool that does nothing."""
50 def GetTestWrapper(self):
51 """Returns a string that is to be prepended to the test command line."""
54 def GetUtilWrapper(self):
55 """Returns the wrapper name for the utilities.
58 A string that is to be prepended to the command line of utility
59 processes (forwarder, etc.).
64 """Copies tool-specific files to the device, create directories, etc."""
67 def SetupEnvironment(self):
68 """Sets up the system environment for a test.
70 This is a good place to set system properties.
74 def CleanUpEnvironment(self):
75 """Cleans up environment."""
78 def GetTimeoutScale(self):
79 """Returns a multiplier that should be applied to timeout values."""
82 def NeedsDebugInfo(self):
83 """Whether this tool requires debug info.
86 True if this tool can not work with stripped binaries.
91 class AddressSanitizerTool(BaseTool):
92 """AddressSanitizer tool."""
94 WRAPPER_NAME = '/system/bin/asanwrapper'
95 # Disable memcmp overlap check.There are blobs (gl drivers)
96 # on some android devices that use memcmp on overlapping regions,
97 # nothing we can do about that.
98 EXTRA_OPTIONS = 'strict_memcmp=0,use_sigaltstack=1'
100 def __init__(self, device):
101 super(AddressSanitizerTool, self).__init__()
102 self._device = device
103 # Configure AndroidCommands to run utils (such as md5sum_bin) under ASan.
104 # This is required because ASan is a compiler-based tool, and md5sum
105 # includes instrumented code from base.
106 device.old_interface.SetUtilWrapper(self.GetUtilWrapper())
107 libs = glob.glob(os.path.join(DIR_SOURCE_ROOT,
108 'third_party/llvm-build/Release+Asserts/',
109 'lib/clang/*/lib/linux/',
110 'libclang_rt.asan-arm-android.so'))
111 assert len(libs) == 1
115 """Copies ASan tools to the device."""
116 subprocess.call([os.path.join(DIR_SOURCE_ROOT,
117 'tools/android/asan/asan_device_setup.sh'),
118 '--device', self._device.old_interface.GetDevice(),
120 '--extra-options', AddressSanitizerTool.EXTRA_OPTIONS])
121 self._device.old_interface.WaitForDevicePm()
123 def GetTestWrapper(self):
124 return AddressSanitizerTool.WRAPPER_NAME
126 def GetUtilWrapper(self):
127 """Returns the wrapper for utilities, such as forwarder.
129 AddressSanitizer wrapper must be added to all instrumented binaries,
130 including forwarder and the like. This can be removed if such binaries
131 were built without instrumentation. """
132 return self.GetTestWrapper()
134 def SetupEnvironment(self):
135 self._device.old_interface.EnableAdbRoot()
136 SetChromeTimeoutScale(self._device, self.GetTimeoutScale())
138 def CleanUpEnvironment(self):
139 SetChromeTimeoutScale(self._device, None)
141 def GetTimeoutScale(self):
146 class ValgrindTool(BaseTool):
147 """Base abstract class for Valgrind tools."""
149 VG_DIR = '/data/local/tmp/valgrind'
150 VGLOGS_DIR = '/data/local/tmp/vglogs'
152 def __init__(self, device):
153 super(ValgrindTool, self).__init__()
154 self._device = device
155 # exactly 31 chars, SystemProperties::PROP_NAME_MAX
156 self._wrap_properties = ['wrap.com.google.android.apps.ch',
157 'wrap.org.chromium.native_test']
160 """Copies Valgrind tools to the device."""
161 self._device.old_interface.RunShellCommand(
162 'rm -r %s; mkdir %s' % (ValgrindTool.VG_DIR, ValgrindTool.VG_DIR))
163 self._device.old_interface.RunShellCommand(
164 'rm -r %s; mkdir %s' % (ValgrindTool.VGLOGS_DIR,
165 ValgrindTool.VGLOGS_DIR))
166 files = self.GetFilesForTool()
168 self._device.old_interface.PushIfNeeded(
169 os.path.join(DIR_SOURCE_ROOT, f),
170 os.path.join(ValgrindTool.VG_DIR, os.path.basename(f)))
172 def SetupEnvironment(self):
173 """Sets up device environment."""
174 self._device.old_interface.RunShellCommand('chmod 777 /data/local/tmp')
175 self._device.old_interface.RunShellCommand('setenforce 0')
176 for prop in self._wrap_properties:
177 self._device.old_interface.RunShellCommand(
178 'setprop %s "logwrapper %s"' % (prop, self.GetTestWrapper()))
179 SetChromeTimeoutScale(self._device, self.GetTimeoutScale())
181 def CleanUpEnvironment(self):
182 """Cleans up device environment."""
183 for prop in self._wrap_properties:
184 self._device.old_interface.RunShellCommand('setprop %s ""' % (prop,))
185 SetChromeTimeoutScale(self._device, None)
187 def GetFilesForTool(self):
188 """Returns a list of file names for the tool."""
189 raise NotImplementedError()
191 def NeedsDebugInfo(self):
192 """Whether this tool requires debug info.
195 True if this tool can not work with stripped binaries.
200 class MemcheckTool(ValgrindTool):
203 def __init__(self, device):
204 super(MemcheckTool, self).__init__(device)
206 def GetFilesForTool(self):
207 """Returns a list of file names for the tool."""
208 return ['tools/valgrind/android/vg-chrome-wrapper.sh',
209 'tools/valgrind/memcheck/suppressions.txt',
210 'tools/valgrind/memcheck/suppressions_android.txt']
212 def GetTestWrapper(self):
213 """Returns a string that is to be prepended to the test command line."""
214 return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper.sh'
216 def GetTimeoutScale(self):
217 """Returns a multiplier that should be applied to timeout values."""
221 class TSanTool(ValgrindTool):
222 """ThreadSanitizer tool. See http://code.google.com/p/data-race-test ."""
224 def __init__(self, device):
225 super(TSanTool, self).__init__(device)
227 def GetFilesForTool(self):
228 """Returns a list of file names for the tool."""
229 return ['tools/valgrind/android/vg-chrome-wrapper-tsan.sh',
230 'tools/valgrind/tsan/suppressions.txt',
231 'tools/valgrind/tsan/suppressions_android.txt',
232 'tools/valgrind/tsan/ignores.txt']
234 def GetTestWrapper(self):
235 """Returns a string that is to be prepended to the test command line."""
236 return ValgrindTool.VG_DIR + '/' + 'vg-chrome-wrapper-tsan.sh'
238 def GetTimeoutScale(self):
239 """Returns a multiplier that should be applied to timeout values."""
244 'memcheck': MemcheckTool,
245 'memcheck-renderer': MemcheckTool,
247 'tsan-renderer': TSanTool,
248 'asan': AddressSanitizerTool,
252 def CreateTool(tool_name, device):
253 """Creates a tool with the specified tool name.
256 tool_name: Name of the tool to create.
257 device: A DeviceUtils instance.
259 A tool for the specified tool_name.
264 ctor = TOOL_REGISTRY.get(tool_name)
268 print 'Unknown tool %s, available tools: %s' % (
269 tool_name, ', '.join(sorted(TOOL_REGISTRY.keys())))