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