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