Improve cosmo's conformance to libc-test

This change addresses various open source compatibility issues, so that
we pass 313/411 of the tests in https://github.com/jart/libc-test where
earlier today we were passing about 30/411 of them, due to header toil.
Please note that Glibc only passes 341/411 so 313 today is pretty good!

- Make the conformance of libc/isystem/ headers nearly perfect
- Import more of the remaining math library routines from Musl
- Fix inconsistencies with type signatures of calls like umask
- Write tests for getpriority/setpriority which work great now
- conform to `struct sockaddr *` on remaining socket functions
- Import a bunch of uninteresting stdlib functions e.g. rand48
- Introduce readdir_r, scandir, pthread_kill, sigsetjmp, etc..

Follow the instructions in our `tool/scripts/cosmocc` toolchain to run
these tests yourself. You use `make CC=cosmocc` on the test repository
This commit is contained in:
Justine Tunney 2022-10-10 17:52:41 -07:00
parent 467a332e38
commit e557058ac8
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
189 changed files with 5091 additions and 884 deletions

View file

@ -38,7 +38,7 @@
static int malloc_lock(atomic_int *lk) {
if (!__threaded) return 0;
while (atomic_exchange_explicit(lk, 1, memory_order_acquire)) {
sched_yield();
donothing;
}
return 0;
}

View file

