Commit 3e344a2d authored by Arjan Oortgiese's avatar Arjan Oortgiese

Added support for positions relative to _ARRAY_ in graph_copy_with_names....

Added support for positions relative to _ARRAY_ in graph_copy_with_names. Relative positions are used for systems with address space layout randomization systems (e.g. MacOS).
parent 6653d64a
......@@ -21,6 +21,20 @@
# endif
#endif
#if defined (MACH_O64)
// Use positions relative to _ARRAY_ for address space layout randomization systems.
# define USE_DESC_RELATIVE_TO_ARRAY 1
#endif
int is_using_desc_relative_to_array()
{
#ifdef USE_DESC_RELATIVE_TO_ARRAY
return 1;
#else
return 0;
#endif
}
extern void *INT_descriptor,*CHAR,*BOOL,*REAL,*__STRING__,*__ARRAY__;
/*inline*/
......@@ -61,7 +75,7 @@ Int *copy_graph_to_string (Int *node_p,void *begin_free_heap,void *end_free_heap
if (((unsigned Int)node_p-(unsigned Int)begin_heap)>=heap_size){
if (heap_p>=(Int*)stack_begin)
return NULL;
# ifdef MACH_O64
# ifdef USE_DESC_RELATIVE_TO_ARRAY
heap_p[0]=3+(Int)node_p-(Int)&__ARRAY__;
# else
heap_p[0]=3+(Int)node_p;
......@@ -78,7 +92,7 @@ Int *copy_graph_to_string (Int *node_p,void *begin_free_heap,void *end_free_heap
if (!(desc & 1)){
*node_p=1+(Int)heap_p;
#ifdef MACH_O64
#ifdef USE_DESC_RELATIVE_TO_ARRAY
*heap_p++=desc-(Int)&__ARRAY__;
#else
*heap_p++=desc;
......@@ -139,7 +153,7 @@ Int *copy_graph_to_string (Int *node_p,void *begin_free_heap,void *end_free_heap
node_p+=3;
heap_p[0]=array_size;
#ifdef MACH_O64
#if defined (MACH_O64)
heap_p[1]=elem_desc==0 ? elem_desc : elem_desc-(Int)&__ARRAY__;
#else
heap_p[1]=elem_desc;
......@@ -510,7 +524,7 @@ void remove_forwarding_pointers_from_graph (Int *node_p,Int **stack_end)
if ((forwarding_pointer & 1)==0)
break;
#ifdef MACH_O64
#ifdef USE_DESC_RELATIVE_TO_ARRAY
desc = (Int)&__ARRAY__ + *((Int*)(forwarding_pointer-1));
#else
desc = *((Int*)(forwarding_pointer-1));
......
......@@ -21,6 +21,11 @@
# endif
#endif
#if defined (MACH_O64)
// Use positions relative to _ARRAY_ for address space layout randomization systems.
# define USE_DESC_RELATIVE_TO_ARRAY 1
#endif
extern void *INT_descriptor,*CHAR,*BOOL,*REAL,*__STRING__,*__ARRAY__;
extern Int small_integers[],static_characters[];
......@@ -62,7 +67,7 @@ Int *copy_string_to_graph (Int *string_p,void *begin_free_heap,void *end_free_he
if (!(desc & 1)){
*string_p=(Int)heap_p;
*arg_a=heap_p;
#ifdef MACH_O64
#ifdef USE_DESC_RELATIVE_TO_ARRAY
desc+=(Int)&__ARRAY__;
#endif
*heap_p=desc;
......@@ -167,7 +172,7 @@ Int *copy_string_to_graph (Int *string_p,void *begin_free_heap,void *end_free_he
array_size=string_p[1];
elem_desc=string_p[2];
string_p+=3;
#ifdef MACH_O64
#if defined (MACH_O64)
if (elem_desc!=0)
elem_desc+=(Int)&__ARRAY__;
#endif
......@@ -653,7 +658,7 @@ Int *copy_string_to_graph (Int *string_p,void *begin_free_heap,void *end_free_he
#ifdef THREAD
if (desc & 2){
# ifdef MACH_O64
# ifdef USE_DESC_RELATIVE_TO_ARRAY
node_p=(Int*)(desc-3)+(Int)&__ARRAY__;
# else
node_p=(Int*)(desc-3);
......@@ -693,7 +698,7 @@ void remove_forwarding_pointers_from_string (Int *string_p,Int *end_forwarding_p
Int desc;
desc=*(Int*)forwarding_pointer;
#ifdef MACH_O64
#ifdef USE_DESC_RELATIVE_TO_ARRAY
*string_p=desc-(Int)&__ARRAY__;
#else
*string_p=desc;
......
......@@ -110,6 +110,25 @@ is_Array_D d = code inline {
eq_desc_b ARRAY 1
}
// The c function is_using_desc_relative_to_array is defined in copy_graph_to_string.c
// the function returns 1 if positions are relative to _ARRAY_ and 0 when not.
is_using_desc_relative_to_array :: Int;
is_using_desc_relative_to_array = code {
ccall is_using_desc_relative_to_array ":I"
}
get_array_D :: !{#Int} -> Int;
get_array_D a = code {
pushD_a 0
pop_a 1
}
get_DESC_D :: !a -> Int;
get_DESC_D a = code {
pushD_a 0
pop_a 1
}
get_D_name :: !Int -> {#Char};
get_D_name d = code {
.d 0 1 i
......@@ -418,7 +437,7 @@ get_module desc
:: DescInfo = {di_prefix_arity_and_mod :: !Int, di_name :: !{#Char}};
info_of_desc_and_mod {desc,desc_mod_n}
info_of_desc_and_mod {desc,desc_mod_n} array_desc
| desc bitand 2==0
# arity = get_thunk_arity desc;
# desc = get_thunk_descriptor desc;
......@@ -508,50 +527,51 @@ lookup_descs i s n_descs symbols
= lookup_descs (i+(IF_INT_64_OR_32 24 12)+l) s n_descs symbols;
= abort (toString l+++" "+++toString d);
replace_descs_by_desc_numbers_and_build_desc_tree :: !Int !*{#Char} !Int !DescOrModTree
replace_descs_by_desc_numbers_and_build_desc_tree :: !Int !*{#Char} !Int !Int !DescOrModTree
-> (!*{#Char},!Int,!DescOrModTree);
replace_descs_by_desc_numbers_and_build_desc_tree i s n_descs desc_tree
replace_descs_by_desc_numbers_and_build_desc_tree i s n_descs array_desc desc_tree
| i>=size s
| i==size s
= (s,n_descs,desc_tree);
= abort "error in replace_descs_by_desc_numbers_and_build_desc_tree";
#! desc=get_D_from_string s i;
#! desc=desc+array_desc;
| desc bitand 1<>0
= replace_descs_by_desc_numbers_and_build_desc_tree (i+IF_INT_64_OR_32 8 4) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+IF_INT_64_OR_32 8 4) s n_descs array_desc desc_tree;
# (s,n_descs,desc_tree) = store_desc_n_and_add_desc desc i s n_descs desc_tree;
| desc bitand 2==0
# d = get_thunk_n_non_pointers desc;
= replace_descs_by_desc_numbers_and_build_desc_tree
(i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s n_descs desc_tree;
(i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s n_descs array_desc desc_tree;
# (d,not_array) = get_descriptor_n_non_pointers_and_not_array desc;
| not_array
= replace_descs_by_desc_numbers_and_build_desc_tree
(i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s n_descs desc_tree;
(i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s n_descs array_desc desc_tree;
| d==0 // _STRING_
#! l = get_D_from_string s (i+IF_INT_64_OR_32 8 4);
# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4);
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 16 8)+l) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 16 8)+l) s n_descs array_desc desc_tree;
| d==1 // _ARRAY_
#! d = get_D_from_string s (i+IF_INT_64_OR_32 16 8);
| d==0
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)) s n_descs array_desc desc_tree;
# (s,n_descs,desc_tree) = store_desc_n_and_add_desc d (i+IF_INT_64_OR_32 16 8) s n_descs desc_tree;
#! l = get_D_from_string s (i+IF_INT_64_OR_32 8 4);
| is_Int_D d
# l = l << IF_INT_64_OR_32 3 2;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs array_desc desc_tree;
| is_Real_D d
# l = l << 3;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs array_desc desc_tree;
| is_Bool_D d
# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4);
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs array_desc desc_tree;
# arity = get_D_node_arity d;
| arity>=256
# record_a_arity = get_D_record_a_arity d;
# record_b_arity = arity-256-record_a_arity;
# l = (l * record_b_arity) << IF_INT_64_OR_32 3 2;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs desc_tree;
= replace_descs_by_desc_numbers_and_build_desc_tree (i+(IF_INT_64_OR_32 24 12)+l) s n_descs array_desc desc_tree;
= abort (toString l+++" "+++toString d);
store_desc_n_and_add_desc :: Int Int !*{#Char} !Int !DescOrModTree -> (!*{#Char},!Int,!DescOrModTree);
......@@ -610,13 +630,14 @@ make_mod_array n_mods mod_tree
copy_to_string_with_names :: a -> (!*{#Char},!*{#DescInfo},!*{#String});
copy_to_string_with_names g
# array_desc = if (is_using_desc_relative_to_array == 1) (get_array_D {} - 2) 0;
# s = copy_to_string g;
# (s,n_descs,desc_tree) = replace_descs_by_desc_numbers_and_build_desc_tree 0 s 0 EmptyDescOrModTree;
# (s,n_descs,desc_tree) = replace_descs_by_desc_numbers_and_build_desc_tree 0 s 0 array_desc EmptyDescOrModTree;
# desc_a = make_desc_array n_descs desc_tree;
# (desc_a,n_mods,mod_tree) = make_module_tree desc_a;
# mod_a = make_mod_array n_mods mod_tree;
# mod_s_a = {#get_module_name mod \\ mod<-:mod_a};
# desc_s_a = {#info_of_desc_and_mod desc_and_mod \\ desc_and_mod <-:desc_a};
# desc_s_a = {#info_of_desc_and_mod desc_and_mod array_desc \\ desc_and_mod <-:desc_a};
= (s,desc_s_a,mod_s_a);
lookup_symbol_value {di_prefix_arity_and_mod,di_name} mod_a symbols
......@@ -643,54 +664,59 @@ lookup_symbol_value {di_prefix_arity_and_mod,di_name} mod_a symbols
lookup_symbol_values desc_info_a mod_a symbols
= {#lookup_symbol_value desc_info mod_a symbols \\ desc_info <-: desc_info_a};
replace_desc_numbers_by_descs :: !Int !*{#Char} !{#Int} -> *{#Char};
replace_desc_numbers_by_descs i s symbol_a
replace_desc_numbers_by_descs :: !Int !*{#Char} !{#Int} !Int !Int -> *{#Char};
replace_desc_numbers_by_descs i s symbol_a symbol_offset array_desc
| i>=size s
| i==size s
= s;
= abort ("error in replace_desc_numbers_by_descs "+++toString i);
#! desc=get_D_from_string s i;
| desc<0
= replace_desc_numbers_by_descs (i+IF_INT_64_OR_32 8 4) s symbol_a;
= replace_desc_numbers_by_descs (i+IF_INT_64_OR_32 8 4) s symbol_a symbol_offset array_desc;
# desc = symbol_a.[desc-1];
# s=store_int_in_string s i desc;
# desc=desc+symbol_offset;
# s=store_int_in_string s i (desc - array_desc);
| desc bitand 2==0
# d = get_thunk_n_non_pointers desc;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a symbol_offset array_desc;
# (d,not_array) = get_descriptor_n_non_pointers_and_not_array desc;
| not_array
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 8 4)+(d<<(IF_INT_64_OR_32 3 2))) s symbol_a symbol_offset array_desc;
| d==0 // _STRING_
#! l = get_D_from_string s (i+IF_INT_64_OR_32 8 4);
# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4);
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 16 8)+l) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 16 8)+l) s symbol_a symbol_offset array_desc;
| d==1 // _ARRAY_
#! d = get_D_from_string s (i+IF_INT_64_OR_32 16 8);
| d==0
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)) s symbol_a symbol_offset array_desc;
# d = symbol_a.[d-1];
# s=store_int_in_string s (i+IF_INT_64_OR_32 16 8) d;
# d = d+symbol_offset;
# s=store_int_in_string s (i+IF_INT_64_OR_32 16 8) array_desc;
#! l = get_D_from_string s (i+IF_INT_64_OR_32 8 4);
| is_Int_D d
# l = l << IF_INT_64_OR_32 3 2;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc;
| is_Real_D d
# l = l << 3;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc;
| is_Bool_D d
# l = IF_INT_64_OR_32 ((l+7) bitand -8) ((l+3) bitand -4);
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc;
# arity = get_D_node_arity d;
| arity>=256
# record_a_arity = get_D_record_a_arity d;
# record_b_arity = arity-256-record_a_arity;
# l = (l * record_b_arity) << IF_INT_64_OR_32 3 2;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a;
= replace_desc_numbers_by_descs (i+(IF_INT_64_OR_32 24 12)+l) s symbol_a symbol_offset array_desc;
= abort (toString l+++" "+++toString d);
copy_from_string_with_names :: !*{#Char} !*{#DescInfo} !*{#String} !{#Symbol} -> (.a,!Int);
copy_from_string_with_names s desc_s_a mod_s_a symbols
# symbol_offset = if (is_using_desc_relative_to_array == 1) (((get_array_D {}) - (get_symbol_value "__ARRAY__" symbols)) - 2) 0;
# array_desc = if (is_using_desc_relative_to_array == 1) ((get_array_D {} - 2)) 0;
# symbol_a = lookup_symbol_values desc_s_a mod_s_a symbols;
# s = replace_desc_numbers_by_descs 0 s symbol_a;
# s = replace_desc_numbers_by_descs 0 s symbol_a symbol_offset array_desc;
= copy_from_string s;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment