chiark / gitweb /
Fix typo in changelog entry for 1.6.1
[adns.git] / regress / hnonfuzz.c
1 /* nhonfuzz.c
2  * (part of complex test harness, not of the library)
3  * - routines used for record and playback but not for fuzzing
4  *
5  *  This file is part of adns, which is
6  *    Copyright (C) 1997-2000,2003,2006,2014-2016  Ian Jackson
7  *    Copyright (C) 2014  Mark Wooding
8  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
9  *    Copyright (C) 1991 Massachusetts Institute of Technology
10  *  (See the file INSTALL for full details.)
11  *  
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 3, or (at your option)
15  *  any later version.
16  *  
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *  
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software Foundation.
24  */
25
26 #include <stdio.h>
27
28 #include "harness.h"
29
30 extern int Hmain(int argc, char **argv);
31 int main(int argc, char **argv) { return Hmain(argc, argv); }
32
33 FILE *Hfopen(const char *path, const char *mode) { return fopen(path,mode); }
34
35 void Texit(int rv) {
36   Tcommonshutdown();
37   exit(rv);
38 }
39
40 int Ttestinputfd(void) {
41   const char *fdstr= getenv("ADNS_TEST_IN_FD");
42   if (!fdstr) return -1;
43   return atoi(fdstr);
44 }
45
46 struct malloced {
47   struct malloced *next, *back;
48   size_t sz;
49   unsigned long count;
50   struct { double d; long ul; void *p; void (*fp)(void); } data;
51 };
52
53 static unsigned long malloccount, mallocfailat;
54 static struct { struct malloced *head, *tail; } mallocedlist;
55
56 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
57
58 void *Hmalloc(size_t sz) {
59   struct malloced *newnode;
60   const char *mfavar;
61   char *ep;
62
63   assert(sz);
64
65   newnode= malloc(MALLOCHSZ + sz);  if (!newnode) Tnomem();
66
67   LIST_LINK_TAIL(mallocedlist,newnode);
68   newnode->sz= sz;
69   newnode->count= ++malloccount;
70   if (!mallocfailat) {
71     mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
72     if (mfavar) {
73       mallocfailat= strtoul(mfavar,&ep,10);
74       if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
75     } else {
76       mallocfailat= ~0UL;
77     }
78   }
79   assert(newnode->count != mallocfailat);
80   memset(&newnode->data,0xc7,sz);
81   return &newnode->data;
82 }
83
84 void Hfree(void *ptr) {
85   struct malloced *oldnode;
86
87   if (!ptr) return;
88
89   oldnode= (void*)((char*)ptr - MALLOCHSZ);
90   LIST_UNLINK(mallocedlist,oldnode);
91   memset(&oldnode->data,0x38,oldnode->sz);
92   free(oldnode);
93 }
94
95 void *Hrealloc(void *op, size_t nsz) {
96   struct malloced *oldnode;
97   void *np;
98   size_t osz;
99
100   if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
101   np= Hmalloc(nsz);
102   if (osz) memcpy(np,op, osz>nsz ? nsz : osz);
103   Hfree(op);
104   return np;
105 }
106
107 void Tmallocshutdown(void) {
108   struct malloced *loopnode;
109   if (mallocedlist.head) {
110     fprintf(stderr,"adns test harness: memory leaked:");
111     for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
112       fprintf(stderr," %lu",loopnode->count);
113     putc('\n',stderr);
114     if (ferror(stderr)) exit(-1);
115   }
116 }