forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryPolicyTest.cpp
More file actions
278 lines (239 loc) · 10.9 KB
/
MemoryPolicyTest.cpp
File metadata and controls
278 lines (239 loc) · 10.9 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
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "catch.hpp"
#pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs
namespace MemoryPolicyTests
{
static const size_t MemoryLimit = 10 * 1024 * 1024;
void ValidateOOMException()
{
JsValueRef exception = JS_INVALID_REFERENCE;
JsErrorCode errorCode = JsGetAndClearException(&exception);
if (errorCode == JsNoError)
{
JsPropertyIdRef property = JS_INVALID_REFERENCE;
JsValueRef value = JS_INVALID_REFERENCE;
LPCWSTR str = nullptr;
size_t length;
REQUIRE(JsGetPropertyIdFromName(_u("message"), &property) == JsNoError);
REQUIRE(JsGetProperty(exception, property, &value) == JsNoError);
REQUIRE(JsStringToPointer(value, &str, &length) == JsNoError);
CHECK(wcscmp(str, _u("Out of memory")) == 0);
}
else
{
// If we don't have enough memory to create error object, then GetAndClearException might
// fail and return ErrorInvalidArgument. Check if we don't get any other error code.
CHECK(errorCode == JsErrorInvalidArgument);
}
}
void BasicTest(JsRuntimeAttributes attributes, LPCSTR fileName)
{
LPCWSTR script = nullptr;
REQUIRE(FileLoadHelpers::LoadScriptFromFile(fileName, script) == S_OK);
REQUIRE(script != nullptr);
// Create the runtime
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
REQUIRE(JsCreateRuntime(attributes, nullptr, &runtime) == JsNoError);
// Set memory limit
REQUIRE(JsSetRuntimeMemoryLimit(runtime, MemoryLimit) == JsNoError);
size_t memoryLimit;
size_t memoryUsage;
REQUIRE(JsGetRuntimeMemoryLimit(runtime, &memoryLimit) == JsNoError);
CHECK(memoryLimit == MemoryLimit);
REQUIRE(JsGetRuntimeMemoryUsage(runtime, &memoryUsage) == JsNoError);
CHECK(memoryUsage < MemoryLimit);
// Create and initialize the script context
JsContextRef context = JS_INVALID_RUNTIME_HANDLE;
REQUIRE(JsCreateContext(runtime, &context) == JsNoError);
REQUIRE(JsSetCurrentContext(context) == JsNoError);
// Invoke the script
REQUIRE(JsRunScript(script, JS_SOURCE_CONTEXT_NONE, _u(""), nullptr) == JsErrorScriptException);
ValidateOOMException();
REQUIRE(JsGetRuntimeMemoryLimit(runtime, &memoryLimit) == JsNoError);
CHECK(memoryLimit == MemoryLimit);
REQUIRE(JsGetRuntimeMemoryUsage(runtime, &memoryUsage) == JsNoError);
CHECK(memoryUsage <= MemoryLimit);
// Destroy the runtime
REQUIRE(JsSetCurrentContext(JS_INVALID_REFERENCE) == JsNoError);
REQUIRE(JsCollectGarbage(runtime) == JsNoError);
REQUIRE(JsDisposeRuntime(runtime) == JsNoError);
}
TEST_CASE("MemoryPolicyTest_UnboundedMemory", "[MemoryPolicyTest]")
{
BasicTest(JsRuntimeAttributeNone, "UnboundedMemory.js");
BasicTest(JsRuntimeAttributeDisableBackgroundWork, "UnboundedMemory.js");
}
TEST_CASE("MemoryPolicyTest_ArrayTest", "[MemoryPolicyTest]")
{
BasicTest(JsRuntimeAttributeNone, "arrayTest.js");
BasicTest(JsRuntimeAttributeDisableBackgroundWork, "arrayTest.js");
}
TEST_CASE("MemoryPolicyTest_ArrayBuffer", "[MemoryPolicyTest]")
{
BasicTest(JsRuntimeAttributeNone, "arraybuffer.js");
BasicTest(JsRuntimeAttributeDisableBackgroundWork, "arraybuffer.js");
}
void OOSTest(JsRuntimeAttributes attributes)
{
JsPropertyIdRef property;
JsValueRef value, exception;
LPCWSTR str;
size_t length;
LPCWSTR script = nullptr;
REQUIRE(FileLoadHelpers::LoadScriptFromFile("oos.js", script) == S_OK);
REQUIRE(script != nullptr);
// Create the runtime
JsRuntimeHandle runtime = JS_INVALID_RUNTIME_HANDLE;
REQUIRE(JsCreateRuntime(attributes, nullptr, &runtime) == JsNoError);
// Create and initialize the script context
JsContextRef context;
REQUIRE(JsCreateContext(runtime, &context) == JsNoError);
REQUIRE(JsSetCurrentContext(context) == JsNoError);
// Invoke the script
REQUIRE(JsRunScript(script, JS_SOURCE_CONTEXT_NONE, L"", nullptr) == JsErrorScriptException);
// Verify we got OOS
REQUIRE(JsGetAndClearException(&exception) == JsNoError);
REQUIRE(JsGetPropertyIdFromName(L"message", &property) == JsNoError);
REQUIRE(JsGetProperty(exception, property, &value) == JsNoError);
REQUIRE(JsStringToPointer(value, &str, &length) == JsNoError);
CHECK(wcscmp(str, _u("Out of stack space")) == 0);
// Destroy the runtime
REQUIRE(JsSetCurrentContext(JS_INVALID_REFERENCE) == JsNoError);
REQUIRE(JsDisposeRuntime(runtime) == JsNoError);
}
TEST_CASE("MemoryPolicyTest_OOSTest", "[OOSTest]")
{
OOSTest(JsRuntimeAttributeNone);
OOSTest(JsRuntimeAttributeDisableBackgroundWork);
}
class MemoryPolicyTest
{
public:
MemoryPolicyTest() : totalAllocationSize(0) { }
size_t totalAllocationSize;
};
static bool CALLBACK MemoryAllocationCallback(LPVOID context, JsMemoryEventType allocationEvent, size_t allocationSize)
{
REQUIRE(context != nullptr);
MemoryPolicyTest* memoryPolicyTest = static_cast<MemoryPolicyTest*>(context);
switch (allocationEvent)
{
case JsMemoryAllocate:
memoryPolicyTest->totalAllocationSize += allocationSize;
if (memoryPolicyTest->totalAllocationSize > MemoryLimit)
{
return false;
}
return true;
break;
case JsMemoryFree:
memoryPolicyTest->totalAllocationSize -= allocationSize;
break;
case JsMemoryFailure:
memoryPolicyTest->totalAllocationSize -= allocationSize;
break;
default:
CHECK(false);
}
return true;
}
void NegativeTest(JsRuntimeAttributes attributes)
{
MemoryPolicyTest memoryPolicyTest;
// Create the runtime
JsRuntimeHandle runtime;
REQUIRE(JsCreateRuntime(attributes, nullptr, &runtime) == JsNoError);
// after setting callback, you can set limit
REQUIRE(JsSetRuntimeMemoryAllocationCallback(runtime, &memoryPolicyTest, MemoryAllocationCallback) == JsNoError);
REQUIRE(JsSetRuntimeMemoryLimit(runtime, MemoryLimit) == JsNoError);
REQUIRE(JsSetRuntimeMemoryAllocationCallback(runtime, nullptr, nullptr) == JsNoError);
// now we can set limit
REQUIRE(JsSetRuntimeMemoryLimit(runtime, MemoryLimit) == JsNoError);
// and we can set the callback again.
REQUIRE(JsSetRuntimeMemoryAllocationCallback(runtime, &memoryPolicyTest, MemoryAllocationCallback) == JsNoError);
REQUIRE(JsSetRuntimeMemoryLimit(runtime, (size_t)-1) == JsNoError);
REQUIRE(JsSetRuntimeMemoryAllocationCallback(runtime, &memoryPolicyTest, MemoryAllocationCallback) == JsNoError);
// Destroy the runtime
REQUIRE(JsSetCurrentContext(JS_INVALID_REFERENCE) == JsNoError);
REQUIRE(JsDisposeRuntime(runtime) == JsNoError);
}
TEST_CASE("MemoryPolicyTest_NegativeTest", "[NegativeTest]")
{
NegativeTest(JsRuntimeAttributeNone);
NegativeTest(JsRuntimeAttributeDisableBackgroundWork);
}
void ContextLeak()
{
MemoryPolicyTest memoryPolicyTest;
JsRuntimeHandle jsRuntime = JS_INVALID_RUNTIME_HANDLE;
JsErrorCode errorCode = ::JsCreateRuntime(
(JsRuntimeAttributes)(JsRuntimeAttributeAllowScriptInterrupt | JsRuntimeAttributeDisableEval | JsRuntimeAttributeDisableNativeCodeGeneration),
nullptr,
&jsRuntime);
REQUIRE(errorCode == JsNoError);
REQUIRE(JsSetRuntimeMemoryAllocationCallback(jsRuntime, &memoryPolicyTest, MemoryAllocationCallback) == JsNoError);
for (ULONG i = 0; i < 1000; ++i)
{
JsContextRef jsContext = JS_INVALID_REFERENCE;
errorCode = ::JsCreateContext(jsRuntime, &jsContext);
REQUIRE(errorCode == JsNoError);
errorCode = ::JsSetCurrentContext(jsContext);
REQUIRE(errorCode == JsNoError);
errorCode = ::JsSetCurrentContext(JS_INVALID_REFERENCE);
REQUIRE(errorCode == JsNoError);
if (((ULONG)(i % 100)) * 100 == 0)
{
JsCollectGarbage(jsRuntime);
}
}
REQUIRE(JsDisposeRuntime(jsRuntime) == JsNoError);
}
TEST_CASE("MemoryPolicyTest_ContextLeak", "[ContextLeak]")
{
ContextLeak();
}
void ContextLeak1()
{
MemoryPolicyTest memoryPolicyTest;
JsRuntimeHandle jsRuntime = JS_INVALID_RUNTIME_HANDLE;
JsErrorCode errorCode = ::JsCreateRuntime(
(JsRuntimeAttributes)(JsRuntimeAttributeAllowScriptInterrupt | JsRuntimeAttributeDisableEval | JsRuntimeAttributeDisableNativeCodeGeneration),
nullptr,
&jsRuntime);
REQUIRE(errorCode == JsNoError);
REQUIRE(JsSetRuntimeMemoryAllocationCallback(jsRuntime, &memoryPolicyTest, MemoryAllocationCallback) == JsNoError);
for (ULONG i = 0; i < 1000; ++i)
{
JsContextRef jsContext = JS_INVALID_REFERENCE;
JsContextRef jsContext1 = JS_INVALID_REFERENCE;
JsValueRef jsVal1 = JS_INVALID_REFERENCE, jsGO2 = JS_INVALID_REFERENCE;
JsPropertyIdRef propertyId;
REQUIRE(::JsCreateContext(jsRuntime, &jsContext) == JsNoError);
REQUIRE(::JsSetCurrentContext(jsContext) == JsNoError);
REQUIRE(::JsCreateObject(&jsVal1) == JsNoError);
REQUIRE(::JsGetPropertyIdFromName(L"test", &propertyId) == JsNoError);
REQUIRE(::JsCreateContext(jsRuntime, &jsContext1) == JsNoError);
REQUIRE(::JsSetCurrentContext(jsContext1) == JsNoError);
REQUIRE(::JsGetGlobalObject(&jsGO2) == JsNoError);
REQUIRE(::JsSetProperty(jsGO2, propertyId, jsVal1, true) == JsNoError);
REQUIRE(JsSetCurrentContext(JS_INVALID_REFERENCE) == JsNoError);
if (((ULONG)(i % 100)) * 100 == 0)
{
jsVal1 = JS_INVALID_REFERENCE;
jsGO2 = JS_INVALID_REFERENCE;
jsContext = JS_INVALID_REFERENCE;
jsContext1 = JS_INVALID_REFERENCE;
JsCollectGarbage(jsRuntime);
}
}
REQUIRE(JsDisposeRuntime(jsRuntime) == JsNoError);
}
TEST_CASE("MemoryPolicyTest_ContextLeak1", "[ContextLeak1]")
{
ContextLeak1();
}
}