Example #1
0
    def __init__(self, idbm, name=None, verbose=False):
        '''
		Class constructor.

		@idbm    - Instance of IDASimMMU.
		@name    - Name that will be assigned to the class instance.
		@verbose - Enable verbose mode.

		Returns None.
		'''
        self.idbm = idbm
        self.name = name
        self.verbose = verbose
        self.cpu = Architecture()

        if self.name == None:
            self.name = self.__get_my_name()

        self.bpt_cnd = self.BPT_CND % self.name

        self.UnregisterHandlers()

        # Eval this IDC expression to ensure that Python is set as the
        # preferred external scripting language. This is necessary for the
        # Python function handler to operate correctly.
        idc.Eval('RunPlugin("python", 3)')
Example #2
0
    def __init__(self, base=None):
        '''
		Class constructor.
		'''
        # Disable this for now, it doesn't work.
        self.use_native_malloc = False
        self.allocated_addresses = {}

        self.app = Application()
        self.cpu = Architecture()

        if base is not None:
            self.MP = self.BASE_MP = base
        else:
            self.MP = self.BASE_MP = idc.BADADDR
Example #3
0
def setup(args):
    # Find the workspace and build root directory.
    if not os.path.exists(args.workspace):
        raise RuntimeError("Workspace area %s does not exist." %
                           args.workspace)
    workspace = args.workspace

    bldDir = os.path.join(workspace, 'build')
    if not os.path.exists(bldDir):
        raise RuntimeError("Workspace build area %s does not exist." % bldDir)

    # Architecture is used for deconvolving file paths
    arch = Architecture(args.arch)
    # Build provides accessors for the doc dirs
    build = Build(bldDir, arch)

    pack, proj = getPackageProject()

    # By design, there must be a 'package.doc' in the package directory
    # (which is also the CWD)
    if not os.path.exists('package.doc'):
        raise RuntimeError("File package.doc does not exist in %s",
                           os.path.realpath(os.path.curdir))

    localEnv = LocalEnv(build, pack, proj, workspace, args.sdkVer,
                        args.developer, args.c)
    return localEnv
Example #4
0
def setup(args):
    # Find the workspace and build root directory.
    if not os.path.exists(args.workspace):
        raise RuntimeError("Workspace area %s does not exist." %
                           args.workspace)
    workspace = args.workspace

    bldDir = os.path.join(workspace, 'build')
    if not os.path.exists(bldDir):
        raise RuntimeError("Workspace build area %s does not exist." % bldDir)

    env = Environment(workspace, args.developer, args.sdkVer)
    for archname in args.arch:
        arch = Architecture(archname)
        env.builds.append(Build(bldDir, arch))


#####+++++
#    print "+++++ build directories (setup)"
#    for b in env.builds:
#        print "  ", b.archDir
#    print "----- build directories (setup)"
#####+++++

    return env
Example #5
0
def create_train_model(path_output, image_size, LR):

    model = Architecture.resnet50_tl_ft(image_size)
    opt = optimizers.RMSprop(learning_rate=LR)  # resnet50
    #opt = optimizers.SGD(learning_rate=LR)

    #model = Architecture.xception_tl_ft(image_size)
    #opt = optimizers.RMSprop(learning_rate=LR)

    #model = Architecture.mobilenet_tl_ft(image_size)
    #opt = optimizers.RMSprop(learning_rate=LR)

    #model = Architecture.inseptionv3_tl_ft(image_size)
    #opt = optimizers.RMSprop(learning_rate=LR)

    #opt = optimizers.Adam(learning_rate=LR)

    model.compile(loss='categorical_crossentropy',
                  optimizer=opt,
                  metrics=['accuracy'])  # metrics=['accuracy']

    #model.summary()
    plot_model(model,
               to_file=path_output + "model_plot.png",
               show_shapes=True,
               show_layer_names=True)

    return model
