一例 Go 編譯器代碼優化 bug 定位和修復解析

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"摘要"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文中介紹了 Go 編譯器的整體編譯流程脈絡和一個編譯優化錯誤導致數據越界訪問的 bug,並分析了對這個 bug 的排查和修復過程,希望能夠藉此讓大家對 Go 編譯器有更多的瞭解,在遇到類似問題時有排查思路。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"緣起"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"某日,一位友人在羣裏招呼我,“看到有人給 Go 提了個編譯器的 bug,挺有意思,感覺還挺嚴重的,要不要來看看?”於是我打開了 "},{"type":"text","marks":[{"type":"strong"}],"text":"issue 40367"},{"type":"text","text":" "},{"type":"katexinline","attrs":{"mathString":"^{[1]}"}},{"type":"text","text":" 。彼時,最新一條評論是 "},{"type":"text","marks":[{"type":"strong"}],"text":"這條"},{"type":"text","text":" "},{"type":"katexinline","attrs":{"mathString":"^{[2]}"}},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/4e\/4d\/4ec8e54bf59c2f9499a0614ecbbb974d.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提到將循環體中的一個常數從 1 改成 2 就無法復現問題,這頓時勾起了我的興趣,於是我準備研究一番。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bug 代碼跟現象如下圖,正常來看,代碼應該在輸出 \"5 6\" 後停止,然而實際上卻無限執行了下去,只能強行終止或等待程序觸碰到無權限內存地址之後崩潰。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.infoq.cn\/resource\/image\/70\/b1\/7027c65b007edacb60ec5d47ea2c8cb1.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章