Skip to content

Print stacktrace on precondition failure & assertion on pointer dereference #1695

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

lucteo
Copy link
Contributor

@lucteo lucteo commented May 23, 2025

Print stack trace when a precondition is violated.
Also add assertions on pointer derefence, checking for null pointers.

Also remove concurrent_sort example as is is flaky.

@lucteo lucteo requested a review from Copilot May 23, 2025 19:40
Copilot

This comment was marked as outdated.

@tothambrus11

This comment was marked as outdated.

Copy link

codecov bot commented Jun 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.47%. Comparing base (16d33b8) to head (f152332).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1695   +/-   ##
=======================================
  Coverage   88.47%   88.47%           
=======================================
  Files         382      382           
  Lines       31759    31759           
=======================================
  Hits        28100    28100           
  Misses       3659     3659           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lucteo lucteo requested a review from kyouko-taiga June 11, 2025 19:32
@tothambrus11 tothambrus11 requested a review from Copilot June 15, 2025 14:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances error diagnostics by printing a backtrace on assertion failures, adds null-pointer checks to unsafe pointer subscripts, and removes a flaky concurrent sort example.

  • Add precondition checks for null in PointerToMutable and Pointer unsafe subscripts
  • Extend Assert to call print_backtrace() on failure and define backtrace helpers
  • Remove the flaky concurrent_sort end-to-end test

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
Tests/EndToEndTests/TestCases/Concurrency/concurrent_sort.hylo Deleted flaky concurrent sort test
StandardLibrary/Sources/Core/PointerToMutable.hylo Added null checks before dereferencing in unsafe subscripts
StandardLibrary/Sources/Core/Pointer.hylo Added null check before dereferencing in unsafe subscript
StandardLibrary/Sources/Core/Assert.hylo Invoke print_backtrace() on assertion failure and implement backtrace utilities
Comments suppressed due to low confidence (3)

StandardLibrary/Sources/Core/Assert.hylo:67

  • There are no tests exercising the new backtrace printing. Adding a test that triggers a failure and verifies the backtrace output would help ensure this works across platforms.
fun print_backtrace() {

StandardLibrary/Sources/Core/PointerToMutable.hylo:15

  • The added null-pointer precondition isn't covered by existing tests. Consider adding unit tests that verify an assertion is triggered when dereferencing a null PointerToMutable.
precondition(self != .null(), "dereferencing null pointer")

StandardLibrary/Sources/Core/Pointer.hylo:14

  • Similarly, add tests to confirm that dereferencing a null Pointer triggers the intended precondition failure.
precondition(self != .null(), "dereferencing null pointer")

Copy link
Contributor

@kyouko-taiga kyouko-taiga left a comment

Choose a reason for hiding this comment

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

Very nice work.

One thing I don't like is the precondition added in the operation of Pointer. I left inline comments to explain my rationale.

@@ -45,6 +45,7 @@ fun log_failure_description(_ title: String, _ message: String, in file: String,
print(title, terminator: ": ")
print(message)
}
print_backtrace()
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should print the backtrace in release mode. Do we have a compiler directive to distinguish #debug or #release? I don't remember.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't seem to have such a feature yet.
(and to be honest, I would keep it in release as well, but add a different flag to disable this)

Comment on lines 74 to 79
{
&buffer.with_mutable_contiguous_storage(fun (_ p: PointerToMutable<MemoryAddress>) -> Void {
var size = backtrace(p, C.int(truncating_or_extending: backtrace_depth))
backtrace_symbols_fd(p, size, 2) // write to stderr
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to delimit a scope? Why is that scope necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sometimes (probably due to compiler bugs) the scopes are needed. In this case, it seems to work without one. I think this was a leftover from me trying to solve a lifetime issue. Will drop.

@@ -11,6 +11,7 @@ public type Pointer<Pointee>: Regular {
/// - Requires: `self` is the address of an object of type `Pointee` and its storage
/// is accessed only through this projection during the projection's lifetime.
public subscript unsafe(): Pointee {
precondition(self != .null(), "dereferencing null pointer")
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not convinced about the value of this precondition. We can't in general test that dereferencing a pointer is safe and (perhaps more importantly) we should guarantee that using the unsafe API is as fast as possible. Therefore adding branches on this path seems wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

null appears so often in low-level code, that, I think, it deserves a special check. It's just the checks that we do to ensure we are not past the end of a buffer.

In terms of performance, most of these branches should be removed by the compiler once it figures out that the pointer is valid (for most of the cases that I can think of).

(see also: https://security.googleblog.com/2024/11/retrofitting-spatial-safety-to-hundreds.html)

@hylo-lang hylo-lang deleted a comment from Copilot AI Jun 25, 2025
@lucteo lucteo requested a review from kyouko-taiga June 25, 2025 17:28
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 this pull request may close these issues.

3 participants