Return value write exceeds 4kb in single pass

The way this works is that a range in the address space is reserved, but the pages are not commited (that is, not assigned an actual memory resource). A guard page is included at the very end of this. When you touch this page, and touching means reading or writing to an address in that page, it will trigger a segfault. The OS will handle the segfault by commiting that memory, and making the addresses immediately after it as the new guard page. If you skip this guard page and touch an address after it, there is no way for the OS to know that the stack needs to grow. The segfault will be treated like any other regular segfault. The default behavior in a segfault is to crash.

There is a function that is purposely made to touch the bytes in order, which on Windows is called _chkstk. You call it right a the beginning of your function that uses a lot of stack, and this ensures that every page that you could possibly use in that function is already commited.

But if you can guarantee that the pages will be touched in order, or have already been touched, or have already been committed for any other reason, you’re safe, so you don’t have to call this function.

This instruction is simply setting up an argument to memset. I jumped a bit to conclusions before, so I’ll dial down. Memset has to touch every byte in a range. If you start from the high address and go towards the low address, that will touch every byte in order, so it dispenses with the need for _chkstk. That would be a very clever way to optimise this. Granted, I don’t know for sure that memset starts from the high address. If it starts from the low address, you’ll crash the same way.

But there are other ways to avoid the call to _chkstk. One way is to commit your entire stack all at once. That way, the problem of touching the pages in order disappears. You can still run out of stack, but that is a different problem.

You already start with some amount of stack committed. If you know that, when a function is called, the amount of commited stack is sufficient for it, you also don’t have to call _chkstk.

Yeah the page fault mechanism. Thank you for the move 1 MB clear up. Edit I get it now, Zig uses sequential write via memset

It depends on what you mean by “care”. I care with stack checking, because it’s a waste.
But for correctness, yes, you only have care about stack size. But Zig’s default is very generous already, so I’d say you don’t have to worry about any of this.

Yeah I understood it now, but I think I should care about stack size because as evidenced, once the max limit size of the OS default setting is touched it become runtime crash segfault, using heap for big data not stack

Earlier you mentioned memset touch every byte in order, so is it 1 by 1 or using SIMD? Is it even possible if using SIMD? Because I’m wondering if size is big and 1 by 1 it would be too slow

The exact implementation varies a lot, but it’s supposed to be the fastest way to set a memory region.

Do you mean that it depends on size? I could imagine size below 8 won’t worth to be SIMD. But for size > 4kb, chunking 4kb then SIMD in each 4kb should be possible if it is not done yet while still guarantee it will touch the guard page before moving to the next page. Tho must only be done is machine that place the guard page at each 4kb

Yes, the compiler gathers information about the length to set and either does something in place, which can use scalar registers or SIMD, or calls memset, which, itself, is frequently implemented in a way that checks the length and dispatches to appropriate code (which, again, can either be scalar or vectorized).

I’m not sure I understand what you meant here. I think you are suggesting that the range to be memset could be split into chunks, and the chunks themselves could be ordered so as to avoid skipping a page guard. In that case, yes, but it would be extra complicated for no gain. Just start from the top, and you’re guaranteed to avoid any issues with page guards.