Skip to content

Commit

Permalink
Add the missing fuzz_common.c (bellard#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatahodovan committed May 9, 2024
1 parent db9dbd0 commit d53aafe
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions fuzz/fuzz_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Copyright 2020 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <string.h>

#include "fuzz/fuzz_common.h"

// handle timeouts from infinite loops
static int interrupt_handler(JSRuntime *rt, void *opaque)
{
nbinterrupts++;
return (nbinterrupts > 100);
}

void reset_nbinterrupts() {
nbinterrupts = 0;
}

void test_one_input_init(JSRuntime *rt, JSContext *ctx) {
// 64 Mo
JS_SetMemoryLimit(rt, 0x4000000);
// 64 Kb
JS_SetMaxStackSize(rt, 0x10000);

JS_AddIntrinsicBigFloat(ctx);
JS_AddIntrinsicBigDecimal(ctx);
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, 1);
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
JS_SetInterruptHandler(JS_GetRuntime(ctx), interrupt_handler, NULL);
js_std_add_helpers(ctx, 0, NULL);

// Load os and std
js_std_init_handlers(rt);
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
const char *str = "import * as std from 'std';\n"
"import * as os from 'os';\n"
"globalThis.std = std;\n"
"globalThis.os = os;\n";
JSValue std_val = JS_Eval(ctx, str, strlen(str), "<input>", JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
if (!JS_IsException(std_val)) {
js_module_set_import_meta(ctx, std_val, 1, 1);
std_val = JS_EvalFunction(ctx, std_val);
} else {
js_std_dump_error(ctx);
}
std_val = js_std_await(ctx, std_val);
JS_FreeValue(ctx, std_val);
}

0 comments on commit d53aafe

Please sign in to comment.