Clean up some code

This commit is contained in:
Justine Tunney 2022-12-11 14:30:50 -08:00
parent 531bfbd61f
commit ed161b240e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
17 changed files with 107 additions and 182 deletions

View file

@ -60,7 +60,7 @@
# build/config.mk
SHELL = build/bootstrap/cocmd.com
HOSTS ?= freebsd openbsd netbsd rhel7 rhel5 xnu win10 win10:31336
HOSTS ?= freebsd openbsd netbsd rhel7 rhel5 xnu win10
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:

View file

@ -22,7 +22,7 @@
/**
* Converts timespec to scalar.
*
. * This returns the absolute number of nanoseconds in a timespec. If
* This returns the absolute number of nanoseconds in a timespec. If
* overflow happens, then `INT64_MAX` or `INT64_MIN` is returned. The
* `errno` variable isn't changed.
*

View file

@ -35,9 +35,10 @@
* @param bytes is number of bytes to allocate
* @return return 0 or EINVAL or ENOMEM w/o setting errno
* @see memalign()
* @returnserrno
* @threadsafe
*/
int posix_memalign(void **pp, size_t alignment, size_t bytes) {
errno_t posix_memalign(void **pp, size_t alignment, size_t bytes) {
int e;
void *m;
size_t q, r;
@ -46,11 +47,11 @@ int posix_memalign(void **pp, size_t alignment, size_t bytes) {
if (!r && q && IS2POW(q)) {
e = errno;
m = memalign(alignment, bytes);
errno = e;
if (m) {
*pp = m;
return 0;
} else {
errno = e;
return ENOMEM;
}
} else {

View file

@ -16,10 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/x/x.h"

View file

@ -214,7 +214,7 @@ syscon open O_VERIFY 0 0 0x00200000 0 0 0 #
syscon open O_SHLOCK 0 0x00000010 0x00000010 0x00000010 0x00000010 0 #
syscon open O_EXLOCK 0 0x00000020 0x00000020 0x00000020 0x00000020 0 #
syscon open O_TTY_INIT 0 0 0x00080000 0 0 0 #
syscon compat O_LARGEFILE 0100000 0 0 0 0 0 #
syscon compat O_LARGEFILE 0x00008000 0 0 0 0 0 #
# mmap() flags
# the revolutionary praxis of malloc()

View file

@ -1,2 +1,2 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon compat,O_LARGEFILE,0100000,0,0,0,0,0
.syscon compat,O_LARGEFILE,0x00008000,0,0,0,0,0

View file

@ -26,7 +26,7 @@
* pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
* // ...
* pthread_mutex_lock(&lock);
* pthread_cond_broadcast(&cond, &lock);
* pthread_cond_broadcast(&cond);
* pthread_mutex_unlock(&lock);
*
* This function has no effect if there aren't any threads currently

View file

@ -1,163 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 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/fmt/fmt.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/libfatal.internal.h"
#include "libc/log/log.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
noasan signed char ReadShadow(signed char *s) {
return *s;
}
void SetUp(void) {
if (!IsAsan()) exit(0);
}
TEST(asan, test) {
char *p;
p = gc(malloc(3));
EXPECT_TRUE(__asan_is_valid(0, 0));
EXPECT_TRUE(__asan_is_valid(p, 3));
EXPECT_FALSE(__asan_is_valid(p, 4));
EXPECT_TRUE(__asan_is_valid(p + 1, 2));
EXPECT_FALSE(__asan_is_valid(p + 1, 3));
p = gc(malloc(8 + 3));
EXPECT_TRUE(__asan_is_valid(p, 8 + 3));
EXPECT_FALSE(__asan_is_valid(p, 8 + 4));
EXPECT_TRUE(__asan_is_valid(p + 1, 8 + 2));
EXPECT_FALSE(__asan_is_valid(p + 1, 8 + 3));
p = gc(malloc(64 + 3));
EXPECT_TRUE(__asan_is_valid(p, 64 + 3));
EXPECT_FALSE(__asan_is_valid(p, 64 + 4));
EXPECT_TRUE(__asan_is_valid(p + 1, 64 + 2));
EXPECT_FALSE(__asan_is_valid(p + 1, 64 + 3));
EXPECT_FALSE(__asan_is_valid(p - 1, 64));
}
TEST(asan, test2) {
char *p;
p = gc(memalign(16, 64));
// asan design precludes this kind of poisoning
__asan_poison(p + 1, 1, kAsanProtected);
EXPECT_TRUE(__asan_is_valid(p, 2));
EXPECT_TRUE(__asan_is_valid(p + 1, 2));
// but we can do this
__asan_poison(p + 7, 1, kAsanProtected);
EXPECT_TRUE(__asan_is_valid(p + 6, 1));
EXPECT_FALSE(__asan_is_valid(p + 7, 1));
EXPECT_TRUE(__asan_is_valid(p + 8, 1));
EXPECT_FALSE(__asan_is_valid(p + 6, 2));
EXPECT_FALSE(__asan_is_valid(p + 7, 2));
EXPECT_FALSE(__asan_is_valid(p + 6, 3));
__asan_unpoison(p + 7, 1);
EXPECT_TRUE(__asan_is_valid(p + 6, 3));
}
TEST(asan, testEmptySize_isAlwaysValid) {
EXPECT_TRUE(__asan_is_valid(0, 0));
EXPECT_TRUE(__asan_is_valid((void *)(intptr_t)-2, 0));
EXPECT_TRUE(__asan_is_valid((void *)(intptr_t)9999, 0));
}
TEST(asan, testBigSize_worksFine) {
char *p;
p = malloc(64 * 1024);
EXPECT_TRUE(__asan_is_valid(p, 64 * 1024));
EXPECT_FALSE(__asan_is_valid(p - 1, 64 * 1024));
EXPECT_FALSE(__asan_is_valid(p + 1, 64 * 1024));
EXPECT_TRUE(__asan_is_valid(p + 1, 64 * 1024 - 1));
free(p);
}
TEST(asan, testUnmappedShadowMemory_doesntSegfault) {
EXPECT_FALSE(__asan_is_valid(0, 1));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-1, 1));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-2, 1));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)9999, 1));
EXPECT_FALSE(__asan_is_valid(0, 7));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-1, 7));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)-2, 7));
EXPECT_FALSE(__asan_is_valid((void *)(intptr_t)9999, 7));
}
TEST(asan, str) {
char *p;
int i, j, n, N = 64;
struct AsanFault f;
ASSERT_EQ(kAsanNullPage, __asan_check_str(0).kind);
p = malloc(N);
for (i = 0; i < N / 2; ++i) {
for (n = 1; n < N / 2; ++n) {
__asan_poison(p, i, kAsanProtected);
__asan_unpoison(p + i, n);
__asan_poison(p + i + n, N - i - n, kAsanProtected);
for (j = 0; j < n; ++j) {
p[i + j + 0] = j < n - 1;
}
// test valid string
f = __asan_check_str(p + i);
ASSERT_EQ(0, f.kind);
ASSERT_EQ(0, f.shadow);
// test pointer underrun
f = __asan_check_str(p + i - 1);
if (!i) {
ASSERT_EQ(kAsanHeapUnderrun, f.kind);
} else if (!((intptr_t)(p + i) & 7)) {
ASSERT_EQ(kAsanProtected, f.kind);
}
// test missing nul terminator
p[i + n - 1] = 1;
f = __asan_check_str(p + i);
if (i + n == N) {
ASSERT_EQ(kAsanHeapOverrun, f.kind);
} else if (i + n + 8 <= N) {
ASSERT_EQ(kAsanProtected, f.kind);
} else {
ASSERT_EQ(kAsanHeapOverrun, f.kind);
}
}
}
__asan_unpoison(p, N);
free(p);
}
BENCH(asan, bench) {
char *p;
size_t n, m;
m = 4 * 1024 * 1024;
p = gc(malloc(m));
EZBENCH_N("__asan_check", 0, EXPROPRIATE(__asan_check(p, 0).kind));
for (n = 2; n <= m; n *= 2) {
EZBENCH_N("__asan_check", n, EXPROPRIATE(__asan_check(p, n).kind));
}
memset(p, 1, m);
for (n = m; n >= 2; n /= 2) {
EZBENCH_N("__asan_check_str", n,
(p[n - 1] = 0, EXPROPRIATE(__asan_check_str(p).kind)));
}
}

