2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "FunctionOverrides.h"
31 #include <wtf/DataLog.h>
32 #include <wtf/NeverDestroyed.h>
33 #include <wtf/text/CString.h>
34 #include <wtf/text/StringBuilder.h>
35 #include <wtf/text/StringHash.h>
40 The overrides file defines function bodies that we will want to override with
41 a replacement for debugging purposes. The overrides file may contain
42 'override' and 'with' clauses like these:
44 // Example 1: function foo1(a)
45 override !@#$%{ print("In foo1"); }!@#$%
47 print("I am overridden");
50 // Example 2: function foo2(a)
52 print("foo2's body has a string with }%% in it.");
53 // Because }%% appears in the function body here, we cannot use
54 // %% or % as the delimiter. %%% is ok though.
57 print("Overridden foo2");
60 1. Comments are lines starting with //. All comments will be ignored.
62 2. An 'override' clause is used to specify the original function body we
63 want to override. The with clause is used to specify the overriding
66 An 'override' clause must be followed immediately by a 'with' clause.
68 3. An 'override' clause must be of the form:
69 override <delimiter>{...function body...}<delimiter>
71 The override keyword must be at the start of the line.
73 <delimiter> may be any string of any ASCII characters (except for '{',
74 '}', and whitespace characters) as long as the pattern of "}<delimiter>"
75 does not appear in the function body e.g. the override clause of Example 2
76 above illustrates this.
78 The start and end <delimiter> must be identical.
80 The space between the override keyword and the start <delimiter> is
83 All characters between the pair of delimiters will be considered to
84 be part of the function body string. This allows us to also work
85 with script source that are multi-lined i.e. newlines are allowed.
87 4. A 'with' clause is identical in form to an 'override' clause except that
88 it uses the 'with' keyword instead of the 'override' keyword.
91 FunctionOverrides
& FunctionOverrides::overrides()
93 static LazyNeverDestroyed
<FunctionOverrides
> overrides
;
94 static std::once_flag initializeListFlag
;
95 std::call_once(initializeListFlag
, [] {
96 const char* overridesFileName
= Options::functionOverrides();
97 overrides
.construct(overridesFileName
);
102 FunctionOverrides::FunctionOverrides(const char* overridesFileName
)
104 parseOverridesInFile(overridesFileName
);
107 static void initializeOverrideInfo(const SourceCode
& origCode
, const String
& newBody
, FunctionOverrides::OverrideInfo
& info
)
109 String origProviderStr
= origCode
.provider()->source();
110 unsigned origBraceStart
= origCode
.startOffset();
111 unsigned origFunctionStart
= origProviderStr
.reverseFind("function", origBraceStart
);
112 unsigned headerLength
= origBraceStart
- origFunctionStart
;
113 String origHeader
= origProviderStr
.substring(origFunctionStart
, headerLength
);
115 String newProviderStr
;
116 newProviderStr
.append(origHeader
);
117 newProviderStr
.append(newBody
);
119 RefPtr
<SourceProvider
> newProvider
= StringSourceProvider::create(newProviderStr
, "<overridden>");
122 info
.lineCount
= 1; // Faking it. This doesn't really matter for now.
123 info
.startColumn
= 1;
124 info
.endColumn
= 1; // Faking it. This doesn't really matter for now.
125 info
.parametersStartOffset
= newProviderStr
.find("(");
126 info
.typeProfilingStartOffset
= newProviderStr
.find("{");
127 info
.typeProfilingEndOffset
= newProviderStr
.length() - 1;
130 SourceCode(newProvider
.release(), info
.typeProfilingStartOffset
, info
.typeProfilingEndOffset
+ 1, 1, 1);
133 bool FunctionOverrides::initializeOverrideFor(const SourceCode
& origCode
, FunctionOverrides::OverrideInfo
& result
)
135 ASSERT(Options::functionOverrides());
136 FunctionOverrides
& overrides
= FunctionOverrides::overrides();
138 auto it
= overrides
.m_entries
.find(origCode
.toString());
139 if (it
== overrides
.m_entries
.end())
142 initializeOverrideInfo(origCode
, it
->value
, result
);
146 #define SYNTAX_ERROR "SYNTAX ERROR"
147 #define IO_ERROR "IO ERROR"
148 #define FAIL_WITH_ERROR(error, errorMessageInBrackets) \
150 dataLog("functionOverrides ", error, ": "); \
151 dataLog errorMessageInBrackets; \
152 exit(EXIT_FAILURE); \
155 static bool hasDisallowedCharacters(const char* str
, size_t length
)
159 // '{' is also disallowed, but we don't need to check for it because
160 // parseClause() searches for '{' as the end of the start delimiter.
161 // As a result, the parsed delimiter string will never include '{'.
162 if (c
== '}' || isASCIISpace(c
))
168 static String
parseClause(const char* keyword
, size_t keywordLength
, FILE* file
, const char* line
, char* buffer
, size_t bufferSize
)
170 const char* keywordPos
= strstr(line
, keyword
);
172 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("Expecting '", keyword
, "' clause:\n", line
, "\n"));
173 if (keywordPos
!= line
)
174 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("Cannot have any characters before '", keyword
, "':\n", line
, "\n"));
175 if (line
[keywordLength
] != ' ')
176 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("'", keyword
, "' must be followed by a ' ':\n", line
, "\n"));
178 const char* delimiterStart
= &line
[keywordLength
+ 1];
179 const char* delimiterEnd
= strstr(delimiterStart
, "{");
181 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("Missing { after '", keyword
, "' clause start delimiter:\n", line
, "\n"));
183 size_t delimiterLength
= delimiterEnd
- delimiterStart
;
184 String
delimiter(delimiterStart
, delimiterLength
);
186 if (hasDisallowedCharacters(delimiterStart
, delimiterLength
))
187 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("Delimiter '", delimiter
, "' cannot have '{', '}', or whitespace:\n", line
, "\n"));
189 String terminatorString
;
190 terminatorString
.append("}");
191 terminatorString
.append(delimiter
);
193 const char* terminator
= terminatorString
.ascii().data();
194 line
= delimiterEnd
; // Start from the {.
196 StringBuilder builder
;
198 const char* p
= strstr(line
, terminator
);
200 if (p
[strlen(terminator
)] != '\n')
201 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("Unexpected characters after '", keyword
, "' clause end delimiter '", delimiter
, "':\n", line
, "\n"));
203 builder
.append(line
, p
- line
+ 1);
204 return builder
.toString();
206 builder
.append(line
);
208 } while ((line
= fgets(buffer
, bufferSize
, file
)));
210 FAIL_WITH_ERROR(SYNTAX_ERROR
, ("'", keyword
, "' clause end delimiter '", delimiter
, "' not found:\n", builder
.toString(), "\n", "Are you missing a '}' before the delimiter?\n"));
213 void FunctionOverrides::parseOverridesInFile(const char* fileName
)
218 FILE* file
= fopen(fileName
, "r");
220 FAIL_WITH_ERROR(IO_ERROR
, ("Failed to open file ", fileName
, ". Did you add the file-read-data entitlement to WebProcess.sb?\n"));
224 while ((line
= fgets(buffer
, sizeof(buffer
), file
))) {
225 if (strstr(line
, "//") == line
)
228 if (line
[0] == '\n' || line
[0] == '\0')
231 size_t keywordLength
;
233 keywordLength
= sizeof("override") - 1;
234 String keyStr
= parseClause("override", keywordLength
, file
, line
, buffer
, sizeof(buffer
));
236 line
= fgets(buffer
, sizeof(buffer
), file
);
238 keywordLength
= sizeof("with") - 1;
239 String valueStr
= parseClause("with", keywordLength
, file
, line
, buffer
, sizeof(buffer
));
241 m_entries
.add(keyStr
, valueStr
);
244 int result
= fclose(file
);
246 dataLogF("Failed to close file %s: %s\n", fileName
, strerror(errno
));