bugMIT/GNU Scheme - Bugs: bug #60267, Possible method of resolving the...

 
 

bug #60267: Possible method of resolving the Apple M-1 W^X issue

Submitter:  John Cowan <johnwcowan>
Submitted:  Mon 22 Mar 2021 03:03:27 AM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  None Privacy:  Public
Assigned to:  None Originator Name: 
Open/Closed:  Open
Keywords: 
* Mandatory Fields

Add a New Comment Rich Markup
   

Mon 06 Nov 2023 02:19:45 PM UTC, comment #3: 

Yes, the nature of the issue is well-understood; the problem is a lack of effort to implement it.  See https://lists.gnu.org/archive/html/mit-scheme-devel/2022-12/msg00001.html for the partial effort I started.  Personally I don't have any systems that require this anyway so it hasn't been a priority for me.

Taylor R. Campbell <riastradh>
Group Member
Mon 06 Nov 2023 02:09:34 PM UTC, comment #2: 

Another possible approach is to use a memory region that has two mappings at different addresses: one with W and one with X. GNU libffcall uses this, see https://git.savannah.gnu.org/gitweb/?p=libffcall.git;a=blob;f=trampoline/trampoline.c;h=fdcbcd456aeb20b87b846a8d9f930ce2781a1195;hb=HEAD (search for EXECUTABLE_VIA_MMAP_FILE_SHARED), and it works even on HardenedBSD and macOS.

The drawbacks are:
- This memory is not anonymous; it must be taken from a file.
- Some address management is needed, because in some places you want to address the memory through the W mapping and in other places through the X mapping.

Bruno Haible <haible>
Sat 04 Jun 2022 09:16:00 PM UTC, comment #1: 

We would certainly have to use this approach, but it's not enough because at any given time you can only toggle between W xor X -- you can't have both set at the same time.

Currently compiled code (executable data) and mutable objects (writable data) are both stored in the same heap.  What we need to do is have a separate heap for compiled code, and whenever we make (or load) compiled code:

1. Toggle the compiled-code heap from executable to writable.
2. Run a GC and move any new compiled-code blocks from the object heap where they were created into the compiled-code heap (GC is needed both to free up unreferenced compiled-code blocks and ensure pointers in the object heap to new compiled-code blocks are relocated to point at the compiled-code heap instead).
3. Toggle the compiled-code heap back from writable to executable.

Other systems like NetBSD will use a slightly different approach: create two virtual address windows into the same anonymous mapping, one mapped executable and the other mapped writable (and maybe only created while the GC is happening).

It's doable, just a pain to deal with all the bookkeeping, which is why I haven't bothered.  (I started drafting some logic to do it back in 2020 but I can't find it now.  Not sure I got far enough to bother looking instead of just starting over, though.)

Taylor R. Campbell <riastradh>
Group Member
Mon 22 Mar 2021 03:03:27 AM UTC, original submission:  

Apple has posted a method for supporting JITs on Apple M-1 machines.  Basically one must allocate a segment with mmap using the MAP_JIT option.  Then the function pthread_jit_write_protect_np must be called with a boolean argument:  false turns off write-protection and enables execution, and true turns turns on write-protection and disables execution.

I hope this is helpful.

John Cowan <johnwcowan>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by haible (Posted a comment)
  • -email is unavailable- added by riastradh (Posted a comment)
  • -email is unavailable- added by johnwcowan (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-5884.
    Corresponding source code