示例#1
0
    def instantiate(self, setup):
        nnsimTestBench.instantiate(self, setup)

        self.trace_generator            = ws_PE_trace_generator()
        self.traces_stats               = 'show'
        self.recorder                   = nnsimRecorder() if self.traces_stats == 'show' else None
        self.generated_trace            = True
        
        # bookkeeping variables
        self.result                     = []
        # ---------------------------------------------------------------------
        # IO channels for onchip and offchip communications
        # ---------------------------------------------------------------------
        # -> input channels for sending the input trace data to the GLBs
        # smartbuffer needs channels specified as list format
        self.weights_in_chn             = Channel()
        self.ifmap_chn                  = Channel()
        self.psum_in_chn                = Channel()
        # -> output channel for reciving calculated psum from the chip
        self.psum_out_chn               = Channel()
        
        pe_setup = {'weights_data_in_chn': self.weights_in_chn,\
                    'ifmap_data_in_chn': self.ifmap_chn,\
                    'psum_data_in_chn': self.psum_in_chn,\
                    'psum_data_out_chn': self.psum_out_chn}
        pe_setup.update(self.arch['PE'])
        self.dut  = PE(pe_setup) 
def feature_extraction(location, csv_file):
    csv_f = open(csv_file, "a")
    c = csv.writer(csv_f)
    for root, dirs, files in os.walk(location):
        for f in sorted(files):
            try:
                file_loc = os.path.join(root, f)
                pe = PE(file_loc, name=f, strings=True)
                features = pe.fe_values()
                c.writerow(features)
                csv_f.flush()
            except:
                print "Invalid file."
                pass
示例#3
0
    def _apply_deadline_cst(self, mapping):
        csts = []
        PE.init_apps_pe_by_mapping(self.app_list, mapping, self.pe_list)
        for idx, app in enumerate(self.app_list):
            if config.app_to_cst_dict[idx] == 'Deadline':
                csts.append(Deadline(self.app_list, app, self.pe_list))

        # if there is deadline constraint
        for idx, cst in enumerate(csts):
            penalty = cst.constraint_function(mapping)[0]
            if penalty != 0:
                self._one_bit_flip_local_optimization(mapping, penalty, cst)
                return False

        return True
def initilize_csv(location, csv_file):
    csv_f = open(csv_file, "a")
    c = csv.writer(csv_f)
    for root, dirs, files in os.walk(location):
        for f in sorted(files):
            try:
                file_loc = os.path.join(root, f)
                pe = PE(file_loc, name=f, strings=True)
                header = pe.fe_methods()
                c.writerow(header)
                csv_f.flush()
                csv_f.close()
                return
            except:
                print "Invalid file."
                pass
示例#5
0
 def _apply_cpu_util_cst(self, mapping):
     if config.CPU_UTILIZATION != 100:
         PE.init_apps_pe_by_mapping(self.app_list, mapping, self.pe_list)
         cpu_util = CPU_utilization(self.app_list, None, self.pe_list)
         while cpu_util.constraint_function(mapping)[0] != 0:  # (penalty, util)
             for idx in range(len(mapping)):
                 l = self.layer_list[idx]
                 if not l.is_start_node and not l.is_end_node and l.pe.type == PEType.CPU:
                     best = float("inf")
                     best_pe_idx = None
                     for pe in self.pe_list:
                         target = l.time_list[pe.get_idx()]
                         if target < best:
                             best = target
                             best_pe_idx = pe.get_idx()
                     mapping[idx] = best_pe_idx
示例#6
0
    def calculate_fitness(self, mapping):
        PE.init_apps_pe_by_mapping(self.app_list, mapping, self.pe_list)

        objs = []
        for obj in self.objs:
            obj_temp = obj.objective_function(mapping)
            objs.append(obj_temp[0])

        for idx, cst in enumerate(self.csts):
            if cst is not None and isinstance(cst, Deadline):
                cst_temp = cst.constraint_function(mapping)
                objs[idx] += cst_temp[0]
            elif cst is not None:
                cst_temp = cst.constraint_function(mapping)
                objs = [v + cst_temp[0] for v in objs]

        return tuple(objs)
示例#7
0
    def _move_large2small(self, mapping):
        large = -float("inf")
        small = float("inf")
        large_pe = None
        small_pe = None
        PE.init_apps_pe_by_mapping(self.app_list, mapping, self.pe_list)
        for idx in range(0, self.num_pe):
            if self.cur_map_stat_per_pe[self.pe_list[idx].name] > large:
                large = self.cur_map_stat_per_pe[self.pe_list[idx].name]
                large_pe = self.pe_list[idx]
            if self.cur_map_stat_per_pe[self.pe_list[idx].name] < small:
                small = self.cur_map_stat_per_pe[self.pe_list[idx].name]
                small_pe = self.pe_list[idx]

        for idx in range(0, self.num_layer):
            if mapping[idx] == large_pe.get_idx():
                mapping[idx] = small_pe.get_idx()
                self.cur_map_stat_per_pe[large_pe.name] -= self.layer_list[idx].time_list[large_pe.get_idx()]
                self.cur_map_stat_per_pe[small_pe.name] += self.layer_list[idx].time_list[small_pe.get_idx()]
                break
