Branch: Tag:

2021-01-04

2021-01-04 01:05:53 by Marcus Comstedt <marcus@mc.pp.se>

Fix machine code on macOS 11

macOS 11 adds two JIT hardening features:

* Memory can only be mmapped as both PROT_WRITE and PROT_EXEC if MAP_JIT
is also specified.

* Memory mapped with MAP_JIT is never writeable and executable at the same
time. It is necessary to call a function before writing, and then again
after writing to make it executable again.

1847: Inside #if defined(PIKE_USE_MACHINE_CODE)
   * machine code. For decoding efficiency we also want a multi copy    * variant to be used by decode().    */ + #if !defined(HAVE_PTHREAD_JIT_WRITE_PROTECT_NP) && !defined(pthread_jit_write_protect_np) + #define phtread_jit_write_protect_np(enable) do{}while(0) + #endif   #define BAR(NUMTYPE,TYPE,ARGTYPE,NAME) \   void PIKE_CONCAT(low_add_to_,NAME) (struct program_state *state, \    TYPE ARG) { \
1865:    state->malloc_size_program->PIKE_CONCAT(num_,NAME)=m; \    state->new_program->NAME=tmp; \    } \ +  pthread_jit_write_protect_np(0); \    state->new_program-> \    NAME[state->new_program->PIKE_CONCAT(num_,NAME)++]=(ARG); \ -  +  pthread_jit_write_protect_np(1); \   } \   void PIKE_CONCAT(low_add_many_to_,NAME) (struct program_state *state, \    TYPE *ARG, NUMTYPE cnt) { \
1889:    state->malloc_size_program->PIKE_CONCAT(num_,NAME)=m; \    state->new_program->NAME=tmp; \    } \ +  pthread_jit_write_protect_np(0); \    memcpy(state->new_program->NAME + \    state->new_program->PIKE_CONCAT(num_,NAME), \    ARG, sizeof(TYPE) * cnt); \ -  +  pthread_jit_write_protect_np(1); \    state->new_program->PIKE_CONCAT(num_,NAME) += cnt; \   } \   void PIKE_CONCAT(add_to_,NAME) (ARGTYPE ARG) { \
5311:   PMOD_EXPORT void set_init_callback(void (*init)(struct object *))   {    add_compat_event_handler(); + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(0); + #endif    ((oldhandlertype *)Pike_compiler->new_program->program)[PROG_EVENT_INIT]=init; -  + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(1); + #endif   }      /**
5334:   PMOD_EXPORT void set_exit_callback(void (*exit)(struct object *))   {    add_compat_event_handler(); + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(0); + #endif    ((oldhandlertype *)Pike_compiler->new_program->program)[PROG_EVENT_EXIT]=exit; -  + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(1); + #endif    Pike_compiler->new_program->flags |= PROGRAM_LIVE_OBJ;   }   
5370:   PMOD_EXPORT void set_gc_recurse_callback(void (*m)(struct object *))   {    add_compat_event_handler(); + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(0); + #endif    ((oldhandlertype *)Pike_compiler->new_program->program)[PROG_EVENT_GC_RECURSE]=m; -  + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(1); + #endif   }      /**
5392:   PMOD_EXPORT void set_gc_check_callback(void (*m)(struct object *))   {    add_compat_event_handler(); + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(0); + #endif    ((oldhandlertype *)Pike_compiler->new_program->program)[PROG_EVENT_GC_CHECK]=m; -  + #ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP +  pthread_jit_write_protect_np(1); + #endif   }      /**