{"id":626,"date":"2019-12-05T21:58:30","date_gmt":"2019-12-06T05:58:30","guid":{"rendered":"http:\/\/35.243.195.209\/?p=626"},"modified":"2019-12-05T21:58:30","modified_gmt":"2019-12-06T05:58:30","slug":"addresssanitizer","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2019\/12\/05\/addresssanitizer\/","title":{"rendered":"AddressSanitizer"},"content":{"rendered":"<h2>Summary<\/h2>\n<p>In this post, I will introduce a useful tool called AddressSanitizer. The motivation of it is that I have a debate with one of my colleagues about whether it is necessary to detect memory leaks in small projects. I was the one who persists to avoid memory issues though the program is small and terminates after a single usage. I did some research on AddressSanitizer thanks to the debate.<\/p>\n<h2>Conclusion<\/h2>\n<p>C\/C++ is fast. It does not have garbage collection and programmers need to explicitly manage memories. <\/p>\n<p>To keep your programs safe, compile and link your program using <code>clang<\/code> or <code>gcc<\/code> with the <code>-fsanitize=address<\/code> switch. To get nicer stack traces in error messages add <code>-fno-omit-frame-pointer<\/code>. To get reasonable performance, add <code>-O1<\/code> or higher.<br \/>\nIt detects:<\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleUseAfterFree\">Use after free<\/a> (dangling pointer dereference)<\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleHeapOutOfBounds\">Heap buffer overflow<\/a> (out of boundary)<\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleStackOutOfBounds\">Stack buffer overflow<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleGlobalOutOfBounds\">Global buffer overflow<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleUseAfterReturn\">Use after return<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerExampleUseAfterScope\">Use after scope<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerInitializationOrderFiasco\">Initialization order bugs<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizerLeakSanitizer\">Memory leaks<\/a><\/li>\n<\/ol>\n<h2>Details<\/h2>\n<p>The project <a href=\"https:\/\/github.com\/google\/sanitizers\/wiki\/AddressSanitizer\">AddressSanitizer<\/a> is a very popular open-source project.  The followings are some facts about it:<\/p>\n<ol>\n<li>Popularity: Google develops and maintains it. The project has 4.5k stars and 300+ forks. AddressSanitizer is currently implemented in both Clang and GCC. It is part of Linux kernel as well. <\/li>\n<li>Correctness: The project has a <a href=\"https:\/\/static.googleusercontent.com\/media\/research.google.com\/en\/\/pubs\/archive\/37752.pdf\">paper<\/a> with nearly 500 citings to support it. ; the tool has no false positives.<\/li>\n<li>Users: Chromium and Firefox, the two most famous open-source browsers, are using AddressSanitizer. <\/li>\n<li>Overhead:  The average slowdown of the instrumented program is <code>~2x<\/code>. The increased memory usage is <code>~4x<\/code>.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Summary In this post, I will introduce a useful tool called AddressSanitizer. The motivation of it is that I have a debate with one of my colleagues about whether it is necessary to detect memory leaks in small projects. I was the one who persists to avoid memory issues though the program is small and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5,45],"tags":[],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-c","category-proglang","category-useful-tools"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":2,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":629,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/626\/revisions\/629"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}