示例#8
0
 def __init__(self, project, sample):
     from pe import PE
     self.project = project
     self.sample = sample
     self.pe = PE(sample.file)
示例#9
0
文件: test.py 项目: logan-m-lamb/pev
import os
from pe import PE
import distorm3

# distorm3 things
offset = 0
dt = distorm3.Decode32Bits

f = open('print.exe', 'rb')
o = PE(f)

# distorm3 
f.seek(o.rva2ofs(o.entrypoint))
code = f.read()

iterable = distorm3.DecomposeGenerator(offset, code, dt, \
    distorm3.DF_RETURN_FC_ONLY | distorm3.DF_STOP_ON_FLOW_CONTROL)

print iterable.next()
示例#10
0
文件: flow2.py 项目: logan-m-lamb/pev
            offset = workRva

        # read the next instruction
        code = f.read()
        iterable = distorm3.DecomposeGenerator(offset, code, dt, \
            distorm3.DF_RETURN_FC_ONLY | distorm3.DF_STOP_ON_FLOW_CONTROL)
        inst = iterable.next()

        # if we've encountered a loop exit
        if hasAddr(inst.address):
            print 'Found a loop!', hex(inst.address), inst, inst.flowControl
            return
        workRva = inst.address

if __name__ == '__main__':
    f = PE(open('print.exe', 'rb'))
    print 'ImageBase', f.imagebase
    print 'entrypoint ofs', hex(f.rva2ofs(f.entrypoint))
    getExterns(f)

    # some datastructure of interest
    workQ = collections.deque()

    # distorm3 
    dt = distorm3.Decode32Bits

    # inst1
    f.seek(f.rva2ofs(f.entrypoint))
    code = f.read()

    workQ.append(f.entrypoint)
示例#11
0
class Crafter(object):
    """ this class craft the objects and store them in the database returning
    dicts (or db entries) """
    def __init__(self, project, sample):
        from pe import PE
        self.project = project
        self.sample = sample
        self.pe = PE(sample.file)

    def mutation_sectionchar_rwx(self):
        self.pe.modify_section_characteristics_rwx()

    def mutation_sectionname_random(self):
        self.pe.modify_section_names(rand=True)

    def mutation_sectionname_randomdot(self):
        self.pe.modify_section_names(rand=True, with_dot=True)

    def mutation_sectionname_infer(self):
        self.pe.modify_section_names()

    def mutation_code_entryret(self):
        self.pe.patch_code(va=self.pe.OPTIONAL_HEADER.AddressOfEntryPoint)

    def update_checksum(self):
        self.pe.update_checksum()

    def _craft_all(self):
        results = {}
        for mutate_funcs in self.mutations:

            name = os.path.join(self.outdir, self.filename)
            mut_names = '_'.join([n for n, f in mutate_funcs if f is not None])
            if mut_names:
                name += '_' + mut_names

            for n, f in mutate_funcs:
                if f is not None:
                    f(self)

            #verify if checksum is called it should be last
            if 'checksum' in name and name.index(
                    'checksum') != len(name) - len('checksum'):
                raise Exception("Checksum was not the last mutation called")

            self.workpe.write(name)
            with open(name) as fin:
                sha = sha256(fin.read()).hexdigest()
            results[sha] = [n for n, f in mutate_funcs]  # data about mutation!

        return results
