Ejemplo n.º 1
0
def do_test_plasmasm(filename, quick=False, **kargs):
    print("Testing %s" % filename)
    obj_v1 = mkstemp(suffix='.o')
    asm_v2 = mkstemp(suffix='.s')
    obj_v2 = mkstemp(suffix='.o')
    # Create new asm
    from plasmasm.analyze_file import File
    symbols = File().from_filename(filename, **kargs)
    hook_for_as_bugs(symbols)
    symbols.to_asm(output_filename=asm_v2)
    # Making object files
    if symbols.arch.CPU == 'X64': compile = ['gcc', '-c', '-o']
    else: compile = ['gcc', '-m32', '-c', '-o']
    spawn(compile + [obj_v2, asm_v2])
    os.unlink(asm_v2)
    if filename.endswith('.s'):
        cmd_mkobjv1 = compile + [obj_v1, filename]
    elif filename.endswith('.o'):
        cmd_mkobjv1 = ['cp', filename, obj_v1]
    else:
        TODO
    spawn(cmd_mkobjv1)
    # Do comparison
    diff = do_compare(obj_v2, obj_v1, quick=quick)
    os.unlink(obj_v1)
    os.unlink(obj_v2)
    if diff:
        for line in diff:
            print_red(line[:-1])
        print("Failed for %s" % filename)
        sys.exit(1)
Ejemplo n.º 2
0
def test_io(file, suffix, kargs):
    fd = open("non_regression/" + file, "rb")
    raw = fd.read()
    fd.close()
    fd = open("non_regression/" + file + "." + suffix, "r")
    res = fd.read()
    fd.close()
    pool = File().from_raw(raw, rw=True, **kargs)
    assert pool.to_asm() == res
Ejemplo n.º 3
0
def test_io(file, suffix, kargs):
    fd = open("non_regression/" + file, "rb")
    raw = fd.read()
    fd.close()
    fd = open("non_regression/" + file + "." + suffix, "r")
    res = fd.read()
    fd.close()
    pool = File().from_raw(raw, rw=True, **kargs)
    if "dead" in kargs:
        from staticasm.dead_registers import analyze_dead
        analyze_dead(pool)
        pool.arch.long_display = True
    assert pool.to_asm() == res
Ejemplo n.º 4
0
def test_macho_reloc():
    fd = open("non_regression/macho_reloc.o", "rb")
    raw = fd.read()
    fd.close()
    try:
        pool = File().from_raw(raw, rw=True)
        assert 0 == 'Should raise ValueError'
    except ValueError:
        e = sys.exc_info()[1]
        assert str(e) == "Unknown Mach-O reloc 5;2;0 on cpu X86_64"
Ejemplo n.º 5
0
def test_no_backend():
    fd = open("non_regression/basic_x86_linux.att.s", "rb")
    raw = fd.read()
    fd.close()
    try:
        pool = File().from_raw(raw, rw=True, cpu="/FOO")
        assert 0 == 'Should raise ValueError'
    except ValueError:
        e = sys.exc_info()[1]
        assert str(e) == "No FOO backend in I386-att"
Ejemplo n.º 6
0
def test_section():
    fd = open("non_regression/error_section.s", "rb")
    raw = fd.read()
    fd.close()
    try:
        pool = File().from_raw(raw, rw=True)
        assert 0 == 'Should raise ValueError'
    except ValueError:
        e = sys.exc_info()[1]
        assert str(e) == "Unknown section 'FOO'"
Ejemplo n.º 7
0
 def run(self, input, output):
     output += '.s'
     if '-v' in sys.argv: print("change %r -> %r" % (input, output))
     from plasmasm.analyze_file import File
     options = {'rw': True, 'dead': True}
     for opt in self.param[1:]:
         if opt.startswith('-c'): options['cpu'] = opt[2:]
         else: print("Invalid option %r" % opt)
     symbols = File().from_filename(input, **options)
     if 'dead' in options:
         from staticasm.pic_tracking import analyze_PIC
         analyze_PIC(symbols)
         from staticasm.stack_tracking import analyze_stack
         analyze_stack(symbols)
         from staticasm.dead_registers import analyze_dead
         analyze_dead(symbols)
     change_ret(symbols)
     symbols.to_asm(output_filename=output)
     return output
