Emulator.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Emulator.h"
  7. #include "MmapRegion.h"
  8. #include "SimpleRegion.h"
  9. #include "SoftCPU.h"
  10. #include <AK/Debug.h>
  11. #include <AK/Format.h>
  12. #include <AK/LexicalPath.h>
  13. #include <AK/MappedFile.h>
  14. #include <LibELF/AuxiliaryVector.h>
  15. #include <LibELF/Image.h>
  16. #include <LibELF/Validation.h>
  17. #include <LibX86/ELFSymbolProvider.h>
  18. #include <fcntl.h>
  19. #include <syscall.h>
  20. #include <unistd.h>
  21. #if defined(__GNUC__) && !defined(__clang__)
  22. # pragma GCC optimize("O3")
  23. #endif
  24. namespace UserspaceEmulator {
  25. static constexpr u32 stack_location = 0x10000000;
  26. static constexpr size_t stack_size = 1 * MiB;
  27. static Emulator* s_the;
  28. Emulator& Emulator::the()
  29. {
  30. VERIFY(s_the);
  31. return *s_the;
  32. }
  33. Emulator::Emulator(const String& executable_path, const Vector<String>& arguments, const Vector<String>& environment)
  34. : m_executable_path(executable_path)
  35. , m_arguments(arguments)
  36. , m_environment(environment)
  37. , m_mmu(*this)
  38. , m_cpu(*this)
  39. {
  40. m_malloc_tracer = make<MallocTracer>(*this);
  41. static constexpr FlatPtr userspace_range_base = 0x00800000;
  42. static constexpr FlatPtr userspace_range_ceiling = 0xbe000000;
  43. #ifdef UE_ASLR
  44. static constexpr FlatPtr page_mask = 0xfffff000u;
  45. size_t random_offset = (get_random<u8>() % 32 * MiB) & page_mask;
  46. FlatPtr base = userspace_range_base + random_offset;
  47. #else
  48. FlatPtr base = userspace_range_base;
  49. #endif
  50. m_range_allocator.initialize_with_range(VirtualAddress(base), userspace_range_ceiling - base);
  51. VERIFY(!s_the);
  52. s_the = this;
  53. // setup_stack(arguments, environment);
  54. register_signal_handlers();
  55. setup_signal_trampoline();
  56. }
  57. Vector<ELF::AuxiliaryValue> Emulator::generate_auxiliary_vector(FlatPtr load_base, FlatPtr entry_eip, String executable_path, int executable_fd) const
  58. {
  59. // FIXME: This is not fully compatible with the auxiliary vector the kernel generates, this is just the bare
  60. // minimum to get the loader going.
  61. Vector<ELF::AuxiliaryValue> auxv;
  62. // PHDR/EXECFD
  63. // PH*
  64. auxv.append({ ELF::AuxiliaryValue::PageSize, PAGE_SIZE });
  65. auxv.append({ ELF::AuxiliaryValue::BaseAddress, (void*)load_base });
  66. auxv.append({ ELF::AuxiliaryValue::Entry, (void*)entry_eip });
  67. // FIXME: Don't hard code this? We might support other platforms later.. (e.g. x86_64)
  68. auxv.append({ ELF::AuxiliaryValue::Platform, "i386" });
  69. auxv.append({ ELF::AuxiliaryValue::ExecFilename, executable_path });
  70. auxv.append({ ELF::AuxiliaryValue::ExecFileDescriptor, executable_fd });
  71. auxv.append({ ELF::AuxiliaryValue::Null, 0L });
  72. return auxv;
  73. }
  74. void Emulator::setup_stack(Vector<ELF::AuxiliaryValue> aux_vector)
  75. {
  76. auto stack_region = make<SimpleRegion>(stack_location, stack_size);
  77. stack_region->set_stack(true);
  78. m_mmu.add_region(move(stack_region));
  79. m_cpu.set_esp(shadow_wrap_as_initialized<u32>(stack_location + stack_size));
  80. Vector<u32> argv_entries;
  81. for (auto& argument : m_arguments) {
  82. m_cpu.push_string(argument.characters());
  83. argv_entries.append(m_cpu.esp().value());
  84. }
  85. Vector<u32> env_entries;
  86. for (auto& variable : m_environment) {
  87. m_cpu.push_string(variable.characters());
  88. env_entries.append(m_cpu.esp().value());
  89. }
  90. for (auto& auxv : aux_vector) {
  91. if (!auxv.optional_string.is_empty()) {
  92. m_cpu.push_string(auxv.optional_string.characters());
  93. auxv.auxv.a_un.a_ptr = (void*)m_cpu.esp().value();
  94. }
  95. }
  96. for (ssize_t i = aux_vector.size() - 1; i >= 0; --i) {
  97. auto& value = aux_vector[i].auxv;
  98. m_cpu.push_buffer((const u8*)&value, sizeof(value));
  99. }
  100. m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // char** envp = { envv_entries..., nullptr }
  101. for (ssize_t i = env_entries.size() - 1; i >= 0; --i)
  102. m_cpu.push32(shadow_wrap_as_initialized(env_entries[i]));
  103. u32 envp = m_cpu.esp().value();
  104. m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // char** argv = { argv_entries..., nullptr }
  105. for (ssize_t i = argv_entries.size() - 1; i >= 0; --i)
  106. m_cpu.push32(shadow_wrap_as_initialized(argv_entries[i]));
  107. u32 argv = m_cpu.esp().value();
  108. m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // (alignment)
  109. u32 argc = argv_entries.size();
  110. m_cpu.push32(shadow_wrap_as_initialized(envp));
  111. m_cpu.push32(shadow_wrap_as_initialized(argv));
  112. m_cpu.push32(shadow_wrap_as_initialized(argc));
  113. m_cpu.push32(shadow_wrap_as_initialized<u32>(0)); // (alignment)
  114. }
  115. bool Emulator::load_elf()
  116. {
  117. auto file_or_error = MappedFile::map(m_executable_path);
  118. if (file_or_error.is_error()) {
  119. reportln("Unable to map {}: {}", m_executable_path, file_or_error.error());
  120. return false;
  121. }
  122. auto elf_image_data = file_or_error.value()->bytes();
  123. ELF::Image executable_elf(elf_image_data);
  124. if (!executable_elf.is_dynamic()) {
  125. // FIXME: Support static objects
  126. VERIFY_NOT_REACHED();
  127. }
  128. String interpreter_path;
  129. if (!ELF::validate_program_headers(*(const Elf32_Ehdr*)elf_image_data.data(), elf_image_data.size(), (const u8*)elf_image_data.data(), elf_image_data.size(), &interpreter_path)) {
  130. reportln("failed to validate ELF file");
  131. return false;
  132. }
  133. VERIFY(!interpreter_path.is_null());
  134. dbgln("interpreter: {}", interpreter_path);
  135. auto interpreter_file_or_error = MappedFile::map(interpreter_path);
  136. VERIFY(!interpreter_file_or_error.is_error());
  137. auto interpreter_image_data = interpreter_file_or_error.value()->bytes();
  138. ELF::Image interpreter_image(interpreter_image_data);
  139. constexpr FlatPtr interpreter_load_offset = 0x08000000;
  140. interpreter_image.for_each_program_header([&](const ELF::Image::ProgramHeader& program_header) {
  141. // Loader is not allowed to have its own TLS regions
  142. VERIFY(program_header.type() != PT_TLS);
  143. if (program_header.type() == PT_LOAD) {
  144. auto region = make<SimpleRegion>(program_header.vaddr().offset(interpreter_load_offset).get(), program_header.size_in_memory());
  145. if (program_header.is_executable() && !program_header.is_writable())
  146. region->set_text(true);
  147. memcpy(region->data(), program_header.raw_data(), program_header.size_in_image());
  148. memset(region->shadow_data(), 0x01, program_header.size_in_memory());
  149. if (program_header.is_executable()) {
  150. m_loader_text_base = region->base();
  151. m_loader_text_size = region->size();
  152. }
  153. mmu().add_region(move(region));
  154. return IterationDecision::Continue;
  155. }
  156. return IterationDecision::Continue;
  157. });
  158. auto entry_point = interpreter_image.entry().offset(interpreter_load_offset).get();
  159. m_cpu.set_eip(entry_point);
  160. // executable_fd will be used by the loader
  161. int executable_fd = open(m_executable_path.characters(), O_RDONLY);
  162. if (executable_fd < 0)
  163. return false;
  164. auto aux_vector = generate_auxiliary_vector(interpreter_load_offset, entry_point, m_executable_path, executable_fd);
  165. setup_stack(move(aux_vector));
  166. return true;
  167. }
  168. int Emulator::exec()
  169. {
  170. // X86::ELFSymbolProvider symbol_provider(*m_elf);
  171. X86::ELFSymbolProvider* symbol_provider = nullptr;
  172. constexpr bool trace = false;
  173. while (!m_shutdown) {
  174. m_cpu.save_base_eip();
  175. auto insn = X86::Instruction::from_stream(m_cpu, true, true);
  176. if constexpr (trace) {
  177. outln("{:p} \033[33;1m{}\033[0m", m_cpu.base_eip(), insn.to_string(m_cpu.base_eip(), symbol_provider));
  178. }
  179. (m_cpu.*insn.handler())(insn);
  180. if constexpr (trace) {
  181. m_cpu.dump();
  182. }
  183. if (m_pending_signals) [[unlikely]] {
  184. dispatch_one_pending_signal();
  185. }
  186. }
  187. if (auto* tracer = malloc_tracer())
  188. tracer->dump_leak_report();
  189. return m_exit_status;
  190. }
  191. Vector<FlatPtr> Emulator::raw_backtrace()
  192. {
  193. Vector<FlatPtr, 128> backtrace;
  194. backtrace.append(m_cpu.base_eip());
  195. // FIXME: Maybe do something if the backtrace has uninitialized data in the frame chain.
  196. u32 frame_ptr = m_cpu.ebp().value();
  197. while (frame_ptr) {
  198. u32 ret_ptr = m_mmu.read32({ 0x23, frame_ptr + 4 }).value();
  199. if (!ret_ptr)
  200. break;
  201. backtrace.append(ret_ptr);
  202. frame_ptr = m_mmu.read32({ 0x23, frame_ptr }).value();
  203. }
  204. return backtrace;
  205. }
  206. const MmapRegion* Emulator::find_text_region(FlatPtr address)
  207. {
  208. const MmapRegion* matching_region = nullptr;
  209. mmu().for_each_region([&](auto& region) {
  210. if (!is<MmapRegion>(region))
  211. return IterationDecision::Continue;
  212. const auto& mmap_region = static_cast<const MmapRegion&>(region);
  213. if (!(mmap_region.is_executable() && address >= mmap_region.base() && address < mmap_region.base() + mmap_region.size()))
  214. return IterationDecision::Continue;
  215. matching_region = &mmap_region;
  216. return IterationDecision::Break;
  217. });
  218. return matching_region;
  219. }
  220. String Emulator::create_backtrace_line(FlatPtr address)
  221. {
  222. auto minimal = String::formatted("=={{{}}}== {:p}", getpid(), (void*)address);
  223. const auto* region = find_text_region(address);
  224. if (!region)
  225. return minimal;
  226. auto separator_index = region->name().find(':');
  227. if (!separator_index.has_value())
  228. return minimal;
  229. String lib_name = region->name().substring(0, separator_index.value());
  230. String lib_path = lib_name;
  231. if (region->name().contains(".so"))
  232. lib_path = String::formatted("/usr/lib/{}", lib_path);
  233. if (!m_dynamic_library_cache.contains(lib_path)) {
  234. auto file_or_error = MappedFile::map(lib_path);
  235. if (file_or_error.is_error())
  236. return minimal;
  237. auto debug_info = make<Debug::DebugInfo>(make<ELF::Image>(file_or_error.value()->bytes()));
  238. m_dynamic_library_cache.set(lib_path, CachedELF { file_or_error.release_value(), move(debug_info) });
  239. }
  240. auto it = m_dynamic_library_cache.find(lib_path);
  241. auto& elf = it->value.debug_info->elf();
  242. String symbol = elf.symbolicate(address - region->base());
  243. auto line_without_source_info = String::formatted("=={{{}}}== {:p} [{}]: {}", getpid(), (void*)address, lib_name, symbol);
  244. auto source_position = it->value.debug_info->get_source_position(address - region->base());
  245. if (source_position.has_value())
  246. return String::formatted("=={{{}}}== {:p} [{}]: {} (\e[34;1m{}\e[0m:{})", getpid(), (void*)address, lib_name, symbol, LexicalPath(source_position.value().file_path).basename(), source_position.value().line_number);
  247. return line_without_source_info;
  248. }
  249. void Emulator::dump_backtrace(const Vector<FlatPtr>& backtrace)
  250. {
  251. for (auto& address : backtrace) {
  252. reportln("{}", create_backtrace_line(address));
  253. }
  254. }
  255. void Emulator::dump_backtrace()
  256. {
  257. dump_backtrace(raw_backtrace());
  258. }
  259. static void emulator_signal_handler(int signum)
  260. {
  261. Emulator::the().did_receive_signal(signum);
  262. }
  263. void Emulator::register_signal_handlers()
  264. {
  265. for (int signum = 0; signum < NSIG; ++signum)
  266. signal(signum, emulator_signal_handler);
  267. }
  268. enum class DefaultSignalAction {
  269. Terminate,
  270. Ignore,
  271. DumpCore,
  272. Stop,
  273. Continue,
  274. };
  275. static DefaultSignalAction default_signal_action(int signal)
  276. {
  277. VERIFY(signal && signal < NSIG);
  278. switch (signal) {
  279. case SIGHUP:
  280. case SIGINT:
  281. case SIGKILL:
  282. case SIGPIPE:
  283. case SIGALRM:
  284. case SIGUSR1:
  285. case SIGUSR2:
  286. case SIGVTALRM:
  287. case SIGSTKFLT:
  288. case SIGIO:
  289. case SIGPROF:
  290. case SIGTERM:
  291. return DefaultSignalAction::Terminate;
  292. case SIGCHLD:
  293. case SIGURG:
  294. case SIGWINCH:
  295. case SIGINFO:
  296. return DefaultSignalAction::Ignore;
  297. case SIGQUIT:
  298. case SIGILL:
  299. case SIGTRAP:
  300. case SIGABRT:
  301. case SIGBUS:
  302. case SIGFPE:
  303. case SIGSEGV:
  304. case SIGXCPU:
  305. case SIGXFSZ:
  306. case SIGSYS:
  307. return DefaultSignalAction::DumpCore;
  308. case SIGCONT:
  309. return DefaultSignalAction::Continue;
  310. case SIGSTOP:
  311. case SIGTSTP:
  312. case SIGTTIN:
  313. case SIGTTOU:
  314. return DefaultSignalAction::Stop;
  315. }
  316. VERIFY_NOT_REACHED();
  317. }
  318. void Emulator::dispatch_one_pending_signal()
  319. {
  320. int signum = -1;
  321. for (signum = 1; signum < NSIG; ++signum) {
  322. int mask = 1 << signum;
  323. if (m_pending_signals & mask)
  324. break;
  325. }
  326. VERIFY(signum != -1);
  327. m_pending_signals &= ~(1 << signum);
  328. auto& handler = m_signal_handler[signum];
  329. if (handler.handler == 0) {
  330. // SIG_DFL
  331. auto action = default_signal_action(signum);
  332. if (action == DefaultSignalAction::Ignore)
  333. return;
  334. reportln("\n=={}== Got signal {} ({}), no handler registered", getpid(), signum, strsignal(signum));
  335. dump_backtrace();
  336. m_shutdown = true;
  337. return;
  338. }
  339. if (handler.handler == 1) {
  340. // SIG_IGN
  341. return;
  342. }
  343. reportln("\n=={}== Got signal {} ({}), handler at {:p}", getpid(), signum, strsignal(signum), handler.handler);
  344. auto old_esp = m_cpu.esp();
  345. u32 stack_alignment = (m_cpu.esp().value() - 56) % 16;
  346. m_cpu.set_esp(shadow_wrap_as_initialized(m_cpu.esp().value() - stack_alignment));
  347. m_cpu.push32(shadow_wrap_as_initialized(m_cpu.eflags()));
  348. m_cpu.push32(shadow_wrap_as_initialized(m_cpu.eip()));
  349. m_cpu.push32(m_cpu.eax());
  350. m_cpu.push32(m_cpu.ecx());
  351. m_cpu.push32(m_cpu.edx());
  352. m_cpu.push32(m_cpu.ebx());
  353. m_cpu.push32(old_esp);
  354. m_cpu.push32(m_cpu.ebp());
  355. m_cpu.push32(m_cpu.esi());
  356. m_cpu.push32(m_cpu.edi());
  357. // FIXME: Push old signal mask here.
  358. m_cpu.push32(shadow_wrap_as_initialized(0u));
  359. m_cpu.push32(shadow_wrap_as_initialized((u32)signum));
  360. m_cpu.push32(shadow_wrap_as_initialized(handler.handler));
  361. m_cpu.push32(shadow_wrap_as_initialized(0u));
  362. VERIFY((m_cpu.esp().value() % 16) == 0);
  363. m_cpu.set_eip(m_signal_trampoline);
  364. }
  365. // Make sure the compiler doesn't "optimize away" this function:
  366. static void signal_trampoline_dummy() __attribute__((used));
  367. NEVER_INLINE void signal_trampoline_dummy()
  368. {
  369. // The trampoline preserves the current eax, pushes the signal code and
  370. // then calls the signal handler. We do this because, when interrupting a
  371. // blocking syscall, that syscall may return some special error code in eax;
  372. // This error code would likely be overwritten by the signal handler, so it's
  373. // necessary to preserve it here.
  374. asm(
  375. ".intel_syntax noprefix\n"
  376. "asm_signal_trampoline:\n"
  377. "push ebp\n"
  378. "mov ebp, esp\n"
  379. "push eax\n" // we have to store eax 'cause it might be the return value from a syscall
  380. "sub esp, 4\n" // align the stack to 16 bytes
  381. "mov eax, [ebp+12]\n" // push the signal code
  382. "push eax\n"
  383. "call [ebp+8]\n" // call the signal handler
  384. "add esp, 8\n"
  385. "mov eax, %P0\n"
  386. "int 0x82\n" // sigreturn syscall
  387. "asm_signal_trampoline_end:\n"
  388. ".att_syntax" ::"i"(Syscall::SC_sigreturn));
  389. }
  390. extern "C" void asm_signal_trampoline(void);
  391. extern "C" void asm_signal_trampoline_end(void);
  392. void Emulator::setup_signal_trampoline()
  393. {
  394. auto trampoline_region = make<SimpleRegion>(0xb0000000, 4096);
  395. u8* trampoline = (u8*)asm_signal_trampoline;
  396. u8* trampoline_end = (u8*)asm_signal_trampoline_end;
  397. size_t trampoline_size = trampoline_end - trampoline;
  398. u8* code_ptr = trampoline_region->data();
  399. memcpy(code_ptr, trampoline, trampoline_size);
  400. m_signal_trampoline = trampoline_region->base();
  401. mmu().add_region(move(trampoline_region));
  402. }
  403. bool Emulator::find_malloc_symbols(const MmapRegion& libc_text)
  404. {
  405. auto file_or_error = MappedFile::map("/usr/lib/libc.so");
  406. if (file_or_error.is_error())
  407. return false;
  408. ELF::Image image(file_or_error.value()->bytes());
  409. auto malloc_symbol = image.find_demangled_function("malloc");
  410. auto free_symbol = image.find_demangled_function("free");
  411. auto realloc_symbol = image.find_demangled_function("realloc");
  412. auto calloc_symbol = image.find_demangled_function("calloc");
  413. auto malloc_size_symbol = image.find_demangled_function("malloc_size");
  414. if (!malloc_symbol.has_value() || !free_symbol.has_value() || !realloc_symbol.has_value() || !malloc_size_symbol.has_value())
  415. return false;
  416. m_malloc_symbol_start = malloc_symbol.value().value() + libc_text.base();
  417. m_malloc_symbol_end = m_malloc_symbol_start + malloc_symbol.value().size();
  418. m_free_symbol_start = free_symbol.value().value() + libc_text.base();
  419. m_free_symbol_end = m_free_symbol_start + free_symbol.value().size();
  420. m_realloc_symbol_start = realloc_symbol.value().value() + libc_text.base();
  421. m_realloc_symbol_end = m_realloc_symbol_start + realloc_symbol.value().size();
  422. m_calloc_symbol_start = calloc_symbol.value().value() + libc_text.base();
  423. m_calloc_symbol_end = m_calloc_symbol_start + calloc_symbol.value().size();
  424. m_malloc_size_symbol_start = malloc_size_symbol.value().value() + libc_text.base();
  425. m_malloc_size_symbol_end = m_malloc_size_symbol_start + malloc_size_symbol.value().size();
  426. return true;
  427. }
  428. void Emulator::dump_regions() const
  429. {
  430. const_cast<SoftMMU&>(m_mmu).for_each_region([&](const Region& region) {
  431. reportln("{:p}-{:p} {:c}{:c}{:c} {} {}{}{} ",
  432. region.base(),
  433. region.end() - 1,
  434. region.is_readable() ? 'R' : '-',
  435. region.is_writable() ? 'W' : '-',
  436. region.is_executable() ? 'X' : '-',
  437. is<MmapRegion>(region) ? static_cast<const MmapRegion&>(region).name() : "",
  438. is<MmapRegion>(region) ? "(mmap) " : "",
  439. region.is_stack() ? "(stack) " : "",
  440. region.is_text() ? "(text) " : "");
  441. return IterationDecision::Continue;
  442. });
  443. }
  444. }