summaryrefslogtreecommitdiff
path: root/DevTools/UserspaceEmulator/Emulator.h
blob: 0dcb1016f8d3acb9bb2e77655e053546ca2b0209 (plain)
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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER 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

#include "MallocTracer.h"
#include "Report.h"
#include "SoftCPU.h"
#include "SoftMMU.h"
#include <AK/Types.h>
#include <LibDebug/DebugInfo.h>
#include <LibELF/Loader.h>
#include <LibX86/Instruction.h>
#include <signal.h>
#include <sys/types.h>

namespace UserspaceEmulator {

class MallocTracer;

class Emulator {
public:
    static Emulator& the();

    Emulator(const Vector<String>& arguments, const Vector<String>& environment, NonnullRefPtr<ELF::Loader>);

    bool load_elf();
    void dump_backtrace();
    void dump_backtrace(const Vector<FlatPtr>&);
    Vector<FlatPtr> raw_backtrace();

    int exec();
    u32 virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3);

    SoftMMU& mmu() { return m_mmu; }

    MallocTracer* malloc_tracer() { return m_malloc_tracer; }

    bool is_in_malloc_or_free() const;

    void did_receive_signal(int signum) { m_pending_signals |= (1 << signum); }

private:
    NonnullRefPtr<ELF::Loader> m_elf;
    OwnPtr<Debug::DebugInfo> m_debug_info;

    SoftMMU m_mmu;
    SoftCPU m_cpu;

    OwnPtr<MallocTracer> m_malloc_tracer;

    void setup_stack(const Vector<String>& arguments, const Vector<String>& environment);
    void register_signal_handlers();
    void setup_signal_trampoline();

    int virt$fork();
    int virt$execve(FlatPtr);
    int virt$access(FlatPtr, size_t, int);
    int virt$sigaction(int, FlatPtr, FlatPtr);
    int virt$sigreturn();
    int virt$get_dir_entries(int fd, FlatPtr buffer, ssize_t);
    int virt$ioctl(int fd, unsigned, FlatPtr);
    int virt$stat(FlatPtr);
    int virt$realpath(FlatPtr);
    int virt$gethostname(FlatPtr, ssize_t);
    int virt$shbuf_create(int size, FlatPtr buffer);
    int virt$shbuf_allow_pid(int, pid_t peer_pid);
    int virt$shbuf_allow_all(int);
    FlatPtr virt$shbuf_get(int shbuf_id, FlatPtr size);
    int virt$shbuf_release(int shbuf_id);
    int virt$shbuf_seal(int shbuf_id);
    int virt$shbuf_set_volatile(int shbuf_id, bool);
    u32 virt$mmap(u32);
    u32 virt$mount(u32);
    u32 virt$munmap(FlatPtr address, u32 size);
    u32 virt$gettid();
    u32 virt$getpid();
    u32 virt$unveil(u32);
    u32 virt$pledge(u32);
    uid_t virt$getuid();
    gid_t virt$getgid();
    int virt$setuid(uid_t);
    int virt$setgid(gid_t);
    u32 virt$read(int, FlatPtr, ssize_t);
    u32 virt$write(int, FlatPtr, ssize_t);
    u32 virt$mprotect(FlatPtr, size_t, int);
    u32 virt$madvise(FlatPtr, size_t, int);
    u32 virt$open(u32);
    int virt$pipe(FlatPtr pipefd, int flags);
    int virt$close(int);
    int virt$mkdir(FlatPtr path, size_t path_length, mode_t mode);
    int virt$unlink(FlatPtr path, size_t path_length);
    int virt$get_process_name(FlatPtr buffer, int size);
    int virt$set_mmap_name(FlatPtr);
    int virt$set_process_icon(int);
    int virt$gettimeofday(FlatPtr);
    int virt$clock_gettime(int, FlatPtr);
    int virt$dbgputstr(FlatPtr characters, int length);
    int virt$dbgputch(char);
    int virt$fchmod(int, mode_t);
    int virt$listen(int, int);
    int virt$kill(pid_t, int);
    int virt$fstat(int, FlatPtr);
    u32 virt$fcntl(int fd, int, u32);
    int virt$getgroups(ssize_t count, FlatPtr);
    int virt$lseek(int fd, off_t offset, int whence);
    int virt$socket(int, int, int);
    int virt$getsockopt(FlatPtr);
    int virt$setsockopt(FlatPtr);
    int virt$select(FlatPtr);
    int virt$accept(int sockfd, FlatPtr address, FlatPtr address_length);
    int virt$bind(int sockfd, FlatPtr address, socklen_t address_length);
    int virt$recvmsg(int sockfd, FlatPtr msg_addr, int flags);
    int virt$sendmsg(int sockfd, FlatPtr msg_addr, int flags);
    int virt$connect(int sockfd, FlatPtr address, socklen_t address_size);
    void virt$exit(int);
    ssize_t virt$getrandom(FlatPtr buffer, size_t buffer_size, unsigned int flags);
    int virt$chdir(FlatPtr, size_t);
    int virt$dup2(int, int);
    int virt$getpgrp();
    int virt$getpgid(pid_t);
    int virt$setpgid(pid_t pid, pid_t pgid);
    int virt$ttyname(int fd, FlatPtr buffer, size_t buffer_size);
    int virt$getcwd(FlatPtr buffer, size_t buffer_size);
    int virt$waitid(FlatPtr);
    int virt$getsid(pid_t);

    FlatPtr allocate_vm(size_t size, size_t alignment);

    void dispatch_one_pending_signal();

    bool m_shutdown { false };
    int m_exit_status { 0 };

    FlatPtr m_malloc_symbol_start { 0 };
    FlatPtr m_malloc_symbol_end { 0 };
    FlatPtr m_free_symbol_start { 0 };
    FlatPtr m_free_symbol_end { 0 };

    sigset_t m_pending_signals { 0 };
    sigset_t m_signal_mask { 0 };

    struct SignalHandlerInfo {
        FlatPtr handler { 0 };
        sigset_t mask { 0 };
        int flags { 0 };
    };
    SignalHandlerInfo m_signal_handler[NSIG];

    FlatPtr m_signal_trampoline { 0 };
};

}