Skip to content

Commit

Permalink
Log memory access error details even when no debug flag is set
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Aug 20, 2022
1 parent da33517 commit ff30d4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
16 changes: 8 additions & 8 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,13 @@ void change_clientcount(clientsData *client, int total, int blocked, int overTim
}
}

int findCacheID(int domainID, int clientID, enum query_types query_type)
int _findCacheID(int domainID, int clientID, enum query_types query_type, const char *func, int line, const char *file)
{
// Compare content of client against known client IP addresses
for(int cacheID = 0; cacheID < counters->dns_cache_size; cacheID++)
{
// Get cache pointer
DNSCacheData* dns_cache = getDNSCache(cacheID, true);
DNSCacheData* dns_cache = _getDNSCache(cacheID, true, line, func, file);

// Check if the returned pointer is valid before trying to access it
if(dns_cache == NULL)
Expand All @@ -343,7 +343,7 @@ int findCacheID(int domainID, int clientID, enum query_types query_type)
const int cacheID = counters->dns_cache_size;

// Get client pointer
DNSCacheData* dns_cache = getDNSCache(cacheID, false);
DNSCacheData* dns_cache = _getDNSCache(cacheID, false, line, func, file);

if(dns_cache == NULL)
{
Expand Down Expand Up @@ -563,22 +563,22 @@ static const char *query_status_str[QUERY_STATUS_MAX] = {
"SPECIAL_DOMAIN"
};

void _query_set_status(queriesData *query, const enum query_status new_status, const char *file, const int line)
void _query_set_status(queriesData *query, const enum query_status new_status, const char *func, const int line, const char *file)
{
// Debug logging
if(config.debug & DEBUG_STATUS)
{
const char *oldstr = query->status < QUERY_STATUS_MAX ? query_status_str[query->status] : "INVALID";
if(query->status == new_status)
{
logg("Query %i: status unchanged: %s (%d) in %s:%i",
query->id, oldstr, query->status, short_path(file), line);
logg("Query %i: status unchanged: %s (%d) in %s() (%s:%i)",
query->id, oldstr, query->status, func, short_path(file), line);
}
else
{
const char *newstr = new_status < QUERY_STATUS_MAX ? query_status_str[new_status] : "INVALID";
logg("Query %i: status changed: %s (%d) -> %s (%d) in %s:%i",
query->id, oldstr, query->status, newstr, new_status, short_path(file), line);
logg("Query %i: status changed: %s (%d) -> %s (%d) in %s() (%s:%i)",
query->id, oldstr, query->status, newstr, new_status, func, short_path(file), line);
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,14 @@ int findQueryID(const int id);
int findUpstreamID(const char * upstream, const in_port_t port);
int findDomainID(const char *domain, const bool count);
int findClientID(const char *client, const bool count, const bool aliasclient);
int findCacheID(int domainID, int clientID, enum query_types query_type);
#define findCacheID(domainID, clientID, query_type) _findCacheID(domainID, clientID, query_type, __FUNCTION__, __LINE__, __FILE__)
int _findCacheID(int domainID, int clientID, enum query_types query_type, const char *func, const int line, const char *file);
bool isValidIPv4(const char *addr);
bool isValidIPv6(const char *addr);

bool is_blocked(const enum query_status status) __attribute__ ((const));
#define query_set_status(query, new_status) _query_set_status(query, new_status, __FILE__, __LINE__)
void _query_set_status(queriesData *query, const enum query_status new_status, const char *file, const int line);
#define query_set_status(query, new_status) _query_set_status(query, new_status, __FUNCTION__, __LINE__, __FILE__)
void _query_set_status(queriesData *query, const enum query_status new_status, const char *func, const int line, const char *file);

void FTL_reload_all_domainlists(void);
void FTL_reset_per_client_domain_data(void);
Expand All @@ -135,14 +136,14 @@ const char *get_query_reply_str(const enum reply_type query) __attribute__ ((con

// Pointer getter functions
#define getQuery(queryID, checkMagic) _getQuery(queryID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
queriesData* _getQuery(int queryID, bool checkMagic, int line, const char * function, const char * file);
queriesData* _getQuery(int queryID, bool checkMagic, int line, const char *func, const char *file);
#define getClient(clientID, checkMagic) _getClient(clientID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
clientsData* _getClient(int clientID, bool checkMagic, int line, const char * function, const char * file);
clientsData* _getClient(int clientID, bool checkMagic, int line, const char *func, const char *file);
#define getDomain(domainID, checkMagic) _getDomain(domainID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
domainsData* _getDomain(int domainID, bool checkMagic, int line, const char * function, const char * file);
domainsData* _getDomain(int domainID, bool checkMagic, int line, const char *func, const char *file);
#define getUpstream(upstreamID, checkMagic) _getUpstream(upstreamID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const char * function, const char * file);
upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const char *func, const char *file);
#define getDNSCache(cacheID, checkMagic) _getDNSCache(cacheID, checkMagic, __LINE__, __FUNCTION__, __FILE__)
DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char * function, const char * file);
DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char *func, const char *file);

#endif //DATASTRUCTURE_H
58 changes: 26 additions & 32 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -999,41 +999,35 @@ void set_per_client_regex(const int clientID, const int regexID, const bool valu
((bool*) shm_per_client_regex.ptr)[id] = value;
}

static inline bool check_range(int ID, int MAXID, const char* type, int line, const char * function, const char * file)
static inline bool check_range(int ID, int MAXID, const char* type, const char *func, int line, const char *file)
{
// Check bounds
if(ID < 0 || ID > MAXID)
{
if(config.debug)
{
logg("ERROR: Trying to access %s ID %i, but maximum is %i", type, ID, MAXID);
logg(" found in %s() (%s:%i)", function, file, line);
}
logg("ERROR: Trying to access %s ID %i, but maximum is %i", type, ID, MAXID);
logg(" found in %s() (%s:%i)", func, short_path(file), line);
return false;
}

// Everything okay
return true;
}

static inline bool check_magic(int ID, bool checkMagic, unsigned char magic, const char* type, int line, const char * function, const char * file)
static inline bool check_magic(int ID, bool checkMagic, unsigned char magic, const char *type, const char *func, int line, const char *file)
{
// Check magic only if requested (skipped for new entries which are uninitialized)
if(checkMagic && magic != MAGICBYTE)
{
if(config.debug)
{
logg("ERROR: Trying to access %s ID %i, but magic byte is %x", type, ID, magic);
logg(" found in %s() (%s:%i)", function, file, line);
}
logg("ERROR: Trying to access %s ID %i, but magic byte is %x", type, ID, magic);
logg(" found in %s() (%s:%i)", func, short_path(file), line);
return false;
}

// Everything okay
return true;
}

queriesData* _getQuery(int queryID, bool checkMagic, int line, const char * function, const char * file)
queriesData* _getQuery(int queryID, bool checkMagic, int line, const char *func, const char *file)
{
// This does not exist, return a NULL pointer
if(queryID == -1)
Expand All @@ -1043,19 +1037,19 @@ queriesData* _getQuery(int queryID, bool checkMagic, int line, const char * func
if(config.debug & DEBUG_LOCKS && !is_our_lock())
{
logg("ERROR: Tried to obtain query pointer without lock in %s() (%s:%i)!",
function, file, line);
func, short_path(file), line);
generate_backtrace();
return NULL;
}

if(check_range(queryID, counters->queries_MAX, "query", line, function, file) &&
check_magic(queryID, checkMagic, queries[queryID].magic, "query", line, function, file))
if(check_range(queryID, counters->queries_MAX, "query", func, line, file) &&
check_magic(queryID, checkMagic, queries[queryID].magic, "query", func, line, file))
return &queries[queryID];
else
return NULL;
}

clientsData* _getClient(int clientID, bool checkMagic, int line, const char * function, const char * file)
clientsData* _getClient(int clientID, bool checkMagic, int line, const char *func, const char *file)
{
// This does not exist, we return a NULL pointer
if(clientID == -1)
Expand All @@ -1065,19 +1059,19 @@ clientsData* _getClient(int clientID, bool checkMagic, int line, const char * fu
if(config.debug & DEBUG_LOCKS && !is_our_lock())
{
logg("ERROR: Tried to obtain client pointer without lock in %s() (%s:%i)!",
function, file, line);
func, short_path(file), line);
generate_backtrace();
return NULL;
}

if(check_range(clientID, counters->clients_MAX, "client", line, function, file) &&
check_magic(clientID, checkMagic, clients[clientID].magic, "client", line, function, file))
if(check_range(clientID, counters->clients_MAX, "client", func, line, file) &&
check_magic(clientID, checkMagic, clients[clientID].magic, "client", func, line, file))
return &clients[clientID];
else
return NULL;
}

domainsData* _getDomain(int domainID, bool checkMagic, int line, const char * function, const char * file)
domainsData* _getDomain(int domainID, bool checkMagic, int line, const char *func, const char *file)
{
// This does not exist, we return a NULL pointer
if(domainID == -1)
Expand All @@ -1087,19 +1081,19 @@ domainsData* _getDomain(int domainID, bool checkMagic, int line, const char * fu
if(config.debug & DEBUG_LOCKS && !is_our_lock())
{
logg("ERROR: Tried to obtain domain pointer without lock in %s() (%s:%i)!",
function, file, line);
func, short_path(file), line);
generate_backtrace();
return NULL;
}

if(check_range(domainID, counters->domains_MAX, "domain", line, function, file) &&
check_magic(domainID, checkMagic, domains[domainID].magic, "domain", line, function, file))
if(check_range(domainID, counters->domains_MAX, "domain", func, line, file) &&
check_magic(domainID, checkMagic, domains[domainID].magic, "domain", func, line, file))
return &domains[domainID];
else
return NULL;
}

upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const char * function, const char * file)
upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const char *func, const char *file)
{
// This does not exist, we return a NULL pointer
if(upstreamID == -1)
Expand All @@ -1109,19 +1103,19 @@ upstreamsData* _getUpstream(int upstreamID, bool checkMagic, int line, const cha
if(config.debug & DEBUG_LOCKS && !is_our_lock())
{
logg("ERROR: Tried to obtain upstream pointer without lock in %s() (%s:%i)!",
function, file, line);
func, short_path(file), line);
generate_backtrace();
return NULL;
}

if(check_range(upstreamID, counters->upstreams_MAX, "upstream", line, function, file) &&
check_magic(upstreamID, checkMagic, upstreams[upstreamID].magic, "upstream", line, function, file))
if(check_range(upstreamID, counters->upstreams_MAX, "upstream", func, line, file) &&
check_magic(upstreamID, checkMagic, upstreams[upstreamID].magic, "upstream", func, line, file))
return &upstreams[upstreamID];
else
return NULL;
}

DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char * function, const char * file)
DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char *func, const char *file)
{
// This does not exist, we return a NULL pointer
if(cacheID == -1)
Expand All @@ -1131,13 +1125,13 @@ DNSCacheData* _getDNSCache(int cacheID, bool checkMagic, int line, const char *
if(config.debug & DEBUG_LOCKS && !is_our_lock())
{
logg("ERROR: Tried to obtain cache pointer without lock in %s() (%s:%i)!",
function, file, line);
func, short_path(file), line);
generate_backtrace();
return NULL;
}

if(check_range(cacheID, counters->dns_cache_MAX, "dns_cache", line, function, file) &&
check_magic(cacheID, checkMagic, dns_cache[cacheID].magic, "dns_cache", line, function, file))
if(check_range(cacheID, counters->dns_cache_MAX, "dns_cache", func, line, file) &&
check_magic(cacheID, checkMagic, dns_cache[cacheID].magic, "dns_cache", func, line, file))
return &dns_cache[cacheID];
else
return NULL;
Expand Down

0 comments on commit ff30d4a

Please sign in to comment.