From 05b1e41581f12e0fa3ae9d24775a13469779c736 Mon Sep 17 00:00:00 2001 From: gshine Date: Tue, 7 May 2024 20:45:52 +0800 Subject: [PATCH] =?UTF-8?q?update=202024=E5=B9=B4=2005=E6=9C=88=2007?= =?UTF-8?q?=E6=97=A5=20=E6=98=9F=E6=9C=9F=E4=BA=8C=2020:45:52=20CST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../posts/sysprog/c-compiler-optimization.md | 2 + content/posts/sysprog/c-undefined-behavior.md | 14 +- docs/404.html | 2 +- docs/categories/c/index.html | 2 +- docs/categories/c/index.xml | 4 +- docs/categories/concurrency/index.html | 2 +- docs/categories/csapp/index.html | 2 +- docs/categories/draft/index.html | 2 +- docs/categories/index.html | 2 +- .../linux-kernel-internals/index.html | 2 +- .../linux-kernel-internals/index.xml | 4 +- .../linux-kernel-internals/page/2/index.html | 2 +- docs/categories/linux/index.html | 2 +- docs/categories/mathematics/index.html | 2 +- docs/categories/network/index.html | 2 +- docs/categories/risc-v/index.html | 2 +- docs/categories/rust/index.html | 2 +- docs/categories/systems/index.html | 2 +- docs/categories/toolkit/index.html | 2 +- docs/friends/index.html | 17 +- docs/index.html | 8 +- docs/index.json | 2 +- docs/index.xml | 4 +- docs/page/10/index.html | 4 +- docs/page/2/index.html | 4 +- docs/page/3/index.html | 4 +- docs/page/4/index.html | 4 +- docs/page/5/index.html | 4 +- docs/page/6/index.html | 4 +- docs/page/7/index.html | 4 +- docs/page/8/index.html | 4 +- docs/page/9/index.html | 4 +- docs/posts/binary-representation/index.html | 2 +- docs/posts/c-bitwise/index.html | 2 +- docs/posts/c-compiler-construction/index.html | 157 +++++- docs/posts/c-compiler-construction/index.md | 91 ++- docs/posts/c-compiler-optimization/index.html | 28 +- docs/posts/c-compiler-optimization/index.md | 2 + docs/posts/c-control-flow/index.html | 2 +- docs/posts/c-function/index.html | 2 +- docs/posts/c-linked-list/index.html | 2 +- docs/posts/c-memory/index.html | 2 +- docs/posts/c-numerics/index.html | 2 +- docs/posts/c-pointer/index.html | 2 +- docs/posts/c-preprocessor/index.html | 2 +- docs/posts/c-recursion/index.html | 2 +- docs/posts/c-specification/index.html | 2 +- docs/posts/c-standards/index.html | 2 +- docs/posts/c-std-security/index.html | 2 +- docs/posts/c-trick/index.html | 2 +- docs/posts/c-undefined-behavior/index.html | 522 +++++++++++++++++- docs/posts/c-undefined-behavior/index.md | 335 ++++++++++- docs/posts/channels/index.html | 2 +- docs/posts/concurrency-concepts/index.html | 2 +- docs/posts/concurrency-ordering/index.html | 2 +- docs/posts/csapp-ch2/index.html | 2 +- docs/posts/debug-gdb/index.html | 2 +- docs/posts/declarative-macros/index.html | 2 +- docs/posts/deepin-dragonos/index.html | 2 +- docs/posts/deepin-kvm/index.html | 2 +- docs/posts/deepin20.9/index.html | 2 +- docs/posts/english/index.html | 2 +- docs/posts/git-learn/index.html | 2 +- docs/posts/git/index.html | 2 +- docs/posts/gnu-linux-dev/index.html | 2 +- docs/posts/hello_world/index.html | 22 +- docs/posts/hello_world/index.md | 5 +- docs/posts/index.html | 22 +- docs/posts/index.xml | 4 +- docs/posts/iterators/index.html | 2 +- docs/posts/lifetime-annotations/index.html | 2 +- docs/posts/linux-concepts/index.html | 2 +- docs/posts/linux-dev-review/index.html | 2 +- docs/posts/linux-hashtable/index.html | 2 +- docs/posts/linux-quiz1/index.html | 2 +- docs/posts/linux-rbtree/index.html | 2 +- docs/posts/linux2023-lab0/index.html | 12 +- docs/posts/linux2023/index.html | 2 +- docs/posts/math/index.html | 2 +- docs/posts/nju-osdi/index.html | 2 +- docs/posts/nthu-computer-network/index.html | 2 +- docs/posts/oerv-pretask/index.html | 2 +- docs/posts/openeuler-riscv-qemu/index.html | 2 +- docs/posts/orst/index.html | 2 +- docs/posts/page/2/index.html | 4 +- docs/posts/page/3/index.html | 4 +- docs/posts/posix-threads/index.html | 2 +- .../posts/riscv-optimization-guide/index.html | 2 +- docs/posts/rust-cli/index.html | 2 +- docs/posts/rust-lifetime/index.html | 2 +- docs/posts/rust-tcp/index.html | 2 +- .../index.html | 2 +- docs/posts/subtying-and-variance/index.html | 2 +- docs/posts/ubuntu22.04lts/index.html | 2 +- docs/posts/why-rust-/index.html | 2 +- docs/posts/wsl2/index.html | 2 +- docs/sitemap.xml | 54 +- docs/tags/algorithm/index.html | 2 +- docs/tags/architecture/index.html | 2 +- docs/tags/bitwise/index.html | 2 +- docs/tags/c/c++/index.html | 2 +- docs/tags/c/index.html | 2 +- docs/tags/c/index.xml | 4 +- docs/tags/channel/index.html | 2 +- docs/tags/chroot/index.html | 2 +- docs/tags/cli/index.html | 2 +- docs/tags/compiler/index.html | 2 +- docs/tags/compiler/index.xml | 4 +- docs/tags/concurrency/index.html | 2 +- docs/tags/container/index.html | 2 +- docs/tags/control-flow/index.html | 2 +- docs/tags/csapp/index.html | 2 +- docs/tags/debug/index.html | 2 +- docs/tags/declarative-macros/index.html | 2 +- docs/tags/deepin/index.html | 2 +- docs/tags/draft/index.html | 2 +- docs/tags/dragonos/index.html | 2 +- docs/tags/function/index.html | 2 +- docs/tags/gdb/index.html | 2 +- docs/tags/git/index.html | 2 +- docs/tags/github/index.html | 2 +- docs/tags/hash-table/index.html | 2 +- docs/tags/index.html | 2 +- docs/tags/interior-mutability/index.html | 2 +- docs/tags/iterator/index.html | 2 +- docs/tags/kvm/index.html | 2 +- docs/tags/lifetime/index.html | 2 +- docs/tags/linked-list/index.html | 2 +- docs/tags/linux/index.html | 2 +- docs/tags/macro/index.html | 2 +- docs/tags/mathematics/index.html | 2 +- docs/tags/memory/index.html | 2 +- docs/tags/mugen/index.html | 2 +- docs/tags/neofetch/index.html | 2 +- docs/tags/network/index.html | 2 +- docs/tags/nspawn/index.html | 2 +- docs/tags/numerics/index.html | 2 +- docs/tags/openeuler/index.html | 2 +- docs/tags/optimization/index.html | 2 +- docs/tags/optimization/index.xml | 4 +- docs/tags/pointer/index.html | 2 +- docs/tags/preprocessor/index.html | 2 +- docs/tags/qemu/index.html | 2 +- docs/tags/recursion/index.html | 2 +- docs/tags/red-black-tree/index.html | 2 +- docs/tags/risc-v/index.html | 2 +- docs/tags/rust/index.html | 2 +- docs/tags/security/index.html | 2 +- docs/tags/smart-pointer/index.html | 2 +- docs/tags/sort/index.html | 2 +- docs/tags/standard/index.html | 2 +- docs/tags/subtying/index.html | 2 +- docs/tags/sysprog/index.html | 2 +- docs/tags/sysprog/index.xml | 4 +- docs/tags/sysprog/page/2/index.html | 2 +- docs/tags/tcp/index.html | 2 +- docs/tags/ubuntu/index.html | 2 +- docs/tags/variance/index.html | 2 +- docs/tags/windows/index.html | 2 +- docs/tags/wsl/index.html | 2 +- 160 files changed, 1344 insertions(+), 275 deletions(-) diff --git a/content/posts/sysprog/c-compiler-optimization.md b/content/posts/sysprog/c-compiler-optimization.md index 650be772..01814e28 100644 --- a/content/posts/sysprog/c-compiler-optimization.md +++ b/content/posts/sysprog/c-compiler-optimization.md @@ -42,4 +42,6 @@ repost: # See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter --- +> 編譯器最佳化篇將以 gcc / llvm 為探討對象,簡述編譯器如何運作,以及如何實現最佳化,佐以探究 C 編譯器原理和案例分析,相信可以釐清許多人對 C 編譯器的誤解,從而開發出更可靠、更高效的程式。 + diff --git a/content/posts/sysprog/c-undefined-behavior.md b/content/posts/sysprog/c-undefined-behavior.md index bb098f8a..10992093 100644 --- a/content/posts/sysprog/c-undefined-behavior.md +++ b/content/posts/sysprog/c-undefined-behavior.md @@ -75,7 +75,7 @@ i = i++ + ++i; 从这个角度看,UB 其实就是不遵守语言规范 (等价于 API 的限制) 的行为,编译器对于语言规范的实现一般来说是不考虑 UB 的 (等价于 API 的内部实现),所以因为 UB 造成的结果需要程序员自行承担 (等价于不遵守限制乱用 API 需要自己承担责任)。所以单纯考虑 UB 是没啥意义的,因为它只是结果的具体表现,应该从语言规范和编译器的角度考虑。 -除此之外,因为编译器作为语言规范的实作,它在最佳化时会一般只考虑符合语言规范的部分,简而言之,编译器可能会将 UB 部分的代码移除掉 (越激进的优化越有可能)。 +除此之外,因为编译器作为语言规范的实作,它在最佳化时会一般 **只考虑符合语言规范** 的部分,简而言之,编译器可能会将 UB 部分的代码移除掉 (越激进的优化越有可能)。因为它认为源程序已经是符合语言规范的,所以会移除掉在符合规范情况下显得不必要的逻辑。 {{< /admonition >}} ```c @@ -357,12 +357,16 @@ Note that what exactly is considered undefined differs slightly between C and C+ {{< image src="https://image.slidesharecdn.com/bkk16503-160212222433/75/BKK16-503-Undefined-Behavior-and-Compiler-Optimizations-Why-Your-Program-Stopped-Working-With-A-Newer-Compiler-24-2048.jpg" >}} -{{< image src="https://image.slidesharecdn.com/bkk16503-160212222433/75/BKK16-503-Undefined-Behavior-and-Compiler-Optimizations-Why-Your-Program-Stopped-Working-With-A-Newer-Compiler-25-2048.jpg" >}} +- [x] LWN [Fun with NULL pointers](https://lwn.net/Articles/342330/) -- [ ] LWN [Fun with NULL pointers](https://lwn.net/Articles/342330/) -- gcc [PR68853](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853) -- Wikidepia: [Linux kernel oops](https://en.wikipedia.org/wiki/Linux_kernel_oops) +> There is one little problem with that reasoning, though: NULL (zero) can actually be a valid pointer address. By default, the very bottom of the virtual address space (the "zero page," along with a few pages above it) is set to disallow all access as a way of catching null-pointer bugs (like the one described above) in both user and kernel space. But it is possible, using the mmap() system call, to put real memory at the bottom of the virtual address space. + +> This is where the next interesting step in the chain of failures happens: the GCC compiler will, by default, optimize the NULL test out. The reasoning is that, since the pointer has already been dereferenced (and has not been changed), it cannot be NULL. So there is no point in checking it. Once again, this logic makes sense most of the time, but not in situations where NULL might actually be a valid pointer. +> A NULL pointer was dereferenced before being checked, the check was optimized out by the compiler + +- gcc [PR68853](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853) +- Wikidepia: [Linux kernel oops](https://en.wikipedia.org/wiki/Linux_kernel_oops) {{< image src="https://upload.wikimedia.org/wikipedia/commons/6/6a/Linux-2.6-oops-parisc.jpg" >}} ### Pointer arithmetic that wraps diff --git a/docs/404.html b/docs/404.html index b092cf0b..f15d63ac 100644 --- a/docs/404.html +++ b/docs/404.html @@ -143,7 +143,7 @@

document.getElementById('error-emoji').appendChild(document.createTextNode(emojiArray[Math.floor(Math.random() * emojiArray.length)])); })();