@enclave-vm/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +732 -0
- package/SECURITY-AUDIT.md +916 -0
- package/package.json +59 -0
- package/src/adapters/vm-adapter.d.ts +32 -0
- package/src/adapters/vm-adapter.js +1140 -0
- package/src/adapters/vm-adapter.js.map +1 -0
- package/src/adapters/worker-pool/config.d.ts +149 -0
- package/src/adapters/worker-pool/config.js +129 -0
- package/src/adapters/worker-pool/config.js.map +1 -0
- package/src/adapters/worker-pool/errors.d.ts +109 -0
- package/src/adapters/worker-pool/errors.js +181 -0
- package/src/adapters/worker-pool/errors.js.map +1 -0
- package/src/adapters/worker-pool/execution-queue.d.ts +85 -0
- package/src/adapters/worker-pool/execution-queue.js +179 -0
- package/src/adapters/worker-pool/execution-queue.js.map +1 -0
- package/src/adapters/worker-pool/index.d.ts +48 -0
- package/src/adapters/worker-pool/index.js +73 -0
- package/src/adapters/worker-pool/index.js.map +1 -0
- package/src/adapters/worker-pool/memory-monitor.d.ts +106 -0
- package/src/adapters/worker-pool/memory-monitor.js +184 -0
- package/src/adapters/worker-pool/memory-monitor.js.map +1 -0
- package/src/adapters/worker-pool/protocol.d.ts +314 -0
- package/src/adapters/worker-pool/protocol.js +143 -0
- package/src/adapters/worker-pool/protocol.js.map +1 -0
- package/src/adapters/worker-pool/rate-limiter.d.ts +60 -0
- package/src/adapters/worker-pool/rate-limiter.js +101 -0
- package/src/adapters/worker-pool/rate-limiter.js.map +1 -0
- package/src/adapters/worker-pool/safe-deserialize.d.ts +50 -0
- package/src/adapters/worker-pool/safe-deserialize.js +141 -0
- package/src/adapters/worker-pool/safe-deserialize.js.map +1 -0
- package/src/adapters/worker-pool/worker-pool-adapter.d.ts +84 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js +506 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js.map +1 -0
- package/src/adapters/worker-pool/worker-script.d.ts +12 -0
- package/src/adapters/worker-pool/worker-script.js +770 -0
- package/src/adapters/worker-pool/worker-script.js.map +1 -0
- package/src/adapters/worker-pool/worker-slot.d.ts +116 -0
- package/src/adapters/worker-pool/worker-slot.js +301 -0
- package/src/adapters/worker-pool/worker-slot.js.map +1 -0
- package/src/babel/babel-wrapper.d.ts +118 -0
- package/src/babel/babel-wrapper.js +395 -0
- package/src/babel/babel-wrapper.js.map +1 -0
- package/src/babel/index.d.ts +9 -0
- package/src/babel/index.js +17 -0
- package/src/babel/index.js.map +1 -0
- package/src/babel/multi-file-transform.d.ts +122 -0
- package/src/babel/multi-file-transform.js +344 -0
- package/src/babel/multi-file-transform.js.map +1 -0
- package/src/double-vm/double-vm-wrapper.d.ts +65 -0
- package/src/double-vm/double-vm-wrapper.js +523 -0
- package/src/double-vm/double-vm-wrapper.js.map +1 -0
- package/src/double-vm/index.d.ts +31 -0
- package/src/double-vm/index.js +34 -0
- package/src/double-vm/index.js.map +1 -0
- package/src/double-vm/parent-vm-bootstrap.d.ts +63 -0
- package/src/double-vm/parent-vm-bootstrap.js +1695 -0
- package/src/double-vm/parent-vm-bootstrap.js.map +1 -0
- package/src/double-vm/suspicious-patterns.d.ts +57 -0
- package/src/double-vm/suspicious-patterns.js +304 -0
- package/src/double-vm/suspicious-patterns.js.map +1 -0
- package/src/double-vm/types.d.ts +178 -0
- package/src/double-vm/types.js +11 -0
- package/src/double-vm/types.js.map +1 -0
- package/src/double-vm/wrap-adapter.d.ts +36 -0
- package/src/double-vm/wrap-adapter.js +59 -0
- package/src/double-vm/wrap-adapter.js.map +1 -0
- package/src/enclave.d.ts +159 -0
- package/src/enclave.js +671 -0
- package/src/enclave.js.map +1 -0
- package/src/globals-validator.d.ts +80 -0
- package/src/globals-validator.js +206 -0
- package/src/globals-validator.js.map +1 -0
- package/src/index.d.ts +29 -0
- package/src/index.js +130 -0
- package/src/index.js.map +1 -0
- package/src/memory-proxy.d.ts +69 -0
- package/src/memory-proxy.js +310 -0
- package/src/memory-proxy.js.map +1 -0
- package/src/memory-tracker.d.ts +170 -0
- package/src/memory-tracker.js +230 -0
- package/src/memory-tracker.js.map +1 -0
- package/src/safe-error.d.ts +18 -0
- package/src/safe-error.js +76 -0
- package/src/safe-error.js.map +1 -0
- package/src/safe-runtime.d.ts +72 -0
- package/src/safe-runtime.js +656 -0
- package/src/safe-runtime.js.map +1 -0
- package/src/scoring/cache.d.ts +68 -0
- package/src/scoring/cache.js +158 -0
- package/src/scoring/cache.js.map +1 -0
- package/src/scoring/feature-extractor.d.ts +61 -0
- package/src/scoring/feature-extractor.js +366 -0
- package/src/scoring/feature-extractor.js.map +1 -0
- package/src/scoring/index.d.ts +33 -0
- package/src/scoring/index.js +53 -0
- package/src/scoring/index.js.map +1 -0
- package/src/scoring/scorer.interface.d.ts +65 -0
- package/src/scoring/scorer.interface.js +49 -0
- package/src/scoring/scorer.interface.js.map +1 -0
- package/src/scoring/scorers/disabled.scorer.d.ts +19 -0
- package/src/scoring/scorers/disabled.scorer.js +34 -0
- package/src/scoring/scorers/disabled.scorer.js.map +1 -0
- package/src/scoring/scorers/external-api.scorer.d.ts +29 -0
- package/src/scoring/scorers/external-api.scorer.js +141 -0
- package/src/scoring/scorers/external-api.scorer.js.map +1 -0
- package/src/scoring/scorers/index.d.ts +10 -0
- package/src/scoring/scorers/index.js +22 -0
- package/src/scoring/scorers/index.js.map +1 -0
- package/src/scoring/scorers/local-llm.scorer.d.ts +116 -0
- package/src/scoring/scorers/local-llm.scorer.js +474 -0
- package/src/scoring/scorers/local-llm.scorer.js.map +1 -0
- package/src/scoring/scorers/progressive.scorer.d.ts +77 -0
- package/src/scoring/scorers/progressive.scorer.js +199 -0
- package/src/scoring/scorers/progressive.scorer.js.map +1 -0
- package/src/scoring/scorers/rule-based.scorer.d.ts +66 -0
- package/src/scoring/scorers/rule-based.scorer.js +316 -0
- package/src/scoring/scorers/rule-based.scorer.js.map +1 -0
- package/src/scoring/scoring-gate.d.ts +76 -0
- package/src/scoring/scoring-gate.js +246 -0
- package/src/scoring/scoring-gate.js.map +1 -0
- package/src/scoring/types.d.ts +690 -0
- package/src/scoring/types.js +107 -0
- package/src/scoring/types.js.map +1 -0
- package/src/secure-proxy.d.ts +157 -0
- package/src/secure-proxy.js +609 -0
- package/src/secure-proxy.js.map +1 -0
- package/src/session/channels/embedded-channel.d.ts +76 -0
- package/src/session/channels/embedded-channel.js +130 -0
- package/src/session/channels/embedded-channel.js.map +1 -0
- package/src/session/channels/index.d.ts +9 -0
- package/src/session/channels/index.js +14 -0
- package/src/session/channels/index.js.map +1 -0
- package/src/session/index.d.ts +15 -0
- package/src/session/index.js +27 -0
- package/src/session/index.js.map +1 -0
- package/src/session/session-emitter.d.ts +119 -0
- package/src/session/session-emitter.js +253 -0
- package/src/session/session-emitter.js.map +1 -0
- package/src/session/session-state-machine.d.ts +108 -0
- package/src/session/session-state-machine.js +182 -0
- package/src/session/session-state-machine.js.map +1 -0
- package/src/session/session.d.ts +135 -0
- package/src/session/session.js +398 -0
- package/src/session/session.js.map +1 -0
- package/src/session-types.d.ts +313 -0
- package/src/session-types.js +39 -0
- package/src/session-types.js.map +1 -0
- package/src/sidecar/index.d.ts +12 -0
- package/src/sidecar/index.js +31 -0
- package/src/sidecar/index.js.map +1 -0
- package/src/sidecar/reference-config.d.ts +81 -0
- package/src/sidecar/reference-config.js +118 -0
- package/src/sidecar/reference-config.js.map +1 -0
- package/src/sidecar/reference-resolver.d.ts +115 -0
- package/src/sidecar/reference-resolver.js +262 -0
- package/src/sidecar/reference-resolver.js.map +1 -0
- package/src/sidecar/reference-sidecar.d.ts +161 -0
- package/src/sidecar/reference-sidecar.js +226 -0
- package/src/sidecar/reference-sidecar.js.map +1 -0
- package/src/tool-bridge.d.ts +4 -0
- package/src/tool-bridge.js +306 -0
- package/src/tool-bridge.js.map +1 -0
- package/src/types.d.ts +775 -0
- package/src/types.js +187 -0
- package/src/types.js.map +1 -0
- package/src/value-sanitizer.d.ts +133 -0
- package/src/value-sanitizer.js +421 -0
- package/src/value-sanitizer.js.map +1 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory-Tracking Proxies
|
|
3
|
+
*
|
|
4
|
+
* Provides proxied constructors that track memory allocations.
|
|
5
|
+
* Used in conjunction with MemoryTracker to enforce memory limits.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { MemoryTracker } from './memory-tracker';
|
|
10
|
+
/**
|
|
11
|
+
* Create a memory-tracking String constructor proxy
|
|
12
|
+
*
|
|
13
|
+
* Wraps the String constructor to track string allocations.
|
|
14
|
+
* Tracks both `String()` calls and `new String()` constructions.
|
|
15
|
+
*
|
|
16
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
17
|
+
* @returns Proxied String constructor
|
|
18
|
+
*/
|
|
19
|
+
export declare function createTrackedString(tracker: MemoryTracker): typeof String;
|
|
20
|
+
/**
|
|
21
|
+
* Create a memory-tracking Array constructor proxy
|
|
22
|
+
*
|
|
23
|
+
* Wraps the Array constructor and array methods that can grow arrays.
|
|
24
|
+
* Tracks initial allocation and subsequent growth operations.
|
|
25
|
+
*
|
|
26
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
27
|
+
* @returns Proxied Array constructor
|
|
28
|
+
*/
|
|
29
|
+
export declare function createTrackedArray(tracker: MemoryTracker): typeof Array;
|
|
30
|
+
/**
|
|
31
|
+
* Create tracked array prototype methods
|
|
32
|
+
*
|
|
33
|
+
* Returns an object with wrapped array methods that track allocations.
|
|
34
|
+
* These should be applied to the sandbox's Array.prototype.
|
|
35
|
+
*
|
|
36
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
37
|
+
* @returns Object with tracked array methods
|
|
38
|
+
*/
|
|
39
|
+
export declare function createTrackedArrayMethods(tracker: MemoryTracker): Record<string, unknown>;
|
|
40
|
+
/**
|
|
41
|
+
* Create a tracked string concat/repeat implementation
|
|
42
|
+
*
|
|
43
|
+
* The most common memory attack is string doubling via concatenation.
|
|
44
|
+
* This wraps string operations that create new strings.
|
|
45
|
+
*
|
|
46
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
47
|
+
* @returns Object with tracked string methods
|
|
48
|
+
*/
|
|
49
|
+
export declare function createTrackedStringMethods(tracker: MemoryTracker): Record<string, unknown>;
|
|
50
|
+
/**
|
|
51
|
+
* Wrap the binary string concatenation operator
|
|
52
|
+
*
|
|
53
|
+
* This is tricky because we can't directly intercept the + operator.
|
|
54
|
+
* Instead, we track string results in the safe runtime by checking
|
|
55
|
+
* types after operations.
|
|
56
|
+
*
|
|
57
|
+
* @param tracker MemoryTracker instance
|
|
58
|
+
* @returns A function that should be called after any operation that might produce a string
|
|
59
|
+
*/
|
|
60
|
+
export declare function createStringConcatTracker(tracker: MemoryTracker): (result: unknown, originalLength?: number) => unknown;
|
|
61
|
+
/**
|
|
62
|
+
* Create all tracked globals for injection into sandbox
|
|
63
|
+
*
|
|
64
|
+
* Returns an object containing all memory-tracked constructors and methods.
|
|
65
|
+
*
|
|
66
|
+
* @param tracker MemoryTracker instance
|
|
67
|
+
* @returns Object with all tracked globals
|
|
68
|
+
*/
|
|
69
|
+
export declare function createTrackedGlobals(tracker: MemoryTracker): Record<string, unknown>;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Memory-Tracking Proxies
|
|
4
|
+
*
|
|
5
|
+
* Provides proxied constructors that track memory allocations.
|
|
6
|
+
* Used in conjunction with MemoryTracker to enforce memory limits.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.createTrackedString = createTrackedString;
|
|
12
|
+
exports.createTrackedArray = createTrackedArray;
|
|
13
|
+
exports.createTrackedArrayMethods = createTrackedArrayMethods;
|
|
14
|
+
exports.createTrackedStringMethods = createTrackedStringMethods;
|
|
15
|
+
exports.createStringConcatTracker = createStringConcatTracker;
|
|
16
|
+
exports.createTrackedGlobals = createTrackedGlobals;
|
|
17
|
+
const memory_tracker_1 = require("./memory-tracker");
|
|
18
|
+
/**
|
|
19
|
+
* Create a memory-tracking String constructor proxy
|
|
20
|
+
*
|
|
21
|
+
* Wraps the String constructor to track string allocations.
|
|
22
|
+
* Tracks both `String()` calls and `new String()` constructions.
|
|
23
|
+
*
|
|
24
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
25
|
+
* @returns Proxied String constructor
|
|
26
|
+
*/
|
|
27
|
+
function createTrackedString(tracker) {
|
|
28
|
+
return new Proxy(String, {
|
|
29
|
+
apply(target, thisArg, args) {
|
|
30
|
+
const result = Reflect.apply(target, thisArg, args);
|
|
31
|
+
tracker.trackString(result);
|
|
32
|
+
return result;
|
|
33
|
+
},
|
|
34
|
+
construct(target, args) {
|
|
35
|
+
const result = new target(...args);
|
|
36
|
+
tracker.trackString(result.valueOf());
|
|
37
|
+
return result;
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a memory-tracking Array constructor proxy
|
|
43
|
+
*
|
|
44
|
+
* Wraps the Array constructor and array methods that can grow arrays.
|
|
45
|
+
* Tracks initial allocation and subsequent growth operations.
|
|
46
|
+
*
|
|
47
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
48
|
+
* @returns Proxied Array constructor
|
|
49
|
+
*/
|
|
50
|
+
function createTrackedArray(tracker) {
|
|
51
|
+
// Create a tracked wrapper for array instances to intercept dangerous methods
|
|
52
|
+
function wrapArrayInstance(arr) {
|
|
53
|
+
return new Proxy(arr, {
|
|
54
|
+
get(target, prop, receiver) {
|
|
55
|
+
const value = Reflect.get(target, prop, receiver);
|
|
56
|
+
// Intercept join() to track and limit memory-heavy string creation
|
|
57
|
+
if (prop === 'join' && typeof value === 'function') {
|
|
58
|
+
return function (separator) {
|
|
59
|
+
const sep = separator ?? ',';
|
|
60
|
+
const arrLength = target.length;
|
|
61
|
+
// Pre-calculate estimated size
|
|
62
|
+
let estimatedSize = 0;
|
|
63
|
+
if (arrLength > 10000) {
|
|
64
|
+
// Rough estimate for large arrays
|
|
65
|
+
estimatedSize = arrLength * (10 + sep.length) * 2;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
for (let i = 0; i < arrLength; i++) {
|
|
69
|
+
const item = target[i];
|
|
70
|
+
estimatedSize += (item == null ? 0 : String(item).length) + sep.length;
|
|
71
|
+
}
|
|
72
|
+
estimatedSize = estimatedSize * 2; // UTF-16
|
|
73
|
+
}
|
|
74
|
+
// Pre-check limit before allocating
|
|
75
|
+
const limit = tracker.getLimit();
|
|
76
|
+
if (limit > 0 && estimatedSize > limit) {
|
|
77
|
+
throw new memory_tracker_1.MemoryLimitError(`Array.join would exceed memory limit (estimated ${estimatedSize} bytes, limit ${limit} bytes)`, estimatedSize, limit);
|
|
78
|
+
}
|
|
79
|
+
const result = Array.prototype.join.call(target, separator);
|
|
80
|
+
tracker.trackString(result);
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Intercept toString() (uses join(',') internally)
|
|
85
|
+
if (prop === 'toString' && typeof value === 'function') {
|
|
86
|
+
return function () {
|
|
87
|
+
// Delegate to tracked join
|
|
88
|
+
const joinFn = Reflect.get(receiver, 'join', receiver);
|
|
89
|
+
return joinFn.call(target, ',');
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
const TrackedArray = new Proxy(Array, {
|
|
97
|
+
apply(target, thisArg, args) {
|
|
98
|
+
const result = Reflect.apply(target, thisArg, args);
|
|
99
|
+
if (Array.isArray(result)) {
|
|
100
|
+
tracker.trackArray(result.length);
|
|
101
|
+
return wrapArrayInstance(result);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
},
|
|
105
|
+
construct(target, args) {
|
|
106
|
+
const result = new target(...args);
|
|
107
|
+
tracker.trackArray(result.length);
|
|
108
|
+
return wrapArrayInstance(result);
|
|
109
|
+
},
|
|
110
|
+
get(target, prop, receiver) {
|
|
111
|
+
const value = Reflect.get(target, prop, receiver);
|
|
112
|
+
// Wrap static methods that create arrays
|
|
113
|
+
if (prop === 'from') {
|
|
114
|
+
return function (...args) {
|
|
115
|
+
const result = Array.from(...args);
|
|
116
|
+
tracker.trackArray(result.length);
|
|
117
|
+
return wrapArrayInstance(result);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
if (prop === 'of') {
|
|
121
|
+
return function (...items) {
|
|
122
|
+
const result = Array.of(...items);
|
|
123
|
+
tracker.trackArray(result.length);
|
|
124
|
+
return wrapArrayInstance(result);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return value;
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
return TrackedArray;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Create tracked array prototype methods
|
|
134
|
+
*
|
|
135
|
+
* Returns an object with wrapped array methods that track allocations.
|
|
136
|
+
* These should be applied to the sandbox's Array.prototype.
|
|
137
|
+
*
|
|
138
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
139
|
+
* @returns Object with tracked array methods
|
|
140
|
+
*/
|
|
141
|
+
function createTrackedArrayMethods(tracker) {
|
|
142
|
+
const originalConcat = Array.prototype.concat;
|
|
143
|
+
const originalSlice = Array.prototype.slice;
|
|
144
|
+
const originalSplice = Array.prototype.splice;
|
|
145
|
+
const originalMap = Array.prototype.map;
|
|
146
|
+
const originalFilter = Array.prototype.filter;
|
|
147
|
+
const originalFlatMap = Array.prototype.flatMap;
|
|
148
|
+
const originalFlat = Array.prototype.flat;
|
|
149
|
+
const originalJoin = Array.prototype.join;
|
|
150
|
+
return {
|
|
151
|
+
// join() can create very large strings from large arrays
|
|
152
|
+
// Attack vector: new Array(12_000_000).join('x') creates ~12MB string
|
|
153
|
+
join: function (separator) {
|
|
154
|
+
const sep = separator ?? ',';
|
|
155
|
+
// Pre-calculate estimated size to check limit before allocating
|
|
156
|
+
let estimatedSize = 0;
|
|
157
|
+
const arrLength = this.length;
|
|
158
|
+
// For very large arrays, estimate without iterating every element
|
|
159
|
+
if (arrLength > 10000) {
|
|
160
|
+
// Rough estimate: avg element is ~10 chars + separator
|
|
161
|
+
estimatedSize = arrLength * (10 + sep.length) * 2; // UTF-16
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
for (let i = 0; i < arrLength; i++) {
|
|
165
|
+
const item = this[i];
|
|
166
|
+
estimatedSize += (item == null ? 0 : String(item).length) + sep.length;
|
|
167
|
+
}
|
|
168
|
+
estimatedSize = estimatedSize * 2; // UTF-16
|
|
169
|
+
}
|
|
170
|
+
// Pre-check limit before allocating
|
|
171
|
+
const limit = tracker.getLimit();
|
|
172
|
+
if (limit > 0 && estimatedSize > limit) {
|
|
173
|
+
throw new memory_tracker_1.MemoryLimitError(`Array.join would exceed memory limit (estimated ${estimatedSize} bytes, limit ${limit} bytes)`, estimatedSize, limit);
|
|
174
|
+
}
|
|
175
|
+
const result = originalJoin.call(this, separator);
|
|
176
|
+
tracker.trackString(result);
|
|
177
|
+
return result;
|
|
178
|
+
},
|
|
179
|
+
// toString() is equivalent to join(',')
|
|
180
|
+
toString: function () {
|
|
181
|
+
// Delegate to tracked join method
|
|
182
|
+
return this.join(',');
|
|
183
|
+
},
|
|
184
|
+
// Methods that create new arrays
|
|
185
|
+
concat: function (...items) {
|
|
186
|
+
const result = originalConcat.apply(this, items);
|
|
187
|
+
tracker.trackArray(result.length);
|
|
188
|
+
return result;
|
|
189
|
+
},
|
|
190
|
+
slice: function (start, end) {
|
|
191
|
+
const result = originalSlice.call(this, start, end);
|
|
192
|
+
tracker.trackArray(result.length);
|
|
193
|
+
return result;
|
|
194
|
+
},
|
|
195
|
+
splice: function (start, deleteCount, ...items) {
|
|
196
|
+
const result = originalSplice.call(this, start, deleteCount ?? 0, ...items);
|
|
197
|
+
// Track both the removed items array and the growth
|
|
198
|
+
tracker.trackArray(result.length);
|
|
199
|
+
if (items.length > (deleteCount ?? 0)) {
|
|
200
|
+
tracker.trackArray(items.length - (deleteCount ?? 0));
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
},
|
|
204
|
+
map: function (callback) {
|
|
205
|
+
const result = originalMap.call(this, callback);
|
|
206
|
+
tracker.trackArray(result.length);
|
|
207
|
+
return result;
|
|
208
|
+
},
|
|
209
|
+
filter: function (callback) {
|
|
210
|
+
const result = originalFilter.call(this, callback);
|
|
211
|
+
tracker.trackArray(result.length);
|
|
212
|
+
return result;
|
|
213
|
+
},
|
|
214
|
+
flatMap: function (callback) {
|
|
215
|
+
const result = originalFlatMap.call(this, callback);
|
|
216
|
+
tracker.trackArray(result.length);
|
|
217
|
+
return result;
|
|
218
|
+
},
|
|
219
|
+
flat: function (depth) {
|
|
220
|
+
const result = originalFlat.call(this, depth);
|
|
221
|
+
tracker.trackArray(result.length);
|
|
222
|
+
return result;
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Create a tracked string concat/repeat implementation
|
|
228
|
+
*
|
|
229
|
+
* The most common memory attack is string doubling via concatenation.
|
|
230
|
+
* This wraps string operations that create new strings.
|
|
231
|
+
*
|
|
232
|
+
* @param tracker MemoryTracker instance for allocation tracking
|
|
233
|
+
* @returns Object with tracked string methods
|
|
234
|
+
*/
|
|
235
|
+
function createTrackedStringMethods(tracker) {
|
|
236
|
+
const originalConcat = String.prototype.concat;
|
|
237
|
+
const originalRepeat = String.prototype.repeat;
|
|
238
|
+
const originalPadStart = String.prototype.padStart;
|
|
239
|
+
const originalPadEnd = String.prototype.padEnd;
|
|
240
|
+
return {
|
|
241
|
+
concat: function (...strings) {
|
|
242
|
+
const result = originalConcat.apply(this, strings);
|
|
243
|
+
tracker.trackString(result);
|
|
244
|
+
return result;
|
|
245
|
+
},
|
|
246
|
+
repeat: function (count) {
|
|
247
|
+
// Pre-check to avoid allocating before tracking
|
|
248
|
+
const estimatedSize = (0, memory_tracker_1.estimateStringSize)(this) * count;
|
|
249
|
+
if (tracker.getLimit() > 0 && estimatedSize > tracker.getLimit()) {
|
|
250
|
+
tracker.track(estimatedSize); // This will throw
|
|
251
|
+
}
|
|
252
|
+
const result = originalRepeat.call(this, count);
|
|
253
|
+
tracker.trackString(result);
|
|
254
|
+
return result;
|
|
255
|
+
},
|
|
256
|
+
padStart: function (targetLength, padString) {
|
|
257
|
+
const result = originalPadStart.call(this, targetLength, padString);
|
|
258
|
+
if (result.length > this.length) {
|
|
259
|
+
tracker.trackString(result);
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
},
|
|
263
|
+
padEnd: function (targetLength, padString) {
|
|
264
|
+
const result = originalPadEnd.call(this, targetLength, padString);
|
|
265
|
+
if (result.length > this.length) {
|
|
266
|
+
tracker.trackString(result);
|
|
267
|
+
}
|
|
268
|
+
return result;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Wrap the binary string concatenation operator
|
|
274
|
+
*
|
|
275
|
+
* This is tricky because we can't directly intercept the + operator.
|
|
276
|
+
* Instead, we track string results in the safe runtime by checking
|
|
277
|
+
* types after operations.
|
|
278
|
+
*
|
|
279
|
+
* @param tracker MemoryTracker instance
|
|
280
|
+
* @returns A function that should be called after any operation that might produce a string
|
|
281
|
+
*/
|
|
282
|
+
function createStringConcatTracker(tracker) {
|
|
283
|
+
return (result, originalLength = 0) => {
|
|
284
|
+
if (typeof result === 'string' && result.length > originalLength) {
|
|
285
|
+
// Only track the growth, not the entire string
|
|
286
|
+
const growth = result.length - originalLength;
|
|
287
|
+
if (growth > 0) {
|
|
288
|
+
tracker.track(growth * 2); // UTF-16: 2 bytes per char
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return result;
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Create all tracked globals for injection into sandbox
|
|
296
|
+
*
|
|
297
|
+
* Returns an object containing all memory-tracked constructors and methods.
|
|
298
|
+
*
|
|
299
|
+
* @param tracker MemoryTracker instance
|
|
300
|
+
* @returns Object with all tracked globals
|
|
301
|
+
*/
|
|
302
|
+
function createTrackedGlobals(tracker) {
|
|
303
|
+
return {
|
|
304
|
+
String: createTrackedString(tracker),
|
|
305
|
+
Array: createTrackedArray(tracker),
|
|
306
|
+
// Note: We can't easily replace prototype methods without affecting security
|
|
307
|
+
// The tracked array/string methods are available for manual integration
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=memory-proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-proxy.js","sourceRoot":"","sources":["../../src/memory-proxy.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AAaH,kDAaC;AAWD,gDA+FC;AAWD,8DAoGC;AAWD,gEAwCC;AAYD,8DAaC;AAUD,oDAOC;AA9UD,qDAAuF;AAEvF;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAAC,OAAsB;IACxD,OAAO,IAAI,KAAK,CAAC,MAAM,EAAE;QACvB,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI;YACzB,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAW,CAAC;YAC9D,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;YAC5B,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,SAAS,CAAC,MAAM,EAAE,IAAI;YACpB,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,GAAI,IAAkB,CAAC,CAAC;YAClD,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;YACtC,OAAO,MAAM,CAAC;QAChB,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH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AAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,SAAgB,yBAAyB,CACvC,OAAsB;IAEtB,OAAO,CAAC,MAAe,EAAE,cAAc,GAAG,CAAC,EAAE,EAAE;QAC7C,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,CAAC,MAAM,GAAG,cAAc,EAAE,CAAC;YACjE,+CAA+C;YAC/C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,cAAc,CAAC;YAC9C,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,2BAA2B;YACxD,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAAC,OAAsB;IACzD,OAAO;QACL,MAAM,EAAE,mBAAmB,CAAC,OAAO,CAAC;QACpC,KAAK,EAAE,kBAAkB,CAAC,OAAO,CAAC;QAClC,6EAA6E;QAC7E,wEAAwE;KACzE,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Memory-Tracking Proxies\n *\n * Provides proxied constructors that track memory allocations.\n * Used in conjunction with MemoryTracker to enforce memory limits.\n *\n * @packageDocumentation\n */\n\nimport { MemoryTracker, estimateStringSize, MemoryLimitError } from './memory-tracker';\n\n/**\n * Create a memory-tracking String constructor proxy\n *\n * Wraps the String constructor to track string allocations.\n * Tracks both `String()` calls and `new String()` constructions.\n *\n * @param tracker MemoryTracker instance for allocation tracking\n * @returns Proxied String constructor\n */\nexport function createTrackedString(tracker: MemoryTracker): typeof String {\n return new Proxy(String, {\n apply(target, thisArg, args) {\n const result = Reflect.apply(target, thisArg, args) as string;\n tracker.trackString(result);\n return result;\n },\n construct(target, args) {\n const result = new target(...(args as [unknown]));\n tracker.trackString(result.valueOf());\n return result;\n },\n });\n}\n\n/**\n * Create a memory-tracking Array constructor proxy\n *\n * Wraps the Array constructor and array methods that can grow arrays.\n * Tracks initial allocation and subsequent growth operations.\n *\n * @param tracker MemoryTracker instance for allocation tracking\n * @returns Proxied Array constructor\n */\nexport function createTrackedArray(tracker: MemoryTracker): typeof Array {\n // Create a tracked wrapper for array instances to intercept dangerous methods\n function wrapArrayInstance<T extends unknown[]>(arr: T): T {\n return new Proxy(arr, {\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n\n // Intercept join() to track and limit memory-heavy string creation\n if (prop === 'join' && typeof value === 'function') {\n return function (this: unknown[], separator?: string) {\n const sep = separator ?? ',';\n const arrLength = target.length;\n\n // Pre-calculate estimated size\n let estimatedSize = 0;\n if (arrLength > 10000) {\n // Rough estimate for large arrays\n estimatedSize = arrLength * (10 + sep.length) * 2;\n } else {\n for (let i = 0; i < arrLength; i++) {\n const item = target[i];\n estimatedSize += (item == null ? 0 : String(item).length) + sep.length;\n }\n estimatedSize = estimatedSize * 2; // UTF-16\n }\n\n // Pre-check limit before allocating\n const limit = tracker.getLimit();\n if (limit > 0 && estimatedSize > limit) {\n throw new MemoryLimitError(\n `Array.join would exceed memory limit (estimated ${estimatedSize} bytes, limit ${limit} bytes)`,\n estimatedSize,\n limit,\n );\n }\n\n const result = Array.prototype.join.call(target, separator);\n tracker.trackString(result);\n return result;\n };\n }\n\n // Intercept toString() (uses join(',') internally)\n if (prop === 'toString' && typeof value === 'function') {\n return function (this: unknown[]) {\n // Delegate to tracked join\n const joinFn = Reflect.get(receiver, 'join', receiver) as (sep?: string) => string;\n return joinFn.call(target, ',');\n };\n }\n\n return value;\n },\n }) as T;\n }\n\n const TrackedArray = new Proxy(Array, {\n apply(target, thisArg, args) {\n const result = Reflect.apply(target, thisArg, args) as unknown[];\n if (Array.isArray(result)) {\n tracker.trackArray(result.length);\n return wrapArrayInstance(result);\n }\n return result;\n },\n construct(target, args) {\n const result = new target(...(args as unknown[]));\n tracker.trackArray(result.length);\n return wrapArrayInstance(result);\n },\n get(target, prop, receiver) {\n const value = Reflect.get(target, prop, receiver);\n\n // Wrap static methods that create arrays\n if (prop === 'from') {\n return function (...args: Parameters<typeof Array.from>) {\n const result = Array.from(...args);\n tracker.trackArray(result.length);\n return wrapArrayInstance(result);\n };\n }\n\n if (prop === 'of') {\n return function (...items: unknown[]) {\n const result = Array.of(...items);\n tracker.trackArray(result.length);\n return wrapArrayInstance(result);\n };\n }\n\n return value;\n },\n });\n\n return TrackedArray;\n}\n\n/**\n * Create tracked array prototype methods\n *\n * Returns an object with wrapped array methods that track allocations.\n * These should be applied to the sandbox's Array.prototype.\n *\n * @param tracker MemoryTracker instance for allocation tracking\n * @returns Object with tracked array methods\n */\nexport function createTrackedArrayMethods(tracker: MemoryTracker): Record<string, unknown> {\n const originalConcat = Array.prototype.concat;\n const originalSlice = Array.prototype.slice;\n const originalSplice = Array.prototype.splice;\n const originalMap = Array.prototype.map;\n const originalFilter = Array.prototype.filter;\n const originalFlatMap = Array.prototype.flatMap;\n const originalFlat = Array.prototype.flat;\n const originalJoin = Array.prototype.join;\n\n return {\n // join() can create very large strings from large arrays\n // Attack vector: new Array(12_000_000).join('x') creates ~12MB string\n join: function (this: unknown[], separator?: string) {\n const sep = separator ?? ',';\n\n // Pre-calculate estimated size to check limit before allocating\n let estimatedSize = 0;\n const arrLength = this.length;\n\n // For very large arrays, estimate without iterating every element\n if (arrLength > 10000) {\n // Rough estimate: avg element is ~10 chars + separator\n estimatedSize = arrLength * (10 + sep.length) * 2; // UTF-16\n } else {\n for (let i = 0; i < arrLength; i++) {\n const item = this[i];\n estimatedSize += (item == null ? 0 : String(item).length) + sep.length;\n }\n estimatedSize = estimatedSize * 2; // UTF-16\n }\n\n // Pre-check limit before allocating\n const limit = tracker.getLimit();\n if (limit > 0 && estimatedSize > limit) {\n throw new MemoryLimitError(\n `Array.join would exceed memory limit (estimated ${estimatedSize} bytes, limit ${limit} bytes)`,\n estimatedSize,\n limit,\n );\n }\n\n const result = originalJoin.call(this, separator);\n tracker.trackString(result);\n return result;\n },\n\n // toString() is equivalent to join(',')\n toString: function (this: unknown[]) {\n // Delegate to tracked join method\n return this.join(',');\n },\n\n // Methods that create new arrays\n concat: function (this: unknown[], ...items: unknown[]) {\n const result = originalConcat.apply(this, items);\n tracker.trackArray(result.length);\n return result;\n },\n\n slice: function (this: unknown[], start?: number, end?: number) {\n const result = originalSlice.call(this, start, end);\n tracker.trackArray(result.length);\n return result;\n },\n\n splice: function (this: unknown[], start: number, deleteCount?: number, ...items: unknown[]) {\n const result = originalSplice.call(this, start, deleteCount ?? 0, ...items);\n // Track both the removed items array and the growth\n tracker.trackArray(result.length);\n if (items.length > (deleteCount ?? 0)) {\n tracker.trackArray(items.length - (deleteCount ?? 0));\n }\n return result;\n },\n\n map: function <T, U>(this: T[], callback: (value: T, index: number, array: T[]) => U) {\n const result = originalMap.call(this, callback);\n tracker.trackArray(result.length);\n return result;\n },\n\n filter: function <T>(this: T[], callback: (value: T, index: number, array: T[]) => boolean) {\n const result = originalFilter.call(this, callback);\n tracker.trackArray(result.length);\n return result;\n },\n\n flatMap: function <T, U>(this: T[], callback: (value: T, index: number, array: T[]) => U | U[]) {\n const result = originalFlatMap.call(this, callback);\n tracker.trackArray(result.length);\n return result;\n },\n\n flat: function (this: unknown[], depth?: number) {\n const result = originalFlat.call(this, depth);\n tracker.trackArray(result.length);\n return result;\n },\n };\n}\n\n/**\n * Create a tracked string concat/repeat implementation\n *\n * The most common memory attack is string doubling via concatenation.\n * This wraps string operations that create new strings.\n *\n * @param tracker MemoryTracker instance for allocation tracking\n * @returns Object with tracked string methods\n */\nexport function createTrackedStringMethods(tracker: MemoryTracker): Record<string, unknown> {\n const originalConcat = String.prototype.concat;\n const originalRepeat = String.prototype.repeat;\n const originalPadStart = String.prototype.padStart;\n const originalPadEnd = String.prototype.padEnd;\n\n return {\n concat: function (this: string, ...strings: string[]) {\n const result = originalConcat.apply(this, strings);\n tracker.trackString(result);\n return result;\n },\n\n repeat: function (this: string, count: number) {\n // Pre-check to avoid allocating before tracking\n const estimatedSize = estimateStringSize(this) * count;\n if (tracker.getLimit() > 0 && estimatedSize > tracker.getLimit()) {\n tracker.track(estimatedSize); // This will throw\n }\n const result = originalRepeat.call(this, count);\n tracker.trackString(result);\n return result;\n },\n\n padStart: function (this: string, targetLength: number, padString?: string) {\n const result = originalPadStart.call(this, targetLength, padString);\n if (result.length > this.length) {\n tracker.trackString(result);\n }\n return result;\n },\n\n padEnd: function (this: string, targetLength: number, padString?: string) {\n const result = originalPadEnd.call(this, targetLength, padString);\n if (result.length > this.length) {\n tracker.trackString(result);\n }\n return result;\n },\n };\n}\n\n/**\n * Wrap the binary string concatenation operator\n *\n * This is tricky because we can't directly intercept the + operator.\n * Instead, we track string results in the safe runtime by checking\n * types after operations.\n *\n * @param tracker MemoryTracker instance\n * @returns A function that should be called after any operation that might produce a string\n */\nexport function createStringConcatTracker(\n tracker: MemoryTracker,\n): (result: unknown, originalLength?: number) => unknown {\n return (result: unknown, originalLength = 0) => {\n if (typeof result === 'string' && result.length > originalLength) {\n // Only track the growth, not the entire string\n const growth = result.length - originalLength;\n if (growth > 0) {\n tracker.track(growth * 2); // UTF-16: 2 bytes per char\n }\n }\n return result;\n };\n}\n\n/**\n * Create all tracked globals for injection into sandbox\n *\n * Returns an object containing all memory-tracked constructors and methods.\n *\n * @param tracker MemoryTracker instance\n * @returns Object with all tracked globals\n */\nexport function createTrackedGlobals(tracker: MemoryTracker): Record<string, unknown> {\n return {\n String: createTrackedString(tracker),\n Array: createTrackedArray(tracker),\n // Note: We can't easily replace prototype methods without affecting security\n // The tracked array/string methods are available for manual integration\n };\n}\n"]}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Tracker for VM Execution
|
|
3
|
+
*
|
|
4
|
+
* Provides allocation tracking and memory limit enforcement for the VM adapter.
|
|
5
|
+
* Since Node.js vm module doesn't support native memory limits, this tracker
|
|
6
|
+
* monitors allocations via instrumented code and proxy wrappers.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for memory tracking
|
|
12
|
+
*/
|
|
13
|
+
export interface MemoryTrackerConfig {
|
|
14
|
+
/** Maximum total bytes allowed (0 = unlimited) */
|
|
15
|
+
memoryLimit: number;
|
|
16
|
+
/** Enable string size tracking */
|
|
17
|
+
trackStrings: boolean;
|
|
18
|
+
/** Enable array size tracking */
|
|
19
|
+
trackArrays: boolean;
|
|
20
|
+
/** Enable object property tracking */
|
|
21
|
+
trackObjects: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Snapshot of memory usage at a point in time
|
|
25
|
+
*/
|
|
26
|
+
export interface MemoryUsageSnapshot {
|
|
27
|
+
/** Current tracked bytes (sum of estimated allocation sizes) */
|
|
28
|
+
trackedBytes: number;
|
|
29
|
+
/** Peak tracked bytes during execution */
|
|
30
|
+
peakTrackedBytes: number;
|
|
31
|
+
/** Number of allocation operations tracked */
|
|
32
|
+
allocationCount: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when memory limit is exceeded
|
|
36
|
+
*/
|
|
37
|
+
export declare class MemoryLimitError extends Error {
|
|
38
|
+
/** Bytes used when limit was exceeded */
|
|
39
|
+
readonly usedBytes: number;
|
|
40
|
+
/** Configured limit in bytes */
|
|
41
|
+
readonly limitBytes: number;
|
|
42
|
+
/** Error code for identification */
|
|
43
|
+
readonly code = "MEMORY_LIMIT_EXCEEDED";
|
|
44
|
+
constructor(message: string,
|
|
45
|
+
/** Bytes used when limit was exceeded */
|
|
46
|
+
usedBytes: number,
|
|
47
|
+
/** Configured limit in bytes */
|
|
48
|
+
limitBytes: number);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Estimate the memory size of a string in V8
|
|
52
|
+
*
|
|
53
|
+
* V8 uses 2 bytes per character (UTF-16) plus object overhead.
|
|
54
|
+
* For strings > 12 chars, V8 uses heap allocation with ~40 bytes overhead.
|
|
55
|
+
* For short strings, V8 may use inline storage.
|
|
56
|
+
*
|
|
57
|
+
* @param str String to estimate size for
|
|
58
|
+
* @returns Estimated size in bytes
|
|
59
|
+
*/
|
|
60
|
+
export declare function estimateStringSize(str: string): number;
|
|
61
|
+
/**
|
|
62
|
+
* Estimate the memory size of an array in V8
|
|
63
|
+
*
|
|
64
|
+
* Arrays have object overhead plus pointers to elements.
|
|
65
|
+
* We estimate 8 bytes per element (pointer size on 64-bit).
|
|
66
|
+
*
|
|
67
|
+
* @param length Array length
|
|
68
|
+
* @param elementEstimate Optional per-element size estimate
|
|
69
|
+
* @returns Estimated size in bytes
|
|
70
|
+
*/
|
|
71
|
+
export declare function estimateArraySize(length: number, elementEstimate?: number): number;
|
|
72
|
+
/**
|
|
73
|
+
* Estimate the memory size of an object in V8
|
|
74
|
+
*
|
|
75
|
+
* Objects have base overhead plus per-property storage.
|
|
76
|
+
*
|
|
77
|
+
* @param propertyCount Number of properties
|
|
78
|
+
* @returns Estimated size in bytes
|
|
79
|
+
*/
|
|
80
|
+
export declare function estimateObjectSize(propertyCount: number): number;
|
|
81
|
+
/**
|
|
82
|
+
* Memory tracker for VM execution
|
|
83
|
+
*
|
|
84
|
+
* Tracks allocations via instrumented code and enforces memory limits.
|
|
85
|
+
* Use `start()` before execution and `getSnapshot()` after for stats.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const tracker = new MemoryTracker({ memoryLimit: 10 * 1024 * 1024 }); // 10MB
|
|
90
|
+
* tracker.start();
|
|
91
|
+
*
|
|
92
|
+
* // During execution, track allocations:
|
|
93
|
+
* tracker.track(estimateStringSize(someString));
|
|
94
|
+
*
|
|
95
|
+
* // After execution, get stats:
|
|
96
|
+
* const snapshot = tracker.getSnapshot();
|
|
97
|
+
* console.log(`Peak memory: ${snapshot.peakTrackedBytes} bytes`);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare class MemoryTracker {
|
|
101
|
+
private trackedBytes;
|
|
102
|
+
private peakTrackedBytes;
|
|
103
|
+
private allocationCount;
|
|
104
|
+
private readonly config;
|
|
105
|
+
constructor(config?: Partial<MemoryTrackerConfig>);
|
|
106
|
+
/**
|
|
107
|
+
* Start tracking (resets all counters)
|
|
108
|
+
*/
|
|
109
|
+
start(): void;
|
|
110
|
+
/**
|
|
111
|
+
* Track an allocation
|
|
112
|
+
*
|
|
113
|
+
* @param bytes Number of bytes allocated
|
|
114
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
115
|
+
*/
|
|
116
|
+
track(bytes: number): void;
|
|
117
|
+
/**
|
|
118
|
+
* Track a string allocation
|
|
119
|
+
*
|
|
120
|
+
* @param str String that was allocated
|
|
121
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
122
|
+
*/
|
|
123
|
+
trackString(str: string): void;
|
|
124
|
+
/**
|
|
125
|
+
* Track an array allocation
|
|
126
|
+
*
|
|
127
|
+
* @param length Array length
|
|
128
|
+
* @param elementEstimate Optional per-element size estimate
|
|
129
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
130
|
+
*/
|
|
131
|
+
trackArray(length: number, elementEstimate?: number): void;
|
|
132
|
+
/**
|
|
133
|
+
* Track an object allocation
|
|
134
|
+
*
|
|
135
|
+
* @param propertyCount Number of properties
|
|
136
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
137
|
+
*/
|
|
138
|
+
trackObject(propertyCount: number): void;
|
|
139
|
+
/**
|
|
140
|
+
* Release tracked memory (for GC simulation)
|
|
141
|
+
*
|
|
142
|
+
* @param bytes Number of bytes to release
|
|
143
|
+
*/
|
|
144
|
+
release(bytes: number): void;
|
|
145
|
+
/**
|
|
146
|
+
* Get current memory usage snapshot
|
|
147
|
+
*
|
|
148
|
+
* @returns Snapshot of memory usage
|
|
149
|
+
*/
|
|
150
|
+
getSnapshot(): MemoryUsageSnapshot;
|
|
151
|
+
/**
|
|
152
|
+
* Get configured memory limit
|
|
153
|
+
*
|
|
154
|
+
* @returns Memory limit in bytes (0 = unlimited)
|
|
155
|
+
*/
|
|
156
|
+
getLimit(): number;
|
|
157
|
+
/**
|
|
158
|
+
* Check if tracking is enabled
|
|
159
|
+
*
|
|
160
|
+
* @returns True if any tracking is enabled
|
|
161
|
+
*/
|
|
162
|
+
isEnabled(): boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Format bytes as human-readable string
|
|
165
|
+
*
|
|
166
|
+
* @param bytes Number of bytes
|
|
167
|
+
* @returns Formatted string (e.g., "10.5MB")
|
|
168
|
+
*/
|
|
169
|
+
private formatBytes;
|
|
170
|
+
}
|