Add test for ioctl(SIOCGIFCONF) and polyfill on BSDs

- Use nullness checks when calling weakly linked functions.

- Avoid typedef for reasons described in Linux Kernel style guide.

- Avoid enum in in Windows headers. Earlier in Cosmo's history all one
  hundred files in libc/nt/enum/ used to be enums and it resulted in
  gigabytes of DWARF data almost as large as everything else in the
  codebase combined.

- Bitfields aren't our friends. They have frequent ABI breakages,
  inconsistent arithmetic across compilers, and different endianness
  between cpus. Compiler authors also haven't invested much roi into
  making bit fields go fast so they produce poor assembly.

- Use memccpy() instead of strncpy() or snprintf() for length-bounded
  copying of C strings. strncpy() is a misunderstood function and
  snprintf() is awesome but memccpy() deserves more love.
This commit is contained in:
Justine Tunney 2021-06-25 18:41:02 -07:00
parent 86ab24ce56
commit 5144c22189
41 changed files with 502 additions and 476 deletions

View file

@ -0,0 +1,70 @@
/*-*- 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/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/ioctl.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/sio.h"
#include "libc/sysv/consts/sock.h"
#include "libc/testlib/testlib.h"
TEST(ioctl_siocgifconf, test) {
size_t n;
char *data;
int socketfd;
struct ifreq *ifr;
struct ifconf conf;
char addrbuf[1024];
uint32_t ip, netmask;
bool foundloopback = false;
data = gc(malloc((n = 4096)));
ASSERT_NE(-1, (socketfd = socket(AF_INET, SOCK_DGRAM, 0)));
conf.ifc_buf = data;
conf.ifc_len = n;
ASSERT_NE(-1, ioctl(socketfd, SIOCGIFCONF, &conf));
for (ifr = (struct ifreq *)data; (char *)ifr < data + conf.ifc_len; ++ifr) {
if (ifr->ifr_addr.sa_family != AF_INET) continue;
ip = ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr);
EXPECT_NE(-1, ioctl(socketfd, SIOCGIFNETMASK, ifr));
netmask = ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr);
#if 0
fprintf(stderr,
"%s %x %d\n"
" ip %hhu.%hhu.%hhu.%hhu\n"
" netmask %hhu.%hhu.%hhu.%hhu\n"
"\n",
ifr->ifr_name, ip, ifr->ifr_addr.sa_family, ip >> 24, ip >> 16,
ip >> 8, ip, netmask >> 24, netmask >> 16, netmask >> 8, netmask);
#endif
if (ip == 0x7f000001) {
foundloopback = true;
EXPECT_EQ(0xFF000000, netmask);
}
}
EXPECT_TRUE(foundloopback);
ASSERT_NE(-1, close(socketfd));
}

View file

@ -25,6 +25,7 @@ TEST_LIBC_CALLS_CHECKS = \
TEST_LIBC_CALLS_DIRECTDEPS = \
LIBC_CALLS \
LIBC_SOCK \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
@ -32,6 +33,7 @@ TEST_LIBC_CALLS_DIRECTDEPS = \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_STDIO \
LIBC_SYSV_CALLS \
LIBC_RUNTIME \
LIBC_STR \
LIBC_STUBS \

View file

@ -12,6 +12,7 @@ if CLANG=$(command -v clang); then
-fno-pie \
-nostdlib \
-nostdinc \
-fuse-ld=lld \
-mno-red-zone \
-Wl,-T,o/$MODE/ape/ape.lds \
-include o/cosmopolitan.h \

27
test/libc/release/lld.sh Executable file
View file

@ -0,0 +1,27 @@
#!/bin/sh
#-*-mode:sh;indent-tabs-mode:nil;tab-width:2;coding:utf-8-*-┐
#───vi: set net ft=sh ts=2 sts=2 fenc=utf-8 :vi─────────────┘
# TODO(jart): implement me
if CLANG=$(command -v clang); then
$CLANG \
-o o/$MODE/test/libc/release/smokeclang.com.dbg \
-Os \
-Wall \
-Werror \
-static \
-fno-pie \
-nostdlib \
-nostdinc \
-mno-red-zone \
-Wl,-T,o/$MODE/ape/ape.lds \
-include o/cosmopolitan.h \
test/libc/release/smoke.c \
o/$MODE/libc/crt/crt.o \
o/$MODE/ape/ape.o \
o/$MODE/cosmopolitan.a || exit
o/$MODE/test/libc/release/smokeclang.com.dbg || exit
fi
touch o/$MODE/test/libc/release/clang.ok

View file

@ -125,6 +125,16 @@ o/$(MODE)/test/libc/release/clang.ok: \
o/$(MODE)/cosmopolitan.a
@$(COMPILE) -ASHTEST -T$< $<
o/$(MODE)/test/libc/release/lld.ok: \
test/libc/release/lld.sh \
test/libc/release/smoke.c \
o/cosmopolitan.h \
o/$(MODE)/ape/ape.lds \
o/$(MODE)/libc/crt/crt.o \
o/$(MODE)/ape/ape.o \
o/$(MODE)/cosmopolitan.a
@$(COMPILE) -ASHTEST -T$< $<
o/$(MODE)/test/libc/release/metal.ok: \
test/libc/release/metal.sh \
o/$(MODE)/examples/hello.com \
@ -148,5 +158,6 @@ o/$(MODE)/test/libc/release: \
o/$(MODE)/test/libc/release/smokeansi.com \
o/$(MODE)/test/libc/release/smokeansi.com.runs \
o/$(MODE)/test/libc/release/clang.ok \
o/$(MODE)/test/libc/release/lld.ok \
o/$(MODE)/test/libc/release/emulate.ok \
o/$(MODE)/test/libc/release/metal.ok