10 static unsigned long magic_rand_next
;
11 static void magic_srand(unsigned int seed
)
13 magic_rand_next
= (unsigned long) seed
;
16 static int magic_rand()
18 magic_rand_next
= magic_rand_next
* 1103515245 + 12345;
19 return (int)(magic_rand_next
% ((unsigned long)RAND_MAX
+ 1));
21 static int magic_rand_seed()
24 return (int)&x
+ (int)&magic_rand_seed
;
27 #define magic_srand srand
28 #define magic_rand rand
29 #define magic_rand_seed() time(0)
32 #define MINIMUM_PADDING 1
34 PUBLIC
int magic_asr_get_padding_size(int region
) {
38 case MAGIC_STATE_HEAP
| MAGIC_ASR_FLAG_INIT
:
39 if(_magic_asr_heap_max_offset
){
40 padding
= (magic_rand() % _magic_asr_heap_max_offset
) + MINIMUM_PADDING
;
43 case MAGIC_STATE_HEAP
:
44 if(_magic_asr_heap_max_padding
){
45 padding
= (magic_rand() % _magic_asr_heap_max_padding
) + MINIMUM_PADDING
;
48 case MAGIC_STATE_MAP
| MAGIC_ASR_FLAG_INIT
:
49 if(_magic_asr_map_max_offset_pages
){
50 padding
= ((magic_rand() % _magic_asr_map_max_offset_pages
) + MINIMUM_PADDING
) * magic_get_sys_pagesize();
54 if(_magic_asr_map_max_padding_pages
){
55 padding
= ((magic_rand() % _magic_asr_map_max_padding_pages
) + MINIMUM_PADDING
) * magic_get_sys_pagesize();
64 PUBLIC
void magic_asr_permute_dsentries(struct _magic_dsentry
**first_dsentry_ptr
){
65 struct _magic_dsentry
*first_dsentry
= *first_dsentry_ptr
, *dsentry
= first_dsentry
, *last_dsentry
;
69 if(!_magic_asr_heap_map_do_permutate
){
71 * Dsentries order is reversed anyway, because newer dsentries are
72 * placed at the start of the linked list, instead of the end
77 while(dsentry
!= NULL
){
78 last_dsentry
= dsentry
;
80 dsentry
= dsentry
->next
;
83 for(i
=0; i
< n_dsentries
; i
++){
85 int pos
= magic_rand() % (n_dsentries
- i
);
86 struct _magic_dsentry
*prev_dsentry
= NULL
;
88 if((i
== 0) && (pos
== (n_dsentries
-1))){
90 * Rest of for-loop won't function correctly when last dsentry is chosen first.
91 * Instead, nothing has to be done in this case.
96 dsentry
= first_dsentry
;
99 prev_dsentry
= dsentry
;
100 dsentry
= dsentry
->next
;
104 first_dsentry
= first_dsentry
->next
;
106 prev_dsentry
->next
= dsentry
->next
;
109 dsentry
->next
= NULL
;
110 last_dsentry
->next
= dsentry
;
111 last_dsentry
= dsentry
;
113 *first_dsentry_ptr
= first_dsentry
;
116 PUBLIC
void magic_asr_init(){
117 int seed
, heap_offset
;
119 seed
= _magic_asr_seed
;
121 seed
= magic_rand_seed();
125 heap_offset
= magic_asr_get_padding_size(MAGIC_STATE_HEAP
|MAGIC_ASR_FLAG_INIT
);