Skip to content
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

Crash when hashing a Zalgo string literal #73638

Closed
karwa opened this issue May 15, 2024 · 3 comments
Closed

Crash when hashing a Zalgo string literal #73638

karwa opened this issue May 15, 2024 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@karwa
Copy link
Collaborator

karwa commented May 15, 2024

Description

No response

Reproduction

var h = Hasher()
workload_zalgo.hash(into: &h)
print(h.finalize())

let workload_zalgo = "t\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{032a}\u{032b}\u{032c}\u{032d}\u{032e}\u{032f}\u{0330}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{0316}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0331}\u{0332}\u{0333}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}h\u{0300}\u{0301}\u{0302}\u{0303}e\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{0316}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{032a}\u{032b}\u{032c}\u{032d}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}q\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{0316}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{032a}\u{032b}\u{032c}\u{032d}\u{032e}\u{032f}\u{0330}\u{0331}\u{0332}\u{0333}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}u\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}i\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{032a}\u{032b}\u{032c}\u{032d}\u{032e}\u{032f}\u{0330}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{036c}\u{036d}\u{036e}c\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0314}\u{0315}\u{0316}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{032a}\u{032b}\u{032c}\u{032d}\u{032e}\u{032f}\u{0330}\u{0331}\u{0332}\u{0333}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}k\u{0300}\u{0301}\u{0302}\u{0303}\u{0304}\u{0305}\u{0306}\u{0307}\u{0308}\u{0309}\u{030a}\u{030b}\u{030c}\u{030d}\u{030e}\u{030f}\u{0310}\u{0311}\u{0312}\u{0313}\u{0314}\u{0315}\u{0316}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0323}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0329}\u{032a}\u{032b}\u{032c}\u{032d}\u{032e}\u{032f}\u{0330}\u{0331}\u{0332}\u{0333}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}b\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0320}\u{0321}\u{0322}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}r\u{0329}\u{032a}\u{032b}\u{032f}\u{0330}\u{0331}\u{0332}\u{0333}\u{0334}\u{0335}\u{0336}\u{0337}\u{0338}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{032c}\u{032d}\u{032e}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{031d}\u{031e}\u{031f}\u{0324}\u{0325}\u{0326}\u{0327}\u{0328}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0320}\u{0321}\u{0322}\u{0323}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}ow\u{0317}\u{0318}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0319}\u{031a}\u{031b}\u{031c}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}n\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}f\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{035c}\u{035d}\u{035e}\u{035f}\u{0360}\u{0361}\u{0362}\u{0363}\u{0364}\u{0365}\u{0366}o\u{0317}\u{0318}\u{0319}\u{031a}\u{031b}\u{031c}\u{0339}\u{033a}\u{033b}\u{033c}\u{033d}\u{033e}\u{033f}\u{0340}\u{0341}\u{0342}\u{0343}\u{0344}\u{0345}\u{0346}\u{0347}\u{0348}\u{0349}\u{034a}\u{034b}\u{034c}\u{034d}\u{034e}\u{034f}\u{0350}\u{0351}\u{0352}\u{0353}\u{0354}\u{0355}\u{0356}\u{0357}\u{0358}\u{0359}\u{035a}\u{035b}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}x\u{0363}\u{0364}\u{0365}\u{0366}\u{0367}\u{0368}\u{0369}\u{036a}\u{036b}\u{036c}\u{036d}\u{036e}"

The crash happens both in the repl and compiled binaries.

Stack dump

Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret main.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk -color-diagnostics -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name main -disable-clang-spi -target-sdk-version 14.0 -target-sdk-name macosx14.0 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins
1.	Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
2.	Compiling with the current language version
3.	While running user code "main.swift"
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010520f14c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000107d8df2c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000103acf10c SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001917cf584 _sigtramp + 56
4  libswiftCore.dylib       0x00000001a166a1fc $ss13_StringObjectV10sharedUTF8SRys5UInt8VGvg + 24
5  libswiftCore.dylib       0x00000001a1654d00 $ss16_StringGutsSliceV13_fastNFCCheckyySbz_s5UInt8VztF + 740
6  libswiftCore.dylib       0x00000001a1649aec $ss16_StringGutsSliceV17_withNFCCodeUnitsyyys5UInt8VKXEKF06$ss16_abc40V15_normalizedHash4intoys6HasherVz_tFys5G7VXEfU0_s0L0VTf1cn_n + 404
7  libswiftCore.dylib       0x00000001a1649044 $ss16_StringGutsSliceV15_normalizedHash4intoys6HasherVz_tF + 128
8  libswiftCore.dylib       0x000000010b224080 $ss16_StringGutsSliceV15_normalizedHash4intoys6HasherVz_tF + 18446744071188623548
9  swift-frontend           0x000000010640e054 llvm::orc::runAsMain(int (*)(int, char**), llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, llvm::Optional<llvm::StringRef>) + 1276
10 swift-frontend           0x0000000107d82cd4 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>> const&, swift::IRGenOptions const&, swift::SILOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>&&) + 11524
11 swift-frontend           0x0000000107d42f20 processCommandLineAndRunImmediately(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>&&, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::FrontendObserver*, int&) + 484
12 swift-frontend           0x0000000107623524 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1464
13 swift-frontend           0x0000000107d45e14 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 3020
14 swift-frontend           0x0000000107d49474 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
15 swift-frontend           0x000000010768f4e0 swift::mainEntry(int, char const**) + 4116
16 dyld                     0x00000001914160e0 start + 2360
zsh: segmentation fault  swift main.swift

Expected behavior

No crash

Environment

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0

(Also happens on current main, so it hasn't been fixed yet)

Additional information

No response

@karwa karwa added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels May 15, 2024
@karwa
Copy link
Collaborator Author

karwa commented May 15, 2024

It's probably to do with it being a literal, because you can avoid the crash by mutating:

var workload_zalgo = ...
workload_zalgo += String(Int.random(in: 0...5))

@karwa karwa changed the title Crash when hashing a Zalgo string Crash when hashing a Zalgo string literal May 15, 2024
@tbkka
Copy link
Contributor

tbkka commented May 15, 2024

This is a known bug with top-level code. Top-level code requires definitions to precede their use; move let workload_zalgo ... in your first example to the top of the file and it should work just fine.

Generally, you should always put all your code inside a function. You can call that function from top-level code if you need to execute it directly:

func main() {
    ... stuff ...
}

main()

@Azoy
Copy link
Member

Azoy commented May 21, 2024

Closing this as this isn't an actual issue with hashing or strings

@Azoy Azoy closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

3 participants