11 ms·
Building an efficient and portable programming language with Zig
- einpoklum 5y ago> Hannah: How is it possible that Zig applications run faster than C code while also having far better security guarantees? In a marketing interview with biased questions, anything is possible. TBH, I have a positive impression of zig, but this kind of interviewing doesn't help it.
- pjmlp 5y agoIndeed, Zig still hasn't a story for use after free issues.
- MaxBarraclough 5y agoNot sure what 'story' is intended to mean here. Is use-after-free undefined behaviour in Zig?
- blueblob 5y agoIs use-after-free ever really defined behavior?
- MaxBarraclough 5y agoThat would depend on the language and the libraries. You could roll your own 'safe' memory pool in C such that use-after-free doesn't produce undefined behaviour. You'd still have a bug, but it wouldn't have to invoke UB at the level of the C programming language.
- matheusmoreira 5y agoYes. Undefined behavior exists only in standards. It's perfectly possible for implementations to define everything. https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost-the-api-war/ https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost... > I first heard about this from one of the developers of the hit game SimCity, who told me that there was a critical bug in his application: > it used memory right after freeing it, a major no-no that happened to work OK on DOS but would not work under Windows where memory that is freed is likely to be snatched up by another running application right away. > The testers on the Windows team were going through various popular applications, testing them to make sure they worked OK, but SimCity kept crashing. > They reported this to the Windows developers, who disassembled SimCity, stepped through it in a debugger, found the bug, and added special code that checked if SimCity was running, and if it did, ran the memory allocator in a special mode in which you could still use memory after freeing it.
- deleted 5y ago[deleted]
- AnIdiotOnTheNet 5y agoThere isn't really anything in the language that prevents use-after-free, partially because allocation isn't even part of the language and exists entirely in userland code. However, IIRC the standard library does include an allocator that tries to detect use-after-free.
- skohan 5y agoIsn't that an intentional design decision in Zig? As far as I understood, Zig isn't trying to "solve" memory management, it's trying to provide manual memory management with better tools than C to help you do it right, but it's explicitly not trying to save you from yourself in this respect.
- pjmlp 5y agoExcept that C and C++ already provide similar tools, one just needs to actually enable them.
- MaxBarraclough 5y agoI don't follow. You recently pointed out to me that most of the serious security bugs in the Chromium codebase (C++) are rooted in memory-management. [0] It's no simple thing to fix these bugs. Not even Google can do so in their most treasured code. [0] https://news.ycombinator.com/item?id=26862330 https://news.ycombinator.com/item?id=26862330
- pjmlp 5y agoThe point being that Zig improves very little regarding that, because it is based on the same kind of tooling.
- matheusmoreira 5y ago> allocation isn't even part of the language It's not supposed to be. In most cases, memory allocation is an operating system concept. As a systems programming language, zig must not abstract this away from the programmer.
- smasher164 5y agoThe question is if this should be done at the language-level or library-level. With sufficient metaprogramming capabilities, an enterprising programmer could write safe resource management abstractions for Zig. However, this wouldn't make the language "safe by default," which seems to be what Rust programmers are bringing with this criticism. Even if Zig doesn't bring safety by default to the language, however, I think you could get it in practice by making the standard library enforce safety. Then, unless someone goes out of their way to write their own libraries from scratch and eschew all of the safety mechanisms, they would likely live in this safety bubble.
- pcwalton 5y agoCan you write a borrow checker that enforces alias-xor-mut using metaprogramming? I'm skeptical, because of the flow sensitivity you really want to make it practical.
- smasher164 5y agoNot sure, but I became optimistic of there being some solution after I saw someone implement affine typing using stateful template metaprogramming: https://godbolt.org/z/PnPPrnPjY https://godbolt.org/z/PnPPrnPjY.
- pcwalton 5y agoThat's neat, but also flow-insensitive (and wow, generating a definition for every single use of a variable can't be good for compile-time memory usage).
- smasher164 5y agoYeah, I'm unaware of any push to bring flow-sensitive typing, or a mechanism that affords flow-sensitive analysis, to Zig.
- pron 5y agoYou could with some changes to the type capabilities, but I don't think that's the right direction. The main question is do we want soundness or not? This is a general question for various correctness properties, and, at least in the formal methods space, the answer seems to be "not always." Soundness has a cost, and stopping 100% of UAF bugs for the cost of making the language more complex and even adding a few bugs of other kinds, might not be worth it if you can stop 99% of them for a fraction of the cost. I think the goal should be to not have soundness -- IMO it has more downsides than upsides in this case -- and make good runtime detection joined with fast compile/test cycle and even automatic test generation.
- bsedlm 5y agoI like to think Zig is what we would have if we knew back then (when desiging C) what we know now. (note: "we" excludes me personally but I mean it as "us who work and study comptuing at large")
- pjmlp 5y agoWe already knew in 1981. "Many years later we asked our customers whether they wished us to provide an option to switch off these checks in the interests of efficiency on production runs. Unanimously, they urged us not to--they already knew how frequently subscript errors occur on production runs where failure to detect them could be disastrous. I note with fear and horror that even in 1980, language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law." C.A.R Hoare on his Turing award speech in 1981.
- not2b 5y agoThe difficulty is that in the days when C was being designed, computers were much more irregular than they are today. There were one's complement machines, machines that didn't have power-of-two word sizes, no standardization of character sets, IEEE floating point hadn't been invented yet. The irregular machines weren't fringe stuff, they were the dominant architectures (IBM 360/370, DEC PDP-10, Pr1me, just a big collection of weird stuff). And compiler technology was much less advanced. So C was a messy compromise.
- dnautics 5y agoIirc, C was also designed by committee, and a lot of industry players got some of their grubby hands on the spec... (I could be wrong) but I belive the utter mess that is short/long/char sizes arises from hardware manufacturers wanting their code to be "trivially portable" across platforms with different machine words.
- not2b 5y agoNo, the different sizes were done by the original creators, not by some committee. They had to: a PDP-11 is a 16-bit machine, a PDP-10 is 36-bit, etc.
- EamonnMR 5y agoDoes anyone have a link to a good zig book/tutorial? I keep seeing it and want to try it out.
- bsedlm 5y agoas far as I can tell, none really good ones exist yet... the documentation is still scattered. The official website [1] is a good starting point. and I wish I'd seen this [2] earlier (I hope this gets incoporated into the official resources) [1] https://ziglang.org/learn/ https://ziglang.org/learn/ [2] https://www.lagerdata.com/articles/an-intro-to-zigs-integer-casting-for-c-programmers https://www.lagerdata.com/articles/an-intro-to-zigs-integer-... This is probably the most comprehensive resource: https://ziglang.org/documentation/master/ https://ziglang.org/documentation/master/
- bionade24 5y agohttps://github.com/ratfactor/ziglings https://github.com/ratfactor/ziglings
- jedisct1 5y agohttps://ziglearn.org https://ziglearn.org is a nice way to start!
- stephc_int13 5y agoThe language has not reached 1.0 yet, it would be silly to write a book at this stage. You can find documentation on the language website, also there is an active community and you can ask questions direction on Discord.
- lackbeard 5y agoStart with Ziglings: https://github.com/ratfactor/ziglings https://github.com/ratfactor/ziglings and then after working through all the exercises read chapter 2 of ziglearn.org.
- v8dev123 5y agoI am attracted to the Zig syntax, but I don't like Kelly
- bsedlm 5y agowhat or who is Kelly?
- cassepipe 5y agoCare to elaborate on why?
- v8dev123 5y agovLang He has gone greater length to tarnish this language creator. It shows me he is up to something and I don't like that. I pretty much don't use Vlang, but I dislike the way Kelley behaved.
- deleted 5y ago[deleted]
- UncleOxidant 5y agoYeah, he might have come on kind of strong against the V guy, but as I understand it there are some vapor-warey aspects to VLang (or were at the time he pointed them out).
- drcode 5y agoCurious why someone wouldn't like Kelley- Is it because he shoots down feature additions a lot?
- skohan 5y ago> There is no big company behind Zig, and the non-profit foundation stands on its own legs thanks to a balanced mix of corporate and individual donations. We don't have any big tech company on our board of directors, and frankly, we like it this way. I hope it stays this way. And maybe it can given the way the foundation has been bootstrapped, and that the project is driven largely by a single creator. Having the funding be driven largely by small donations of actual developers is a healthy incentive structure. Zig, next to Rust, is one of the more interesting language projects going on these days. Since Rust became orphaned from Mozilla, I am a bit ambivalent about the amount of investment in the project which is being driven by tech giants currently.
- oblio 5y agoIs there a popular language without investment from tech giants?
- stephc_int13 5y agoA some point, I think this is inevitable. And to be honest, I don't care that much about who is behind a tech, as long as they are making the right choices. (except if it is Apple)
- scrollaway 5y agoI mean at some point, if the tech is good, tech giants will use it. And then you'll have a flurry of people rambling in shock "Wow, Google depends so much on [tech] and they don't even give a single penny to the devs!". Programming languages getting a bunch of investment from its users is open source working possibly at its best.
- InitEnabler 5y agoNim, Pony, and maybe Dlang?
- dragonsh 5y agoPython developed despite tech giants. It's only recently after it became very popular all tech giants started investing in it. Similar are many other languages like Ruby, Haskell etc. Rust is developed within a corporate world of Mozilla, like Go language in google, so its natural it will embrace corporate world and companies like Microsoft, Amazon which will in time decide it's future directions.
- stephc_int13 5y agoI am rarely impressed by new languages, I tend to think we already have too many, a lot of them are slow and or redundant. But I like to see the emergence of a few sane proposals to replace C/C++, I've been extremely impressed so far by the work done by the team behind Zig. The best feature, from my perspective, is that Zig does not intend to be a better C++ but a better C.
- ta_ca 5y agomy only beef with zig/jai is that they both have strong opinions on things like operator overloading and i am afraid they will end up just like java. i have nothing against having strong opinions, i am the biggest offender but completely disallowing some features, some of the best features static typing can offer is just nonsense. every language designer should study common-lisp and start with the operators : and :: these two operators alone tell you that the language designed for practical use. for me overloading the operator + and function 'add' is exactly same. you should then forbid function overloading as well now. you can argue about the weakness of operator overloading but disabling completely, not even a workaround? why not just enable oo and have safe alternatives instead? . safe(a + b) . a s+ b
- stephc_int13 5y agoOperator overloading is something that has often been misused, and even if I tend to like syntactic sugar, I understand that being explicit is more important. When trying to understand a codebase, it is much easier to not worry about hidden indirections.
- ta_ca 5y agobut you can misuse anything, + vs add was an example for this. being able to misuse a PL i believe is not a bug but a feature. we really suck at predicting the future.
- stephc_int13 5y agoThis is not the role of langage designers to prevent bad programmers to write bad code. But it is better to avoid giving them too many ways to shoot themselves in the foot. Forcing the code to be explicit is good design, in my opinion, simply because it is much easier to write code than to read it, we have to put more weight on the clarity side.
- jacobmischka 5y agoThe title made me think it was going to be about creating a new language using Zig to build the compiler. I'm glad that wasn't the case.
- UncleOxidant 5y agoThat's what I thought it would be about as well - not a great title. I kind'a wanted to read about creating a new language using Zig.
- pierrebai 5y agoThere are many erroneous claims made regarding Zig allocators. Contrary to what is claimed, the fact that you pass an explicit allocator does not affect: - That an out-of-memory condition is elegantly handled or not. - That an out-of-memory condition is properly reported to the caller. - That is simplifies WebAssembly support; whatver you put in the allocator your could put into malloc. - That it easily allows arena allocators; you can only use an arena allocator if you intimately know every single uses of the passed in allocator, allo the implementation details of the libraries you call and of *all* its dependencies. If a dependency, for example, implements a cache, the arena allocator will corrupt it when deallocated.
- okareaman 5y agoAs a C programmer, I didn't like that Zig just gets rid of macros. I prefer the Rust approach to make better macros.
- CheezeIt 5y agoOne big benefit I see is that it increases design pressure on other features of the language.
- stephc_int13 5y agoI think they also have better macros, with compile time code evaluation.
- lerno 5y agoThere are many ways to approach macros. Zig’s approach with compile time evaluation handles many of them. Esp since types can be created on the fly. Another approach, which is strongly inspired by the ASTEC project for C, is what I use for my C-like “C3”: http://www.c3-lang.org/macros/ http://www.c3-lang.org/macros/ Zig’s clearly about a more homogeneous approach where I am happy to have multiple pieces of syntax to cover different usecases.
- jcelerier 5y agoIt's always possible to use the C preprocessor on non-C files, I saw people do it in Java and I heard it's somewhat common in C#; it's also used by various Unix utilities (X11 for Xrdb for instance). So technically every language can have C macros :-)
- v8dev123 5y agoI'm leaving HN and it's echo chamber all together. HN has no basis with reality. Bye!
- FnControlOption 5y agoYour account has only been registered for 30 days...
- klyrs 5y agoAnd this is the second departure announcement... https://news.ycombinator.com/item?id=26967555 https://news.ycombinator.com/item?id=26967555