<div dir="ltr">Hello ASan devs,<div><br><div>I am trying to improve ASan functionality with our D compiler, LDC. One of the main issues is that D has a garbage collector, and so I have to modify both the ASan instrumentation and the D runtime such that they work well together.</div><div>My question is about the FakeStack instrumentation to detect stack-use-after-return bugs.</div><div><br></div><div>The D garbage collector needs to scan the stack for pointers, in order to make sure that an object is no longer referenced before it can be collected. So when ASan is dynamically allocating a FakeStack, we have to inform the GC such that it scans that region for pointers too.</div><div><br></div><div>I wrote a pass that adds calls to `gc_addRange` and `gc_removeRange` for each encountered `__asan_stack_malloc` and `__asan_stack_free`, respectively. However, for small stack frames, LLVM does not emit a call to `__asan_stack_free` but instead emits directly inlined equivalent IR, and so my pass misses a lot of stack frees and doesn't work.</div><div><br></div><div>How can I get things to work?</div><div><br></div><div>I see a few possibilities:</div><div>1. Add an LLVM option to disable the `__asan_stack_free` inlining, and use my pass.</div><div>2. Add an LLVM option to emit calls to frontend-specified functions after `__asan_stack_malloc` and `__asan_stack_free`, which replaces my pass.</div><div>3. Figure out how to use ASan's `ForEachExtraStackRange` (callback for each fakestack) and use that in D's GC implementation.</div><div><br></div><div>Probably option 3 is the best (`gc_addRange` involves mutex locking), but the hardest.</div><div><br></div><div>(I also noticed that I have to call __sanitizer_start_switch_fiber and __sanitizer_finish_switch_fiber upon fiber switches, but after reading the code, I think it's only needed when FakeStack is enabled)</div><div><br></div><div>Thanks for your advice,</div><div>Cheers,</div><div>  Johan</div><div><br></div></div></div>