]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGFunctionWhitelist.cpp
JavaScriptCore-7600.1.4.9.tar.gz
[apple/javascriptcore.git] / dfg / DFGFunctionWhitelist.cpp
1 /*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "DFGFunctionWhitelist.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "CodeBlock.h"
32 #include "Options.h"
33 #include <stdio.h>
34 #include <string.h>
35 #include <wtf/NeverDestroyed.h>
36 #include <wtf/text/StringBuilder.h>
37
38 namespace JSC { namespace DFG {
39
40 FunctionWhitelist& FunctionWhitelist::ensureGlobalWhitelist()
41 {
42 static LazyNeverDestroyed<FunctionWhitelist> functionWhitelist;
43 static std::once_flag initializeWhitelistFlag;
44 std::call_once(initializeWhitelistFlag, [] {
45 const char* functionWhitelistFile = Options::dfgFunctionWhitelistFile();
46 functionWhitelist.construct(functionWhitelistFile);
47 });
48 return functionWhitelist;
49 }
50
51 FunctionWhitelist::FunctionWhitelist(const char* filename)
52 {
53 parseFunctionNamesInFile(filename);
54 }
55
56 void FunctionWhitelist::parseFunctionNamesInFile(const char* filename)
57 {
58 if (!filename)
59 return;
60
61 FILE* f = fopen(filename, "r");
62 if (!f) {
63 dataLogF("Failed to open file %s. Did you add the file-read-data entitlement to WebProcess.sb?\n", filename);
64 return;
65 }
66
67 char* line;
68 char buffer[BUFSIZ];
69 while ((line = fgets(buffer, sizeof(buffer), f))) {
70 if (strstr(line, "//") == line)
71 continue;
72
73 // Get rid of newlines at the ends of the strings.
74 size_t length = strlen(line);
75 if (line[length - 1] == '\n') {
76 line[length - 1] = '\0';
77 length--;
78 }
79
80 // Skip empty lines.
81 if (!length)
82 continue;
83
84 m_entries.add(String(line, length));
85 }
86
87 int result = fclose(f);
88 if (result)
89 dataLogF("Failed to close file %s: %s\n", filename, strerror(errno));
90 }
91
92 bool FunctionWhitelist::contains(CodeBlock* codeBlock) const
93 {
94 ASSERT(!isCompilationThread());
95 if (!Options::dfgFunctionWhitelistFile())
96 return true;
97
98 if (m_entries.isEmpty())
99 return false;
100
101 String name = String::fromUTF8(codeBlock->inferredName());
102 if (m_entries.contains(name))
103 return true;
104
105 String hash = String::fromUTF8(codeBlock->hashAsStringIfPossible());
106 if (m_entries.contains(hash))
107 return true;
108
109 String nameAndHash = name;
110 nameAndHash.append('#');
111 nameAndHash.append(hash);
112 return m_entries.contains(nameAndHash);
113 }
114
115 } } // namespace JSC::DFG
116
117 #endif // ENABLE(DFG_JIT)
118