在application程序猿的眼中,C语言写内存的方法,千篇一律,如下:
struct mystr * ptr;
ptr = malloc(sizeof(*ptr));
ptr->fildA='abc';
ptr->fildB=89;
但是,如果是系统程序,没有malloc,甚至需要你自己设计malloc,如何来写内存呢?
下边为大家展示一种技术,此技术比较血腥和暴力,少儿不宜。
满了18岁的小朋友可以继续观看。
1 #include
2 #include
3
4 struct str1 {
5 int i;
6 char j;
7 };
8
9 int main(){
10 struct str1 * ptr1;
11 ptr1 = malloc(sizeof(*ptr1));
12 *((int*)ptr1)=23;
13 *((char*)((int*)ptr1 + 1))='a';
14
15 printf("now ptr1->i is:%dn",ptr1->i);
16 printf("now ptr1->j is:%dn",ptr1->j);
17
18 return 0;
19 }
胆子大一点的童鞋猜一猜呢,ptr1->i是多少呢?
没错,输出是这样的:
[root@localhost ~]# ./memwrite
now ptr1->i is:23
那么,ptr1->j是多少呢?
如果知道答案,那么,他为什么会是这个结果呢?
如果你还没有看懂,加我的QQ号吧。
如果你理解了本hack的核心原理,再去阅读Linux内核的分页管理、swap技术什么的,就易如反掌了。