Data structure alignment by binary operation
在寫C的過程中,我們會很自然地以為,我連續宣告一堆大小不一的char array。
經過Complier之後這些char array未必是連續擺放。至於為什麼就要談到我們今天的主角了alignment
以x86-32bit為例,
他喜歡一次讀取 4 Bytes (i.e. 32 bits),記憶中可以想成一格一格為 4 Bytes.其indes從 0 ~ 2^32 - 1
我們可以用 printf(sizeof(void *)); 來得知。machine在讀取指令時以多少Bytes為單位。
printf(sizeof(unsigned long));也行。
好讓每次讀取的位置皆為4的倍數,e.g. 0, 4, 8, 12 ...etc.
為了電腦的執行速度,Complier 會幫我們增加一些padding(填充),好讓每次讀取的位置都能是4的倍數。
實例:
在exploit(http://www.exploit-db.com/exploits/15285/)中
假設 def_ops 指向struct security_operations開頭
先看一下 Kernel source code
struct security_operations {
char name[SECURITY_NAME_MAX + ]; //SECURITY_NAME_MAX 預設10,所以我們知道為何target要+11
int (*ptrace_access_check) (struct task_struct *child, unsigned int mode);
int (*ptrace_traceme) (struct task_struct *parent);
....
http://lxr.linux.no/linux+v2.6.36/include/linux/security.h#L1363
解讀一下exploit
target = def_ops + sizeof(void *) + (( + sizeof(void *)) & ~(sizeof(void *) - )); //target想跳至ptrace_traceme
第一個 sizeof(void *) 因為要跳過 int (*ptrace_access_check) 這個pointer。
然而 name 這個 string 因為大小為11,但是為了做alignment。所以寫成
( + sizeof(void *)) & ~(sizeof(void *) - )
11 長度的char array 為了要入memory 又要 alignment 4 的倍數情況下,是要 給他三格(i.e. 12 Bytes)
多出的那 1 Bytes, 就是我們所謂的padding。 而"&"這邊的技巧在Linux Kernel也有用到[註1]。
& ~(sizeof(void *) - 1) == & 1100 //sizeof(void *) = 4
而(11 + sizeof(void *)) & 1100 在 Binary的角度來看就是做mask來遮蔽後面2-bits
前面兩個2-bits則保留。而最後
(11 + sizeof(void *)) & ~(sizeof(void *) - 1) == 12 Bytes
用我們人類常用的10進位來舉例更貼近,假設記憶體中以0, 10, 20, 30 ...來存取指令。 假設有一到指令從0放到15的位置。為了alignment 10 的倍數, 我們必須這指令從0~20。 實作上就是 15 + 10 = 25 個位數的地方mask掉變成20。 就像前面我們做
& ~(sizeof(void *) - 1) 一樣。
[註1]
#define PAGE_MASK (~(PAGE_SIZE-1))
http://lxr.free-electrons.com/source/arch/arm/include/asm/page.h#L15
Data structure alignment by binary operation的更多相关文章
- data structure alignment(数据对齐)
		概述: 数据对齐指数据在计算机内存中排放和获取的方式.包含三个方面:数据对齐(data alignment).数据结构填充(data alignment).打包(packing) 如果数据是自然对齐的 ... 
- 对象内存     (扩展  Data Structure Alignment)
		对于一个class object来说,我们需要多少内存才能表现出来,大致分为3类,这里在前面文章有内存图 (1)非静态数据成员的综合大小,这也符合了c++对象模型的结构 (2)填充字节,就是我们所说的 ... 
- CDOJ 483 Data Structure Problem DFS
		Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ... 
- 笔记-python-tutorial-5.data structure
		笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ... 
- hdu-5929 Basic Data Structure(双端队列+模拟)
		题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ... 
- HDU 5929 Basic Data Structure 模拟
		Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ... 
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
		Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ... 
- [转]Data Structure Recovery using PIN and PyGraphviz
		Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ... 
- Basic Data Structure
		Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ... 
随机推荐
- mybatis关联查询之一对多查询
			一对多,是最常见的一种设计.就是 A 表的一条记录,对应 B 表的多条记录,且 A 的主键作为 B 表的外键.这主要看以哪张表为中心,下面的测试数据中,从employee 表来看,一个员工对应一个部门 ... 
- Fix invisible cursor issue in Ubuntu 13.10
			Fix invisible cursor issue in Ubuntu 13.10 Fixing this problem is rather too easy. Open a terminal ( ... 
- config maven in intellij IDEA
			Config maven in IDEA File ->Settings->Build,Execution.Deployment->build Too ... 
- Jquery的$.ajax、$.get、$.post发送、接收JSON数据及回调函数用法
			平时研究代码时,经常会遇到AJAX的相关用法,做项目时才真正体会到Ajax的强大之处(与服务器数据交互如此之便捷,更新DOM节点而不用刷新整个页面),以及运用的频繁程度.今天整理了一下自己之前没搞清楚 ... 
- C# Aspose.Words  数据写入到word,模板样式复杂(转换指定内容并返回多张图片)
			public ResultResponse<string[]> PrintStudyRecords([FromBody]StudyInfo info) { ResultResponse&l ... 
- wpf 查找控件
			public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : Framewo ... 
- exp ORA-01455: converting column overflows integer datatype
			EXP-00008: ORACLE error 1455 encounteredORA-01455: converting column overflows integer datatype add ... 
- js判断设备(转)
			<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ... 
- 关于Linux_系统资源监控_dmesg_free_uptime_uname
			(系统资源查看命令-dmesg[查看系统内核资源信息])->判断服务器的硬件状态 Comment:dmesg | grep CPU,指定查看cpu资源信息 (系统资源查看命令-free[查看内存 ... 
- 如何在Mac上将视频刻录到DVD / ISO文件
			如果您希望将喜爱的视频转换为DVD / Blu-ray光盘以进行物理备份或播放,则Mac版Wondershare UniConverter可以专业地完成任务.今天的教程就是如何在Mac上轻松刻录DVD ... 