示例#12
0
    def instantiate(self, arr_y,
            input_chn, output_chn,
            block_size, num_nonzero,
            ifmap_glb_depth, psum_glb_depth, weight_glb_depth):
        # PE static configuration (immutable)
        self.name = 'chip'
        #self.arr_x = arr_x
        self.arr_y = arr_y
        self.block_size = block_size
        self.num_nonzero = num_nonzero
        
        self.stat_type = 'show'

        # Instantiate DRAM IO channels
        self.input_chn = input_chn
        self.output_chn = output_chn

        # Instantiate input deserializer and output serializer
        self.ifmap_wr_chn = Channel()
        self.psum_wr_chn = Channel()
        self.weights_wr_chn = Channel()
        self.deserializer = InputDeserializer(self.input_chn, self.ifmap_wr_chn,
                self.weights_wr_chn, self.psum_wr_chn, arr_y,
                block_size, num_nonzero)

        self.psum_output_chn = Channel()
        self.serializer = OutputSerializer(self.output_chn, self.psum_output_chn)

        # Instantiate GLB and GLB channels
        self.ifmap_rd_chn = Channel(3)
        #self.ifmap_glb = IFMapGLB(self.ifmap_wr_chn, self.ifmap_rd_chn, arr_y,
        #        ifmap_glb_depth, block_size, num_nonzero)

        self.psum_rd_chn = Channel(3)
        self.psum_noc_wr_chn = Channel()
        self.psum_glb = PSumGLB(self.psum_wr_chn, self.psum_noc_wr_chn, self.psum_rd_chn,
                psum_glb_depth, block_size, num_nonzero)

        self.weights_rd_chn = Channel()
        #self.weights_glb = WeightsGLB(self.weights_wr_chn, self.weights_rd_chn, weight_glb_depth, block_size)

        self.ifmap_weights_glb = IFMapWeightsGLB(self.ifmap_wr_chn, self.ifmap_rd_chn,\
            self.weights_wr_chn, self.weights_rd_chn, arr_y, ifmap_glb_depth,\
            weight_glb_depth, block_size, num_nonzero)
        # PE Array and local channel declaration
        self.pe_array = ModuleList()
        self.pe_ifmap_chns = ModuleList()
        self.pe_filter_chns = ModuleList()
        self.pe_psum_in_chns = ModuleList()
        self.pe_psum_out_chns = ModuleList()

        # Actual array instantiation
        for y in range(self.arr_y):
            self.pe_array.append(ModuleList())
            self.pe_ifmap_chns.append(ModuleList())
            self.pe_filter_chns.append(ModuleList())
            self.pe_psum_in_chns.append(ModuleList())
            self.pe_psum_out_chns.append(ModuleList())
            for x in range(1):
                self.pe_ifmap_chns[y].append(Channel(32))
                self.pe_filter_chns[y].append(Channel(32))
                self.pe_psum_in_chns[y].append(Channel(32))
                self.pe_psum_out_chns[y].append(Channel(32))
                self.pe_array[y].append(
                    PE(x, y,
                        self.pe_ifmap_chns[y][x],
                        self.pe_filter_chns[y][x],
                        self.pe_psum_in_chns[y][x],
                        self.pe_psum_out_chns[y][x]
                    )
                )

        # Setup NoC to deliver weights, ifmaps and psums
        self.filter_noc = WeightsNoC(self.weights_rd_chn, self.pe_filter_chns, block_size)
        self.ifmap_noc = IFMapNoC(self.ifmap_rd_chn, self.pe_ifmap_chns)
        self.psum_rd_noc = PSumRdNoC(self.psum_rd_chn, self.pe_psum_in_chns, self.arr_y, block_size)
        self.psum_wr_noc = PSumWrNoC(self.pe_psum_out_chns, self.psum_noc_wr_chn, self.psum_output_chn, self.arr_y, block_size)
示例#13
0
文件: flow.py 项目: logan-m-lamb/pev
import os
import sys
from pe import PE
import distorm3

encountered = list()
def hasAddr(addr):
    for r in encountered:
        if addr in r:
            return True
    return False

if __name__ == '__main__':
    pe = PE(open('print.exe', 'rb'))
    print 'ImageBase', pe.imagebase
    print 'entrypoint ofs', hex(pe.rva2ofs(pe.entrypoint))

    # distorm3 
    dt = distorm3.Decode32Bits

    # inst1
    pe.seek(pe.rva2ofs(pe.entrypoint))
    code = pe.read()

    offset = pe.entrypoint
    iterable = distorm3.DecomposeGenerator(offset, code, dt, \
        distorm3.DF_RETURN_FC_ONLY | distorm3.DF_STOP_ON_FLOW_CONTROL)

    inst = iterable.next()
    
    # add what we've encountered
示例#14
0
def build_PE(in_dim,
             out_dim,
             name='BNN',
             hidden_dims=(200, 200, 200),
             num_networks=7,
             num_elites=5,
             loss='MSPE',
             activation='swish',
             output_activation=None,
             decay=1e-4,
             lr=1e-3,
             lr_decay=None,
             decay_steps=None,
             use_scaler_in=False,
             use_scaler_out=False,
             clip_loss=False,
             kl_cliprange=0.1,
             max_logvar=.5,
             min_logvar=-6,
             session=None):
    """
	Constructs a tf probabilistic ensemble model.
	Args:
		loss: Choose from 'MSPE', 'NLL', 'MSE', 'Huber', or 'CE'. 
				choosing MSPE or NLL will construct a model with variance output
	"""
    print('[PE] dim in / out: {} / {} | Hidden dim: {}'.format(
        in_dim, out_dim, hidden_dims))
    #print('[ BNN ] Input Layer dim: {} | Output Layer dim: {} '.format(obs_dim_in+act_dim+prior_dim, obs_dim_out+rew_dim))
    params = {
        'name': name,
        'loss': loss,
        'num_networks': num_networks,
        'num_elites': num_elites,
        'sess': session,
        'use_scaler_in': use_scaler_in,
        'use_scaler_out': use_scaler_out,
        'clip_loss': clip_loss,
        'kl_cliprange': kl_cliprange,
        'max_logvar': max_logvar,
        'min_logvar': min_logvar,
    }
    model = PE(params)
    model.add(
        FC(hidden_dims[0],
           input_dim=in_dim,
           activation=activation,
           weight_decay=decay / 4))  # def dec: 0.000025))

    for hidden_dim in hidden_dims[1:]:
        model.add(FC(hidden_dim, activation=activation,
                     weight_decay=decay / 2))  # def dec: 0.00005))

    model.add(FC(out_dim, activation=output_activation,
                 weight_decay=decay))  # def dec: 0.0001

    opt_params = {
        "learning_rate": lr
    } if lr_decay is None else {
        "learning_rate": lr,
        "learning_rate_decay": lr_decay,
        "decay_steps": decay_steps
    }
    model.finalize(tf.train.AdamOptimizer, opt_params, lr_decay=lr_decay)

    total_parameters = 0
    for variable in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                      scope=name):
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        variable_parameters = 1
        for dim in shape:
            variable_parameters *= dim.value
        total_parameters += variable_parameters
    print('[ Probabilistic Ensemble ] Total trainable Parameteres: {} '.format(
        total_parameters))

    return model
