From 03e603365321e520c53db18ff528520ccb1d2b13 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 12 May 2026 22:21:34 -0300 Subject: [PATCH] mlibc: more networking sysdeps and fixes --- patches/mlibc/jinx-working-patch.patch | 156 +++++++++++++++++++------ 1 file changed, 119 insertions(+), 37 deletions(-) diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index 02b25140..753674bb 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -258,8 +258,17 @@ index ed0c463..8df2580 100644 } unsigned int if_nametoindex(const char *name) { +diff --git mlibc-clean/options/rtld/x86_64/runtime.S mlibc-workdir/options/rtld/x86_64/runtime.S +index ba9a520..b958ef3 100644 +--- mlibc-clean/options/rtld/x86_64/runtime.S ++++ mlibc-workdir/options/rtld/x86_64/runtime.S +@@ -60,4 +60,3 @@ pltRelocateStub: + jmp *%r11 + + .section .note.GNU-stack,"",%progbits +- diff --git mlibc-clean/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp -index 3b04372..a4b934a 100644 +index 3b04372..e8ef8d5 100644 --- mlibc-clean/sysdeps/astral/generic/generic.cpp +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp @@ -1,7 +1,9 @@ @@ -272,7 +281,7 @@ index 3b04372..a4b934a 100644 #include #include #include -@@ -11,8 +13,20 @@ +@@ -11,8 +13,29 @@ #include #include #include @@ -290,10 +299,19 @@ index 3b04372..a4b934a 100644 + short flags; + int mtu; +} astral_net_info_t; ++ ++typedef struct { ++ struct ifaddrs ifa; ++ unsigned int if_index; ++ char if_name[IFNAMSIZ]; ++ struct sockaddr addr; ++ struct sockaddr broadaddr; ++ struct sockaddr netmask; ++} astral_ifaddrs_t; namespace mlibc { int Sysdeps::operator()(size_t size, const gid_t *list) { -@@ -31,14 +45,214 @@ namespace mlibc { +@@ -31,14 +54,276 @@ namespace mlibc { return 0; } @@ -390,7 +408,6 @@ index 3b04372..a4b934a 100644 + } + +#ifndef MLIBC_BUILDING_RTLD -+ // XXX this might leak memory in case of an error. + int Sysdeps::operator()(struct ifaddrs **out) { + struct ifaddrs *list = nullptr; + @@ -416,6 +433,9 @@ index 3b04372..a4b934a 100644 + if(!S_ISCHR(st.st_mode) || major(st.st_rdev) != 10) + continue; + ++ if(strlen(dent.d_name) >= IFNAMSIZ) ++ continue; ++ + int fd; + error = sysdep(dirfd, dent.d_name, 0, 0, &fd); + if(error) @@ -429,35 +449,39 @@ index 3b04372..a4b934a 100644 + continue; + } + -+ struct ifaddrs *next = (struct ifaddrs *)calloc(1, sizeof(struct ifaddrs)); -+ __ensure(next); ++ astral_ifaddrs_t *next = (astral_ifaddrs_t *)calloc(1, sizeof(*next)); ++ if(!next) { ++ sysdep(fd); ++ error = ENOMEM; ++ goto leave; ++ } + -+ next->ifa_next = list; ++ next->ifa.ifa_next = list; ++ next->if_index = minor(st.st_rdev) + 1; + -+ next->ifa_name = strdup(dent.d_name); -+ __ensure(next->ifa_name); ++ strcpy(next->if_name, dent.d_name); ++ next->ifa.ifa_name = next->if_name; + -+ next->ifa_flags = net_info.flags; ++ next->ifa.ifa_flags = net_info.flags; + -+ next->ifa_addr = (struct sockaddr *)malloc(sizeof(struct sockaddr)); -+ __ensure(next->ifa_addr); -+ memcpy(next->ifa_addr, &net_info.addr, sizeof(struct sockaddr)); ++ if(net_info.addr.sa_family) { ++ memcpy(&next->addr, &net_info.addr, sizeof(next->addr)); ++ next->ifa.ifa_addr = &next->addr; ++ } + + if(net_info.netmask.sa_family) { -+ next->ifa_netmask = (struct sockaddr *)malloc(sizeof(struct sockaddr)); -+ __ensure(next->ifa_netmask); -+ memcpy(next->ifa_netmask, &net_info.netmask, sizeof(struct sockaddr)); ++ memcpy(&next->netmask, &net_info.netmask, sizeof(next->netmask)); ++ next->ifa.ifa_netmask = &next->netmask; + } + + if(net_info.broadaddr.sa_family) { -+ next->ifa_broadaddr = (struct sockaddr *)malloc(sizeof(struct sockaddr)); -+ __ensure(next->ifa_broadaddr); -+ memcpy(next->ifa_broadaddr, &net_info.broadaddr, sizeof(struct sockaddr)); ++ memcpy(&next->broadaddr, &net_info.broadaddr, sizeof(next->broadaddr)); ++ next->ifa.ifa_broadaddr = &next->broadaddr; + } + -+ next->ifa_data = nullptr; ++ next->ifa.ifa_data = nullptr; + -+ list = next; ++ list = &next->ifa; + sysdep(fd); + } + @@ -465,10 +489,57 @@ index 3b04372..a4b934a 100644 + +leave: + sysdep(dirfd); ++ if(error) ++ freeifaddrs(list); ++ return error; ++ } ++ ++ int Sysdeps::operator()(unsigned int index, char *name) { ++ if(!index || !name) ++ return EINVAL; ++ ++ struct ifaddrs *ifaddrs; ++ if(int e = sysdep(&ifaddrs); e) ++ return e; ++ ++ int error = ENXIO; ++ for(struct ifaddrs *it = ifaddrs; it; it = it->ifa_next) { ++ astral_ifaddrs_t *entry = (astral_ifaddrs_t *)it; ++ if(entry->if_index != index) ++ continue; ++ ++ strcpy(name, entry->if_name); ++ error = 0; ++ break; ++ } ++ ++ freeifaddrs(ifaddrs); ++ return error; ++ } ++ ++ int Sysdeps::operator()(const char *name, unsigned int *ret) { ++ if(!name) ++ return EINVAL; ++ ++ struct ifaddrs *ifaddrs; ++ if(int e = sysdep(&ifaddrs); e) ++ return e; ++ ++ int error = ENXIO; ++ for(struct ifaddrs *it = ifaddrs; it; it = it->ifa_next) { ++ astral_ifaddrs_t *entry = (astral_ifaddrs_t *)it; ++ if(strcmp(entry->if_name, name)) ++ continue; ++ ++ *ret = entry->if_index; ++ error = 0; ++ break; ++ } ++ ++ freeifaddrs(ifaddrs); + return error; + } + -+ // XXX extremely leaky. + int Sysdeps::operator()(struct if_nameindex **out) { + struct ifaddrs *ifaddrs; + if(int e = sysdep(&ifaddrs); e) @@ -482,24 +553,33 @@ index 3b04372..a4b934a 100644 + } + + struct if_nameindex *arr = (struct if_nameindex *)malloc(sizeof(struct if_nameindex) * (count + 1)); -+ if(!arr) ++ if(!arr) { ++ freeifaddrs(ifaddrs); + return ENOMEM; ++ } + + int i = 0; -+ while(ifaddrs) { -+ arr[i].if_index = i + 1; -+ arr[i].if_name = strdup(ifaddrs->ifa_name); -+ __ensure(arr[i].if_name); ++ for(struct ifaddrs *it = ifaddrs; it; it = it->ifa_next) { ++ astral_ifaddrs_t *entry = (astral_ifaddrs_t *)it; ++ ++ arr[i].if_index = entry->if_index; ++ arr[i].if_name = strdup(entry->if_name); ++ if(!arr[i].if_name) { ++ for(int j = 0; j < i; ++j) ++ free(arr[j].if_name); ++ free(arr); ++ freeifaddrs(ifaddrs); ++ return ENOMEM; ++ } + + ++i; -+ -+ ifaddrs = ifaddrs->ifa_next; + } + + arr[count].if_index = 0; + arr[count].if_name = nullptr; + + *out = arr; ++ freeifaddrs(ifaddrs); + return 0; } @@ -513,7 +593,7 @@ index 3b04372..a4b934a 100644 int Sysdeps::operator()(bool *ipv4, bool *ipv6) { // there is no ipv6 support in the kernel currently and no way of checking for configured interfaces -@@ -47,6 +261,71 @@ namespace mlibc { +@@ -47,6 +332,71 @@ namespace mlibc { return 0; } @@ -585,7 +665,7 @@ index 3b04372..a4b934a 100644 int Sysdeps::operator()(int fd, const struct iovec *iovs, int iovc, ssize_t *bytes_read) { long r; int error = syscall(SYSCALL_READV, &r, fd, (uint64_t)iovs, iovc); -@@ -489,6 +768,7 @@ namespace mlibc { +@@ -489,6 +839,7 @@ namespace mlibc { int Sysdeps::operator()(int fd, int layer, int number, const void *buffer, socklen_t size) { long ret; @@ -593,7 +673,7 @@ index 3b04372..a4b934a 100644 return syscall(SYSCALL_SETSOCKOPT, &ret, fd, layer, number, (uint64_t)buffer, size); } -@@ -562,6 +842,16 @@ namespace mlibc { +@@ -562,6 +913,16 @@ namespace mlibc { return sysdep(fd, TCSETS, (void *)attr, &res); } @@ -610,7 +690,7 @@ index 3b04372..a4b934a 100644 int Sysdeps::operator()(struct pollfd *fds, nfds_t count, int timeout, int *num_events) { long ret; long error = syscall(SYSCALL_POLL, &ret, (uint64_t)fds, count, timeout); -@@ -932,4 +1222,13 @@ namespace mlibc { +@@ -932,4 +1293,13 @@ namespace mlibc { *nanos = ts.tv_nsec; return err; } @@ -663,10 +743,10 @@ index a6c4067..f1e0b65 100644 #ifndef __MLIBC_ABI_ONLY diff --git mlibc-clean/sysdeps/astral/include/mlibc/sysdeps.hpp mlibc-workdir/sysdeps/astral/include/mlibc/sysdeps.hpp -index ce91084..e7e9b6d 100644 +index ce91084..8af97e2 100644 --- mlibc-clean/sysdeps/astral/include/mlibc/sysdeps.hpp +++ mlibc-workdir/sysdeps/astral/include/mlibc/sysdeps.hpp -@@ -7,8 +7,25 @@ namespace mlibc { +@@ -7,8 +7,27 @@ namespace mlibc { struct AstralSysdepTags : SetGroups, GetGroups, @@ -680,6 +760,8 @@ index ce91084..e7e9b6d 100644 + Llistxattr, + Flistxattr, + Getifaddrs, ++ IfIndextoname, ++ IfNametoindex, + IfNameindex, GetSockopt, InetConfigured, @@ -692,7 +774,7 @@ index ce91084..e7e9b6d 100644 Readv, Writev, Sysinfo, -@@ -85,6 +102,8 @@ struct AstralSysdepTags : +@@ -85,6 +104,8 @@ struct AstralSysdepTags : Sleep, Tcgetattr, Tcsetattr, @@ -701,7 +783,7 @@ index ce91084..e7e9b6d 100644 Poll, Ppoll, Pselect, -@@ -135,6 +154,7 @@ struct AstralSysdepTags : +@@ -135,6 +156,7 @@ struct AstralSysdepTags : VmUnmap, Isatty, ClockGet,