# Crash when accessing UnsafeMutablePointer with “-Xlinker -debug” flags

**URL:** <https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139>\
**Category:** Windows\
**Tags:** windows, cmake\
**Created:** [May 29, 2024, 7:15pm UTC](https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139 "2024-05-29T19:15:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arasan01](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/arasan01/32/29218_2.png) [@arasan01](https://forums.swift.org/u/arasan01)\
**Post date:** [May 29, 2024, 7:15pm UTC](https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139/1 "2024-05-29T19:15:45Z")

</div>

# What happened?

Accessing UnsafeMutablePointer and other pointers with Swift's -Xlinker -debug enabled raises an Exception

Environment

```swift
S:\src\swift-issue-windows\hand>swift --version
compnerd.org Swift version 6.0-dev (LLVM 1a1e1e53e130e97, Swift b93c0be01b9d7dd)
Target: x86_64-unknown-windows-msvc

```

CMakeLists.txt

```cmake
project(hello LANGUAGES Swift)

add_link_options(
  "-Xlinker" "-debug"
)

add_executable(hello hello.swift)

```

Hello.swift

```swift
let allocatePtr = UnsafeMutablePointer<Int>.allocate(capacity: 1)
allocatePtr.initialize(to: 42)
let s = allocatePtr.pointee + 1
print(s) // throw Exception

```

using command

```swift
mkdir build
cd build
cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
cmake --build .
lldb hello.exe

```

output log

```swift
* thread #1, stop reason = Exception 0xc0000005 encountered at address 0x7ffd901ad8e4: Access violation reading location 0x7ff6d62bfc19
    frame #0: 0x00007ffd901ad8e4 swiftCore.dll`swift_conformsToProtocol + 5556
swiftCore.dll`swift_conformsToProtocol:
-> 0x7ffd901ad8e4 <+5556>: movq (%rax), %rax
    0x7ffd901ad8e7 <+5559>: jmp 0x7ffd901ad8eb ; <+5563>
    0x7ffd901ad8e9 <+5561>: xorl %eax, %eax
    0x7ffd901ad8eb <+5563>: movq (%rsi), %rcx

```

On Windows, “app.pdb” can be generated by using “-Xlinker -debug” flags. By explicitly setting this option in CMakeLists.txt, I can debug with WinDbg and LLDB. Is this a bug in Swift or lld-link.exe? Or is there an error in my code?

> **[GitHub - arasan01/swift-issue-pointer](https://github.com/arasan01/swift-issue-pointer)**
>
> Contribute to arasan01/swift-issue-pointer development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [July 16, 2024, 6:17pm UTC](https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139/2 "2024-07-16T18:17:08Z")

</div>

Are you always doing a clean build and hitting this? If there is an incremental build, this would be a problem. This is something that LLD does not support and thus if you are linking with lld, it should work fine (`-use-ld=lld`). The padding for incremental linking causes issues for the runtime initialisation.

---

<div class="post-metadata">

**Author:** ![arasan01](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/arasan01/32/29218_2.png) [@arasan01](https://forums.swift.org/u/arasan01)\
**Post date:** [July 17, 2024, 4:14pm UTC](https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139/3 "2024-07-17T16:14:01Z")

</div>

thanks @compnerd -san.

The reason for my mistake was that I had assumed LLD would be used even without specifying -use-ld. In reality, for static linking, if nothing is specified, 'link' is used as is. I understood this by reading [WindowsToolChains.cpp](https://github.com/swiftlang/swift/blob/main/lib/Driver/WindowsToolChains.cpp). By specifying the `-use-ld=lld` option that you suggested, it worked correctly.

> The padding for incremental linking causes issues for the runtime initialisation.

I understand why this was failing as an "Access violation reading location 0xAddress". Thank you for explaining it to me.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [July 17, 2024, 4:20pm UTC](https://forums.swift.org/t/crash-when-accessing-unsafemutablepointer-with-xlinker-debug-flags/72139/4 "2024-07-17T16:20:02Z")

</div>

`link.exe` is the platform linker and is preferred (just as `ld64` is the preferred platform linker on Darwin).

For SPM, we default to lld by means of an external configuration file (`Info.plist`) which specifies `SWIFTC_FLAGS` which is used as the default set of flags to pass the compiler. Included in this is `-use-ld=lld`. For CMake, no such override is considered which is why you need to specify it.