示例#15
0
文件: packer.py 项目: x0xa/resources
    def pack(self, stub_path, output_path, input_file):
        f = open(stub_path, 'rb')
        stub_file = f.read()
        f.close()

        # encrypt the input
        key_str = ''
        for i in range(4):
            key_str += chr(random.randint(1, 0xFF))

        input_encrypted = self.xor_long(input_file, key_str)
        input_file = key_str + input_encrypted

        # increment the number of sections
        dos_header = PE.unpack(PE.IMAGE_DOS_HEADER, stub_file)
        peHeader_offset = dos_header['e_lfanew']
        numberOfSections_offset = peHeader_offset + 0x6
        numberOfSections = struct.unpack(
            '<H',
            stub_file[numberOfSections_offset:numberOfSections_offset + 2])[0]
        numberOfSections = numberOfSections + 1
        stub_file = stub_file[:numberOfSections_offset] + struct.pack(
            '<H', numberOfSections) + stub_file[numberOfSections_offset + 2:]

        # add the section header
        sectionHeader_offset = peHeader_offset + 0xF8
        last_section_offset = sectionHeader_offset + (
            (numberOfSections - 2) * 0x28)  # last section
        print 'last section: {0:x}'.format(last_section_offset)
        last_section = PE.unpack(PE.IMAGE_SECTION_HEADER,
                                 stub_file[last_section_offset:])

        new_section_header_offset = last_section_offset + 0x28
        new_section = PE.structDictionary(PE.IMAGE_SECTION_HEADER)
        new_section['Name'] = '.rsrc'
        new_section['Misc_VirtualSize'] = len(input_file)
        new_section['VirtualAddress'] = PE.align_value(
            last_section['VirtualAddress'] + last_section['SizeOfRawData'],
            0x1000)
        new_section['SizeOfRawData'] = PE.align_value(len(input_file), 0x200)
        new_section['PointerToRawData'] = PE.align_value(len(stub_file), 0x200)
        new_section[
            'Characteristics'] = PE.IMAGE_SCN_CNT_INITIALIZED_DATA | PE.IMAGE_SCN_MEM_READ | PE.IMAGE_SCN_MEM_WRITE | PE.IMAGE_SCN_ALIGN_4BYTES
        stub_file = stub_file[:new_section_header_offset] + PE.packDictionary(
            PE.IMAGE_SECTION_HEADER,
            new_section) + stub_file[new_section_header_offset + 0x28:]

        # section jam
        stub_file += '\x00' * (new_section['PointerToRawData'] -
                               len(stub_file))

        # append the section
        stub_file += input_file
        padding = new_section['SizeOfRawData'] - len(input_file)
        stub_file += '\x00' * padding

        # increment SizeOfImage
        optionalHeader_offset = peHeader_offset + 0x18
        optionalHeader_size = PE.structSize(PE.IMAGE_OPTIONAL_HEADER)
        optional_header = PE.unpack(PE.IMAGE_OPTIONAL_HEADER,
                                    stub_file[optionalHeader_offset:])
        optional_header['SizeOfImage'] = PE.align_value(
            new_section['VirtualAddress'] + new_section['SizeOfRawData'],
            0x1000)
        stub_file = stub_file[:optionalHeader_offset] + PE.packDictionary(
            PE.IMAGE_OPTIONAL_HEADER,
            optional_header) + stub_file[optionalHeader_offset +
                                         optionalHeader_size:]

        # save the file
        f = open(output_path, 'wb')
        f.write(stub_file)
        f.close()
示例#16
0
文件: pearmor.py 项目: x0xa/resources
code += asm_seh_function
offset_code += len(asm_seh_function)

# AntiEmulator
asm_anti_emulator = stub.asm_anti_emulator(meta_engine)
meta_engine.obfs_block(asm_anti_emulator, obfs_level, obfs_variation)
asm_anti_emulator = asm_anti_emulator.assemble()
stub.add_function('AntiEmulator', offset_code)
code += asm_anti_emulator
offset_code += len(asm_anti_emulator)

# Program
entry_point = base_offset_code + offset_code
print 'Entry point: ', hex(entry_point)
program_size = PE.align_value(
    base_offset_data + len(data),
    0x1000)  # this can be bad... @TODO update the push
asm_program = stub.asm_program(meta_engine, base_offset_code, program_size,
                               entry_point)
