/src/quickjs/fuzz/fuzz_common.c
Line | Count | Source (jump to first uncovered line) |
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 | 44 | { |
23 | 44 | nbinterrupts++; |
24 | 44 | return (nbinterrupts > 100); |
25 | 44 | } |
26 | | |
27 | 1 | void reset_nbinterrupts() { |
28 | 1 | nbinterrupts = 0; |
29 | 1 | } |
30 | | |
31 | 3 | void test_one_input_init(JSRuntime *rt, JSContext *ctx) { |
32 | | // 64 Mo |
33 | 3 | JS_SetMemoryLimit(rt, 0x4000000); |
34 | | // 64 Kb |
35 | 3 | JS_SetMaxStackSize(rt, 0x10000); |
36 | | |
37 | 3 | JS_AddIntrinsicBigFloat(ctx); |
38 | 3 | JS_AddIntrinsicBigDecimal(ctx); |
39 | 3 | JS_AddIntrinsicOperators(ctx); |
40 | 3 | JS_EnableBignumExt(ctx, 1); |
41 | 3 | JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL); |
42 | 3 | JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL); |
43 | 3 | js_std_add_helpers(ctx, 0, NULL); |
44 | | |
45 | | // Load os and std |
46 | 3 | js_std_init_handlers(rt); |
47 | 3 | js_init_module_std(ctx, "std"); |
48 | 3 | js_init_module_os(ctx, "os"); |
49 | 3 | const char *str = "import * as std from 'std';\n" |
50 | 3 | "import * as os from 'os';\n" |
51 | 3 | "globalThis.std = std;\n" |
52 | 3 | "globalThis.os = os;\n"; |
53 | 3 | JSValue std_val = JS_Eval(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY); |
54 | 3 | if (!JS_IsException(std_val)) { |
55 | 3 | js_module_set_import_meta(ctx, std_val, 1, 1); |
56 | 3 | std_val = JS_EvalFunction(ctx, std_val); |
57 | 3 | } else { |
58 | 0 | js_std_dump_error(ctx); |
59 | 0 | } |
60 | 3 | std_val = js_std_await(ctx, std_val); |
61 | 3 | JS_FreeValue(ctx, std_val); |
62 | 3 | } |