@ -1,25 +1,29 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_GETOPT_GETOPT_H_
#define COSMOPOLITAN_THIRD_PARTY_GETOPT_GETOPT_H_
#define no_argument 0
#define required_argument 1
#define optional_argument 2
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern char *optarg;
extern int optind, opterr, optopt, optreset;
int getopt(int nargc, char *const nargv[], const char *ostr);
#define no_argument 0
#define required_argument 1
#define optional_argument 2
int getopt(int, char *const[], const char *);
int getsubopt(char **, char *const *, char **);
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
int getopt_long(int nargc, char *const *nargv, const char *options,
const struct option *long_options, int *idx);
int getopt_long_only(int nargc, char *const *nargv, const char *options,
const struct option *long_options, int *idx);
int getopt_long(int, char *const *, const char *, const struct option *, int *);
int getopt_long_only(int, char *const *, const char *, const struct option *,
int *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

91
third_party/getopt/getsubopt.c vendored Normal file
View file

@ -0,0 +1,91 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
Copyright (c) 1990, 1993
The Regents of the University of California. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "libc/str/str.h"
#include "third_party/getopt/getopt.h"
// clang-format off
/*
* The SVID interface to getsubopt provides no way of figuring out which
* part of the suboptions list wasn't matched. This makes error messages
* tricky... The extern variable suboptarg is a pointer to the token
* which didn't match.
*/
char *suboptarg;
int
getsubopt(char **optionp, char * const *tokens, char **valuep)
{
int cnt;
char *p;
suboptarg = *valuep = NULL;
if (!optionp || !*optionp)
return(-1);
/* skip leading white-space, commas */
for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
if (!*p) {
*optionp = p;
return(-1);
}
/* save the start of the token, and skip the rest of the token. */
for (suboptarg = p;
*++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
if (*p) {
/*
* If there's an equals sign, set the value pointer, and
* skip over the value part of the token. Terminate the
* token.
*/
if (*p == '=') {
*p = '\0';
for (*valuep = ++p;
*p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
if (*p)
*p++ = '\0';
} else
*p++ = '\0';
/* Skip any whitespace or commas after this token. */
for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
}
/* set optionp for next round. */
*optionp = p;
for (cnt = 0; *tokens; ++tokens, ++cnt)
if (!strcmp(suboptarg, *tokens))
return(cnt);
return(-1);
}

View file

@ -62,6 +62,7 @@
#include "libc/sock/struct/ifconf.h"
#include "libc/sock/struct/linger.h"
#include "libc/sock/struct/pollfd.h"
#include "libc/sock/struct/sockaddr.h"
#include "libc/sock/syslog.h"
#include "libc/stdio/append.h"
#include "libc/stdio/stdio.h"
@ -858,7 +859,8 @@ static int LuaUnixSetpgid(lua_State *L) {
setpgid(luaL_checkinteger(L, 1), luaL_checkinteger(L, 2)));
}
static dontinline int LuaUnixSetid(lua_State *L, const char *call, int f(int)) {
static dontinline int LuaUnixSetid(lua_State *L, const char *call,
int f(unsigned)) {
int olderr = errno;
return SysretBool(L, call, olderr, f(luaL_checkinteger(L, 1)));
}
@ -1373,8 +1375,9 @@ static int LuaUnixBind(lua_State *L) {
struct sockaddr_storage ss;
int olderr = errno;
MakeSockaddr(L, 2, &ss, &salen);
return SysretBool(L, "bind", olderr,
bind(luaL_checkinteger(L, 1), &ss, salen));
return SysretBool(
L, "bind", olderr,
bind(luaL_checkinteger(L, 1), (struct sockaddr *)&ss, salen));
}
// unix.connect(fd:int, ip:uint32, port:uint16)
@ -1386,8 +1389,9 @@ static int LuaUnixConnect(lua_State *L) {
struct sockaddr_storage ss;
int olderr = errno;
MakeSockaddr(L, 2, &ss, &salen);
return SysretBool(L, "connect", olderr,
connect(luaL_checkinteger(L, 1), &ss, salen));
return SysretBool(
L, "connect", olderr,
connect(luaL_checkinteger(L, 1), (struct sockaddr *)&ss, salen));
}
// unix.listen(fd:int[, backlog:int])
@ -1400,13 +1404,13 @@ static int LuaUnixListen(lua_State *L) {
}
static int LuaUnixGetname(lua_State *L, const char *name,
int func(int, void *, uint32_t *)) {
int func(int, struct sockaddr *, uint32_t *)) {
int olderr;
uint32_t addrsize;
struct sockaddr_storage ss = {0};
olderr = errno;
addrsize = sizeof(ss) - 1;
if (!func(luaL_checkinteger(L, 1), &ss, &addrsize)) {
if (!func(luaL_checkinteger(L, 1), (struct sockaddr *)&ss, &addrsize)) {
return PushSockaddr(L, &ss);
} else {
return LuaUnixSysretErrno(L, name, olderr);
@ -1525,7 +1529,7 @@ static int LuaUnixAccept(lua_State *L) {
addrsize = sizeof(ss);
serverfd = luaL_checkinteger(L, 1);
flags = luaL_optinteger(L, 2, 0);
clientfd = accept4(serverfd, &ss, &addrsize, flags);
clientfd = accept4(serverfd, (struct sockaddr *)&ss, &addrsize, flags);
if (clientfd != -1) {
lua_pushinteger(L, clientfd);
return 1 + PushSockaddr(L, &ss);
@ -1607,7 +1611,8 @@ static int LuaUnixRecvfrom(lua_State *L) {
bufsiz = MIN(bufsiz, 0x7ffff000);
flags = luaL_optinteger(L, 3, 0);
buf = LuaAllocOrDie(L, bufsiz);
if ((rc = recvfrom(fd, buf, bufsiz, flags, &ss, &addrsize)) != -1) {
if ((rc = recvfrom(fd, buf, bufsiz, flags, (struct sockaddr *)&ss,
&addrsize)) != -1) {
got = rc;
lua_pushlstring(L, buf, got);
pushed = 1 + PushSockaddr(L, &ss);
@ -1673,8 +1678,9 @@ static int LuaUnixSendto(lua_State *L) {
data = luaL_checklstring(L, 2, &size);
i = MakeSockaddr(L, 3, &ss, &salen);
flags = luaL_optinteger(L, i, 0);
return SysretInteger(L, "sendto", olderr,
sendto(fd, data, size, flags, &ss, salen));
return SysretInteger(
L, "sendto", olderr,
sendto(fd, data, size, flags, (struct sockaddr *)&ss, salen));
}
// unix.shutdown(fd:int, how:int)

View file

@ -1904,7 +1904,9 @@ child_execute_job (struct childbase *child,
c = 0;
}
internet = parse_bool (get_target_variable
internet = !get_target_variable(STRING_SIZE_TUPLE (".STRICT"),
c->file, 0) ||
parse_bool (get_target_variable
(STRING_SIZE_TUPLE (".INTERNET"),
c ? c->file : 0, "0"));

View file

@ -8,6 +8,8 @@ struct crypt_data {
char __buf[256];
};
void encrypt(char *, int);
void setkey(const char *);
char *crypt(const char *, const char *);
char *crypt_r(const char *, const char *, struct crypt_data *);

68
third_party/musl/lockf.c vendored Normal file
View file

@ -0,0 +1,68 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/flock.h"
#include "libc/calls/weirdtypes.h"
#include "libc/errno.h"
#include "libc/sysv/consts/f.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
int lockf(int fd, int op, off_t size)
{
struct flock l = {
.l_type = F_WRLCK,
.l_whence = SEEK_CUR,
.l_len = size,
};
if (op == F_TEST){
l.l_type = F_RDLCK;
if (fcntl(fd, F_GETLK, &l) < 0)
return -1;
if (l.l_type == F_UNLCK || l.l_pid == getpid())
return 0;
errno = EACCES;
return -1;
}
if (op == F_ULOCK) {
l.l_type = F_UNLCK;
return fcntl(fd, F_SETLK, &l);
}
if (op == F_TLOCK) {
return fcntl(fd, F_SETLK, &l);
}
if (op == F_LOCK) {
return fcntl(fd, F_SETLKW, &l);
}
errno = EINVAL;
return -1;
}

101
third_party/musl/rand48.c vendored Normal file
View file

@ -0,0 +1,101 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/str/str.h"
#include "third_party/musl/rand48.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static unsigned short __seed48[7] = { 0, 0, 0, 0xe66d, 0xdeec, 0x5, 0xb };
static uint64_t __rand48_step(unsigned short *xi, unsigned short *lc)
{
uint64_t a, x;
x = xi[0] | xi[1]+0U<<16 | xi[2]+0ULL<<32;
a = lc[0] | lc[1]+0U<<16 | lc[2]+0ULL<<32;
x = a*x + lc[3];
xi[0] = x;
xi[1] = x>>16;
xi[2] = x>>32;
return x & 0xffffffffffffull;
}
double erand48(unsigned short s[3])
{
union {
uint64_t u;
double f;
} x = { 0x3ff0000000000000ULL | __rand48_step(s, __seed48+3)<<4 };
return x.f - 1.0;
}
double drand48(void)
{
return erand48(__seed48);
}
void lcong48(unsigned short p[7])
{
memcpy(__seed48, p, sizeof __seed48);
}
long nrand48(unsigned short s[3])
{
return __rand48_step(s, __seed48+3) >> 17;
}
long lrand48(void)
{
return nrand48(__seed48);
}
long jrand48(unsigned short s[3])
{
return (int32_t)(__rand48_step(s, __seed48+3) >> 16);
}
long mrand48(void)
{
return jrand48(__seed48);
}
unsigned short *seed48(unsigned short *s)
{
static unsigned short p[3];
memcpy(p, __seed48, sizeof p);
memcpy(__seed48, s, sizeof p);
return p;
}
void srand48(long seed)
{
seed48((unsigned short [3]){ 0x330e, seed, seed>>16 });
}

18
third_party/musl/rand48.h vendored Normal file
View file

@ -0,0 +1,18 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_MUSL_RAND48_H_
#define COSMOPOLITAN_THIRD_PARTY_MUSL_RAND48_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
double drand48(void);
double erand48(unsigned short[3]);
long int lrand48(void);
long int nrand48(unsigned short[3]);
long mrand48(void);
long jrand48(unsigned short[3]);
void srand48(long);
unsigned short *seed48(unsigned short[3]);
void lcong48(unsigned short[7]);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_MUSL_RAND48_H_ */