Example #6
0
    def main(self):
        CPU = Architecture(self.SCALE)

        CPU.LOAD_ROMFILE(self.FONT_FILE, 0)
        CPU.LOAD_ROMFILE(self.ROM_FILE)

        pygame.time.set_timer(self.TIMER, self.DELAY_INTERVAL)

        running = True

        while running:
            pygame.time.wait(self.DELAY)
            CurrentOperand = CPU.EXECUTE()

            # Check for various events
            for event in pygame.event.get():
                if event.type == self.TIMER:
                    CPU.DECREMENT_TIMERS()

                if event.type == pygame.QUIT:
                    running = False
                
                if event.type == pygame.KEYDOWN:
                    all_keys_down = pygame.key.get_pressed()
                    if all_keys_down[pygame.K_q]:
                        running = False

            if CurrentOperand == 0x00FD:
                running = False
Example #7
0
    packageName = os.path.split(packageLocation.strip(os.sep))[1]
    pubIncs = set()
    if not os.path.isdir(os.path.join(destination, packageName)):
        os.makedirs(os.path.join(destination, packageName))
    for g in INC_GLOBS:
        pubIncs.update(glob.glob(os.path.join(packageLocation,g)))
    for inc in pubIncs:
        mkLink(inc, os.path.join(destination, packageName, os.path.basename(inc)))

if __name__ == "__main__":
    import optparse
    #print "jhpDebug: ", sys.argv, os.environ['PWD']
    
    parser = optparse.OptionParser()
    parser.add_option('-a', '--arch', dest="architecture", help='Architecture')
    parser.add_option('-d', '--dest', dest="destination", help='Link farm destination')

    opts,args = parser.parse_args()
    arch = Architecture(opts.architecture)
    TGT_ARCH=opts.architecture

    for prj in getProjects():
        for pkg in getPackages(prj):
            pLoc = os.path.join(RELEASE_DIR,prj,pkg)
            linkPublicIncludes(pLoc, opts.destination)
            linkIncludeSubdirs(pLoc, opts.destination, arch)

    #sys.exit(-1)


Example #8
0
class IDASimMMU:
    '''
	Manages the allocation of memory while running in the debugger.
	The term 'manage' is used very loosely here; it really only allocates memory.
	'''
    ALIGN = 4
    DEFAULT_MP = 0x100000
    SEGNAME = 'MMU'
    LAST_SEGNAME = ['MEMORY', 'RAM']

    def __init__(self, base=None):
        '''
		Class constructor.
		'''
        # Disable this for now, it doesn't work.
        self.use_native_malloc = False
        self.allocated_addresses = {}

        self.app = Application()
        self.cpu = Architecture()

        if base is not None:
            self.MP = self.BASE_MP = base
        else:
            self.MP = self.BASE_MP = idc.BADADDR

    def _detect_membase(self):
        '''
		Attempts to locate a section of memory for IDBMMU's internal memory allocation.
		For internal use only.
		'''
        if self.BASE_MP == idc.BADADDR:

            # Look for the MMU segment
            ea = idc.SegByName(self.SEGNAME)

            # No MMU segment?
            if ea == idc.BADADDR:
                ea = 0

                # Find the very last defined segment
                while True:
                    segea = idc.NextSeg(ea)

                    if segea == idc.BADADDR:
                        break
                    else:
                        ea = segea

                # Is it not a memory segment?
                if idc.SegName(ea) not in self.LAST_SEGNAME:
                    try:
                        # Find the start of the stack
                        ea = idc.SegStart(self.cpu.StackPointer())

                        # Still nothing? Use the default.
                        if ea == idc.BADADDR:
                            ea = self.DEFAULT_MP
                    except:
                        if not self.use_native_malloc:
                            raise Exception(
                                "No available segments for memory allocation! Try defining segment %s."
                                % self.SEGNAME)
            self.BASE_MP = ea

        if self.MP == idc.BADADDR:
            self.MP = self.BASE_MP

        return self.BASE_MP

    def reset(self):
        '''
		Resets the current allocation address.
		'''
        self.MP = idc.BADADDR
        self.allocated_addresses = {}

    def base(self, base=None):
        '''
		Set the base address at which to start allocating memory. Default: 0x100000.

		@base - The base address. If specified BASE_MP will be set to this value.

		Returns the current BASE_MP value.
		'''
        if base is not None:
            self.MP = self.BASE_MP = base
        return self.BASE_MP

    def malloc(self, data=None, size=0):
        '''
		Allocates space for data in the debugger's memory and populates it.
	
		@data - Data to place into memory. If None, NULL bytes will be used.
		@size - Size of memory to allocate. If 0, len(data) bytes will be allocated.
	
		Returns the address of the allocated memory.
		'''
        if size == 0 and data is not None:
            size = len(data)

        if data is None:
            data = "\x00" * size

        if self.use_native_malloc:
            addr = self.app.Call('malloc',
                                 arguments=[size],
                                 retaddr=self.cpu.ReturnAddress())
        else:
            self._detect_membase()

            addr = self.MP
            self.MP += size
            # This ensures memory addresses are 4-byte aligned. This is important for some architectures.
            if (self.MP % self.ALIGN) > 0:
                self.MP += (self.ALIGN - (self.MP % self.ALIGN))

            # Keep a dictionary of allocated addresses and their sizes
            self.allocated_addresses[addr] = size

        idc.DbgWrite(addr, data)

        return addr
