[docs]defVMJump(vm:VM,insn:Insn,verify=True)->None:""" Set the program counter (PC) to the next instruction address based on the instruction's hash verification. Args: vm (VM): The virtual machine instance. insn (Insn): The instruction containing the jump information. verify (bool): Whether to perform hash verification before jumping. """next_instruction_addr=insn.info.nextifverify:ifnotvm.verify_hash(insn.address+insn.insn_format.hash_xor_value_off):next_instruction_addr=insn.info.fallbackvm.context.pc=next_instruction_addr
[docs]defVMDeref(vm:VM,addr:addr_t):""" Dereference a memory address to retrieve the stored value. Args: vm (VM): The virtual machine instance. addr (addr_t): The address to dereference. Returns: Any: The value stored at the given address. """returnvm.mem[vm.context.u64(addr)]
[docs]defVMNewGlobalVar(vm:VM,addr:addr_t,type:str,value=None)->VMVariable:""" Create a new global variable and add it to the VM's memory. Args: vm (VM): The virtual machine instance. addr (addr_t): The address for the new global variable. type (str): The type of the variable. value (Optional[Any]): The initial value of the variable, if any. Returns: VMVariable: The newly created global variable. """var=VMVariable(addr,type,value)vm.mem.variables[addr]=varvar.name=f"lVar{len(vm.mem.variables)}"returnvar
[docs]defVMGetGlobalVar(vm:VM,addr:addr_t)->VMVariable:""" Retrieve an existing global variable by its address. Args: vm (VM): The virtual machine instance. addr (addr_t): The address of the global variable. Returns: VMVariable: The global variable at the given address. """returnvm.mem.variables[addr]
[docs]defVMGetOrCreateGlobalVar(vm:VM,addr:addr_t,type:str,value=None)->VMVariable:""" Get an existing global variable or create a new one if it doesn't exist. Args: vm (VM): The virtual machine instance. addr (addr_t): The address of the global variable. type (str): The type of the variable. value (Optional[Any]): The value to set for the variable if created. Returns: VMVariable: The global variable at the given address, either newly created or existing. """ifaddrnotinvm.mem.variables:var=VMNewGlobalVar(vm,addr,type,value)else:var=vm.mem.variables[addr]ifvalueisnotNone:var.value=valuereturnvar