Clean up more code

- Found some bugs in LLVM compiler-rt library
- The useless LIBC_STUBS package is now deleted
- Improve the overflow checking story even further
- Get chibicc tests working in MODE=dbg mode again
- The libc/isystem/ headers now have correctly named guards
This commit is contained in:
Justine Tunney 2023-06-18 00:55:09 -07:00
parent afc58a8b41
commit d7c79f43ef
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
294 changed files with 912 additions and 1208 deletions

View file

@ -44,7 +44,6 @@ TEST_LIBC_CALLS_DIRECTDEPS = \
LIBC_SYSV_CALLS \
LIBC_RUNTIME \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_THREAD \
LIBC_TIME \

View file

@ -34,7 +34,6 @@ TEST_LIBC_DNS_DIRECTDEPS = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_X

View file

@ -26,7 +26,6 @@ TEST_LIBC_FMT_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_TINYMATH \

View file

@ -17,10 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdckdint.h"
#include "libc/intrin/kprintf.h"
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#define FAIL ++failed < 100
#define uintmax_t uint128_t
#define T int
@ -77,11 +79,11 @@ void test_ckd_add(void) {
int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("add %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "add %d + %d = %d vs. %d\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("add %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "add %d + %d overflow disagreement\n", x, y);
}
}
}
@ -94,11 +96,11 @@ void test_ckd_sub(void) {
int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("sub %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "sub %d - %d = %d vs. %d\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("sub %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "sub %d - %d overflow disagreement\n", x, y);
}
}
}
@ -111,11 +113,11 @@ void test_ckd_mul(void) {
int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("mul %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "mul %d * %d = %d vs. %d\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("mul %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "mul %d * %d overflow disagreement\n", x, y);
}
}
}
@ -128,11 +130,11 @@ void test_ckd_add_long(void) {
long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("add %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "add %ld + %ld = %ld vs. %ld\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("add %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "add %ld + %ld overflow disagreement\n", x, y);
}
}
}
@ -145,11 +147,11 @@ void test_ckd_sub_long(void) {
long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("sub %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "sub %ld - %ld = %ld vs. %ld\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("sub %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "sub %ld - %ld overflow disagreement\n", x, y);
}
}
}
@ -162,11 +164,11 @@ void test_ckd_mul_long(void) {
long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("mul %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "mul %ld * %ld = %ld vs. %ld\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("mul %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "mul %ld * %ld overflow disagreement\n", x, y);
}
}
}
@ -179,11 +181,11 @@ void test_ckd_add_int128(void) {
int128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("add %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "add %jjd * %jjd = %jjd vs. %jjd\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("add %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "add %jjd * %jjd overflow disagreement\n", x, y);
}
}
}
@ -196,11 +198,11 @@ void test_ckd_sub_int128(void) {
int128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("sub %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "sub %jjd * %jjd = %jjd vs. %jjd\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("sub %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "sub %jjd * %jjd overflow disagreement\n", x, y);
}
}
}
@ -213,11 +215,164 @@ void test_ckd_mul_int128(void) {
int128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && ++failed < 100) {
kprintf("mul %d * %d = %d vs. %d\n", x, y, z1, z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "mul %jjd * %jjd = %jjd vs. %jjd\n", x, y, z1, z2);
}
if (o1 != o2 && ++failed < 100) {
kprintf("mul %d * %d overflow disagreement\n", x, y);
if (o1 != o2 && FAIL) {
fprintf(stderr, "mul %jjd * %jjd overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_addu(void) {
for (int i = 0; i < ARRAYLEN(Vint); ++i) {
unsigned int x = Vint[i];
for (int j = 0; j < ARRAYLEN(Vint); ++j) {
unsigned int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "unsigned add %u + %u = %u vs. %u\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "unsigned add %u + %u overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_subu(void) {
for (int i = 0; i < ARRAYLEN(Vint); ++i) {
unsigned int x = Vint[i];
for (int j = 0; j < ARRAYLEN(Vint); ++j) {
unsigned int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "unsigned sub %u - %u = %u vs. %u\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "unsigned sub %u - %u overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_mulu(void) {
for (int i = 0; i < ARRAYLEN(Vint); ++i) {
unsigned int x = Vint[i];
for (int j = 0; j < ARRAYLEN(Vint); ++j) {
unsigned int z1, z2, o1, o2, y = Vint[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "unsigned mul %u * %u = %u vs. %u\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "unsigned mul %u * %u overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_addul(void) {
for (int i = 0; i < ARRAYLEN(Vlong); ++i) {
unsigned long x = Vlong[i];
for (int j = 0; j < ARRAYLEN(Vlong); ++j) {
unsigned long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "ulong add %lu + %lu = %lu vs. %lu\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "ulong add %lu + %lu overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_subul(void) {
for (int i = 0; i < ARRAYLEN(Vlong); ++i) {
unsigned long x = Vlong[i];
for (int j = 0; j < ARRAYLEN(Vlong); ++j) {
unsigned long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "ulong sub %lu - %lu = %lu vs. %lu\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "ulong sub %lu - %lu overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_mulul(void) {
for (int i = 0; i < ARRAYLEN(Vlong); ++i) {
unsigned long x = Vlong[i];
for (int j = 0; j < ARRAYLEN(Vlong); ++j) {
unsigned long z1, z2, o1, o2, y = Vlong[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "ulong mul %lu * %lu = %lu vs. %lu\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "ulong mul %lu * %lu overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_addull(void) {
for (int i = 0; i < ARRAYLEN(Vint128); ++i) {
uint128_t x = Vint128[i];
for (int j = 0; j < ARRAYLEN(Vint128); ++j) {
uint128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_add(&z1, x, y);
o2 = __builtin_add_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "u128 add %jju + %jju = %jju vs. %jju\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "u128 add %jju + %jju overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_subull(void) {
for (int i = 0; i < ARRAYLEN(Vint128); ++i) {
uint128_t x = Vint128[i];
for (int j = 0; j < ARRAYLEN(Vint128); ++j) {
uint128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_sub(&z1, x, y);
o2 = __builtin_sub_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "u128 sub %jju - %jju = %jju vs. %jju\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "u128 sub %jju - %jju overflow disagreement\n", x, y);
}
}
}
}
void test_ckd_mulull(void) {
for (int i = 0; i < ARRAYLEN(Vint128); ++i) {
uint128_t x = Vint128[i];
for (int j = 0; j < ARRAYLEN(Vint128); ++j) {
uint128_t z1, z2, o1, o2, y = Vint128[i];
o1 = ckd_mul(&z1, x, y);
o2 = __builtin_mul_overflow(x, y, &z2);
if (z1 != z2 && FAIL) {
fprintf(stderr, "u128 mul %jju * %jju = %jju vs. %jju\n", x, y, z1, z2);
}
if (o1 != o2 && FAIL) {
fprintf(stderr, "u128 mul %jju * %jju overflow disagreement\n", x, y);
}
}
}
@ -233,5 +388,14 @@ int main(int argc, char *argv[]) {
test_ckd_add_int128();
test_ckd_sub_int128();
test_ckd_mul_int128();
test_ckd_addu();
test_ckd_subu();
test_ckd_mulu();
test_ckd_addul();
test_ckd_subul();
test_ckd_mulul();
test_ckd_addull();
test_ckd_subull();
test_ckd_mulull();
return failed ? 1 : 0;
}

View file

@ -32,7 +32,6 @@ TEST_LIBC_INTRIN_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_THREAD \

View file

@ -36,7 +36,6 @@ TEST_LIBC_LOG_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_LOG \
LIBC_STR \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_SYSV \
LIBC_LOG \

View file

@ -39,7 +39,6 @@ TEST_LIBC_MEM_DIRECTDEPS = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_TESTLIB \

View file

@ -34,7 +34,6 @@ TEST_LIBC_NEXGEN32E_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_THREAD \
LIBC_TESTLIB \

View file

@ -32,7 +32,6 @@ TEST_LIBC_RUNTIME_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_THREAD \
LIBC_TESTLIB \

View file

@ -32,7 +32,6 @@ TEST_LIBC_SOCK_DIRECTDEPS = \
LIBC_SOCK \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_LOG \
LIBC_SYSV_CALLS \

View file

@ -31,7 +31,6 @@ TEST_LIBC_STDIO_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TINYMATH \
LIBC_TESTLIB \

View file

@ -41,7 +41,6 @@ TEST_LIBC_STR_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_SYSV_CALLS \
LIBC_TESTLIB \

View file

@ -26,6 +26,7 @@
#include "libc/str/str.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/subprocess.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/semaphore.h"
@ -107,7 +108,7 @@ TEST(sem_close, withUnnamedSemaphore_isUndefinedBehavior) {
SPAWN(fork);
IgnoreStderr();
sem_close(&sem);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
ASSERT_SYS(0, 0, sem_destroy(&sem));
}
@ -118,7 +119,7 @@ TEST(sem_destroy, withNamedSemaphore_isUndefinedBehavior) {
SPAWN(fork);
IgnoreStderr();
sem_destroy(sem);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
ASSERT_SYS(0, 0, sem_unlink("/boop"));
ASSERT_SYS(0, 0, sem_close(sem));
}

View file

@ -51,7 +51,7 @@ TEST(sem_post, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_post(&sem);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
}
TEST(sem_trywait, afterDestroyed_isUndefinedBehavior) {
@ -63,7 +63,7 @@ TEST(sem_trywait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_trywait(&sem);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
}
TEST(sem_wait, afterDestroyed_isUndefinedBehavior) {
@ -75,7 +75,7 @@ TEST(sem_wait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_wait(&sem);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
}
TEST(sem_timedwait, afterDestroyed_isUndefinedBehavior) {
@ -87,7 +87,7 @@ TEST(sem_timedwait, afterDestroyed_isUndefinedBehavior) {
ASSERT_SYS(0, 0, sem_destroy(&sem));
IgnoreStderr();
sem_timedwait(&sem, 0);
EXITS(23); // see __assert_fail
EXITS(128 + SIGILL); // see __assert_fail
}
void *Worker(void *arg) {

View file

@ -31,7 +31,6 @@ TEST_LIBC_THREAD_DIRECTDEPS = \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_THREAD \

View file

@ -24,7 +24,6 @@ TEST_LIBC_TIME_DIRECTDEPS = \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_TIME \

View file

@ -31,7 +31,6 @@ TEST_LIBC_TINYMATH_DIRECTDEPS = \
LIBC_STDIO \
LIBC_RUNTIME \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TESTLIB \
LIBC_TINYMATH \

View file

@ -32,7 +32,6 @@ TEST_LIBC_X_DIRECTDEPS = \
LIBC_STDIO \
LIBC_STR \
LIBC_SOCK \
LIBC_STUBS \
LIBC_SYSV \
LIBC_THREAD \
LIBC_TESTLIB \

View file

@ -30,7 +30,6 @@ TEST_LIBC_XED_TESTLIB_A_DIRECTDEPS = \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_TESTLIB \
LIBC_X \
THIRD_PARTY_XED
@ -75,7 +74,6 @@ TEST_LIBC_XED_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_RUNTIME \
LIBC_STUBS \
LIBC_TESTLIB \
TEST_LIBC_XED_TESTLIB \
THIRD_PARTY_XED

View file

@ -30,7 +30,6 @@ TEST_LIBC_ZIPOS_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_RUNTIME \
LIBC_STR \
LIBC_STUBS \
LIBC_THREAD \
LIBC_SYSV \
LIBC_ZIPOS \