在寫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的更多相关文章

  1. data structure alignment(数据对齐)

    概述: 数据对齐指数据在计算机内存中排放和获取的方式.包含三个方面:数据对齐(data alignment).数据结构填充(data alignment).打包(packing) 如果数据是自然对齐的 ...

  2. 对象内存 (扩展 Data Structure Alignment)

    对于一个class object来说,我们需要多少内存才能表现出来,大致分为3类,这里在前面文章有内存图 (1)非静态数据成员的综合大小,这也符合了c++对象模型的结构 (2)填充字节,就是我们所说的 ...

  3. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  4. 笔记-python-tutorial-5.data structure

    笔记-python-tutorial-5.data structure 1.      data structure 1.1.    list operation list.append(x) #尾部 ...

  5. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  9. Basic Data Structure

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

随机推荐

  1. Redis项目实战,一些经验总结

    来源:https://my.oschina.net/u/920698/blog/3031587 背景 Redis 是一个开源的内存数据结构存储系统. 可以作为数据库.缓存和消息中间件使用. 支持多种类 ...

  2. leetcode.排序.451根据字符出现频率排序-Java

    1. 具体题目 给定一个字符串,请将字符串里的字符按照出现的频率降序排列. 示例 1: 输入: "tree" 输出: "eert" 解释: 'e'出现两次,'r ...

  3. luoguP1313 计算系数 题解(NOIP2011)

    P1313 计算系数 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<cm ...

  4. 34.Merge Intervals(合并序列)

    Level:   Medium 题目描述: Given a collection of intervals, merge all overlapping intervals. Example 1: I ...

  5. android SharedPreferences 存储文件

  6. selectDOM操作详解-select option详细解释

    首先从一个面试题来讲,我最近在做前端面试题,题目如下: 完成函数showlmg(),要求能够动态根据下拉列表的选项变化,更新图片的显示<body><script type=" ...

  7. Nginx之Keepalived

    目录 Nginx之Keepalived 1. Keepalived 高可用基本概述 1.1 什么是高可用 1.2 高可用通常使用什么软件? 1.3 keepalived是如何实现高可用的? 1.4 那 ...

  8. linux 用户空间与内核空间——高端内存了解

    Linux 操作系统和驱动程序运行在内核空间,应用程序运行在用户空间,两者不能简单地使用指针传递数据,因为Linux使用的虚拟内存机制,用户空间的数据可能被换出,当内核空间使用用户空间指针时,对应的数 ...

  9. Spring 2.5配置文件详解(转)

    http://book.51cto.com/art/201004/193743.htm 6.2.3  Spring 2.5配置文件详解 Spring配置文件是用于指导Spring工厂进行Bean生产. ...

  10. Linux下tree的使用介绍

    1.在ubuntu系统中默认是没有tree这个命令的,需要安装,用下面的命令就可以安装tree这个命令工具sudo apt-get install tree. 2.首先来说说使用tree这个命令,就是 ...