From e3ac047b23c34609cb247d9c4becb416ae5e2b73 Mon Sep 17 00:00:00 2001 From: alessio Date: Tue, 5 Nov 2024 09:36:24 +0100 Subject: [PATCH] Pack structures more tightly --- lib/dns/include/dns/name.h | 10 +++++++--- lib/dns/include/dns/rdata.h | 9 +++++---- lib/dns/include/dns/rdataslab.h | 29 ++++++++++++----------------- lib/dns/name.c | 5 ++--- lib/dns/nsec3.c | 2 +- lib/dns/zone.c | 4 ++-- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index 740fb9c3f9..76a0e0eaaa 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -97,9 +97,11 @@ ISC_LANG_BEGINDECLS */ struct dns_name { unsigned int magic; - unsigned char *ndata; - unsigned int length; - unsigned int labels; +#pragma pack(push, 1) + uint8_t length; + uint8_t labels; +#pragma pack(pop) +#pragma pack(push, 2) struct dns_name_attrs { bool absolute : 1; /*%< Used by name.c */ bool readonly : 1; /*%< Used by name.c */ @@ -116,6 +118,8 @@ struct dns_name { bool update : 1; /*%< Used by client. */ bool hasupdaterec : 1; /*%< Used by client. */ } attributes; +#pragma pack(pop) + unsigned char *ndata; unsigned char *offsets; isc_buffer_t *buffer; ISC_LINK(dns_name_t) link; diff --git a/lib/dns/include/dns/rdata.h b/lib/dns/include/dns/rdata.h index 36f79c8edd..2d2200655b 100644 --- a/lib/dns/include/dns/rdata.h +++ b/lib/dns/include/dns/rdata.h @@ -109,17 +109,18 @@ ISC_LANG_BEGINDECLS * purpose the client desires. */ struct dns_rdata { - unsigned char *data; - unsigned int length; dns_rdataclass_t rdclass; dns_rdatatype_t type; - unsigned int flags; + uint16_t length; + uint16_t flags; + unsigned char *data; ISC_LINK(dns_rdata_t) link; }; +// *data, len , rdclass, type, flags, link #define DNS_RDATA_INIT \ { \ - NULL, 0, 0, 0, 0, { (void *)(-1), (void *)(-1) } \ + 0, 0, 0, 0, NULL, { (void *)(-1), (void *)(-1) } \ } #define DNS_RDATA_CHECKINITIALIZED diff --git a/lib/dns/include/dns/rdataslab.h b/lib/dns/include/dns/rdataslab.h index 4a0d58a542..7eb70e7c58 100644 --- a/lib/dns/include/dns/rdataslab.h +++ b/lib/dns/include/dns/rdataslab.h @@ -68,31 +68,27 @@ struct dns_slabheader_proof { }; struct dns_slabheader { - /*% - * Locked by the owning node's lock. - */ - uint32_t serial; - dns_ttl_t ttl; - dns_typepair_t type; - atomic_uint_least16_t attributes; - dns_trust_t trust; + // 2 + _Atomic(uint16_t) attributes; // Locked by the owning node's lock. + dns_trust_t trust; // Locked by the owning node's lock. + // 4 + uint32_t serial; // Locked by the owning node's lock. + dns_ttl_t ttl; // Locked by the owning node's lock. + dns_typepair_t type; // Locked by the owning node's lock. - unsigned int heap_index; - /*%< - * Used for TTL-based cache cleaning. - */ - - isc_stdtime_t resign; + _Atomic(uint16_t) count; unsigned int resign_lsb : 1; - atomic_uint_fast16_t count; + isc_stdtime_t resign; + unsigned int heap_index; /*%< * Monotonically increased every time this rdataset is bound so that * it is used as the base of the starting point in DNS responses * when the "cyclic" rrset-order is required. */ - atomic_uint_fast32_t last_refresh_fail_ts; + isc_stdtime_t last_used; + _Atomic(uint32_t) last_refresh_fail_ts; dns_slabheader_proof_t *noqname; dns_slabheader_proof_t *closest; @@ -122,7 +118,6 @@ struct dns_slabheader { * this rdataset, if any. */ - isc_stdtime_t last_used; ISC_LINK(struct dns_slabheader) link; /*% diff --git a/lib/dns/name.c b/lib/dns/name.c index 32770402ba..3ee12464a7 100644 --- a/lib/dns/name.c +++ b/lib/dns/name.c @@ -107,8 +107,7 @@ dns_name_isvalid(const dns_name_t *name) { return (false); } - if (name->length > DNS_NAME_MAXWIRE || - name->labels > DNS_NAME_MAXLABELS) + if (name->labels > DNS_NAME_MAXLABELS) { return (false); } @@ -616,7 +615,7 @@ dns_name_getlabel(const dns_name_t *name, unsigned int n, dns_label_t *label) { SETUP_OFFSETS(name, offsets, odata); label->base = &name->ndata[offsets[n]]; - if (n == name->labels - 1) { + if (n == (unsigned int) name->labels - 1) { label->length = name->length - offsets[n]; } else { label->length = offsets[n + 1] - offsets[n]; diff --git a/lib/dns/nsec3.c b/lib/dns/nsec3.c index e92557e002..ae283e5825 100644 --- a/lib/dns/nsec3.c +++ b/lib/dns/nsec3.c @@ -995,7 +995,7 @@ void dns_nsec3param_toprivate(dns_rdata_t *src, dns_rdata_t *target, dns_rdatatype_t privatetype, unsigned char *buf, size_t buflen) { - REQUIRE(buflen >= src->length + 1); + REQUIRE(buflen >= (unsigned int) src->length + 1); REQUIRE(DNS_RDATA_INITIALIZED(target)); diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 828f61161c..5b35a85a30 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -17192,7 +17192,7 @@ getprivate: { next = ISC_LIST_NEXT(nsec3p, link); - if (nsec3p->length == rdata.length + 1 && + if (nsec3p->length == (unsigned int) rdata.length + 1 && memcmp(rdata.data, nsec3p->data + 1, nsec3p->length - 1) == 0) { @@ -23773,7 +23773,7 @@ rss_post(void *arg) { dns_rdata_init(&rdata); dns_rdataset_current(&nrdataset, &rdata); - if (np->length == (rdata.length + 1) && + if (np->length == ((unsigned int) rdata.length + 1) && memcmp(rdata.data, np->data + 1, np->length - 1) == 0) {