[video output=day345 member=cmuratori stream_platform=twitch stream_username=handmade_hero project=code title="Protecting Memory Pages for Underflow Detection" vod_platform=youtube id=imxqyPgUIcM annotator=Miblo]
[0:01][Recap and set the stage for the day]
[1:16][Blackboard: Buffer Overruns]
[2:47][Blackboard: VirtualAlloc() and virtual memory mapping]
[6:23][Blackboard: Playing with how this memory works]
[8:23][Blackboard: Guard Page with VirtualProtect()]
[9:38][Blackboard: Faulting at the smallest error]
[13:29][handmade_platform.h: Add PlatformMemory_OverflowCheck and PlatformMemory_UnderflowCheck flags]
[14:51][handmade_memory.h: Enable PushSize_() to perform bounds checking by passing every allocation down to the system every time]
[17:26][Run the game and see nothing in particular happen]
[17:37][handmade_memory.h: Make PushSize_() set the OverflowCheck flag on everything, run the game and grind to a halt]
[19:24]["You're an operating system. Have some self-respect"][quote 530]
[19:51]["It's 2016, and all you have to do to kill Windows is just allocate some memory"][quote 531]
[20:35][Return with the determination to provide a way to free our memory]
[21:48][handmade_memory.h: Change PushSize_() to ensure arenas only keep the exact size they need]
[23:44][Run the game to give that a shot]
[24:32][handmade_memory.h: Rename memory_block_footer to memory_block_chain]
[27:02][Blackboard: Positioning that memprotect in order to catch underflows]
[27:26][win32_handmade.cpp: Make PLATFORM_ALLOCATE_MEMORY() allocate one blank 4K page at the beginning]
[30:13][handmade_memory.h: Consider unifying the arena and allocator's data structures]
[31:40][win32_handmade.cpp: Make PLATFORM_ALLOCATE_MEMORY() allocate]
[32:34][Run the game to see it run exactly the same]
[32:45][win32_handmade.cpp: Make PLATFORM_ALLOCATE_MEMORY() call VirtualProtect()[ref
    site=MSDN
    page=VirtualProtect
    url=https://msdn.microsoft.com/en-us/library/windows/desktop/aa366898][ref
    site=MSDN
    page="Memory Protection Constants"
    url=https://msdn.microsoft.com/en-us/library/windows/desktop/aa366786]]
[37:03][Blackboard: What actually happens with the guard page]
[39:00][win32_handmade.cpp: Pass PAGE_NOACCESS to that VirtualProtect() call]
[39:38][win32_handmade.cpp: Verify that the guard pages are happening properly]
[40:11][Run the game with the expectation to crash and investigate why we don't]
[40:38][handmade_memory.h: Make PushSize_() set the UnderflowCheck flag, run the game and successfully crash]
[41:28][win32_handmade.cpp: Enable PLATFORM_ALLOCATE_MEMORY() to perform underflow checking]
[47:02][win32_handmade.cpp: Enable PLATFORM_DEALLOCATE_MEMORY() to handle the checking]
[51:06][handmade_world.cpp: Make GetWorldChunk() write off the beginning of the buffer, run the game and see nothing being detected]
[51:57][handmade_memory.cpp: Toggle on the underflow checking in PushSize_(), run the game and immediately catch that offending write]
[52:49][handmade_world.cpp: Make GetWorldChunk() write off the end of the buffer, run the game and see nothing being detected]
[53:34][handmade_memory.cpp: Determine to enable PushSize_() to perform overflow checking]
[55:16][todo.txt: Update the TODO list]
[55:39][Q&A][:speech]
[55:59][@insobot][!qa]
[56:46][@insofaras][Yes]
[58:16][@k2t0f12d][I'm following an Linux with SDL because I'm not sure if it's worth building on X11 or if Wayland is ready yet. Have you thought about building on X11, Wayland or both?]
[59:28][@drjeats][Before when you had fixed allocation, you could do a between-runs save state kind of thing (if I recall correctly). How would you make that happen with the dynamic blocks?]
[59:48][@flirtychair][Do you have any insight into if you are developing this game in a similar timeflow to major devs, e.g. you spent some time today working on under / overflow before such a thing came into play. Is this just because of the format of the show?]
[1:01:06][@ab1093996300][Why do you look so different from Handmade Hero Day 001?]
[1:01:45][@drjeats][Does it really work between runs of the program? I saw it work with looped editing, but not different runs. Wouldn't you need to muck with base pointers?]
[1:03:46][@zamar037][I know this isn't relevant to the code, but does it seem strange streaming in a different location?]
[1:04:05][@enemymouse][Seems that Arena started out as a fast, stack-style allocator and is now moving towards a dynamic heap. You've demonstrated the benefits of VirtualAlloc etc. but can you comment on the performance hit and why you choose it over malloc?]
[1:05:22][@williambundy][Don't recognize the tune]
[1:05:45][@windowl][Are any days (streams) missing from the YouTube archive?]
[1:07:05][@drjeats][Was memprotect the primary motivation for moving to dynamic memory, or were there other reasons?]
[1:09:22][Wrap it up][:speech]
[/video]
