1# Copyright (c) 2014 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, os, subprocess
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9
10class hardware_UnsafeMemory(test.test):
11    """
12      This test runs for the specified number of seconds
13      checking for user-controllable memory corruption using
14      the rowhammer-test tools:
15         https://code.google.com/a/google.com/p/rowhammer-test
16    """
17
18    version = 1
19    _DIR_NAME = 'rowhammer-test-4d619293e1c7'
20
21    def setup(self):
22        os.chdir(os.path.join(self.srcdir, self._DIR_NAME))
23        utils.make('clean')
24        utils.make('all')
25
26    def get_thermals(self):
27        therm0 = '-'
28        therm1 = '-'
29        try:
30            therm0 = utils.read_file(
31                '/sys/devices/virtual/thermal/thermal_zone0/temp')
32        except:
33            pass
34        try:
35            therm1 = utils.read_file(
36                '/sys/devices/virtual/thermal/thermal_zone1/temp')
37        except:
38            pass
39        return (therm0, therm1)
40
41    def run_once(self, sec=(60*25)):
42        """
43        Executes the test and logs the output.
44
45        @param sec: seconds to test memory
46        """
47        self._hammer_path = os.path.join(self.srcdir, self._DIR_NAME,
48                                         'rowhammer_test')
49        logging.info('cmd: %s %d' % (self._hammer_path, sec))
50        # Grab the CPU temperature before hand if possible.
51        logging.info('start temp: %s %s' % self.get_thermals())
52        try:
53            output = subprocess.check_output([self._hammer_path, '%d' % sec])
54            logging.info("run complete. Output below:")
55            logging.info(output)
56        except subprocess.CalledProcessError, e:
57            logging.error("Unsafe memory found!")
58            logging.error(e.output)
59            logging.info('end temp: %s %s' % self.get_thermals())
60            raise error.TestFail('Unsafe memory found!')
61        logging.info('end temp: %s %s' % self.get_thermals())
62        return True
63