1# Copyright (c) 2012 The Chromium OS 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 5import logging 6import os 7 8from autotest_lib.client.bin import test, utils 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import chrome 11from autotest_lib.client.cros import cros_logging 12 13 14class logging_AsanCrash(test.test): 15 """Verify Address Sanitizer does catch memory errors.""" 16 17 version = 1 18 19 20 def run_once(self): 21 if not 'asan' in utils.read_file('/etc/ui_use_flags.txt'): 22 raise error.TestFail('Current image not built with ASAN') 23 24 extension_path = os.path.join(os.path.dirname(__file__), 25 'asan_crash_ext') 26 27 with chrome.Chrome(extension_paths=[extension_path]) as cr: 28 pid = utils.get_oldest_pid_by_name('chrome') 29 asan_log_name = '/var/log/chrome/asan_log.%d' % pid 30 logging.info('Browser PID under telemetry control is %d. ' 31 'So ASAN log is expected at %s.', pid, asan_log_name) 32 33 logging.info('Initiate simulating memory bug to be caught by ASAN.') 34 extension = cr.get_extension(extension_path) 35 if not extension: 36 raise error.TestFail('Failed to find extension %s' 37 % extension_path) 38 39 # Catch the exception raised when the browser crashes. 40 cr.did_browser_crash(lambda: extension.ExecuteJavaScript( 41 'chrome.autotestPrivate.simulateAsanMemoryBug();')) 42 43 utils.poll_for_condition( 44 lambda: os.path.isfile(asan_log_name), 45 timeout=10, 46 exception=error.TestFail( 47 'Found no asan log file %s during 10s' 48 % asan_log_name)) 49 ui_log = cros_logging.LogReader(asan_log_name) 50 ui_log.read_all_logs() 51 52 # We must wait some time until memory bug is simulated (happens 53 # immediately after the return on the call) and caught by ASAN. 54 try: 55 utils.poll_for_condition( 56 lambda: ui_log.can_find('ERROR: AddressSanitizer'), 57 timeout=10, 58 exception=error.TestFail( 59 'Found no asan log message about ' 60 'Address Sanitizer catch')) 61 62 utils.poll_for_condition( 63 lambda: ui_log.can_find("'testarray'"), 64 timeout=10, 65 exception=error.TestFail( 66 'ASAN caught bug but did not mention ' 67 'the cause in the log')) 68 69 except: 70 logging.debug('ASAN log content: ' + ui_log.get_logs()) 71 raise 72 73 # The cbuildbot logic will look for asan logs and process them. 74 # Remove the simulated log file to avoid that. 75 os.remove(asan_log_name) 76