Skip to content

Commit fc3ee10

Browse files
authored
Fix static alignment and realloc before initialization (#1025)
1 parent 01fe0b4 commit fc3ee10

File tree

181 files changed

+24343
-24854
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+24343
-24854
lines changed

src/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ export class Compiler extends DiagnosticEmitter {
14501450
// === Memory ===================================================================================
14511451

14521452
/** Adds a static memory segment with the specified data. */
1453-
addMemorySegment(buffer: Uint8Array, alignment: i32 = 8): MemorySegment {
1453+
addMemorySegment(buffer: Uint8Array, alignment: i32 = 16): MemorySegment {
14541454
var memoryOffset = i64_align(this.memoryOffset, alignment);
14551455
var segment = MemorySegment.create(buffer, memoryOffset);
14561456
this.memorySegments.push(segment);

std/assembly/rt/stub.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function __alloc(size: usize, id: u32): usize {
3131
maybeGrowMemory(ptr + actualSize);
3232
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
3333
block.mmInfo = actualSize;
34-
if (DEBUG) block.gcInfo = -1;
34+
if (DEBUG) block.gcInfo = 1;
3535
block.rtId = id;
3636
block.rtSize = size;
3737
return ptr;
@@ -43,7 +43,7 @@ export function __realloc(ptr: usize, size: usize): usize {
4343
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
4444
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
4545
var actualSize = block.mmInfo;
46-
if (DEBUG) assert(block.gcInfo == -1);
46+
if (DEBUG) assert(block.gcInfo == 1);
4747
var isLast = ptr + actualSize == offset;
4848
var alignedSize = (size + AL_MASK) & ~AL_MASK;
4949
if (size > actualSize) {
@@ -69,7 +69,7 @@ export function __realloc(ptr: usize, size: usize): usize {
6969
export function __free(ptr: usize): void {
7070
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
7171
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
72-
if (DEBUG) assert(block.gcInfo == -1);
72+
if (DEBUG) assert(block.gcInfo == 1);
7373
if (ptr + block.mmInfo == offset) { // last block: discard
7474
offset = changetype<usize>(block);
7575
}

std/assembly/rt/tlsf.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -459,22 +459,26 @@ function prepareSize(size: usize): usize {
459459
}
460460

461461
/** Initilizes the root structure. */
462-
export function initializeRoot(): void {
463-
var rootOffset = (__heap_base + AL_MASK) & ~AL_MASK;
464-
var pagesBefore = memory.size();
465-
var pagesNeeded = <i32>((((rootOffset + ROOT_SIZE) + 0xffff) & ~0xffff) >>> 16);
466-
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
467-
var root = changetype<Root>(rootOffset);
468-
root.flMap = 0;
469-
SETTAIL(root, changetype<Block>(0));
470-
for (let fl: usize = 0; fl < FL_BITS; ++fl) {
471-
SETSL(root, fl, 0);
472-
for (let sl: u32 = 0; sl < SL_SIZE; ++sl) {
473-
SETHEAD(root, fl, sl, null);
462+
export function maybeInitialize(): Root {
463+
var root = ROOT;
464+
if (!root) {
465+
const rootOffset = (__heap_base + AL_MASK) & ~AL_MASK;
466+
let pagesBefore = memory.size();
467+
let pagesNeeded = <i32>((((rootOffset + ROOT_SIZE) + 0xffff) & ~0xffff) >>> 16);
468+
if (pagesNeeded > pagesBefore && memory.grow(pagesNeeded - pagesBefore) < 0) unreachable();
469+
root = changetype<Root>(rootOffset);
470+
root.flMap = 0;
471+
SETTAIL(root, changetype<Block>(0));
472+
for (let fl: usize = 0; fl < FL_BITS; ++fl) {
473+
SETSL(root, fl, 0);
474+
for (let sl: u32 = 0; sl < SL_SIZE; ++sl) {
475+
SETHEAD(root, fl, sl, null);
476+
}
474477
}
478+
addMemory(root, (rootOffset + ROOT_SIZE + AL_MASK) & ~AL_MASK, memory.size() << 16);
479+
ROOT = root;
475480
}
476-
addMemory(root, (rootOffset + ROOT_SIZE + AL_MASK) & ~AL_MASK, memory.size() << 16);
477-
ROOT = root;
481+
return root;
478482
}
479483

480484
// @ts-ignore: decorator
@@ -551,9 +555,11 @@ export function reallocateBlock(root: Root, block: Block, size: usize): Block {
551555
var newBlock = allocateBlock(root, size);
552556
newBlock.rtId = block.rtId;
553557
memory.copy(changetype<usize>(newBlock) + BLOCK_OVERHEAD, changetype<usize>(block) + BLOCK_OVERHEAD, size);
554-
block.mmInfo = blockInfo | FREE;
555-
insertBlock(root, block);
556-
if (isDefined(ASC_RTRACE)) onfree(block);
558+
if (changetype<usize>(block) >= __heap_base) {
559+
block.mmInfo = blockInfo | FREE;
560+
insertBlock(root, block);
561+
if (isDefined(ASC_RTRACE)) onfree(block);
562+
}
557563
return newBlock;
558564
}
559565

@@ -569,28 +575,21 @@ export function freeBlock(root: Root, block: Block): void {
569575
// @ts-ignore: decorator
570576
@global @unsafe
571577
export function __alloc(size: usize, id: u32): usize {
572-
var root = ROOT;
573-
if (!root) {
574-
initializeRoot();
575-
root = ROOT;
576-
}
577-
var block = allocateBlock(root, size);
578+
var block = allocateBlock(maybeInitialize(), size);
578579
block.rtId = id;
579580
return changetype<usize>(block) + BLOCK_OVERHEAD;
580581
}
581582

582583
// @ts-ignore: decorator
583584
@global @unsafe
584585
export function __realloc(ref: usize, size: usize): usize {
585-
if (DEBUG) assert(ROOT); // must be initialized
586586
assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned
587-
return changetype<usize>(reallocateBlock(ROOT, changetype<Block>(ref - BLOCK_OVERHEAD), size)) + BLOCK_OVERHEAD;
587+
return changetype<usize>(reallocateBlock(maybeInitialize(), changetype<Block>(ref - BLOCK_OVERHEAD), size)) + BLOCK_OVERHEAD;
588588
}
589589

590590
// @ts-ignore: decorator
591591
@global @unsafe
592592
export function __free(ref: usize): void {
593-
if (DEBUG) assert(ROOT); // must be initialized
594593
assert(ref != 0 && !(ref & AL_MASK)); // must exist and be aligned
595-
freeBlock(ROOT, changetype<Block>(ref - BLOCK_OVERHEAD));
594+
freeBlock(maybeInitialize(), changetype<Block>(ref - BLOCK_OVERHEAD));
596595
}

tests/compiler/abi.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $none_=>_none (func))
33
(type $none_=>_i32 (func (result i32)))
44
(memory $0 1)
5-
(data (i32.const 8) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
5+
(data (i32.const 16) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
66
(global $abi/condition (mut i32) (i32.const 0))
77
(global $abi/y (mut i32) (i32.const 0))
88
(export "memory" (memory $0))

tests/compiler/abi.untouched.wat

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
55
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
66
(memory $0 1)
7-
(data (i32.const 8) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s\00")
7+
(data (i32.const 16) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s\00")
88
(table $0 1 funcref)
99
(global $abi/condition (mut i32) (i32.const 0))
1010
(global $abi/y (mut i32) (i32.const 0))
@@ -35,7 +35,7 @@
3535
i32.eqz
3636
if
3737
i32.const 0
38-
i32.const 24
38+
i32.const 32
3939
i32.const 32
4040
i32.const 2
4141
call $~lib/builtins/abort
@@ -72,7 +72,7 @@
7272
i32.eqz
7373
if
7474
i32.const 0
75-
i32.const 24
75+
i32.const 32
7676
i32.const 45
7777
i32.const 2
7878
call $~lib/builtins/abort
@@ -101,7 +101,7 @@
101101
i32.eqz
102102
if
103103
i32.const 0
104-
i32.const 24
104+
i32.const 32
105105
i32.const 58
106106
i32.const 2
107107
call $~lib/builtins/abort
@@ -118,7 +118,7 @@
118118
i32.eqz
119119
if
120120
i32.const 0
121-
i32.const 24
121+
i32.const 32
122122
i32.const 65
123123
i32.const 2
124124
call $~lib/builtins/abort
@@ -133,7 +133,7 @@
133133
i32.eqz
134134
if
135135
i32.const 0
136-
i32.const 24
136+
i32.const 32
137137
i32.const 72
138138
i32.const 2
139139
call $~lib/builtins/abort
@@ -148,7 +148,7 @@
148148
i32.eqz
149149
if
150150
i32.const 0
151-
i32.const 24
151+
i32.const 32
152152
i32.const 74
153153
i32.const 2
154154
call $~lib/builtins/abort
@@ -161,7 +161,7 @@
161161
i32.eqz
162162
if
163163
i32.const 0
164-
i32.const 24
164+
i32.const 32
165165
i32.const 77
166166
i32.const 2
167167
call $~lib/builtins/abort
@@ -174,7 +174,7 @@
174174
i32.eqz
175175
if
176176
i32.const 0
177-
i32.const 24
177+
i32.const 32
178178
i32.const 79
179179
i32.const 2
180180
call $~lib/builtins/abort

tests/compiler/assert-nonnull.optimized.wat

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
55
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
66
(memory $0 1)
7-
(data (i32.const 8) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
8-
(data (i32.const 64) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
9-
(data (i32.const 112) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
7+
(data (i32.const 16) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
8+
(data (i32.const 80) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
9+
(data (i32.const 128) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
1010
(table $0 1 funcref)
1111
(global $~lib/argc (mut i32) (i32.const 0))
1212
(export "memory" (memory $0))
@@ -60,8 +60,8 @@
6060
i32.load offset=12
6161
i32.ge_u
6262
if
63-
i32.const 24
64-
i32.const 80
63+
i32.const 32
64+
i32.const 96
6565
i32.const 93
6666
i32.const 41
6767
call $~lib/builtins/abort
@@ -72,8 +72,8 @@
7272
local.tee $0
7373
i32.eqz
7474
if
75-
i32.const 128
76-
i32.const 80
75+
i32.const 144
76+
i32.const 96
7777
i32.const 97
7878
i32.const 39
7979
call $~lib/builtins/abort
@@ -96,8 +96,8 @@
9696
i32.load offset=12
9797
i32.ge_u
9898
if
99-
i32.const 24
100-
i32.const 80
99+
i32.const 32
100+
i32.const 96
101101
i32.const 93
102102
i32.const 41
103103
call $~lib/builtins/abort

tests/compiler/assert-nonnull.untouched.wat

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
77
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
88
(memory $0 1)
9-
(data (i32.const 8) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00")
10-
(data (i32.const 64) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00")
11-
(data (i32.const 112) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00")
9+
(data (i32.const 16) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e\00")
10+
(data (i32.const 80) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00")
11+
(data (i32.const 128) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00")
1212
(table $0 1 funcref)
1313
(global $~lib/argc (mut i32) (i32.const 0))
1414
(export "memory" (memory $0))
@@ -103,8 +103,8 @@
103103
i32.load offset=12
104104
i32.ge_u
105105
if
106-
i32.const 24
107-
i32.const 80
106+
i32.const 32
107+
i32.const 96
108108
i32.const 93
109109
i32.const 41
110110
call $~lib/builtins/abort
@@ -119,8 +119,8 @@
119119
if
120120
local.get $2
121121
call $~lib/rt/stub/__release
122-
i32.const 128
123-
i32.const 80
122+
i32.const 144
123+
i32.const 96
124124
i32.const 97
125125
i32.const 39
126126
call $~lib/builtins/abort
@@ -164,8 +164,8 @@
164164
i32.load offset=12
165165
i32.ge_u
166166
if
167-
i32.const 24
168-
i32.const 80
167+
i32.const 32
168+
i32.const 96
169169
i32.const 93
170170
i32.const 41
171171
call $~lib/builtins/abort

tests/compiler/bool.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(module
22
(type $none_=>_none (func))
33
(memory $0 1)
4-
(data (i32.const 8) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
4+
(data (i32.const 16) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
55
(export "memory" (memory $0))
66
(start $start)
77
(func $start (; 0 ;)

tests/compiler/bool.untouched.wat

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
44
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
55
(memory $0 1)
6-
(data (i32.const 8) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s\00")
6+
(data (i32.const 16) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s\00")
77
(table $0 1 funcref)
88
(global $bool/i (mut i32) (i32.const 2))
99
(global $bool/I (mut i64) (i64.const 2))
@@ -23,7 +23,7 @@
2323
i32.eqz
2424
if
2525
i32.const 0
26-
i32.const 24
26+
i32.const 32
2727
i32.const 2
2828
i32.const 0
2929
call $~lib/builtins/abort
@@ -37,7 +37,7 @@
3737
i32.eqz
3838
if
3939
i32.const 0
40-
i32.const 24
40+
i32.const 32
4141
i32.const 4
4242
i32.const 0
4343
call $~lib/builtins/abort
@@ -51,7 +51,7 @@
5151
i32.eqz
5252
if
5353
i32.const 0
54-
i32.const 24
54+
i32.const 32
5555
i32.const 6
5656
i32.const 0
5757
call $~lib/builtins/abort
@@ -65,7 +65,7 @@
6565
i32.eqz
6666
if
6767
i32.const 0
68-
i32.const 24
68+
i32.const 32
6969
i32.const 8
7070
i32.const 0
7171
call $~lib/builtins/abort
@@ -79,7 +79,7 @@
7979
i32.eqz
8080
if
8181
i32.const 0
82-
i32.const 24
82+
i32.const 32
8383
i32.const 10
8484
i32.const 0
8585
call $~lib/builtins/abort
@@ -93,7 +93,7 @@
9393
i32.eqz
9494
if
9595
i32.const 0
96-
i32.const 24
96+
i32.const 32
9797
i32.const 12
9898
i32.const 0
9999
call $~lib/builtins/abort
@@ -107,7 +107,7 @@
107107
i32.eqz
108108
if
109109
i32.const 0
110-
i32.const 24
110+
i32.const 32
111111
i32.const 14
112112
i32.const 0
113113
call $~lib/builtins/abort

0 commit comments

Comments
 (0)