Debian Codegraph @

js/src/jit/TrampolineNatives.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/TrampolineNatives.h"
8
9 #include "jit/CalleeToken.h"
10 #include "jit/Ion.h"
11 #include "jit/JitCommon.h"
12 #include "jit/JitRuntime.h"
13 #include "jit/MacroAssembler.h"
14 #include "jit/PerfSpewer.h"
15 #include "js/CallArgs.h"
16 #include "js/experimental/JitInfo.h"
17 #include "vm/TypedArrayObject.h"
18
19 #include "jit/MacroAssembler-inl.h"
20 #include "vm/Activation-inl.h"
21
22 using namespace js;
23 using namespace js::jit;
24
25 #define ADD_NATIVE(native) \
26 const JSJitInfo js::jit::JitInfo_##native{ \
27 {nullptr}, \
28 {uint16_t(TrampolineNative::native)}, \
29 {0}, \
30 JSJitInfo::TrampolineNative};
31 TRAMPOLINE_NATIVE_LIST(ADD_NATIVE)
32 #undef ADD_NATIVE
33
34 void js::jit::SetTrampolineNativeJitEntry(JSContext* cx, JSFunction* fun,
35 TrampolineNative native) {
36 if (!cx->runtime()->jitRuntime()) {
37 // No JIT support so there's no trampoline.
38 return;
39 }
40 void** entry = cx->runtime()->jitRuntime()->trampolineNativeJitEntry(native);
41 MOZ_ASSERT(entry);
42 MOZ_ASSERT(*entry);
43 fun->setTrampolineNativeJitEntry(entry);
44 }
45
46 uint32_t JitRuntime::generateArraySortTrampoline(MacroAssembler& masm,
47 ArraySortKind kind) {
48 AutoCreatedBy acb(masm, "JitRuntime::generateArraySortTrampoline");
49
50 const uint32_t offset = startTrampolineCode(masm);
51
52 // The stack for the trampoline frame will look like this:
53 //
54 // [TrampolineNativeFrameLayout]
55 // * this and arguments passed by the caller
56 // * CalleeToken
57 // * Descriptor
58 // * Return Address
59 // * Saved frame pointer <= FramePointer
60 // [ArraySortData]
61 // * ...
62 // * Comparator this + argument Values --+ -> comparator JitFrameLayout
63 // * Comparator (CalleeToken) |
64 // * Descriptor ----+ <= StackPointer
65 //
66 // The call to the comparator pushes the return address and the frame pointer,
67 // so we check the alignment after pushing these two pointers.
68 constexpr size_t FrameSize = sizeof(ArraySortData);
69 constexpr size_t PushedByCall = 2 * sizeof(void*);
70 static_assert((FrameSize + PushedByCall) % JitStackAlignment == 0);
71
72 // Assert ArraySortData comparator data matches JitFrameLayout.
73 static_assert(PushedByCall + ArraySortData::offsetOfDescriptor() ==
74 JitFrameLayout::offsetOfDescriptor());
75 static_assert(PushedByCall + ArraySortData::offsetOfComparator() ==
76 JitFrameLayout::offsetOfCalleeToken());
77 static_assert(PushedByCall + ArraySortData::offsetOfComparatorThis() ==
78 JitFrameLayout::offsetOfThis());
79 static_assert(PushedByCall + ArraySortData::offsetOfComparatorArgs() ==
80 JitFrameLayout::offsetOfActualArgs());
81 static_assert(CalleeToken_Function == 0,
82 "JSFunction* is valid CalleeToken for non-constructor calls");
83
84 // Compute offsets from FramePointer.
85 constexpr int32_t ComparatorOffset =
86 -int32_t(FrameSize) + ArraySortData::offsetOfComparator();
87 constexpr int32_t RvalOffset =
88 -int32_t(FrameSize) + ArraySortData::offsetOfComparatorReturnValue();
89 constexpr int32_t DescriptorOffset =
90 -int32_t(FrameSize) + ArraySortData::offsetOfDescriptor();
91 constexpr int32_t ComparatorThisOffset =
92 -int32_t(FrameSize) + ArraySortData::offsetOfComparatorThis();
93
94 #ifdef JS_USE_LINK_REGISTER
95 masm.pushReturnAddress();
96 #endif
97 masm.push(FramePointer);
98 masm.moveStackPtrTo(FramePointer);
99
100 AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All());
101 regs.takeUnchecked(ReturnReg);
102 regs.takeUnchecked(JSReturnOperand);
103 Register temp0 = regs.takeAny();
104 Register temp1 = regs.takeAny();
105 Register temp2 = regs.takeAny();
106
107 // Reserve space and check alignment of the comparator frame.
108 masm.reserveStack(FrameSize);
109 masm.assertStackAlignment(JitStackAlignment, PushedByCall);
110
111 // Trampoline control flow looks like this:
112 //
113 // call ArraySortFromJit or TypedArraySortFromJit
114 // goto checkReturnValue
115 // call_comparator:
116 // call comparator
117 // call ArraySortData::sortArrayWithComparator or
118 // ArraySortData::sortTypedArrayWithComparator
119 // checkReturnValue:
120 // check return value, jump to call_comparator if needed
121 // return rval
122
123 auto pushExitFrame = [&](Register cxReg, Register scratchReg) {
124 MOZ_ASSERT(masm.framePushed() == FrameSize);
125 masm.PushFrameDescriptor(FrameType::TrampolineNative);
126 masm.Push(ImmWord(0)); // Fake return address.
127 masm.Push(FramePointer);
128 masm.enterFakeExitFrame(cxReg, scratchReg, ExitFrameType::Bare);
129 };
130
131 // Call {Typed}ArraySortFromJit.
132 using Fn1 = ArraySortResult (*)(JSContext* cx,
133 jit::TrampolineNativeFrameLayout* frame);
134 masm.loadJSContext(temp0);
135 pushExitFrame(temp0, temp1);
136 masm.setupAlignedABICall();
137 masm.passABIArg(temp0);
138 masm.passABIArg(FramePointer);
139 switch (kind) {
140 case ArraySortKind::Array:
141 masm.callWithABI<Fn1, ArraySortFromJit>(
142 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
143 break;
144 case ArraySortKind::TypedArray:
145 masm.callWithABI<Fn1, TypedArraySortFromJit>(
146 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
147 break;
148 }
149
150 // Check return value.
151 Label checkReturnValue;
152 masm.jump(&checkReturnValue);
153 masm.setFramePushed(FrameSize);
154
155 // Call the comparator. Store the frame descriptor before each call to ensure
156 // the HASCACHEDSAVEDFRAME_BIT flag from a previous call is cleared.
157 uintptr_t jitCallDescriptor = MakeFrameDescriptorForJitCall(
158 jit::FrameType::TrampolineNative, ArraySortData::ComparatorActualArgs);
159 Label callDone, jitCallFast, jitCallSlow;
160 masm.bind(&jitCallFast);
161 {
162 masm.storeValue(UndefinedValue(),
163 Address(FramePointer, ComparatorThisOffset));
164 masm.storePtr(ImmWord(jitCallDescriptor),
165 Address(FramePointer, DescriptorOffset));
166 masm.loadPtr(Address(FramePointer, ComparatorOffset), temp0);
167 masm.loadJitCodeRaw(temp0, temp1);
168 masm.callJit(temp1);
169 masm.jump(&callDone);
170 }
171 masm.bind(&jitCallSlow);
172 {
173 masm.storeValue(UndefinedValue(),
174 Address(FramePointer, ComparatorThisOffset));
175 masm.storePtr(ImmWord(jitCallDescriptor),
176 Address(FramePointer, DescriptorOffset));
177 masm.loadPtr(Address(FramePointer, ComparatorOffset), temp0);
178 masm.loadJitCodeRaw(temp0, temp1);
179 masm.switchToObjectRealm(temp0, temp2);
180
181 // Handle arguments underflow.
182 Label noUnderflow, restoreRealm;
183 masm.loadFunctionArgCount(temp0, temp0);
184 masm.branch32(Assembler::BelowOrEqual, temp0,
185 Imm32(ArraySortData::ComparatorActualArgs), &noUnderflow);
186 {
187 Label rectifier;
188 bindLabelToOffset(&rectifier, argumentsRectifierOffset_);
189 masm.call(&rectifier);
190 masm.jump(&restoreRealm);
191 }
192 masm.bind(&noUnderflow);
193 masm.callJit(temp1);
194
195 masm.bind(&restoreRealm);
196 Address calleeToken(FramePointer,
197 TrampolineNativeFrameLayout::offsetOfCalleeToken());
198 masm.loadFunctionFromCalleeToken(calleeToken, temp0);
199 masm.switchToObjectRealm(temp0, temp1);
200 }
201
202 // Store the comparator's return value.
203 masm.bind(&callDone);
204 masm.storeValue(JSReturnOperand, Address(FramePointer, RvalOffset));
205
206 // Call ArraySortData::sort{Typed}ArrayWithComparator.
207 using Fn2 = ArraySortResult (*)(ArraySortData* data);
208 masm.moveStackPtrTo(temp2);
209 masm.loadJSContext(temp0);
210 pushExitFrame(temp0, temp1);
211 masm.setupAlignedABICall();
212 masm.passABIArg(temp2);
213 switch (kind) {
214 case ArraySortKind::Array:
215 masm.callWithABI<Fn2, ArraySortData::sortArrayWithComparator>(
216 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
217 break;
218 case ArraySortKind::TypedArray:
219 masm.callWithABI<Fn2, ArraySortData::sortTypedArrayWithComparator>(
220 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
221 break;
222 }
223
224 // Check return value.
225 masm.bind(&checkReturnValue);
226 masm.branch32(Assembler::Equal, ReturnReg,
227 Imm32(int32_t(ArraySortResult::Failure)), masm.failureLabel());
228 masm.freeStack(ExitFrameLayout::SizeWithFooter());
229 masm.branch32(Assembler::Equal, ReturnReg,
230 Imm32(int32_t(ArraySortResult::CallJSSameRealmNoRectifier)),
231 &jitCallFast);
232 masm.branch32(Assembler::Equal, ReturnReg,
233 Imm32(int32_t(ArraySortResult::CallJS)), &jitCallSlow);
234 #ifdef DEBUG
235 Label ok;
236 masm.branch32(Assembler::Equal, ReturnReg,
237 Imm32(int32_t(ArraySortResult::Done)), &ok);
238 masm.assumeUnreachable("Unexpected return value");
239 masm.bind(&ok);
240 #endif
241
242 masm.loadValue(Address(FramePointer, RvalOffset), JSReturnOperand);
243 masm.moveToStackPtr(FramePointer);
244 masm.pop(FramePointer);
245 masm.ret();
246
247 return offset;
248 }
249
250 void JitRuntime::generateTrampolineNatives(
251 MacroAssembler& masm, TrampolineNativeJitEntryOffsets& offsets,
252 PerfSpewerRangeRecorder& rangeRecorder) {
253 offsets[TrampolineNative::ArraySort] =
254 generateArraySortTrampoline(masm, ArraySortKind::Array);
255 rangeRecorder.recordOffset("Trampoline: ArraySort");
256
257 offsets[TrampolineNative::TypedArraySort] =
258 generateArraySortTrampoline(masm, ArraySortKind::TypedArray);
259 rangeRecorder.recordOffset("Trampoline: TypedArraySort");
260 }
261
262 bool jit::CallTrampolineNativeJitCode(JSContext* cx, TrampolineNative native,
263 CallArgs& args) {
264 // Use the EnterJit trampoline to enter the native's trampoline code.
265
266 AutoCheckRecursionLimit recursion(cx);
267 if (!recursion.check(cx)) {
268 return false;
269 }
270
271 MOZ_ASSERT(!args.isConstructing());
272 CalleeToken calleeToken = CalleeToToken(&args.callee().as<JSFunction>(),
273 /* constructing = */ false);
274
275 Value* maxArgv = args.array() - 1; // -1 to include |this|
276 size_t maxArgc = args.length() + 1;
277
278 Rooted<Value> result(cx, Int32Value(args.length()));
279
280 AssertRealmUnchanged aru(cx);
281 JitActivation activation(cx);
282
283 EnterJitCode enter = cx->runtime()->jitRuntime()->enterJit();
284 void* code = *cx->runtime()->jitRuntime()->trampolineNativeJitEntry(native);
285
286 CALL_GENERATED_CODE(enter, code, maxArgc, maxArgv, /* osrFrame = */ nullptr,
287 calleeToken, /* envChain = */ nullptr,
288 /* osrNumStackValues = */ 0, result.address());
289
290 // Ensure the counter was reset to zero after exiting from JIT code.
291 MOZ_ASSERT(!cx->isInUnsafeRegion());
292
293 // Release temporary buffer used for OSR into Ion.
294 cx->runtime()->jitRuntime()->freeIonOsrTempData();
295
296 if (result.isMagic()) {
297 MOZ_ASSERT(result.isMagic(JS_ION_ERROR));
298 return false;
299 }
300
301 args.rval().set(result);
302 return true;
303 }