|
| 1 | +# Copyright (c) 2018 Adrian Herrera |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import datetime |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import shutil |
| 20 | + |
| 21 | +from s2e_env.command import CommandError |
| 22 | +from s2e_env.commands.project_creation.abstract_project import AbstractProject |
| 23 | +from s2e_env.utils.templates import render_template |
| 24 | + |
| 25 | + |
| 26 | +L = logging.getLogger('deepstate.s2e') |
| 27 | +L.setLevel(logging.INFO) |
| 28 | + |
| 29 | +# Only Linux targets are supported |
| 30 | +ARCH_TO_IMAGE = { |
| 31 | + 'i386': 'debian-9.2.1-i386', |
| 32 | + 'x86_64': 'debian-9.2.1-x86_64', |
| 33 | +} |
| 34 | + |
| 35 | +DEEPSTATE_TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), 'templates') |
| 36 | + |
| 37 | +INSTRUCTIONS = """ |
| 38 | +Your DeepState project is available in {project_dir}. |
| 39 | +
|
| 40 | +This is a simplified version of a regular S2E analysis project. To start the |
| 41 | +analysis: |
| 42 | +
|
| 43 | + * cd {project_dir} && ./launch-s2e.sh |
| 44 | +
|
| 45 | +This will run the DeepState-enabled program in an S2E guest VM. The generated |
| 46 | +tests will appear in the s2e-last/tests directory. The tests can be run using |
| 47 | +the `--input_test_files_dir` option on a **NON** S2E-compiled program (running |
| 48 | +an S2E-compiled program **outside** of S2E will result in an illegal |
| 49 | +instruction error). |
| 50 | +
|
| 51 | +Customization |
| 52 | +============= |
| 53 | +
|
| 54 | +* By default, your analysis will run with {num_workers} worker(s). This can be |
| 55 | + changed by modifying the S2E_MAX_PROCESSES variable in launch-s2e.sh |
| 56 | +* If your target program depends on other files (e.g., shared libraries, etc.), |
| 57 | + then these should be retrieved in bootstrap.sh by adding a call to ${{S2EGET}} |
| 58 | +* Only the minimum plugins required to generate tests have been enabled. To |
| 59 | + enable more, edit s2e-config.lua |
| 60 | +""" |
| 61 | + |
| 62 | + |
| 63 | +class DeepStateProject(AbstractProject): |
| 64 | + """A simplified S2E analysis project for DeepState-compiled programs.""" |
| 65 | + |
| 66 | + def _configure(self, target, *args, **options): |
| 67 | + """ |
| 68 | + Generate the S2E analysis project configuration. |
| 69 | + """ |
| 70 | + if target.is_empty(): |
| 71 | + raise CommandError('Cannot use an empty target for a DeepState ' |
| 72 | + 'project') |
| 73 | + |
| 74 | + # Decide on the image to use |
| 75 | + image = ARCH_TO_IMAGE.get(target.arch) |
| 76 | + if not image: |
| 77 | + raise CommandError('Unable to find a suitable image for %s' % |
| 78 | + target.path) |
| 79 | + img_desc = self._select_image(target, image, download_image=False) |
| 80 | + L.info('Using %s', img_desc['name']) |
| 81 | + |
| 82 | + # Return the project config dict |
| 83 | + return { |
| 84 | + 'creation_time': str(datetime.datetime.now()), |
| 85 | + 'project_dir': self.env_path('projects', options['name']), |
| 86 | + 'image': img_desc, |
| 87 | + 'target_path': target.path, |
| 88 | + 'target_arch': target.arch, |
| 89 | + 'num_workers': options['num_workers'], |
| 90 | + } |
| 91 | + |
| 92 | + def _create(self, config, force=False): |
| 93 | + """ |
| 94 | + Create the S2E analysis project, based on the given configuration. |
| 95 | + """ |
| 96 | + project_dir = config['project_dir'] |
| 97 | + |
| 98 | + # The force option is not exposed on the command-line for a DeepState |
| 99 | + # project, so fail if the project directory already exists |
| 100 | + if os.path.isdir(project_dir): |
| 101 | + raise CommandError('Project directory %s already exists' % |
| 102 | + project_dir) |
| 103 | + |
| 104 | + os.mkdir(project_dir) |
| 105 | + |
| 106 | + try: |
| 107 | + # Create a symlink to the analysis target |
| 108 | + self._symlink_project_files(project_dir, config['target_path']) |
| 109 | + |
| 110 | + # Create a symlink to the guest tools directory |
| 111 | + self._symlink_guest_tools(project_dir, config['image']) |
| 112 | + |
| 113 | + # Render the templates |
| 114 | + self._create_launch_script(project_dir, config) |
| 115 | + self._create_lua_config(project_dir, config) |
| 116 | + self._create_bootstrap(project_dir, config) |
| 117 | + except Exception: |
| 118 | + # If anything goes wrong during project creation, remove anything |
| 119 | + # incomplete |
| 120 | + shutil.rmtree(project_dir) |
| 121 | + raise |
| 122 | + |
| 123 | + def _get_instructions(self, config): |
| 124 | + """ |
| 125 | + Generate instructions. |
| 126 | + """ |
| 127 | + return INSTRUCTIONS.format(**config) |
| 128 | + |
| 129 | + def _create_launch_script(self, project_dir, config): |
| 130 | + """ |
| 131 | + Create the S2E launch script. |
| 132 | + """ |
| 133 | + L.info('Creating launch script') |
| 134 | + |
| 135 | + img_desc = config['image'] |
| 136 | + context = { |
| 137 | + 'creation_time': config['creation_time'], |
| 138 | + 'env_dir': self.env_path(), |
| 139 | + 'rel_image_path': os.path.relpath(img_desc['path'], self.env_path()), |
| 140 | + 'max_processes': config['num_workers'], |
| 141 | + 'qemu_arch': img_desc['qemu_build'], |
| 142 | + 'qemu_memory': img_desc['memory'], |
| 143 | + 'qemu_snapshot': img_desc['snapshot'], |
| 144 | + 'qemu_extra_flags': img_desc['qemu_extra_flags'], |
| 145 | + } |
| 146 | + |
| 147 | + output_file = 'launch-s2e.sh' |
| 148 | + output_path = os.path.join(project_dir, output_file) |
| 149 | + |
| 150 | + render_template(context, '%s.j2' % output_file, output_path, |
| 151 | + templates_dir=DEEPSTATE_TEMPLATES_DIR, executable=True) |
| 152 | + |
| 153 | + def _create_lua_config(self, project_dir, config): |
| 154 | + """ |
| 155 | + Create the S2E Lua config. |
| 156 | + """ |
| 157 | + L.info('Creating S2E configuration') |
| 158 | + |
| 159 | + self._copy_lua_library(project_dir) |
| 160 | + |
| 161 | + context = { |
| 162 | + 'creation_time': config['creation_time'], |
| 163 | + 'project_dir': config['project_dir'], |
| 164 | + 'target_process': os.path.basename(config['target_path']), |
| 165 | + } |
| 166 | + |
| 167 | + output_file = 's2e-config.lua' |
| 168 | + output_path = os.path.join(project_dir, output_file) |
| 169 | + |
| 170 | + render_template(context, '%s.j2' % output_file, output_path, |
| 171 | + templates_dir=DEEPSTATE_TEMPLATES_DIR) |
| 172 | + |
| 173 | + def _create_bootstrap(self, project_dir, config): |
| 174 | + """ |
| 175 | + Create the S2E bootstrap script. |
| 176 | + """ |
| 177 | + L.info('Creating S2E bootstrap script') |
| 178 | + |
| 179 | + context = { |
| 180 | + 'creation_time': config['creation_time'], |
| 181 | + 'target': os.path.basename(config['target_path']), |
| 182 | + } |
| 183 | + |
| 184 | + output_file = 'bootstrap.sh' |
| 185 | + output_path = os.path.join(project_dir, output_file) |
| 186 | + |
| 187 | + render_template(context, '%s.j2' % output_file, output_path, |
| 188 | + templates_dir=DEEPSTATE_TEMPLATES_DIR) |
0 commit comments