How to use the benchexec.util.write_file function in BenchExec

To help you get started, we’ve selected a few BenchExec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sosy-lab / benchexec / benchexec / tablegenerator / test_integration / __init__.py View on Github external
def assert_file_content_equals(self, content, file):
        if OVERWRITE_MODE:
            benchexec.util.write_file(content, *file)
        else:
            self.assertMultiLineEqual(content, benchexec.util.read_file(*file))
github sosy-lab / benchexec / benchexec / oomhandler.py View on Github external
super(KillProcessOnOomThread, self).__init__()
        self.name = "KillProcessOnOomThread-" + self.name
        self._finished = threading.Event()
        self._pid_to_kill = pid_to_kill
        self._cgroups = cgroups
        self._callback = callbackFn

        cgroup = cgroups[MEMORY]  # for raw access
        ofd = os.open(os.path.join(cgroup, "memory.oom_control"), os.O_WRONLY)
        try:
            # Important to use CLOEXEC, otherwise the benchmarked tool inherits
            # the file descriptor.
            self._efd = _libc.eventfd(0, _EFD_CLOEXEC)

            try:
                util.write_file(
                    "{} {}".format(self._efd, ofd), cgroup, "cgroup.event_control"
                )

                # If everything worked, disable Kernel-side process killing.
                # This is not allowed if memory.use_hierarchy is enabled,
                # but we don't care.
                try:
                    os.write(ofd, "1".encode("ascii"))
                except OSError as e:
                    logging.debug(
                        "Failed to disable kernel-side OOM killer: error %s (%s)",
                        e.errno,
                        e.strerror,
                    )
            except EnvironmentError as e:
                os.close(self._efd)
github sosy-lab / benchexec / benchexec / container.py View on Github external
"""Create a minimal system configuration for use in a container.
    @param basedir: The directory where the configuration files should be placed (bytes)
    @param mountdir: The base directory of the mount hierarchy in the container (bytes).
    @param dir_modes: All directory modes in the container.
    """
    # If overlayfs is not used for /etc, we need additional bind mounts
    # for files in /etc that we want to override, like /etc/passwd
    symlinks_required = determine_directory_mode(dir_modes, b"/etc") != DIR_OVERLAY

    etc = os.path.join(basedir, b"etc")
    if not os.path.exists(etc):
        os.mkdir(etc)

    for file, content in CONTAINER_ETC_FILE_OVERRIDE.items():
        # Create "basedir/etc/file"
        util.write_file(content, etc, file)
        if symlinks_required:
            # Create bind mount to "mountdir/etc/file"
            make_bind_mount(
                os.path.join(etc, file),
                os.path.join(mountdir, b"etc", file),
                private=True,
            )

    os.symlink(b"/proc/self/mounts", os.path.join(etc, b"mtab"))
    # Bind bounds for symlinks are not possible, so do nothing for "mountdir/etc/mtab".
    # This is not a problem because most systems have the correct symlink anyway.

    if not os.path.isdir(mountdir.decode() + CONTAINER_HOME):
        logging.warning(
            "Home directory in container should be %(h)s but this directory "
            "cannot be created due to directory mode of parent directory. "
github sosy-lab / cpachecker / scripts / benchmark / vcloud.py View on Github external
def execute_benchmark(benchmark, output_handler):
    if not _justReprocessResults:
        # build input for cloud
        (cloudInput, numberOfRuns) = getCloudInput(benchmark)
        if benchmark.config.debug:
            cloudInputFile = os.path.join(benchmark.log_folder, "cloudInput.txt")
            benchexec.util.write_file(cloudInput, cloudInputFile)
            output_handler.all_created_files.add(cloudInputFile)
        meta_information = json.dumps(
            {
                "tool": {
                    "name": benchmark.tool_name,
                    "revision": benchmark.tool_version,
                    "benchexec-module": benchmark.tool_module,
                },
                "benchmark": benchmark.name,
                "timestamp": benchmark.instance,
                "generator": "benchmark.vcloud.py",
            }
        )

        # install cloud and dependencies
        ant = subprocess.Popen(
github sosy-lab / benchexec / benchexec / cgroups.py View on Github external
def set_value(self, subsystem, option, value):
        """
        Write the given value for the given subsystem.
        Do not include the subsystem name in the option name.
        Only call this method if the given subsystem is available.
        """
        assert subsystem in self
        util.write_file(
            str(value), self.per_subsystem[subsystem], subsystem + "." + option
        )
github sosy-lab / benchexec / benchexec / container.py View on Github external
util.write_file(uid_map, proc_child, "uid_map")
    except OSError as e:
        logging.warning("Creating UID mapping into container failed: %s", e)

    try:
        util.write_file("deny", proc_child, "setgroups")
    except OSError as e:
        # Not all systems have this file (depends on the kernel version),
        # but if it does not exist, we do not need to write to it.
        if e.errno != errno.ENOENT:
            logging.warning("Could not write to setgroups file in /proc: %s", e)

    try:
        # map gid internally to our gid externally
        gid_map = "{0} {1} 1".format(gid, parent_gid)
        util.write_file(gid_map, proc_child, "gid_map")
    except OSError as e:
        logging.warning("Creating GID mapping into container failed: %s", e)
github sosy-lab / benchexec / benchexec / filewriter.py View on Github external
Add content to the represented file.
        If keep is False, the new content will be forgotten during the next call
        to this method.
        """
        content = self.__content + newContent
        if keep:
            self.__content = content

        if self.__needsRewrite:
            """
            Replace the content of the file.
            A temporary file is used to avoid loss of data through an interrupt.
            """
            tmpFilename = self.filename + ".tmp"

            util.write_file(content, tmpFilename)

            os.rename(tmpFilename, self.filename)
        else:
            with open(self.filename, "a") as file:
                file.write(newContent)

        self.__needsRewrite = not keep
github sosy-lab / benchexec / benchexec / filewriter.py View on Github external
def __init__(self, filename, content):
        """
        The constructor of FileWriter creates the file.
        If the file exist, it will be OVERWRITTEN without a message!
        """

        self.filename = filename
        self.__needsRewrite = False
        self.__content = content

        # Open file with "w" at least once so it will be overwritten.
        util.write_file(content, self.filename)
github sosy-lab / cpachecker / scripts / benchmark / appengine.py View on Github external
def saveResult(self, run, task):
        taskKey = task['key']
        log_file = run['logFile']
        headers = {'Accept':'text/plain'}

        fileNames = []
        for file in task['files']:
            fileNames.append(file['name'])

        try:
            util.write_file(json.dumps(task), log_file+'.stdOut')
        except:
            logging.debug('Could not save task '+taskKey)

        statisticsProcessed = False
        if APPENGINE_SETTINGS['statisticsFileName'] in fileNames:
            try:
                uri = self.benchmark.config.appengineURI+'/tasks/'+taskKey+'/files/' + APPENGINE_SETTINGS['statisticsFileName']
                request = urllib2.Request(uri, headers=headers)
                response = urllib2.urlopen(request).read().decode()
                util.write_file(response, log_file)
                statisticsProcessed = True
            except:
                statisticsProcessed = False
                logging.exception('Could not save statistics of'+taskKey)
        else:
            statisticsProcessed = True