Esempio n. 1
0
def _compile(code, werror, flags, libs):
    digest = md5sumhex(code + str(werror) + str(flags) + str(libs))
    if digest in __cache:
        return __cache[digest]
    sopath = os.path.join(__tempdir, digest + '.so')
    try:
        if os.path.exists(sopath):
            return CDDL(sopath)
    except:
        pass
    cpath = os.path.join(__tempdir, digest + '.c')
    with open(cpath, 'w') as f:
        f.write(code)
    flags += ['-fPIC', '-shared', '-O3', '-march=native', '-mtune=native',
              '-Wall']
    if werror:
        flags.append('-Werror')
    cmd = ['gcc'] + flags + ['-o', sopath, cpath] + libs
    p = Popen(cmd, stderr = PIPE)
    _, s = p.communicate()
    s = s.replace(cpath + ':', '').replace(cpath, '')
    if p.returncode <> 0:
        log.error('GCC error (%s):' % cpath)
        log.trace(s)
        sys.exit(p.returncode)
    elif s <> '':
        log.warning('GCC warning (%s):' % cpath)
        log.trace(s)
    return CDLL(sopath)
Esempio n. 2
0
def _compile(code, werror, flags, libs):
    digest = md5sumhex(code + str(werror) + str(flags) + str(libs))
    if digest in __cache:
        return __cache[digest]
    sopath = os.path.join(__tempdir, digest + '.so')
    try:
        if os.path.exists(sopath):
            return CDDL(sopath)
    except:
        pass
    cpath = os.path.join(__tempdir, digest + '.c')
    with open(cpath, 'w') as f:
        f.write(code)
    flags += [
        '-fPIC', '-shared', '-O3', '-march=native', '-mtune=native', '-Wall'
    ]
    if werror:
        flags.append('-Werror')
    cmd = ['gcc'] + flags + ['-o', sopath, cpath] + libs
    p = Popen(cmd, stderr=PIPE)
    _, s = p.communicate()
    s = s.replace(cpath + ':', '').replace(cpath, '')
    if p.returncode <> 0:
        log.error('GCC error (%s):' % cpath)
        log.trace(s)
        sys.exit(p.returncode)
    elif s <> '':
        log.warning('GCC warning (%s):' % cpath)
        log.trace(s)
    return CDLL(sopath)
Esempio n. 3
0
        def wrapper(*args, **kwargs):
            import os
            from cPickle import load, dump
            sig = (str(args), str(kwargs))
            t = None
            fname = None
            file_miss = False
            dict_miss = False

            try:
                if use_mem:
                    if sig in cache:
                        t, val = cache[sig]
                    else:
                        dict_miss = True

                if t == None and use_file:
                    digest = pwn.md5sumhex(
                        '.'.join((f.__module__, f.__name__) + sig))
                    fname = os.path.join(_tempdir(), digest)
                    try:
                        if os.path.exists(fname):
                            with open(fname) as fd:
                                t, val = load(fd)
                        else:
                            file_miss = True
                    except:
                        file_miss = True

                if t == None:
                    try:
                        t, val = _TYPE_VALUE, f(*args, **kwargs)
                    except Exception as e:
                        t, val = _TYPE_EXCEPTION, e
                        raise

                if t == _TYPE_VALUE:
                    return val
                else:
                    raise val
            finally:
                if t != None:
                    if dict_miss:
                        cache[sig] = (t, val)
                    if file_miss:
                        try:
                            with open(fname, 'w') as fd:
                                dump((t, val), fd)
                        except:
                            pass
Esempio n. 4
0
        def wrapper(*args, **kwargs):
            import os
            from cPickle import load, dump
            sig = (str(args), str(kwargs))
            t = None
            fname = None
            file_miss = False
            dict_miss = False

            try:
                if use_mem:
                    if sig in cache:
                        t, val = cache[sig]
                    else:
                        dict_miss = True

                if t == None and use_file:
                    digest = pwn.md5sumhex('.'.join((f.__module__, f.__name__) + sig))
                    fname = os.path.join(_tempdir(), digest)
                    try:
                        if os.path.exists(fname):
                            with open(fname) as fd:
                                t, val = load(fd)
                        else:
                            file_miss = True
                    except:
                        file_miss = True

                if t == None:
                    try:
                        t, val = _TYPE_VALUE, f(*args, **kwargs)
                    except Exception as e:
                        t, val = _TYPE_EXCEPTION, e
                        raise

                if t == _TYPE_VALUE:
                    return val
                else:
                    raise val
            finally:
                if t != None:
                    if dict_miss:
                        cache[sig] = (t, val)
                    if file_miss:
                        try:
                            with open(fname, 'w') as fd:
                                dump((t, val), fd)
                        except:
                            pass