1 /*
2 * Copyright (C) 2014, 2016 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 "FunctionWhitelist.h"
28
29 #if ENABLE(JIT)
30
31 #include "CodeBlock.h"
32 #include <stdio.h>
33 #include <string.h>
34
35 namespace JSC {
36
37 FunctionWhitelist::FunctionWhitelist(const char* filename)
38 {
39 if (!filename)
40 return;
41
42 FILE* f = fopen(filename, "r");
43 if (!f) {
44 dataLogF("Failed to open file %s. Did you add the file-read-data entitlement to WebProcess.sb?\n", filename);
45 return;
46 }
47
48 m_hasActiveWhitelist = true;
49
50 char* line;
51 char buffer[BUFSIZ];
52 while ((line = fgets(buffer, sizeof(buffer), f))) {
53 if (strstr(line, "//") == line)
54 continue;
55
56 // Get rid of newlines at the ends of the strings.
57 size_t length = strlen(line);
58 if (line[length - 1] == '\n') {
59 line[length - 1] = '\0';
60 length--;
61 }
62
63 // Skip empty lines.
64 if (!length)
65 continue;
66
67 m_entries.add(String(line, length));
68 }
69
70 int result = fclose(f);
71 if (result)
72 dataLogF("Failed to close file %s: %s\n", filename, strerror(errno));
73 }
74
75 bool FunctionWhitelist::contains(CodeBlock* codeBlock) const
76 {
77 if (!m_hasActiveWhitelist)
78 return true;
79
80 if (m_entries.isEmpty())
81 return false;
82
83 String name = String::fromUTF8(codeBlock->inferredName());
84 if (m_entries.contains(name))
85 return true;
86
87 String hash = String::fromUTF8(codeBlock->hashAsStringIfPossible());
88 if (m_entries.contains(hash))
89 return true;
90
91 return m_entries.contains(name + '#' + hash);
92 }
93
94 } // namespace JSC
95
96 #endif // ENABLE(JIT)
97