-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathdynamicVat.js
194 lines (176 loc) · 6.28 KB
/
dynamicVat.js
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
import { importBundle } from '@agoric/import-bundle';
import { makeVatSlot } from '../parseVatSlots';
export function makeVatRootObjectSlot() {
return makeVatSlot('object', true, 0);
}
export function makeDynamicVatCreator(stuff) {
const {
allocateUnusedVatID,
vatNameToID,
makeVatEndowments,
dynamicVatPowers,
transformMetering,
makeGetMeter,
addVatManager,
addExport,
queueToExport,
} = stuff;
/** A function to be called from the vatAdmin device to create a new vat. It
* creates the vat and sends a notification to the device. The root object
* will be available soon, but we immediately return the vatID so the ultimate
* requestor doesn't have to wait.
*
* @param vatSourceBundle a source bundle (JSON-serializable data) which
* defines the vat. This should be generated by calling bundle-source on a
* module whose default export is makeRootObject(), which takes E as a
* parameter and returns a root object.
* @param options an options bundle. The only option defined so far is
* 'metered', which defaults to 'true'. If 'true', the new dynamic vat is
* subject to a meter that limits the amount of computation and allocation
* that can occur during any given crank. Stack frames are limited as well.
* The meter is refilled between cranks, but if the meter ever underflows,
* the vat is terminated. If 'false', the vat is unmetered.
*
* @return { vatID } the vatID for a newly created vat. The success or
* failure of the operation will be reported in a message to the admin vat,
* citing this vatID
*/
function createVatDynamically(vatSourceBundle, options = {}) {
const { metered = true, ...unknownOptions } = options;
if (Object.keys(unknownOptions).length) {
const msg = JSON.stringify(Object.keys(unknownOptions));
throw Error(`createVatDynamically got unknown options ${msg}`);
}
const vatID = allocateUnusedVatID();
let meterRecord = null;
if (metered) {
// fail-stop: we refill the meter after each crank (in vatManager
// doProcess()), but if the vat exhausts its meter within a single crank,
// it will never run again. We set refillEachCrank:false because we want
// doProcess to do the refilling itself, so it can count the usage
meterRecord = makeGetMeter({
refillEachCrank: false,
refillIfExhausted: false,
});
}
let terminated = false;
function notifyTermination(error) {
if (terminated) {
return;
}
terminated = true;
const vatAdminVatId = vatNameToID('vatAdmin');
const vatAdminRootObjectSlot = makeVatRootObjectSlot();
const args = {
body: JSON.stringify([
vatID,
error
? { '@qclass': 'error', name: error.name, message: error.message }
: { '@qclass': 'undefined' },
]),
slots: [],
};
queueToExport(
vatAdminVatId,
vatAdminRootObjectSlot,
'vatTerminated',
args,
'logFailure',
);
}
async function makeBuildRootObject() {
if (typeof vatSourceBundle !== 'object') {
throw Error(
`createVatDynamically() requires bundle, not a plain string`,
);
}
const inescapableTransforms = [];
const inescapableGlobalLexicals = {};
if (metered) {
const getMeter = meterRecord.getMeter;
inescapableTransforms.push(src => transformMetering(src, getMeter));
inescapableGlobalLexicals.getMeter = getMeter;
}
const vatNS = await importBundle(vatSourceBundle, {
filePrefix: vatID,
endowments: makeVatEndowments(vatID),
inescapableTransforms,
inescapableGlobalLexicals,
});
if (typeof vatNS.buildRootObject !== 'function') {
throw Error(
`vat source bundle does not export buildRootObject function`,
);
}
return vatNS.buildRootObject;
}
function makeVatManager(buildRootObject) {
function setup(syscall, state, helpers, _vatPowers) {
return helpers.makeLiveSlots(
syscall,
state,
buildRootObject,
helpers.vatID,
dynamicVatPowers,
);
}
addVatManager(
vatID,
`dynamicVat${vatID}`,
setup,
{},
meterRecord,
notifyTermination,
);
}
function makeSuccessResponse() {
// build success message, giving admin vat access to the new vat's root
// object
const kernelRootObjSlot = addExport(vatID, makeVatRootObjectSlot());
return {
body: JSON.stringify([
vatID,
{ rootObject: { '@qclass': 'slot', index: 0 } },
]),
slots: [kernelRootObjSlot],
};
}
function makeErrorResponse(error) {
return {
body: JSON.stringify([vatID, { error: `${error}` }]),
slots: [],
};
}
function sendResponse(args) {
const vatAdminVatId = vatNameToID('vatAdmin');
const vatAdminRootObjectSlot = makeVatRootObjectSlot();
queueToExport(
vatAdminVatId,
vatAdminRootObjectSlot,
'newVatCallback',
args,
'logFailure',
);
}
// importBundle is async, so we prepare a callback chain to execute the
// resulting setup function, create the new vat around the resulting
// dispatch object, and notify the admin vat of our success (or failure).
// We presume that importBundle's Promise will fire promptly (before
// setImmediate does, i.e. importBundle is async but doesn't do any IO,
// so it doesn't really need to be async), because otherwise the
// queueToExport might fire (and insert messages into the kernel run
// queue) in the middle of some other vat's crank. TODO: find a safer
// way, maybe the response should go out to the controller's "queue
// things single file into the kernel" queue, once such a thing exists.
Promise.resolve()
.then(makeBuildRootObject)
.then(makeVatManager)
.then(makeSuccessResponse, makeErrorResponse)
.then(sendResponse)
.catch(err => console.error(`error in createVatDynamically`, err));
// and we return the vatID right away, so the the admin vat can prepare
// for the notification
return vatID;
}
return createVatDynamically;
}