Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9b21306

Browse files
authoredApr 6, 2021
fix: Fix invalid store offsets in memset polyfill (#1787)
1 parent be3dfaf commit 9b21306

File tree

120 files changed

+3506
-1471
lines changed

Some content is hidden

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

120 files changed

+3506
-1471
lines changed
 

‎std/assembly/util/memory.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,17 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
201201
} else {
202202
// fill head and tail with minimal branching
203203
if (!n) return;
204-
let dend = dest + n - 4;
204+
let dend = dest + n;
205205
store<u8>(dest, c);
206-
store<u8>(dend, c, 3);
206+
store<u8>(dend - 1, c);
207207
if (n <= 2) return;
208208
store<u8>(dest, c, 1);
209209
store<u8>(dest, c, 2);
210-
store<u8>(dend, c, 2);
211-
store<u8>(dend, c, 1);
210+
store<u8>(dend - 2, c);
211+
store<u8>(dend - 3, c);
212212
if (n <= 6) return;
213213
store<u8>(dest, c, 3);
214-
store<u8>(dend, c);
214+
store<u8>(dend - 4, c);
215215
if (n <= 8) return;
216216

217217
// advance pointer to align it at 4-byte boundary
@@ -223,23 +223,23 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
223223
let c32: u32 = <u32>-1 / 255 * c;
224224

225225
// fill head/tail up to 28 bytes each in preparation
226-
dend = dest + n - 28;
226+
dend = dest + n;
227227
store<u32>(dest, c32);
228-
store<u32>(dend, c32, 24);
228+
store<u32>(dend - 4, c32);
229229
if (n <= 8) return;
230230
store<u32>(dest, c32, 4);
231231
store<u32>(dest, c32, 8);
232-
store<u32>(dend, c32, 16);
233-
store<u32>(dend, c32, 20);
232+
store<u32>(dend - 12, c32);
233+
store<u32>(dend - 8, c32);
234234
if (n <= 24) return;
235235
store<u32>(dest, c32, 12);
236236
store<u32>(dest, c32, 16);
237237
store<u32>(dest, c32, 20);
238238
store<u32>(dest, c32, 24);
239-
store<u32>(dend, c32);
240-
store<u32>(dend, c32, 4);
241-
store<u32>(dend, c32, 8);
242-
store<u32>(dend, c32, 12);
239+
store<u32>(dend - 28, c32);
240+
store<u32>(dend - 24, c32);
241+
store<u32>(dend - 20, c32);
242+
store<u32>(dend - 16, c32);
243243

244244
// align to a multiple of 8
245245
k = 24 + (dest & 4);

‎tests/compiler/call-super.optimized.wat

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,11 +1449,11 @@
14491449
local.get $0
14501450
local.get $1
14511451
i32.add
1452-
i32.const 4
1453-
i32.sub
14541452
local.tee $2
1453+
i32.const 1
1454+
i32.sub
14551455
i32.const 0
1456-
i32.store8 offset=3
1456+
i32.store8
14571457
local.get $1
14581458
i32.const 2
14591459
i32.le_u
@@ -1465,11 +1465,15 @@
14651465
i32.const 0
14661466
i32.store8 offset=2
14671467
local.get $2
1468+
i32.const 2
1469+
i32.sub
14681470
i32.const 0
1469-
i32.store8 offset=2
1471+
i32.store8
14701472
local.get $2
1473+
i32.const 3
1474+
i32.sub
14711475
i32.const 0
1472-
i32.store8 offset=1
1476+
i32.store8
14731477
local.get $1
14741478
i32.const 6
14751479
i32.le_u
@@ -1478,6 +1482,8 @@
14781482
i32.const 0
14791483
i32.store8 offset=3
14801484
local.get $2
1485+
i32.const 4
1486+
i32.sub
14811487
i32.const 0
14821488
i32.store8
14831489
local.get $1
@@ -1503,11 +1509,11 @@
15031509
i32.and
15041510
local.tee $2
15051511
i32.add
1506-
i32.const 28
1507-
i32.sub
15081512
local.tee $1
1513+
i32.const 4
1514+
i32.sub
15091515
i32.const 0
1510-
i32.store offset=24
1516+
i32.store
15111517
local.get $2
15121518
i32.const 8
15131519
i32.le_u
@@ -1519,11 +1525,15 @@
15191525
i32.const 0
15201526
i32.store offset=8
15211527
local.get $1
1528+
i32.const 12
1529+
i32.sub
15221530
i32.const 0
1523-
i32.store offset=16
1531+
i32.store
15241532
local.get $1
1533+
i32.const 8
1534+
i32.sub
15251535
i32.const 0
1526-
i32.store offset=20
1536+
i32.store
15271537
local.get $2
15281538
i32.const 24
15291539
i32.le_u
@@ -1541,17 +1551,25 @@
15411551
i32.const 0
15421552
i32.store offset=24
15431553
local.get $1
1554+
i32.const 28
1555+
i32.sub
15441556
i32.const 0
15451557
i32.store
15461558
local.get $1
1559+
i32.const 24
1560+
i32.sub
15471561
i32.const 0
1548-
i32.store offset=4
1562+
i32.store
15491563
local.get $1
1564+
i32.const 20
1565+
i32.sub
15501566
i32.const 0
1551-
i32.store offset=8
1567+
i32.store
15521568
local.get $1
1569+
i32.const 16
1570+
i32.sub
15531571
i32.const 0
1554-
i32.store offset=12
1572+
i32.store
15551573
local.get $0
15561574
local.get $0
15571575
i32.const 4

0 commit comments

Comments
 (0)