Skip to content

Commit d53aafe

Browse files
Add the missing fuzz_common.c (bellard#292)
1 parent db9dbd0 commit d53aafe

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

fuzz/fuzz_common.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright 2020 Google Inc.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
#include <string.h>
17+
18+
#include "fuzz/fuzz_common.h"
19+
20+
// handle timeouts from infinite loops
21+
static int interrupt_handler(JSRuntime *rt, void *opaque)
22+
{
23+
nbinterrupts++;
24+
return (nbinterrupts > 100);
25+
}
26+
27+
void reset_nbinterrupts() {
28+
nbinterrupts = 0;
29+
}
30+
31+
void test_one_input_init(JSRuntime *rt, JSContext *ctx) {
32+
// 64 Mo
33+
JS_SetMemoryLimit(rt, 0x4000000);
34+
// 64 Kb
35+
JS_SetMaxStackSize(rt, 0x10000);
36+
37+
JS_AddIntrinsicBigFloat(ctx);
38+
JS_AddIntrinsicBigDecimal(ctx);
39+
JS_AddIntrinsicOperators(ctx);
40+
JS_EnableBignumExt(ctx, 1);
41+
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
42+
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
43+
js_std_add_helpers(ctx, 0, NULL);
44+
45+
// Load os and std
46+
js_std_init_handlers(rt);
47+
js_init_module_std(ctx, "std");
48+
js_init_module_os(ctx, "os");
49+
const char *str = "import * as std from 'std';\n"
50+
"import * as os from 'os';\n"
51+
"globalThis.std = std;\n"
52+
"globalThis.os = os;\n";
53+
JSValue std_val = JS_Eval(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
54+
if (!JS_IsException(std_val)) {
55+
js_module_set_import_meta(ctx, std_val, 1, 1);
56+
std_val = JS_EvalFunction(ctx, std_val);
57+
} else {
58+
js_std_dump_error(ctx);
59+
}
60+
std_val = js_std_await(ctx, std_val);
61+
JS_FreeValue(ctx, std_val);
62+
}

0 commit comments

Comments
 (0)