Giter VIP home page Giter VIP logo

Comments (8)

mlugg avatar mlugg commented on May 29, 2024 10

Outside of comptime, the developer rarely means "Compiler, optimize this code in a way that leverages the fact that this code will never be reached."

On the contrary, this is precisely what I mean when I write an assertion. When I write assert(xyz), I am stating that xyz will always be true. If it is not true in any case, I of course would like to know about that when safety is enabled; but otherwise, I expect the optimizer to utilize that fact when it makes sense. It's no different than any other safety check.

This proposal doesn't just lose the optimizations made possible by certain code paths being unreachable (although that is still itself a major loss); it also requires unnecessary code to be evaluated, even in ReleaseFast. It is not uncommon for me to write code like assert(nonTrivialFunction(foo)) -- I absolutely do not want that function to be called in unsafe optimized builds, because it wastes runtime to check for a bug which I ought to have already caught in debugging. I highly doubt you would propose to make other safety checks apply in ReleaseFast; why should unreachable be any different?

At worst, it's the start of a security vulnerability.

If you are not confident in the correctness of your assertions, you should not run the code in security-critical environments. Assertions are intended to be factual statements about how your code's logic works: an assertion failing indicates a bug which, in all likelihood, could manifest regardless of the assertion. We can't magically guard against incorrect code. It is not appropriate to complicate the language, and hugely limit optimization capabilities, just in order for certain incorrect code to manifest itself in a safer way in release builds.

If you are working in a complex codebase where performance is less critical and there are many trivial assertions, it may indeed be appropriate to create a "custom" assertion function which panics in all release modes. This should not be the default.

[...] the keyword unreachable does not make that clear enough to the developer.

As already mentioned by the commenters above me, the keyword is completely unambiguous. I very rarely see anyone misinterpret its meaning, and when I do, they are virtually always complete beginners to the language. The word "unreachable" in English literally describes "a location that cannot be reached", and that is precisely what the construct models.

There are enough commits in Zig's codebase where developers made this mistake to disprove this.

I highly doubt that many -- if any -- of the examples you brought up were people misunderstanding the keyword. unreachable is not a complex construct, and is widely understood by basically everyone with a reasonable understanding of Zig. Instead, placing unreachable here is a simple logic error: the author thought this code path was unreachable, but it was not. Zig, including its standard library, is v0 software which undergoes rapid change -- there are going to be bugs. That isn't a reason to hinder the optimizer for everyone on the off-chance that someone deploys software without adequate testing in a security-critical environment in a manner where the failure happens to be caught by this mechanism (rather than being a more subtle logic error, or other illegal behavior).

from zig.

Vexu avatar Vexu commented on May 29, 2024 5

Another solution to your use case would be implementing #13339 and compiling Bun with -OReleaseFast+unreachable.

from zig.

silversquirl avatar silversquirl commented on May 29, 2024 5

If you are not confident in the correctness of your assertions, you should not run the code in security-critical environments.

I'd like to add that if you care about security you should be a) exhaustively testing and fuzzing your code, with runtime safety enabled; and possibly even b) shipping in ReleaseSafe, so that when something does go wrong, you immediately crash.

from zig.

silversquirl avatar silversquirl commented on May 29, 2024

It should be extremely clear to the developer when the compiler will use this information in optimizations.

I would argue the name unreachable is very clear - it means "I guarantee this line will never be reached". If people are using it without understanding what it does, that's a documentation problem.

from zig.

xdBronch avatar xdBronch commented on May 29, 2024

theres also precedent for unreachable to mean this or something very similar, C has __builtin_unreachable() and rust has std::hint::unreachable_unchecked() which is functionally identical to zigs. i think the solution to this is stricter testing and/or using ReleaseSafe which is already planned for zig

from zig.

Jarred-Sumner avatar Jarred-Sumner commented on May 29, 2024

I would argue the name unreachable is very clear - it means "I guarantee this line will never be reached". If people are using it without understanding what it does, that's a documentation problem.

There are enough commits in Zig's codebase where developers made this mistake to disprove this

theres also precedent for unreachable to mean this or something very similar, C has __builtin_unreachable() and rust has std::hint::unreachable_unchecked() which is functionally identical to zigs. i think the solution to this is stricter testing and/or using ReleaseSafe which is already planned for zig

In both of those cases, unreachable is not a keyword. __builtin_unreachable() is a scary-sounding function. rust's std::hint__unreachable_unchecked() makes it extremely clear that it is unchecked, which is why unreachable! is the default people use instead (which panics)

Note that this is less about Zig's codebase and more about the general problem. We've run into many issues related to unreachable at Bun and now use it very rarely

from zig.

xdBronch avatar xdBronch commented on May 29, 2024

__builtin_unreachable() is a scary-sounding function

i dont really understand your point? i dont see how it being a function instead of a keyword all of a sudden makes it scary

rust's std::hint__unreachable_unchecked() makes it extremely clear that it is unchecked

thats fair but surely we shouldnt shape the language around things that people who are unfamiliar with zig assume about it? zigs unreachable is actually the safest of the 3 here, Cs is completely unchecked and rusts is unchecked in any release mode afaik, zig gives you the choice to keep these things checked while still getting optimizations

from zig.

andrewrk avatar andrewrk commented on May 29, 2024

Recent incorrect usages of unreachable in the Zig compiler

Your very first example is bogus. You're assuming the catch unreachable was an invalid assumption and the commit was to fix it but if you actually look at 0a3dff8 you'll see that's not the case.

Anyway it's moot. This proposal is never going to happen. It destroys the heart of the language.

It sounds like you're using ReleaseFast but you want to be using ReleaseSafe.

from zig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.