# 2018q3 Homework3 (review) contributed by < [`butastur-rtos`](https://github.com/butastur-rtos) > ###### tags: `homework3` `sysprog2018` `2018q3` `review` `c99` `macro` `list` `linked list` `bit-fields` `union` `cache` `pointer` `memory leak` - [ ] [2018q3 第 1 週測驗題](https://hackmd.io/s/S1a9-YO_Q) - [ ] [2018q3 第 2 週測驗題](https://hackmd.io/s/BJA6LjbK7) - [ ] [2018q3 第 3 週測驗題](https://hackmd.io/s/BkyQskjK7) ## 複習背景知識 * C99規格書 * bitwise operator * pointer, union, bit-fields * linked list ### C99規格書 ### bitwise operator * pointer, union, bit-fields * pointer * union * bit-fields ### linked list * list.h ## 2018q3 第 1 週測驗題 * XOR * 解釋和實作循環冗餘碼 (CRC) 程式碼 ## [2018q3 第 2 週測驗題](https://hackmd.io/s/BJA6LjbK7) * 測驗 1 * 測驗 2 * 測驗 3 * 在 GitHub 找出使用 the pointer to the pointer 的 C 語言程式碼,並加以討論 * 翻閱 C 語言規格書解說合法 C99 程式 * 藉由 AddressSanitizer 一類的記憶體分析工具,探討上述程式碼在執行時期的行為,並且比較你引入的改善是否有效益 * 在 glibc 原始程式碼找出類似作用和寫法的程式碼,並探討其實作技巧 * 參照 Linux 核心原始程式碼 (以及 git log),探討對應的修正 * 在 Common Vulnerabilities and Exposure (CVE) 找出類似的安全弱點,並且進行案例分析 ### 測驗 1 * 指出存在的問題和提出修正機制,需要用 C99/C11 規格解釋。 * 在 [Common Vulnerabilities and Exposure](https://cve.mitre.org/) (CVE) 找出類似上述不當的 string literal 操作,而導致的安全漏洞,並加以探討 ```clike const char *p; void dont_do_this(void) { const char c_str[] = "This will change"; p = c_str; } ``` #### 指出存在的問題和提出修正機制,需要用 C99/C11 規格解釋。 * undefined behavior [ [6.2.4] ](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) C99/C11 6.2.4 object referred out of lifetime is undefined behavior 當離開這個 function dont_do_this 的時候, c_str 的 lifetime 就已經結束, 如果再 access const char *p,就會是一個 undefined behavior 修正機制就是讓離開 dont_do_this 的時候, const char *p 所 referred 的 object 的 lifetime 不要結束, 可以用 strdup ```clike p = strdup(c_str); ``` #### 在 [Common Vulnerabilities and Exposure](https://cve.mitre.org/) (CVE) 找出類似上述不當的 string literal 操作,而導致的安全漏洞,並加以探討 ### [測驗 2](https://hackmd.io/s/BJA6LjbK7) * 說明 void (*signal(int sig, void (*handler)(int))) (int); * [SIGACTION(2)](http://man7.org/linux/man-pages/man2/sigaction.2.html) * 解釋 signal(2) 的作用,並在 GitHub 找出應用案例 #### 說明 signal ```clike void (*signal(int sig, void (*handler)(int))) (int); ``` [man signal(2)](http://man7.org/linux/man-pages/man2/signal.2.html) * signal 會返回一個 pointer to function (int) return void * signal() 替 sig 指定一個 signal handler ### 測驗 3 * 為何每個 head 使用時都要先加上 () * 在 Linux 核心原始程式碼找出類似上述「走訪節點」的片段,討論其實作技巧和考量點 [source](https://github.com/torvalds/linux/blob/master/include/linux/list.h#L458) ```clike #define list_for_each_prev(pos, head) \ for (pos = (head)->prev; pos != (head); pos = pos->prev) ``` #### 為何每個 head 使用時都要先加上 () for example: &ptr->prev 是等價於以下哪一種? 1. <b>&(ptr->prev)</b> 2. <b>(&ptr)->prev</b> 如果 head 沒有加上 (), 那 &ptr->prev 會等價於 <b>&(ptr->prev)</b>, 所以需要加上 () #### 在 Linux 核心原始程式碼找出類似上述「走訪節點」的片段,討論其實作技巧和考量點 * 找出類似上述「走訪節點」的片段 * 討論其實作技巧和考量點 [fib_trie.c](https://github.com/torvalds/linux/blob/master/net/ipv4/fib_trie.c#L2661) 為什麼會注意到這個檔案呢? 是由於看了以色列人 Rami Rosen 寫的一本書 Linux Kernel Networking - Implementation and Theory(簡中版 「精通 Linux 內核網路」) ```clike static int fib_route_seq_show(struct seq_file *seq, void *v) { struct fib_alias *fa; struct key_vector *l = v; ... hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) { ... } ... } ``` ## 2018q3 第 3 週測驗題 * union * 解釋 parity bit 原理 * 透過 SIMD 計算 parity bit * string literal * insert_sorted ### 解釋以下運作原理,並找出類似的程式碼 ```clike int main() { union { int a; char b; } c = { .a = K1 }; return c.b == K2; } ``` ### 解釋 parity bit 原理 ### 透過 SIMD 計算 parity bit * SIMD * Calculate parity bit without SIMD * Calculate parity bit with SIMD ### 說明 string literal ### insert_sorted * 解釋運作原理 * 新增刪除和搜尋節點 * 完整的測試計畫 :::danger 持續更新中 :::