|
3 | 3 | # Cross Platform and Multi Architecture Advanced Binary Emulation Framework |
4 | 4 | # |
5 | 5 |
|
6 | | -from qiling import Qiling |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import ctypes |
| 9 | +from typing import TYPE_CHECKING, Type |
| 10 | + |
| 11 | +from qiling.const import QL_ARCH |
| 12 | +from qiling.os import struct |
| 13 | +from qiling.os.posix.const import NSIG |
| 14 | + |
| 15 | +# TODO: MIPS differs in too many details around signals; MIPS implementation is better extracted out |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from qiling import Qiling |
| 19 | + from qiling.arch.arch import QlArch |
| 20 | + |
| 21 | + |
| 22 | +@struct.cache |
| 23 | +def __make_sigset(arch: QlArch): |
| 24 | + native_type = struct.get_native_type(arch.bits) |
| 25 | + |
| 26 | + sigset_type = { |
| 27 | + QL_ARCH.X86: native_type, |
| 28 | + QL_ARCH.X8664: native_type, |
| 29 | + QL_ARCH.ARM: native_type, |
| 30 | + QL_ARCH.ARM64: native_type, |
| 31 | + QL_ARCH.MIPS: ctypes.c_uint32 * (128 // (4 * 8)), |
| 32 | + QL_ARCH.CORTEX_M: native_type |
| 33 | + } |
| 34 | + |
| 35 | + if arch.type not in sigset_type: |
| 36 | + raise NotImplementedError(f'sigset definition is missing for {arch.type.name}') |
| 37 | + |
| 38 | + return sigset_type[arch.type] |
| 39 | + |
| 40 | + |
| 41 | +@struct.cache |
| 42 | +def __make_sigaction(arch: QlArch) -> Type[struct.BaseStruct]: |
| 43 | + native_type = struct.get_native_type(arch.bits) |
| 44 | + Struct = struct.get_aligned_struct(arch.bits, arch.endian) |
| 45 | + |
| 46 | + sigset_type = __make_sigset(arch) |
| 47 | + |
| 48 | + # # FIXME: untill python 3.11 ctypes Union does not support an endianess that is different from |
| 49 | + # the hosting paltform. if a LE system is emulating a BE one or vice versa, this will fail. to |
| 50 | + # work around that we avoid using a union and refer to the inner field as 'sa_handler' regardless. |
| 51 | + # |
| 52 | + # Union = struct.get_aligned_union(arch.bits) |
| 53 | + # |
| 54 | + # class sighandler_union(Union): |
| 55 | + # _fields_ = ( |
| 56 | + # ('sa_handler', native_type), |
| 57 | + # ('sa_sigaction', native_type) |
| 58 | + # ) |
| 59 | + |
| 60 | + # <WORKAROUND> see FIXME above |
| 61 | + class sighandler_union(Struct): |
| 62 | + _fields_ = ( |
| 63 | + ('sa_handler', native_type), |
| 64 | + ) |
| 65 | + # </WORKAROUND> |
| 66 | + |
| 67 | + # see: https://elixir.bootlin.com/linux/v5.19.17/source/arch/arm/include/uapi/asm/signal.h |
| 68 | + class arm_sigaction(Struct): |
| 69 | + _anonymous_ = ('_u',) |
| 70 | + |
| 71 | + _fields_ = ( |
| 72 | + ('_u', sighandler_union), |
| 73 | + ('sa_mask', sigset_type), |
| 74 | + ('sa_flags', native_type), |
| 75 | + ('sa_restorer', native_type) |
| 76 | + ) |
| 77 | + |
| 78 | + # see: https://elixir.bootlin.com/linux/v5.19.17/source/arch/x86/include/uapi/asm/signal.h |
| 79 | + class x86_sigaction(Struct): |
| 80 | + _anonymous_ = ('_u',) |
| 81 | + |
| 82 | + _fields_ = ( |
| 83 | + ('_u', sighandler_union), |
| 84 | + ('sa_mask', sigset_type), |
| 85 | + ('sa_flags', native_type), |
| 86 | + ('sa_restorer', native_type) |
| 87 | + ) |
| 88 | + |
| 89 | + class x8664_sigaction(Struct): |
| 90 | + _fields_ = ( |
| 91 | + ('sa_handler', native_type), |
| 92 | + ('sa_flags', native_type), |
| 93 | + ('sa_restorer', native_type), |
| 94 | + ('sa_mask', sigset_type) |
| 95 | + ) |
| 96 | + |
| 97 | + # see: https://elixir.bootlin.com/linux/v5.19.17/source/arch/mips/include/uapi/asm/signal.h |
| 98 | + class mips_sigaction(Struct): |
| 99 | + _fields_ = ( |
| 100 | + ('sa_flags', ctypes.c_uint32), |
| 101 | + ('sa_handler', native_type), |
| 102 | + ('sa_mask', sigset_type) |
| 103 | + ) |
| 104 | + |
| 105 | + sigaction_struct = { |
| 106 | + QL_ARCH.X86: x86_sigaction, |
| 107 | + QL_ARCH.X8664: x8664_sigaction, |
| 108 | + QL_ARCH.ARM: arm_sigaction, |
| 109 | + QL_ARCH.ARM64: arm_sigaction, |
| 110 | + QL_ARCH.MIPS: mips_sigaction, |
| 111 | + QL_ARCH.CORTEX_M: arm_sigaction |
| 112 | + } |
| 113 | + |
| 114 | + if arch.type not in sigaction_struct: |
| 115 | + raise NotImplementedError(f'sigaction definition is missing for {arch.type.name}') |
| 116 | + |
| 117 | + return sigaction_struct[arch.type] |
| 118 | + |
7 | 119 |
|
8 | 120 | def ql_syscall_rt_sigaction(ql: Qiling, signum: int, act: int, oldact: int): |
| 121 | + SIGKILL = 9 |
| 122 | + SIGSTOP = 23 if ql.arch.type is QL_ARCH.MIPS else 19 |
| 123 | + |
| 124 | + if signum not in range(NSIG) or signum in (SIGKILL, SIGSTOP): |
| 125 | + return -1 # EINVAL |
| 126 | + |
| 127 | + sigaction = __make_sigaction(ql.arch) |
| 128 | + |
9 | 129 | if oldact: |
10 | | - arr = ql.os.sigaction_act[signum] or [0] * 5 |
11 | | - data = b''.join(ql.pack32(key) for key in arr) |
| 130 | + old = ql.os.sig[signum] or sigaction() |
12 | 131 |
|
13 | | - ql.mem.write(oldact, data) |
| 132 | + old.save_to(ql.mem, oldact) |
14 | 133 |
|
15 | 134 | if act: |
16 | | - ql.os.sigaction_act[signum] = [ql.mem.read_ptr(act + 4 * i, 4) for i in range(5)] |
| 135 | + ql.os.sig[signum] = sigaction.load_from(ql.mem, act) |
17 | 136 |
|
18 | 137 | return 0 |
19 | 138 |
|
20 | 139 |
|
21 | | -def ql_syscall_rt_sigprocmask(ql: Qiling, how: int, nset: int, oset: int, sigsetsize: int): |
22 | | - # SIG_BLOCK = 0x0 |
23 | | - # SIG_UNBLOCK = 0x1 |
| 140 | +def __sigprocmask(ql: Qiling, how: int, newset: int, oldset: int): |
| 141 | + SIG_BLOCK = 0 |
| 142 | + SIG_UNBLOCK = 1 |
| 143 | + SIG_SETMASK = 2 |
| 144 | + |
| 145 | + SIGKILL = 9 |
| 146 | + SIGSTOP = 19 |
| 147 | + |
| 148 | + if oldset: |
| 149 | + ql.mem.write_ptr(newset, ql.os.blocked_signals) |
| 150 | + |
| 151 | + if newset: |
| 152 | + set_mask = ql.mem.read_ptr(newset) |
| 153 | + |
| 154 | + if how == SIG_BLOCK: |
| 155 | + ql.os.blocked_signals |= set_mask |
| 156 | + |
| 157 | + elif how == SIG_UNBLOCK: |
| 158 | + ql.os.blocked_signals &= ~set_mask |
| 159 | + |
| 160 | + elif how == SIG_SETMASK: |
| 161 | + ql.os.blocked_signals = set_mask |
24 | 162 |
|
| 163 | + else: |
| 164 | + return -1 # EINVAL |
| 165 | + |
| 166 | + # silently drop attempts to block SIGKILL and SIGSTOP |
| 167 | + ql.os.blocked_signals &= ~((1 << SIGKILL) | (1 << SIGSTOP)) |
| 168 | + |
| 169 | + return 0 |
| 170 | + |
| 171 | + |
| 172 | +def __sigprocmask_mips(ql: Qiling, how: int, newset: int, oldset: int): |
| 173 | + SIG_BLOCK = 1 |
| 174 | + SIG_UNBLOCK = 2 |
| 175 | + SIG_SETMASK = 3 |
| 176 | + |
| 177 | + SIGKILL = 9 |
| 178 | + SIGSTOP = 23 |
| 179 | + |
| 180 | + # TODO: to implement |
25 | 181 | return 0 |
26 | 182 |
|
27 | 183 |
|
| 184 | +def ql_syscall_rt_sigprocmask(ql: Qiling, how: int, newset: int, oldset: int): |
| 185 | + impl = __sigprocmask_mips if ql.arch.type is QL_ARCH.MIPS else __sigprocmask |
| 186 | + |
| 187 | + return impl(ql, how, newset, oldset) |
| 188 | + |
| 189 | + |
28 | 190 | def ql_syscall_signal(ql: Qiling, sig: int, sighandler: int): |
29 | 191 | return 0 |
0 commit comments