Example #9
0
from alien import Alien
from animal import Animal
from architecture import Architecture
from corporation import Corporation
from faction import Faction
from heresy import Heresy
from npc import NPC
from political_party import PoliticalParty
from religion import Religion
from trade import Trade
from world import World
import random

if __name__ == "__main__":
    print(World())
    print("Predominant architectural feature: {}".format(Architecture()))
    print("---")
    for i in range(random.choice([2, 3])):
        print("Animal:\n{}\n".format(Animal()))
    print("---")
    for i in range(random.choice([2, 3])):
        print("Political Party:\n{}\n".format(PoliticalParty()))
    print("---")
    for i in range(random.choice([2, 3])):
        print("Corporation:\n{}\n".format(Corporation()))
    print("---")
    for i in range(random.choice([1, 2])):
        print(
            random.choice([
                "Religion:\n{}\n".format(Religion()),
                "Heresy:\n{}\n".format(Heresy())
Example #10
0
    if args.network in ['resnet18', 'resnet34']:
        opt.co_graph_gen = 'get_graph_resnet'
    elif args.network == 'vgg19':
        opt.co_graph_gen = 'get_graph_vgg'
    elif args.network == 'shufflenet':
        opt.co_graph_gen = 'get_graph_shufflenet'

    if args.dataset == 'cifar10':
        opt.dataset = 'CIFAR10Val'
    elif args.dataset == 'cifar100':
        opt.dataset = 'CIFAR100Val'

    opt.device = args.device

    opt.model = './models/pretrained/%s_%s.pth' % (args.network, args.dataset)
    opt.savedir = './save/%s_%s_%s' % (args.network, args.dataset, args.suffix)
    opt.writer = SummaryWriter('./runs/%s_%s_%s' %
                               (args.network, args.dataset, args.suffix))
    assert not (os.path.exists(opt.savedir)), 'Overwriting existing files!'

    print(
        'Start compression. Please check the TensorBoard log in the folder ./runs/%s_%s_%s.'
        % (args.network, args.dataset, args.suffix))

    model = torch.load(opt.model).to(opt.device)
    teacher = Architecture(*(getattr(gr, opt.co_graph_gen)(model)))
    dataset = getattr(datasets, opt.dataset)()
    record = Record()
    compression(teacher, dataset, record)
    fully_train(dataset=opt.dataset[:-3])
Example #11
0
class Application:
    '''
	Class for invoking functions in the target process.
	'''
    def __init__(self):
        '''
		Class constructor.
		'''
        self.cpu = Architecture()

    def Call(self, function, arguments=[], retaddr=0, block_until_return=True):
        '''
		Call a given function. Arguments must already be configured.
		This should not be used to call functions hooked with IDASimulator or it likely won't work.

		@function           - The name or address of the function to call.
		@arguments          - A list of function arguments.
		@retaddr            - The address to return to.
		@block_until_return - If set to True, this method will not return until the function does.
				      If set to False, this method will return immediately after calling the function.

		Returns the return value of the function on success.
		Returns None on failure, or if block_until_return is False.
		'''
        retval = None

        # Process should already be paused, but just in case...
        idc.PauseProcess()

        # If a function name was specified, get its address
        if isinstance(function, type('')):
            function = idc.LocByName('.' + function)

            if function == idc.BADADDR:
                function = idc.LocByName(function)

        if function != idc.BADADDR:
            if not retaddr:
                retaddr = self.cpu.ProgramCounter()

            # Set the specified function arguments
            self.cpu.SetArguments(arguments)

            # Do any arch-specific initialization before the function call
            self.cpu.PreFunctionCall(function)

            # Set up the return address and point the program counter to the start of the target function
            self.cpu.ReturnAddress(value=retaddr)
            self.cpu.ProgramCounter(value=function)
            idc.Jump(function)

            if block_until_return:
                # Resume process and wait for the target function to return
                idc.StepUntilRet()
                idc.GetDebuggerEvent(idc.WFNE_CONT | idc.WFNE_SUSP, -1)
                idc.Jump(retaddr)
                retval = self.cpu.ReturnValue()
            else:
                idc.ResumeProcess()

        return retval
Example #12
0
    def __init__(self):
        '''
		Class constructor.
		'''
        self.cpu = Architecture()
Example #13
0
import pandas as pd
import torch.nn as nn
import graph as gr
from architecture import Architecture

h = 16
w = 16

model = models.Sample10()
#model.load_state_dict(torch.load('base/groundtruth10.pth'))

model.cuda()

#torch.save(model.state_dict(), 'base/groundtruth10.pth')

model = Architecture(*(getattr(gr, 'get_graph_vgg')(model)))
#model = torch.load('base/groundtruth8.pth')

print(model.param_n())

#for parameter in model.parameters():
#    print(parameter.size())

model.eval()

#data = {"image":[], "category":[]}
#
#for i in range(60000):
#
#    #fake_img = torch.randn([1, 3, h, w], dtype=torch.float32)
#    fake_img = torch.empty(1,3,h,w).normal_(mean=128,std=32)
    clk = Clock(10, 0)
    rst = Reset(1)

    b0 = Bus(8, 0)
    b1 = Bus(8, 0)
    c1 = Constant(8, 1)

    reg = Register(8, clk, rst, b0, b1)
    add = Adder(8, c1, b1, b0)

    hooks = OrderedDict([('clk', clk), ('rst', rst), ('b0', b0), ('b1', b1),
                         ('c1', c1)])
    entities = OrderedDict([('clk', clk), ('add', add), ('reg', reg)])

    arch = Architecture(0.0001, clk, rst, hooks, entities)

    msg_inspect = {'inspect': ['clk', 'rst', 'b0', 'b1', 'c1']}

    async def interface_to_frontend(websocket, path):
        async for message in websocket:
            msg = json.loads(message)

            retMsg = {}

            if 'hook' in msg:
                retMsg = arch.hook(msg['hook'])

            if 'architecture' in msg:
                if 'simulate' in msg['architecture']:
                    arch.logic_run()
Example #15
0
class IDASimFunctionHandler:
    '''
	Registers and manages function simulators.
	'''
    FUNCTION_HANDLERS = {}
    DEFAULT_HANDLER = None
    BPT_CND = '%s.Handler()'
    STUB_NAMING_CONVENTION = '.%s'

    def __init__(self, idbm, name=None, verbose=False):
        '''
		Class constructor.

		@idbm    - Instance of IDASimMMU.
		@name    - Name that will be assigned to the class instance.
		@verbose - Enable verbose mode.

		Returns None.
		'''
        self.idbm = idbm
        self.name = name
        self.verbose = verbose
        self.cpu = Architecture()

        if self.name == None:
            self.name = self.__get_my_name()

        self.bpt_cnd = self.BPT_CND % self.name

        self.UnregisterHandlers()

        # Eval this IDC expression to ensure that Python is set as the
        # preferred external scripting language. This is necessary for the
        # Python function handler to operate correctly.
        idc.Eval('RunPlugin("python", 3)')

    def cleanup(self):
        idc.Eval('RunPlugin("python", 4)')

    def __del__(self):
        self.cleanup()

    def __get_my_name(self):
        '''
		This is a hack to get the name of the class instance variable. For internal use only.
		'''
        i = -3
        (filename, line_number, function_name,
         text) = traceback.extract_stack()[i]
        name = text[:text.find('=')].strip()
        while 'self' in name:
            i -= 1
            (filename, line_number, function_name,
             text) = traceback.extract_stack()[i]
            name = name.replace('self', text[:text.find('=')].strip())
        return name

    def SetHandlerBreakpoint(self, address):
        '''
		Sets a handler breakpoint on the specified address.

		@address - Address to set the breakpoint at.

		Returns True on success, False on failure.
		'''
        # Some remote debugger stubs have special needs for different architectures (e.g., gdb).
        # Thus, setting breakpoints should be done through the architecture abstraction class,
        # rather than directly through AddBpt/AddBptEx.
        self.cpu.SetBreakpoint(address)

        # A bug in versions of IDAPython shipped with IDA prior to 6.4sp1 improperly interpreted
        # the is_lowcnd value set via SetBptCnd/SetBptCndEx. Do this directly through idaapi
        # ourselves in order to support older versions.
        bpt = idaapi.bpt_t()
        idaapi.get_bpt(address, bpt)
        bpt.condition = self.bpt_cnd
        bpt.flags &= ~idc.BPT_LOWCND
        return idaapi.update_bpt(bpt)

    def __register_internal_handler(self, name, handler):
        '''
		Internal handler registration function. For internal use only.
		'''
        if type(name) == type(""):
            address = idc.LocByName(name)
        else:
            address = name

        if address != idc.BADADDR:
            bpt_result = self.SetHandlerBreakpoint(address)

            if bpt_result:
                self.FUNCTION_HANDLERS[name] = {}
                self.FUNCTION_HANDLERS[name]["handler"] = handler
                self.FUNCTION_HANDLERS[name]["address"] = address

            return bpt_result
        else:
            return False

    def Handler(self):
        '''
		Breakpoint condition handler, called by IDA to evaluate conditional brekpoints. It in turn calls the 
		appropriate function handler, populates the return value and puts execution back at the return address. 
	
		This is a (slight) abuse of IDA's conditional breakpoints; this function always returns 0, indicating that
		the breakpoint condition has not been met. However, it does ensure that every call to a given function
		can be intercepted and simulated, regardless of whether the process is running freely, or the function has 
		been stepped over, stepped into, etc.
		'''
        retval = 0
        retaddr = None

        if self.verbose:
            print self.FUNCTION_HANDLERS

        for (name, properties) in self.FUNCTION_HANDLERS.iteritems():
            if self.cpu.ProgramCounter() == properties["address"]:
                handler = properties["handler"]
                break

        # If no explicit handler was found, use the default handler
        if not handler and self.DEFAULT_HANDLER:
            handler = self.DEFAULT_HANDLER

        if handler:
            if self.verbose:
                print "Using function handler:", handler.__name__

            parameters = {}

            # Enumerate the arguments and default values for the handler
            args, varargs, keywords, defaults = inspect.getargspec(handler)
            try:
                defaults = dict(zip(reversed(args), reversed(defaults)))
            except:
                defaults = {}

            # Build the handler parameters
            try:
                i = 0
                for arg in args:
                    if arg != 'self':
                        parameters[arg] = self.cpu.Argument(i)

                        if defaults.has_key(arg):
                            # If default value is of type string, get the string automatically
                            if type(defaults[arg]) == type(''):
                                parameters[arg] = idc.GetString(
                                    parameters[arg])
                            # If default value is of type list, get an array of bytes
                            elif type(defaults[arg]) == type([]) and len(
                                    defaults[arg]) == 1:
                                parameters[arg] = [
                                    c for c in idc.DbgRead(
                                        parameters[arg], defaults[arg][0])
                                ]
                        i += 1
            except Exception, e:
                print "WARNING: Failed to parse handler parameters:", str(e)
                parameters = {}

            try:
                retval = handler(**parameters)
            except JumpTo, offset:
                retaddr = self.cpu.ReturnAddress() + offset.message
            except GoTo, addr:
                retaddr = addr.message
Example #16
0
from architecture import Architecture
from pipe import Pipe
from filter import Filter

arc = Architecture()

f1 = Filter(speed=3)
f2 = Filter()
f3 = Filter()

p1 = Pipe(speed=2)
p2 = Pipe()

f1.set_target(p1)
p1.set_target(f2)
f2.set_target(p2)
p2.set_target(f3)

f1.set_value(10)

arc.set_start_node(f1)

arc.launch()