-
Notifications
You must be signed in to change notification settings - Fork 119
/
memcheck.ljs
251 lines (209 loc) · 5.25 KB
/
memcheck.ljs
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
extern undefined;
extern print;
extern console;
extern snarf;
extern process;
extern newGlobal;
extern setDebug;
extern ArrayBuffer;
extern Uint8Array;
extern Uint32Array;
let NODE_JS = 1;
let JS_SHELL = 2;
let BROWSER = 3;
let enabled = false;
let mode;
if (typeof process !== "undefined") {
mode = NODE_JS;
} else if (typeof snarf !== "undefined") {
mode = JS_SHELL;
} else {
mode = BROWSER;
}
let ck, root, dbg;
if (mode === NODE_JS) {
print = console.log;
}
let memcheck = {};
// The shadow memory (SM) is a typed array with a corresponding U1
// view that holds a addresssable/defined flag (FLAG_ACC and FLAG_DEF)
// for each byte of main memory.
let u8 FLAG_ACC = 0x1;
let u8 FLAG_DEF = 0x2;
let u8 FLAG_MAL = 0x4
let SM;
let viewSM;
function reset(memSize) {
SM = new ArrayBuffer(memSize);
viewSM = new Uint8Array(SM);
exports.enabled = enabled = true;
memcheck = {
// {byte* => [str]}
used: [],
errors: {
// [byte*]
double_free: [],
// [{membyte: byte*, trace: [str]]
bad_access: [],
// [{membyte: byte*, trace: [str]]
undef_access: []
}
};
}
function setFlag(byte *idx, uint size, uint mask) {
for (let uint i = 0; i < size; i++) {
viewSM[idx + i] |= mask;
}
}
function clearFlag(byte *idx, uint size, uint mask) {
for (let uint i = 0; i < size; i++) {
viewSM[idx + i] &= ~mask;
}
}
function isSet(byte *bt, flag) {
if(viewSM[bt] & flag) {
return true;
} else {
return false;
}
}
// (byte, uint, bool) -> unit
function setAddressable(byte *bt, uint size, on) {
if(on) {
setFlag(bt, size, FLAG_ACC);
} else {
clearFlag(bt, size, FLAG_ACC);
}
}
// (byte) -> bool
function isAddressable(byte *bt) {
return isSet(bt, FLAG_ACC);
}
// (byte, uint, bool) -> unit
function setDefined(byte *bt, uint size, on) {
if(on) {
setFlag(bt, size, FLAG_DEF);
} else {
clearFlag(bt, size, FLAG_DEF);
}
}
// (byte) -> bool
function isDefined(byte *bt) {
return isSet(bt, FLAG_DEF);
}
// (byte, bool) -> unit
function setAlloc(byte *bt, value) {
if(value) {
setFlag(bt, 1, FLAG_MAL);
memcheck.used[bt] = callstack.slice(0);
} else {
clearFlag(bt, 1, FLAG_MAL);
memcheck.used[bt] = undefined;
}
}
// (byte) -> bool
function isAlloc(byte *bt) {
return isSet(bt, FLAG_MAL);
}
// (str, str) -> unit
function addError(kind, msg) {
if(memcheck.errors[kind] === undefined) {
memcheck.errors[kind] = [];
}
memcheck.errors[kind].push(msg);
}
function addDoubleFreeError(byte *bt) {
memcheck.errors.double_free.push({membyte: bt, trace: callstack.slice(0)});
}
function addBadAccessError(byte *bt) {
memcheck.errors.bad_access.push({membyte: bt, trace: callstack.slice(0) });
}
function addUndefinedError(byte *bt) {
memcheck.errors.undef_access.push({membyte: bt, trace: callstack.slice(0)});
}
// unit -> [byte*]
function getBadAccesses() {
return memcheck.errors.bad_access;
}
// unit -> [byte*]
function getBadUndefined() {
return memcheck.errors.undef_access;
}
// unit -> [byte*]
function getBadFrees() {
return memcheck.errors.double_free;
}
// unit -> [byte, [str]]
function getLeaks() {
return memcheck.used.map(function(val, idx) {
if(val) {
return {membyte: idx, trace: val};
}
}).filter(function (val, idx) {
if(val) {
return val;
}
});
}
let callstack = [];
function call_push(name, fname, line, col) {
callstack.push(name + " (" + fname + ".ljs:" + line + ":" + col + ")")
}
function call_pop() {
callstack.pop()
}
function call_reset(name, line, col) {
let fn = name + ":" + line + ":" + col;
let idx = callstack.lastIndexOf(fn)
if(idx !== -1) {
callstack = callstack.slice(0, idx+1);
}
}
function getCallstack() {
return callstack;
}
// uint -> str
function report(limit) {
function fmtErrors(err) {
let errors;
if(limit >= 0) {
errors = err.slice(0, limit);
} else {
errors = err;
}
return errors.map(function(val, idx) {
let stack;
if(val.trace.length === 0) {
stack = "at <empty stack>";
} else {
stack = val.trace.reverse().join("\n\tat ");
}
return "address " + val.membyte + "\n\t" + stack;
}).join("\n")
}
let leaks = "== Memory Leaks ==\n" + fmtErrors(getLeaks());
let access = "== Access of unallocated memory ==\n" + fmtErrors(getBadAccesses());
let undef = "== Access of uninitialized memory ==\n" + fmtErrors(getBadUndefined());
let frees = "== Free of unallocated memory ==\n" + fmtErrors(getBadFrees());
return [access, undef, frees, leaks].join("\n\n");
}
exports.setAddressable = setAddressable;
exports.isAddressable = isAddressable;
exports.setDefined = setDefined;
exports.isDefined = isDefined;
exports.setAlloc = setAlloc;
exports.isAlloc = isAlloc;
exports.addDoubleFreeError = addDoubleFreeError;
exports.addBadAccessError = addBadAccessError;
exports.addUndefinedError = addUndefinedError;
exports.getBadAccesses = getBadAccesses;
exports.getBadUndefined = getBadUndefined;
exports.getBadFrees = getBadFrees;
exports.getLeaks = getLeaks;
exports.report = report;
exports.reset = reset;
exports.enabled = enabled;
exports.memcheck_call_pop = call_pop;
exports.memcheck_call_push = call_push;
exports.memcheck_call_reset = call_reset;
exports.getCallstack = getCallstack;