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 "quickjs-libc.h"
17 #include "cutils.h"
18
19 #include <stdint.h>
20 #include <stdio.h>
21
22 static int initialized = 0;
23 JSRuntime *rt;
24 JSContext *ctx;
25 static int nbinterrupts = 0;
26
27 // handle timeouts from infinite loops
interrupt_handler(JSRuntime * rt,void * opaque)28 static int interrupt_handler(JSRuntime *rt, void *opaque)
29 {
30 nbinterrupts++;
31 return (nbinterrupts > 100);
32 }
33
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)34 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
35 if (initialized == 0) {
36 rt = JS_NewRuntime();
37 // 64 Mo
38 JS_SetMemoryLimit(rt, 0x4000000);
39 //TODO JS_SetMaxStackSize ?
40 ctx = JS_NewContextRaw(rt);
41 JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
42 JS_AddIntrinsicBaseObjects(ctx);
43 JS_AddIntrinsicDate(ctx);
44 JS_AddIntrinsicEval(ctx);
45 JS_AddIntrinsicStringNormalize(ctx);
46 JS_AddIntrinsicRegExp(ctx);
47 JS_AddIntrinsicJSON(ctx);
48 JS_AddIntrinsicProxy(ctx);
49 JS_AddIntrinsicMapSet(ctx);
50 JS_AddIntrinsicTypedArrays(ctx);
51 JS_AddIntrinsicPromise(ctx);
52 JS_AddIntrinsicBigInt(ctx);
53 JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
54 js_std_add_helpers(ctx, 0, NULL);
55 initialized = 1;
56 }
57
58 if (Size > 0) {
59 if (Data[Size-1] != 0) {
60 return 0;
61 }
62 JSValue obj;
63 obj = JS_Eval(ctx, (const char *)Data, Size-1, "<none>", JS_EVAL_FLAG_COMPILE_ONLY | JS_EVAL_TYPE_GLOBAL | JS_EVAL_TYPE_MODULE);
64 //TODO target with JS_ParseJSON
65 if (JS_IsException(obj)) {
66 return 0;
67 }
68 size_t bytecode_size;
69 uint8_t* bytecode = JS_WriteObject(ctx, &bytecode_size, obj, JS_WRITE_OBJ_BYTECODE);
70 JS_FreeValue(ctx, obj);
71 if ( !bytecode ) {
72 return 0;
73 }
74 obj = JS_ReadObject(ctx, bytecode, bytecode_size, JS_READ_OBJ_BYTECODE);
75 if (JS_IsException(obj)) {
76 js_free(ctx, bytecode);
77 return 0;
78 }
79 nbinterrupts = 0;
80 /* this is based on
81 * js_std_eval_binary(ctx, bytecode, bytecode_size, 0);
82 * modified so as not to exit on JS exception
83 */
84 JSValue val;
85 if (JS_VALUE_GET_TAG(obj) == JS_TAG_MODULE) {
86 if (JS_ResolveModule(ctx, obj) < 0) {
87 JS_FreeValue(ctx, obj);
88 js_free(ctx, bytecode);
89 return 0;
90 }
91 js_module_set_import_meta(ctx, obj, FALSE, TRUE);
92 }
93 val = JS_EvalFunction(ctx, obj);
94 if (JS_IsException(val)) {
95 js_std_dump_error(ctx);
96 } else {
97 js_std_loop(ctx);
98 }
99 JS_FreeValue(ctx, val);
100 js_free(ctx, bytecode);
101 }
102
103 return 0;
104 }
105