{"id":1774,"date":"2018-02-03T11:13:45","date_gmt":"2018-02-03T01:13:45","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=1774"},"modified":"2018-02-03T11:14:06","modified_gmt":"2018-02-03T01:14:06","slug":"watch-as-the-os-rewrite-my-buggy-program","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=1774","title":{"rendered":"Watch as the OS rewrites my buggy program."},"content":{"rendered":"<p>I didn&#8217;t know that SetErrorMode(SEM_NOALIGNMENTFAULTEXCEPT) was a thing, until I wrote a bad test that wouldn&#8217;t crash.<\/p>\n<p>Digging into it, I found that a movaps instruction was being rewritten as movups, which was a\u00a0<em>thoroughly<\/em> confusing thing to see.<\/p>\n<p>The one clue I had was that a fault due to an unaligned load had been observed in non-test code, but did not reproduce when written as a test using the google-test framework. A short hunt later (including a failed attempt at writing a small repro case), I found an explanation: <a href=\"https:\/\/github.com\/google\/googletest\/blob\/master\/googletest\/src\/gtest.cc#L4225\">google test suppresses this class of failure<\/a>.<\/p>\n<p>The code below will successfully demonstrate the behavior, printing out the SIMD load instruction before and after calling the function with an unaligned pointer.<\/p>\n<p>[<a href=\"https:\/\/gist.github.com\/twoscomplement\/1d03dcbcb22efb407b57295c94f67d88\">Gist<\/a>]<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/twoscomplement\/1d03dcbcb22efb407b57295c94f67d88.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/twoscomplement\/1d03dcbcb22efb407b57295c94f67d88\">Gist<\/a>.<\/noscript><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I didn&#8217;t know that SetErrorMode(SEM_NOALIGNMENTFAULTEXCEPT) was a thing, until I wrote a bad test that wouldn&#8217;t crash. Digging into it, I found that a movaps instruction was being rewritten as movups, which was a\u00a0thoroughly confusing thing to see. The one clue I had was that a fault due to an unaligned load had been observed &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=1774\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Watch as the OS rewrites my buggy program.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,26],"tags":[],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1774"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1774"}],"version-history":[{"count":4,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1774\/revisions"}],"predecessor-version":[{"id":1778,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1774\/revisions\/1778"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}