1/*
2 * Copyright (C) 2012 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#ifndef JITStubRoutine_h
27#define JITStubRoutine_h
28
29#include <wtf/Platform.h>
30
31#if ENABLE(JIT)
32
33#include "ExecutableAllocator.h"
34#include "MacroAssemblerCodeRef.h"
35#include <wtf/RefCounted.h>
36#include <wtf/Vector.h>
37
38namespace JSC {
39
40class JITStubRoutineSet;
41
42// This is a base-class for JIT stub routines, and also the class you want
43// to instantiate directly if you have a routine that does not need any
44// help from the GC. If in doubt, use one of the other stub routines. But
45// if you know for sure that the stub routine cannot be on the stack while
46// someone triggers a stub routine reset, then using this will speed up
47// memory reclamation. One case where a stub routine satisfies this
48// condition is if it doesn't make any calls, to either C++ or JS code. In
49// such a routine you know that it cannot be on the stack when anything
50// interesting happens.
51// See GCAwareJITStubRoutine.h for the other stub routines.
52class JITStubRoutine {
53    WTF_MAKE_NONCOPYABLE(JITStubRoutine);
54    WTF_MAKE_FAST_ALLOCATED;
55public:
56    JITStubRoutine(const MacroAssemblerCodeRef& code)
57        : m_code(code)
58        , m_refCount(1)
59    {
60    }
61
62    // Use this if you want to pass a CodePtr to someone who insists on taking
63    // a RefPtr<JITStubRoutine>.
64    static PassRefPtr<JITStubRoutine> createSelfManagedRoutine(
65        MacroAssemblerCodePtr rawCodePointer)
66    {
67        return adoptRef(new JITStubRoutine(MacroAssemblerCodeRef::createSelfManagedCodeRef(rawCodePointer)));
68    }
69
70    virtual ~JITStubRoutine();
71
72    // MacroAssemblerCodeRef is copyable, but at the cost of reference
73    // counting churn. Returning a reference is a good way of reducing
74    // the churn.
75    const MacroAssemblerCodeRef& code() const { return m_code; }
76
77    static MacroAssemblerCodePtr asCodePtr(PassRefPtr<JITStubRoutine> stubRoutine)
78    {
79        if (!stubRoutine)
80            return MacroAssemblerCodePtr();
81
82        MacroAssemblerCodePtr result = stubRoutine->code().code();
83        ASSERT(!!result);
84        return result;
85    }
86
87    void ref()
88    {
89        m_refCount++;
90    }
91
92    void deref()
93    {
94        if (--m_refCount)
95            return;
96        observeZeroRefCount();
97    }
98
99    // Helpers for the GC to determine how to deal with marking JIT stub
100    // routines.
101    uintptr_t startAddress() const { return m_code.executableMemory()->startAsInteger(); }
102    uintptr_t endAddress() const { return m_code.executableMemory()->endAsInteger(); }
103    static uintptr_t addressStep() { return jitAllocationGranule; }
104
105    static bool canPerformRangeFilter()
106    {
107#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
108        return true;
109#else
110        return false;
111#endif
112    }
113    static uintptr_t filteringStartAddress()
114    {
115#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
116        return startOfFixedExecutableMemoryPool;
117#else
118        UNREACHABLE_FOR_PLATFORM();
119        return 0;
120#endif
121    }
122    static size_t filteringExtentSize()
123    {
124#if ENABLE(EXECUTABLE_ALLOCATOR_FIXED)
125        return fixedExecutableMemoryPoolSize;
126#else
127        UNREACHABLE_FOR_PLATFORM();
128        return 0;
129#endif
130    }
131    static bool passesFilter(uintptr_t address)
132    {
133        if (!canPerformRangeFilter()) {
134            // Just check that the address doesn't use any special values that would make
135            // our hashtables upset.
136            return address >= jitAllocationGranule && address != std::numeric_limits<uintptr_t>::max();
137        }
138
139        if (address - filteringStartAddress() >= filteringExtentSize())
140            return false;
141
142        return true;
143    }
144
145protected:
146    virtual void observeZeroRefCount();
147
148    MacroAssemblerCodeRef m_code;
149    unsigned m_refCount;
150};
151
152// Helper for the creation of simple stub routines that need no help from the GC.
153#define FINALIZE_CODE_FOR_STUB(patchBuffer, dataLogFArguments) \
154    (adoptRef(new JITStubRoutine(FINALIZE_CODE((patchBuffer), dataLogFArguments))))
155
156#define FINALIZE_CODE_FOR_DFG_STUB(patchBuffer, dataLogFArguments) \
157    (adoptRef(new JITStubRoutine(FINALIZE_DFG_CODE((patchBuffer), dataLogFArguments))))
158
159} // namespace JSC
160
161#endif // ENABLE(JIT)
162
163#endif // JITStubRoutine_h
164
165