Code cleanup

This commit is contained in:
topjohnwu
2025-12-13 22:12:30 -08:00
committed by John Wu
parent 33eaa7c5eb
commit 01cb75eaef
12 changed files with 383 additions and 463 deletions
+24 -31
View File
@@ -1,13 +1,9 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/prctl.h>
#include <sys/mman.h>
#include <android/log.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
#include <syscall.h>
#include <random>
#include <string>
#include <base.hpp>
#include <flags.h>
@@ -18,6 +14,12 @@ using namespace std;
#define __call_bypassing_fortify(fn) (&fn)
#endif
#ifdef __LP64__
static_assert(BLKGETSIZE64 == 0x80081272);
#else
static_assert(BLKGETSIZE64 == 0x80041272);
#endif
// Override libc++ new implementation to optimize final build size
void* operator new(std::size_t s) { return std::malloc(s); }
@@ -29,32 +31,19 @@ void* operator new[](std::size_t s, const std::nothrow_t&) noexcept { return std
void operator delete(void *p, const std::nothrow_t&) noexcept { std::free(p); }
void operator delete[](void *p, const std::nothrow_t&) noexcept { std::free(p); }
bool byte_view::contains(byte_view pattern) const {
return _buf != nullptr && memmem(_buf, _sz, pattern._buf, pattern._sz) != nullptr;
}
bool byte_view::operator==(byte_view rhs) const {
return _sz == rhs._sz && memcmp(_buf, rhs._buf, _sz) == 0;
}
void byte_data::swap(byte_data &o) {
std::swap(_buf, o._buf);
std::swap(_sz, o._sz);
}
rust::Vec<size_t> byte_data::patch(byte_view from, byte_view to) const {
rust::Vec<size_t> v;
if (_buf == nullptr)
if (ptr == nullptr)
return v;
auto p = _buf;
auto eof = _buf + _sz;
auto p = ptr;
auto eof = ptr + sz;
while (p < eof) {
p = static_cast<uint8_t *>(memmem(p, eof - p, from.data(), from.size()));
if (p == nullptr)
return v;
memset(p, 0, from.size());
memcpy(p, to.data(), to.size());
v.push_back(p - _buf);
v.push_back(p - ptr);
p += from.size();
}
return v;
@@ -374,30 +363,34 @@ sFILE make_file(FILE *fp) {
mmap_data::mmap_data(const char *name, bool rw) {
auto slice = rust::map_file(name, rw);
if (!slice.empty()) {
_buf = slice.data();
_sz = slice.size();
this->ptr = slice.data();
this->sz = slice.size();
}
}
mmap_data::mmap_data(int dirfd, const char *name, bool rw) {
auto slice = rust::map_file_at(dirfd, name, rw);
if (!slice.empty()) {
_buf = slice.data();
_sz = slice.size();
this->ptr = slice.data();
this->sz = slice.size();
}
}
mmap_data::mmap_data(int fd, size_t sz, bool rw) {
auto slice = rust::map_fd(fd, sz, rw);
if (!slice.empty()) {
_buf = slice.data();
_sz = slice.size();
this->ptr = slice.data();
this->sz = slice.size();
}
}
mmap_data::~mmap_data() {
if (_buf)
munmap(_buf, _sz);
if (ptr) munmap(ptr, sz);
}
void mmap_data::swap(mmap_data &o) {
std::swap(ptr, o.ptr);
std::swap(sz, o.sz);
}
string resolve_preinit_dir(const char *base_dir) {
@@ -420,7 +413,7 @@ extern "C" void cxx$utf8str$new(Utf8CStr *self, const void *s, size_t len);
extern "C" const char *cxx$utf8str$ptr(const Utf8CStr *self);
extern "C" size_t cxx$utf8str$len(const Utf8CStr *self);
Utf8CStr::Utf8CStr(const char *s, size_t len) {
Utf8CStr::Utf8CStr(const char *s, size_t len) : repr{} {
cxx$utf8str$new(this, s, len);
}