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

[SR-11741] Fix "dangling pointer" warning in benchmark suite #54148

Closed
hamishknight opened this issue Nov 8, 2019 · 7 comments
Closed

[SR-11741] Fix "dangling pointer" warning in benchmark suite #54148

hamishknight opened this issue Nov 8, 2019 · 7 comments
Labels
benchmarks bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-11741
Radar None
Original Reporter @hamishknight
Type Bug
Status Closed
Resolution Done
Environment

Swift version 5.1.1-dev (Swift a90450e8c1)
Target: x86_64-apple-darwin19.0.0

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, Benchmark
Assignee jacobmao0416 (JIRA)
Priority Medium

md5: 298dbcfb2f25566234230bd6b2cde0fe

Issue Description:

Currently compiling the benchmark suite emits the following warning:

swift/benchmark/utils/DriverUtils.swift:415:15: warning: initialization of 'UnsafeMutablePointer<rusage_info_v4>' results in a dangling pointer
      let p = UnsafeMutablePointer(&u)
              ^~~~~~~~~~~~~~~~~~~~~~~~
swift/benchmark/utils/DriverUtils.swift:415:36: note: implicit argument conversion from 'rusage_info_v4' to 'UnsafeMutablePointer<rusage_info_v4>' produces a pointer valid only for the duration of the call to 'init(_:)'
      let p = UnsafeMutablePointer(&u)
                                   ^~
swift/benchmark/utils/DriverUtils.swift:415:36: note: use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope
      let p = UnsafeMutablePointer(&u)
                                   ^

We should fix this such that we don't escape the inout-to-pointer conversion.

@beccadax
Copy link
Contributor

@gottesmm Who owns the benchmark suite?

@gottesmm
Copy link
Member

I wouldn't say that anyone owns it specifically. If this is fixing a real issue, feel free to change. We always fix bugs. Put up a PR and lets see if the fix changes performance?

FYI: @atrick@eeckstein

@gottesmm
Copy link
Member

Oh, I see this is just in the driver. Please put up a PR. I can review this. The change will for sure be tested by a full test.

@swift-ci
Copy link
Collaborator

Comment by Jacob Mao (JIRA)

Hi:
Can I take this one?

I am going to change the code to:

let p = UnsafeMutablePointer(withUnsafeMutablePointer(to: &u) { return $0 })

@hamishknight
Copy link
Collaborator Author

jacobmao0416 (JIRA User) Sure thing, feel free to assign yourself! Note though that the pointer provided by withUnsafeMutablePointer(to:) is only valid within the scope of the closure passed to it, attempting to escape it is undefined behaviour. What you'll want to do is move the call to proc_pid_rusage inside the closure, e.g:

withUnsafeMutablePointer(to: &u) { p in
  p.withMemoryRebound(to: Optional<rusage_info_t>.self, capacity: 1) { up in
    let _ = proc_pid_rusage(getpid(), RUSAGE_INFO_V4, up)
  }
}

Though while we're here we should also avoid using withMemoryRebound, as the type we're rebinding to has a completely different layout. I believe we should be able to use assumingMemoryBound(to:) instead given we're passing the resulting pointer off to a C API.

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 9, 2019

Comment by Jacob Mao (JIRA)

@hamishknight, thanks

I have created pr for this:

#28648

@hamishknight
Copy link
Collaborator Author

Fixed in the above linked PR, thanks jacobmao0416 (JIRA User)!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
benchmarks bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants