swift-server / swift-backtrace

💥 Backtraces for Swift on Linux and Windows
Apache License 2.0
294 stars 36 forks source link

"unable to protect … swift-backtrace … disabling backtracing" #72

Open marc-medley opened 3 months ago

marc-medley commented 3 months ago

Could an "unable to protect … swift-backtrace … disabling backtracing" message come from this package via a Swift Community Apt Repository distribution of Swift? If yes, what might steps resolve the "unable to protect" issue. If not, any idea of where I should be asking this question?

Scenario: Swift Community Apt Repository installation of Swift on a Raspberry Pi 5 with Raspberry Pi OS.

Steps: The command swift --version is sufficient to invoke the "unable to protect" messages:

swift --version
#swift runtime: unable to protect path to swift-backtrace at 0x7fff2ffa7000: 22; disabling backtracing.
#swift runtime: unable to protect environment for swift-backtrace at 0x7fff2ff9f000: 22; disabling backtracing.
#swift runtime: unable to protect path to swift-backtrace at 0x7fff8a907000: 22; disabling backtracing.
#swift runtime: unable to protect environment for swift-backtrace at 0x7fff8a8ff000: 22; disabling backtracing.
#swift runtime: unable to protect path to swift-backtrace at 0x7fffafe97000: 22; disabling backtracing.
#swift runtime: unable to protect environment for swift-backtrace at 0x7fffafe8f000: 22; disabling backtracing.
#Swift version 5.10 (swift-5.10-RELEASE)
#Target: aarch64-unknown-linux-gnu
blainerothrock commented 2 months ago

+1

futurejones commented 2 months ago

@marc-medley, I have been looking into this and it appears to be a Raspberry Pi 5 only issue.

If you run the official Swift.org ubuntu 22.04 5.10.1 docker release from here - https://hub.docker.com/_/swift on the Raspberry Pi 5 you will also get the same warning messages.

user@raspberrypi5:~ $ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
user@raspberrypi5:~ $ swift --version
swift runtime: unable to protect path to swift-backtrace at 0x7ffecbd67000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7ffecbd5f000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7fff7b2a7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff7b29f000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7fff710f7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff710ef000: 22; disabling backtracing.
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: aarch64-unknown-linux-gnu
user@raspberrypi5:~ $ docker run  --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -it --user root --rm swift:latest   /bin/bash
root@86390ca1586c:/# cat /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
root@86390ca1586c:/# swift --version
swift runtime: unable to protect path to swift-backtrace at 0x7fff0eeb7000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff0eeaf000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7ffecdb57000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7ffecdb4f000: 22; disabling backtracing.
swift runtime: unable to protect path to swift-backtrace at 0x7fff54447000: 22; disabling backtracing.
swift runtime: unable to protect environment for swift-backtrace at 0x7fff5443f000: 22; disabling backtracing.
Swift version 5.10.1 (swift-5.10.1-RELEASE)
Target: aarch64-unknown-linux-gnu
root@86390ca1586c:/# 

None of these warning messages are occurring when testing on the Raspberry Pi 4.

This indicates that the issue is with the Raspberry Pi 5 and not with the Swift installation or package source.

At this point I haven't been able to find any explanation for what is happening.

marc-medley commented 2 months ago

@futurejones

…it appears to be a Raspberry Pi 5 only issue.

This matches my findings. I had concurrently set up full clean installs of operating systems and Swift (via Swift Community Apt Repository) on the following:

Only the Raspberry Pi 5 had the "unable to protect … swift-backtrace" issue.

Some key differences that I could find relative to Pi 4 vs Pi 5 are as follows:

Raspberry Pi 4

uname --kernel-release
# 6.6.31+rpt-rpi-v8

lscpu
# Architecture:             aarch64
#   CPU op-mode(s):         32-bit, 64-bit
# Vendor ID:                ARM
#   Model name:             Cortex-A72
#     Model:                3

Raspberry Pi 5

uname --kernel-release
# 6.6.31+rpt-rpi-2712

lscpu
# Architecture:             aarch64
#   CPU op-mode(s):         32-bit, 64-bit
# Vendor ID:                ARM
#   Model name:             Cortex-A76
#     Model:                1

First, I wondered if this was something that I could address by configurating some aspect of the OS & Swift toolchain … StackOverflow: How to resolve "unable to protect" "swift-backtrace"?

Later I reached out here and on the Swift Forum … Swift Forum: How to resolve "unable to protect" "swift-backtrace"?

This indicates that the issue is with the Raspberry Pi 5 and not with the Swift installation or package source. At this point I haven't been able to find any explanation for what is happening.

If it's not a packaging/install-process issue, then perhaps it's ultimately upstream in Swift itself??

GitHub: swiftlang/swift … runtime/Backtrace.cpp

_swift_backtraceSetupEnvironment();

if (!VirtualProtect(swiftBacktraceEnv,
                    sizeof(swiftBacktraceEnv),
                    PAGE_READONLY,
                    NULL)) {
  swift::warning(0,
                 "swift runtime: unable to protect environment "
                 "for swift-backtrace: %08lx; disabling backtracing.\n",
                 ::GetLastError());
  _swift_backtraceSettings.enabled = OnOffTty::Off;
}
al45tair commented 1 month ago

It's the page size. Swift assumes it can work out the page size at build time, and picks 4KB for everything except Apple Silicon (where we use 16KB), but Raspberry Pi 5 uses 16KB pages (well, technically, granules, but that's nitpicking). The upshot is that some mprotect() calls that we use to make the runtime's backtracer safe to use will fail because they won't be page aligned.

Note that this is nothing to do with the swift-backtrace repo. It's the Swift runtime's built-in backtrack (i.e. the new one) that gets disabled here.