10 ms·
C library system-call wrappers, or the lack thereof
- en4bz 8y agoLack of `gettid` and `futex` have always annoyed me.
- monocasa 8y agoThere's this sense that they're not for the consumption of mere mortals. That being said, you can still at least always call syscall(2).
- glandium 8y agoOne problem is that not all system calls take the same kinds of arguments on all platforms. Example: SYS_mmap takes a pointer to a struct containing all the arguments on s390. Even better, on Alpha, glibc's syscall cannot call system calls with 6 or more arguments (although maybe that was fixed in the past 8 years?).
- murderfs 8y ago> Even better, on Alpha, glibc's syscall cannot call system calls with 6 or more arguments That's just generic kernel ABI: syscalls can have at most 6 arguments: https://elixir.bootlin.com/linux/latest/source/include/asm-generic/syscall.h#L104 https://elixir.bootlin.com/linux/latest/source/include/asm-g...
- glandium 8y agoSkip the "or more" part, then, but that doesn't make it less true: it wasn't possible to make a 6-argument system call with syscall on alpha 8 years ago. I don't know whether that's been fixed or not.
- pm215 8y agoThe syscall(5) manpage documents the Alpha syscall abi as passing arguments in a0,a1,a2,a3,a4,a5 which would suggest so. (MIPS o32 is the only listed one which is a bit oddball: you can only pass 4 args in registers and then use stack for 5 and 6.)
- jws 8y agoI’m sure it would give people discomfort, but I wonder how it would work if the kernel presented a pseudo file system with an “include” and a “src” directory to provide C interfaces to the unassimilated syscalls. Just enough syntax to keep people from defining their own types and have to use the syscall() interface. Maybe make it a module so space constrained systems can leave it out. The kernel patch process could keep everything nicely in sync and native build processes would easily find the right source code. Cross compiling would require you to find and copies though.
- tacostakohashi 8y agoOne issue with that idea is that the source code for implementing a system call is necessarily compiler-specific, it is not standard C. So, the issue would become which compiler (version) should the source code of the include/ and src/ be for? GCC? Clang?
- olliej 8y agoA lot of the problems occur in things like struct layout of syscall arguments, which would be cross platform. But then you run into the problem of how willing to commit to ABI stability in a non-posix API. I assume there’s some commitment to such at the moment, but how much also depends on *libc abstracting them? Libcs always seem to (to me) be approximately kernel version specific
- Aic1kuir 8y agoAncient glibc versions run ok on current kernels, you're just missing out on some newer features. Otherwise statically linked binaries would cease to work.
- zaarn 8y agoThere is still a limit, very old software doesn't run anymore, largely because something in the kernel broke it. But it has been years and nobody complained so nobody will revert this or fix it. Linus has stated as such and I think a few other maintainers agree there. If you don't find the problem until years later, chances are, too few people care.
- joshumax 8y agoI ran into this problem a little while back trying to get my Linux implementation of the BSD unveil() system call merged into mainline. Some of the responses to the RFC told me that it shouldn't be added because glibc likely won't add a syscall wrapper for it. However, a response from glibc states that they won't consider adding it until it's been successfully merged into mainline, creating a sort of catch-22 situation.
- kjeetgill 8y agoThank you for your efforts! Can you just cc them both on an email and get them to agree together?
- joshumax 8y agoHopefully! I plan on doing that for my next RFC!
- emilfihlman 8y agoThis is an idiotic stance from maintainers. Linux already has a lot of things that most currently used libc implementations don't support, like getrandom.
- userbinator 8y agoI'm curious what led to Linux (and it seems the other Unices) adopting the "double indirection" strategy of having a separate wrapper/stub function for system calls vs. the approach common in the MS-DOS world where the compiler would directly embed e.g. INT 21H instructions and generate the code to put parameters into registers itself. It's a small inefficiency, but still seems a bit wasteful nonetheless.
- tacostakohashi 8y agoThere's nothing stopping the wrapper function from being inlined (as it commonly is for some compiler + libc combinations), with all the usual tradeoffs for code size and ease of debugging.
- JdeBP 8y agoInlining isn't the answer here. userbinator and you are missing out on more than a decade of IBM/Microsoft operating system evolution, with the invention of high-level-language-callable kernel APIs in the likes of DOSCALLS.DLL and NTDLL.DLL in the 1980s.
- megous 8y agoIt doesn't happen, unless you compile C library with LTO. I'm yet to succeed in doing that for my musl C mipsel target.
- skissane 8y agoWhen you write C code on MS-DOS, you usually don't directly call INT 21h. Instead you call fopen(),printf(), etc, which are library calls which call INT 21h in their implementation. As well as standard C functions, there are also C functions for MS-DOS specific services, e.g. chdrive(), findfirst(), etc. Even when you want to call some INT 21h service which doesn't have a C function, it is more common to use interrupt functions like int86() or intdos() than to use inline assembly. (Or something like __dpmi_int from 32-bit code, such as DJGPP) So, I don't think MS-DOS is as different from Linux/Unix as you think. High-level language code on MS-DOS (whether in C or Pascal or BASIC or whatever) usually doesn't directly invoke INT 21h, it goes through higher-level libraries / wrapper functions. Only software written in assembly tends to invoke INT 21h directly.
- int_19h 8y agoThis is one area where I feel that BSD approach (where the same team maintains both the kernel and the libc, and they're shipped in sync as part of the same release) makes a lot more sense. In fact, come to think of it, Linux is the only OS where syscalls are the official public userspace API, is it not? On all other platforms, they're an implementation detail behind the system libraries.
- sanxiyn 8y agosyscall-as-API does have some advantages in that you can avoid libc if you want to. One example is Go's Linux target; on Linux, Go standard library calls syscalls directly without any help of libc.
- kayamon 8y agoYou can achieve that on other OSes too though, still without requiring libc. For instance, on Windows the syscall API is hidden and changes from release to release, but KERNEL32.DLL persists as the stable system API that you're supposed to call. Linux desperately needs to get past the notion that libc is the only gateway to the kernel, and instead start supplying a standardized "system/kernel" user-level wrapper library. One which wraps the syscalls nicely but doesn't try to add additional language-specific functionality like printf or memcpy.
- int_19h 8y agoOn Windows, kernel32.dll is basically the Windows equivalent of libc - it's still a userspace library that wraps syscalls, and you're not supposed to use them directly. But I don't see why it's a problem, either. What's the actual benefit of Go invoking syscalls directly on Linux? That it doesn't depend on glibc? But that is only an advantage because glibc is not guaranteed to be there on Linux, the way e.g. kernel32 is on Windows. If it were, there'd be no reason to not use it.
- IshKebab 8y ago> That it doesn't depend on glibc? But that is only an advantage because glibc is not guaranteed to be there on Linux, the way e.g. kernel32 is on Windows. If it were, there'd be no reason to not use it. Yes - but it's an advantage because glibc is a compatibility nightmare. One of Go's really nice features is that you can compile a binary and it will run pretty much anywhere. That would be pretty much impossible if it linked with glibc (even if you ignore the fact that glibc might not be present).
- pmoriarty 8y agoHow did Plan 9 and Inferno handle this?
- xenadu02 8y agoThe comments on the article have some funny ideas about versioning. Apple platforms manage to support the idea of a “deployment target” and binary compatibility; the new symbols are weak-linked. Broken old behavior is preserved with linked on-or-after checks. Not sure what makes it so difficult for glibc.
- matthewbauer 8y agoYou need quite a bit of coordination between your toolchain, your kernel, and the standard c library for that to work. Linux/GCC/Glibc has never had that.
- jancsika 8y agoTwo noob questions: What are the technical reasons that glibc cannot adhere to the Linux dogma, "Don't break userspace?" Since glibc does not adhere to that dogma, why the decades-long reluctance to add certain syscall wrappers? If they screw up and make a bad interface just modify it and bump the version number. I just waded through the lwn cross-purpose-writing-festival comments and did not see them answered.
- Annatar 8y agoThere are no technical reasons. The issue is that the kernel and glibc are not one coherent whole because they were always developed by two unrelated groups. glibc wasn't even designed for Linux, but for the GNU kernel. The end result is that the users of GNU/Linux will always draw the short end of the stick.
- jancsika 8y ago> The issue is that the kernel and glibc are not one coherent whole because they were always developed by two unrelated groups. But AFAICT the glibc dogma is based on the premise that it would be impossible for a large, complex project to have backward compatibility without making regular changes to the extant interfaces that it provides. Given that premise glibc devs seem to have some process for figuring out what "correctness" means for time=now and then noodle around with their interface to reflect that correctness in the next version of the lib. Thus symbol versioning is employed. At the same time, Linux is a large, complex project with backward compatibility which does significantly less noodling around with the extant interface. AFAICT the process consists mainly of a) devs breaking the extant interface for correctness, b) a user submitting a bug, and c) the lead dev surrounding the declarative sentence "We don't break userspace" with imperative sentences containing curses and then rejecting the change. I've read where Linus and others have tried to defend their choice and argued that the glibc dev process is worse. Regardless of the persuasiveness of that argument, I've read it and am familiar with it. I am not familiar with the glibc argument as to why they require regular interface changes, nor an acknowledgement that a closely connected large complex project gets by without that. I don't see anything on the glibc FAQ about it-- only a question about symbol versioning where the answer assumes that the interface must change.
- Annatar 8y agoThis is exactly why BSD and illumos based operating systems ship libc, the kernel and userland (/usr) as one coherent whole. Perhaps now, reading the LWN article, people who are comfortable with GNU/Linux will start to realize it's high time to outgrow it and move on to one of the BSD or illumos based systems. The longer you wait, the harder the transition will be and besides, it's good to go out of one's comfort zone.
- majewsky 8y agoSure, I'll just write up a proposal for my employer to move multiple tens of thousands of Linux servers, VMs and containers over to BSD. I have a good feeling about this. /s
- Annatar 8y agoHere is something to consider: how did your employer get to tens of thousands of Linux servers from whatever they were running on before? And: do you really want to spend the rest of your professional career wrangling with a shoddy product, or do you want to actally do professional, cutting edge IT? I can't write for you, but I did not graduate computer science at the top of my class so that I could spend the next several decades working with / on the shittiest, amateur knock-off copy of UNIX when I could run the real thing for free & cheap. That's not why I studied at a university and got a degree for. How about you, what's it gonna be, shitty Linux for the next 20-30 years or the real computer science with SmartOS or FreeBSD?
- rkeene2 8y agoThis is the whole point of a Linux distribution -- it takes the disparate parts and makes them into one coherent whole.
- Annatar 8y agoBut it's not coherent, far from it: it is often broken by various parties involved because there is no architecture and there are heterogenous interests at play. Compared to a FreeBSD or a SmartOS system, architecturally, it barely works, and it's rickety and shoddy.
- molticrystal 8y agoArbitrage issues wherever glibc and the syscall differ seem to be where the bugs and security issues lay or at least a good place to look. Whenever a person has to roll out their own handler it will almost always undergo less testing and auditing. The article points gettid which for at least 10 years required your own method to use, and the comment section for the article points out that the getpid call had caching that was bugged for a long time. Having no glibc implementation of a syscall affects its usage and the total number of people knowledgeable about that function, so it would be a perfect place to look for bugs and security. In the nearly reverse case a poor implementation of a glibc handler might be doing something that would allow an attacker to take advantage. The same applies where the glibc and syscall functionality differ, an aspect only part of the syscall might be undertested.
- bogomipz 8y agoThe post states: >"In such cases, user-space developers must fall back on syscall() to access that functionality, an approach that is both non-portable and error-prone." I understand about portability but can someone elaborate on why using syscall() is inherently error-prone?