Skip to content

Fixes overflow trap when creating DispatchTime objects with large upt… #12387

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

Merged
merged 1 commit into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions stdlib/public/SDK/Dispatch/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ public struct DispatchTime : Comparable {
/// system sleep time), not zero nanoseconds since boot.
public init(uptimeNanoseconds: UInt64) {
var rawValue = uptimeNanoseconds
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
rawValue = (rawValue * UInt64(DispatchTime.timebaseInfo.denom)
+ UInt64(DispatchTime.timebaseInfo.numer - 1)) / UInt64(DispatchTime.timebaseInfo.numer)

// UInt64.max means distantFuture. Do not try to scale it.
if rawValue != UInt64.max && DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the scaling could always trigger an overflow, right? Is this sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I protected the operations with overflow checks, apart from the division which can't overflow, so I don't think anything else can trap.

Copy link
Contributor

@gparker42 gparker42 Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what does this if do? Is it simply trying to optimize some values on some platforms?

Copy link
Contributor Author

@ktopley-apple ktopley-apple Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first part lets UInt64.max get through unchanged, the second part avoids the arithmetic when there is no timebase scaling. So yes, the second part is an optimization (that was already there).

var (result, overflow) = rawValue.multipliedReportingOverflow(by: UInt64(DispatchTime.timebaseInfo.denom))
if !overflow {
(result, overflow) = result.addingReportingOverflow(UInt64(DispatchTime.timebaseInfo.numer - 1))
}
rawValue = overflow ? UInt64.max : result / UInt64(DispatchTime.timebaseInfo.numer)
}
self.rawValue = dispatch_time_t(rawValue)
}

public var uptimeNanoseconds: UInt64 {
var result = self.rawValue
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
result = result * UInt64(DispatchTime.timebaseInfo.numer) / UInt64(DispatchTime.timebaseInfo.denom)
var overflow: Bool

// UInt64.max means distantFuture. Do not try to scale it.
if rawValue != UInt64.max && DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom {
(result, overflow) = result.multipliedReportingOverflow(by: UInt64(DispatchTime.timebaseInfo.numer))
result = overflow ? UInt64.max : result / UInt64(DispatchTime.timebaseInfo.denom)
}
return result
}
Expand Down
29 changes: 25 additions & 4 deletions test/stdlib/Dispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

// REQUIRES: objc_interop

// FIXME: rdar://34751238 DispatchTime.addSubtract test traps
// XFAIL: CPU=armv7 || CPU=armv7k || CPU=armv7s || CPU=arm64


import Dispatch
import Foundation
import StdlibUnittest
Expand Down Expand Up @@ -119,6 +115,31 @@ DispatchAPI.test("DispatchTime comparisons") {
}
}

DispatchAPI.test("DispatchTime.create") {
var info = mach_timebase_info_data_t(numer: 1, denom: 1)
mach_timebase_info(&info)
let scales = info.numer != info.denom

// Simple tests for non-overflow behavior
var time = DispatchTime(uptimeNanoseconds: 0)
expectEqual(time.uptimeNanoseconds, 0)

time = DispatchTime(uptimeNanoseconds: 15 * NSEC_PER_SEC)
expectEqual(time.uptimeNanoseconds, 15 * NSEC_PER_SEC)

// On platforms where the timebase scale is not 1, the next two cases
// overflow and become DISPATCH_TIME_FOREVER (UInt64.max) instead of trapping.
time = DispatchTime(uptimeNanoseconds: UInt64.max - 1)
expectEqual(time.uptimeNanoseconds, scales ? UInt64.max : UInt64.max - UInt64(1))

time = DispatchTime(uptimeNanoseconds: UInt64.max / 2)
expectEqual(time.uptimeNanoseconds, scales ? UInt64.max : UInt64.max / 2)

// UInt64.max must always be returned as UInt64.max.
time = DispatchTime(uptimeNanoseconds: UInt64.max)
expectEqual(time.uptimeNanoseconds, UInt64.max)
}

DispatchAPI.test("DispatchTime.addSubtract") {
var then = DispatchTime.now() + Double.infinity
expectEqual(DispatchTime.distantFuture, then)
Expand Down