meta_engine.obfs_block(asm_program, obfs_level, obfs_variation, 1)
asm_program = asm_program.assemble()
code += asm_program
offset_code += len(asm_program)

(l, h) = hist(data + code)
print 'New entropy:', round(entropy(h, l), 2)

########################################################################
# Make the IAT
########################################################################
示例#17
0
class ws_PE_tb(nnsimTestBench):
    ''' Testbench specifically for weight stationary PE_ARARRY + GLB + NoC testing '''
    def instantiate(self, setup):
        nnsimTestBench.instantiate(self, setup)

        self.trace_generator            = ws_PE_trace_generator()
        self.traces_stats               = 'show'
        self.recorder                   = nnsimRecorder() if self.traces_stats == 'show' else None
        self.generated_trace            = True
        
        # bookkeeping variables
        self.result                     = []
        # ---------------------------------------------------------------------
        # IO channels for onchip and offchip communications
        # ---------------------------------------------------------------------
        # -> input channels for sending the input trace data to the GLBs
        # smartbuffer needs channels specified as list format
        self.weights_in_chn             = Channel()
        self.ifmap_chn                  = Channel()
        self.psum_in_chn                = Channel()
        # -> output channel for reciving calculated psum from the chip
        self.psum_out_chn               = Channel()
        
        pe_setup = {'weights_data_in_chn': self.weights_in_chn,\
                    'ifmap_data_in_chn': self.ifmap_chn,\
                    'psum_data_in_chn': self.psum_in_chn,\
                    'psum_data_out_chn': self.psum_out_chn}
        pe_setup.update(self.arch['PE'])
        self.dut  = PE(pe_setup) 
        
    def configure(self):
        # ====================================================================================================
        # 1. check if the setup meets the constraints,  if not met, program will exit
        # 2. apply paddings to input channels if total input channels not divisible by input channel mapping
        # ====================================================================================================
        check_constriant(self.mapping, self.shape, self.arch, self.ifmap_data, self.weights_data, self.bias_data)
        # ====================================================================================================        
        print('>> Generating input/output traces ')
        
        # configure trace generator
        trace_gen_config = { 'mapping':       self.mapping,\
                             'shape':         self.shape,\
                             'ifmap_data':    self.ifmap_data,\
                             'weights_data':  self.weights_data,\
                             'bias_data':     self.bias_data}
        
        
        self.trace_generator.configure(trace_gen_config)
        traces = self.trace_generator.generate_trace()
        
        # configure input traces
        self.weights_itrace          = traces['weights']
        self.ifmap_itrace            = traces['ifmap']
        self.psum_itrace             = traces['psum_in']
        self.psum_otrace             = traces['psum_out']        
        
        print('>> Configuring the testbech and unit under test ')
        # bookkeeping indexes used to step through the generated traces cycle by cycle 
        self.weights_itrace_idx      = 0
        self.ifmap_itrace_idx        = 0
        self.psum_itrace_idx         = 0
        
        #===========================================================================
        # PE configuration
        #===========================================================================   
        # configure the PE for specific workload and define the output channel
        lm_sram_map   = {'WeightsSp': [{'drain': 0, 'fill': 1}]}
        
        # weight scratchpad (depth = 1) config info
        buffer_config = {'LM': [{'base': 0, 'bound': self.arch['PE']['wsp_depth']-1}],\
                         'lm_sram_map':lm_sram_map['WeightsSp'].copy() }
                                
        shape_mapping_info = {'shape': self.shape.copy(), 'mapping': self.mapping.copy()}
        config =  { 'WeightsSp':            buffer_config,\
                    'shape_mapping_info':   shape_mapping_info,\
                    'clk_gated':            False}  
        self.dut.configure(config)
        
        print('----------------------------------------------------------------------------------')
        print(' Simulation starting ...')
        print('----------------------------------------------------------------------------------')
        
    def tick(self):
        # ----------------------------------------------------------------------------------------
        # Send Traces into the Designs input channels, cycle by cycle
        # ----------------------------------------------------------------------------------------
        
        # ------------------------- weights input trace ----------------------------------------------
        if self.weights_in_chn.vacancy():
            if self.weights_itrace_idx  < len(self.weights_itrace):
                if not np.isnan(self.weights_itrace[self.weights_itrace_idx]):
                    weight_data_to_send = [ self.weights_itrace[self.weights_itrace_idx]]
                    self.weights_in_chn.push(weight_data_to_send)
                    self.weights_itrace_idx += 1
            else:
                self.weights_done = True
        # ------------------------- ifmap input trace ------------------------------------------------    
        if self.ifmap_chn.vacancy():
            if self.ifmap_itrace_idx < len(self.ifmap_itrace):
                if not np.isnan(self.ifmap_itrace[self.ifmap_itrace_idx]):
                    ifmap_data_to_send = [ self.ifmap_itrace[self.ifmap_itrace_idx]]
                    self.ifmap_chn.push(ifmap_data_to_send)
                    self.ifmap_itrace_idx += 1
            else:
                self.ifmap_done = True  
                
        if self.psum_in_chn.vacancy():
            if self.psum_itrace_idx < len(self.psum_itrace):
                if not np.isnan(self.psum_itrace[self.psum_itrace_idx]):
                    psum_data_to_send = [ self.psum_itrace[self.psum_itrace_idx]]
                    self.psum_in_chn.push(psum_data_to_send)
                    self.psum_itrace_idx += 1
            else:
                self.psum_done = True  
                
        # -------------------------------------------------------------------------------------------
        # Collect output from the design's output channel
        # -------------------------------------------------------------------------------------------
        if self.psum_out_chn.valid():
            psum_out = self.psum_out_chn.pop()
            for idx in range(len(psum_out)):
                p = psum_out[idx]
                self.result.append(p)
