Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf0b3bcc authored by Bernie Innocenti's avatar Bernie Innocenti
Browse files

Cull res_static

res_static was used by the non-reentrant legacy resolver calls to hold
a per-thread buffer for the hostent struct and its associated
8KB buffer, which is now stack allocated by the caller.

Change-Id: I01a2ea4c2e290d8e4f19fc3be3ebf827c8200b0e
parent 324f9f8c
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -76,7 +76,6 @@
#include "netd_resolv/resolv.h"
#include "netd_resolv/resolv.h"
#include "resolv_cache.h"
#include "resolv_cache.h"
#include "resolv_private.h"
#include "resolv_private.h"
#include "resolv_static.h"
#include "stats.pb.h"
#include "stats.pb.h"


using android::net::NetworkDnsEventReported;
using android::net::NetworkDnsEventReported;
@@ -97,6 +96,8 @@ using android::net::NetworkDnsEventReported;


#define MAXPACKET (8 * 1024)
#define MAXPACKET (8 * 1024)


constexpr int MAXADDRS = 35;

typedef union {
typedef union {
    HEADER hdr;
    HEADER hdr;
    uint8_t buf[MAXPACKET];
    uint8_t buf[MAXPACKET];
+0 −25
Original line number Original line Diff line number Diff line
@@ -41,41 +41,22 @@
#include "res_init.h"
#include "res_init.h"
#include "resolv_cache.h"
#include "resolv_cache.h"
#include "resolv_private.h"
#include "resolv_private.h"
#include "resolv_static.h"


typedef struct {
typedef struct {
    // TODO: Have one __res_state per network so we don't have to repopulate frequently.
    // TODO: Have one __res_state per network so we don't have to repopulate frequently.
    struct __res_state _nres[1];
    struct __res_state _nres[1];
    struct res_static _rstatic[1];
} _res_thread;
} _res_thread;


static _res_thread* res_thread_alloc(void) {
static _res_thread* res_thread_alloc(void) {
    _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));
    _res_thread* rt = (_res_thread*) calloc(1, sizeof(*rt));

    if (rt) {
        memset(rt->_rstatic, 0, sizeof rt->_rstatic);
    }
    return rt;
    return rt;
}
}


static void res_static_done(struct res_static* rs) {
    /* fortunately, there is nothing to do here, since the
     * points in h_addr_ptrs and host_aliases should all
     * point to 'hostbuf'
     */
    if (rs->hostf) { /* should not happen in theory, but just be safe */
        fclose(rs->hostf);
        rs->hostf = NULL;
    }
    free(rs->servent.s_aliases);
}

static void res_thread_free(void* _rt) {
static void res_thread_free(void* _rt) {
    _res_thread* rt = (_res_thread*) _rt;
    _res_thread* rt = (_res_thread*) _rt;


    LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();
    LOG(VERBOSE) << __func__ << ": rt=" << rt << " for thread=" << gettid();


    res_static_done(rt->_rstatic);
    res_nclose(rt->_nres);
    res_nclose(rt->_nres);
    free(rt);
    free(rt);
}
}
@@ -112,9 +93,3 @@ res_state res_get_state(void) {


    return rt ? rt->_nres : NULL;
    return rt ? rt->_nres : NULL;
}
}

res_static* res_get_static(void) {
    _res_thread* rt = res_thread_get();

    return rt ? rt->_rstatic : NULL;
}

resolv_static.h

deleted100644 → 0
+0 −32
Original line number Original line Diff line number Diff line
#ifndef NETD_RESOLV_STATIC_H
#define NETD_RESOLV_STATIC_H

#include <netdb.h>

/* this structure contains all the variables that were declared
 * 'static' in the original NetBSD resolver code.
 *
 * this caused vast amounts of crashes and memory corruptions
 * when the resolver was being used by multiple threads.
 *
 * (note: the OpenBSD/FreeBSD resolver has similar 'issues')
 */

#define MAXALIASES 35
#define MAXADDRS 35

struct res_static {
    char* h_addr_ptrs[MAXADDRS + 1];
    char* host_aliases[MAXALIASES];
    char hostbuf[8 * 1024];
    uint32_t host_addr[16 / sizeof(uint32_t)]; /* IPv4 or IPv6 */
    FILE* hostf;
    int stayopen;
    const char* servent_ptr;
    struct servent servent;
    struct hostent host;
};

res_static* res_get_static();

#endif  // NETD_RESOLV_STATIC_H
+3 −1
Original line number Original line Diff line number Diff line
@@ -42,7 +42,9 @@


#include "hostent.h"
#include "hostent.h"
#include "resolv_private.h"
#include "resolv_private.h"
#include "resolv_static.h"

constexpr int MAXALIASES = 35;
constexpr int MAXADDRS = 35;


#define ALIGNBYTES (sizeof(uintptr_t) - 1)
#define ALIGNBYTES (sizeof(uintptr_t) - 1)
#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)