def obtain_corrupted_program():
     # Obtain a program and corrupt it
     instructions = ElfioTextDisassembleReader("data/helloworld_elfiodissasembly.disam").read()[1]
     collector = MetadataCollector()
     collector.collect(instructions)
     corruptor = RandomCorruptor(30.0, 5, True)
     corruptor.save_corrupted_program = False
     program = [CAPSInstruction(x.encoding, x.address) for x in instructions]
     program = corruptor.corrupt(from_instruction_list_to_dict(program))
     return instructions, program
示例#2
0
 def test_count(self):
     ARM_SIMPLE = os.path.join(os.path.dirname(__file__),
                               'data/helloworld_elfiodissasembly.disam')
     program = ElfioTextDisassembleReader(ARM_SIMPLE).read_instructions()
     fc = InstructionPartFrequencyCounter()
     fc.count(program)
     self.assertTrue(fc.reg_frequency[AReg.R0], 33)
     self.assertTrue(fc.reg_frequency[AReg.R1], 17)
     self.assertTrue(fc.cond_frequency[14], 115)
     self.assertTrue(fc.opcode_frequency[23], 9)
示例#3
0
 def test_read(self):
     fns, instructions = ElfioTextDisassembleReader(
         "data/helloworld_elfiodissasembly.disam").read()
     # Some smoke to start with
     for inst in instructions:
         self.assertFalse(inst.ignore)
     self.assertEqual(instructions[0].encoding, 0xe92d4008)  # push {r3, lr}
     self.assertEqual(instructions[2].encoding,
                      0xe52de004)  # str lr, [sp, #-4]!
     self.assertEqual(instructions[len(instructions) - 2].encoding,
                      0xe8bd8800)  # pop {fp, pc}
     self.assertEqual(instructions[len(instructions) - 1].encoding,
                      0xe92d4008)  # push {r3, lr}
     self.assertGreater(len(fns), 10)
     self.assertEqual(len(instructions), 143)
示例#4
0
    def test_corrupted_collect(self):
        # TODO: This is an smoke test
        # Load the program
        instructions = ElfioTextDisassembleReader(
            self.ASM_LONG_PATH).read_instructions()

        # Corrupt the program
        ll = len(instructions)
        packet_count = ll / 32
        cc = PacketCorruptor(packet_count, ll, packets_lost=[3])
        instructions = cc.corrupt(from_instruction_list_to_dict(instructions))

        # Collect some metrics in the corrupted program
        c = CorruptedProgramMetadataCollector()
        c.collect(instructions)

        # Some very soft tests
        self.assertGreater(len(c.address_with_cond), 0)
        self.assertGreater(len(c.address_with_reg), 0)
        self.assertGreater(len(c.address_with_op), 0)
示例#5
0
import os
import random

from semantic_codec.architecture.disassembler_readers import ElfioTextDisassembleReader
from semantic_codec.distributed.qos_functions import DistributedQoS

_qos = DistributedQoS("10.0.0.83:5555", 'basic_math')

ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/basicmath_small.disam')
program = ElfioTextDisassembleReader(ARM_SIMPLE).read_instructions()

#converts the text into a list of characters
#RLE
final = "" #string of encoded text
last_char = program[0]
counter = 0 #keeps track of i's location
first_of_a_kind = 0 #keeps track of the location of where the set started
for i in program:
    if i != last_char: #if a different character was found
        final += str(counter - first_of_a_kind) + last_char
        first_of_a_kind = counter
    counter += 1
    last_char = i

final_s = list(final) #Replacements begin
counter = 0 #keeps track of how many times a set of characters has been replaced (to skip the characters that have been replaced already)
final_result = ""
for i in range(0, (len(final_s) - 1)):
    if i%2 == 0: #makes sure that i is an even number (this means that an integer value will be found in the list)
        if counter == 0:
            try:
    # Check Arguments
    expected_num_args = 3
    if len(sys.argv) != expected_num_args:
        print('Usage: <source_file> <output_file>')
        exit(1)

    # Read the file to be stored without the loss packets
    input_file = os.path.realpath(os.path.join(os.getcwd(), sys.argv[1]))
    try:
        f = open(input_file)
    except:
        print('Unable to find source: {}'.format(input_file))
        exit(1)
    # Parse the dissasembly text
    try:
        original_program = ElfioTextDisassembleReader(
            input_file).read_functions()
        original_program, fns = from_functions_to_list_and_addr(
            original_program)
        original_program = [inst.encoding for inst in original_program]
    except:
        print('Unable to parse source file, is it a valid dissasembly?')
        exit(1)

    # Interleave the file using the SP algorithm
    bits_per_interlave = 2
    packet_size = 16
    packet_count = len(
        original_program
    ) * 4 / packet_size  # <- Total bytes in data divided by packet_size

    # Build the 2D interleave order
if __name__ == "__main__":
    use_packets = True
    use_file = False
    recovered_program = None
    corruptor = None

    # ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/dissasembly.armasm')
    #ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/helloworld_elfiodissasembly.txt')
    #ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/fft.disam')
    #ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/dijkstra_small.disam')
    #ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/crc32.disam')
    #ARM_SIMPLE = os.path.join(os.path.dirname(__file__), 'tests/data/bitcount.disam')
    ARM_SIMPLE = os.path.join(os.path.dirname(__file__),
                              'tests/data/basicmath_small.disam')
    #original_program = TextDisassembleReader(ARM_SIMPLE).read_functions()
    original_program = ElfioTextDisassembleReader(ARM_SIMPLE).read_functions()

    if not use_file:
        if use_packets:
            ll = 0
            for f in original_program:
                ll += len(f.instructions)
            packet_count = ll / 32
            lost = [3]
            print(
                '[INFO:] Program Size: {} bytes -- Loss: {} -- Packet count: {}'
                .format(ll * 4, 16 * len(lost) * 2, packet_count))
            corruptor = PacketCorruptor(packet_count, ll, packets_lost=lost)
        else:
            corruptor = RandomCorruptor(30.0, 3, True)
    else: