primitiveStore*IntoBytes) and
primitiveFFIIntegerAtPut do not test the receiver for immutability. A
typed store into a read-only ByteArray changes the bytes, and the VM
signals no ModificationForbidden.PrimErrNoModification for a read-only
receiver, as primitiveAtPut does. The JIT has machine code versions of
the 32-bit and 64-bit stores. The machine code now leaves without a store
when the receiver is read-only. Without that part, a store was blocked in
the interpreter and went through after the JIT compiled the method.WriteBarrierTest gives
33 passes of 33 with the image fixes of this repository. The stock VM
fails 4 of the 33: the two tests that upstream marked as expected failures
(testMutateByteArrayUsingDoubleAtPut, testMutateByteArrayUsingFloatAtPut)
and the two integer tests that this repository adds.TFFIBackend>>on:float64At:put: and on:float32At:put: test self
(the backend) for read-only, not anObject. The image fix is in
scripts/image-fixes/20-WriteBarrierTest.st.primitiveFFIIntegerAtPut wrote into a
read-only object). This repository has the fix for that defect."Run in a Playground of a Pharo 14 image." g := ByteArray new: 8. g beReadOnlyObject. [ g float64AtOffset: 0 put: 2.5 ] on: ModificationForbidden do: [ :e | e return: #forbidden ]. g printString. "Stock Cog: the store changes g to #[0 0 0 0 0 0 4 64]. With the change: ModificationForbidden, and g stays all zero."
The tests that show the defect, with the image fix entries:
How to apply an entries file: file in 00-header.st and the entries file as one chunk (the iospharo script scripts/gen-image-fixes.py makes that chunk). Each entry checks for the defect first and prints FIXED, ADDED or SKIP. Download 00-header.st.
"Write barrier for the typed integer stores into a ByteArray (2026-11-17). The typed store primitives 615-627 fail on a read-only ByteArray on our VM. The fallback of each accessor goes through ByteArray>>integerAt:put:size:signed: to the named primitive primitiveFFIIntegerAtPut. That primitive wrote the bytes of the read-only object on our VM (fixed in the VM on 2026-11-17: the primitive now fails). Stock Cog writes the bytes in the primitives 615-627 themselves (docs/upstream has the Cog change). W1. With the two VM changes the fallback ended in PrimitiveFailed. A store into a read-only object must signal ModificationForbidden, as at:put: does, so that a handler can make the object writable and retry. The retry selector is the index-based accessor of the same size and sign. W2, W3. WriteBarrierTest has tests for at:put:, doubleAt:put: and floatAt:put: on a read-only ByteArray, and none for an integer store. Two tests with the form of testMutateByteArrayUsingDoubleAtPut." fix value: #ByteArray value: #integerAt:put:size:signed: value: [ :source | (source includesSubstring: 'isReadOnlyObject') not ] value: 'integerAt: byteOffset put: value size: nBytes signed: aBoolean self isReadOnlyObject ifTrue: [ | selectors | selectors := aBoolean ifTrue: [ #( #signedByteAt:put: #signedShortAt:put: nil #signedLongAt:put: nil nil nil #signedLongLongAt:put: ) ] ifFalse: [ #( #unsignedByteAt:put: #unsignedShortAt:put: nil #unsignedLongAt:put: nil nil nil #unsignedLongLongAt:put: ) ]. ^ self modificationForbiddenFor: (selectors at: nBytes) index: byteOffset value: value ]. ^ FFIBackend current integerOfObject: self at: byteOffset put: value size: nBytes signed: aBoolean'. add value: #WriteBarrierTest value: #testMutateByteArrayUsingUnsignedLongAtPut value: 'tests - object' value: 'testMutateByteArrayUsingUnsignedLongAtPut | guineaPig | guineaPig := ByteArray new: 8. guineaPig beReadOnlyObject. self should: [ guineaPig unsignedLongAt: 1 put: 16r01020304 ] raise: ModificationForbidden. self assert: guineaPig equals: (ByteArray new: 8). [ guineaPig unsignedLongAt: 1 put: 16r01020304 ] on: ModificationForbidden do: [:modification | self assert: modification fieldIndex equals: 1. modification object beWritableObject. modification retryModification ]. self assert: (guineaPig unsignedLongAt: 1) equals: 16r01020304'. add value: #WriteBarrierTest value: #testMutateByteArrayUsingSignedShortAtPut value: 'tests - object' value: 'testMutateByteArrayUsingSignedShortAtPut | guineaPig | guineaPig := ByteArray new: 8. guineaPig beReadOnlyObject. self should: [ guineaPig signedShortAt: 3 put: -2 ] raise: ModificationForbidden. self assert: guineaPig equals: (ByteArray new: 8). [ guineaPig signedShortAt: 3 put: -2 ] on: ModificationForbidden do: [:modification | self assert: modification fieldIndex equals: 3. modification object beWritableObject. modification retryModification ]. self assert: (guineaPig signedShortAt: 3) equals: -2'.
fix value: #TFFIBackend value: #'on:float64At:put:' value: [ :src | src includesSubstring: 'self isReadOnlyObject' ] value: 'on: anObject float64At: offset put: value value isFloat ifFalse: [ ^ anObject float64AtOffset: offset - 1 put: value asFloat ]. anObject isReadOnlyObject ifTrue: [ ^ anObject modificationForbiddenFor: #doubleAt:put: index: offset value: value ]. ^ self primitiveFailed'. fix value: #TFFIBackend value: #'on:float32At:put:' value: [ :src | src includesSubstring: 'self isReadOnlyObject' ] value: 'on: anObject float32At: offset put: value value isFloat ifFalse: [ ^ anObject float32AtOffset: offset - 1 put: value asFloat ]. anObject isReadOnlyObject ifTrue: [ ^ anObject modificationForbiddenFor: #floatAt:put: index: offset value: value ]. ^ self primitiveFailed'. fix value: #WriteBarrierTest value: #testMutateByteArrayUsingDoubleAtPut value: [ :src | src includesSubstring: 'expectedFailure' ] value: 'testMutateByteArrayUsingDoubleAtPut | guineaPig | guineaPig := ByteArray new: 8. guineaPig beReadOnlyObject. self should: [ guineaPig doubleAt: 1 put: (2 raisedTo: 65) asFloat ] raise: ModificationForbidden. [ guineaPig doubleAt: 1 put: (2 raisedTo: 65) asFloat ] on: ModificationForbidden do: [:modification | self assert: modification fieldIndex equals: 1. modification object beWritableObject. modification retryModification ]. self assert: (guineaPig doubleAt: 1) equals: (2 raisedTo: 65) asFloat'. fix value: #WriteBarrierTest value: #testMutateByteArrayUsingFloatAtPut value: [ :src | src includesSubstring: 'expectedFailure' ] value: 'testMutateByteArrayUsingFloatAtPut | guineaPig | guineaPig := ByteArray new: 5. guineaPig beReadOnlyObject. self should: [ guineaPig floatAt: 1 put: 1.0 ] raise: ModificationForbidden. [ guineaPig floatAt: 1 put: 1.0 ] on: ModificationForbidden do: [:modification | self assert: modification fieldIndex equals: 1. modification object beWritableObject. modification retryModification ]. self assert: (guineaPig floatAt: 1) equals: 1.0'.
Names such as scripts/vmrun.sh and scripts/cogdiff/ are scripts of the iospharo repository, which runs one VM at a time. cog is stock Cog, cogp is Cog with the change, ours is iospharo.
A change of the Cog sources (pharo-project/pharo-vm, branch pharo-12), on the local branch fix/write-barrier-typed-stores. State: committed on the branch, tested. Not sent upstream.
From 03d50c9754c3be0145864b0f815c0d1af15646e0 Mon Sep 17 00:00:00 2001
From: Aaron Wohl
Date: Tue, 17 Nov 2026 07:39:09 -0500
Subject: [PATCH] Typed store primitives respect the write barrier
The primitives that store a typed value into the bytes of an object
(primitiveStore*IntoBytes, 615 to 629) and primitiveFFIIntegerAtPut did not
test the receiver for immutability. A store into a read-only ByteArray
changed the bytes, and no ModificationForbidden was signalled:
g := ByteArray new: 8. g beReadOnlyObject.
g float64AtOffset: 0 put: 2.5. "g is now #[0 0 0 0 0 0 4 64]"
g uint32AtOffset: 0 put: 7. "g is now #[7 0 0 0 ...]"
Each C primitive now fails with PrimErrNoModification for a read-only
receiver, as primitiveAtPut does. primitiveFFIIntegerAtPut does the test
only for a store into the bytes of the receiver itself; a store through an
ExternalAddress goes to outside memory.
The JIT has machine code versions of these primitives
(genPrimitiveStore:intoBytesWith:needsToTrap:). The machine code now leaves
without a store when the receiver is read-only, so the C primitive or the
fallback code of the method handles the case. Without this part a store
was blocked in the interpreter and went through after the method was
compiled.
The image has two tests for this with the expected-failure mark
(WriteBarrierTest>>testMutateByteArrayUsingDoubleAtPut and
testMutateByteArrayUsingFloatAtPut). With this change, and with the image
methods TFFIBackend>>on:float64At:put: and on:float32At:put: testing
anObject in place of self, the two tests pass.
Measured on macOS 27, arm64: after the change the two stores above leave g
all zero, also through unsignedLongAt:put: from compiled code.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
.../CogObjectRepresentationForSpur.class.st | 15 ++++++++++---
.../StackInterpreterPrimitives.class.st | 21 +++++++++++++++++++
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/smalltalksrc/VMMaker/CogObjectRepresentationForSpur.class.st b/smalltalksrc/VMMaker/CogObjectRepresentationForSpur.class.st
index 2346db982..2473cbe66 100644
--- a/smalltalksrc/VMMaker/CogObjectRepresentationForSpur.class.st
+++ b/smalltalksrc/VMMaker/CogObjectRepresentationForSpur.class.st
@@ -1761,14 +1761,20 @@ CogObjectRepresentationForSpur >> genPrimitiveStore: accessedElementByteSize int
{ #category : 'primitive generators' }
CogObjectRepresentationForSpur >> genPrimitiveStore: accessedElementByteSize intoBytesWith: aBlock needsToTrap: needsToTrap [
- | nSlotsOrBytesReg jumpBadIndex formatReg jumpIsPointers jumpOutOfBounds jumpSmaller jumpImmediate destPointerRegister |
+ | nSlotsOrBytesReg jumpBadIndex formatReg jumpIsPointers jumpOutOfBounds jumpSmaller jumpImmediate jumpImmutable destPointerRegister |
+ <var: #jumpImmutable type: #'AbstractInstruction *'>
nSlotsOrBytesReg := ClassReg.
destPointerRegister := ClassReg.
cogit genLoadArgAtDepth: 1 into: Arg0Reg.
cogit genLoadArgAtDepth: 0 into: Arg1Reg.
-
+
jumpImmediate := self genJumpImmediate: ReceiverResultReg.
+ "A read-only receiver leaves the machine code version without a store. The C primitive (64-bit values)
+ or the fallback code of the method then handles the read-only case."
+ self
+ cppIf: IMMUTABILITY
+ ifTrue: [jumpImmutable := self genJumpImmutable: ReceiverResultReg scratchReg: TempReg].
jumpBadIndex := self genJumpNotSmallInteger: Arg0Reg scratchReg: TempReg.
self genConvertSmallIntegerToIntegerInReg: Arg0Reg.
@@ -1804,8 +1810,11 @@ CogObjectRepresentationForSpur >> genPrimitiveStore: accessedElementByteSize int
"Restore the Arg0 into an oop."
self genConvertIntegerToSmallIntegerInReg: Arg0Reg.
-
+
jumpBadIndex jmpTarget: (jumpImmediate jmpTarget: cogit Label).
+ self
+ cppIf: IMMUTABILITY
+ ifTrue: [jumpImmutable jmpTarget: jumpImmediate getJmpTarget].
^ needsToTrap ifTrue: [0] ifFalse: [CompletePrimitive]
]
diff --git a/smalltalksrc/VMMaker/StackInterpreterPrimitives.class.st b/smalltalksrc/VMMaker/StackInterpreterPrimitives.class.st
index 3e19d3e0a..90a5442b4 100644
--- a/smalltalksrc/VMMaker/StackInterpreterPrimitives.class.st
+++ b/smalltalksrc/VMMaker/StackInterpreterPrimitives.class.st
@@ -1334,6 +1334,12 @@ StackInterpreterPrimitives >> primitiveFFIIntegerAtPut [
addr := self maybeExternalAddressOf: rcvr startingAt: byteOffset size: byteSize.
addr = 0 ifTrue: [^self primitiveFailFor: PrimErrBadArgument].
+
+ "A store into the bytes of the receiver itself must respect the write barrier.
+ A store through an ExternalAddress goes to outside memory and is not a store into the receiver."
+ ((objectMemory fetchClassOf: rcvr) ~= objectMemory classExternalAddress
+ and: [objectMemory isObjImmutable: rcvr])
+ ifTrue: [^self primitiveFailFor: PrimErrNoModification].
value := isSigned
ifTrue:[objectMemory signed64BitValueOf: valueOop]
@@ -2910,6 +2916,7 @@ StackInterpreterPrimitives >> primitiveStoreBoolean8IntoBytes [
index := objectMemory integerValueOf: index.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
valueObject = objectMemory trueObject ifTrue: [
value := 1
@@ -2972,6 +2979,7 @@ StackInterpreterPrimitives >> primitiveStoreChar16IntoBytes [
value := objectMemory characterValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 1) or: [byteSize < 2]]) ifTrue:
@@ -3020,6 +3028,7 @@ StackInterpreterPrimitives >> primitiveStoreChar32IntoBytes [
value := self characterValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 3) or: [byteSize < 4]]) ifTrue:
@@ -3068,6 +3077,7 @@ StackInterpreterPrimitives >> primitiveStoreChar8IntoBytes [
value := objectMemory characterValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= byteSize]) ifTrue:
@@ -3117,6 +3127,7 @@ StackInterpreterPrimitives >> primitiveStoreFloat32IntoBytes [
value := objectMemory floatValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 3) or: [byteSize < 4]]) ifTrue:
@@ -3167,6 +3178,7 @@ StackInterpreterPrimitives >> primitiveStoreFloat64IntoBytes [
value := objectMemory floatValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 7) or: [byteSize < 8]]) ifTrue:
@@ -3216,6 +3228,7 @@ StackInterpreterPrimitives >> primitiveStoreInt16IntoBytes [
value := objectMemory integerValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 1) or: [byteSize < 2]]) ifTrue:
@@ -3266,6 +3279,7 @@ StackInterpreterPrimitives >> primitiveStoreInt32IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 3) or: [byteSize < 4]]) ifTrue:
@@ -3318,6 +3332,7 @@ StackInterpreterPrimitives >> primitiveStoreInt64IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 7) or: [byteSize < 8]]) ifTrue:
@@ -3368,6 +3383,7 @@ StackInterpreterPrimitives >> primitiveStoreInt8IntoBytes [
value := objectMemory integerValueOf: valueObject.
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= byteSize]) ifTrue:
@@ -3418,6 +3434,7 @@ StackInterpreterPrimitives >> primitiveStorePointerIntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - (BytesPerWord - 1)) or: [byteSize < BytesPerWord]]) ifTrue:
@@ -3505,6 +3522,7 @@ StackInterpreterPrimitives >> primitiveStoreUInt16IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 1) or: [byteSize < 2]]) ifTrue:
@@ -3557,6 +3575,7 @@ StackInterpreterPrimitives >> primitiveStoreUInt32IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 3) or: [byteSize < 4]]) ifTrue:
@@ -3609,6 +3628,7 @@ StackInterpreterPrimitives >> primitiveStoreUInt64IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= (byteSize - 7) or: [byteSize < 8]]) ifTrue:
@@ -3661,6 +3681,7 @@ StackInterpreterPrimitives >> primitiveStoreUInt8IntoBytes [
bytes := objectMemory firstBytePointerOfDataObject: rcvr.
bytes == 0 ifTrue: [ ^ self primitiveFailFor: PrimErrBadArgument ].
+ (objectMemory isObjImmutable: rcvr) ifTrue: [ ^ self primitiveFailFor: PrimErrNoModification ].
byteSize := objectMemory numBytesOf: rcvr.
(index < 0 or: [index >= byteSize]) ifTrue:
--
2.54.0 (Apple Git-157)
State of 2026-11-20. Made from the test records of the iospharo project (a second Pharo VM). The fixes are proposals for review.