TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

My Favorite Debug Ever

71 pointsby strzalekover 9 years ago

7 comments

mrichover 9 years ago
I prefer using AddressSanitizer for finding bugs like these now. In addition to heap overflows it can also find stack and global overflows.<p>As a second option I&#x27;m using valgrind which can find uninitialized memory (and doesn&#x27;t require recompile of all libs as MemorySanitizer does).
timclassicover 9 years ago
ElectricFence is great, and I recall having a similar feeling to the OP when I first used it because many of the described issues were also new to me at the time. It&#x27;s a great experience :)<p>If you&#x27;re on Solaris or a Solaris-derived OS, you also have the libumem and watchmalloc libraries that can help you out. I have used libumem to great effect in the past.<p>It&#x27;s been a while, but I anecdotally recall that Solaris is more stingy with its mallocs than Linux. I used to compile and run my C projects on Solaris as a first pass in my search for memory errors.
teddyhover 9 years ago
Is using GDB and Electric Fence to find a normal buffer overflow bug really interesting enough to warrant a blog post these days, let alone a HN story?<p>I guess it might be interesting for all the people who, like the author, are Java and PHP programmers and have no experience with C?
评论 #10843155 未加载
评论 #10843126 未加载
评论 #10843085 未加载
评论 #10844038 未加载
评论 #10843518 未加载
评论 #10844107 未加载
评论 #10843431 未加载
vardumpover 9 years ago
Usually the toughest things to debug is when stack is overwritten. Worse, if it&#x27;s stack and heap. Not my favorite things to debug...<p>Here&#x27;s what I usually do in such cases:<p>Call stack is unreliable at that point. You&#x27;ll see puzzling things like function calls with impossible parameters, etc. If things just don&#x27;t make any sense, it&#x27;s better to map all code paths that can lead to the crashing EIP&#x2F;RIP (hopefully valid pointer to the instruction that caused the crash). Check EIP&#x2F;RIP if it&#x27;s in some rep movsd (= potentially inlined memcpy, check ECX (RCX) rep counter, EDI (RDI) rep pointer), or if the execution is in some runtime library code such as memset, memcpy, etc. similar. The next thing is to make sense of the call stack manually, if there are portions not overwritten, but what stack walk couldn&#x27;t resolve. Of course it pays to take a look around stack otherwise as well, for signs of overwrite and contents of the overwrite.<p>It&#x27;s also possible a pointer to stack object leaked at some point and the crash occurs at completely different part of code than where it actually segfaulted. Or some runtime structure was corrupted, like heap. Sometimes you can find those by just inspecting and guessing struct&#x2F;object shape and values near stack pointer ESP (RSP).<p>If the bug can be reproduced, memory breakpoints, logging (especially if multithreaded, but watch out for blocking I&#x2F;O from logging), tools for debugging memory corruption (valgrind, compiler paranoid mode, etc.) etc, even mapping some pages unreadable and unwritable. It can take a while to find the actual bug.<p>If reproduction is not possible, good luck. Better spend some quality time with memory hex view, disassembler, trying to locate registers and stack values that might contain pointers, etc. It might take a while to find the issue...
adricnetover 9 years ago
Thanks for sharing this! I could wish for some tool use (gdb) screenshots, though really the inspiration to learn is valuable enough.<p>A next read in this vein might be Bug Hunter&#x27;s Diary by Tobias Klein: <a href="https:&#x2F;&#x2F;www.nostarch.com&#x2F;bughunter" rel="nofollow">https:&#x2F;&#x2F;www.nostarch.com&#x2F;bughunter</a>
64bitbrainover 9 years ago
Great article! I had a similar experience, a Java programmer turned Linux Kernel programmer. The first think I remember doing was debugging a kernel panic using crash. It is not doubt challenging for Java&#x2F;PHP programmers to debug C coredumps&#x2F;KP, when there is pointer reference involved. But at the same time I learned a lot, specially memory management.
mschuster91over 9 years ago
I wonder how many of the young so called &quot;programmers&quot; would be able to solve this. It&#x27;s a pity that one can call himself a &quot;developer&quot; without having the tiniest idea how a computer actually runs, what a process, a thread or a pointer is...
评论 #10843652 未加载
评论 #10843834 未加载