| 1 | /* |
| 2 | * Copyright (C) 2009-2018 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. ``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. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(YARR_JIT) |
| 29 | |
| 30 | #include "MacroAssemblerCodeRef.h" |
| 31 | #include "MatchResult.h" |
| 32 | #include "Yarr.h" |
| 33 | #include "YarrPattern.h" |
| 34 | |
| 35 | #if CPU(X86) && !COMPILER(MSVC) |
| 36 | #define YARR_CALL __attribute__ ((regparm (3))) |
| 37 | #else |
| 38 | #define YARR_CALL |
| 39 | #endif |
| 40 | |
| 41 | namespace JSC { |
| 42 | |
| 43 | class VM; |
| 44 | class ExecutablePool; |
| 45 | |
| 46 | namespace Yarr { |
| 47 | |
| 48 | enum class JITFailureReason : uint8_t { |
| 49 | DecodeSurrogatePair, |
| 50 | BackReference, |
| 51 | ForwardReference, |
| 52 | VariableCountedParenthesisWithNonZeroMinimum, |
| 53 | ParenthesizedSubpattern, |
| 54 | FixedCountParenthesizedSubpattern, |
| 55 | ParenthesisNestedTooDeep, |
| 56 | ExecutableMemoryAllocationFailure, |
| 57 | }; |
| 58 | |
| 59 | class YarrCodeBlock { |
| 60 | // Technically freeParenContext and parenContextSize are only used if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS) is set. Fortunately, all the calling conventions we support have caller save argument registers. |
| 61 | using YarrJITCode8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
| 62 | using YarrJITCode16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
| 63 | using YarrJITCodeMatchOnly8 = EncodedMatchResult (*)(const LChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
| 64 | using YarrJITCodeMatchOnly16 = EncodedMatchResult (*)(const UChar* input, unsigned start, unsigned length, void*, void* freeParenContext, unsigned parenContextSize) YARR_CALL; |
| 65 | |
| 66 | public: |
| 67 | YarrCodeBlock() = default; |
| 68 | |
| 69 | void setFallBackWithFailureReason(JITFailureReason failureReason) { m_failureReason = failureReason; } |
| 70 | Optional<JITFailureReason> failureReason() { return m_failureReason; } |
| 71 | |
| 72 | bool has8BitCode() { return m_ref8.size(); } |
| 73 | bool has16BitCode() { return m_ref16.size(); } |
| 74 | void set8BitCode(MacroAssemblerCodeRef<Yarr8BitPtrTag> ref) { m_ref8 = ref; } |
| 75 | void set16BitCode(MacroAssemblerCodeRef<Yarr16BitPtrTag> ref) { m_ref16 = ref; } |
| 76 | |
| 77 | bool has8BitCodeMatchOnly() { return m_matchOnly8.size(); } |
| 78 | bool has16BitCodeMatchOnly() { return m_matchOnly16.size(); } |
| 79 | void set8BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> matchOnly) { m_matchOnly8 = matchOnly; } |
| 80 | void set16BitCodeMatchOnly(MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> matchOnly) { m_matchOnly16 = matchOnly; } |
| 81 | |
| 82 | bool usesPatternContextBuffer() { return m_usesPatternContextBuffer; } |
| 83 | #if ENABLE(YARR_JIT_ALL_PARENS_EXPRESSIONS) |
| 84 | void setUsesPatternContextBuffer() { m_usesPatternContextBuffer = true; } |
| 85 | #endif |
| 86 | |
| 87 | MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) |
| 88 | { |
| 89 | ASSERT(has8BitCode()); |
| 90 | return MatchResult(untagCFunctionPtr<YarrJITCode8, Yarr8BitPtrTag>(m_ref8.code().executableAddress())(input, start, length, output, freeParenContext, parenContextSize)); |
| 91 | } |
| 92 | |
| 93 | MatchResult execute(const UChar* input, unsigned start, unsigned length, int* output, void* freeParenContext, unsigned parenContextSize) |
| 94 | { |
| 95 | ASSERT(has16BitCode()); |
| 96 | return MatchResult(untagCFunctionPtr<YarrJITCode16, Yarr16BitPtrTag>(m_ref16.code().executableAddress())(input, start, length, output, freeParenContext, parenContextSize)); |
| 97 | } |
| 98 | |
| 99 | MatchResult execute(const LChar* input, unsigned start, unsigned length, void* freeParenContext, unsigned parenContextSize) |
| 100 | { |
| 101 | ASSERT(has8BitCodeMatchOnly()); |
| 102 | return MatchResult(untagCFunctionPtr<YarrJITCodeMatchOnly8, YarrMatchOnly8BitPtrTag>(m_matchOnly8.code().executableAddress())(input, start, length, 0, freeParenContext, parenContextSize)); |
| 103 | } |
| 104 | |
| 105 | MatchResult execute(const UChar* input, unsigned start, unsigned length, void* freeParenContext, unsigned parenContextSize) |
| 106 | { |
| 107 | ASSERT(has16BitCodeMatchOnly()); |
| 108 | return MatchResult(untagCFunctionPtr<YarrJITCodeMatchOnly16, YarrMatchOnly16BitPtrTag>(m_matchOnly16.code().executableAddress())(input, start, length, 0, freeParenContext, parenContextSize)); |
| 109 | } |
| 110 | |
| 111 | #if ENABLE(REGEXP_TRACING) |
| 112 | void *get8BitMatchOnlyAddr() |
| 113 | { |
| 114 | if (!has8BitCodeMatchOnly()) |
| 115 | return 0; |
| 116 | |
| 117 | return m_matchOnly8.code().executableAddress(); |
| 118 | } |
| 119 | |
| 120 | void *get16BitMatchOnlyAddr() |
| 121 | { |
| 122 | if (!has16BitCodeMatchOnly()) |
| 123 | return 0; |
| 124 | |
| 125 | return m_matchOnly16.code().executableAddress(); |
| 126 | } |
| 127 | |
| 128 | void *get8BitMatchAddr() |
| 129 | { |
| 130 | if (!has8BitCode()) |
| 131 | return 0; |
| 132 | |
| 133 | return m_ref8.code().executableAddress(); |
| 134 | } |
| 135 | |
| 136 | void *get16BitMatchAddr() |
| 137 | { |
| 138 | if (!has16BitCode()) |
| 139 | return 0; |
| 140 | |
| 141 | return m_ref16.code().executableAddress(); |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | size_t size() const |
| 146 | { |
| 147 | return m_ref8.size() + m_ref16.size() + m_matchOnly8.size() + m_matchOnly16.size(); |
| 148 | } |
| 149 | |
| 150 | void clear() |
| 151 | { |
| 152 | m_ref8 = MacroAssemblerCodeRef<Yarr8BitPtrTag>(); |
| 153 | m_ref16 = MacroAssemblerCodeRef<Yarr16BitPtrTag>(); |
| 154 | m_matchOnly8 = MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag>(); |
| 155 | m_matchOnly16 = MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag>(); |
| 156 | m_failureReason = WTF::nullopt; |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | MacroAssemblerCodeRef<Yarr8BitPtrTag> m_ref8; |
| 161 | MacroAssemblerCodeRef<Yarr16BitPtrTag> m_ref16; |
| 162 | MacroAssemblerCodeRef<YarrMatchOnly8BitPtrTag> m_matchOnly8; |
| 163 | MacroAssemblerCodeRef<YarrMatchOnly16BitPtrTag> m_matchOnly16; |
| 164 | bool m_usesPatternContextBuffer { false }; |
| 165 | Optional<JITFailureReason> m_failureReason; |
| 166 | }; |
| 167 | |
| 168 | enum YarrJITCompileMode { |
| 169 | MatchOnly, |
| 170 | IncludeSubpatterns |
| 171 | }; |
| 172 | void jitCompile(YarrPattern&, String& patternString, YarrCharSize, VM*, YarrCodeBlock& jitObject, YarrJITCompileMode = IncludeSubpatterns); |
| 173 | |
| 174 | } } // namespace JSC::Yarr |
| 175 | |
| 176 | #endif |
| 177 | |