Commit Graph

16 Commits

Author SHA1 Message Date
Alastair Houghton
6c7eda887e [Backtracing] Harden output path setting.
We should harden the output path setting so that it can't be used
as part of an exploit to get a crashing process to overwrite a
file at an attacker-controller path, or to divert the crash report
to `/dev/null` to hide their tracks or other such undesirable
activity.

Take a copy of the setting at start-up and write-protect it to
prevent attackers overwriting it.

Note that we already protect against attempts to trigger the
backtracer from privileged programs (both on Darwin and Linux);
this is really a belt and braces measure to make life harder for
attackers.

rdar://136977833
2025-01-31 14:36:56 +00:00
Alastair Houghton
9bdd9e73dc [Backtracing] Support redirection to a named file.
Add the ability to specify a filename or directory name as the output-to
setting in `SWIFT_BACKTRACE`.  If the option is set to a directory name,
generate a unique filename in that directory using the process name,
process ID and timestamp.

rdar://136977833
2025-01-29 10:20:15 +00:00
Alastair Houghton
dd0b826235 [Runtime] Improved symbolication option for backtracing.
Rather than just on or off, I've changed it to allow "off", "fast",
or "full".  "fast" means that we'll do symbol lookup, but we won't
try to find inline frames and we won't run line number programs
(those are the things that are taking considerable time in some
cases).

rdar://122302117
2024-02-06 17:38:20 +00:00
Alastair Houghton
8269bf72eb [Runtime] Add an option to produce non-symbolicated backtraces.
Symbolication can take some time, depending on the binaries involved.
In certain contexts it's better for the backtrace to finish quickly,
and then symbolication could be done offline.

rdar://122302117
2024-02-05 12:56:47 +00:00
Alastair Houghton
5c427124ae [Backtracing] Fix ARM64e build failure.
Accessing the thread context structures is complicated because their
member variables change name depending on various macros.  This caused
a build failure for ARM64e.

rdar://118402226
2023-11-14 17:37:00 +00:00
Alastair Houghton
9ca81c63b1 [Backtracing] Massage the output a little in various cases.
We shouldn't try to erase the message when in non-color mode; mostly
that's intended for redirected output scenarios anyway, and in that case
we don't really want garbage in the output.

Also, improve the failure messages when the backtracer itself goes
wrong or can't be executed.

Finally, change the behaviour slightly so that *if* we're explicitly
enabled, *and* the backtracer path wasn't explicitly specified, *and*
we can't find the backtracer, we print a warning on start-up.  We
don't do that in any other case because we don't want spurious warnings
everywhere.

rdar://118055527
2023-11-09 18:12:18 +00:00
Alastair Houghton
9d462a7b69 [Backtracing] Add an indication that we're working on a backtrace.
When we crash, emit a message straight away that says we're working
on a backtrace.  If starting the backtracer fails, report that also.

Finally, add a duration to the messages output by the backtracer, so
that we can see how long it took.

rdar://118055527
2023-11-09 18:12:18 +00:00
Alastair Houghton
2f0e42ba8d [Backtracing] Use stderr by default unless interactive.
This means we don't need to specify `output-to` in CI.

rdar://110371557
2023-06-07 13:54:36 +01:00
Alastair Houghton
2dcaa6f14f [Backtracing][Linux] Add Linux crash handler to the runtime.
This also adds a function to demangle a symbol, and a way for the
backtracing code to report warning messages to the same place as
the main runtime.

I'd like to rename the _swift_isThunkFunction() SPI also, but we
can't do that until we've made the changes to the _Backtracing
library, so we'll do that there instead.

rdar://110261430
2023-06-05 15:39:50 +01:00
Alastair Houghton
e5680de071 [Backtracing] Security improvements.
Use `task_read_for_pid()` rather than having the crashing program pass its
own task port through.  This opts us in to additional OS security measures
surrounding the use of this call.

rdar://107362003
2023-04-12 12:48:33 +01:00
Alastair Houghton
9209bdd8c4 [Backtracing] Add an option to output to stderr.
In CI, it would be better if the backtraces went to stderr rather than
stdout, so provide an option for that.

rdar://107192120
2023-03-24 17:13:52 +00:00
Alastair Houghton
1258d45152 [Backtracing] Build work.
Additional shimming required for some builds, as well as a few other build
related tweaks.

rdar://106234311
2023-03-04 15:46:30 +00:00
Alastair Houghton
43ac069ad0 [Backtracing] Add control over symbol caching.
Some symbolication frameworks have a symbol cache; we probably don't want
to use that for test cases, to avoid running into problems where the cache
holds stale information.

rdar://105409147
2023-03-04 08:00:09 +00:00
Alastair Houghton
4edfacb16b [Backtracing][Runtime] Fix a copy/paste blunder.
Accidentally wrote `thread_resume()` instead of `thread_suspend()`.

rdar://105391747
2023-03-04 07:56:03 +00:00
Alastair Houghton
b9e79c431f [Backtracing][Runtime] Don't replace existing signal handlers.
If we find signal handlers already installed, leave them alone.

rdar://105391747
2023-03-04 07:56:03 +00:00
Alastair Houghton
8a29657f4c [Runtime] [Darwin] Add code to trigger the external backtracer.
When a Swift program crashes, we should catch the crash and execute the
external backtracer.

rdar://105391747
2023-03-04 07:56:02 +00:00