TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Cake – C23 and Beyond (2023)

172 pointsby hudonabout 1 year ago

13 comments

fl0kiabout 1 year ago
Nice. If this can be reasonably retrofitted to existing libraries and projects so that the safety properties compose from local to global, then this could actually be a meaningful improvement to the safety of real-world C code.<p>There would be many more steps required &quot;toward&quot; memory safety, such as eliminating all forms of UB including uninitialized memory, out of bounds pointers, data races, etc. but if this direction is to be pursued it has to start somewhere.
评论 #39437421 未加载
评论 #39437484 未加载
woodruffwabout 1 year ago
I might be missing something, but this seems to require ownership annotations on all functions, e.g. a compatible and correct prototype for `fclose` to correctly note that the owned `FILE *` is moved into the call.<p>If that&#x27;s correct, then this is somewhat practically limited: either pre-existing codebases will need to be retrofitted with an essentially bespoke set of macros, or the compiler will need to be &quot;fail open&quot; by default. The tradeoffs between these two are hard (substantial developer pain versus being ineffective against the bulk of a compiled program&#x27;s API surface).<p>(Also, this design appears to be for temporal safety only, not spatial safety. But again I might have missed something.)
评论 #39440165 未加载
评论 #39437855 未加载
ahgamutabout 1 year ago
&gt; new methods of communication with the compiler have been established.<p>From what I understand, this appears to a be separate binary from GCC&#x2F;Clang that does static analysis and outputs C99.<p>Can this be a GCC plugin? I know we can write plugins that are activated when a specific macro is provided, and the GCC plugin event list allows intercepting the AST at every function declaration&#x2F;definition. Unless you&#x27;re rewriting the AST substantially, I feel this could be a compiler plugin. I&#x27;d like to know a bit more about what kinds of AST transformations&#x2F;checks are run as part of Cake.
评论 #39437359 未加载
irogersabout 1 year ago
Agreed this is awesome, obviously sanitizers fill some of this gap currently but they aren&#x27;t great with things like reference counting that RAII makes a doddle. Fwiw, here is an implementation of a runtime RAII style checking on top of leak sanitizer: <a href="https:&#x2F;&#x2F;perf.wiki.kernel.org&#x2F;index.php&#x2F;Reference_Count_Checking" rel="nofollow">https:&#x2F;&#x2F;perf.wiki.kernel.org&#x2F;index.php&#x2F;Reference_Count_Check...</a> There&#x27;s an interesting overlap with the cleanup attribute that is now appearing in the Linux kernel (by way of systemd): <a href="https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;linux&#x2F;kernel&#x2F;git&#x2F;torvalds&#x2F;linux.git&#x2F;tree&#x2F;include&#x2F;linux&#x2F;cleanup.h" rel="nofollow">https:&#x2F;&#x2F;git.kernel.org&#x2F;pub&#x2F;scm&#x2F;linux&#x2F;kernel&#x2F;git&#x2F;torvalds&#x2F;lin...</a>
评论 #39439847 未加载
Rucadiabout 1 year ago
This project is amazing because it also seems that has #embed included, IIRC no other compiler has it yet.<p>Just for that #embed directive I would already use cake for the moment (although it seems like it is only doing the file-&gt;array conversion)
评论 #39440167 未加载
评论 #39439313 未加载
dataflowabout 1 year ago
This is awesome. Could they reconcile this with [[gsl::Owner]] or gsl::owner&lt;T&gt; somehow so we don&#x27;t end up with multiple syntaxes in C++?<p><a href="https:&#x2F;&#x2F;reviews.llvm.org&#x2F;D64448" rel="nofollow">https:&#x2F;&#x2F;reviews.llvm.org&#x2F;D64448</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;blob&#x2F;main&#x2F;docs&#x2F;headers.md#gslowner">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;GSL&#x2F;blob&#x2F;main&#x2F;docs&#x2F;headers.md#g...</a>
评论 #39437994 未加载
Animatsabout 1 year ago
C safety addons like this (there have been many) is that they don&#x27;t prevent extracting raw pointers from controlled pointers. Optional memory safety isn&#x27;t.<p>&gt; If this can be reasonably retrofitted to existing libraries and projects<p>That&#x27;s the problem.<p>If you want to fool around in this space, consider revisiting C++ to Rust conversion. There&#x27;s something called Corrode, which compiles C to a weird subset of Rust full of objects that implement C raw pointers. The output is verbose and unmaintainable. What&#x27;s needed is something that can figure out how big things are and who owns what, possibly guessing, and generate appropriate ideomatic Rust. Now that LLMs are sort of working, that might be possible.<p>Can you ask Github Co-pilot to look at C code and answer the question &quot;What is the length of the array &#x27;buf&#x27; passed to this function&quot;? That tells you how to express the array in a language where arrays have enforced lengths, whicn includes both C++ and Rust. With hints like that, ideomatic translation becomes possible. Bad guesses will result in programs that subscript out of range, which is caught at run time. But guesses should be correct most of the time, because C programmers tend to use the same idioms for arrays with lengths. Forms such as &quot;int read(int fd, char* buf, size_t buf_l)&quot; show up often.<p>Using LLMs to help with tightening up existing code might work.
评论 #39438150 未加载
评论 #39438107 未加载
评论 #39437876 未加载
评论 #39438434 未加载
评论 #39438447 未加载
JonChesterfieldabout 1 year ago
I think this is a really interesting direction.<p>That it can translate C23 to C89 means it has most of the work in place to translate C23 to C23, or C99 to C99 etc. If that is done in a (mostly) reversible fashion - successfully re-encode back to the original, where you `preprocess -&gt; parse -&gt; unparse -&gt; re-preprocess` which is a nuisance but possible, then it opens the door to much more aggressive type systems.<p>In particular, the input can be C with the ownership annotations, and if they&#x27;re valid, the output can be C with those annotations dropped to be fed into some other compiler. Or whatever other invariant systems the compiler dev is interested in.<p>Or the input could be C extended with namespace {} syntax, C++ style lambdas, contract checking - whatever you wish really, and the output can be the extensions desugared into C. Templates (possibly the D style ones) can be implemented as instantiating normal functions from said template.<p>That the output is C means this is usable in all the pipelines that already work with C.<p>Good stuff, thanks for posting.
评论 #39440268 未加载
yau8edq12iabout 1 year ago
I&#x27;ve been dabbling in embedded programming. Everything is written in C. I just don&#x27;t understand why. C++ solves pretty much all problems if you want it too (RAII, smart pointers, move semantics) and the frameworks writers wouldn&#x27;t need to implement their bespoke OOP system on top of opaque pointers and callbacks.<p>Maybe it was bad luck on my part, and other embedded frameworks are better; but I got into both ESP32 and STM32, both frameworks are the worst spaghetti code I have ever seen. You need to jump through at least one, often two layers of indirection to understand what a particular function call will do. Here&#x27;s an example of what I mean:<p><pre><code> &#x2F;&#x2F; peripheral_conf.h #define USE_FOOBAR_PERIPHERAL 1 &#x2F;&#x2F; obj_t.h #define USE_OBJ_PARAM2 &#x2F;&#x2F; In the library header #ifdef USE_FOOBAR_PERIPHERAL #define DoSomethingCallback FoobarCallback #endif &#x2F;&#x2F; foobar.h status_t FoobarCallback(int32_t data, int32_t param); &#x2F;&#x2F; obj_t.c status_t Init(Obj_t* obj) { obj-&gt;param1 = obj-&gt;init.initparam &amp; 0xFF; #ifdef USE_OBJ_PARAM2 obj-&gt;param2 = (obj-&gt;init.initparam &gt;&gt; 16) &amp; 0xFF; #endif obj-&gt;callback = DoSomethingCallback; return OK; } status_t DoSomething(Obj_t *obj, int32_t data) { #ifdef USE_OBJ_PARAM2 return obj-&gt;callback(data, obj-&gt;param2); #else return obj-&gt;callback(data, obj-&gt;param1); #endif } &#x2F;&#x2F; main.c Obj obj = {0}; obj.init.initparam = 0x12345678; Init(obj); DoSomething(obj, 0x42); </code></pre> And that&#x27;s an <i>easy</i> example. Macros everywhere, you need to grok what&#x27;s happening in four different files to understand what the hell a single function call will <i>actually</i> do. Sure, the code is super efficient, because once it&#x27;s compiled all the extraneous information is pre-processed away if you don&#x27;t use such and such peripheral or configuration option. But all this could be replaced by an abstract class, perhaps some templates... And if you disable stuff you may not need (RTTI, exceptions) then you&#x27;d get just as efficient compiled code. It would be much easier to understand what going on, and <i>you wouldn&#x27;t be able call DoSomething on uninitialized data</i>... Because you&#x27;d have to call the constructor first to even have access to the method.<p>Anyway, thank god for debuggers, step-by-step execution, and IDEs.
评论 #39438802 未加载
评论 #39440289 未加载
taminkaabout 1 year ago
lowkey smart pointers are often just used to deflect the responsibility of thinking about memory layouts<p><a href="https:&#x2F;&#x2F;floooh.github.io&#x2F;2018&#x2F;06&#x2F;17&#x2F;handles-vs-pointers.html" rel="nofollow">https:&#x2F;&#x2F;floooh.github.io&#x2F;2018&#x2F;06&#x2F;17&#x2F;handles-vs-pointers.html</a><p>and most issues can be caught by using a static analyser of a memory leak checker (getting ppl to consistently use them is another issue, but still)
评论 #39437603 未加载
eatonphilabout 1 year ago
Do I need to use the Cake frontend to use the ownership library or is it actually macros (or an extension?) I could use in code compiled with gcc or clang?
评论 #39437412 未加载
pizlonatorabout 1 year ago
I think that ownership for C is gross. It&#x27;s hard to convert code to something like this.<p>But you could get most of the benefit by just isoheaping (strictly allocate different types in different heaps).
评论 #39437428 未加载
评论 #39437407 未加载
评论 #39437457 未加载
评论 #39437510 未加载
评论 #39440138 未加载
ActorNightlyabout 1 year ago
This is overly complicated, there is no need to bring Rust semantics to C to ensure memory safety.<p>A good mempool implementation is all you need (i.e keeps track of every request, and zeros out the memory on release)
评论 #39438086 未加载
评论 #39438095 未加载