| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
""" |
| 6 |
In asm code, out-of-bounds heap accesses cause segfaults, which the engine |
| 7 |
handles internally. Make GDB ignore them. |
| 8 |
""" |
| 9 |
|
| 10 |
import gdb |
| 11 |
|
| 12 |
SIGSEGV = 11 |
| 13 |
|
| 14 |
# A sigaction buffer for each inferior process. |
| 15 |
sigaction_buffers = {} |
| 16 |
|
| 17 |
|
| 18 |
def on_stop(event): |
| 19 |
if isinstance(event, gdb.SignalEvent) and event.stop_signal == "SIGSEGV": |
| 20 |
# Allocate memory for sigaction, once per js shell process. |
| 21 |
process = gdb.selected_inferior() |
| 22 |
buf = sigaction_buffers.get(process) |
| 23 |
if buf is None: |
| 24 |
buf = gdb.parse_and_eval( |
| 25 |
"(struct sigaction *) malloc(sizeof(struct sigaction))" |
| 26 |
) |
| 27 |
sigaction_buffers[process] = buf |
| 28 |
|
| 29 |
# See if WasmFaultHandler is installed as the SIGSEGV signal action. |
| 30 |
sigaction_fn = gdb.parse_and_eval( |
| 31 |
"(void(*)(int,void*,void*))__sigaction" |
| 32 |
).dereference() |
| 33 |
sigaction_fn(SIGSEGV, 0, buf) |
| 34 |
WasmTrapHandler = gdb.parse_and_eval("WasmTrapHandler") |
| 35 |
if buf["__sigaction_handler"]["sa_handler"] == WasmTrapHandler: |
| 36 |
# Advise the user that magic is happening. |
| 37 |
print("js/src/gdb/mozilla/asmjs.py: Allowing WasmTrapHandler to run.") |
| 38 |
|
| 39 |
# If WasmTrapHandler doesn't handle this segfault, it will unhook |
| 40 |
# itself and re-raise. |
| 41 |
gdb.execute("continue") |
| 42 |
|
| 43 |
|
| 44 |
def on_exited(event): |
| 45 |
if event.inferior in sigaction_buffers: |
| 46 |
del sigaction_buffers[event.inferior] |
| 47 |
|
| 48 |
|
| 49 |
def install(): |
| 50 |
gdb.events.stop.connect(on_stop) |
| 51 |
gdb.events.exited.connect(on_exited) |