blob: 95ec12be3cf365a6d8c4eac416055a5ec79f936d [file] [log] [blame]
darin@apple.com4cd37c32011-01-20 01:53:50 +00001/*
mark.lam@apple.com1d9c98d2016-07-13 00:19:15 +00002 * Copyright (C) 2011, 2016 Apple Inc. All rights reserved.
darin@apple.com4cd37c32011-01-20 01:53:50 +00003 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000020#pragma once
darin@apple.com4cd37c32011-01-20 01:53:50 +000021
annulen@yandex.ru6712c2d2017-06-25 17:40:30 +000022#include "CallFrame.h"
mark.lam@apple.com1d9c98d2016-07-13 00:19:15 +000023#include "VMInlines.h"
mark.lam@apple.com6df1a802012-10-19 20:09:36 +000024#include <wtf/StackStats.h>
darin@apple.com4cd37c32011-01-20 01:53:50 +000025
26namespace JSC {
27
28class StringRecursionChecker {
29 WTF_MAKE_NONCOPYABLE(StringRecursionChecker);
30
31public:
32 StringRecursionChecker(ExecState*, JSObject* thisObject);
33 ~StringRecursionChecker();
34
barraclough@apple.com4782ba82011-09-30 23:54:44 +000035 JSValue earlyReturnValue() const; // 0 if everything is OK, value to return for failure cases
darin@apple.com4cd37c32011-01-20 01:53:50 +000036
37private:
barraclough@apple.com4782ba82011-09-30 23:54:44 +000038 JSValue throwStackOverflowError();
39 JSValue emptyString();
40 JSValue performCheck();
darin@apple.com4cd37c32011-01-20 01:53:50 +000041
42 ExecState* m_exec;
43 JSObject* m_thisObject;
barraclough@apple.com4782ba82011-09-30 23:54:44 +000044 JSValue m_earlyReturnValue;
mark.lam@apple.com6df1a802012-10-19 20:09:36 +000045
46 StackStats::CheckPoint stackCheckpoint;
darin@apple.com4cd37c32011-01-20 01:53:50 +000047};
48
barraclough@apple.com4782ba82011-09-30 23:54:44 +000049inline JSValue StringRecursionChecker::performCheck()
darin@apple.com4cd37c32011-01-20 01:53:50 +000050{
mark.lam@apple.combd1385d2013-06-21 23:58:52 +000051 VM& vm = m_exec->vm();
mark.lam@apple.com1d9c98d2016-07-13 00:19:15 +000052 if (UNLIKELY(!vm.isSafeToRecurseSoft()))
darin@apple.com4cd37c32011-01-20 01:53:50 +000053 return throwStackOverflowError();
benjamin@webkit.orgc4dc1712015-05-18 06:23:31 +000054
55 bool alreadyVisited = false;
56 if (!vm.stringRecursionCheckFirstObject)
57 vm.stringRecursionCheckFirstObject = m_thisObject;
58 else if (vm.stringRecursionCheckFirstObject == m_thisObject)
59 alreadyVisited = true;
60 else
61 alreadyVisited = !vm.stringRecursionCheckVisitedObjects.add(m_thisObject).isNewEntry;
62
darin@apple.com4cd37c32011-01-20 01:53:50 +000063 if (alreadyVisited)
64 return emptyString(); // Return empty string to avoid infinite recursion.
barraclough@apple.com4782ba82011-09-30 23:54:44 +000065 return JSValue(); // Indicate success.
darin@apple.com4cd37c32011-01-20 01:53:50 +000066}
67
68inline StringRecursionChecker::StringRecursionChecker(ExecState* exec, JSObject* thisObject)
69 : m_exec(exec)
70 , m_thisObject(thisObject)
71 , m_earlyReturnValue(performCheck())
72{
73}
74
barraclough@apple.com4782ba82011-09-30 23:54:44 +000075inline JSValue StringRecursionChecker::earlyReturnValue() const
darin@apple.com4cd37c32011-01-20 01:53:50 +000076{
77 return m_earlyReturnValue;
78}
79
80inline StringRecursionChecker::~StringRecursionChecker()
81{
82 if (m_earlyReturnValue)
83 return;
benjamin@webkit.orgc4dc1712015-05-18 06:23:31 +000084
85 VM& vm = m_exec->vm();
86 if (vm.stringRecursionCheckFirstObject == m_thisObject)
87 vm.stringRecursionCheckFirstObject = nullptr;
88 else {
89 ASSERT(vm.stringRecursionCheckVisitedObjects.contains(m_thisObject));
90 vm.stringRecursionCheckVisitedObjects.remove(m_thisObject);
91 }
darin@apple.com4cd37c32011-01-20 01:53:50 +000092}
93
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000094} // namespace JSC