View file

@ -2328,7 +2328,7 @@ static void OnKeyboardServiceReadKeyPress(void) {
static char buf[32];
static size_t pending;
pty->conf |= kPtyBlinkcursor;
if (!pending && !(pending = ReadAnsi(0, buf, sizeof(buf)))) {
if (!pending && !(pending = ReadAnsi(ttyin, buf, sizeof(buf)))) {
exitcode = 0;
action |= EXIT;
return;

72
tool/build/fastdiff.c Normal file
View file

@ -0,0 +1,72 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 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/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
/**
* @fileoverview scalable diff tool
*
* The normal `diff` command can take hours to diff text files that are
* hundreds of megabytes in size. This tool is a useful replacement for
* use cases like comparing a log of CPU registers.
*/
char line1[4096];
char line2[4096];
int main(int argc, char *argv[]) {
int line;
char *l1, *l2;
FILE *f1, *f2;
int differences;
if (argc < 3) {
fprintf(stderr, "usage: %s FILE1 FILE2\n", argv[0]);
exit(1);
}
if (!(f1 = fopen(argv[1], "r"))) {
perror(argv[1]);
exit(1);
}
if (!(f2 = fopen(argv[2], "r"))) {
perror(argv[2]);
exit(1);
}
for (differences = 0, line = 1;; ++line) {
l1 = fgets(line1, sizeof(line1), f1);
l2 = fgets(line2, sizeof(line2), f2);
if (!l1 && !l2) {
exit(0);
}
if (l1) _chomp(l1);
if (l2) _chomp(l2);
if (!l1 || !l2) {
printf("> %s\n", l1 ? l1 : "EOF");
printf("< %s\n", l2 ? l2 : "EOF");
exit(0);
}
if (!strcmp(l1, l2)) {
printf("%s\n", l1);
} else {
printf("# line %d differed!\n", line);
printf("> %s\n", l1);
printf("< %s\n", l2);
}
}
}

View file

@ -469,7 +469,7 @@ int64_t Ror32(uint64_t x64, uint64_t y, uint32_t *f) {
uint32_t x = x64;
if ((y &= 31)) {
x = x >> y | x << (32 - y);
return RotateFlags(x, x >> 31, f, (x >> 31) ^ (x >> 30) & 1);
return RotateFlags(x, x >> 31, f, ((x >> 31) ^ (x >> 30)) & 1);
} else {
return x;
}
@ -478,7 +478,7 @@ int64_t Ror32(uint64_t x64, uint64_t y, uint32_t *f) {
int64_t Ror64(uint64_t x, uint64_t y, uint32_t *f) {
if ((y &= 63)) {
x = x >> y | x << (64 - y);
return RotateFlags(x, x >> 63, f, (x >> 63) ^ (x >> 62) & 1);
return RotateFlags(x, x >> 63, f, ((x >> 63) ^ (x >> 62)) & 1);
} else {
return x;
}
@ -498,7 +498,7 @@ int64_t Ror8(uint64_t x64, uint64_t y, uint32_t *f) {
uint8_t x = x64;
if (y & 31) {
if ((y &= 7)) x = x >> y | x << (8 - y);
return RotateFlags(x, x >> 7, f, (x >> 7) ^ (x >> 6) & 1);
return RotateFlags(x, x >> 7, f, ((x >> 7) ^ (x >> 6)) & 1);
} else {
return x;
}
@ -756,7 +756,7 @@ int64_t Ror16(uint64_t x64, uint64_t y, uint32_t *f) {
uint16_t x = x64;
if (y & 31) {
if ((y &= 15)) x = x >> y | x << (16 - y);
return RotateFlags(x, x >> 15, f, (x >> 15) ^ (x >> 14) & 1);
return RotateFlags(x, x >> 15, f, ((x >> 15) ^ (x >> 14)) & 1);
} else {
return x;
}

View file

@ -19,6 +19,7 @@
#include "libc/fmt/bing.internal.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/tpenc.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/mem/arraylist2.internal.h"
#include "libc/mem/mem.h"
@ -90,7 +91,7 @@ static char *DisAddr(struct Dis *d, char *p) {
int64_t x = d->addr;
if (0 <= x && x < 0x10fff0) {
return p + uint64toarray_fixed16(x, p, 24);
} else if (-2147483648 <= x && x <= 2147483647) {
} else if (INT_MIN <= x && x <= INT_MAX) {
return p + uint64toarray_fixed16(x, p, 32);
} else {
return p + uint64toarray_fixed16(x, p, 48);

View file

@ -186,8 +186,9 @@ long DisFindSym(struct Dis *d, int64_t addr) {
l = m + 1;
}
}
if (r && d->syms.p[r - 1].addr < 256) {
/* XXX: prevent skewed binbase from doing weirdness */
// TODO(jart): This was <256 but that broke SectorLISP debugging
// Why did the Cosmo binbase bootloader need this?
if (r && d->syms.p[r - 1].addr < 32) {
return -1;
}
if (r && (addr == d->syms.p[r - 1].addr ||

View file

@ -64,6 +64,7 @@
(gcc-builtin-functions
'("__has_attribute"
"__has_builtin"
"__has_feature"
"__has_cpp_attribute"
"__builtin_va_arg"
"__builtin_va_copy"

View file

@ -11,11 +11,15 @@
(c11
'("_Atomic"
"alignas"
"_Alignas"
"alignof"
"_Alignof"
"_Noreturn"
"_Generic"
"thread_local"
"_Thread_local"
"static_assert"
"_Static_assert"
"_Complex_I"
"_Imaginary_I"))
@ -30,8 +34,9 @@
"textexit"
"externinline"
"dontinline"
"noclone"
"dontclone"
"donothing"
"notsan"
"printfesque"
"flattenout"
"mallocesque"
@ -68,7 +73,9 @@
"frownedupon"
"wontreturn"
"noasan"
"nomsan"
"noubsan"
"smashmystack"
"initarray"
"mayalias"
"noinstrument"
@ -171,6 +178,7 @@
"__no_sanitize_address__"
"__no_address_safety_analysis__"
"__no_sanitize_thread__"
"__no_stack_protector__"
"__leaf__"
"__no_sanitize_undefined__"
"__no_split_stack__"
@ -203,7 +211,9 @@
(clang
'("__optnone__"
"__nodebug__"))
"__nodebug__"
"musttail"
"__musttail__"))
)
(concat "\\_<"

View file

@ -57,6 +57,7 @@
"__SSP_EXPLICIT__"
"__SANITIZE_ADDRESS__"
"__SANITIZE_THREAD__"
"__SANITIZE_MEMORY__"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2"
"__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4"

View file

@ -164,7 +164,7 @@
((eq arg 3) "rel")
((eq arg 4) "dbg")
((eq arg 5) "")
((eq arg 6) "optlinux")
((eq arg 6) "llvm")
((eq arg 7) "tinylinux")
((eq arg 8) "tsan")
(default default)