Skip to content

Truly independent web browser

LibJS: Call native functions of another realm in the fast paths

Andreas Kling864f4362b533
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index b206ffcc803..c7e6348b02d 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -5571,7 +5571,7 @@ asm_handler_GetById:
ldr x2, [x2, x4, lsl #3]
cbz x2, .Lasm_GetById.ssa_block_1
and x2, x2, #0xfffffffffffffffc
- cbz x2, .Lasm_GetById.assert_predicate_failure_328
+ cbz x2, .Lasm_GetById.assert_predicate_failure_333
ldr x9, [x2, #24]
cmp x9, x0
b.ne .Lasm_GetById.ssa_block_15
@@ -6387,10 +6387,10 @@ asm_handler_Call:
b.ls .Lasm_Call.ssa_block_17
mov x2, x4
.Lasm_Call.ssa_block_17:
- cbz x3, .Lasm_Call.assert_predicate_failure_230
+ cbz x3, .Lasm_Call.assert_predicate_failure_235
ldp w0, w4, [x3, #12]
cmp x4, x0
- b.lo .Lasm_Call.assert_predicate_failure_236
+ b.lo .Lasm_Call.assert_predicate_failure_241
movn w0, #0
subs x0, x0, x4
cmp x0, x2
@@ -6405,7 +6405,7 @@ asm_handler_Call:
add x5, x5, #136
adds x5, x5, x0
cmp x5, x0
- b.lo .Lasm_Call.assert_predicate_failure_250
+ b.lo .Lasm_Call.assert_predicate_failure_255
cmp x5, x8
b.hi .Lasm_Call.ssa_block_1
mov x8, x20
@@ -6418,7 +6418,7 @@ asm_handler_Call:
add x9, x24, w1, uxtw #3
cmp w1, #0
csel x1, x9, xzr, ne
- cbz x1, .Lasm_Call.assert_predicate_failure_271
+ cbz x1, .Lasm_Call.assert_predicate_failure_276
str x1, [x0, #32]
stp x6, x3, [x0, #80]
movz x1, #0x7ffb, lsl #48
@@ -6445,7 +6445,7 @@ asm_handler_Call:
stp w1, w5, [x0, #108]
ldrb w3, [x21, #6]
cmp x2, x3
- b.lo .Lasm_Call.assert_predicate_failure_327
+ b.lo .Lasm_Call.assert_predicate_failure_332
add x6, x21, #7
add x5, x0, #136
add x7, x28, #136
@@ -6471,7 +6471,7 @@ asm_handler_Call:
b .Lasm_Call.ssa_block_25
.Lasm_Call.ssa_block_24:
ldr x1, [x0, #88]
- cbz x1, .Lasm_Call.assert_predicate_failure_349
+ cbz x1, .Lasm_Call.assert_predicate_failure_354
ldr x26, [x1, #40]
cbz x26, .Lasm_Call.assert_failure_5
mov x1, x20
@@ -6488,48 +6488,38 @@ asm_handler_Call:
b .Lasm_Call.ssa_block_1
1:
ldr w4, [x1, #92]
- ldr w0, [x1, #20]
- cbz x0, .Lasm_Call.assert_failure_6
- add x0, x24, w0, uxtw #3
- ldr w0, [x0, #12]
- cbz x0, .Lasm_Call.assert_failure_7
- add x0, x24, w0, uxtw #3
- mov x2, x20
- ldr x9, [x2, #208]
- cmp x9, x0
- b.ne .Lasm_Call.ssa_block_1
- ldrb w2, [x21, #6]
+ ldrb w3, [x21, #6]
mov x0, x20
ldp x0, x5, [x0, #16]
- cbz x0, .Lasm_Call.assert_failure_8
- cbz x5, .Lasm_Call.assert_failure_9
- lsl x3, x2, #3
- add x3, x3, #136
- adds x3, x3, x0
- cmp x3, x0
- b.lo .Lasm_Call.assert_predicate_failure_50
- cmp x3, x5
+ cbz x0, .Lasm_Call.assert_failure_6
+ cbz x5, .Lasm_Call.assert_failure_7
+ lsl x2, x3, #3
+ add x2, x2, #136
+ adds x2, x2, x0
+ cmp x2, x0
+ b.lo .Lasm_Call.assert_predicate_failure_41
+ cmp x2, x5
b.hi .Lasm_Call.ssa_block_1
mov x5, x20
ldr x6, [x5, #40]
mov x5, x6
add x5, x5, #8, lsl #12
cmp x5, x6
- b.lo .Lasm_Call.assert_predicate_failure_109
+ b.lo .Lasm_Call.assert_predicate_failure_100
cmp x5, x29
b.hi .Lasm_Call.ssa_block_1
mov x5, x20
- str x3, [x5, #16]
- stp w2, w2, [x0, #116]
- str w2, [x0, #104]
+ str x2, [x5, #16]
+ stp w3, w3, [x0, #116]
+ str w3, [x0, #104]
str x1, [x0]
- ldr x1, [x28, #32]
- cbz x1, .Lasm_Call.assert_predicate_failure_125
- str x1, [x0, #32]
- ldrb w1, [x21, #5]
- ldr x1, [x27, x1, lsl #3]
- mov w3, #0
- stp x1, x3, [x0, #80]
+ ldr x2, [x28, #32]
+ cbz x2, .Lasm_Call.assert_predicate_failure_116
+ str x2, [x0, #32]
+ ldrb w2, [x21, #5]
+ ldr x2, [x27, x2, lsl #3]
+ mov w5, #0
+ stp x2, x5, [x0, #80]
str wzr, [x0, #64]
str xzr, [x0, #56]
strb wzr, [x0, #78]
@@ -6537,42 +6527,51 @@ asm_handler_Call:
strb wzr, [x0, #124]
strb wzr, [x0, #125]
strb wzr, [x0, #126]
- movz x1, #0x7ffe, lsl #48
- str x1, [x0, #128]
- ldrb w3, [x21, #6]
- mov w1, #7
- adds x1, x1, x3
- ldrb w5, [x21, #3]
- sub w3, w21, w26
- adds x1, x1, x3
- str w3, [x28, #64]
+ movz x2, #0x7ffe, lsl #48
+ str x2, [x0, #128]
+ ldrb w5, [x21, #6]
+ mov w2, #7
+ adds x2, x2, x5
+ ldrb w6, [x21, #3]
+ sub w5, w21, w26
+ adds x2, x2, x5
+ str w5, [x28, #64]
str x28, [x0, #96]
- stp w1, w5, [x0, #108]
- add x3, x21, #7
- add x5, x28, #136
- add x6, x0, #136
- mov x1, #0
+ stp w2, w6, [x0, #108]
+ add x5, x21, #7
+ add x6, x28, #136
+ add x7, x0, #136
+ mov x2, #0
.Lasm_Call.ssa_block_12:
- cmp x1, x2
+ cmp x2, x3
b.hs .Lasm_Call.ssa_block_11
- ldrb w7, [x3, x1]
- ldr x7, [x5, x7, lsl #3]
- str x7, [x6, x1, lsl #3]
- add x1, x1, #1
+ ldrb w8, [x5, x2]
+ ldr x8, [x6, x8, lsl #3]
+ str x8, [x7, x2, lsl #3]
+ add x2, x2, #1
b .Lasm_Call.ssa_block_12
.Lasm_Call.ssa_block_11:
- mov x1, x20
- str x0, [x1]
+ mov x2, x20
+ str x0, [x2]
+ ldr x3, [x2, #208]
+ str x3, [x0, #8]
+ ldr w1, [x1, #20]
+ cbz x1, .Lasm_Call.assert_failure_8
+ add x1, x24, w1, uxtw #3
+ ldr w1, [x1, #12]
+ cbz x1, .Lasm_Call.assert_failure_9
+ add x1, x24, w1, uxtw #3
+ str x1, [x2, #208]
mov x28, x0
add x27, x28, #136
- ldr x0, [x1, #80]
+ ldr x0, [x2, #80]
cbz x0, .Lasm_Call.assert_failure_10
and w4, w4, #0xffff
lsl x4, x4, #4
adds x4, x4, x0
ldr w0, [x4, #8]
cmp x0, #1
- b.hs .Lasm_Call.assert_predicate_failure_194
+ b.hs .Lasm_Call.assert_predicate_failure_195
ldr x2, [x4]
cbz x2, .Lasm_Call.assert_failure_11
mov x9, x2
@@ -6580,12 +6579,14 @@ asm_handler_Call:
blr x9
and x1, x1, #0xff
cmp x1, #2
- b.hs .Lasm_Call.assert_predicate_failure_201
+ b.hs .Lasm_Call.assert_predicate_failure_202
cbnz x1, .Lasm_Call.ssa_block_6
ldr x1, [x28, #96]
- cbz x1, .Lasm_Call.assert_predicate_failure_207
+ cbz x1, .Lasm_Call.assert_predicate_failure_208
mov x2, x20
str x1, [x2]
+ ldr x3, [x28, #8]
+ str x3, [x2, #208]
str x28, [x2, #16]
mov x28, x1
add x27, x28, #136
@@ -14584,33 +14585,23 @@ asm_handler_GetById_cold:
ldr x10, [x19, x9, lsl #3]
br x10
.Lasm_GetById.ssa_block_25:
- ldr w0, [x1, #20]
- cbz x0, .Lasm_GetById.assert_failure_5
- add x0, x24, w0, uxtw #3
- ldr w0, [x0, #12]
- cbz x0, .Lasm_GetById.assert_failure_6
- add x0, x24, w0, uxtw #3
- mov x2, x20
- ldr x9, [x2, #208]
- cmp x9, x0
- b.ne .Lasm_GetById.ssa_block_3
ldr w2, [x1, #92]
mov x0, x20
ldr x0, [x0, #80]
- cbz x0, .Lasm_GetById.assert_failure_7
+ cbz x0, .Lasm_GetById.assert_failure_5
and w2, w2, #0xffff
lsl x2, x2, #4
adds x2, x2, x0
ldr w4, [x2, #8]
cmp x4, #1
- b.hs .Lasm_GetById.assert_predicate_failure_170
+ b.hs .Lasm_GetById.assert_predicate_failure_161
mov x0, x20
ldp x0, x6, [x0, #16]
- cbz x0, .Lasm_GetById.assert_failure_8
- cbz x6, .Lasm_GetById.assert_failure_9
+ cbz x0, .Lasm_GetById.assert_failure_6
+ cbz x6, .Lasm_GetById.assert_failure_7
add x5, x0, #136
cmp x5, x0
- b.lo .Lasm_GetById.assert_predicate_failure_179
+ b.lo .Lasm_GetById.assert_predicate_failure_170
cmp x5, x6
b.hi .Lasm_GetById.ssa_block_3
mov x6, x20
@@ -14618,7 +14609,7 @@ asm_handler_GetById_cold:
mov x6, x7
add x6, x6, #8, lsl #12
cmp x6, x7
- b.lo .Lasm_GetById.assert_predicate_failure_187
+ b.lo .Lasm_GetById.assert_predicate_failure_178
cmp x6, x29
b.hi .Lasm_GetById.ssa_block_3
mov x6, x20
@@ -14626,14 +14617,14 @@ asm_handler_GetById_cold:
stp w4, w4, [x0, #116]
str wzr, [x0, #104]
str x1, [x0]
- ldr x1, [x28, #32]
- cbz x1, .Lasm_GetById.assert_predicate_failure_204
- str x1, [x0, #32]
+ ldr x4, [x28, #32]
+ cbz x4, .Lasm_GetById.assert_predicate_failure_195
+ str x4, [x0, #32]
and x3, x3, #0x7ffffffff
- movz x1, #0xfff9, lsl #48
- orr x3, x3, x1
- mov w1, #0
- stp x3, x1, [x0, #80]
+ movz x4, #0xfff9, lsl #48
+ orr x3, x3, x4
+ mov w4, #0
+ stp x3, x4, [x0, #80]
str wzr, [x0, #64]
str xzr, [x0, #56]
strb wzr, [x0, #78]
@@ -14641,16 +14632,25 @@ asm_handler_GetById_cold:
strb wzr, [x0, #124]
strb wzr, [x0, #125]
strb wzr, [x0, #126]
- movz x1, #0x7ffe, lsl #48
- str x1, [x0, #128]
- sub w1, w21, w26
- ldrb w3, [x21, #1]
- str w1, [x28, #64]
+ movz x3, #0x7ffe, lsl #48
+ str x3, [x0, #128]
+ sub w3, w21, w26
+ ldrb w4, [x21, #1]
+ str w3, [x28, #64]
str x28, [x0, #96]
- add x1, x1, #1
- stp w1, w3, [x0, #108]
- mov x1, x20
- str x0, [x1]
+ add x3, x3, #1
+ stp w3, w4, [x0, #108]
+ mov x3, x20
+ str x0, [x3]
+ ldr x4, [x3, #208]
+ str x4, [x0, #8]
+ ldr w1, [x1, #20]
+ cbz x1, .Lasm_GetById.assert_failure_8
+ add x1, x24, w1, uxtw #3
+ ldr w1, [x1, #12]
+ cbz x1, .Lasm_GetById.assert_failure_9
+ add x1, x24, w1, uxtw #3
+ str x1, [x3, #208]
mov x28, x0
add x27, x28, #136
ldr x2, [x2]
@@ -14660,12 +14660,14 @@ asm_handler_GetById_cold:
blr x9
and x1, x1, #0xff
cmp x1, #2
- b.hs .Lasm_GetById.assert_predicate_failure_258
+ b.hs .Lasm_GetById.assert_predicate_failure_259
cbnz x1, .Lasm_GetById.ssa_block_6
ldr x1, [x28, #96]
- cbz x1, .Lasm_GetById.assert_predicate_failure_267
+ cbz x1, .Lasm_GetById.assert_predicate_failure_268
mov x2, x20
str x1, [x2]
+ ldr x3, [x28, #8]
+ str x3, [x2, #208]
str x28, [x2, #16]
mov x28, x1
add x27, x28, #136
@@ -15864,10 +15866,10 @@ asm_handler_Call_cold:
1:
mov x0, x20
ldr x28, [x0]
- cbz x28, .Lasm_Call.assert_predicate_failure_91
+ cbz x28, .Lasm_Call.assert_predicate_failure_82
add x27, x28, #136
ldr x0, [x28, #88]
- cbz x0, .Lasm_Call.assert_predicate_failure_97
+ cbz x0, .Lasm_Call.assert_predicate_failure_88
ldr x26, [x0, #40]
cbz x26, .Lasm_Call.assert_failure_25
ldr w25, [x28, #64]
@@ -21545,7 +21547,7 @@ asm_assertion_failure_traps:
brk #0
.Lasm_GetById.assert_failure_1:
brk #0
-.Lasm_GetById.assert_predicate_failure_328:
+.Lasm_GetById.assert_predicate_failure_333:
brk #0
.Lasm_GetById.assert_failure_2:
brk #0
@@ -21555,27 +21557,27 @@ asm_assertion_failure_traps:
brk #0
.Lasm_GetById.assert_failure_5:
brk #0
+.Lasm_GetById.assert_predicate_failure_161:
+ brk #0
.Lasm_GetById.assert_failure_6:
brk #0
.Lasm_GetById.assert_failure_7:
brk #0
.Lasm_GetById.assert_predicate_failure_170:
brk #0
-.Lasm_GetById.assert_failure_8:
- brk #0
-.Lasm_GetById.assert_failure_9:
+.Lasm_GetById.assert_predicate_failure_178:
brk #0
-.Lasm_GetById.assert_predicate_failure_179:
+.Lasm_GetById.assert_predicate_failure_195:
brk #0
-.Lasm_GetById.assert_predicate_failure_187:
+.Lasm_GetById.assert_failure_8:
brk #0
-.Lasm_GetById.assert_predicate_failure_204:
+.Lasm_GetById.assert_failure_9:
brk #0
.Lasm_GetById.assert_failure_10:
brk #0
-.Lasm_GetById.assert_predicate_failure_258:
+.Lasm_GetById.assert_predicate_failure_259:
brk #0
-.Lasm_GetById.assert_predicate_failure_267:
+.Lasm_GetById.assert_predicate_failure_268:
brk #0
.Lasm_GetById.assert_predicate_failure_26:
brk #0
@@ -21687,21 +21689,21 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Call.assert_failure_2:
brk #0
-.Lasm_Call.assert_predicate_failure_230:
+.Lasm_Call.assert_predicate_failure_235:
brk #0
-.Lasm_Call.assert_predicate_failure_236:
+.Lasm_Call.assert_predicate_failure_241:
brk #0
.Lasm_Call.assert_failure_3:
brk #0
.Lasm_Call.assert_failure_4:
brk #0
-.Lasm_Call.assert_predicate_failure_250:
+.Lasm_Call.assert_predicate_failure_255:
brk #0
-.Lasm_Call.assert_predicate_failure_271:
+.Lasm_Call.assert_predicate_failure_276:
brk #0
-.Lasm_Call.assert_predicate_failure_327:
+.Lasm_Call.assert_predicate_failure_332:
brk #0
-.Lasm_Call.assert_predicate_failure_349:
+.Lasm_Call.assert_predicate_failure_354:
brk #0
.Lasm_Call.assert_failure_5:
brk #0
@@ -21709,25 +21711,25 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Call.assert_failure_7:
brk #0
-.Lasm_Call.assert_failure_8:
+.Lasm_Call.assert_predicate_failure_41:
brk #0
-.Lasm_Call.assert_failure_9:
+.Lasm_Call.assert_predicate_failure_100:
brk #0
-.Lasm_Call.assert_predicate_failure_50:
+.Lasm_Call.assert_predicate_failure_116:
brk #0
-.Lasm_Call.assert_predicate_failure_109:
+.Lasm_Call.assert_failure_8:
brk #0
-.Lasm_Call.assert_predicate_failure_125:
+.Lasm_Call.assert_failure_9:
brk #0
.Lasm_Call.assert_failure_10:
brk #0
-.Lasm_Call.assert_predicate_failure_194:
+.Lasm_Call.assert_predicate_failure_195:
brk #0
.Lasm_Call.assert_failure_11:
brk #0
-.Lasm_Call.assert_predicate_failure_201:
+.Lasm_Call.assert_predicate_failure_202:
brk #0
-.Lasm_Call.assert_predicate_failure_207:
+.Lasm_Call.assert_predicate_failure_208:
brk #0
.Lasm_Call.assert_predicate_failure_11:
brk #0
@@ -21735,9 +21737,9 @@ asm_assertion_failure_traps:
brk #0
.Lasm_Call.assert_failure_24:
brk #0
-.Lasm_Call.assert_predicate_failure_91:
+.Lasm_Call.assert_predicate_failure_82:
brk #0
-.Lasm_Call.assert_predicate_failure_97:
+.Lasm_Call.assert_predicate_failure_88:
brk #0
.Lasm_Call.assert_failure_25:
brk #0
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index f11dc22bef1..2d3c754a2eb 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -5575,7 +5575,7 @@ asm_handler_GetById:
jz .Lasm_GetById.ssa_block_1
and rdx, -4
test rdx, rdx
- jz .Lasm_GetById.assert_predicate_failure_328
+ jz .Lasm_GetById.assert_predicate_failure_333
cmp QWORD PTR [rdx + 24], rax
jne .Lasm_GetById.ssa_block_15
mov ecx, DWORD PTR [rdx]
@@ -6444,11 +6444,11 @@ asm_handler_Call:
mov rdx, rdi
.Lasm_Call.ssa_block_17:
test rsi, rsi
- jz .Lasm_Call.assert_predicate_failure_230
+ jz .Lasm_Call.assert_predicate_failure_235
mov eax, DWORD PTR [rsi + 12]
mov edi, DWORD PTR [rsi + 16]
cmp rdi, rax
- jb .Lasm_Call.assert_predicate_failure_236
+ jb .Lasm_Call.assert_predicate_failure_241
mov eax, 4294967295
sub rax, rdi
cmp rax, rdx
@@ -6467,7 +6467,7 @@ asm_handler_Call:
lea r8, [r8 + 136]
add r8, rax
cmp r8, rax
- jb .Lasm_Call.assert_predicate_failure_250
+ jb .Lasm_Call.assert_predicate_failure_255
cmp r8, r11
ja .Lasm_Call.ssa_block_1
mov r11, QWORD PTR [rbp - 48]
@@ -6482,7 +6482,7 @@ asm_handler_Call:
test ecx, ecx
cmovnz rcx, r11
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_271
+ jz .Lasm_Call.assert_predicate_failure_276
mov QWORD PTR [rax + 32], rcx
mov QWORD PTR [rax + 80], r9
mov QWORD PTR [rax + 88], rsi
@@ -6514,7 +6514,7 @@ asm_handler_Call:
mov DWORD PTR [rax + 112], r8d
movzx esi, BYTE PTR [r14 + r13 + 6]
cmp rdx, rsi
- jb .Lasm_Call.assert_predicate_failure_327
+ jb .Lasm_Call.assert_predicate_failure_332
lea r9, [r14 + r13 + 7]
lea r8, [rax + 136]
lea r10, [rbx]
@@ -6541,7 +6541,7 @@ asm_handler_Call:
.Lasm_Call.ssa_block_24:
mov rcx, QWORD PTR [rax + 88]
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_349
+ jz .Lasm_Call.assert_predicate_failure_354
mov r14, QWORD PTR [rcx + 40]
test r14, r14
jz .Lasm_Call.assert_failure_5
@@ -6555,56 +6555,45 @@ asm_handler_Call:
test eax, 2
jz .Lasm_Call.ssa_block_1
mov edi, DWORD PTR [rcx + 92]
- mov eax, DWORD PTR [rcx + 20]
- test rax, rax
- jz .Lasm_Call.assert_failure_6
- lea rax, [r15 + rax * 8]
- mov eax, DWORD PTR [rax + 12]
- test rax, rax
- jz .Lasm_Call.assert_failure_7
- lea rax, [r15 + rax * 8]
- mov rdx, QWORD PTR [rbp - 48]
- cmp QWORD PTR [rdx + 208], rax
- jne .Lasm_Call.ssa_block_1
- movzx edx, BYTE PTR [r14 + r13 + 6]
+ movzx esi, BYTE PTR [r14 + r13 + 6]
mov rax, QWORD PTR [rbp - 48]
mov r8, QWORD PTR [rax + 24]
mov rax, QWORD PTR [rax + 16]
test rax, rax
- jz .Lasm_Call.assert_failure_8
+ jz .Lasm_Call.assert_failure_6
test r8, r8
- jz .Lasm_Call.assert_failure_9
- mov rsi, rdx
- shl rsi, 3
- lea rsi, [rsi + 136]
- add rsi, rax
- cmp rsi, rax
- jb .Lasm_Call.assert_predicate_failure_50
- cmp rsi, r8
+ jz .Lasm_Call.assert_failure_7
+ mov rdx, rsi
+ shl rdx, 3
+ lea rdx, [rdx + 136]
+ add rdx, rax
+ cmp rdx, rax
+ jb .Lasm_Call.assert_predicate_failure_41
+ cmp rdx, r8
ja .Lasm_Call.ssa_block_1
mov r8, QWORD PTR [rbp - 48]
mov r9, QWORD PTR [r8 + 40]
mov r8, r9
add r8, 32768
cmp r8, r9
- jb .Lasm_Call.assert_predicate_failure_109
+ jb .Lasm_Call.assert_predicate_failure_100
cmp r8, rbp
ja .Lasm_Call.ssa_block_1
mov r8, QWORD PTR [rbp - 48]
- mov QWORD PTR [r8 + 16], rsi
- mov DWORD PTR [rax + 116], edx
- mov DWORD PTR [rax + 120], edx
- mov DWORD PTR [rax + 104], edx
+ mov QWORD PTR [r8 + 16], rdx
+ mov DWORD PTR [rax + 116], esi
+ mov DWORD PTR [rax + 120], esi
+ mov DWORD PTR [rax + 104], esi
mov QWORD PTR [rax], rcx
- mov rcx, QWORD PTR [rbx - 104]
- test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_125
- mov QWORD PTR [rax + 32], rcx
- movzx ecx, BYTE PTR [r14 + r13 + 5]
- mov rcx, QWORD PTR [rbx + rcx * 8]
- mov rsi, 0
- mov QWORD PTR [rax + 80], rcx
- mov QWORD PTR [rax + 88], rsi
+ mov rdx, QWORD PTR [rbx - 104]
+ test rdx, rdx
+ jz .Lasm_Call.assert_predicate_failure_116
+ mov QWORD PTR [rax + 32], rdx
+ movzx edx, BYTE PTR [r14 + r13 + 5]
+ mov rdx, QWORD PTR [rbx + rdx * 8]
+ mov r8, 0
+ mov QWORD PTR [rax + 80], rdx
+ mov QWORD PTR [rax + 88], r8
mov DWORD PTR [rax + 64], 0
mov QWORD PTR [rax + 56], 0
mov BYTE PTR [rax + 78], 0
@@ -6612,36 +6601,47 @@ asm_handler_Call:
mov BYTE PTR [rax + 124], 0
mov BYTE PTR [rax + 125], 0
mov BYTE PTR [rax + 126], 0
- movabs rcx, 9222809086901354496
- mov QWORD PTR [rax + 128], rcx
- movzx esi, BYTE PTR [r14 + r13 + 6]
- mov rcx, 7
- add rcx, rsi
- movzx r8d, BYTE PTR [r14 + r13 + 3]
- mov esi, r13d
- add rcx, rsi
- mov DWORD PTR [rbx - 72], esi
+ movabs rdx, 9222809086901354496
+ mov QWORD PTR [rax + 128], rdx
+ movzx r8d, BYTE PTR [r14 + r13 + 6]
+ mov rdx, 7
+ add rdx, r8
+ movzx r9d, BYTE PTR [r14 + r13 + 3]
+ mov r8d, r13d
+ add rdx, r8
+ mov DWORD PTR [rbx - 72], r8d
lea r11, [rbx - 136]
mov QWORD PTR [rax + 96], r11
- mov DWORD PTR [rax + 108], ecx
- mov DWORD PTR [rax + 112], r8d
- lea rsi, [r14 + r13 + 7]
- lea r8, [rbx]
- lea r9, [rax + 136]
- xor rcx, rcx
+ mov DWORD PTR [rax + 108], edx
+ mov DWORD PTR [rax + 112], r9d
+ lea r8, [r14 + r13 + 7]
+ lea r9, [rbx]
+ lea r10, [rax + 136]
+ xor rdx, rdx
.Lasm_Call.ssa_block_12:
- cmp rcx, rdx
+ cmp rdx, rsi
jae .Lasm_Call.ssa_block_11
- movzx r10d, BYTE PTR [rsi + rcx]
- mov r10, QWORD PTR [r8 + r10 * 8]
- mov QWORD PTR [r9 + rcx * 8], r10
- lea rcx, [rcx + 1]
+ movzx r11d, BYTE PTR [r8 + rdx]
+ mov r11, QWORD PTR [r9 + r11 * 8]
+ mov QWORD PTR [r10 + rdx * 8], r11
+ lea rdx, [rdx + 1]
jmp .Lasm_Call.ssa_block_12
.Lasm_Call.ssa_block_11:
- mov rcx, QWORD PTR [rbp - 48]
- mov QWORD PTR [rcx], rax
+ mov rdx, QWORD PTR [rbp - 48]
+ mov QWORD PTR [rdx], rax
+ mov rsi, QWORD PTR [rdx + 208]
+ mov QWORD PTR [rax + 8], rsi
+ mov ecx, DWORD PTR [rcx + 20]
+ test rcx, rcx
+ jz .Lasm_Call.assert_failure_8
+ lea rcx, [r15 + rcx * 8]
+ mov ecx, DWORD PTR [rcx + 12]
+ test rcx, rcx
+ jz .Lasm_Call.assert_failure_9
+ lea rcx, [r15 + rcx * 8]
+ mov QWORD PTR [rdx + 208], rcx
lea rbx, [rax + 136]
- mov rax, QWORD PTR [rcx + 80]
+ mov rax, QWORD PTR [rdx + 80]
test rax, rax
jz .Lasm_Call.assert_failure_10
and edi, 65535
@@ -6649,7 +6649,7 @@ asm_handler_Call:
add rdi, rax
mov eax, DWORD PTR [rdi + 8]
cmp rax, 1
- jae .Lasm_Call.assert_predicate_failure_194
+ jae .Lasm_Call.assert_predicate_failure_195
mov rdx, QWORD PTR [rdi]
test rdx, rdx
jz .Lasm_Call.assert_failure_11
@@ -6659,14 +6659,16 @@ asm_handler_Call:
mov rcx, rdx
and rcx, 255
cmp rcx, 2
- jae .Lasm_Call.assert_predicate_failure_201
+ jae .Lasm_Call.assert_predicate_failure_202
test rcx, rcx
jnz .Lasm_Call.ssa_block_6
mov rcx, QWORD PTR [rbx - 40]
test rcx, rcx
- jz .Lasm_Call.assert_predicate_failure_207
+ jz .Lasm_Call.assert_predicate_failure_208
mov rdx, QWORD PTR [rbp - 48]
mov QWORD PTR [rdx], rcx
+ mov rsi, QWORD PTR [rbx - 128]
+ mov QWORD PTR [rdx + 208], rsi
lea r11, [rbx - 136]
mov QWORD PTR [rdx + 16], r11
lea rbx, [rcx + 136]
@@ -14177,38 +14179,27 @@ asm_handler_GetById_cold:
movzx eax, BYTE PTR [r14 + r13]
jmp [r12 + rax * 8]
.Lasm_GetById.ssa_block_25:
- mov eax, DWORD PTR [rcx + 20]
- test rax, rax
- jz .Lasm_GetById.assert_failure_5
- lea rax, [r15 + rax * 8]
- mov eax, DWORD PTR [rax + 12]
- test rax, rax
- jz .Lasm_GetById.assert_failure_6
- lea rax, [r15 + rax * 8]
- mov rdx, QWORD PTR [rbp - 48]
- cmp QWORD PTR [rdx + 208], rax
- jne .Lasm_GetById.ssa_block_3
mov edx, DWORD PTR [rcx + 92]
mov rax, QWORD PTR [rbp - 48]
mov rax, QWORD PTR [rax + 80]
test rax, rax
- jz .Lasm_GetById.assert_failure_7
+ jz .Lasm_GetById.assert_failure_5
and edx, 65535
shl rdx, 4
add rdx, rax
mov edi, DWORD PTR [rdx + 8]
cmp rdi, 1
- jae .Lasm_GetById.assert_predicate_failure_170
+ jae .Lasm_GetById.assert_predicate_failure_161
mov rax, QWORD PTR [rbp - 48]
mov r9, QWORD PTR [rax + 24]
mov rax, QWORD PTR [rax + 16]
test rax, rax
- jz .Lasm_GetById.assert_failure_8
+ jz .Lasm_GetById.assert_failure_6
test r9, r9
- jz .Lasm_GetById.assert_failure_9
+ jz .Lasm_GetById.assert_failure_7
lea r8, [rax + 136]
cmp r8, rax
- jb .Lasm_GetById.assert_predicate_failure_179
+ jb .Lasm_GetById.assert_predicate_failure_170
cmp r8, r9
ja .Lasm_GetById.ssa_block_3
mov r9, QWORD PTR [rbp - 48]
@@ -14216,7 +14207,7 @@ asm_handler_GetById_cold:
mov r9, r10
add r9, 32768
cmp r9, r10
- jb .Lasm_GetById.assert_predicate_failure_187
+ jb .Lasm_GetById.assert_predicate_failure_178
cmp r9, rbp
ja .Lasm_GetById.ssa_block_3
mov r9, QWORD PTR [rbp - 48]
@@ -14225,17 +14216,17 @@ asm_handler_GetById_cold:
mov DWORD PTR [rax + 120], edi
mov DWORD PTR [rax + 104], 0
mov QWORD PTR [rax], rcx
- mov rcx, QWORD PTR [rbx - 104]
- test rcx, rcx
- jz .Lasm_GetById.assert_predicate_failure_204
- mov QWORD PTR [rax + 32], rcx
- movabs rcx, 34359738367
- and rsi, rcx
- movabs rcx, -1970324836974592
- or rsi, rcx
- mov rcx, 0
+ mov rdi, QWORD PTR [rbx - 104]
+ test rdi, rdi
+ jz .Lasm_GetById.assert_predicate_failure_195
+ mov QWORD PTR [rax + 32], rdi
+ movabs rdi, 34359738367
+ and rsi, rdi
+ movabs rdi, -1970324836974592
+ or rsi, rdi
+ mov rdi, 0
mov QWORD PTR [rax + 80], rsi
- mov QWORD PTR [rax + 88], rcx
+ mov QWORD PTR [rax + 88], rdi
mov DWORD PTR [rax + 64], 0
mov QWORD PTR [rax + 56], 0
mov BYTE PTR [rax + 78], 0
@@ -14243,18 +14234,29 @@ asm_handler_GetById_cold:
mov BYTE PTR [rax + 124], 0
mov BYTE PTR [rax + 125], 0
mov BYTE PTR [rax + 126], 0
- movabs rcx, 9222809086901354496
- mov QWORD PTR [rax + 128], rcx
- mov ecx, r13d
- movzx esi, BYTE PTR [r14 + r13 + 1]
- mov DWORD PTR [rbx - 72], ecx
+ movabs rsi, 9222809086901354496
+ mov QWORD PTR [rax + 128], rsi
+ mov esi, r13d
+ movzx edi, BYTE PTR [r14 + r13 + 1]
+ mov DWORD PTR [rbx - 72], esi
lea r11, [rbx - 136]
mov QWORD PTR [rax + 96], r11
- lea rcx, [rcx + 1]
- mov DWORD PTR [rax + 108], ecx
- mov DWORD PTR [rax + 112], esi
- mov rcx, QWORD PTR [rbp - 48]
- mov QWORD PTR [rcx], rax
+ lea rsi, [rsi + 1]
+ mov DWORD PTR [rax + 108], esi
+ mov DWORD PTR [rax + 112], edi
+ mov rsi, QWORD PTR [rbp - 48]
+ mov QWORD PTR [rsi], rax
+ mov rdi, QWORD PTR [rsi + 208]
+ mov QWORD PTR [rax + 8], rdi
+ mov ecx, DWORD PTR [rcx + 20]
+ test rcx, rcx
+ jz .Lasm_GetById.assert_failure_8
+ lea rcx, [r15 + rcx * 8]
+ mov ecx, DWORD PTR [rcx + 12]
+ test rcx, rcx
+ jz .Lasm_GetById.assert_failure_9
+ lea rcx, [r15 + rcx * 8]
+ mov QWORD PTR [rsi + 208], rcx
lea rbx, [rax + 136]
mov rdx, QWORD PTR [rdx]
test rdx, rdx
@@ -14265,14 +14267,16 @@ asm_handler_GetById_cold:
mov rcx, rdx
and rcx, 255
cmp rcx, 2
- jae .Lasm_GetById.assert_predicate_failure_258
+ jae .Lasm_GetById.assert_predicate_failure_259
test rcx, rcx
jnz .Lasm_GetById.ssa_block_6
mov rcx, QWORD PTR [rbx - 40]
test rcx, rcx
- jz .Lasm_GetById.assert_predicate_failure_267
+ jz .Lasm_GetById.assert_predicate_failure_268
mov rdx, QWORD PTR [rbp - 48]
mov QWORD PTR [rdx], rcx
+ mov rsi, QWORD PTR [rbx - 128]
+ mov QWORD PTR [rdx + 208], rsi
lea r11, [rbx - 136]
mov QWORD PTR [rdx + 16], r11
lea rbx, [rcx + 136]
@@ -15428,10 +15432,10 @@ asm_handler_Call_cold:
mov rbx, QWORD PTR [rax]
add rbx, 136
test rbx, rbx
- jz .Lasm_Call.assert_predicate_failure_91
+ jz .Lasm_Call.assert_predicate_failure_82
mov rax, QWORD PTR [rbx - 48]
test rax, rax
- jz .Lasm_Call.assert_predicate_failure_97
+ jz .Lasm_Call.assert_predicate_failure_88
mov r14, QWORD PTR [rax + 40]
test r14, r14
jz .Lasm_Call.assert_failure_25
@@ -20923,7 +20927,7 @@ asm_assertion_failure_traps:
ud2
.Lasm_GetById.assert_failure_1:
ud2
-.Lasm_GetById.assert_predicate_failure_328:
+.Lasm_GetById.assert_predicate_failure_333:
ud2
.Lasm_GetById.assert_failure_2:
ud2
@@ -20933,27 +20937,27 @@ asm_assertion_failure_traps:
ud2
.Lasm_GetById.assert_failure_5:
ud2
+.Lasm_GetById.assert_predicate_failure_161:
+ ud2
.Lasm_GetById.assert_failure_6:
ud2
.Lasm_GetById.assert_failure_7:
ud2
.Lasm_GetById.assert_predicate_failure_170:
ud2
-.Lasm_GetById.assert_failure_8:
- ud2
-.Lasm_GetById.assert_failure_9:
+.Lasm_GetById.assert_predicate_failure_178:
ud2
-.Lasm_GetById.assert_predicate_failure_179:
+.Lasm_GetById.assert_predicate_failure_195:
ud2
-.Lasm_GetById.assert_predicate_failure_187:
+.Lasm_GetById.assert_failure_8:
ud2
-.Lasm_GetById.assert_predicate_failure_204:
+.Lasm_GetById.assert_failure_9:
ud2
.Lasm_GetById.assert_failure_10:
ud2
-.Lasm_GetById.assert_predicate_failure_258:
+.Lasm_GetById.assert_predicate_failure_259:
ud2
-.Lasm_GetById.assert_predicate_failure_267:
+.Lasm_GetById.assert_predicate_failure_268:
ud2
.Lasm_GetById.assert_predicate_failure_26:
ud2
@@ -21065,21 +21069,21 @@ asm_assertion_failure_traps:
ud2
.Lasm_Call.assert_failure_2:
ud2
-.Lasm_Call.assert_predicate_failure_230:
+.Lasm_Call.assert_predicate_failure_235:
ud2
-.Lasm_Call.assert_predicate_failure_236:
+.Lasm_Call.assert_predicate_failure_241:
ud2
.Lasm_Call.assert_failure_3:
ud2
.Lasm_Call.assert_failure_4:
ud2
-.Lasm_Call.assert_predicate_failure_250:
+.Lasm_Call.assert_predicate_failure_255:
ud2
-.Lasm_Call.assert_predicate_failure_271:
+.Lasm_Call.assert_predicate_failure_276:
ud2
-.Lasm_Call.assert_predicate_failure_327:
+.Lasm_Call.assert_predicate_failure_332:
ud2
-.Lasm_Call.assert_predicate_failure_349:
+.Lasm_Call.assert_predicate_failure_354:
ud2
.Lasm_Call.assert_failure_5:
ud2
@@ -21087,25 +21091,25 @@ asm_assertion_failure_traps:
ud2
.Lasm_Call.assert_failure_7:
ud2
-.Lasm_Call.assert_failure_8:
+.Lasm_Call.assert_predicate_failure_41:
ud2
-.Lasm_Call.assert_failure_9:
+.Lasm_Call.assert_predicate_failure_100:
ud2
-.Lasm_Call.assert_predicate_failure_50:
+.Lasm_Call.assert_predicate_failure_116:
ud2
-.Lasm_Call.assert_predicate_failure_109:
+.Lasm_Call.assert_failure_8:
ud2
-.Lasm_Call.assert_predicate_failure_125:
+.Lasm_Call.assert_failure_9:
ud2
.Lasm_Call.assert_failure_10:
ud2
-.Lasm_Call.assert_predicate_failure_194:
+.Lasm_Call.assert_predicate_failure_195:
ud2
.Lasm_Call.assert_failure_11:
ud2
-.Lasm_Call.assert_predicate_failure_201:
+.Lasm_Call.assert_predicate_failure_202:
ud2
-.Lasm_Call.assert_predicate_failure_207:
+.Lasm_Call.assert_predicate_failure_208:
ud2
.Lasm_Call.assert_predicate_failure_11:
ud2
@@ -21113,9 +21117,9 @@ asm_assertion_failure_traps:
ud2
.Lasm_Call.assert_failure_24:
ud2
-.Lasm_Call.assert_predicate_failure_91:
+.Lasm_Call.assert_predicate_failure_82:
ud2
-.Lasm_Call.assert_predicate_failure_97:
+.Lasm_Call.assert_predicate_failure_88:
ud2
.Lasm_Call.assert_failure_25:
ud2
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index 67a190df72c..b2c6b300849 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -1323,7 +1323,8 @@ inline fn coerce_this() {
}
# NB: Sloppy functions that use their this value start with this. Null and undefined become the global this value of
-# the current realm, which is the function's, as a call into another realm goes through the runtime.
+# the current realm, which is the function's, as a call of an ECMAScript function of another realm goes through
+# the runtime.
handler EnterCoercingThis() {
let this_value = exec_ctx.this_value_register;
if this_value is not Value<Object> {
@@ -3348,11 +3349,6 @@ handler GetById(
goto invoke_accessor_slow();
}
- # NB: A getter of another realm is the runtime's, which changes the
- # current realm.
- let getter_realm = function.shape.realm;
- let getter_realm_vm = current_vm();
- guard getter_realm == getter_realm_vm.current_realm else invoke_accessor_slow;
let raw_native_function: RawNativeFunction = alias(function);
let native_function_index = raw_native_function.native_function_index;
let native_table_vm = current_vm();
@@ -3404,6 +3400,9 @@ handler GetById(
native_frame.caller_dst_raw = destination_offset;
let native_running_vm = current_vm();
native_running_vm.running_execution_context = native_frame;
+ # The getter runs in its own realm, as in call_raw_native_function().
+ native_frame.realm_without_function = native_running_vm.current_realm;
+ native_running_vm.current_realm = function.shape.realm;
exec_ctx = native_frame;
values = exec_ctx.slots;
let native_function = native_function_entry.function;
@@ -3436,6 +3435,7 @@ handler GetById(
assert(caller_frame != 0);
let return_vm = current_vm();
return_vm.running_execution_context = caller_frame;
+ return_vm.current_realm = exec_ctx.realm_without_function;
return_vm.interpreter_stack_top = alias(exec_ctx);
exec_ctx = caller_frame;
values = exec_ctx.slots;
@@ -4204,6 +4204,11 @@ inline fn call_raw_native_function(
}
let native_running_vm = current_vm();
native_running_vm.running_execution_context = native_frame;
+ # The function runs in its own realm, which may be another one. The
+ # frame keeps the caller's realm, which the return makes current again,
+ # as does the runtime when it unwinds the frame of a function that threw.
+ native_frame.realm_without_function = native_running_vm.current_realm;
+ native_running_vm.current_realm = function.shape.realm;
exec_ctx = native_frame;
values = exec_ctx.slots;
let native_function_entry = &native_running_vm.native_function_table[native_function_index & NATIVE_FUNCTION_TABLE_INDEX_MASK];
@@ -4242,6 +4247,7 @@ inline fn call_raw_native_function(
assert(caller_frame != 0);
let return_vm = current_vm();
return_vm.running_execution_context = caller_frame;
+ return_vm.current_realm = exec_ctx.realm_without_function;
return_vm.interpreter_stack_top = alias(exec_ctx);
exec_ctx = caller_frame;
values = exec_ctx.slots;
@@ -4332,8 +4338,9 @@ handler Call(
let metadata = shared_data.asm_call_metadata;
guard metadata has SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA_CAN_INLINE_CALL else slow;
guard metadata lacks SHARED_FUNCTION_INSTANCE_DATA_ASM_CALL_METADATA_NEEDS_ENVIRONMENT_OR_THIS_VALUE_RESOLUTION else invoke_interp_inline;
- # NB: A call into another realm is the runtime's, which changes the
- # current realm. A site's call cache is never linked to such a callee.
+ # NB: A call of an ECMAScript function of another realm is the runtime's,
+ # which changes the current realm. A site's call cache is never linked
+ # to such a callee.
let callee_realm = function.shape.realm;
let realm_vm = current_vm();
guard callee_realm == realm_vm.current_realm else slow;
@@ -4361,9 +4368,6 @@ handler Call(
let raw_native_function: RawNativeFunction = alias(function);
let native_function_index = raw_native_function.native_function_index;
let native_target: u64 = (alias(native_function_index) << CALL_CACHE_TARGET_INDEX_SHIFT) | CALL_CACHE_TARGET_NATIVE_FUNCTION;
- let native_realm = function.shape.realm;
- let native_realm_vm = current_vm();
- guard native_realm == native_realm_vm.current_realm else slow;
link_call_cache(call_feedback, callee, native_target, call_site_flags);
let argument_operand_indices = &arguments;
call_raw_native_function(function, native_function_index, call_feedback, dst, this_value, argument_count, argument_operand_indices) else slow;
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
index bfd08922734..1e729b8b606 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/inlining.rs
@@ -674,11 +674,7 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
unreachable!("calls are generic instructions");
};
let slots_offset = u64::from(self.runtime.offsets.execution_context_slots);
- // NB: Calls into another realm are the runtime's (see `RuntimeOffsets::vm_current_realm`).
- if let Some(native) = feedback
- .native_call
- .filter(|native| native.entry.is_some() && native.realm == self.runtime.realm)
- {
+ if let Some(native) = feedback.native_call.filter(|native| native.entry.is_some()) {
// NB: Native calls take the callee, the `this` value and the
// arguments as inputs like direct calls do.
let mut inputs = ZoneVec::with_capacity_in(arguments.len() + 2, self.work);
@@ -693,6 +689,8 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
self.reserve_call_frame(slots_offset + 8 * u64::from(*argument_count));
self.embed(native.function);
self.embed(native.realm);
+ // NB: A call of a function of another realm makes this code's realm current again when it returns.
+ self.embed(self.runtime.realm);
if let Some(inlined_frame_bytes) = without_frames {
self.check_callee(*callee, native.function)?;
let op = Op::CallNative {
@@ -787,7 +785,7 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
let Handling::Generic(info) = handling(OpCode::Call) else {
unreachable!("calls are generic instructions");
};
- // NB: Calls into another realm are the runtime's (see `RuntimeOffsets::vm_current_realm`).
+ // NB: Calls of ECMAScript functions of another realm are the runtime's (see `RuntimeOffsets::vm_current_realm`).
if target.function.realm != self.runtime.realm {
return Ok(None);
}
@@ -2208,7 +2206,7 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
) -> Option<InlinableCallee<'a>> {
let snapshot = self.snapshot;
let callee_executable = snapshot.executables.get(index as usize)?;
- // NB: Calls into another realm are the runtime's, which changes the current realm (see
+ // NB: Calls of ECMAScript functions of another realm are the runtime's, which changes the current realm (see
// `RuntimeOffsets::vm_current_realm`).
if callee_executable.function?.realm != self.runtime.realm {
return None;
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
index 5b40cdcea0e..9f1c5de6556 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/property_access.rs
@@ -435,10 +435,6 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
let first = entries[0];
let accessor = first.accessor_function?;
let mut target = accessor.native?;
- // NB: Calls into another realm are the runtime's (see `RuntimeOffsets::vm_current_realm`).
- if target.realm != self.runtime.realm {
- return None;
- }
let location = first.accessor_location(part)?;
let same_accessor = entries.iter().all(|entry| {
entry.holds_accessor
@@ -477,6 +473,8 @@ impl<'a, 'z> GraphBuilder<'a, 'z> {
self.reserve_call_frame(slots_offset + 8 * argument_count);
self.embed(target.function);
self.embed(target.realm);
+ // NB: A call of a function of another realm makes this code's realm current again when it returns.
+ self.embed(self.runtime.realm);
let op = Op::CallNativeAccessor {
pc: self.function.pc,
part,
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
index ef25667d38c..c1ca7e924c9 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
@@ -1475,6 +1475,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
),
]);
constants.push((offsets.execution_context_function, 8, target.function.0));
+ // NB: The frame keeps the realm of this code, which the runtime makes
+ // current again when it unwinds the frame of a function that
+ // threw.
+ constants.push((offsets.execution_context_realm_without_function, 8, runtime.realm.0));
self.emit_constant_fields(native_frame, temp, &mut constants)?;
self.spare_fields = constants;
if site.is_some() {
@@ -1496,6 +1500,13 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
}
self.masm
.store64(&vm_field(offsets.vm_running_execution_context), native_frame);
+ // A function of another realm runs in its own realm. Its return makes
+ // the realm of this code current again.
+ let switches_realm = target.realm != runtime.realm;
+ if switches_realm {
+ self.masm.move_imm64(temp, target.realm.0);
+ self.masm.store64(&vm_field(offsets.vm_current_realm), temp);
+ }
// Call the native function: it returns a ThrowCompletionOr<Value>,
// the value or exception in the first return register and whether it
@@ -1516,6 +1527,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.masm.store64(&vm_field(offsets.vm_interpreter_stack_top), scratch);
self.masm
.store64(&vm_field(offsets.vm_running_execution_context), frame);
+ if switches_realm {
+ self.masm.move_imm64(scratch, runtime.realm.0);
+ self.masm.store64(&vm_field(offsets.vm_current_realm), scratch);
+ }
if let (None, Some(dst)) = (site, dst) {
let destination = self.slot_address(dst.raw())?;
self.masm.store64(&destination, value_register);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
index 507f7f619fb..f1223f761ca 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call_stub.rs
@@ -409,12 +409,6 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.masm.move64(temp, callee_value);
self.branch_on_tag(Condition::NotEqual, temp, value::OBJECT_TAG, scratch, slow);
self.emit_unbox_cell(function_register, temp);
- // A callee of another realm is called by the runtime, which changes
- // the current realm.
- self.emit_load_compressed_cell(temp, &Address::new(function_register, offsets.object_shape as i32));
- self.emit_load_compressed_cell(temp, &Address::new(temp, layout.shape_realm as i32));
- self.masm
- .branch64_memory(Condition::NotEqual, &vm_field(offsets.vm_current_realm), temp, slow);
self.masm
.load16(temp, &Address::new(function_register, offsets.object_flags as i32));
@@ -439,8 +433,13 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
}
// An ECMAScript function the interpreter's call fast path calls in an
- // inline frame.
+ // inline frame. One of another realm is called by the runtime, which
+ // changes the current realm.
self.masm.bind(ecmascript_function);
+ self.emit_load_compressed_cell(temp, &Address::new(function_register, offsets.object_shape as i32));
+ self.emit_load_compressed_cell(temp, &Address::new(temp, layout.shape_realm as i32));
+ self.masm
+ .branch64_memory(Condition::NotEqual, &vm_field(offsets.vm_current_realm), temp, slow);
// NB: The executable is decoded once it is known not to be null,
// which it stays: the runtime compiles the function again if a
// collection in it flushed the executable.
@@ -1043,6 +1042,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
let vm_field = |offset: u32| Address::new(vm, offset as i32);
let frame_field = |offset: u32| Address::new(frame, offset as i32);
let field = |offset: u32| Address::new(native_frame, offset as i32);
+ let field_of = |base: Gpr, offset: u32| Address::new(base, offset as i32);
self.emit_native_stack_check(slow);
@@ -1093,6 +1093,16 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.emit_stub_copy_arguments(other, count, registers[0], temp);
self.masm
.store64(&vm_field(offsets.vm_running_execution_context), native_frame);
+ // The function runs in its own realm, which may be another one. The
+ // frame keeps the caller's realm, which the return makes current
+ // again, as does the runtime when it unwinds the frame of a function
+ // that threw.
+ self.masm.load64(temp, &vm_field(offsets.vm_current_realm));
+ self.masm
+ .store64(&field(offsets.execution_context_realm_without_function), temp);
+ self.emit_load_compressed_cell(temp, &Address::new(function_register, offsets.object_shape as i32));
+ self.emit_load_compressed_cell(temp, &Address::new(temp, layout.shape_realm as i32));
+ self.masm.store64(&vm_field(offsets.vm_current_realm), temp);
// Call the native function: it returns a ThrowCompletionOr<Value>,
// the value or exception in the first return register and whether it
@@ -1123,6 +1133,11 @@ impl<M: PortableMacroAssembler> Codegen<'_, '_, M> {
self.masm.branch_test32(Condition::NonZero, variant, 0xFF, threw);
// Pop the frame, which is the running one, like the interpreter does.
self.masm.load64(temp, &vm_field(offsets.vm_running_execution_context));
+ self.masm.load64(
+ scratch,
+ &field_of(temp, offsets.execution_context_realm_without_function),
+ );
+ self.masm.store64(&vm_field(offsets.vm_current_realm), scratch);
self.masm.store64(&vm_field(offsets.vm_interpreter_stack_top), temp);
self.masm
.store64(&vm_field(offsets.vm_running_execution_context), frame);
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
index 43d57976a31..c4a35e42272 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests.rs
@@ -55,6 +55,7 @@ fn offsets() -> RuntimeOffsets {
accessor_getter: 8,
accessor_setter: 16,
execution_context_function: 64,
+ execution_context_realm_without_function: 72,
execution_context_variable_environment: 96,
execution_context_frame_id: 104,
execution_context_yield_continuation: 112,
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index e35adebb652..eaf946eb637 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -1622,8 +1622,10 @@ pub struct RuntimeOffsets {
/// `VmHead::running_execution_context`.
pub vm_running_execution_context: u32,
/// `VmHead::current_realm` (cell pointer), the realm of the running
- /// frame. JIT code calls functions of another realm through the
- /// runtime, which changes it, and compiles no call of them inline.
+ /// frame. JIT code calls ECMAScript functions of another realm through
+ /// the runtime, which changes it, and compiles no call of them inline.
+ /// Its calls of native functions in lightweight frames change it
+ /// themselves.
pub vm_current_realm: u32,
/// `Vm::jit_native_stack_limit` (u64): JIT code exits at entry if the
/// stack pointer is below it, and its calls take their slow paths,
@@ -1654,6 +1656,10 @@ pub struct RuntimeOffsets {
pub accessor_setter: u32,
/// `ExecutionContext::function` (cell pointer).
pub execution_context_function: u32,
+ /// `ExecutionContext::realm_without_function` (cell pointer), which
+ /// the lightweight frame of a native function call sets to the
+ /// caller's realm. JIT code stores its own realm there.
+ pub execution_context_realm_without_function: u32,
/// `ExecutionContext::variable_environment` (cell pointer).
pub execution_context_variable_environment: u32,
/// `ExecutionContext::frame_id` (u64).
diff --git a/Libraries/LibJS/Runtime/ExecutionContext.h b/Libraries/LibJS/Runtime/ExecutionContext.h
index f702c767673..7be8795a1d8 100644
--- a/Libraries/LibJS/Runtime/ExecutionContext.h
+++ b/Libraries/LibJS/Runtime/ExecutionContext.h
@@ -115,7 +115,8 @@ struct JS_API ExecutionContext {
GC::Ptr<FunctionObject> function; // [[Function]]
// The [[Realm]] of a context without a function. A context with a function runs in the function's realm, and
- // this holds an old value: use realm() and set_realm().
+ // this holds an old value: use realm() and set_realm(). The lightweight frame of a native function call keeps
+ // the caller's realm here (see the Rust ExecutionContext).
GC::Ptr<Realm> realm_without_function;
// The [[ScriptOrModule]] of a context without a function. That of a context with a function comes from the
// function, and this holds an old value: use script_or_module() and set_script_or_module().
diff --git a/Libraries/LibJS/Rust/src/interpreter/execution_context.rs b/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
index 4f650037256..129406d008d 100644
--- a/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/execution_context.rs
@@ -317,8 +317,12 @@ unsafe impl Trace for ExecutionContext {
if self.function.get().is_none() || (self.frame_initialized.get() && self.keeps_private_environment()) {
self.private_environment_in_frame.trace(visitor);
}
- if self.function.get().is_none() {
+ // NB: A frame with a function holds an old value there too, apart from the lightweight frame of a native
+ // function call, which has no executable and keeps the caller's realm.
+ if self.function.get().is_none() || self.executable.get().is_none() {
self.realm_without_function.trace(visitor);
+ }
+ if self.function.get().is_none() {
match self.script_or_module_without_function.get() {
ScriptOrModule::Empty => {}
ScriptOrModule::Script(script) => script.trace(visitor),
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
index 69269fbab65..7370ebfd5d2 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/calls.rs
@@ -1284,6 +1284,9 @@ pub fn handle_raw_native_exception(vm: &Vm, exception: Value) -> SlowPathControl
// dispatch. Match VM::handle_exception()'s inline-frame semantics by
// probing the caller with a PC inside the Call instruction.
let caller_pc = callee_frame.caller_return_pc.get();
+ // NB: The native function ran in its own realm, which may be another one than its caller's. Its frame keeps the
+ // realm that was current when it was entered.
+ vm.head.current_realm.set(callee_frame.realm_without_function.get());
vm.unwind_inline_frame_for_exception();
handle_asm_exception(vm, caller_pc - 1, exception)
}
diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rs
index 9265330f083..7c76ce6750d 100644
--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs
@@ -1069,8 +1069,10 @@ impl Vm {
self.head.current_realm.set(realm);
}
- /// Makes `frame`, a frame entered inline for a call or left for its caller, the running execution context. Such
- /// frames run in the current realm, as the interpreter and JIT code leave calls into another realm to the runtime.
+ /// Makes `frame`, a frame of bytecode entered inline for a call or left for its caller, the running execution
+ /// context. Such frames run in the current realm: the interpreter and JIT code leave calls of ECMAScript functions
+ /// of another realm to the runtime, and make the realm of the caller current again once a native function of
+ /// another realm returned or threw.
pub(crate) fn set_running_frame_in_current_realm(&self, frame: *mut ExecutionContext) {
self.head.running_execution_context.set(frame);
debug_assert!(
diff --git a/Libraries/LibJS/Rust/src/jit/entry_exit.rs b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
index 501f19e3fc9..ebe3df36bec 100644
--- a/Libraries/LibJS/Rust/src/jit/entry_exit.rs
+++ b/Libraries/LibJS/Rust/src/jit/entry_exit.rs
@@ -328,6 +328,9 @@ pub unsafe extern "C" fn libjs_jit_inlined_raw_native_exception(
.running_execution_context()
.expect("the native function's frame runs");
assert_eq!(frame_of(native_frame).caller_frame.get(), root.as_ptr());
+ // NB: The native function ran in its own realm, which may be another one than that of the frames of the call
+ // site, whose realm the runtime unwinds to.
+ vm.head.current_realm.set(frame_of(root).realm());
translate::materialize_call_site_frames(vm, root, site, frame_pointer, Some(native_frame));
crate::interpreter::slow_paths::calls::handle_raw_native_exception(vm, Value::from_bits(exception)).0
}
diff --git a/Libraries/LibJS/Rust/src/jit/runtime_info.rs b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
index b8aa9c7d736..14aec0b69cb 100644
--- a/Libraries/LibJS/Rust/src/jit/runtime_info.rs
+++ b/Libraries/LibJS/Rust/src/jit/runtime_info.rs
@@ -145,6 +145,7 @@ fn runtime_offsets() -> RuntimeOffsets {
accessor_getter: offset(offset_of!(Accessor, getter)),
accessor_setter: offset(offset_of!(Accessor, setter)),
execution_context_function: offset(offset_of!(ExecutionContext, function)),
+ execution_context_realm_without_function: offset(offset_of!(ExecutionContext, realm_without_function)),
execution_context_variable_environment: offset(offset_of!(ExecutionContext, variable_environment_in_frame)),
execution_context_frame_id: offset(offset_of!(ExecutionContext, frame_id)),
execution_context_yield_continuation: offset(offset_of!(ExecutionContext, yield_continuation)),
diff --git a/Libraries/LibJS/Rust/src/jit/translate.rs b/Libraries/LibJS/Rust/src/jit/translate.rs
index a35a698896f..84bb51223e6 100644
--- a/Libraries/LibJS/Rust/src/jit/translate.rs
+++ b/Libraries/LibJS/Rust/src/jit/translate.rs
@@ -938,7 +938,13 @@ pub(super) fn materialize_call_site_frames(
callee_ref.caller_frame.set(innermost.as_ptr());
callee_ref.caller_return_pc.set(linkage.return_pc);
callee_ref.caller_dst_raw.set(linkage.dst);
- vm.set_running_frame_in_current_realm(running);
+ // NB: A native callee may be of another realm, and the runtime unwinds its frame next. The current realm
+ // stays that of the call site's frames.
+ if callee_ref.executable.get().is_some() {
+ vm.set_running_frame_in_current_realm(running);
+ } else {
+ vm.head.running_execution_context.set(running);
+ }
stack.top.set(top);
}
innermost
diff --git a/Libraries/LibJS/Rust/src/layout/execution_context.rs b/Libraries/LibJS/Rust/src/layout/execution_context.rs
index 2c775cb9898..668a9d159bf 100644
--- a/Libraries/LibJS/Rust/src/layout/execution_context.rs
+++ b/Libraries/LibJS/Rust/src/layout/execution_context.rs
@@ -77,7 +77,9 @@ impl ScriptOrModuleSlot {
pub struct ExecutionContext {
pub function: Cell<Option<Gc<FunctionObject>>>,
/// The [[Realm]] of a frame without a function, which native code makes. A frame with a function runs in the
- /// function's realm, and this holds an old value: read it through `ExecutionContext::realm()`.
+ /// function's realm, and this holds an old value: read it through `ExecutionContext::realm()`. The exception is
+ /// the lightweight frame the interpreter or JIT code makes for a call of a native function, which has no
+ /// executable: it keeps the caller's realm, which its return or unwinding makes current again.
pub realm_without_function: Cell<Option<Gc<Realm>>>,
/// The [[ScriptOrModule]] of a frame without a function, which native code makes. A frame with a function gets
/// it from the function, and this holds an old value: read it through `ExecutionContext::script_or_module()`.
diff --git a/Libraries/LibJS/Rust/src/layout/vm.rs b/Libraries/LibJS/Rust/src/layout/vm.rs
index 4881eea724a..10a2720e82e 100644
--- a/Libraries/LibJS/Rust/src/layout/vm.rs
+++ b/Libraries/LibJS/Rust/src/layout/vm.rs
@@ -73,8 +73,9 @@ pub struct VmHead {
/// `ExecutableHead::dispatch_table_index`). Indices without a table of their own have the plain one.
pub dispatch_tables: [Cell<*const c_void>; DISPATCH_TABLE_COUNT],
/// The current Realm Record, the [[Realm]] of the running execution context (see
- /// `Vm::set_running_execution_context()`). A frame that the interpreter or JIT code enters for a call runs in the
- /// realm of its caller: they leave a call into another realm to the runtime.
+ /// `Vm::set_running_execution_context()`). A frame of an ECMAScript function that the interpreter or JIT code
+ /// enters for a call runs in the realm of its caller: they leave a call of one of another realm to the runtime. The
+ /// frame of a native function they call runs in the function's realm, which they make current for the call.
pub current_realm: Cell<Option<Gc<Realm>>>,
}
diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rs
index 877dddfe443..775ca75166a 100644
--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs
@@ -131,6 +131,11 @@ impl NativeFunction {
}
fn from_function_object(base: FunctionObject, name: Option<Utf16FlyString>, realm: Gc<Realm>) -> NativeFunction {
+ // NB: The fast paths of calls make the realm of the function's shape current.
+ debug_assert!(
+ base.shape().realm() == realm,
+ "a native function's shape is of its realm"
+ );
base.inline_named_capacity.set(FUNCTION_LENGTH_AND_NAME_SLOTS as u8);
NativeFunction {
base,
diff --git a/Tests/LibWeb/Text/expected/cross-realm-calls.txt b/Tests/LibWeb/Text/expected/cross-realm-calls.txt
index 8027ae16484..a819dee1e27 100644
--- a/Tests/LibWeb/Text/expected/cross-realm-calls.txt
+++ b/Tests/LibWeb/Text/expected/cross-realm-calls.txt
@@ -5,3 +5,9 @@ errors thrown in the frame: 50
sloppy this of a frame function is the frame: 50
objects the Object functions make: 100
getters of both realms: frame HTML BODY parent
+native functions of the frame: 600
+errors thrown by native functions of the frame: 200
+errors thrown by native functions of the frame in a callee: 200
+errors thrown by getters of the frame: 200
+setters of the frame: 200
+errors thrown by setters of the frame: 200
diff --git a/Tests/LibWeb/Text/input/cross-realm-calls.html b/Tests/LibWeb/Text/input/cross-realm-calls.html
index 38f52d0dd9d..b47db733aed 100644
--- a/Tests/LibWeb/Text/input/cross-realm-calls.html
+++ b/Tests/LibWeb/Text/input/cross-realm-calls.html
@@ -72,5 +72,68 @@
seen.add(`${valueOf(other.getterObject)} ${nodeNameOf(other.document.documentElement)} ${nodeNameOf(document.body)} ${where}`);
}
println(`getters of both realms: ${[...seen].join(", ")}`);
+
+ const otherDocument = other.document;
+ let nativesOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ const element = otherDocument.createElement("div");
+ const array = other.Array.of(1, 2).map(parentWhere);
+ if (element instanceof other.HTMLDivElement && !(element instanceof HTMLDivElement)) ++nativesOfTheFrame;
+ if (array instanceof other.Array && array.join() === "parent,parent" && where === "parent") ++nativesOfTheFrame;
+ if (otherDocument.querySelectorAll("html").length === 1) ++nativesOfTheFrame;
+ }
+ println(`native functions of the frame: ${nativesOfTheFrame}`);
+
+ let nativeErrorsOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ try {
+ otherDocument.querySelector("!");
+ } catch (error) {
+ if (error instanceof other.DOMException && !(error instanceof DOMException) && where === "parent") ++nativeErrorsOfTheFrame;
+ }
+ }
+ println(`errors thrown by native functions of the frame: ${nativeErrorsOfTheFrame}`);
+
+ function querySelectorOf(document, selector) {
+ return document.querySelector(selector);
+ }
+ let calleeNativeErrorsOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ try {
+ querySelectorOf(otherDocument, "!");
+ } catch (error) {
+ if (error instanceof other.DOMException && where === "parent") ++calleeNativeErrorsOfTheFrame;
+ }
+ }
+ println(`errors thrown by native functions of the frame in a callee: ${calleeNativeErrorsOfTheFrame}`);
+
+ const notAnElement = Object.create(other.HTMLElement.prototype);
+ let getterErrorsOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ try {
+ notAnElement.title;
+ } catch (error) {
+ if (error instanceof other.TypeError && !(error instanceof TypeError) && where === "parent") ++getterErrorsOfTheFrame;
+ }
+ }
+ println(`errors thrown by getters of the frame: ${getterErrorsOfTheFrame}`);
+
+ const otherBody = otherDocument.body;
+ let settersOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ otherBody.title = where;
+ if (otherBody.title === "parent" && where === "parent") ++settersOfTheFrame;
+ }
+ println(`setters of the frame: ${settersOfTheFrame}`);
+
+ let setterErrorsOfTheFrame = 0;
+ for (let i = 0; i < 200; ++i) {
+ try {
+ otherBody.tabIndex = Symbol();
+ } catch (error) {
+ if (error instanceof other.TypeError && !(error instanceof TypeError) && where === "parent") ++setterErrorsOfTheFrame;
+ }
+ }
+ println(`errors thrown by setters of the frame: ${setterErrorsOfTheFrame}`);
});
</script>