File: WasmExecutionHandler.h

package info (click to toggle)
webkit2gtk 2.51.91-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 484,084 kB
  • sloc: cpp: 3,931,583; javascript: 197,713; ansic: 167,619; python: 53,160; asm: 21,857; ruby: 18,114; perl: 17,149; xml: 4,631; sh: 2,462; yacc: 2,394; java: 2,032; lex: 1,358; pascal: 372; makefile: 215
file content (183 lines) | stat: -rw-r--r-- 6,423 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2025 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#if ENABLE(WEBASSEMBLY)

#include "StopTheWorldCallback.h"
#include "WasmDebugServer.h"
#include "WasmModuleManager.h"
#include "WasmVirtualAddress.h"

#include <wtf/Condition.h>
#include <wtf/HashMap.h>
#include <wtf/Lock.h>
#include <wtf/Locker.h>
#include <wtf/Vector.h>
#include <wtf/text/CString.h>

namespace JSC {

class CalleeBits;
class CallFrame;
class JSWebAssemblyInstance;
class VM;

namespace IPInt {
struct IPIntLocal;
struct IPIntStackEntry;
}

namespace Wasm {

class IPIntCallee;
class BreakpointManager;

class ExecutionHandler {
    WTF_MAKE_TZONE_ALLOCATED(ExecutionHandler);

public:
    ExecutionHandler(DebugServer&, ModuleManager&);

    enum class ResumeMode : uint8_t {
        One, // Resume only debuggee VM
        All, // Resume all VMs
        Switch, // Switch to current debuggee VM
    };

    enum class DebuggerState : uint8_t {
        Replied, // Sent reply to LLDB, waiting for next command.
        InterruptRequested, // Interrupt all mutators requested.
        ContinueRequested, // Resume all mutators requested.
        StepRequested, // Step debuggee requested.
        SwitchRequested, // Switch to debuggee requested.
    };

    ResumeMode stopCode(Locker<Lock>&, StopTheWorldEvent) WTF_REQUIRES_LOCK(m_lock);
    bool hitBreakpoint(CallFrame*, JSWebAssemblyInstance*, IPIntCallee*, uint8_t* pc, uint8_t* mc, IPInt::IPIntLocal* = nullptr, IPInt::IPIntStackEntry* = nullptr);

    JS_EXPORT_PRIVATE void resume();
    JS_EXPORT_PRIVATE void step();
    JS_EXPORT_PRIVATE void interrupt();
    void handleThreadStopInfo(StringView packet);
    String callStackStringFor(uint64_t threadId);
    JS_EXPORT_PRIVATE void reset();

    JS_EXPORT_PRIVATE void setBreakpointAtEntry(JSWebAssemblyInstance*, IPIntCallee*, Breakpoint::Type);
    void setBreakpointAtPC(JSWebAssemblyInstance*, FunctionCodeIndex, Breakpoint::Type, const uint8_t* pc);
    void setBreakpoint(StringView packet);
    void removeBreakpoint(StringView packet);
    JS_EXPORT_PRIVATE BreakpointManager* breakpointManager() { return m_breakpointManager.get(); };

    void setStepIntoBreakpointForCall(VM&, CalleeBits, JSWebAssemblyInstance*);
    void setStepIntoBreakpointForThrow(VM&);

    bool hasBreakpoints() const;

    JS_EXPORT_PRIVATE static uint64_t threadId(const VM&);
    uint64_t debugServerThreadId() const
    {
        RELEASE_ASSERT(m_debugServerThreadId.has_value());
        return *m_debugServerThreadId;
    }
    void setDebugServerThreadId(uint64_t threadId) { m_debugServerThreadId = threadId; }

    JS_EXPORT_PRIVATE DebugState* debuggeeState() const WTF_REQUIRES_LOCK(m_lock);
    JS_EXPORT_PRIVATE DebugState* debuggeeStateSafe() const; // FIXME: Should be used for test only

    VM* debuggeeVM() const // Used for test only
    {
        Locker locker { m_lock };
        return m_debuggee;
    }

    void notifyVMDestruction(VM* vm)
    {
        Locker locker { m_lock };
        if (m_debuggee == vm)
            m_debuggee = nullptr;
    }

    JS_EXPORT_PRIVATE void switchTarget(uint64_t threadId);

    StopTheWorldStatus handleStopTheWorld(VM&, StopTheWorldEvent);
    void handlePostResume();
    bool takeAwaitingResumeNotification() WTF_REQUIRES_LOCK(m_lock) { return std::exchange(m_awaitingResumeNotification, false); }

private:
    friend class DebugServer;

    void stopTheWorld(VM&, StopTheWorldEvent);

    ResumeMode stopImpl(Locker<Lock>&) WTF_REQUIRES_LOCK(m_lock);

    void resumeImpl(Locker<Lock>&) WTF_REQUIRES_LOCK(m_lock);

    bool stepAtBreakpoint(Locker<Lock>&, DebugState*) WTF_REQUIRES_LOCK(m_lock);

    void sendStopReply(AbstractLocker&);
    void sendStopReplyForThread(AbstractLocker&, uint64_t threadId);
    void sendReplyOK();
    void sendReply(StringView reply);
    void sendReplyImpl(AbstractLocker&, StringView reply);
    void sendErrorReply(ProtocolError);

    void selectDebuggeeIfNeeded(VM& fallbackVM) WTF_REQUIRES_LOCK(m_lock);
    void markVMStates(VM* debuggee) WTF_REQUIRES_LOCK(m_lock);
    void clearOtherVMStopData(VM* debuggee) WTF_REQUIRES_LOCK(m_lock);

    bool requiresStopConfirmation() const WTF_REQUIRES_LOCK(m_lock)
    {
        switch (m_debuggerState) {
        case DebuggerState::InterruptRequested:
        case DebuggerState::StepRequested:
        case DebuggerState::SwitchRequested:
            return true;
        default:
            return false;
        }
    }

    DebugServer& m_debugServer;
    ModuleManager& m_moduleManager;
    std::unique_ptr<BreakpointManager> m_breakpointManager;

    mutable Lock m_lock;
    Condition m_debuggerContinue;
    Condition m_debuggeeContinue;
    DebuggerState m_debuggerState WTF_GUARDED_BY_LOCK(m_lock) { DebuggerState::Replied };
    bool m_awaitingResumeNotification WTF_GUARDED_BY_LOCK(m_lock) { false };
    VM* m_debuggee WTF_GUARDED_BY_LOCK(m_lock) { nullptr };
    std::optional<uint64_t> m_debugServerThreadId;
};

StopTheWorldStatus wasmDebuggerOnStopCallback(VM&, StopTheWorldEvent);
void wasmDebuggerOnResumeCallback();

} // namespace Wasm
} // namespace JSC

#endif // ENABLE(WEBASSEMBLY)