Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add the missing fuzz_common.c #292

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}