1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "builtin/Eval.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Range.h"
#include "jscntxt.h"
#include "jshashutil.h"
#include "frontend/BytecodeCompiler.h"
#include "vm/Debugger.h"
#include "vm/GlobalObject.h"
#include "vm/JSONParser.h"
#include "vm/Interpreter-inl.h"
using namespace js;
using mozilla::AddToHash;
using mozilla::HashString;
using mozilla::RangedPtr;
using JS::AutoCheckCannotGC;
// We should be able to assert this for *any* fp->environmentChain().
static void
AssertInnerizedEnvironmentChain(JSContext* cx, JSObject& env)
{
#ifdef DEBUG
RootedObject obj(cx);
for (obj = &env; obj; obj = obj->enclosingEnvironment())
MOZ_ASSERT(!IsWindowProxy(obj));
#endif
}
static bool
IsEvalCacheCandidate(JSScript* script)
{
// Make sure there are no inner objects which might use the wrong parent
// and/or call scope by reusing the previous eval's script.
return script->isDirectEvalInFunction() &&
!script->hasSingletons() &&
!script->hasObjects();
}
/* static */ HashNumber
EvalCacheHashPolicy::hash(const EvalCacheLookup& l)
{
AutoCheckCannotGC nogc;
uint32_t hash = l.str->hasLatin1Chars()
? HashString(l.str->latin1Chars(nogc), l.str->length())
: HashString(l.str->twoByteChars(nogc), l.str->length());
return AddToHash(hash, l.callerScript.get(), l.version, l.pc);
}
/* static */ bool
EvalCacheHashPolicy::match(const EvalCacheEntry& cacheEntry, const EvalCacheLookup& l)
{
JSScript* script = cacheEntry.script;
MOZ_ASSERT(IsEvalCacheCandidate(script));
return EqualStrings(cacheEntry.str, l.str) &&
cacheEntry.callerScript == l.callerScript &&
script->getVersion() == l.version &&
cacheEntry.pc == l.pc;
}
// Add the script to the eval cache when EvalKernel is finished
class EvalScriptGuard
{
JSContext* cx_;
Rooted<JSScript*> script_;
/* These fields are only valid if lookup_.str is non-nullptr. */
EvalCacheLookup lookup_;
mozilla::Maybe<DependentAddPtr<EvalCache>> p_;
RootedLinearString lookupStr_;
public:
explicit EvalScriptGuard(JSContext* cx)
: cx_(cx), script_(cx), lookup_(cx), lookupStr_(cx) {}
~EvalScriptGuard() {
if (script_ && !cx_->isExceptionPending()) {
script_->cacheForEval();
EvalCacheEntry cacheEntry = {lookupStr_, script_, lookup_.callerScript, lookup_.pc};
lookup_.str = lookupStr_;
if (lookup_.str && IsEvalCacheCandidate(script_)) {
// Ignore failure to add cache entry.
if (!p_->add(cx_, cx_->caches.evalCache, lookup_, cacheEntry))
cx_->recoverFromOutOfMemory();
}
}
}
void lookupInEvalCache(JSLinearString* str, JSScript* callerScript, jsbytecode* pc)
{
lookupStr_ = str;
lookup_.str = str;
lookup_.callerScript = callerScript;
lookup_.version = cx_->findVersion();
lookup_.pc = pc;
p_.emplace(cx_, cx_->caches.evalCache, lookup_);
if (*p_) {
script_ = (*p_)->script;
p_->remove(cx_, cx_->caches.evalCache, lookup_);
script_->uncacheForEval();
}
}
void setNewScript(JSScript* script) {
// JSScript::initFromEmitter has already called js_CallNewScriptHook.
MOZ_ASSERT(!script_ && script);
script_ = script;
script_->setActiveEval();
}
bool foundScript() {
return !!script_;
}
HandleScript script() {
MOZ_ASSERT(script_);
return script_;
}
};
enum EvalJSONResult {
EvalJSON_Failure,
EvalJSON_Success,
EvalJSON_NotJSON
};
template <typename CharT>
static bool
EvalStringMightBeJSON(const mozilla::Range<const CharT> chars)
{
// If the eval string starts with '(' or '[' and ends with ')' or ']', it may be JSON.
// Try the JSON parser first because it's much faster. If the eval string
// isn't JSON, JSON parsing will probably fail quickly, so little time
// will be lost.
size_t length = chars.length();
if (length > 2 &&
((chars[0] == '[' && chars[length - 1] == ']') ||
(chars[0] == '(' && chars[length - 1] == ')')))
{
// Remarkably, JavaScript syntax is not a superset of JSON syntax:
// strings in JavaScript cannot contain the Unicode line and paragraph
// terminator characters U+2028 and U+2029, but strings in JSON can.
// Rather than force the JSON parser to handle this quirk when used by
// eval, we simply don't use the JSON parser when either character
// appears in the provided string. See bug 657367.
if (sizeof(CharT) > 1) {
for (RangedPtr<const CharT> cp = chars.begin() + 1, end = chars.end() - 1;
cp < end;
cp++)
{
char16_t c = *cp;
if (c == 0x2028 || c == 0x2029)
return false;
}
}
return true;
}
return false;
}
template <typename CharT>
static EvalJSONResult
ParseEvalStringAsJSON(JSContext* cx, const mozilla::Range<const CharT> chars, MutableHandleValue rval)
{
size_t len = chars.length();
MOZ_ASSERT((chars[0] == '(' && chars[len - 1] == ')') ||
(chars[0] == '[' && chars[len - 1] == ']'));
auto jsonChars = (chars[0] == '[')
? chars
: mozilla::Range<const CharT>(chars.begin().get() + 1U, len - 2);
Rooted<JSONParser<CharT>> parser(cx, JSONParser<CharT>(cx, jsonChars, JSONParserBase::NoError));
if (!parser.parse(rval))
return EvalJSON_Failure;
return rval.isUndefined() ? EvalJSON_NotJSON : EvalJSON_Success;
}
static EvalJSONResult
TryEvalJSON(JSContext* cx, JSLinearString* str, MutableHandleValue rval)
{
if (str->hasLatin1Chars()) {
AutoCheckCannotGC nogc;
if (!EvalStringMightBeJSON(str->latin1Range(nogc)))
return EvalJSON_NotJSON;
} else {
AutoCheckCannotGC nogc;
if (!EvalStringMightBeJSON(str->twoByteRange(nogc)))
return EvalJSON_NotJSON;
}
AutoStableStringChars linearChars(cx);
if (!linearChars.init(cx, str))
return EvalJSON_Failure;
return linearChars.isLatin1()
? ParseEvalStringAsJSON(cx, linearChars.latin1Range(), rval)
: ParseEvalStringAsJSON(cx, linearChars.twoByteRange(), rval);
}
enum EvalType { DIRECT_EVAL, INDIRECT_EVAL };
// Common code implementing direct and indirect eval.
//
// Evaluate call.argv[2], if it is a string, in the context of the given calling
// frame, with the provided scope chain, with the semantics of either a direct
// or indirect eval (see ES5 10.4.2). If this is an indirect eval, env
// must be a global object.
//
// On success, store the completion value in call.rval and return true.
static bool
EvalKernel(JSContext* cx, HandleValue v, EvalType evalType, AbstractFramePtr caller,
HandleObject env, jsbytecode* pc, MutableHandleValue vp)
{
MOZ_ASSERT((evalType == INDIRECT_EVAL) == !caller);
MOZ_ASSERT((evalType == INDIRECT_EVAL) == !pc);
MOZ_ASSERT_IF(evalType == INDIRECT_EVAL, IsGlobalLexicalEnvironment(env));
AssertInnerizedEnvironmentChain(cx, *env);
Rooted<GlobalObject*> envGlobal(cx, &env->global());
if (!GlobalObject::isRuntimeCodeGenEnabled(cx, envGlobal)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CSP_BLOCKED_EVAL);
return false;
}
// ES5 15.1.2.1 step 1.
if (!v.isString()) {
vp.set(v);
return true;
}
RootedString str(cx, v.toString());
// ES5 15.1.2.1 steps 2-8.
// Per ES5, indirect eval runs in the global scope. (eval is specified this
// way so that the compiler can make assumptions about what bindings may or
// may not exist in the current frame if it doesn't see 'eval'.)
MOZ_ASSERT_IF(evalType != DIRECT_EVAL,
cx->global() == &env->as<LexicalEnvironmentObject>().global());
RootedLinearString linearStr(cx, str->ensureLinear(cx));
if (!linearStr)
return false;
RootedScript callerScript(cx, caller ? caller.script() : nullptr);
EvalJSONResult ejr = TryEvalJSON(cx, linearStr, vp);
if (ejr != EvalJSON_NotJSON)
return ejr == EvalJSON_Success;
EvalScriptGuard esg(cx);
if (evalType == DIRECT_EVAL && caller.isFunctionFrame())
esg.lookupInEvalCache(linearStr, callerScript, pc);
if (!esg.foundScript()) {
RootedScript maybeScript(cx);
unsigned lineno;
const char* filename;
bool mutedErrors;
uint32_t pcOffset;
DescribeScriptedCallerForCompilation(cx, &maybeScript, &filename, &lineno, &pcOffset,
&mutedErrors,
evalType == DIRECT_EVAL
? CALLED_FROM_JSOP_EVAL
: NOT_CALLED_FROM_JSOP_EVAL);
const char* introducerFilename = filename;
if (maybeScript && maybeScript->scriptSource()->introducerFilename())
introducerFilename = maybeScript->scriptSource()->introducerFilename();
RootedScope enclosing(cx);
if (evalType == DIRECT_EVAL)
enclosing = callerScript->innermostScope(pc);
else
enclosing = &cx->global()->emptyGlobalScope();
CompileOptions options(cx);
options.setIsRunOnce(true)
.setNoScriptRval(false)
.setMutedErrors(mutedErrors)
.maybeMakeStrictMode(evalType == DIRECT_EVAL && IsStrictEvalPC(pc));
if (introducerFilename) {
options.setFileAndLine(filename, 1);
options.setIntroductionInfo(introducerFilename, "eval", lineno, maybeScript, pcOffset);
} else {
options.setFileAndLine("eval", 1);
options.setIntroductionType("eval");
}
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, linearStr))
return false;
const char16_t* chars = linearChars.twoByteRange().begin().get();
SourceBufferHolder::Ownership ownership = linearChars.maybeGiveOwnershipToCaller()
? SourceBufferHolder::GiveOwnership
: SourceBufferHolder::NoOwnership;
SourceBufferHolder srcBuf(chars, linearStr->length(), ownership);
JSScript* compiled = frontend::CompileEvalScript(cx, cx->tempLifoAlloc(),
env, enclosing,
options, srcBuf);
if (!compiled)
return false;
esg.setNewScript(compiled);
}
// Look up the newTarget from the frame iterator.
Value newTargetVal = NullValue();
return ExecuteKernel(cx, esg.script(), *env, newTargetVal,
NullFramePtr() /* evalInFrame */, vp.address());
}
bool
js::DirectEvalStringFromIon(JSContext* cx,
HandleObject env, HandleScript callerScript,
HandleValue newTargetValue, HandleString str,
jsbytecode* pc, MutableHandleValue vp)
{
AssertInnerizedEnvironmentChain(cx, *env);
Rooted<GlobalObject*> envGlobal(cx, &env->global());
if (!GlobalObject::isRuntimeCodeGenEnabled(cx, envGlobal)) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CSP_BLOCKED_EVAL);
return false;
}
// ES5 15.1.2.1 steps 2-8.
RootedLinearString linearStr(cx, str->ensureLinear(cx));
if (!linearStr)
return false;
EvalJSONResult ejr = TryEvalJSON(cx, linearStr, vp);
if (ejr != EvalJSON_NotJSON)
return ejr == EvalJSON_Success;
EvalScriptGuard esg(cx);
esg.lookupInEvalCache(linearStr, callerScript, pc);
if (!esg.foundScript()) {
RootedScript maybeScript(cx);
const char* filename;
unsigned lineno;
bool mutedErrors;
uint32_t pcOffset;
DescribeScriptedCallerForCompilation(cx, &maybeScript, &filename, &lineno, &pcOffset,
&mutedErrors, CALLED_FROM_JSOP_EVAL);
const char* introducerFilename = filename;
if (maybeScript && maybeScript->scriptSource()->introducerFilename())
introducerFilename = maybeScript->scriptSource()->introducerFilename();
RootedScope enclosing(cx, callerScript->innermostScope(pc));
CompileOptions options(cx);
options.setIsRunOnce(true)
.setNoScriptRval(false)
.setMutedErrors(mutedErrors)
.maybeMakeStrictMode(IsStrictEvalPC(pc));
if (introducerFilename) {
options.setFileAndLine(filename, 1);
options.setIntroductionInfo(introducerFilename, "eval", lineno, maybeScript, pcOffset);
} else {
options.setFileAndLine("eval", 1);
options.setIntroductionType("eval");
}
AutoStableStringChars linearChars(cx);
if (!linearChars.initTwoByte(cx, linearStr))
return false;
const char16_t* chars = linearChars.twoByteRange().begin().get();
SourceBufferHolder::Ownership ownership = linearChars.maybeGiveOwnershipToCaller()
? SourceBufferHolder::GiveOwnership
: SourceBufferHolder::NoOwnership;
SourceBufferHolder srcBuf(chars, linearStr->length(), ownership);
JSScript* compiled = frontend::CompileEvalScript(cx, cx->tempLifoAlloc(),
env, enclosing,
options, srcBuf);
if (!compiled)
return false;
esg.setNewScript(compiled);
}
return ExecuteKernel(cx, esg.script(), *env, newTargetValue,
NullFramePtr() /* evalInFrame */, vp.address());
}
bool
js::IndirectEval(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
Rooted<GlobalObject*> global(cx, &args.callee().global());
RootedObject globalLexical(cx, &global->lexicalEnvironment());
// Note we'll just pass |undefined| here, then return it directly (or throw
// if runtime codegen is disabled), if no argument is provided.
return EvalKernel(cx, args.get(0), INDIRECT_EVAL, NullFramePtr(), globalLexical, nullptr,
args.rval());
}
bool
js::DirectEval(JSContext* cx, HandleValue v, MutableHandleValue vp)
{
// Direct eval can assume it was called from an interpreted or baseline frame.
ScriptFrameIter iter(cx);
AbstractFramePtr caller = iter.abstractFramePtr();
MOZ_ASSERT(JSOp(*iter.pc()) == JSOP_EVAL ||
JSOp(*iter.pc()) == JSOP_STRICTEVAL ||
JSOp(*iter.pc()) == JSOP_SPREADEVAL ||
JSOp(*iter.pc()) == JSOP_STRICTSPREADEVAL);
MOZ_ASSERT(caller.compartment() == caller.script()->compartment());
RootedObject envChain(cx, caller.environmentChain());
return EvalKernel(cx, v, DIRECT_EVAL, caller, envChain, iter.pc(), vp);
}
bool
js::IsAnyBuiltinEval(JSFunction* fun)
{
return fun->maybeNative() == IndirectEval;
}
JS_FRIEND_API(bool)
js::ExecuteInGlobalAndReturnScope(JSContext* cx, HandleObject global, HandleScript scriptArg,
MutableHandleObject envArg)
{
CHECK_REQUEST(cx);
assertSameCompartment(cx, global);
MOZ_ASSERT(global->is<GlobalObject>());
MOZ_RELEASE_ASSERT(scriptArg->hasNonSyntacticScope());
RootedScript script(cx, scriptArg);
Rooted<GlobalObject*> globalRoot(cx, &global->as<GlobalObject>());
if (script->compartment() != cx->compartment()) {
script = CloneGlobalScript(cx, ScopeKind::NonSyntactic, script);
if (!script)
return false;
Debugger::onNewScript(cx, script);
}
Rooted<EnvironmentObject*> env(cx, NonSyntacticVariablesObject::create(cx));
if (!env)
return false;
// Unlike the non-syntactic scope chain API used by the subscript loader,
// this API creates a fresh block scope each time.
env = LexicalEnvironmentObject::createNonSyntactic(cx, env);
if (!env)
return false;
RootedValue rval(cx);
if (!ExecuteKernel(cx, script, *env, UndefinedValue(),
NullFramePtr() /* evalInFrame */, rval.address()))
{
return false;
}
envArg.set(env);
return true;
}
|