contributed by < Xx-oX
>
5
巨集 ACCESS_ONCE
作用是確實地讀取所指定記憶體位址的內容值,且限這一次。
常用於防止編譯器最佳化導致讀取變數的指令被省略。
下方是可能的實作
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define __READ_ONCE_SIZE \
({ \
switch (size) { \
case 1: \
*(uint8_t *) res = *(volatile uint8_t *) p; \
break; \
case 2: \
*(uint16_t *) res = *(volatile uint16_t *) p; \
break; \
case 4: \
*(uint32_t *) res = *(volatile uint32_t *) p; \
break; \
case 8: \
*(uint64_t *) res = *(volatile uint64_t *) p; \
break; \
default: \
memcpy((void *) res, (const void *) p, size); \
} \
})
static inline void __read_once_size(const volatile void *p, void *res, int size)
{
__READ_ONCE_SIZE;
}
#define READ_ONCE(x) \
({ \
union { \
typeof(x) __val; \
DECL0; \
} __u; \
__read_once_size(&(x), __u.__c, sizeof(x)); \
__u.__val; \
})
其中 DECL0
應填入 char[1] __c
觀察 union __u
可以發現其中有兩個成員 __val
以及 __c
(由下方程式碼得知名稱)
而 union 的特性為所有成員的初始記憶體位置均對齊,並且大小由最大的成員決定
__read_once_size()
中使用 volatile
關鍵字告知編譯器不要最佳化這段程式碼,
並且將 __u.__c
的大小開成 x
所佔的大小,
如此一來讀取跟 __c
共用記憶體的 __val
就可以保證讀取一次 x
,達到想要的效果。
而之所以將 __c
的型態設成 char[1]
則是因為
x
所需之空間擴張 => 大小設為 1 byte__c
要是一個 pointer (從 __read_once_size()
的呼叫中可以得知)使用 char[1]
便可以同時達到這兩個目的。
contributed by < Xx-oX > cserv cserv is an event-driven and non-blocking web server. cserv 是一個高效的網頁伺服器 採用非阻塞式 I/O 的事件驅動架構 單執行緒、支援多核並具備 CPU 親和性 執行方法
Jun 1, 2023The Life Cycles of Cyber Threats The Life Cycles of Cyber Threats 4 stagesdiscovery and development - discovery a vulnerability introduction - an exploit can begin to be used against operational systems growth - malicious actors will know that the exploit can be effectively used against a set of targets maturation - fixes are developed to address the problem Cyber Kill Chain The Cyber Kill Chain
May 9, 2023Q/As 1. 哪些config可以設定buffer 參考 rfc 3416 規範 The maximum size of an SNMP message is limited to the minimum of: (1) the maximum message size which the destination SNMP entity can accept; and, (2) the maximum message size which the source SNMP entity can generate. 可以藉由修改 agent 或 manager 的 maximum messgae size 來限制 SNMP message 的 maximum size
May 9, 2023reference Definition Data Exchange The process of sending and receiving data so that the information content or meaning attributed to the data is not changed during transmission. 用處
Jul 18, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up