8 ms·
Building the Linux kernel with clang
- bitcharmer 9y agoThis is a great step forward. Compiling with clang will allow kernel developers to run code checks and analysis unavailable to gcc. This will definitely have a positive effect on code quality. Not even mentioning it should be a little bit easier to configure IDEs like CLion for working with Linux source tree.
- taeric 9y agoIs there a list of checks and analysis that you can't do from a gcc pipeline?
- kevindqc 9y agoMaybe he's talking about the sanitizers? eg: https://clang.llvm.org/docs/AddressSanitizer.html https://clang.llvm.org/docs/AddressSanitizer.html
- pksadiq 9y agoThose sanitizers are present in gcc too[0]. [0] https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.h...
- kevindqc 9y agoah! thanks
- ndesaulniers 9y agoPresent != Equal, feature wise. Not by a long shot. At the same conf, one of my friends who develops the sanitizers said it's easier to develop for LLVM, so that's where Dev work is done.
- pksadiq 9y ago> Present != Equal, feature wise. It is developed by google[0] afaik. The link says it is present in LLVM and gcc. I don't know what makes the difference (may be License?). On the contrary I have felt gcc better for my work. I have been following a clang bug[1] for long and have seen no fix so far. [0] https://github.com/google/sanitizers/wiki/AddressSanitizer https://github.com/google/sanitizers/wiki/AddressSanitizer [1] https://bugs.llvm.org/show_bug.cgi?id=23910 https://bugs.llvm.org/show_bug.cgi?id=23910
- pebers 9y agoClang has better support for plugins and calling it as a library; that sort of thing was resisted by GCC. Google has a bunch of large-scale tooling based on the Clang frontend which AFAIK wouldn't be possible with GCC.
- jcelerier 9y agosure sure, but today (and even two years ago) you can add -fsanitize={address,undefined,etc} on both clang and gcc. So this does not answer the question.
- deleted 9y ago[deleted]
- sanxiyn 9y agoAll sanitizers are also available in GCC, so no.
- bitcharmer 9y agoAre you sure? Last time I checked the ones llvm had and gcc did not were: Dynamic alloca overflow detection Symbol size changing for global variables Adaptive global redzone sizes Invalid pointer pairs detection Support for no_sanitize attribute Support for dead stripping of globals on Linux I might be out of date on some or even all, so please correct me where I'm wrong. Thank you in advance.
- ndesaulniers 9y agoWrong. Not all kernel versions.
- albinofrenchy 9y agoSanitizers are run-time tools; I'd be surprised if any of them worked out of the box with the kernel. Address sanitizer was "ported" to the kernel a while ago; not sure if UBSAN is or will be in the future.
- ndesaulniers 9y agoThere are kernel versions. KASAN, KUBSAN, KMSAN.
- melkiaur 9y agothe static analyzer, along with all the other checks available from clang-tidy: https://clang.llvm.org/extra/clang-tidy/checks/list.html https://clang.llvm.org/extra/clang-tidy/checks/list.html
- taeric 9y agoI'm surprised there is a static analyzer available for clang that isn't somewhat equivalently offered in gcc toolchains. I guess I'm assuming folks would be using something like Coverity for major static analysis. Specifically, this sounds like it is just preferring the other toolchain. Are there literally things that don't have equivalents?
- benmmurphy 9y agohow do you even have VLAs where the array is the non-terminal field? do you embed the size of the array in the type definition or in the structure?
- unwind 9y agoIt's weird. As far as I can tell, GCC's documentation[1] clearly states: Flexible array members may only appear as the last member of a struct that is otherwise non-empty. What is the article referring to, then? 1: https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Zero-Length.html#Zero-Length https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Zero-Length.htm...
- jwilk 9y agoflexible array member ≠ variable-length array See: https://news.ycombinator.com/item?id=15303211 https://news.ycombinator.com/item?id=15303211
- marcoperaza 9y agoIt has to be in the definition since it is a VLA, not a flexible array member. Accessing the VLAs or members after/between them will generate an offset based on whatever values were used to define the length of the VLAs, so the definition needs to be in scope. Seems like the use case is for struct types defined within a function and used only within that same function execution, or perhaps also accessed through another execution of that same function that results in the exact same definition. In the latter case, you would need to cast the memory to void* for storage between executions, and then back to the regenerated type, since you can only reference types within the scope that they are defined. Weird, but I can see the potential utility when you're optimizing for locality in heap allocations, since the allocator will give you a single contiguous memory block with all of the VLAs, while making several allocations for multiple VLAs could very well return disjoint memory ranges. And then there's the aesthetic utility of behaving like objects in higher-level languages, where having multiple variable-length arrays within an object is standard. See https://stackoverflow.com/questions/14629504/variable-length-array-in-the-middle-of-struct-why-this-c-code-is-valid-for-gcc https://stackoverflow.com/questions/14629504/variable-length... and linked pages.
- MichaelMoser123 9y agoI found that clang compiled code was requiring more memory on the stack than the same code compiling with gcc. Was it some obscure switch that did that? no idea. However with the Linux kernel you have some tight limitations with regard to stack size. Could the author run his clang compiled kernel? The article does not tell.
- wiz21c 9y agoClang builds the kernel, EFF leaves W3C... What's next ? Amazon rewrites libc ?
- ndesaulniers 9y ago1000 years of darkness!!!
- pjmlp 9y agoEvery C compiler has a libc.
- wiz21c 9y agoI didn't know... https://libcxx.llvm.org/ https://libcxx.llvm.org/ But according to what I see on that page, my interrogation remains : once the kernel is built without gcc, then GNU's power will fade away. GPL and HTML have, in their own ways, enforced that some important code remains open (and sharable and modifiable). By weakening them, the openness will be weakened too. Of course it won't be 1000 years of darkness, but well, it makes me a bit sad :-)
- pjmlp 9y agoWhy do you think Apple, Google, Sony, Nintendo and many other embedded OEMs have moved to clang? It wasn't only due to the better plugin support.
- swills 9y agoGiven the number of operating system kernels in the world, it might be helpful to change the title to say "the Linux kernel" instead of "the kernel". I understand TFA is on lwn.net, which has "Linux" in it's title, so it's clear there, but here it becomes a little vague.
- Koshkin 9y agoPerhaps, that was in reference to https://www.kernel.org/ https://www.kernel.org/.
- howard941 9y agoThis. FreeBSD's been building on CLANG for amd64 and i386 since before 10.0 (now on 11). See, for ex., https://wiki.freebsd.org/BuildingFreeBSDWithClang https://wiki.freebsd.org/BuildingFreeBSDWithClang https://unix.stackexchange.com/questions/49906/why-is-freebsd-deprecating-gcc-in-favor-of-clang-llvm https://unix.stackexchange.com/questions/49906/why-is-freebs...
- notaplumber 9y agoOpenBSD is also now building the kernel/userland and most of the ports tree with Clang for i386/amd64 and arm64. 6.2 will ship with it as the default system compiler. > Kroah-Hartman said that he wished there was a competitor to the Linux kernel. Linux people refuse to look outside their vacuum chamber.
- ndesaulniers 9y agoThat was not what he said. Article is wrong. He started he wants more competition amongst compiler vendors for the benefit of the kernel.
- kasabali 9y ago> Sorry, but you might have missed my follow-on comment which was, "I wish we had a viable competitor to the Linux kernel as well". Jake correctly quoted me here. https://lwn.net/Articles/734405/ https://lwn.net/Articles/734405/
- Koshkin 9y agoI wonder what Torvalds' opinion on Clang is. He had harsh words to say about a recent version of gcc, as I remember.
- paulddraper 9y agoIIRC, his complaints are of the nasal demon variety. He hates compiler writers being tricky.
- d33 9y agoContext: In the C community, undefined behavior may be humorously referred to as "nasal demons", after a comp.std.c post that explained undefined behavior as allowing the compiler to do anything it chooses, even "to make demons fly out of your nose". https://en.wikipedia.org/wiki/Undefined_behavior https://en.wikipedia.org/wiki/Undefined_behavior
- Sir_Cmpwn 9y agoI know this isn't the opinion of many kernel developers, but I think a lot of the problems encountered are better solved by removing proprietary GNU C extensions from the kernel code. I would love to start sending patches but I expect they'd just be NACKed.
- taeric 9y agoSounds like this is a prime example where evidence would help. Just thinking "a lot of the problems..." doesn't do much to show why that is the case. Especially without even knowing which extensions you are specifically referring to. That is, my guess is these extensions are the equivalent of Chesterton's fence. They were introduced for a reason. If you have evidence that that reason is no longer apropros, present the evidence. But don't just complain about the fence.
- Sir_Cmpwn 9y agoI mean, the problems faced by compiling the kernel with Clang (or any other standards-implementing C compiler). The problems are pretty easy to find, just look at the relevant commits in Clang and Linux. The main problem Clang faced with the kernel was lots of use of GNU C extensions everywhere. I think it's self-evident that the use of proprietary, nonstandard language extensions is a bad idea. It leads to multi-year efforts getting other compilers to support "features" that aren't in the spec (and as a result, are typically underspeced by the compilers that implement them and require reverse engineering to fully understand and implement correctly). Linux isn't written in C, it's written in some other language that's deceptively similar to C. It should be enough to write a compiler that accurately implements the C standard to compile the kernel with.
- taeric 9y agoBut, I could just as easily say it is self-evident that the use of proprietary, nonstandard languages extensions was required to get the linux kernel written. Don't get me wrong, I'm highly amenable to this argument, but it is not an evidence based one. Now, there could be some easy evidence that would help. For example, evidence that kernels which are built using clang build just as easily using gcc. Or icc. Or ___. That would help. Right off, I do not know if that is the case.
- ezoe 9y agoThe Linux kernel use the variable length array in the structure which is a GCC extension and not part of the C standard. Clang don't want to support that GCC extension because it's too ugly.