Beispiel #1
0
    def _create_launch_script(self, project_dir, config):
        """
        Create the S2E launch script.
        """
        logger.info('Creating launch script')

        context = {
            'creation_time':
            config['creation_time'],
            'env_dir':
            self.env_path(),
            'rel_image_path':
            os.path.relpath(config['image']['path'], self.env_path()),
            'qemu_arch':
            config['image']['qemu_build'],
            'qemu_memory':
            config['image']['memory'],
            'qemu_snapshot':
            config['image']['snapshot'],
            'qemu_extra_flags':
            config['image']['qemu_extra_flags'],
        }

        template = 'launch-s2e.sh'
        output_path = os.path.join(project_dir, template)
        render_template(context, template, output_path, executable=True)
Beispiel #2
0
    def _create_lua_config(self, config):
        """
        Create the S2E Lua config.
        """
        context = {
            'creation_time': config['creation_time'],
            'target': config['target'],
            'target_lua_template': self._configurator.LUA_TEMPLATE,
            'project_dir': config['project_dir'],
            'use_seeds': config['use_seeds'],
            'use_cupa': config['use_cupa'],
            'use_test_case_generator': config['use_test_case_generator'],
            'enable_pov_generation': config['enable_pov_generation'],
            'seeds_dir': config['seeds_dir'],
            'has_guestfs': config['has_guestfs'],
            'guestfs_dir': config['guestfs_dir'],
            'recipes_dir': config['recipes_dir'],
            'target_files': config['target_files'],
            'modules': config['modules'],
            'processes': config['processes'],
        }

        for f in ('s2e-config.lua', 'models.lua', 'library.lua'):
            output_path = os.path.join(self._project_dir, f)
            render_template(context, f, output_path)
Beispiel #3
0
    def _create_lua_config(self, project_dir, config):
        """
        Create the S2E Lua config.
        """
        logger.info('Creating S2E configuration')

        self._copy_lua_library(project_dir)

        target_path = config['target_path']
        context = {
            'creation_time': config['creation_time'],
            'target': os.path.basename(target_path) if target_path else None,
            'target_lua_template': self._lua_template,
            'project_dir': project_dir,
            'use_seeds': config['use_seeds'],
            'use_cupa': config['use_cupa'],
            'use_test_case_generator': config['use_test_case_generator'],
            'enable_pov_generation': config['enable_pov_generation'],
            'seeds_dir': config['seeds_dir'],
            'has_guestfs': config['has_guestfs'],
            'guestfs_path': config['guestfs_path'],
            'recipes_dir': config['recipes_dir'],
            'target_files':
            [os.path.basename(tf) for tf in config['target_files']],
            'modules': config['modules'],
            'processes': config['processes'],
        }

        for template in ('s2e-config.lua', 'models.lua'):
            output_path = os.path.join(project_dir, template)
            render_template(context, template, output_path)
Beispiel #4
0
    def _create_bootstrap(self, config):
        """
        Create the S2E bootstrap script.
        """
        # The target arguments are specified using a format similar to the
        # American Fuzzy Lop fuzzer. Options are specified as normal, however
        # for programs that take input from a file, '@@' is used to mark the
        # location in the target's command line where the input file should be
        # placed. This will automatically be substituted with a symbolic file
        # in the S2E bootstrap script.
        parsed_args = [
            '${SYMB_FILE}' if arg == '@@' else arg
            for arg in config['target_args']
        ]

        template = 'bootstrap.sh'
        context = {
            'creation_time': config['creation_time'],
            'target': config['target'],
            'target_args': parsed_args,
            'sym_args': config['sym_args'],
            'target_bootstrap_template': self._configurator.BOOTSTRAP_TEMPLATE,
            'image_arch': config['image']['os']['arch'],
            'use_symb_input_file': config['use_symb_input_file'],
            'use_seeds': config['use_seeds'],
            'dynamically_linked': config['dynamically_linked'],
            'project_type': config['project_type'],
        }

        script_path = os.path.join(self._project_dir, template)
        render_template(context, template, script_path)
Beispiel #5
0
    def _create_config(self, context):
        context[
            'target_bootstrap_template'] = self._configurator.BOOTSTRAP_TEMPLATE
        context['target_lua_template'] = self._configurator.LUA_TEMPLATE

        for f in ('s2e-config.lua', 'models.lua', 'library.lua',
                  'bootstrap.sh'):
            output_path = os.path.join(self._project_dir, f)
            render_template(context, f, output_path)
