How do functions like alloca allocate memory from the stack?

14 hours ago (devblogs.microsoft.com)

One of those functions that isn't really implementable in standard C, requiring either compiler support, or being written in straight Assembly for stack registers manipulation, one of those "micro runtime" features for C.

From UNIX 7th edition all the way up to C99, when VLAs where introduced, only to be made optional in C11, and the C23 update still doesn't support automatic VLAs, only for function parameters, thus the point stands.

So what if several functions that use less than 4KB each call each other before using the stack variables in a way that the first access skips over one page?

  • A function call causes the return address to be pushed onto the stack, thus accessing the stack below the adjusted stack pointer address.

    On compiler generated x86 code, the base pointer register will quickly follow when entering the target function.

One thing that scares me a little is whether there are younger developers, say, 25-40, who can and want to pick up the mantle of Windows internals gurus.

I mean, Chen has decades of winternals in his head. Microsoft has been gutting their staff for years now. When the Petzold/Chen generation hang up their spurs, does Microsoft still have a critical mass of people who understand Windows from the metal up?

  • As someone who moved back to Windows for development and entertainment, I find systems programming on Windows more fun and engaging than on competition OSs. Oddly enough the open-source nature of the latter kind of takes away some of the thrill. Eerything is just... there, whereas with Windows there's always quite a bit of digging and investigation involved. Or maybe this is Stockholm syndrome; I dunno.

    • Funny feelings in my tummy reading this! I worked with winapi back in late 2000s to early 2010s and I remember having some great fun with it. Although there was MFC and WPF I didn't want to learn them because I wanted the fastest & leanest (catch the reference :) executable I could get; I'd then run gnu strip over the .exe too.

      Stack Overflow was essential to figure out arcane flags that could solve my issues (when even MSDN, another great site with its examples, couldn't) and Raymond was very present in SO at that time, iirc he replied to one of my questions too. That's when I found his blog, always great reads!

      Now I've been a 14-year Linux user and none of the toolkits and libraries give anything close to the winapi experience.

      1 reply →

  • Why do you think Windows is currently such a mess?

    Microsoft new blood has been educated on Macs and ChromeOS, even if they do games it is most likely consoles.

    On WinUI community calls you usually would get puzzled faces when the Q&A touched when would WinUI be able to do "insert basic Win32/Forms/WPF" feature.

    Management apparently doesn't care they actually understand Windows, or get the required trainings to meet the quality of their predecessors.

    That is how you get Webview2 all over the place.

    • >Why do you think Windows is currently such a mess?

      There have been some writings and posts here about Microsoft. Here is one from last spring, from a guy that was long time Windows core developer and moved to Azure group. It's well worth reading, what he writes about challenges they have had and most likely still have if not even worse now.

      https://news.ycombinator.com/item?id=47616242

      And from what I've understood old chaps like Dave Cutler are involved much less than they were for a very long time.

    • i fear the times when the chip makers change their architectures and the OS makers have to change the inner working of their OSes but then again, I guess there is an equivalent in the chip-making industry as well

  • > One thing that scares me a little is whether there are younger developers, say, 25-40, who can and want to pick up the mantle of Windows internals gurus.

    A similar "brain drain" has occurred in macOS (formerly known as OS-X) over the years, as evident in man page documentation for "newer" daemons shipped. An easy way to verify this is to run:

      ps -A | awk '{ print $4 }' | grep 'libexec/.*[a-z]d$'
    

    And compare the man pages for the daemons running with the man page for `launchd`.

    While this exercise is illuminating, it is also depressing IMHO.

    • > formerly known as OS-X

      It was never OS-X, it was OS X, and originally Mac OS X, as in the one after Mac OS 9. The Mac prefix was dropped with Lion (10.7). The Mac OS lingo having itself been introduced with 7.6, before that the OS core was called System.

    • And the documentation, now it is mostly generated, the famous Apple books are now gone, at least the archive is still available.

  • I was a dev on the Visual Studio and Windows teams in the 90s. I’m retired but mentor CS students at two local universities.

    I haven’t had a student in two years that was even remotely interested in ring-0, internals, or really understanding a debugger.

    I’m not being critical; they are just focused on higher level abstractions.

  • > who can

    Probably enough to keep Windows going, at least.

    > and want to

    Not if the pay or location is uncompetitive.

  • One trend to watch is AI cheat devices. Instead of running detectable software they have a fully separate device that uses AI for object detection and aimbotting. If cheaters move to using those, then the argument for kernel mode anticheat weakens. And that is the cornerstone keeping gamers on windows.

    • Now I'm looking forward to these AI cheat devices. It's going to be hilarious if the kernel anticheat malware finally gets killed by AI aimbots of all things.

    • While I’m not sure if this is a bot (where did vidya enter the convo?), game hacking on both cheat and anticheat side has genuinely deep Windows internals knowledge (admittedly somewhat lopsided, but deep nonetheless)

      2 replies →

Only passingly related, some fun rust stack-allocation insanity by my 17yo son:

https://ogghostjelly.github.io/slog/alloca.html

  • This is great. I'm learning Rust myself and your son's article contributed to my knowledge.

    I'm also very impressed by part 2. I have my own lisp but I haven't managed to implement a compiler or code generation yet. Really enjoyed reading about the hashmap too. The textbook solution to collisions is probing and comparison. It never occurred to me that I could just resize the underlying array until the collisions disappear altogether.

  • 17? You should be very proud. This is good work for anyone, but especially at his age!

Related to the above, two important concepts to know w.r.t a stack are "Red Zone" and "Guard Pages".

Raymond Chen again;

Why do we even need to define a red zone? Can’t I just use my stack for anything? - https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=10...

A closer look at the stack guard page - https://devblogs.microsoft.com/oldnewthing/20220203-00/?p=10...