Skip to content

Commit 678593d

Browse files
authored
Fix TLSF failing on certain sizes when implicitly growing memory (#706)
1 parent 496be27 commit 678593d

36 files changed

+1310
-349
lines changed

std/assembly/rt/tlsf.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ function addMemory(root: Root, start: usize, end: usize): bool {
415415
}
416416

417417
// left size is total minus its own and the zero-length tail's header
418-
var leftSize = size - 2 * BLOCK_OVERHEAD;
418+
var leftSize = size - (BLOCK_OVERHEAD << 1);
419419
var left = changetype<Block>(start);
420420
left.mmInfo = leftSize | FREE | (tailInfo & LEFTFREE);
421421
left.prev = null;
@@ -433,7 +433,16 @@ function addMemory(root: Root, start: usize, end: usize): bool {
433433

434434
/** Grows memory to fit at least another block of the specified size. */
435435
function growMemory(root: Root, size: usize): void {
436+
// Here, both rounding performed in searchBlock ...
437+
const halfMaxSize = BLOCK_MAXSIZE >> 1;
438+
if (size < halfMaxSize) { // don't round last fl
439+
const invRound = (sizeof<usize>() * 8 - 1) - SL_BITS;
440+
size += (1 << (invRound - clz<usize>(size))) - 1;
441+
}
442+
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
443+
// to merge with the tail block, that's one time, otherwise it's two times.
436444
var pagesBefore = memory.size();
445+
size += BLOCK_OVERHEAD << usize((<usize>pagesBefore << 16) - BLOCK_OVERHEAD != changetype<usize>(GETTAIL(root)));
437446
var pagesNeeded = <i32>(((size + 0xffff) & ~0xffff) >>> 16);
438447
var pagesWanted = max(pagesBefore, pagesNeeded); // double memory
439448
if (memory.grow(pagesWanted) < 0) {

tests/compiler/empty.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"asc_flags": [
3-
"--runtime none",
3+
"--runtime half",
44
"--use ASC_RTRACE=1"
55
]
66
}

tests/compiler/rc/global-init.optimized.wat

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@
541541
if
542542
i32.const 0
543543
i32.const 88
544-
i32.const 537
544+
i32.const 546
545545
i32.const 2
546546
call $~lib/builtins/abort
547547
unreachable
@@ -781,7 +781,7 @@
781781
if
782782
i32.const 232
783783
i32.const 88
784-
i32.const 448
784+
i32.const 457
785785
i32.const 29
786786
call $~lib/builtins/abort
787787
unreachable
@@ -933,7 +933,32 @@
933933
(local $2 i32)
934934
memory.size
935935
local.tee $2
936+
i32.const 16
937+
local.get $0
938+
i32.load offset=1568
939+
local.get $2
940+
i32.const 16
941+
i32.shl
942+
i32.const 16
943+
i32.sub
944+
i32.ne
945+
i32.shl
946+
i32.const 1
947+
i32.const 27
936948
local.get $1
949+
i32.clz
950+
i32.sub
951+
i32.shl
952+
i32.const 1
953+
i32.sub
954+
local.get $1
955+
i32.add
956+
local.get $1
957+
local.get $1
958+
i32.const 536870904
959+
i32.lt_u
960+
select
961+
i32.add
937962
i32.const 65535
938963
i32.add
939964
i32.const -65536
@@ -1064,7 +1089,7 @@
10641089
if
10651090
i32.const 0
10661091
i32.const 88
1067-
i32.const 478
1092+
i32.const 487
10681093
i32.const 15
10691094
call $~lib/builtins/abort
10701095
unreachable
@@ -1079,7 +1104,7 @@
10791104
if
10801105
i32.const 0
10811106
i32.const 88
1082-
i32.const 480
1107+
i32.const 489
10831108
i32.const 13
10841109
call $~lib/builtins/abort
10851110
unreachable
@@ -1302,7 +1327,7 @@
13021327
if
13031328
i32.const 0
13041329
i32.const 88
1305-
i32.const 567
1330+
i32.const 576
13061331
i32.const 13
13071332
call $~lib/builtins/abort
13081333
unreachable
@@ -1318,7 +1343,7 @@
13181343
if
13191344
i32.const 0
13201345
i32.const 88
1321-
i32.const 568
1346+
i32.const 577
13221347
i32.const 2
13231348
call $~lib/builtins/abort
13241349
unreachable

tests/compiler/rc/global-init.untouched.wat

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@
668668
if
669669
i32.const 0
670670
i32.const 88
671-
i32.const 537
671+
i32.const 546
672672
i32.const 2
673673
call $~lib/builtins/abort
674674
unreachable
@@ -812,9 +812,9 @@
812812
return
813813
end
814814
local.get $6
815-
i32.const 2
816815
i32.const 16
817-
i32.mul
816+
i32.const 1
817+
i32.shl
818818
i32.sub
819819
local.set $7
820820
local.get $1
@@ -1011,7 +1011,7 @@
10111011
if
10121012
i32.const 232
10131013
i32.const 88
1014-
i32.const 448
1014+
i32.const 457
10151015
i32.const 29
10161016
call $~lib/builtins/abort
10171017
unreachable
@@ -1221,9 +1221,40 @@
12211221
(local $5 i32)
12221222
(local $6 i32)
12231223
(local $7 i32)
1224+
local.get $1
1225+
i32.const 536870904
1226+
i32.lt_u
1227+
if
1228+
local.get $1
1229+
i32.const 1
1230+
i32.const 27
1231+
local.get $1
1232+
i32.clz
1233+
i32.sub
1234+
i32.shl
1235+
i32.const 1
1236+
i32.sub
1237+
i32.add
1238+
local.set $1
1239+
end
12241240
memory.size
12251241
local.set $2
12261242
local.get $1
1243+
i32.const 16
1244+
local.get $2
1245+
i32.const 16
1246+
i32.shl
1247+
i32.const 16
1248+
i32.sub
1249+
local.get $0
1250+
local.set $3
1251+
local.get $3
1252+
i32.load offset=1568
1253+
i32.ne
1254+
i32.shl
1255+
i32.add
1256+
local.set $1
1257+
local.get $1
12271258
i32.const 65535
12281259
i32.add
12291260
i32.const 65535
@@ -1232,12 +1263,12 @@
12321263
i32.and
12331264
i32.const 16
12341265
i32.shr_u
1235-
local.set $3
1266+
local.set $4
12361267
local.get $2
1237-
local.tee $4
1238-
local.get $3
1239-
local.tee $5
1268+
local.tee $3
12401269
local.get $4
1270+
local.tee $5
1271+
local.get $3
12411272
local.get $5
12421273
i32.gt_s
12431274
select
@@ -1247,7 +1278,7 @@
12471278
i32.const 0
12481279
i32.lt_s
12491280
if
1250-
local.get $3
1281+
local.get $4
12511282
memory.grow
12521283
i32.const 0
12531284
i32.lt_s
@@ -1387,7 +1418,7 @@
13871418
if
13881419
i32.const 0
13891420
i32.const 88
1390-
i32.const 478
1421+
i32.const 487
13911422
i32.const 15
13921423
call $~lib/builtins/abort
13931424
unreachable
@@ -1403,7 +1434,7 @@
14031434
if
14041435
i32.const 0
14051436
i32.const 88
1406-
i32.const 480
1437+
i32.const 489
14071438
i32.const 13
14081439
call $~lib/builtins/abort
14091440
unreachable
@@ -2707,7 +2738,7 @@
27072738
if
27082739
i32.const 0
27092740
i32.const 88
2710-
i32.const 567
2741+
i32.const 576
27112742
i32.const 13
27122743
call $~lib/builtins/abort
27132744
unreachable
@@ -2727,7 +2758,7 @@
27272758
if
27282759
i32.const 0
27292760
i32.const 88
2730-
i32.const 568
2761+
i32.const 577
27312762
i32.const 2
27322763
call $~lib/builtins/abort
27332764
unreachable

tests/compiler/rc/local-init.optimized.wat

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@
539539
if
540540
i32.const 0
541541
i32.const 88
542-
i32.const 537
542+
i32.const 546
543543
i32.const 2
544544
call $~lib/builtins/abort
545545
unreachable
@@ -779,7 +779,7 @@
779779
if
780780
i32.const 232
781781
i32.const 88
782-
i32.const 448
782+
i32.const 457
783783
i32.const 29
784784
call $~lib/builtins/abort
785785
unreachable
@@ -931,7 +931,32 @@
931931
(local $2 i32)
932932
memory.size
933933
local.tee $2
934+
i32.const 16
935+
local.get $0
936+
i32.load offset=1568
937+
local.get $2
938+
i32.const 16
939+
i32.shl
940+
i32.const 16
941+
i32.sub
942+
i32.ne
943+
i32.shl
944+
i32.const 1
945+
i32.const 27
934946
local.get $1
947+
i32.clz
948+
i32.sub
949+
i32.shl
950+
i32.const 1
951+
i32.sub
952+
local.get $1
953+
i32.add
954+
local.get $1
955+
local.get $1
956+
i32.const 536870904
957+
i32.lt_u
958+
select
959+
i32.add
935960
i32.const 65535
936961
i32.add
937962
i32.const -65536
@@ -1062,7 +1087,7 @@
10621087
if
10631088
i32.const 0
10641089
i32.const 88
1065-
i32.const 478
1090+
i32.const 487
10661091
i32.const 15
10671092
call $~lib/builtins/abort
10681093
unreachable
@@ -1077,7 +1102,7 @@
10771102
if
10781103
i32.const 0
10791104
i32.const 88
1080-
i32.const 480
1105+
i32.const 489
10811106
i32.const 13
10821107
call $~lib/builtins/abort
10831108
unreachable
@@ -1299,7 +1324,7 @@
12991324
if
13001325
i32.const 0
13011326
i32.const 88
1302-
i32.const 567
1327+
i32.const 576
13031328
i32.const 13
13041329
call $~lib/builtins/abort
13051330
unreachable
@@ -1315,7 +1340,7 @@
13151340
if
13161341
i32.const 0
13171342
i32.const 88
1318-
i32.const 568
1343+
i32.const 577
13191344
i32.const 2
13201345
call $~lib/builtins/abort
13211346
unreachable

0 commit comments

Comments
 (0)