#                print('-->', len(self.result), 'calculated:', p, 'reference:', self.psum_otrace[len(self.result)-1], 'total:',len(self.psum_otrace) )
    
        # -------------------------------------------------------------------------------------------
        # Compare the design's output with the generated output traces to validate functionality
        # -------------------------------------------------------------------------------------------        
        if len(self.result) == len(self.psum_otrace):
            if self.result == self.psum_otrace:
                raise Finish('Success')
            else:
              print('Failed')
              print('---------ofmap-----------------')
              print(self.result)
              print('----------ofmap_trace------------')
              print(self.psum_otrace)
              print('---------difference------------')
              difference = self.result
              for i in range(len(difference)):
                  difference[i] = self.result[i] - self.psum_otrace[i]
              print(difference)
              raise Finish()    
示例#18
0
    def instantiate(self, setup):

        self.pe_array_row = setup['pe_array'][0]
        self.pe_array_col = setup['pe_array'][1]

        # ---------------------------------------------------------------------
        # io data b/w offchip
        # ---------------------------------------------------------------------
        self.weights_in_chns = setup['io_chns']['weights']
        self.ifmap_in_chns = setup['io_chns']['ifmap']
        self.psum_in_chns = setup['io_chns']['psum_in']
        self.psum_out_chn = setup['io_chns']['psum_out']

        # ---------------------------------------------------------------------
        # onchip channels
        # ---------------------------------------------------------------------
        # >> buffer output channels
        self.weights_out_chns = ModuleList(Channel())
        self.ifmap_out_chns = ModuleList(Channel())
        self.ifmap_out_chns = ModuleList(Channel())
        self.psum_out_chns = ModuleList(Channel())

        # >> update data to buffer
        self.psum_update_chns = ModuleList(Channel())

        # ===================================================================
        # GLBs
        # ===================================================================
        # ------------------------ WEIGHTS -------------------------------
        weights_glb_setup = {'fill_data_ichns':        self.weights_in_chns,\
                             'drain_data_ochns':       self.weights_out_chns, \
                             'num_logical_managers':   1,\
                             'SRAM':                   {\
                                                        'depth':      setup['depth']['WeightsGLB'],\
                                                        'width':      setup['width']['WeightsGLB'],\
                                                        'data_width': setup['data_width']['WeightsGLB'],\
                                                        'nbanks':     setup['nbanks']['WeightsGLB'],\
                                                        'nports':     setup['nports']['WeightsGLB'],\
                                                        'port_type':  setup['port_type']['WeightsGLB'],\
                                                        },\
                             'debug': ' WeightsGLB'}
        self.weights_glb = WeightsGLB(weights_glb_setup)

        # ------------------------ IFMAP -------------------------------
        ifmap_glb_setup = {'fill_data_ichns':        self.ifmap_in_chns,\
                           'drain_data_ochns':       self.ifmap_out_chns, \
                           'num_logical_managers':   1,\
                           'SRAM':                   {\
                                                      'depth':      setup['depth']['IFmapGLB'],\
                                                      'width':      setup['width']['IFmapGLB'],\
                                                      'data_width': setup['data_width']['IFmapGLB'],\
                                                      'nbanks':     setup['nbanks']['IFmapGLB'],\
                                                      'nports':     setup['nports']['IFmapGLB'],\
                                                      'port_type':  setup['port_type']['IFmapGLB'],\
                                                        },\
                            'debug': ' IFmapGLB'}
        self.ifmap_glb = IFmapGLB(ifmap_glb_setup)

        # ------------------------ PSUM -------------------------------
        psum_glb_setup = {'fill_data_ichns':        self.psum_in_chns,\
                          'update_data_ichns':      self.psum_update_chns,\
                          'drain_data_ochns':       self.psum_out_chns, \
                          'num_logical_managers':   1,\
                          'SRAM':                   {\
                                                      'depth':      setup['depth']['PsumGLB'],\
                                                      'width':      setup['width']['PsumGLB'],\
                                                      'data_width': setup['data_width']['PsumGLB'],\
                                                      'nbanks':     setup['nbanks']['PsumGLB'],\
                                                      'nports':     setup['nports']['PsumGLB'],\
                                                      'port_type':  setup['port_type']['PsumGLB'],\
                                                        },\
                            'debug': ' PsumGLB'}
        self.psum_glb = PsumGLB(psum_glb_setup)

        # ===================================================================
        # PE Array Channels
        # ===================================================================
        self.ifmap_pe_data_chns = ModuleList()
        self.weights_pe_data_chns = ModuleList()
        self.psum_data_chns = ModuleList()

        self.pe_data_chns = {'ifmap':        self.ifmap_pe_data_chns,\
                             'weights':      self.weights_pe_data_chns,\
                             'psum':         self.psum_data_chns}

        for pe_row in range(self.pe_array_row):
            for chn_type, chn_row in self.pe_data_chns.items():
                chn_row.append(ModuleList())
            for pe_col in range(self.pe_array_col):
                for chn_type, chn_col in self.pe_data_chns.items():
                    chn_col[pe_row].append(Channel())

        self.psum_data_chns.append(ModuleList())
        for pe_col in range(self.pe_array_col):
            self.psum_data_chns[-1].append(Channel())

        # ===================================================================
        # Destination Calculators for NoC
        # ===================================================================
        self.ifmap_NoC_destination_chn = ModuleList(Channel())
        ifmap_NoC_destination_calculator_setup = \
                          {'out_chn': self.ifmap_NoC_destination_chn[0],\
                           'out_channel_width': 1,\
                           'debug': 'IFmapNoCDestCalc'}
        self.ifmap_NoC_destination_calculator = IFmapNoCDestCalc(
            ifmap_NoC_destination_calculator_setup)

        self.weights_NoC_destination_chn = ModuleList(Channel())
        weights_NoC_desitnation_calculator_setup = \
                           {'out_chn': self.weights_NoC_destination_chn[0],\
                            'out_channel_width': 1,\
                            'debug': 'WeightsNoCDestCalc'}
        self.weights_NoC_destionation_calculator = WeightsNoCDestCalc(
            weights_NoC_desitnation_calculator_setup)

        self.psum_in_NoC_destination_chn = ModuleList(Channel())
        psum_in_NoC_desitnation_calculator_setup = \
                           {'out_chn': self.psum_in_NoC_destination_chn[0],\
                            'out_channel_width': 1,\
                            'debug': 'PsumInNoCDestCalc'}
        self.psum_in_NoC_destionation_calculator = PsumInNoCDestCalc(
            psum_in_NoC_desitnation_calculator_setup)

        # ===================================================================
        # NoCs
        # ===================================================================
        # weights serializer 4:1
        self.weights_serializered_data_chn = Channel()
        weights_serializer_setup           = {'in_chn':  self.weights_out_chns[0],\
                                              'out_chn': self.weights_serializered_data_chn,\
                                              'ratio':   setup['weights_seri_ratio'],\
                                              'debug':   'weights_serialzer'}
        self.weights_serializer = WeightsSerializer(weights_serializer_setup)

        # Weights NoC: weightGLB -> PEs
        weights_noc_setup = {'rd_chns': self.pe_data_chns['weights'],\
                             'wr_chns': self.weights_serializered_data_chn,\
                             'dest_chns': self.weights_NoC_destination_chn,\
                             'debug': 'WeightsNoC'}
        self.weightsNoC = WeightsNoC(weights_noc_setup)

        # -------------- IFmap NoC: IfmapGLB -> PEs -----------------------
        # ifmap serializer 4:1
        self.ifmap_serialized_data_chn = Channel()
        ifmap_serializer_setup            = {'in_chn':  self.ifmap_out_chns[0],\
                                             'out_chn': self.ifmap_serialized_data_chn,\
                                             'ratio':   setup['ifmap_seri_ratio'],\
                                             'debug':  'ifmap_serializer'}
        self.ifmap_serilizer = IfmapSerializer(ifmap_serializer_setup)

        # ifmap NoC
        ifmap_noc_wr_chns = ModuleList()
        ifmap_noc_wr_chns.append(self.ifmap_serialized_data_chn)
        ifmap_noc_setup = {'rd_chns':   self.pe_data_chns['ifmap'],\
                           'wr_chns':   ifmap_noc_wr_chns,\
                           'dest_chns': self.ifmap_NoC_destination_chn,\
                           'debug':     'IFmapNoC'}
        self.ifmapNoC = IFMapNoC(ifmap_noc_setup)
        # ---------------------------------------------------------------------

        # -------------- PsumRd NoC: Psum GLB -> PEs ----------------------

        # psum serializer 4:1
        self.psum_serialized_data_chn = Channel()
        psum_serializer_setup            = {'in_chn':  self.psum_out_chns[0],\
                                            'out_chn': self.psum_serialized_data_chn,\
                                            'ratio':   setup['psum_seri_ratio'],\
                                            'debug':   'psum_serialzer'}
        self.psum_serializer = PsumSerializer(psum_serializer_setup)

        # psum read noc
        pe_noc_wr_chn = ModuleList(self.psum_serialized_data_chn)

        psum_rd_noc_setup = {'rd_chns':   self.psum_data_chns,\
                             'wr_chns':   pe_noc_wr_chn,\
                             'dest_chns': self.psum_in_NoC_destination_chn,\
                             'debug':     'PsumRdNoC'}
        self.psumRdNoC = PsumRdNoC(psum_rd_noc_setup)
        # ---------------------------------------------------------------------

        # -------------- PsumWrNoC:  PEs -> ifmapPsum GLB ---------------------
        self.psum_out_noc_rd_chns = ModuleList()
        self.psum_out_noc_rd_chns.append(
            self.psum_update_chns[0])  # write back to GLB
        self.psum_out_noc_rd_chns.append(self.psum_out_chn)  # write offchip
        psum_wr_noc_setup = {'rd_chns':       self.psum_out_noc_rd_chns,\
                             'wr_chns':       self.psum_data_chns,\
                             'debug':        'PsumWrNoC'}
        self.psumWrNoC = PsumWrNoC(psum_wr_noc_setup)

        # ===================================================================
        # PE Array
        # ===================================================================
        # general setup of a PE
        PE_setup = setup['PE']

        self.PE = ModuleList()

        for pe_row in range(self.pe_array_row):
            self.PE.append(ModuleList())
            for pe_col in range(self.pe_array_col):
                # PE specific setup
                PE_setup['row'] = pe_row
                PE_setup['col'] = pe_col
                PE_setup['weights_data_in_chn'] = self.weights_pe_data_chns[
                    pe_row][pe_col]
                PE_setup['ifmap_data_in_chn'] = self.ifmap_pe_data_chns[
                    pe_row][pe_col]
                PE_setup['psum_data_in_chn'] = self.psum_data_chns[pe_row][
                    pe_col]
                PE_setup['psum_data_out_chn'] = self.psum_data_chns[pe_row +
                                                                    1][pe_col]
                self.PE[pe_row].append(PE(PE_setup))
