diff --git a/interop/src/arkts/DeserializerBase.ts b/interop/src/arkts/DeserializerBase.ts index 2f6d36a368ca28ca26490d35c2613e03422454da..be65a57e8821bcf7f0f04dba32b2edd06c8d506d 100644 --- a/interop/src/arkts/DeserializerBase.ts +++ b/interop/src/arkts/DeserializerBase.ts @@ -80,7 +80,11 @@ export class DeserializerBase implements Disposable { final readInt8(): int32 { const pos = this._position - const newPos = pos + 1 + + // align for 4 for arm32 + const lenAlignInt8 = 4; + + const newPos = pos + lenAlignInt8 if (newPos > this._end) { throw new Error(`value size(1) is less than remaining buffer length`) @@ -154,7 +158,11 @@ export class DeserializerBase implements Disposable { final readBoolean(): boolean { const pos = this._position - const newPos = pos + 1 + + // align for 4 for arm32 + const lenAlignInt8 = 4; + + const newPos = pos + lenAlignInt8 if (newPos > this._end) { throw new Error(`value size(1) is less than remaining buffer length`) @@ -180,7 +188,10 @@ export class DeserializerBase implements Disposable { final readString(): string { const encodedLength = this.readInt32(); const pos = this._position - const newPos = pos + encodedLength + let newPos = pos + encodedLength + if (newPos % 4 != 0) { + newPos += 4 - newPos % 4 + } if (newPos > this._end) { throw new Error(`value size(${encodedLength}) is less than remaining buffer length`) diff --git a/interop/src/arkts/SerializerBase.ts b/interop/src/arkts/SerializerBase.ts index 033a2bf48a2c53fe96a09fec8015fdbc01c60a05..90be35e9f658226a222f1728d3d7b4e985c83896 100644 --- a/interop/src/arkts/SerializerBase.ts +++ b/interop/src/arkts/SerializerBase.ts @@ -323,11 +323,15 @@ export class SerializerBase implements Disposable { final writeInt8(value: int32) { let pos = this._position - let newPos = pos + 1 + + // align for 4 for arm32 + const lenAlignInt8 = 4; + + let newPos = pos + lenAlignInt8 if (newPos > this._last) { - this.updateCapacity(1) + this.updateCapacity(lenAlignInt8) pos = this._position - newPos = pos + 1 + newPos = pos + lenAlignInt8 } unsafeMemory.writeInt8(pos, value.toByte()) @@ -383,11 +387,14 @@ export class SerializerBase implements Disposable { } final writeBoolean(value: boolean | undefined) { let pos = this._position - let newPos = pos + 1 + // align for 4 for arm32 + const lenAlignInt8 = 4; + + let newPos = pos + lenAlignInt8 if (newPos > this._last) { - this.updateCapacity(1) + this.updateCapacity(lenAlignInt8) pos = this._position - newPos = pos + 1 + newPos = pos + lenAlignInt8 } this._position = newPos @@ -399,11 +406,17 @@ export class SerializerBase implements Disposable { unsafeMemory.writeInt8(pos, VALUE_FALSE.toByte()); } final writeString(value: string) { - const encodedLength = unsafeMemory.getStringSizeInBytes(value) - + let encodedLength = unsafeMemory.getStringSizeInBytes(value) let pos = this._position - if (pos + encodedLength + 5 > this._last) { - this.updateCapacity(encodedLength + 5) + + let posOffset = encodedLength + 4 + 1 + + if (posOffset % 4 != 0) { + posOffset += 4 - posOffset % 4 + } + + if (pos + posOffset > this._last) { + this.updateCapacity(posOffset) pos = this._position } @@ -413,7 +426,7 @@ export class SerializerBase implements Disposable { // need check native part fot utf16 cases and probably change this solution. unsafeMemory.writeInt8(pos + encodedLength + 4, 0) unsafeMemory.writeInt32(pos, encodedLength + 1) - this._position = pos + encodedLength + 4 + 1 + this._position = pos + posOffset } final writeBuffer(value: NativeBuffer) { this.holdAndWriteObject(value) diff --git a/interop/src/cpp/DeserializerBase.h b/interop/src/cpp/DeserializerBase.h index 105036dfdb4bd1e634294a4d5f80ad0f0f97cbcb..a4cc799e7fd5acf3838806aaf301a36103957969 100644 --- a/interop/src/cpp/DeserializerBase.h +++ b/interop/src/cpp/DeserializerBase.h @@ -377,9 +377,10 @@ public: int8_t readInt8() { - check(1); + // align for arm32 + check(4); int8_t value = *(data + position); - position += 1; + position += 4; return value; } InteropTag readTag() @@ -388,9 +389,10 @@ public: } InteropBoolean readBoolean() { - check(1); + // align for arm32 + check(4); int8_t value = *(data + position); - position += 1; + position += 4; return value; } InteropInt32 readInt32() @@ -501,11 +503,18 @@ public: { InteropString result; InteropInt32 length = readInt32(); - check(length); + + int offset = length; + // align for arm32 + if (offset % 4) { + offset += 4 - offset % 4; + } + + check(offset); // We refer to string data in-place. result.chars = (const char *)(data + position); result.length = length - 1; - this->position += length; + this->position += offset; return result; } diff --git a/interop/src/cpp/SerializerBase.h b/interop/src/cpp/SerializerBase.h index c501eb05e7cd353d9b3cb48cdc8a6418563ed4a0..2e5f559f3a5ed8a78569b0ec58c8192c0a401698 100644 --- a/interop/src/cpp/SerializerBase.h +++ b/interop/src/cpp/SerializerBase.h @@ -130,9 +130,10 @@ public: } void writeInt8(InteropInt8 value) { - check(1); + check(4); *((InteropInt8*)(data + position)) = value; - position += 1; + // 4 for align + position += 4; } void writeInt32(InteropInt32 value) { @@ -209,9 +210,16 @@ public: void writeString(InteropString value) { writeInt32(value.length + 1); - check(value.length + 1); + + int length = value.length + 1; + // align for arm32 + if (length % 4) { + length += 4 - length % 4; + } + + check(length); interop_strcpy((char*)(data + position), dataLength, value.chars); - position += value.length + 1; + position += length; } void writeBoolean(InteropBoolean value) {