2021-04-18 18:34:59 +00:00
|
|
|
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
|
|
|
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/macros.internal.h"
|
|
|
|
#include "libc/sysv/consts/auxv.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
.privileged
|
|
|
|
|
|
|
|
// Opens executable in O_RDWR mode.
|
|
|
|
//
|
|
|
|
// To avoid ETXTBSY we need to unmap the running executable first,
|
|
|
|
// then open the file, and finally load the code back into memory.
|
|
|
|
//
|
|
|
|
// @return file descriptor
|
|
|
|
// @note only works on .com binary (not .com.dbg)
|
|
|
|
// @note only supports linux, freebsd, openbsd, and netbsd
|
|
|
|
OpenExecutable:
|
|
|
|
push %rbp
|
|
|
|
mov %rsp,%rbp
|
|
|
|
pushq __NR_open(%rip) # -0x08(%rbp)
|
|
|
|
pushq __NR_mmap(%rip) # -0x10(%rbp)
|
|
|
|
pushq __NR_munmap(%rip) # -0x18(%rbp)
|
|
|
|
pushq O_RDWR(%rip) # -0x20(%rbp)
|
|
|
|
pushq MAP_ANONYMOUS(%rip) # -0x28(%rbp)
|
|
|
|
pushq MAP_PRIVATE(%rip) # -0x30(%rbp)
|
|
|
|
pushq MAP_FIXED(%rip) # -0x38(%rbp)
|
2021-05-02 18:50:43 +00:00
|
|
|
pushq __NR_mprotect(%rip) # -0x40(%rbp)
|
2022-03-15 00:13:28 +00:00
|
|
|
pushq O_RDONLY(%rip) # -0x48(%rbp)
|
2021-04-18 18:34:59 +00:00
|
|
|
push %rbx # code buffer
|
|
|
|
push %r12 # data buffer
|
|
|
|
push %r14 # filename
|
|
|
|
push %r15 # fd
|
|
|
|
|
|
|
|
// Get filename.
|
Improve ZIP filesystem and change its prefix
The ZIP filesystem has a breaking change. You now need to use /zip/ to
open() / opendir() / etc. assets within the ZIP structure of your APE
binary, instead of the previous convention of using zip: or zip! URIs.
This is needed because Python likes to use absolute paths, and having
ZIP paths encoded like URIs simply broke too many things.
Many more system calls have been updated to be able to operate on ZIP
files and file descriptors. In particular fcntl() and ioctl() since
Python would do things like ask if a ZIP file is a terminal and get
confused when the old implementation mistakenly said yes, because the
fastest way to guarantee native file descriptors is to dup(2). This
change also improves the async signal safety of zipos and ensures it
doesn't maintain any open file descriptors beyond that which the user
has opened.
This change makes a lot of progress towards adding magic numbers that
are specific to platforms other than Linux. The philosophy here is that,
if you use an operating system like FreeBSD, then you should be able to
take advantage of FreeBSD exclusive features, even if we don't polyfill
them on other platforms. For example, you can now open() a file with the
O_VERIFY flag. If your program runs on other platforms, then Cosmo will
automatically set O_VERIFY to zero. This lets you safely use it without
the need for #ifdef or ifstatements which detract from readability.
One of the blindspots of the ASAN memory hardening we use to offer Rust
like assurances has always been that memory passed to the kernel via
system calls (e.g. writev) can't be checked automatically since the
kernel wasn't built with MODE=asan. This change makes more progress
ensuring that each system call will verify the soundness of memory
before it's passed to the kernel. The code for doing these checks is
fast, particularly for buffers, where it can verify 64 bytes a cycle.
- Correct O_LOOP definition on NT
- Introduce program_executable_name
- Add ASAN guards to more system calls
- Improve termios compatibility with BSDs
- Fix bug in Windows auxiliary value encoding
- Add BSD and XNU specific errnos and open flags
- Add check to ensure build doesn't talk to internet
2021-08-22 08:04:18 +00:00
|
|
|
lea program_executable_name(%rip),%r14
|
2021-04-18 18:34:59 +00:00
|
|
|
|
|
|
|
// Allocate code buffer.
|
|
|
|
mov -0x10(%rbp),%eax # __NR_mmap
|
|
|
|
xor %edi,%edi
|
|
|
|
mov $PAGESIZE,%esi
|
2021-04-23 17:45:19 +00:00
|
|
|
mov $PROT_READ|PROT_WRITE,%edx
|
2021-04-18 18:34:59 +00:00
|
|
|
mov -0x28(%rbp),%r10d # MAP_ANONYMOUS
|
|
|
|
or -0x30(%rbp),%r10d # MAP_PRIVATE
|
|
|
|
mov $-1,%r8
|
|
|
|
mov $0,%r9
|
|
|
|
push %r9 # openbsd:pad
|
|
|
|
push %r9 # openbsd:align
|
|
|
|
syscall
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
mov %rax,%rbx
|
|
|
|
|
|
|
|
// Allocate data buffer.
|
|
|
|
mov -0x10(%rbp),%eax # __NR_mmap
|
|
|
|
xor %edi,%edi
|
|
|
|
mov $ape_ram_filesz,%esi
|
|
|
|
mov $PROT_READ|PROT_WRITE,%edx
|
|
|
|
mov -0x28(%rbp),%r10d # MAP_ANONYMOUS
|
|
|
|
or -0x30(%rbp),%r10d # MAP_PRIVATE
|
|
|
|
mov $-1,%r8
|
|
|
|
mov $0,%r9
|
|
|
|
push %r9 # openbsd:pad
|
|
|
|
push %r9 # openbsd:align
|
|
|
|
syscall
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
mov %rax,%r12
|
|
|
|
|
|
|
|
// Move data.
|
|
|
|
mov %r12,%rdi
|
|
|
|
mov $ape_ram_vaddr,%esi
|
|
|
|
mov $ape_ram_filesz,%ecx
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
// Move code.
|
|
|
|
mov %rbx,%rdi
|
|
|
|
mov $8f,%esi
|
|
|
|
mov $9f-8f,%ecx
|
|
|
|
rep movsb
|
2021-04-23 17:45:19 +00:00
|
|
|
|
|
|
|
// Change protection.
|
2021-05-02 18:50:43 +00:00
|
|
|
mov -0x40(%rbp),%eax # __NR_mprotect
|
2021-04-23 17:45:19 +00:00
|
|
|
mov %rbx,%rdi
|
|
|
|
mov $PAGESIZE,%esi
|
|
|
|
mov $PROT_READ|PROT_EXEC,%edx
|
|
|
|
syscall
|
|
|
|
|
2021-04-18 18:34:59 +00:00
|
|
|
jmp *%rbx
|
|
|
|
|
|
|
|
// <LIMBO>
|
|
|
|
|
|
|
|
// Unmap code segment.
|
|
|
|
8: mov -0x18(%rbp),%eax # __NR_munmap
|
|
|
|
mov $ape_rom_vaddr,%edi
|
|
|
|
mov $ape_rom_filesz,%esi
|
|
|
|
syscall
|
|
|
|
|
|
|
|
// Unmap data segment.
|
|
|
|
mov -0x18(%rbp),%eax # __NR_munmap
|
|
|
|
mov $ape_ram_vaddr,%edi
|
|
|
|
mov $ape_ram_filesz,%esi
|
|
|
|
syscall
|
|
|
|
|
|
|
|
// Open executable in read-write mode.
|
|
|
|
mov -0x08(%rbp),%eax # __NR_open
|
|
|
|
mov %r14,%rdi
|
|
|
|
mov -0x20(%rbp),%esi # O_RDWR
|
2022-03-15 00:13:28 +00:00
|
|
|
clc # clear carry flag
|
|
|
|
syscall
|
|
|
|
jc .Lohno # bsd error
|
|
|
|
cmp $-4095,%eax
|
|
|
|
jae .Lohno # linux error
|
|
|
|
jmp .Lok
|
|
|
|
|
|
|
|
// Open executable in read-only mode.
|
|
|
|
.Lohno: mov -0x08(%rbp),%eax # __NR_open
|
|
|
|
mov %r14,%rdi
|
|
|
|
mov -0x48(%rbp),%esi # O_RDONLY
|
|
|
|
syscall
|
|
|
|
|
|
|
|
.Lok: mov %eax,%r15d
|
2021-04-18 18:34:59 +00:00
|
|
|
|
|
|
|
// Map code segment.
|
|
|
|
mov -0x10(%rbp),%eax # __NR_mmap
|
|
|
|
mov $ape_rom_vaddr,%edi
|
|
|
|
mov $ape_rom_filesz,%esi
|
|
|
|
mov $PROT_READ|PROT_EXEC,%edx
|
|
|
|
mov -0x38(%rbp),%r10d # MAP_FIXED
|
2021-05-02 18:50:43 +00:00
|
|
|
or -0x30(%rbp),%r10d # MAP_PRIVATE
|
2021-04-18 18:34:59 +00:00
|
|
|
mov %r15d,%r8d
|
|
|
|
mov $ape_rom_offset,%r9d
|
|
|
|
push %r9 # openbsd:pad
|
|
|
|
push %r9 # openbsd:align
|
|
|
|
syscall
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
|
|
|
|
// Allocate data segment.
|
|
|
|
mov -0x10(%rbp),%eax # __NR_mmap
|
|
|
|
mov $ape_ram_vaddr,%edi
|
|
|
|
mov $ape_ram_filesz,%esi
|
|
|
|
mov $PROT_READ|PROT_WRITE,%edx
|
|
|
|
mov -0x38(%rbp),%r10d # MAP_FIXED
|
|
|
|
or -0x30(%rbp),%r10d # MAP_PRIVATE
|
|
|
|
or -0x28(%rbp),%r10d # MAP_ANONYMOUS
|
|
|
|
mov $-1,%r8
|
|
|
|
mov $0,%r9
|
|
|
|
push %r9 # openbsd:pad
|
|
|
|
push %r9 # openbsd:align
|
|
|
|
syscall
|
|
|
|
pop %r9
|
|
|
|
pop %r9
|
|
|
|
|
|
|
|
// Put data back.
|
|
|
|
mov $ape_ram_vaddr,%edi
|
2021-04-23 17:45:19 +00:00
|
|
|
mov %r12,%rsi
|
2021-04-18 18:34:59 +00:00
|
|
|
mov $ape_ram_filesz,%ecx
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
// Jump back.
|
|
|
|
mov $9f,%eax
|
|
|
|
jmp *%rax
|
|
|
|
|
|
|
|
// </LIMBO>
|
|
|
|
|
|
|
|
// Deallocate code buffer.
|
|
|
|
9: mov __NR_munmap,%eax
|
|
|
|
mov %rbx,%rdi
|
|
|
|
mov $PAGESIZE,%esi
|
|
|
|
syscall
|
|
|
|
|
|
|
|
// Deallocate data buffer.
|
|
|
|
mov __NR_munmap,%eax
|
|
|
|
mov %r12,%rdi
|
|
|
|
mov $ape_ram_filesz,%esi
|
|
|
|
syscall
|
|
|
|
|
|
|
|
mov %r15d,%eax
|
|
|
|
pop %r15
|
|
|
|
pop %r14
|
|
|
|
pop %r12
|
|
|
|
pop %rbx
|
|
|
|
leave
|
|
|
|
ret
|
|
|
|
9: .endfn OpenExecutable,globl
|
|
|
|
|
|
|
|
.weak ape_rom_vaddr
|
|
|
|
.weak ape_rom_filesz
|
|
|
|
.weak ape_rom_offset
|
|
|
|
.weak ape_ram_vaddr
|
|
|
|
.weak ape_ram_filesz
|