Perform some code cleanup

This commit is contained in:
Justine Tunney 2023-05-28 19:42:00 -07:00
parent 992a4638ae
commit 72f8bd10b7
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
8 changed files with 55 additions and 265 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/state.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/atomic.h"
@ -249,17 +250,13 @@ static void __asan_memset(void *p, char c, size_t n) {
__builtin_memcpy(b + n - 8, &x, 8);
break;
default:
if (n <= 64) {
i = 0;
do {
__builtin_memcpy(b + i, &x, 8);
asm volatile("" ::: "memory");
__builtin_memcpy(b + i + 8, &x, 8);
} while ((i += 16) + 16 <= n);
for (; i < n; ++i) b[i] = x;
} else {
__repstosb(p, c, n);
}
i = 0;
do {
__builtin_memcpy(b + i, &x, 8);
asm volatile("" ::: "memory");
__builtin_memcpy(b + i + 8, &x, 8);
} while ((i += 16) + 16 <= n);
for (; i < n; ++i) b[i] = x;
break;
}
}
@ -317,18 +314,14 @@ static void *__asan_mempcpy(void *dst, const void *src, size_t n) {
__builtin_memcpy(d + n - 8, &b, 8);
return d + n;
default:
if (n <= 64) {
i = 0;
do {
__builtin_memcpy(&a, s + i, 8);
asm volatile("" ::: "memory");
__builtin_memcpy(d + i, &a, 8);
} while ((i += 8) + 8 <= n);
for (; i < n; ++i) d[i] = s[i];
return d + i;
} else {
return __repmovsb(d, s, n);
}
i = 0;
do {
__builtin_memcpy(&a, s + i, 8);
asm volatile("" ::: "memory");
__builtin_memcpy(d + i, &a, 8);
} while ((i += 8) + 8 <= n);
for (; i < n; ++i) d[i] = s[i];
return d + i;
}
}
@ -1426,8 +1419,14 @@ void __asan_map_shadow(uintptr_t p, size_t n) {
__asan_unpoison((char *)p, n);
}
static size_t __asan_strlen(const char *s) {
size_t i = 0;
while (s[i]) ++i;
return i;
}
static textstartup void __asan_shadow_string(char *s) {
__asan_map_shadow((intptr_t)s, __strlen(s) + 1);
__asan_map_shadow((intptr_t)s, __asan_strlen(s) + 1);
}
static textstartup void __asan_shadow_auxv(intptr_t *auxv) {
@ -1469,6 +1468,10 @@ static textstartup void __asan_shadow_existing_mappings(void) {
__asan_poison((void *)GetStackAddr(), GUARDSIZE, kAsanStackOverflow);
}
forceinline ssize_t __write_str(const char *s) {
return sys_write(2, s, __asan_strlen(s));
}
void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) {
static bool once;
if (!_cmpxchg(&once, false, true)) return;
@ -1491,7 +1494,7 @@ void __asan_init(int argc, char **argv, char **envp, intptr_t *auxv) {
__asan_map_shadow(0, 4096);
__asan_poison(0, GUARDSIZE, kAsanNullPage);
if (!IsWindows()) {
__sysv_mprotect((void *)0x7fff8000, 0x10000, PROT_READ);
sys_mprotect((void *)0x7fff8000, 0x10000, PROT_READ);
}
__asan_shadow_string_list(argv);
__asan_shadow_string_list(envp);

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/promises.internal.h"
@ -23,6 +24,7 @@
#include "libc/log/log.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
#define kBufSize 1024
@ -48,15 +50,15 @@ int IsDebuggerPresent(bool force) {
if (!PLEDGED(RPATH)) return false;
res = 0;
e = errno;
if ((fd = __sysv_open("/proc/self/status", O_RDONLY, 0)) >= 0) {
if ((got = __sysv_read(fd, buf, sizeof(buf) - 1)) > 0) {
if ((fd = __sys_openat(AT_FDCWD, "/proc/self/status", O_RDONLY, 0)) >= 0) {
if ((got = sys_read(fd, buf, sizeof(buf) - 1)) > 0) {
buf[got] = '\0';
if ((p = __strstr(buf, kPid))) {
p += sizeof(kPid) - 1;
res = __atoul(p);
}
}
__sysv_close(fd);
sys_close(fd);
}
errno = e;
return res;

View file

@ -16,9 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/log/libfatal.internal.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"
privileged void PrintSystemMappings(int outfd) {
@ -26,12 +27,12 @@ privileged void PrintSystemMappings(int outfd) {
ssize_t rc;
char buf[64];
if (!IsWindows()) {
if ((infd = __sysv_open("/proc/self/maps", O_RDONLY, 0)) >= 0) {
__sysv_write(outfd, "\n", 1);
while ((rc = __sysv_read(infd, buf, sizeof(buf))) > 0) {
__sysv_write(outfd, buf, rc);
if ((infd = __sys_openat(AT_FDCWD, "/proc/self/maps", O_RDONLY, 0)) >= 0) {
sys_write(outfd, "\n", 1);
while ((rc = sys_read(infd, buf, sizeof(buf))) > 0) {
sys_write(outfd, buf, rc);
}
}
__sysv_close(infd);
sys_close(infd);
}
}

View file

@ -20,7 +20,6 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/errno.h"
#include "libc/log/internal.h"
#include "libc/log/libfatal.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/termios.h"
@ -39,6 +38,12 @@
static bool __isrestorable;
static union metatermios __oldtermios;
static size_t __strlen(const char *s) {
size_t i = 0;
while (s[i]) ++i;
return i;
}
// called weakly by libc/calls/ioctl_tcsets.c to avoid pledge("tty")
void __on_ioctl_tcsets(int fd) {
int e;

View file

@ -166,10 +166,16 @@ static char *__ubsan_itpcpy(char *p, struct UbsanTypeDescriptor *t,
}
}
static size_t __ubsan_strlen(const char *s) {
size_t i = 0;
while (s[i]) ++i;
return i;
}
static const char *__ubsan_dubnul(const char *s, unsigned i) {
size_t n;
while (i--) {
if ((n = __strlen(s))) {
if ((n = __ubsan_strlen(s))) {
s += n + 1;
} else {
return NULL;