Skip to content

Overwriting a file with Data.write(to:) causes an error #5584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kkebo opened this issue Jun 8, 2024 · 0 comments · Fixed by swiftlang/swift-corelibs-foundation#4976
Closed

Comments

@kkebo
Copy link

kkebo commented Jun 8, 2024

Description

The Wasm binary built with SwiftWasm can use Foundation's Data.write(to:). However, when the file already exists, the write itself succeeds but an error occurs.

That's because it tries to set permissions even though it does not have permission concept.

Steps to reproduce

$ tree .
.
├── Package.swift
└── Sources
    └── main.swift

2 directories, 2 files
$ cat Package.swift
// swift-tools-version: 6.0

import PackageDescription

let package = Package(
    name: "write_test",
    targets: [
        .executableTarget(name: "write_test")
    ]
)
$ cat Sources/main.swift
import Foundation
try! "test".data(using: .utf8)!.write(to: URL(fileURLWithPath: "./test.txt"))
$ swift build --swift-sdk wasm32-unknown-wasi
Building for debugging...
[1/1] Write swift-version-31C605C61487384.txt
Build complete! (0.13s)
$ wasmtime run --dir . .build/debug/write_test.wasm
$ cat test.txt
test
$ wasmtime run --dir . .build/debug/write_test.wasm
write_test/main.swift:3: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=512 "(null)"
Error: failed to run main module `.build/debug/write_test.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x3a612 - <unknown>!$ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF
           1: 0xe04c8 - <unknown>!swift_unexpectedError
           2: 0x24192 - <unknown>!main
           3: 0xbd138c - <unknown>!__main_void
           4: 0x23b2b - <unknown>!_start
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    2: wasm trap: wasm `unreachable` instruction executed

Environment

$ uname -a
Linux Brown-rhinoceros-beetle 6.8.10-400.asahi.fc40.aarch64+16k #1 SMP PREEMPT_DYNAMIC Mon May 27 16:05:41 UTC 2024 aarch64 GNU/Linux
$ which swift
/home/kebo/.local/bin/swift
$ swiftly list
Installed release toolchains
----------------------------

Installed snapshot toolchains
-----------------------------
6.0-snapshot-2024-06-07
main-snapshot-2024-06-07
main-snapshot-2024-06-06 (in use)
$ swift --version
Swift version 6.0-dev (LLVM 70f5988c1c84591, Swift 4f8181dde6a5a62)
Target: aarch64-unknown-linux-gnu
$ swift sdk list
DEVELOPMENT-SNAPSHOT-2024-06-07-a-wasm32-unknown-wasi
$ wasmtime -V
wasmtime-cli 19.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant