Page MenuHomeFreeBSD

kernel: provide panicky version of __unreachable
ClosedPublic

Authored by kevans on Feb 22 2020, 6:34 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Apr 6, 5:35 PM
Unknown Object (File)
Sun, Mar 31, 8:29 PM
Unknown Object (File)
Sat, Mar 30, 9:52 AM
Unknown Object (File)
Dec 20 2023, 7:15 AM
Unknown Object (File)
Dec 12 2023, 4:20 AM
Unknown Object (File)
Dec 8 2023, 1:38 AM
Unknown Object (File)
Dec 8 2023, 1:38 AM
Unknown Object (File)
Nov 16 2023, 6:55 PM
Subscribers

Details

Summary

__builtin_unreachable doesn't raise any compile-time warnings/errors on its own, so problems with its usage can't be easily detected. While it would be nice for this situation to change and compilers to at least add a warning for trivial cases where local state means the instruction can't be reached, this isn't the case at the moment.

This commit adds an assert_unreachable, whose intend is incredibly clear: it asserts that this instruction is unreachable. On INVARIANTS builds, it's a panic(), and on non-INVARIANTS it's unreachable().

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Ugh.. I think this was discussed when __unreachable() was introduced (D2536 perhaps), and back then it was not considered a good idea.

The idea of __unreachable() is/was exclusively for static checkers and compiler optimizations. There was some rule against adding assertions here unless we are specifically building code in "debug mode".

In D23793#522846, @pfg wrote:

Ugh.. I think this was discussed when __unreachable() was introduced (D2536 perhaps), and back then it was not considered a good idea.

The idea of __unreachable() is/was exclusively for static checkers and compiler optimizations. There was some rule against adding assertions here unless we are specifically building code in "debug mode".

Indeed, said discussion partially drove this. Right now __unreachable has been deployed in some questionable ways that leave it hard to detect actual problems; INVARIANTS is, I think, a good enough analogy for "debug mode" in the kernel.

kevans edited the summary of this revision. (Show Details)

Provide an __asserts_unreachable instead; this leaves sys/cdefs.h unmolested and provides a name that makes it clear that it functions like other assertions.

sys/sys/systm.h
120 ↗(On Diff #68690)

this really does not tell you which is one is it and this call will not even print a backtrace. iow this really wants to be panic("executing segment marked as unreachable at %s (%s:%s)", func, FILE, LINE); or so

otherwise lgtm

sys/sys/systm.h
120 ↗(On Diff #68690)

Whoops, lost that in an iteration where I converted it from a kassert_panic("Unreachable segment reached\n")) to a panic - will fix.

Include file/line/function in panic string in a similar format to the others in the area.

This revision is now accepted and ready to land.Feb 23 2020, 2:07 AM
sys/net/mppcc.c
237 ↗(On Diff #68692)

Why is there unreachable code here?

kevans added inline comments.
sys/net/mppcc.c
237 ↗(On Diff #68692)

Sorry, I overlooked this comment. =-( I have no familiarity with this, but I guess the author decided it's OK to proceed with the caller seeing this as just a failure case. I think this is part of the problem with the status quo; we don't currently have a way to detect when something's reaching bits like this that are clearly assumed to be unreachable, and they'll plow on with looking like some other failure case as a result.

Maybe this is OK on stable branches since the author's clearly designed it to continue working if their assumption is invalidated, but IMO we should be doing better on -CURRENT at least.

This revision was automatically updated to reflect the committed changes.