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.
4
5# pylint: disable=R0201
6
7from __future__ import print_function
8
9import logging
10import sys
11
12from devil.android import device_errors
13from devil.android.valgrind_tools import base_tool
14
15
16def SetChromeTimeoutScale(device, scale):
17  """Sets the timeout scale in /data/local/tmp/chrome_timeout_scale to scale."""
18  path = '/data/local/tmp/chrome_timeout_scale'
19  if not scale or scale == 1.0:
20    # Delete if scale is None/0.0/1.0 since the default timeout scale is 1.0
21    device.RemovePath(path, force=True, as_root=True)
22  else:
23    device.WriteFile(path, '%f' % scale, as_root=True)
24
25
26
27class AddressSanitizerTool(base_tool.BaseTool):
28  """AddressSanitizer tool."""
29
30  WRAPPER_NAME = '/system/bin/asanwrapper'
31  # Disable memcmp overlap check.There are blobs (gl drivers)
32  # on some android devices that use memcmp on overlapping regions,
33  # nothing we can do about that.
34  EXTRA_OPTIONS = 'strict_memcmp=0,use_sigaltstack=1'
35
36  def __init__(self, device):
37    super(AddressSanitizerTool, self).__init__()
38    self._device = device
39
40  @classmethod
41  def CopyFiles(cls, device):
42    """Copies ASan tools to the device."""
43    del device
44
45  def GetTestWrapper(self):
46    return AddressSanitizerTool.WRAPPER_NAME
47
48  def GetUtilWrapper(self):
49    """Returns the wrapper for utilities, such as forwarder.
50
51    AddressSanitizer wrapper must be added to all instrumented binaries,
52    including forwarder and the like. This can be removed if such binaries
53    were built without instrumentation. """
54    return self.GetTestWrapper()
55
56  def SetupEnvironment(self):
57    try:
58      self._device.EnableRoot()
59    except device_errors.CommandFailedError as e:
60      # Try to set the timeout scale anyway.
61      # TODO(jbudorick) Handle this exception appropriately after interface
62      #                 conversions are finished.
63      logging.error(str(e))
64    SetChromeTimeoutScale(self._device, self.GetTimeoutScale())
65
66  def CleanUpEnvironment(self):
67    SetChromeTimeoutScale(self._device, None)
68
69  def GetTimeoutScale(self):
70    # Very slow startup.
71    return 20.0
72
73
74TOOL_REGISTRY = {
75    'asan': AddressSanitizerTool,
76}
77
78
79def CreateTool(tool_name, device):
80  """Creates a tool with the specified tool name.
81
82  Args:
83    tool_name: Name of the tool to create.
84    device: A DeviceUtils instance.
85  Returns:
86    A tool for the specified tool_name.
87  """
88  if not tool_name:
89    return base_tool.BaseTool()
90
91  ctor = TOOL_REGISTRY.get(tool_name)
92  if ctor:
93    return ctor(device)
94  else:
95    print('Unknown tool %s, available tools: %s' % (tool_name, ', '.join(
96        sorted(TOOL_REGISTRY.keys()))))
97    sys.exit(1)
98
99def PushFilesForTool(tool_name, device):
100  """Pushes the files required for |tool_name| to |device|.
101
102  Args:
103    tool_name: Name of the tool to create.
104    device: A DeviceUtils instance.
105  """
106  if not tool_name:
107    return
108
109  clazz = TOOL_REGISTRY.get(tool_name)
110  if clazz:
111    clazz.CopyFiles(device)
112  else:
113    print('Unknown tool %s, available tools: %s' % (tool_name, ', '.join(
114        sorted(TOOL_REGISTRY.keys()))))
115    sys.exit(1)
116