示例#19
0
    def pack(self):
        # names table
        number_of_dll = len(self.functions)
        names_table_base = self.base_address + PE.structSize(PE.IMAGE_IMPORT_DESCRIPTOR)*(number_of_dll + 1) # for the last null value
        names_table = ''
        names_table_offset = {}
        offset = names_table_base
        tmp = ''
        for (dll_name, functions) in self.functions.items(): # for each dll
            names_table_offset[dll_name] = offset
            tmp = dll_name + '\x00'
            names_table += tmp
            offset += len(tmp)
            for function_name in functions:                 # for each function in this dll
                names_table_offset[function_name] = offset
                tmp = '\x00\x00' + function_name + '\x00'
                names_table += tmp
                offset += len(tmp)

        # Original First Thunk table
        original_first_thunk_table = ''
        original_first_thunk_base = offset
        original_first_thunk_offset = {}
        for (dll_name, functions) in self.functions.items(): # for each dll
            original_first_thunk_offset[dll_name] = offset

            for function_name in functions:                 # for each function in this dll
                tmp = struct.pack('<I', names_table_offset[function_name])
                original_first_thunk_table += tmp
                offset += len(tmp)

            original_first_thunk_table += '\x00\x00\x00\x00'
            offset += 4

        # First Thunk Table
        first_thunk_table = original_first_thunk_table
        first_thunk_base = offset
        first_thunk_offset = {}
        for (dll_name, offset_in_oft) in original_first_thunk_offset.items():
            first_thunk_offset[dll_name] = offset_in_oft - original_first_thunk_base + first_thunk_base

        # IAT Table
        iat_table = ''

        for (dll_name, functions) in self.functions.items(): # for each dll
            function = PE.structDictionary(PE.IMAGE_IMPORT_DESCRIPTOR)
            function['OriginalFirstThunk'] = original_first_thunk_offset[dll_name]
            function['TimeDateStamp'] = 0
            function['ForwarderChain'] = 0 
            function['Name'] = names_table_offset[dll_name]
            function['FirstThunk'] = first_thunk_offset[dll_name]
            iat_table += PE.packDictionary(PE.IMAGE_IMPORT_DESCRIPTOR, function)
        function['OriginalFirstThunk'] = 0
        function['TimeDateStamp'] = 0
        function['ForwarderChain'] = 0 
        function['Name'] = 0
        function['FirstThunk'] = 0
        iat_table += PE.packDictionary(PE.IMAGE_IMPORT_DESCRIPTOR, function)

        iat_table += names_table
        iat_table += original_first_thunk_table
        iat_table += first_thunk_table

        return iat_table
示例#20
0
文件: pe_test.py 项目: chuanwang66/pe
from pe import PE

# list export functions
pe = PE('res/obs-aipai.dll')
print("ImageBaseAddr: 0x{0:08x}".format(pe.imagebase))
for api, addr in pe.exports.items():
    print("{0} @ 0x{1:08x}".format(api, addr))

print('\n\n')

# list import functions
pe = PE('res/notepad.exe')
print("ImageBaseAddr: 0x{0:08x}".format(pe.imagebase))
for dllname in pe.imports:
    for api, addr in pe.imports[dllname].items():
        print("{0} ({1}) @ 0x{2:08x} (IAT)".format(api, dllname, addr))