File: remote_link_unit_tests.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rwxr-xr-x 10,611 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#! /usr/bin/env python3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Unit tests for remote_link.
#
# Usage:
#
#   tools/clang/scripts/remote_link_unit_tests.py
#
# A coverage report combining these tests with the integration tests
# in remote_link_integration_tests.py can be generated by running:
#
#   env COVERAGE_FILE=.coverage.unit python3 third_party/pycoverage run \
#     tools/clang/scripts/remote_link_unit_tests.py
#   env COVERAGE_FILE=.coverage.integration python3 third_party/pycoverage \
#     run tools/clang/scripts/remote_link_integration_tests.py
#   python3 third_party/pycoverage combine
#   python3 third_party/pycoverage html
#
# The report will be available as htmlcov/index.html

import remote_ld
import remote_link

import os
import unittest
from unittest import mock

from remote_link_test_utils import named_directory, working_directory


class FakeFs(object):
  """
  Context manager that mocks the functions through which remote_link
  interacts with the filesystem.
  """

  def __init__(self, bitcode_files=None, other_files=None):
    self.bitcode_files = set(bitcode_files or [])
    self.other_files = set(other_files or [])

    def ensure_file(path):
      self.other_files.add(path)

    def exists(path):
      return path in self.bitcode_files or path in self.other_files

    def is_bitcode_file(path):
      return path in self.bitcode_files

    self.mock_ensure_file = mock.patch('remote_link.ensure_file', ensure_file)
    self.mock_exists = mock.patch('os.path.exists', exists)
    self.mock_is_bitcode_file = mock.patch('remote_link.is_bitcode_file',
                                           is_bitcode_file)

  def __enter__(self):
    self.mock_ensure_file.start()
    self.mock_exists.start()
    self.mock_is_bitcode_file.start()
    return self

  def __exit__(self, exnty, *args, **kwargs):
    self.mock_is_bitcode_file.stop()
    self.mock_exists.stop()
    self.mock_ensure_file.stop()
    return exnty is None


