Skip to content

Commit 65e786e

Browse files
committed
[TS] Ensure uint64_t arrays are 8-byte aligned by using an 8-byte len
1 parent 6e1119f commit 65e786e

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

typescript_strings.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,28 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
167167
168168
/* @internal */
169169
export function encodeUint8Array (inputArray: Uint8Array): number {
170-
const cArrayPointer = wasm.TS_malloc(inputArray.length + 4);
171-
const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
172-
arrayLengthView[0] = inputArray.length;
173-
const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
170+
const cArrayPointer = wasm.TS_malloc(inputArray.length + 8);
171+
const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
172+
arrayLengthView[0] = BigInt(inputArray.length);
173+
const arrayMemoryView = new Uint8Array(wasm.memory.buffer, cArrayPointer + 8, inputArray.length);
174174
arrayMemoryView.set(inputArray);
175175
return cArrayPointer;
176176
}
177177
/* @internal */
178178
export function encodeUint32Array (inputArray: Uint32Array|Array<number>): number {
179-
const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 4);
180-
const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer, inputArray.length);
181-
arrayMemoryView.set(inputArray, 1);
182-
arrayMemoryView[0] = inputArray.length;
179+
const cArrayPointer = wasm.TS_malloc((inputArray.length + 2) * 4);
180+
const arrayLengthView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
181+
arrayLengthView[0] = BigInt(inputArray.length);
182+
const arrayMemoryView = new Uint32Array(wasm.memory.buffer, cArrayPointer + 8, inputArray.length);
183+
arrayMemoryView.set(inputArray);
183184
return cArrayPointer;
184185
}
185186
/* @internal */
186187
export function encodeUint64Array (inputArray: BigUint64Array|Array<bigint>): number {
187-
const cArrayPointer = wasm.TS_malloc(inputArray.length * 8 + 1);
188-
const arrayLengthView = new Uint32Array(wasm.memory.buffer, cArrayPointer, 1);
189-
arrayLengthView[0] = inputArray.length;
190-
const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer + 4, inputArray.length);
191-
arrayMemoryView.set(inputArray);
188+
const cArrayPointer = wasm.TS_malloc((inputArray.length + 1) * 8);
189+
const arrayMemoryView = new BigUint64Array(wasm.memory.buffer, cArrayPointer, 1);
190+
arrayMemoryView.set(inputArray, 1);
191+
arrayMemoryView[0] = BigInt(inputArray.length);
192192
return cArrayPointer;
193193
}
194194
@@ -200,13 +200,15 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
200200
201201
/* @internal */
202202
export function getArrayLength(arrayPointer: number): number {
203-
const arraySizeViewer = new Uint32Array(wasm.memory.buffer, arrayPointer, 1);
204-
return arraySizeViewer[0];
203+
const arraySizeViewer = new BigUint64Array(wasm.memory.buffer, arrayPointer, 1);
204+
const len = arraySizeViewer[0];
205+
if (len >= (2n ** 32n)) throw new Error("Bogus Array Size");
206+
return Number(len % (2n ** 32n));
205207
}
206208
/* @internal */
207209
export function decodeUint8Array (arrayPointer: number, free = true): Uint8Array {
208210
const arraySize = getArrayLength(arrayPointer);
209-
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, arraySize);
211+
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, arraySize);
210212
// Clone the contents, TODO: In the future we should wrap the Viewer in a class that
211213
// will free the underlying memory when it becomes unreachable instead of copying here.
212214
// Note that doing so may have edge-case interactions with memory resizing (invalidating the buffer).
@@ -220,7 +222,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
220222
const arraySize = getArrayLength(arrayPointer);
221223
const actualArrayViewer = new Uint32Array(
222224
wasm.memory.buffer, // value
223-
arrayPointer + 4, // offset (ignoring length bytes)
225+
arrayPointer + 8, // offset (ignoring length bytes)
224226
arraySize // uint32 count
225227
);
226228
// Clone the contents, TODO: In the future we should wrap the Viewer in a class that
@@ -236,7 +238,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
236238
const arraySize = getArrayLength(arrayPointer);
237239
const actualArrayViewer = new BigUint64Array(
238240
wasm.memory.buffer, // value
239-
arrayPointer + 4, // offset (ignoring length bytes)
241+
arrayPointer + 8, // offset (ignoring length bytes)
240242
arraySize // uint32 count
241243
);
242244
// Clone the contents, TODO: In the future we should wrap the Viewer in a class that
@@ -253,13 +255,13 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
253255
254256
/* @internal */
255257
export function getU32ArrayElem(arrayPointer: number, idx: number): number {
256-
const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
258+
const actualArrayViewer = new Uint32Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
257259
return actualArrayViewer[idx];
258260
}
259261
260262
/* @internal */
261263
export function getU8ArrayElem(arrayPointer: number, idx: number): number {
262-
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 4, idx + 1);
264+
const actualArrayViewer = new Uint8Array(wasm.memory.buffer, arrayPointer + 8, idx + 1);
263265
return actualArrayViewer[idx];
264266
}
265267
@@ -273,7 +275,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
273275
/* @internal */
274276
export function decodeString(stringPointer: number, free = true): string {
275277
const arraySize = getArrayLength(stringPointer);
276-
const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 4, arraySize);
278+
const memoryView = new Uint8Array(wasm.memory.buffer, stringPointer + 8, arraySize);
277279
const result = new TextDecoder("utf-8").decode(memoryView);
278280
279281
if (free) {
@@ -579,12 +581,12 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
579581
580582
#define DECL_ARR_TYPE(ty, name) \\
581583
struct name##array { \\
582-
uint32_t arr_len; \\
584+
uint64_t arr_len; /* uint32_t would suffice but we want to align uint64_ts as well */ \\
583585
ty elems[]; \\
584586
}; \\
585587
typedef struct name##array * name##Array; \\
586588
static inline name##Array init_##name##Array(size_t arr_len, int lineno) { \\
587-
name##Array arr = (name##Array)do_MALLOC(arr_len * sizeof(ty) + sizeof(uint32_t), #name" array init", lineno); \\
589+
name##Array arr = (name##Array)do_MALLOC(arr_len * sizeof(ty) + sizeof(uint64_t), #name" array init", lineno); \\
588590
arr->arr_len = arr_len; \\
589591
return arr; \\
590592
}
@@ -679,8 +681,8 @@ def get_native_arr_elem(self, arr_name, idxc, ty_info):
679681
assert False # Only called if above is None
680682
def get_native_arr_ptr_call(self, ty_info):
681683
if ty_info.subty is not None:
682-
return "(" + ty_info.subty.c_ty + "*)(((uint8_t*)", ") + 4)"
683-
return "(" + ty_info.c_ty + "*)(((uint8_t*)", ") + 4)"
684+
return "(" + ty_info.subty.c_ty + "*)(((uint8_t*)", ") + 8)"
685+
return "(" + ty_info.c_ty + "*)(((uint8_t*)", ") + 8)"
684686
def get_native_arr_entry_call(self, ty_info, arr_name, idxc, entry_access):
685687
return None
686688
def cleanup_native_arr_ref_contents(self, arr_name, dest_name, arr_len, ty_info):

0 commit comments

Comments
 (0)