1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// Copyright 2021 The Libc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !libc.membrk && libc.memgrind
// +build !libc.membrk,libc.memgrind
// This is a debug-only version of the memory handling functions. When a
// program is built with -tags=libc.memgrind the functions MemAuditStart and
// MemAuditReport can be used to check for memory leaks.
package libc // import "modernc.org/libc"
import (
"fmt"
"runtime"
"sort"
"strings"
"unsafe"
"modernc.org/libc/errno"
"modernc.org/libc/sys/types"
"modernc.org/memory"
)
const memgrind = true
type memReportItem struct {
p, pc uintptr
s string
}
func (it *memReportItem) String() string {
more := it.s
if more != "" {
a := strings.Split(more, "\n")
more = "\n\t\t" + strings.Join(a, "\n\t\t")
}
return fmt.Sprintf("\t%s: %#x%s", pc2origin(it.pc), it.p, more)
}
type memReport []memReportItem
func (r memReport) Error() string {
a := []string{"memory leaks"}
for _, v := range r {
a = append(a, v.String())
}
return strings.Join(a, "\n")
}
var (
allocator memory.Allocator
allocs map[uintptr]uintptr // addr: caller
allocsMore map[uintptr]string
frees map[uintptr]uintptr // addr: caller
memAudit memReport
memAuditEnabled bool
)
func pc2origin(pc uintptr) string {
f := runtime.FuncForPC(pc)
var fn, fns string
var fl int
if f != nil {
fn, fl = f.FileLine(pc)
fns = f.Name()
if x := strings.LastIndex(fns, "."); x > 0 {
fns = fns[x+1:]
}
}
return fmt.Sprintf("%s:%d:%s", fn, fl, fns)
}
// void *malloc(size_t size);
func Xmalloc(t *TLS, size types.Size_t) uintptr {
if size == 0 {
return 0
}
allocMu.Lock()
defer allocMu.Unlock()
p, err := allocator.UintptrCalloc(int(size))
if dmesgs {
dmesg("%v: %v -> %#x, %v", origin(1), size, p, err)
}
if err != nil {
t.setErrno(errno.ENOMEM)
return 0
}
if memAuditEnabled {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("cannot obtain caller's PC")
}
delete(frees, p)
if pc0, ok := allocs[p]; ok {
dmesg("%v: malloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0))
panic(fmt.Errorf("%v: malloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0)))
}
allocs[p] = pc
}
return p
}
// void *calloc(size_t nmemb, size_t size);
func Xcalloc(t *TLS, n, size types.Size_t) uintptr {
rq := int(n * size)
if rq == 0 {
return 0
}
allocMu.Lock()
defer allocMu.Unlock()
p, err := allocator.UintptrCalloc(int(n * size))
if dmesgs {
dmesg("%v: %v -> %#x, %v", origin(1), n*size, p, err)
}
if err != nil {
t.setErrno(errno.ENOMEM)
return 0
}
if memAuditEnabled {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("cannot obtain caller's PC")
}
delete(frees, p)
if pc0, ok := allocs[p]; ok {
dmesg("%v: calloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0))
panic(fmt.Errorf("%v: calloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0)))
}
allocs[p] = pc
}
return p
}
// void *realloc(void *ptr, size_t size);
func Xrealloc(t *TLS, ptr uintptr, size types.Size_t) uintptr {
allocMu.Lock()
defer allocMu.Unlock()
var pc uintptr
if memAuditEnabled {
var ok bool
if pc, _, _, ok = runtime.Caller(1); !ok {
panic("cannot obtain caller's PC")
}
if ptr != 0 {
if pc0, ok := frees[ptr]; ok {
dmesg("%v: realloc: double free of %#x, previous call at %v:", pc2origin(pc), ptr, pc2origin(pc0))
panic(fmt.Errorf("%v: realloc: double free of %#x, previous call at %v:", pc2origin(pc), ptr, pc2origin(pc0)))
}
if _, ok := allocs[ptr]; !ok {
dmesg("%v: %v: realloc, free of unallocated memory: %#x", origin(1), pc2origin(pc), ptr)
panic(fmt.Errorf("%v: realloc, free of unallocated memory: %#x", pc2origin(pc), ptr))
}
delete(allocs, ptr)
delete(allocsMore, ptr)
frees[ptr] = pc
}
}
p, err := allocator.UintptrRealloc(ptr, int(size))
if dmesgs {
dmesg("%v: %#x, %v -> %#x, %v", origin(1), ptr, size, p, err)
}
if err != nil {
t.setErrno(errno.ENOMEM)
return 0
}
if memAuditEnabled && p != 0 {
delete(frees, p)
if pc0, ok := allocs[p]; ok {
dmesg("%v: realloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0))
panic(fmt.Errorf("%v: realloc returns same address twice, previous call at %v:", pc2origin(pc), pc2origin(pc0)))
}
allocs[p] = pc
}
return p
}
// void free(void *ptr);
func Xfree(t *TLS, p uintptr) {
if p == 0 {
return
}
if dmesgs {
dmesg("%v: %#x", origin(1), p)
}
allocMu.Lock()
defer allocMu.Unlock()
sz := memory.UintptrUsableSize(p)
if memAuditEnabled {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("cannot obtain caller's PC")
}
if pc0, ok := frees[p]; ok {
dmesg("%v: double free of %#x, previous call at %v:", pc2origin(pc), p, pc2origin(pc0))
panic(fmt.Errorf("%v: double free of %#x, previous call at %v:", pc2origin(pc), p, pc2origin(pc0)))
}
if _, ok := allocs[p]; !ok {
dmesg("%v: free of unallocated memory: %#x", pc2origin(pc), p)
panic(fmt.Errorf("%v: free of unallocated memory: %#x", pc2origin(pc), p))
}
delete(allocs, p)
delete(allocsMore, p)
frees[p] = pc
}
for i := uintptr(0); i < uintptr(sz); i++ {
*(*byte)(unsafe.Pointer(p + i)) = 0
}
allocator.UintptrFree(p)
}
func UsableSize(p uintptr) types.Size_t {
allocMu.Lock()
defer allocMu.Unlock()
if memAuditEnabled {
pc, _, _, ok := runtime.Caller(1)
if !ok {
panic("cannot obtain caller's PC")
}
if _, ok := allocs[p]; !ok {
dmesg("%v: usable size of unallocated memory: %#x", pc2origin(pc), p)
panic(fmt.Errorf("%v: usable size of unallocated memory: %#x", pc2origin(pc), p))
}
}
return types.Size_t(memory.UintptrUsableSize(p))
}
// MemAuditStart locks the memory allocator, initializes and enables memory
// auditing. Finally it unlocks the memory allocator.
//
// Some memory handling errors, like double free or freeing of unallocated
// memory, will panic when memory auditing is enabled.
//
// This memory auditing functionality has to be enabled using the libc.memgrind
// build tag.
//
// It is intended only for debug/test builds. It slows down memory allocation
// routines and it has additional memory costs.
func MemAuditStart() {
allocMu.Lock()
defer allocMu.Unlock()
allocs = map[uintptr]uintptr{} // addr: caller
allocsMore = map[uintptr]string{}
frees = map[uintptr]uintptr{} // addr: caller
memAuditEnabled = true
}
// MemAuditReport locks the memory allocator, reports memory leaks, if any.
// Finally it disables memory auditing and unlocks the memory allocator.
//
// This memory auditing functionality has to be enabled using the libc.memgrind
// build tag.
//
// It is intended only for debug/test builds. It slows down memory allocation
// routines and it has additional memory costs.
func MemAuditReport() (r error) {
allocMu.Lock()
defer func() {
allocs = nil
allocsMore = nil
frees = nil
memAuditEnabled = false
memAudit = nil
allocMu.Unlock()
}()
if len(allocs) != 0 {
for p, pc := range allocs {
memAudit = append(memAudit, memReportItem{p, pc, allocsMore[p]})
}
sort.Slice(memAudit, func(i, j int) bool {
return memAudit[i].String() < memAudit[j].String()
})
return memAudit
}
return nil
}
func MemAuditAnnotate(pc uintptr, s string) {
allocMu.Lock()
allocsMore[pc] = s
allocMu.Unlock()
}
|