[squeak-dev] FFI: FFI-Callbacks-mt.30.mcz

tim Rowledge tim at rowledge.org
Tue Apr 19 16:48:37 UTC 2022


I blame the StaticTypingMafia.

A long time ago (mid/late 90's) ARM were seriously considering making it effectively impossible to do any kind of dynamic code generation, because after all, all real languages relied on static compilation. Since I was doing a little consulting with them at the time I heard about this and banged very hard on the drum. Eventually they noticed java getting headlines and that probably swung the vote.

> On 2022-04-19, at 2:36 AM, Marcel Taeumel <marcel.taeumel at hpi.de> wrote:
> 
> Writing into executable memory becomes harder and harder these days ;o)
>> Am 19.04.2022 11:25:40 schrieb commits at source.squeak.org <commits at source.squeak.org>:
>> 
>> Marcel Taeumel uploaded a new version of FFI-Callbacks to project FFI:
>> http://source.squeak.org/FFI/FFI-Callbacks-mt.30.mcz
>> 
>> ==================== Summary ====================
>> 
>> Name: FFI-Callbacks-mt.30
>> Author: mt
>> Time: 19 April 2022, 11:25:30.789004 am
>> UUID: 0278f498-8447-4e48-84db-9c5bf5cae4d3
>> Ancestors: FFI-Callbacks-mt.29
>> 
>> Fixes callbacks "Bus error" on (macOS) ARM64 platforms.
>> 
>> =============== Diff against FFI-Callbacks-mt.29 ===============
>> 
>> Item was changed:
>> ----- Method: FFICallback class>>allocateExecutableBlock (in category 'executable pages') -----
>> allocateExecutableBlock
>> 
>> + | blockSize newBlock |
>> - | blockSize |
>> blockSize := MaxThunkSize.
>> - ExecutablePagesAccessProtect critical:
>> - [ExecutablePages do:
>> - [:page |
>> - 1 to: page size - blockSize by: blockSize do:
>> - [:i|
>> - (page at: i) = 0 ifTrue:
>> - [page at: i put: 1.
>> - ^ page from: i to: i + blockSize - 1]]]].
>> ExecutablePagesAccessProtect critical: [
>> + ExecutablePages do: [:page |
>> + 1 to: page size - blockSize by: blockSize do: [:i |
>> + (page at: i) = 0 ifTrue: [
>> + newBlock := page from: i to: i + blockSize - 1.
>> + self markExecutableBlock: newBlock.
>> + ^ newBlock]]]].
>> + ExecutablePagesAccessProtect critical: [
>> | newPage |
>> newPage := ExecutablePages add: self allocateExecutablePage.
>> + newBlock := newPage from: 1 to: blockSize.
>> + self markExecutableBlock: newBlock.
>> + ^ newBlock]!
>> - ^ (newPage from: 1 to: blockSize)
>> - at: 1 put: 1; "Mark as allocated."
>> - yourself]!
>> 
>> Item was changed:
>> ----- Method: FFICallback class>>allocateExternal (in category 'instance creation') -----
>> allocateExternal
>> "Overwritten for custom management of executable pages. Assure compatibility with Alien."
>> 
>> | instance |
>> + instance := self fromHandle: self allocateExecutableBlock getHandle.
>> - instance := self fromHandle: self allocateExecutableBlock getHandle.
>> AlienStub isAlienLoaded ifTrue: [instance addToAlienThunkTable].
>> ^ instance!
>> 
>> Item was added:
>> + ----- Method: FFICallback class>>markExecutableBlock: (in category 'executable pages') -----
>> + markExecutableBlock: externalData
>> + "Mark the block of executable memory that is represented by externalData as being used. We do this to allow several subsequent calls to #allocateExecutableBlock without actually writing a thunk in it. NOTE THAT we must use a special primitive to write into executable memory on ARM64 platforms."
>> + 
>> + FFIPlatformDescription current abi == #ARM64
>> + ifTrue: [self writeExecutableBlockAt: externalData getHandle bytes: #[1]]
>> + ifFalse: [externalData at: 1 put: 1].!
>> 
>> Item was added:
>> + ----- Method: FFICallback class>>zeroExecutableBlock: (in category 'executable pages') -----
>> + zeroExecutableBlock: externalObject "< ExternalData | FFICallback>"
>> + "Variation of #zeroMemory to handle writing into executable memory on ARM64 platforms."
>> + 
>> + FFIPlatformDescription current abi == #ARM64
>> + ifTrue: [self writeExecutableBlockAt: externalObject getHandle bytes: (ByteArray new: externalObject byteSize)]
>> + ifFalse: [externalObject getHandle zeroMemory: externalObject byteSize].!
>> 
>> Item was changed:
>> ----- Method: FFICallback>>zeroMemory (in category 'initialize-release') -----
>> zeroMemory
>> 
>> ExecutablePagesAccessProtect critical: [
>> + self class zeroExecutableBlock: self].!
>> - super zeroMemory].!
>> 
>> 
> 


tim
--
tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
"!" The strange little noise you make when you can't scream...




More information about the Squeak-dev mailing list