Ejemplo n.º 8
0
def test_file_no_quotes():
    global log_history
    log_history = []
    fd = open("non_regression/error_file_no_quotes.s", "rb")
    raw = fd.read()
    fd.close()
    pool = File().from_raw(raw, rw=True)
    assert log_history == [
        ('error', ('%s name %r is not between quotes', 'file', 'a00.c'), {}),
        ('error', ('%s name %r is not between quotes', 'ident',
                   'GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3'), {}),
    ]
Ejemplo n.º 9
0
def test_io(file, suffix, kargs):
    pool = File().from_filename("non_regression/" + file, **kargs)
    fd = open("non_regression/" + file + "." + suffix, "r")
    res = fd.read()
    fd.close()
    from plasmasm import constants
    if "rw" in kargs:
        # Same output as disass.py -rLWT
        pool.arch.set_asm_format('att_syntax')
        pool.arch.long_display = True
        constants.Constant.out_format = None
    else:
        # Same output as disass.py -r
        pool.arch.set_asm_format('raw')
        constants.Constant.out_format = 'raw'
    if "pic" in kargs:
        from staticasm.pic_tracking import analyze_PIC
        analyze_PIC(pool)
        from staticasm.stack_tracking import analyze_stack
        analyze_stack(pool)
    if "dead" in kargs:
        from staticasm.dead_registers import analyze_dead
        analyze_dead(pool)
    rep = [_.display() for _ in pool.blocs]
    rep += [
        _.display() for _ in sorted(
            [s for s in pool.symbols if not s in pool.bloc_set],
            key=lambda x: (getattr(x, 'section', ''), getattr(x, 'address', 0),
                           getattr(x, 'name', None)))
    ]
    rep += [
        str(tuple((s, pool.sections.asm_name[s])))
        for s in sorted(pool.sections.asm_name)
    ]
    rep += [
        ','.join(
            ["%s:%r" % (_, pool.meta[_]) for _ in reversed(sorted(pool.meta))])
    ]
    rep += ['']
    rep = '\n'.join(rep)
    if sys.version_info[0] == 2 and sys.version_info[1] <= 3:
        # long are displayed differently
        import re
        rep = re.sub('([0-9]+)L', '\\1', rep)
    assert rep == res
Ejemplo n.º 10
0
def test_empty():
    fd = open("non_regression/error_empty.s", "rb")
    raw = fd.read()
    fd.close()
    pool = File().from_raw(raw, rw=True)
    assert pool.file_type == 'EMPTY'
Ejemplo n.º 11
0
# Copyright (C) 2011-2020 Airbus, [email protected]
try:
    import pytest
except:
    from run_tests import pytest
import sys, os.path
basedir = os.path.dirname(os.path.dirname(__file__))
sys.path.append(basedir)
from plasmasm.analyze_file import File
# To be able to import elfesteem in the parent directory, with python3
sys.path.append(os.path.dirname(basedir) + '/elfesteem')

pool = File()
i_list = []

from plasmasm.arch import I386_MIASM


def i_x86_miasm(s):
    I386_MIASM.Instruction.set_asm_format('intel_syntax noprefix')
    return I386_MIASM.Instruction(pool).from_txt(s, asm_format='att_syntax')


def i_x86_amoco(s):
    I386_AMOCO.Instruction.set_asm_format('intel_syntax noprefix')
    return I386_AMOCO.Instruction(pool).from_txt(s)


def i_x64_amoco(s):
    return X64_AMOCO.Instruction(pool).from_txt(s)