Skip to content

Commit

Permalink
Allow dynamic import of an entire module
Browse files Browse the repository at this point in the history
  • Loading branch information
elicn committed Apr 19, 2022
1 parent 3d6574c commit 759f4c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion qiling/os/posix/posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from qiling.exception import QlErrorSyscallNotFound
from qiling.os.os import QlOs
from qiling.os.posix.const import NR_OPEN, errors
from qiling.utils import ql_get_module_function
from qiling.utils import ql_get_module, ql_get_module_function

SYSCALL_PREF: str = f'ql_syscall_'

Expand Down
24 changes: 12 additions & 12 deletions qiling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import importlib, os

from configparser import ConfigParser
from types import ModuleType
from typing import TYPE_CHECKING, Any, Callable, Mapping, Optional, Tuple, TypeVar, Union

from unicorn import UC_ERR_READ_UNMAPPED, UC_ERR_FETCH_UNMAPPED
Expand Down Expand Up @@ -66,24 +67,23 @@ def debugger_convert(debugger: str) -> Optional[QL_DEBUGGER]:
def arch_os_convert(arch: QL_ARCH) -> Optional[QL_OS]:
return arch_os_map.get(arch)

# Call `function_name` in `module_name`.
# e.g. map_syscall in qiling.os.linux.map_syscall
def ql_get_module_function(module_name: str, function_name: str):

def ql_get_module(module_name: str) -> ModuleType:
try:
imp_module = importlib.import_module(module_name, 'qiling')
except ModuleNotFoundError:
raise QlErrorModuleNotFound(f'Unable to import module {module_name}')
except KeyError:
module = importlib.import_module(module_name, 'qiling')
except (ModuleNotFoundError, KeyError):
raise QlErrorModuleNotFound(f'Unable to import module {module_name}')

return module

def ql_get_module_function(module_name: str, member_name: str):
module = ql_get_module(module_name)

try:
module_function = getattr(imp_module, function_name)
member = getattr(module, member_name)
except AttributeError:
raise QlErrorModuleFunctionNotFound(f'Unable to import {function_name} from {imp_module}')

return module_function
raise QlErrorModuleFunctionNotFound(f'Unable to import {member_name} from {module_name}')

return member

def __emu_env_from_pathname(path: str) -> Tuple[Optional[QL_ARCH], Optional[QL_OS], Optional[QL_ENDIAN]]:
if os.path.isdir(path) and path.endswith('.kext'):
Expand Down

0 comments on commit 759f4c1

Please sign in to comment.