forked from MozillaSecurity/funfuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-in-sandbox.js
More file actions
94 lines (80 loc) · 2.63 KB
/
run-in-sandbox.js
File metadata and controls
94 lines (80 loc) · 2.63 KB
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
/*********************
* SANDBOXED RUNNING *
*********************/
// We support three ways to run generated code:
// * useGeckoSandbox(), which uses Components.utils.Sandbox.
// * In xpcshell, we always use this method, so we don't accidentally erase the hard drive.
//
// * useSpidermonkeyShellSandbox(), which uses evalcx() with newGlobal().
// * In spidermonkey shell, we often use this method, so we can do additional correctness tests.
//
// * tryRunningDirectly(), which uses eval() or new Function().
// * This creates the most "interesting" testcases.
var tryRunning = xpcshell ? useGeckoSandbox() : tryRunningDirectly;
function fillShellSandbox(sandbox)
{
var safeFuns = [
"print",
"schedulegc", "selectforgc", "gczeal", "gc", "gcslice",
"verifyprebarriers", "gcPreserveCode",
"minorgc", "abortgc",
"evalcx", "newGlobal", "evaluate",
"dumpln", "fillShellSandbox",
"testMathyFunction", "hashStr",
"isAsmJSCompilationAvailable",
];
for (var i = 0; i < safeFuns.length; ++i) {
var fn = safeFuns[i];
if (sandbox[fn]) {
//print("Target already has " + fn);
} else if (this[fn]) { // FIXME: strict mode compliance requires passing glob around
sandbox[fn] = this[fn].bind(this);
} else {
//print("Source is missing " + fn);
}
}
return sandbox;
}
function useSpidermonkeyShellSandbox(sandboxType)
{
var primarySandbox;
switch (sandboxType) {
case 0: primarySandbox = evalcx('');
case 1: primarySandbox = evalcx('lazy');
case 2: primarySandbox = newGlobal({sameZoneAs: {}}); // same zone
default: primarySandbox = newGlobal(); // new zone
}
fillShellSandbox(primarySandbox);
return function(f, code, wtt) {
try {
evalcx(code, primarySandbox);
} catch(e) {
dumpln("Running in sandbox threw " + errorToString(e));
}
};
}
// When in xpcshell,
// * Run all testing in a sandbox so it doesn't accidentally wipe my hard drive.
// * Test interaction between sandboxes with same or different principals.
function newGeckoSandbox(n)
{
var t = (typeof n == "number") ? n : 1;
var s = Components.utils.Sandbox("http://x" + t + ".example.com/");
// Allow the sandbox to do a few things
s.newGeckoSandbox = newGeckoSandbox;
s.evalInSandbox = function(str, sbx) {
return Components.utils.evalInSandbox(str, sbx);
};
s.print = function(str) { print(str); };
return s;
}
function useGeckoSandbox() {
var primarySandbox = newGeckoSandbox(0);
return function(f, code, wtt) {
try {
Components.utils.evalInSandbox(code, primarySandbox);
} catch(e) {
// It might not be safe to operate on |e|.
}
};
}