Esempio n. 1
0
File: scan.py Progetto: plkms/ikos
def scan_main(argv):
    # parse arguments
    opt = scan_parse_arguments(argv[1:])

    # setup colors and logging
    colors.setup(opt.color, file=log.out)
    log.setup(opt.log_level)

    # scan server
    server = ScanServer()
    server.daemon = True
    server.start()

    # setup environment variables
    os.environ['IKOS_SCAN_COLOR'] = 'yes' if colors.ENABLE else 'no'
    os.environ['IKOS_SCAN_LOG_LEVEL'] = opt.log_level
    os.environ['IKOS_SCAN_SERVER'] = 'http://localhost:%d' % server.port
    os.environ['PATH'] += os.path.pathsep + settings.BIN_DIR
    os.environ['CC'] = 'ikos-scan-cc'
    os.environ['CXX'] = 'ikos-scan-c++'
    os.environ['LD'] = 'ikos-scan-cc'

    # add -e to make commands, to avoid makefiles overriding CC/CXX/LD
    if os.path.basename(opt.args[0]) in ('make', 'gmake'):
        opt.args.insert(1, '-e')

    # run the build command
    rc = run(opt.args)

    # stop the scan server
    server.cancel()
    server.join()

    # skip binaries that have been removed
    binaries = [binary for binary in server.binaries
                if os.path.exists(binary['exe_path'])]

    if not binaries:
        printf('Nothing to analyze.\n')

    # analyze each binary
    for binary in binaries:
        exe_path = os.path.relpath(binary['exe_path'])
        bc_path = os.path.relpath(binary['bc_path'])

        printf('Analyze %s? [Y/n] ', colors.bold(exe_path))
        answer = sys.stdin.readline().strip().lower()

        if answer in ('', 'y', 'yes'):
            cmd = ['ikos', bc_path, '-o', '%s.db' % exe_path]
            log.info('Running %s' % colors.bold(command_string(cmd)))

            cmd = [sys.executable,
                   settings.ikos(),
                   bc_path,
                   '-o',
                   '%s.db' % exe_path,
                   '--color=%s' % opt.color,
                   '--log=%s' % opt.log_level]
            run(cmd)
Esempio n. 2
0
def check_output(cmd):
    ''' Run the given command and return the standard output, in bytes '''
    log.debug('Running %s' % command_string(cmd))

    try:
        return subprocess.check_output(cmd)
    except OSError as e:
        printf('error: %s: %s\n', cmd[0], e.strerror, file=sys.stderr)
        sys.exit(e.errno)
Esempio n. 3
0
def run(cmd):
    ''' Run the given command and return the exit code '''
    log.debug('Running %s' % command_string(cmd))

    try:
        proc = subprocess.Popen(cmd)
        rc = proc.wait()
    except OSError as e:
        printf('error: %s: %s\n', cmd[0], e.strerror, file=sys.stderr)
        sys.exit(e.errno)

    if rc != 0:
        sys.exit(rc)

    return rc