Beispiel #6
0
def _create_config(env_path):
    """
    Create the YAML config file for the new environment.
    """
    s2e_yaml = 's2e.yaml'
    version_path = os.path.join(os.path.dirname(__file__), '..', 'dat', 'VERSION')

    context = {
        'creation_time': str(datetime.datetime.now()),
        'version': open(version_path, 'r').read().strip(),
    }

    render_template(context, s2e_yaml, os.path.join(env_path, s2e_yaml))
Beispiel #7
0
    def _create_bootstrap(self, project_dir, config):
        """
        Create the S2E bootstrap script.
        """
        logger.info('Creating S2E bootstrap script')

        context = config.copy()
        context['target_bootstrap_template'] = self._bootstrap_template
        context['image_arch'] = config['image']['os']['arch']

        template = 'bootstrap.sh'
        output_path = os.path.join(project_dir, template)
        render_template(context, template, output_path)
Beispiel #8
0
def _create_activate_script(env_path):
    """
    Create the environment activation script.
    """
    # TODO detect shell to determine template
    template = 's2e_activate.sh'

    context = {
        'S2EDIR': env_path,
    }

    render_template(context, template,
                    os.path.join(env_path, 'install', 'bin', 's2e_activate'))
Beispiel #9
0
def _create_activate_script(env_path):
    """
    Create the environment activation script.
    """
    # TODO detect shell to determine template
    template = 's2e_activate.sh'

    context = {
        'creation_time': str(datetime.datetime.now()),
        'S2EDIR': env_path,
    }

    render_template(context, template, os.path.join(env_path, 's2e_activate'))
Beispiel #10
0
    def _create_lua_config(self, project_dir, config):
        """
        Create the S2E Lua config.
        """
        logger.info('Creating S2E configuration')

        self._copy_lua_library(project_dir)

        context = config.copy()
        context['target_lua_template'] = self._lua_template
        context['project_dir'] = project_dir

        for template in ('s2e-config.lua', 'models.lua'):
            output_path = os.path.join(project_dir, template)
            render_template(context, template, output_path)
Beispiel #11
0
    def _create_launch_script(self, config):
        """
        Create the S2E launch script.
        """
        template = 'launch-s2e.sh'
        script_path = os.path.join(self._project_dir, template)
        context = {
            'creation_time': config['creation_time'],
            'env_dir': self.env_path(),
            'rel_image_path': os.path.relpath(config['image']['path'], self.env_path()),
            'qemu_arch': self._qemu_arch,
            'qemu_memory': config['image']['memory'],
            'qemu_snapshot': config['image']['snapshot'],
            'qemu_extra_flags': config['image']['qemu_extra_flags'],
        }

        render_template(context, template, script_path, executable=True)
Beispiel #12
0
    def _create_launch_script(self):
        """
        Create the S2E launch script.
        """
        template = 'launch-s2e.sh'
        script_path = os.path.join(self._project_dir, template)
        context = {
            'creation_time': str(datetime.datetime.now()),
            'env_dir': self.env_path(),
            'install_dir': self.install_path(),
            'build_dir': self.build_path(),
            'arch': self._arch,
            'image_path': self._img_json['path'],
            'memory': self._img_json['memory'],
            'snapshot': self._img_json['snapshot'],
            'qemu_extra_flags': self._img_json['qemu_extra_flags'],
        }

        render_template(context, template, script_path, executable=True)
Beispiel #13
0
    def _create_bootstrap(self, project_dir, config):
        """
        Create the S2E bootstrap script.
        """
        logger.info('Creating S2E bootstrap script')

        # The target arguments are specified using a format similar to the
        # American Fuzzy Lop fuzzer. Options are specified as normal, however
        # for programs that take input from a file, '@@' is used to mark the
        # location in the target's command line where the input file should be
        # placed. This will automatically be substituted with a symbolic file
        # in the S2E bootstrap script.
        parsed_args = [
            '${SYMB_FILE}' if arg == '@@' else arg
            for arg in config['target_args']
        ]

        target_path = config['target_path']
        context = {
            'creation_time': config['creation_time'],
            'target': os.path.basename(target_path) if target_path else None,
            'target_args': parsed_args,
            'sym_args': config['sym_args'],
            'target_bootstrap_template': self._bootstrap_template,
            'target_arch': config['target_arch'],
            'image_arch': config['image']['os']['arch'],
            'use_symb_input_file': config['use_symb_input_file'],
            'use_seeds': config['use_seeds'],
            'use_fault_injection': config['use_fault_injection'],
            'enable_pov_generation': config['enable_pov_generation'],
            'dynamically_linked': config['dynamically_linked'],
            'project_type': config['project_type'],
            'target_files':
            [os.path.basename(tf) for tf in config['target_files']],
            'modules': config['modules'],
            'processes': config['processes'],
        }

        template = 'bootstrap.sh'
        output_path = os.path.join(project_dir, template)
        render_template(context, template, output_path)
