Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1720

Miscompile / misoptimize / Foundation bug with JSON decoding

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Foundation
    • Labels:
      None

      Description

      The following small test case, extracted from SwiftPM, fails non-deterministically when built with optimizations enabled. I haven't yet isolated it past this point, so this could be a memory smasher in XCTest, a bug in Foundation, or a misoptimization.

      I have only reproduced this failure on Ubuntu 14.04.

      This is causing non-deterministic CI test failures.

      import XCTest
      import Foundation
      
      class JSONTests: XCTestCase {
          func testDecoding() {
              func decode(_ string: String) -> Any {
                      let bytes = [UInt8](string.utf8)
                      let data = NSData(bytes: bytes, length: bytes.count)
                      return try! NSJSONSerialization.jsonObject(with: data, options: [.allowFragments])
              }
      
              _ = decode("false")
              _ = decode("true")
              switch decode("1") {
              case let asInt as Int:
                 if asInt != 1 { fatalError("unexpected value: \(asInt)") }
              case let value:
                 fatalError("unexpected type: \(value)")
              }
          }
      
          static var allTests = [("testDecoding", testDecoding)]
      }
      
      XCTMain([testCase(JSONTests.allTests)])
      

      This is the test script I am using to reproduce the problems:

      $ ~/public/swift-project/build/Ninja-ReleaseAssert/swift-linux-x86_64/bin/swiftc -O foo.swift -I ~/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_\
      64/Foundation/ -I ~/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/usr/lib/swift/ -I ~/public/swift-project/build/Ninja-ReleaseAssert/xctest-linux-x86_64/\
       -L ~/public/swift-project/build/Ninja-ReleaseAssert/xctest-linux-x86_64/ -L ~/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/ && (set -ex; for i in {0..1\
      000}; do LD_LIBRARY_PATH=$(echo ~/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/):$(echo ~/public/swift-project/build/Ninja-ReleaseAssert/xctest-linux-x8\
      6_64/) ./foo; done)
      

      This script will build and run (once adjusted for your build directory) and usually fails in the first few iterations with something like:

      ++ echo /home/daniel_dunbar/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/
      ++ echo /home/daniel_dunbar/public/swift-project/build/Ninja-ReleaseAssert/xctest-linux-x86_64/
      + LD_LIBRARY_PATH=/home/daniel_dunbar/public/swift-project/build/Ninja-ReleaseAssert/foundation-linux-x86_64/Foundation/:/home/daniel_dunbar/public/swift-project/build/Ninja-ReleaseAssert/x\
      ctest-linux-x86_64/
      + ./foo
      Test Suite 'All tests' started at 20:36:27.472
      Test Suite 'tmp.xctest' started at 20:36:27.487
      Test Suite 'JSONTests' started at 20:36:27.488
      Test Case 'JSONTests.testDecoding' started at 20:36:27.488
      fatal error: unexpected value: 17: file foo.swift, line 16
      

        Attachments

          Activity

            People

            Assignee:
            phausler Philippe Hausler
            Reporter:
            ddunbar Daniel Dunbar
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: