??????д C ??????????????????????????????????????????????????????д洢?Щ?????????????????????????????????????????е?????????????
????????е???????????????????????????????????????С????????????????????????Ч???????????????????С??????????????е??????е???????????32λ?????????????4??????????????????????????4??????????
???????棬?????????????????????С??? 4 ????
??????????????????????????????????????????????????????0×1000????0×1004????0×1008???????????????0×1001????0×1002????0×1003?????????????4???????κε??????????4?????????????????????00??β????????????ζ???????????????????????????λ????0??
?????????2??????г????κ?????????????????????????????????????????У????????????????????????????????????????????
????????C????????λ???????????????????????????????????????????????
????????????μ??????????Ρ???????????github???????е? hide-data-in-ptr??

 

void put_data(int *p?? unsigned int data)
{
assert(data < 4);
*p |= data;
}
unsigned int get_data(unsigned int p)
{
return (p & 3);
}
void cleanse_pointer(int *p)
{
*p &= ~3;
}
int main(void)
{
unsigned int x = 701;
unsigned int p = (unsigned int) &x;
printf("Original ptr: %un"?? p);
put_data(&p?? 3);
printf("ptr with data: %un"?? p);
printf("data stored in ptr: %un"?? get_data(p));
cleanse_pointer(&p);
printf("Cleansed ptr: %un"?? p);
printf("Dereferencing cleansed ptr: %un"?? *(int*)p);
return 0;
}

???????????????:
????Original ptr:  3216722220
????ptr with data: 3216722223
????data stored in ptr: 3
????Cleansed ptr:  3216722220
????Dereferencing cleansed ptr: 701
?????????????????д洢?κο?????????????λ?????????????put_data()?????????????????λ???洢???????????????????get_data()???????????????????λ???е?λ?????????????????????????????????????
????cleanse_pointer()??????????λ?????????????????á?????????ЩCPU??Intel???????????δ?????????????????CPU??ARM??????????????????μ??????????????????????????????
????????????????????
????????????á???Linux????к?????????????????
???????????????£?
????struct rb_node {
????unsigned long  __rb_parent_color;
????struct rb_node *rb_right;
????struct rb_node *rb_left;
????} __attribute__((aligned(sizeof(long))));
???????unsigned long __rb_parent_color?洢???????????
????1??????????
????2?????????
????????????0????????1????????
???????????????????????????????????“?????”????λ?С?
???????濴??????????????????λ?????
????/* in rbtree.h */
????#define rb_parent(r)   ((struct rb_node *)((r)->__rb_parent_color & ~3))
????/* in rbtree_augmented.h */
????#define __rb_color(pc)     ((pc) & 1)
????#define rb_color(rb)       __rb_color((rb)->__rb_parent_color)