forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshadowstack.ts
685 lines (644 loc) · 22.2 KB
/
shadowstack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/**
* @fileoverview Shadow stack instrumentation for a precise GC.
*
* Instruments function arguments and local assignments marked with a 'tostack'
* call to also do stores to a shadow stack of managed values only.
*
* Consider a simple call to a function looking like the following, taking
* managed arguments, plus assigning managed values to locals:
*
* function foo(a: Obj, b: Obj): Obj {
* var c = __tostack(a) // slot 2
* __collect()
* return b
* }
*
* foo(__tostack(a), __tostack(b)) // slot 0, 1
*
* At the call to `__collect()` the 32-bit stack frame of the function is:
*
* Offset | Value stored
* -------|----------------------------
* 0 | First managed argument 'a'
* 4 | Second managed argument 'b'
* -------|----------------------------
* 8 | First managed local 'c'
*
* We are splitting the frame in two halves as annotated since both halves are
* only known separately for indirect calls, with the first half becoming an
* extension of the calling function's stack frame by means of treating the
* arguments as if these were locals beyond the caller's `numLocals`. Function
* arguments stay a bit longer on the stack than usually, but we also don't have
* to modify the stack pointer pre-call at all this way. The caller's amended
* stack frame when assuming one managed local may look like this:
*
* Offset | Value stored
* -------|----------------------------
* 0 | First managed local '?'
* 4 | Extended with first managed argument 'a'
* 8 | Extended with second managed argument 'b'
*
* with the callee's stack frame becoming just:
*
* Offset | Value stored
* -------|----------------------------
* 0 | First managed local 'c'
*
* Instrumentation added below looks about like the following, with the stack
* growing downwards and 't' and 'r' being new temporary locals:
*
* // callee frameSize = 1 * sizeof<usize>()
* function foo(a: usize, b: usize): usize {
* memory.fill(__stack_pointer -= frameSize, 0, frameSize)
* store<usize>(__stack_pointer, c = a, 0 * sizeof<usize>())
* __collect()
* var r = b
* __stack_pointer += frameSize
* return r
* }
*
* // caller frameSize = (numLocalSlots + 2 [by extension]) * sizeof<usize>()
* (
* r = foo(
* ( t = a,
* store<usize>(__stack_pointer, t, (numLocalSlots + 0) * sizeof<usize>()),
* t ),
* ( t = b,
* store<usize>(__stack_pointer, t, (numLocalSlots + 1) * sizeof<usize>()),
* t )
* ),
* r
* )
*
* Also note that we have to `memory.fill` the second half because the first
* assignment to a local may happen at a later point within the function. The
* invariant we need to maintain for a precise GC is that it only sees zeroes
* or valid pointers, but never an invalid pointer left on the stack earlier.
* Since most frames are small, we unroll a sequence of `store`s up to a frame
* size of 16 bytes, and `memory.fill`, if available, beyond.
*
* @license Apache-2.0
*/
import {
Pass
} from "./pass";
import {
_BinaryenAddFunction,
_BinaryenAddFunctionExport,
_BinaryenCallGetNumOperands,
_BinaryenCallGetOperandAt,
_BinaryenCallGetTarget,
_BinaryenCallIndirectGetNumOperands,
_BinaryenCallIndirectGetOperandAt,
_BinaryenCallIndirectSetOperandAt,
_BinaryenCallSetOperandAt,
_BinaryenExportGetKind,
_BinaryenExportGetName,
_BinaryenExportGetValue,
_BinaryenExpressionGetId,
_BinaryenExpressionGetType,
_BinaryenFunctionGetBody,
_BinaryenFunctionGetName,
_BinaryenFunctionGetNumLocals,
_BinaryenFunctionGetNumVars,
_BinaryenFunctionGetParams,
_BinaryenFunctionGetResults,
_BinaryenFunctionGetVar,
_BinaryenFunctionSetBody,
_BinaryenGetExport,
_BinaryenGetFunction,
_BinaryenLocalSetGetIndex,
_BinaryenLocalSetGetValue,
_BinaryenLocalSetIsTee,
_BinaryenLocalSetSetValue,
_BinaryenRemoveExport,
_BinaryenRemoveFunction,
_BinaryenReturnGetValue,
_BinaryenReturnSetValue,
_free
} from "../glue/binaryen";
import {
ExpressionId,
ExpressionRef,
FunctionRef,
Index,
BinaryOp,
TypeRef,
allocPtrArray,
Module,
ExternalKind,
ExportRef,
expandType,
isConstZero
} from "../module";
import {
Compiler,
Options
} from "../compiler";
import {
Feature
} from "../common";
import {
BuiltinNames
} from "../builtins";
type LocalIndex = Index;
type SlotIndex = Index;
type SlotMap = Map<LocalIndex,SlotIndex>;
type TempMap = Map<TypeRef,LocalIndex>;
/** Attempts to match the `__tostack(value)` pattern. Returns `value` if a match, otherwise `0`. */
function matchPattern(module: Module, expr: ExpressionRef): ExpressionRef {
if (
_BinaryenExpressionGetId(expr) == ExpressionId.Call &&
module.readStringCached(_BinaryenCallGetTarget(expr)) == BuiltinNames.tostack
) {
assert(_BinaryenCallGetNumOperands(expr) == 1);
return _BinaryenCallGetOperandAt(expr, 0);
}
return 0;
}
/** Tests whether a `value` matched by `matchTostack` needs a slot. */
function needsSlot(module: Module, value: ExpressionRef): bool {
switch (_BinaryenExpressionGetId(value)) {
// no need to stack null pointers
case ExpressionId.Const: return !isConstZero(value);
// already kept in another slot
case ExpressionId.LocalGet:
case ExpressionId.LocalSet: return false; // tee
}
return true;
}
/** Instruments a module with a shadow stack for precise GC. */
export class ShadowStackPass extends Pass {
/** Stack frame slots, per function. */
slotMaps: Map<FunctionRef, SlotMap> = new Map();
/** Temporary locals, per function. */
tempMaps: Map<FunctionRef, TempMap> = new Map();
/** Exports (with managed operands) map. */
exportMap: Map<string,i32[]> = new Map();
/** Compiler reference. */
compiler: Compiler;
constructor(compiler: Compiler) {
super(compiler.module);
this.compiler = compiler;
}
/** Compiler options. */
get options(): Options { return this.compiler.options; }
/** Target pointer type. */
get ptrType(): TypeRef { return this.options.sizeTypeRef; }
/** Target pointer size. */
get ptrSize(): i32 { return this.ptrType == TypeRef.I64 ? 8 : 4; }
/** Target pointer addition operation. */
get ptrBinaryAdd(): BinaryOp { return this.ptrType == TypeRef.I64 ? BinaryOp.AddI64 : BinaryOp.AddI32; }
/** Target pointer subtraction operation. */
get ptrBinarySub(): BinaryOp { return this.ptrType == TypeRef.I64 ? BinaryOp.SubI64 : BinaryOp.SubI32; }
/** Gets a constant with the specified value of the target pointer type. */
ptrConst(value: i32): ExpressionRef {
return this.ptrType == TypeRef.I64
? this.module.i64(value)
: this.module.i32(value);
}
/** Notes the presence of a slot for the specified (imaginary) local, returning the slot index. */
noteSlot(func: FunctionRef, localIndex: Index): i32 {
let slotMap: SlotMap;
if (this.slotMaps.has(func)) {
slotMap = changetype<SlotMap>(this.slotMaps.get(func));
if (slotMap.has(localIndex)) {
return changetype<i32>(slotMap.get(localIndex));
}
} else {
slotMap = new Map();
this.slotMaps.set(func, slotMap);
}
let slotIndex = slotMap.size;
slotMap.set(localIndex, slotIndex);
return slotIndex;
}
/** Notes the presence of an exported function taking managed operands. */
noteExport(name: string, managedOperandIndices: i32[]): void {
if (!managedOperandIndices.length) return;
this.exportMap.set(name, managedOperandIndices);
}
/** Gets a shared temporary local of the given type in the specified functions. */
getSharedTemp(func: FunctionRef, type: TypeRef): Index {
let tempMap: TempMap;
if (this.tempMaps.has(func)) {
tempMap = changetype<TempMap>(this.tempMaps.get(func));
if (tempMap.has(type)) {
return changetype<Index>(tempMap.get(type));
}
} else {
tempMap = new Map();
this.tempMaps.set(func, tempMap);
}
let numLocals = _BinaryenFunctionGetNumLocals(func);
let localIndex = numLocals + tempMap.size;
tempMap.set(type, localIndex);
return localIndex;
}
/** Makes an expression modifying the stack pointer by the given offset. */
makeStackOffset(offset: i32): ExpressionRef {
assert(offset != 0);
var module = this.module;
var expr = module.global_set(BuiltinNames.stack_pointer,
module.binary(offset >= 0 ? this.ptrBinaryAdd : this.ptrBinarySub,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
this.ptrConst(abs(offset))
)
);
if (offset > 0) return expr;
return module.block(null, [
expr,
this.makeStackCheck()
], TypeRef.None);
}
/** Makes a sequence of expressions zeroing the stack frame. */
makeStackFill(frameSize: i32, stmts: ExpressionRef[]): void {
assert(frameSize > 0);
var module = this.module;
if (this.options.hasFeature(Feature.BULK_MEMORY) && frameSize > 16) {
stmts.push(
module.memory_fill(
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.i32(0), // TODO: Wasm64 also i32?
this.ptrConst(frameSize)
)
);
} else {
let remain = frameSize;
while (remain >= 8) {
// store<i64>(__stack_pointer, 0, frameSize - remain)
stmts.push(
module.store(8,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.i64(0),
TypeRef.I64,
frameSize - remain
)
);
remain -= 8;
}
if (remain) {
assert(remain == 4);
// store<i32>(__stack_pointer, 0, frameSize - remain)
stmts.push(
module.store(4,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.i32(0),
TypeRef.I32,
frameSize - remain
)
);
}
}
}
private hasStackCheckFunction: bool = false;
/** Makes a check that the current stack pointer is valid. */
makeStackCheck(): ExpressionRef {
var module = this.module;
if (!this.hasStackCheckFunction) {
this.hasStackCheckFunction = true;
module.addFunction("~stack_check", TypeRef.None, TypeRef.None, null,
module.if(
module.binary(BinaryOp.LtI32,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.global_get(BuiltinNames.data_end, this.ptrType)
),
this.compiler.makeStaticAbort(
this.compiler.ensureStaticString("stack overflow"),
this.compiler.program.nativeSource
)
)
);
}
return module.call("~stack_check", null, TypeRef.None);
}
private updateCallOperands(operands: ExpressionRef[]): i32 {
var module = this.module;
var numSlots = 0;
for (let i = 0, k = operands.length; i < k; ++i) {
let operand = operands[i];
let match = matchPattern(module, operand);
if (!match) continue;
if (!needsSlot(module, match)) {
operands[i] = match;
continue;
}
let currentFunction = this.currentFunction;
let numLocals = _BinaryenFunctionGetNumLocals(currentFunction);
let slotIndex = this.noteSlot(currentFunction, numLocals + this.callSlotOffset + i);
let temp = this.getSharedTemp(currentFunction, this.ptrType);
let stmts = new Array<ExpressionRef>();
// t = value
stmts.push(
module.local_set(temp, match, false)
);
// store<usize>(__stack_pointer, t, slotIndex * ptrSize)
stmts.push(
module.store(this.ptrSize,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.local_get(temp, this.ptrType),
this.ptrType, slotIndex * this.ptrSize
)
);
// -> t
stmts.push(
module.local_get(temp, this.ptrType)
);
operands[i] = module.block(null, stmts, this.ptrType);
++numSlots;
}
return numSlots;
}
/** Slot offset accounting for nested calls. */
private callSlotOffset: i32 = 0;
/** Slot offset stack in nested calls. */
private callSlotStack: i32[] = new Array();
/** @override */
visitCallPre(call: ExpressionRef): void {
var numOperands = _BinaryenCallGetNumOperands(call);
var operands = new Array<ExpressionRef>(numOperands);
for (let i: Index = 0; i < numOperands; ++i) {
operands[i] = _BinaryenCallGetOperandAt(call, i);
}
let numSlots = this.updateCallOperands(operands);
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallSetOperandAt(call, i, operands[i]);
}
if (numSlots) {
// Reserve these slots for us so nested calls use their own
this.callSlotOffset += numSlots;
}
this.callSlotStack.push(numSlots);
}
/** @override */
visitCall(call: ExpressionRef): void {
let numSlots = this.callSlotStack.pop();
if (numSlots) this.callSlotOffset -= numSlots;
}
/** @override */
visitCallIndirectPre(callIndirect: ExpressionRef): void {
let numOperands = _BinaryenCallIndirectGetNumOperands(callIndirect);
let operands = new Array<ExpressionRef>(numOperands);
for (let i: Index = 0; i < numOperands; ++i) {
operands[i] = _BinaryenCallIndirectGetOperandAt(callIndirect, i);
}
let numSlots = this.updateCallOperands(operands);
for (let i = 0, k = operands.length; i < k; ++i) {
_BinaryenCallIndirectSetOperandAt(callIndirect, i, operands[i]);
}
if (numSlots) {
// Reserve these slots for us so nested calls use their own
this.callSlotOffset += numSlots;
}
this.callSlotStack.push(numSlots);
}
/** @override */
visitCallIndirect(callIndirect: ExpressionRef): void {
let numSlots = this.callSlotStack.pop();
if (numSlots) this.callSlotOffset -= numSlots;
}
/** @override */
visitLocalSet(localSet: ExpressionRef): void {
let module = this.module;
let value = _BinaryenLocalSetGetValue(localSet);
let match = matchPattern(module, value);
if (!match) return;
if (!needsSlot(module, match)) {
_BinaryenLocalSetSetValue(localSet, match);
return;
}
let index = _BinaryenLocalSetGetIndex(localSet);
let slotIndex = this.noteSlot(this.currentFunction, index);
let stmts = new Array<ExpressionRef>();
// store<usize>(__stack_pointer, local = match, slotIndex * ptrSize)
stmts.push(
module.store(this.ptrSize,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.local_tee(index, match, false),
this.ptrType, slotIndex * this.ptrSize
)
);
if (_BinaryenLocalSetIsTee(localSet)) {
// -> local
stmts.push(
module.local_get(index, this.ptrType)
);
this.replaceCurrent(module.flatten(stmts, this.ptrType));
} else {
this.replaceCurrent(module.flatten(stmts, TypeRef.None));
}
}
/** Updates a function with additional locals etc. */
updateFunction(funcRef: FunctionRef): void {
let name = _BinaryenFunctionGetName(funcRef);
let params = _BinaryenFunctionGetParams(funcRef);
let results = _BinaryenFunctionGetResults(funcRef);
let body = assert(_BinaryenFunctionGetBody(funcRef));
let numVars = _BinaryenFunctionGetNumVars(funcRef);
let vars = new Array<TypeRef>();
for (let i: Index = 0; i < numVars; ++i) {
vars[i] = _BinaryenFunctionGetVar(funcRef, i);
}
let tempMaps = this.tempMaps;
if (tempMaps.has(funcRef)) {
let tempMap = changetype<TempMap>(tempMaps.get(funcRef));
for (let _keys = Map_keys(tempMap), i = 0, k = _keys.length; i < k; ++i) {
vars.push(_keys[i]);
}
}
let moduleRef = this.module.ref;
_BinaryenRemoveFunction(moduleRef, name);
let cArr = allocPtrArray(vars);
_BinaryenAddFunction(moduleRef, name, params, results, cArr, vars.length, body);
_free(cArr);
}
/** Updates a function export taking managed arguments. */
updateExport(exportRef: ExportRef, managedOperandIndices: i32[]): void {
var module = this.module;
var moduleRef = module.ref;
assert(_BinaryenExportGetKind(exportRef) == ExternalKind.Function);
var internalNameRef = _BinaryenExportGetValue(exportRef);
var internalName = module.readStringCached(internalNameRef)!;
var externalNameRef = _BinaryenExportGetName(exportRef);
var funcRef = _BinaryenGetFunction(moduleRef, internalNameRef);
var params = _BinaryenFunctionGetParams(funcRef);
var paramTypes = expandType(params);
var numParams = paramTypes.length;
var results = _BinaryenFunctionGetResults(funcRef);
var numLocals = numParams;
var vars = new Array<TypeRef>();
var numSlots = assert(managedOperandIndices.length);
var frameSize = numSlots * this.ptrSize;
var wrapperName = "export:" + internalName;
var wrapperNameRef = module.allocStringCached(wrapperName);
if (_BinaryenGetFunction(moduleRef, wrapperNameRef) == 0) {
let stmts = new Array<ExpressionRef>();
// __stack_pointer -= frameSize
stmts.push(
this.makeStackOffset(-frameSize)
);
for (let slotIndex = 0; slotIndex < numSlots; ++slotIndex) {
// store<usize>(__stack_pointer, $local, slotIndex * ptrSize)
stmts.push(
module.store(this.ptrSize,
module.global_get(BuiltinNames.stack_pointer, this.ptrType),
module.local_get(managedOperandIndices[slotIndex], this.ptrType),
this.ptrType, slotIndex * this.ptrSize
)
);
}
let forwardedOperands = new Array<ExpressionRef>(numParams);
for (let i = 0; i < numParams; ++i) {
forwardedOperands[i] = module.local_get(i, paramTypes[i]);
}
if (results != TypeRef.None) {
let tempIndex = numLocals++;
vars.push(results);
// t = original(...)
stmts.push(
module.local_set(tempIndex,
module.call(internalName, forwardedOperands, results),
false // internal
)
);
// __stack_pointer += frameSize
stmts.push(
this.makeStackOffset(+frameSize)
);
// -> t
stmts.push(
module.local_get(tempIndex, results)
);
} else {
// original(...)
stmts.push(
module.call(internalName, forwardedOperands, results)
);
// __stack_pointer += frameSize
stmts.push(
this.makeStackOffset(+frameSize)
);
}
let cArr = allocPtrArray(vars);
_BinaryenAddFunction(moduleRef, wrapperNameRef, params, results, cArr, vars.length,
module.block(null, stmts, results)
);
_free(cArr);
}
_BinaryenRemoveExport(moduleRef, externalNameRef);
_BinaryenAddFunctionExport(moduleRef, wrapperNameRef, externalNameRef);
}
/** @override */
walkModule(): void {
// Run the pass normally
super.walkModule();
// Instrument returns in functions utilizing stack slots
var module = this.module;
var instrumentReturns = new InstrumentReturns(this);
for (let _keys = Map_keys(this.slotMaps), i = 0, k = _keys.length; i < k; ++i) {
let func = _keys[i];
let slotMap = changetype<SlotMap>(this.slotMaps.get(func));
let frameSize = slotMap.size * this.ptrSize;
// Instrument function returns
instrumentReturns.frameSize = frameSize;
instrumentReturns.walkFunction(func);
// Instrument function entry
let stmts = new Array<ExpressionRef>();
// __stack_pointer -= frameSize
stmts.push(
this.makeStackOffset(-frameSize)
);
// memory.fill(__stack_pointer, 0, frameSize)
this.makeStackFill(frameSize, stmts);
// Handle implicit return
let body = _BinaryenFunctionGetBody(func);
let bodyType = _BinaryenExpressionGetType(body);
if (bodyType == TypeRef.Unreachable) {
// body
stmts.push(
body
);
} else if (bodyType == TypeRef.None) {
// body
stmts.push(
body
);
// __stack_pointer += frameSize
stmts.push(
this.makeStackOffset(+frameSize)
);
} else {
let temp = this.getSharedTemp(func, bodyType);
// t = body
stmts.push(
module.local_set(temp, body, false)
);
// __stack_pointer += frameSize
stmts.push(
this.makeStackOffset(+frameSize)
);
// -> t
stmts.push(
module.local_get(temp, bodyType)
);
}
_BinaryenFunctionSetBody(func, module.flatten(stmts, bodyType));
}
// Update functions we added more locals to
// TODO: _BinaryenFunctionAddVar ?
for (let _keys = Map_keys(this.tempMaps), i = 0, k = _keys.length; i < k; ++i) {
this.updateFunction(_keys[i]);
}
// Update exports taking managed arguments
var exportMap = this.exportMap;
for (let _keys = Map_keys(exportMap), i = 0, k = _keys.length; i < k; ++i) {
let exportName = _keys[i];
let exportRef = _BinaryenGetExport(module.ref, module.allocStringCached(exportName));
let managedOperandIndices = changetype<i32[]>(exportMap.get(exportName));
this.updateExport(exportRef, managedOperandIndices);
}
}
}
/** Companion pass instrumenting `return` statements to restore the stack frame. */
class InstrumentReturns extends Pass {
/** Parent pass. */
parentPass: ShadowStackPass;
/** Frame size of the current function being processed. */
frameSize: i32 = 0;
constructor(shadowStack: ShadowStackPass) {
super(shadowStack.module);
this.parentPass = shadowStack;
}
/** @override */
visitReturn(ret: ExpressionRef): void {
assert(this.frameSize);
var module = this.module;
var value = _BinaryenReturnGetValue(ret);
var stmts = new Array<ExpressionRef>();
if (value) {
let returnType = _BinaryenExpressionGetType(value);
if (returnType == TypeRef.Unreachable) return;
let temp = this.parentPass.getSharedTemp(this.currentFunction, returnType);
// t = value
stmts.push(
module.local_set(temp, value, false)
);
// __stack_pointer += frameSize
stmts.push(
this.parentPass.makeStackOffset(+this.frameSize)
);
// return t
_BinaryenReturnSetValue(ret, module.local_get(temp, returnType));
} else {
// __stack_pointer += frameSize
stmts.push(
this.parentPass.makeStackOffset(+this.frameSize)
);
// return
}
stmts.push(
ret
);
this.replaceCurrent(module.flatten(stmts, TypeRef.Unreachable));
}
}