class RemoteLinkUnitTest(unittest.TestCase):
  """
  Unit tests for remote_link.
  """

  def test_analyze_expanded_args_nocodegen(self):
    with FakeFs(other_files=['foo.o', 'bar.o']):
      self.assertIsNone(remote_ld.RemoteLinkUnix().analyze_expanded_args(
          ['clang', 'foo.o', 'bar.o', '-o', 'foo'], 'foo', 'clang', 'lto.foo',
          'common', False))

  def test_analyze_expanded_args_one_codegen(self):
    with FakeFs(bitcode_files=['foo.o'], other_files=['bar.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args(
          ['clang', 'foo.o', 'bar.o', '-o', 'foo'], 'foo', 'clang', 'lto.foo',
          'common', False)
      self.assertIsNotNone(result)
      self.assertNotEqual(len(result.codegen), 0)
      self.assertEqual(result.codegen[0][1], 'foo.o')
      self.assertEqual(len(result.codegen), 1)
      self.assertIn('foo.o', result.index_params)
      self.assertIn('bar.o', result.index_params)
      self.assertIn('bar.o', result.final_params)
      # foo.o should not be in final_params because it will be added via
      # the used object file.
      self.assertNotIn('foo.o', result.final_params)

  def test_analyze_expanded_args_params(self):
    with FakeFs(bitcode_files=['foo.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args([
          'clang', '-O2', '--target=arm-none-eabi', '-march=armv7-a',
          '-flto=thin', '-fsplit-lto-unit', '-fwhole-program-vtables',
          '-fsanitize=cfi', '-g', '-gsplit-dwarf', '-mllvm',
          '-generate-type-units', 'foo.o', '-o', 'foo'
      ], 'foo', 'clang', 'lto.foo', 'common', False)
      self.assertIsNotNone(result)
      self.assertIn('-Wl,-plugin-opt=obj-path=lto.foo/foo.split.o',
                    result.index_params)
      self.assertIn('-O2', result.index_params)
      self.assertIn('--target=arm-none-eabi', result.codegen_params)
      self.assertIn('-march=armv7-a', result.codegen_params)
      self.assertIn('-g', result.index_params)
      self.assertIn('-gsplit-dwarf', result.index_params)
      self.assertIn('-mllvm -generate-type-units',
                    ' '.join(result.index_params))
      self.assertIn('-flto=thin', result.index_params)
      self.assertIn('-fwhole-program-vtables', result.index_params)
      self.assertIn('-fsanitize=cfi', result.index_params)

      self.assertIn('-O2', result.codegen_params)
      self.assertIn('--target=arm-none-eabi', result.codegen_params)
      self.assertIn('-march=armv7-a', result.codegen_params)
      self.assertIn('-gsplit-dwarf', result.codegen_params)
      self.assertIn('-mllvm -generate-type-units',
                    ' '.join(result.codegen_params))
      self.assertNotIn('-flto=thin', result.codegen_params)
      self.assertNotIn('-fwhole-program-vtables', result.codegen_params)
      self.assertNotIn('-fsanitize=cfi', result.codegen_params)

      self.assertIn('-flto=thin', result.final_params)

  def test_codegen_params_default(self):
    with FakeFs(bitcode_files=['foo.o'], other_files=['bar.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args(
          ['clang', 'foo.o', 'bar.o', '-o', 'foo'], 'foo', 'clang', 'lto.foo',
          'common', False)
      # Codegen optimization level should default to 2.
      self.assertIn('-O2', result.codegen_params)
      # -fdata-sections and -ffunction-sections default to on to match the
      # behavior of local linking.
      self.assertIn('-fdata-sections', result.codegen_params)
      self.assertIn('-ffunction-sections', result.codegen_params)

  def test_codegen_params_default_cl(self):
    with FakeFs(bitcode_files=['foo.obj'], other_files=['bar.obj']):
      result = remote_link.RemoteLinkWindows().analyze_expanded_args(
          ['clang-cl', 'foo.obj', 'bar.obj', '-Fefoo.exe'], 'foo.exe',
          'clang-cl', 'lto.foo', 'common', False)
      # Codegen optimization level should default to 2.
      self.assertIn('-O2', result.codegen_params)
      # -Gw and -Gy default to on to match the behavior of local linking.
      self.assertIn('-Gw', result.codegen_params)
      self.assertIn('-Gy', result.codegen_params)

  def test_codegen_params_no_data_sections(self):
    with FakeFs(bitcode_files=['foo.o'], other_files=['bar.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args(
          ['clang', '-fno-data-sections', 'foo.o', 'bar.o', '-o', 'foo'], 'foo',
          'clang', 'lto.foo', 'common', False)
      self.assertNotIn('-fdata-sections', result.codegen_params)
      self.assertIn('-ffunction-sections', result.codegen_params)

  def test_codegen_params_no_function_sections(self):
    with FakeFs(bitcode_files=['foo.o'], other_files=['bar.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args(
          ['clang', '-fno-function-sections', 'foo.o', 'bar.o', '-o', 'foo'],
          'foo', 'clang', 'lto.foo', 'common', False)
      self.assertIn('-fdata-sections', result.codegen_params)
      self.assertNotIn('-ffunction-sections', result.codegen_params)

  def test_codegen_params_no_data_sections_cl(self):
    with FakeFs(bitcode_files=['foo.obj'], other_files=['bar.obj']):
      result = remote_link.RemoteLinkWindows().analyze_expanded_args(
          ['clang-cl', '/Gw-', 'foo.obj', 'bar.obj', '/Fefoo.exe'], 'foo.exe',
          'clang-cl', 'lto.foo', 'common', False)
      self.assertNotIn('-fdata-sections', result.codegen_params)
      self.assertNotIn('-Gw', result.codegen_params)
      self.assertNotIn('/Gw', result.codegen_params)
      self.assertIn('-Gy', result.codegen_params)

  def test_codegen_params_no_function_sections_cl(self):
    with FakeFs(bitcode_files=['foo.obj'], other_files=['bar.obj']):
      result = remote_link.RemoteLinkWindows().analyze_expanded_args(
          ['clang-cl', '/Gy-', 'foo.obj', 'bar.obj', '/Fefoo.exe'], 'foo.exe',
          'clang-cl', 'lto.foo', 'common', False)
      self.assertIn('-Gw', result.codegen_params)
      self.assertNotIn('-ffunction-sections', result.codegen_params)
      self.assertNotIn('-Gy', result.codegen_params)
      self.assertNotIn('/Gy', result.codegen_params)

  def test_codegen_params_explicit_data_and_function_sections(self):
    with FakeFs(bitcode_files=['foo.o'], other_files=['bar.o']):
      result = remote_ld.RemoteLinkUnix().analyze_expanded_args([
          'clang', '-ffunction-sections', '-fdata-sections', 'foo.o', 'bar.o',
          '-o', 'foo'
      ], 'foo', 'clang', 'lto.foo', 'common', False)
      self.assertIn('-fdata-sections', result.codegen_params)
      self.assertIn('-ffunction-sections', result.codegen_params)

  def test_codegen_params_explicit_data_and_function_sections_cl(self):
    with FakeFs(bitcode_files=['foo.obj'], other_files=['bar.obj']):
      result = remote_link.RemoteLinkWindows().analyze_expanded_args(
          ['clang-cl', '/Gy', '-Gw', 'foo.obj', 'bar.obj', '/Fefoo.exe'],
          'foo.exe', 'clang-cl', 'lto.foo', 'common', False)
      self.assertIn('-Gw', result.codegen_params)
      self.assertIn('/Gy', result.codegen_params)
      self.assertNotIn('-fdata-sections', result.codegen_params)
      self.assertNotIn('-ffunction-sections', result.codegen_params)

  def test_ensure_file_no_dir(self):
    with named_directory() as d, working_directory(d):
      self.assertFalse(os.path.exists('test'))
      remote_link.ensure_file('test')
      self.assertTrue(os.path.exists('test'))

  def test_ensure_file_existing(self):
    with named_directory() as d, working_directory(d):
      self.assertFalse(os.path.exists('foo/test'))
      remote_link.ensure_file('foo/test')
      self.assertTrue(os.path.exists('foo/test'))
      os.utime('foo/test', (0, 0))
      statresult = os.stat('foo/test')
      remote_link.ensure_file('foo/test')
      self.assertTrue(os.path.exists('foo/test'))
      newstatresult = os.stat('foo/test')
      self.assertEqual(newstatresult.st_mtime, statresult.st_mtime)

  def test_ensure_file_error(self):
    with named_directory() as d, working_directory(d):
      self.assertFalse(os.path.exists('test'))
      remote_link.ensure_file('test')
      self.assertTrue(os.path.exists('test'))
      self.assertRaises(OSError, remote_link.ensure_file, 'test/impossible')

  def test_transform_codegen_param_on_mllvm(self):
    # Regression test for crbug.com/1135234
    link = remote_ld.RemoteLinkUnix()
    self.assertEqual(
        link.transform_codegen_param_common('-mllvm,-import-instr-limit=20'),
        ['-mllvm', '-import-instr-limit=20'])


if __name__ == '__main__':
  unittest.main()