Beispiel #14
0
    def _generate_run_tests(self, ts_dir, test, script_template, options):
        ctx = {
            'test_dir': os.path.join(ts_dir, test),
            'project_name': options['name'],
            'creation_time': str(datetime.datetime.now())
        }

        run_tests_template = '%s/%s' % (test, script_template)
        run_tests = render_template(ctx, run_tests_template, templates_dir=ts_dir)

        run_tests_path = os.path.join(self.projects_path(options['name']), 'run-tests')
        with open(run_tests_path, 'w') as fp:
            fp.write(run_tests)

        st = os.stat(run_tests_path)
        os.chmod(run_tests_path, st.st_mode | stat.S_IEXEC)
Beispiel #15
0
def _read_config(test_root, s2e_images_root):
    cfg_file = os.path.join(test_root, 'config.yml')
    ctx = {'s2e_images': s2e_images_root}
    rendered = render_template(ctx, cfg_file, templates_dir='/')
    return yaml.safe_load(rendered)['test']
Beispiel #16
0
def _create_instructions(context):
    ret = render_template(context, 'instructions.txt')
    # Due to how templates work, there may be many useless new lines, remove
    # them here
    return re.sub(r'([\r\n][\r\n])+', r'\n\n', ret)
Beispiel #17
0
    def _get_instructions(self, config):
        instructions = render_template(config, 'instructions.txt')

        # Due to how templates work, there may be many useless new lines,
        # remove them here
        return re.sub(r'([\r\n][\r\n])+', r'\n\n', instructions)
Beispiel #18
0
    def handle(self, *args, **options):
        s2e_src_dir = self.env_path('source', 's2e', 'libs2eplugins', 'src')
        s2e_plugins_dir = self.env_path(s2e_src_dir, 's2e', 'Plugins')

        plugin_name = os.path.basename(options['plugin_name'][0])
        plugin_rel_dir = os.path.dirname(options['plugin_name'][0])
        author = options['author_name'] or _get_user_name()

        if not author:
            raise CommandError(
                'Could not determine your name. Run this command again and provide a '
                '"--author-name NAME" or set it with "git config user.name NAME"'
            )

        if not os.path.exists(s2e_src_dir):
            raise CommandError(
                f'{s2e_src_dir} does not exist. Make sure the source code is initialized properly.'
            )

        if plugin_rel_dir:
            if os.path.isabs(plugin_rel_dir):
                raise CommandError(
                    f'The plugin name must be relative to the {s2e_plugins_dir} directory.'
                )
            output_dir = os.path.join(s2e_plugins_dir, plugin_rel_dir)
            os.makedirs(output_dir, exist_ok=True)
        else:
            output_dir = s2e_plugins_dir

        logger.info('Creating plugin in %s', output_dir)

        cpp_plugin_path = os.path.join(output_dir, f'{plugin_name}.cpp')
        header_plugin_path = os.path.join(output_dir, f'{plugin_name}.h')

        if os.path.exists(cpp_plugin_path) or os.path.exists(
                header_plugin_path):
            if not options['force']:
                raise CommandError(
                    'The specified plugin already exists. Use --force to overwrite.'
                )

        context = {
            'author': {
                'name': author,
                'year': datetime.datetime.now().year
            },
            'plugin': {
                'name': plugin_name,
                'description': 'Describe what the plugin does here'
            },
            'use_guest_interface': options['use_guest_interface']
        }

        template_dir = os.path.join(DEFAULT_TEMPLATES_DIR, 'plugin_creation')
        render_template(context, 'plugin.cpp.template', cpp_plugin_path,
                        template_dir)
        render_template(context, 'plugin.h.template', header_plugin_path,
                        template_dir)

        # Update CMakeLists.txt
        rel_plugin_path = os.path.relpath(cpp_plugin_path, s2e_src_dir)
        s2e_plugins_makefile = self.env_path('source', 's2e', 'libs2eplugins',
                                             'src', 'CMakeLists.txt')
        ret = _inject_plugin_path(s2e_plugins_makefile, rel_plugin_path)

        with open(s2e_plugins_makefile, 'w', encoding='utf-8') as fp:
            fp.write(ret)

        logger.success(
            'Plugin successfully created. Please rebuild S2E and activate it in s2e-config.lua by adding these lines:\n'
            + f'\nadd_plugin("{plugin_name}")\n\n' +
            f'pluginsConfig.{plugin_name} = {{\n' +
            '  -- Set here your plugin configuration\n' + '}')