]> git.cworth.org Git - apitrace/blob - retrace.hpp
Prevent derreference after free when retracing glFeedbackBuffer/glSelectBuffer.
[apitrace] / retrace.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #ifndef _RETRACE_HPP_
27 #define _RETRACE_HPP_
28
29 #include <assert.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #include <list>
34 #include <map>
35 #include <ostream>
36
37 #include "trace_model.hpp"
38
39
40 namespace retrace {
41
42
43 /**
44  * Handle map.
45  *
46  * It is just like a regular std::map<T, T> container, but lookups of missing
47  * keys return the key instead of default constructor.
48  *
49  * This is necessary for several GL named objects, where one can either request
50  * the implementation to generate an unique name, or pick a value never used
51  * before.
52  *
53  * XXX: In some cases, instead of returning the key, it would make more sense
54  * to return an unused data value (e.g., container count).
55  */
56 template <class T>
57 class map
58 {
59 private:
60     typedef std::map<T, T> base_type;
61     base_type base;
62
63 public:
64
65     T & operator[] (const T &key) {
66         typename base_type::iterator it;
67         it = base.find(key);
68         if (it == base.end()) {
69             return (base[key] = key);
70         }
71         return it->second;
72     }
73     
74     const T & operator[] (const T &key) const {
75         typename base_type::const_iterator it;
76         it = base.find(key);
77         if (it == base.end()) {
78             return (base[key] = key);
79         }
80         return it->second;
81     }
82 };
83
84
85 /**
86  * Similar to alloca(), but implemented with malloc.
87  */
88 class ScopedAllocator
89 {
90 private:
91     uintptr_t next;
92
93 public:
94     ScopedAllocator() :
95         next(0) {
96     }
97
98     inline void *
99     alloc(size_t size) {
100         if (!size) {
101             return NULL;
102         }
103
104         uintptr_t * buf = static_cast<uintptr_t *>(malloc(sizeof(uintptr_t) + size));
105         if (!buf) {
106             return NULL;
107         }
108
109         *buf = next;
110         next = reinterpret_cast<uintptr_t>(buf);
111         assert((next & 1) == 0);
112
113         return static_cast<void *>(&buf[1]);
114     }
115
116     template< class T >
117     inline T *
118     alloc(size_t n = 1) {
119         return static_cast<T *>(alloc(sizeof(T) * n));
120     }
121
122     /**
123      * Prevent this pointer from being automatically freed.
124      */
125     template< class T >
126     inline void
127     bind(T *ptr) {
128         if (ptr) {
129             reinterpret_cast<uintptr_t *>(ptr)[-1] |= 1;
130         }
131     }
132
133     inline
134     ~ScopedAllocator() {
135         while (next) {
136             uintptr_t temp = *reinterpret_cast<uintptr_t *>(next);
137
138             bool bind = temp & 1;
139             temp &= ~1;
140
141             if (!bind) {
142                 free(reinterpret_cast<void *>(next));
143             }
144
145             next = temp;
146         }
147     }
148 };
149
150
151 void
152 addRegion(unsigned long long address, void *buffer, unsigned long long size);
153
154 void
155 delRegionByPointer(void *ptr);
156
157 void *
158 toPointer(trace::Value &value, bool bind = false);
159
160
161 /**
162  * Output verbosity when retracing files.
163  */
164 extern int verbosity;
165
166 /**
167  * Add profiling data to the dump when retracing.
168  */
169 extern bool profiling;
170
171
172 std::ostream &warning(trace::Call &call);
173
174
175 void ignore(trace::Call &call);
176 void unsupported(trace::Call &call);
177
178
179 typedef void (*Callback)(trace::Call &call);
180
181 struct Entry {
182     const char *name;
183     Callback callback;
184 };
185
186
187 struct stringComparer {
188   bool operator() (const char *a, const  char *b) const {
189     return strcmp(a, b) < 0;
190   }
191 };
192
193
194 extern const Entry stdc_callbacks[];
195
196
197 class Retracer
198 {
199     typedef std::map<const char *, Callback, stringComparer> Map;
200     Map map;
201
202     std::vector<Callback> callbacks;
203
204 public:
205     Retracer() {
206         addCallbacks(stdc_callbacks);
207     }
208
209     virtual ~Retracer() {}
210
211     void addCallback(const Entry *entry);
212     void addCallbacks(const Entry *entries);
213
214     void retrace(trace::Call &call);
215 };
216
217
218 } /* namespace retrace */
219
220 #endif /* _RETRACE_HPP_ */