Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Initial mJS support #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file be added?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speeds up build time with docker, as we don't need anything in docker context.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ v8: check_folder build_base
v8 \
./fluff/docker/scripts/build_v8.sh

mjs: check_folder build_base
@echo "[*] Building mjs images"
docker build -q \
-t mjs \
--build-arg ver=$(MJS_VERSION) \
-f docker/mjs.Dockerfile .
docker run \
--rm \
-v `pwd`:/home/build/fluff \
mjs \
./fluff/docker/scripts/build_mjs.sh

2 changes: 2 additions & 0 deletions Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ JERRYSCRIPT_VERSION="latest"
DUKTAPE_URL="https://duktape.org/duktape-2.3.0.tar.xz"
# V8 offical git url: https://github.com/v8/v8
V8_VERSION="latest"
# mjs official git repository https://github.com/cesanta/mjs
MJS_VERSION="latest"
18 changes: 18 additions & 0 deletions docker/mjs.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM base

ARG ver
ENV CC /home/build/afl/afl-2.52b/afl-clang

RUN git clone https://github.com/cesanta/mjs
RUN if [ "latest" != "$ver" ]; then cd mjs && git checkout $ver; fi
RUN ln -s /usr/bin/clang-6.0 /usr/bin/clang
WORKDIR /home/build/mjs/mjs
RUN ${CC} -c -fsanitize=address -lm -std=c99 -Wall -Wextra -pedantic -g -O3 -I. -I.. -Isrc -DMJS_MAIN -DMJS_EXPOSE_PRIVATE -DCS_ENABLE_STDIO -DMJS_ENABLE_DEBUG -I../frozen -DCS_MMAP -DMJS_MODULE_LINES -Wl,--no-as-needed -ldl src/../../common/cs_dbg.c src/../../common/cs_file.c src/../../common/cs_varint.c src/../../common/mbuf.c src/../../common/mg_str.c src/../../common/str_util.c src/../../frozen/frozen.c src/ffi/ffi.c src/mjs_array.c src/mjs_bcode.c src/mjs_builtin.c src/mjs_conversion.c src/mjs_core.c src/mjs_dataview.c src/mjs_exec.c src/mjs_ffi.c src/mjs_gc.c src/mjs_json.c src/mjs_main.c src/mjs_object.c src/mjs_parser.c src/mjs_primitive.c src/mjs_string.c src/mjs_tok.c src/mjs_util.c
RUN ar -rv libmjs.a cs_dbg.o cs_file.o cs_varint.o mbuf.o mg_str.o str_util.o frozen.o ffi.o mjs_array.o mjs_bcode.o mjs_builtin.o mjs_conversion.o mjs_core.o mjs_dataview.o mjs_exec.o mjs_ffi.o mjs_gc.o mjs_json.o mjs_main.o mjs_object.o mjs_parser.o mjs_primitive.o mjs_string.o mjs_tok.o mjs_util.o

WORKDIR /home/build/mjs/mjs/build
RUN cp ../libmjs.a .
WORKDIR /home/build/mjs/mjs/build/mjs
RUN cp /home/build/mjs/mjs.h .
WORKDIR /home/build/
CMD [ "/bin/bash", "-c" ]
16 changes: 16 additions & 0 deletions docker/scripts/build_mjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

echo "[*] Creating build directory"
mkdir /home/build/fluff/build
cd /home/build/fluff/src
echo "[*] Building fluff_mjs"
bazel build --show_result=0 --noshow_progress --cxxopt='-std=c++17' --cxxopt='-fsanitize=address' --test_output=errors --action_env="GTEST_COLOR=1" :fluff_mjs
if [ $? -ne 0 ]; then exit 1; fi
echo "[*] Building fluff_mjs_dry"
bazel build --show_result=0 --noshow_progress --cxxopt='-std=c++17' --cxxopt='-fsanitize=address' --test_output=errors --action_env="GTEST_COLOR=1" :fluff_mjs_dry
if [ $? -ne 0 ]; then exit 1; fi
cp /home/build/fluff/src/bazel-bin/fluff_mjs /home/build/fluff/build
cp /home/build/fluff/src/bazel-bin/fluff_mjs_dry /home/build/fluff/build
cp -R /home/build/fluff/src/grammars/js_grammar_mjs.yaml /home/build/fluff/build
echo "[!] Build successfull, all files available in build directory"
tree /home/build/fluff/build
38 changes: 38 additions & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,41 @@ cc_binary(
],
defines = ["FUZZ_JERRYSCRIPT"]
)

cc_binary(
name = "fluff_mjs",
srcs = ["fluff.cc"],
linkopts = [
"-lyaml-cpp",
"-fsanitize=address",
],
deps = [
"//utils:identifier_register",
"//statements:instruction",
"//interfaces:builtins",
"//interfaces:javascript_interface_mjs",
"//utils:parser",
"//utils:reader_file",
"//utils:status"
],
defines = ["FUZZ_MJS"]
)

cc_binary(
name = "fluff_mjs_dry",
srcs = ["fluff.cc"],
linkopts = [
"-lyaml-cpp",
"-fsanitize=address",
],
deps = [
"//utils:identifier_register",
"//statements:instruction",
"//interfaces:builtins",
"//interfaces:javascript_interface_dryrun",
"//utils:parser",
"//utils:reader_file",
"//utils:status"
],
defines = ["FUZZ_MJS"]
)
6 changes: 6 additions & 0 deletions src/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ new_local_repository(
build_file = "v8.BUILD",
path = "/home/build/v8/",
)

new_local_repository(
name = "mjs",
build_file = "mjs.BUILD",
path = "/home/build/mjs/mjs/build"
)
66 changes: 66 additions & 0 deletions src/grammars/js_grammar_mjs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# mjs has a stripped down version of JS
# for more info check https://github.com/cesanta/mjs
0:
0: LoopWhile
1: LoopFor
2: ConditionalIf
3: ConditionalIfElse
4: Return
5: Break
6: Continue
7: LetDeclaration
1:
0: Addition
1: Subtraction
2: Multiplication
3: Division
4:
0: Integer
1: Real
2: Boolean
3: String
4: SimpleArray
5: ENull
5: Variable
6: LeftShift
7: RightShiftSigned
8: RightShiftUnsigned
9: BitwiseOr
10: BitwiseXor
11: BitwiseAnd
12: LogicalOr
13: LogicalNot
14: LogicalAnd
15:
0: SimpleAssign
1: AddAssign
2: SubAssign
3: MulAssign
4: DivAssign
5: ModAssign
6: LeftShiftAssign
7: RightShiftUnsignedAssign
8: RightShiftSignedAssign
9: BitwiseAndAssign
10: BitwiseOrAssign
11: BitwiseXorAssign
16: FunctionApplication
17: MethodApplication
18: ClassFieldDotAccess
19: ClassFieldBracketAccess
20: PreIncrement
21: PostIncrement
22: PreDecrement
23: PostDecrement
24: FunctionDefinition
25: ClassDefinition
26:
0: Greater
1: Smaller
2: GreaterEqual
3: SmallerEqual
4: Equal
5: NotEqual
6: EqualType
7: NotEqualType
27: ShortIf
13 changes: 12 additions & 1 deletion src/interfaces/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,20 @@ cc_library(
]
)

cc_library(
name = "javascript_interface_mjs",
srcs = ["javascript_interface_mjs.cc"],
hdrs = ["javascript_interface.h"],
deps = [
"builtins",
"//statements:instruction",
"@mjs//:mjs_lib"
]
)

cc_library(
name = "builtins",
hdrs = ["builtins.h", "builtins_chakra.h", "builtins_njs.h",
"builtins_duktape.h", "builtins_espruino.h", "builtins_jerryscript.h",
"builtins_spidermonkey.h", "builtins_v8.h"]
"builtins_spidermonkey.h", "builtins_v8.h", "builtins_mjs.h"]
)
4 changes: 4 additions & 0 deletions src/interfaces/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
#ifdef FUZZ_V8
#include "builtins_v8.h"
#endif // ifdef FUZZ_V8

#ifdef FUZZ_MJS
#include "builtins_mjs.h"
#endif // ifdef FUZZ_MJS
33 changes: 33 additions & 0 deletions src/interfaces/builtins_mjs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef BUILTINS_H_
#define BUILTINS_H_
#include <string>
namespace builtins {
const std::string functions[] = {
"load",
"print",
"ffi",
"ffi_cb_free",
"mkstr",
"getMJS",
"die",
"gc",
"chr",
"s2o",
};
const std::string methods[] = {
"stringify",
"parse",
"create",
"isNaN",
"at",
"length",
"indexOf",
"slice",
"splice",
"push",
"apply"
};
const std::string variables[] = {"global", "Object", "JSON"};
}; // namespace builtins

#endif // ifndef BUILTINS_H_
25 changes: 25 additions & 0 deletions src/interfaces/javascript_interface_mjs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <string>
#include "mjs/mjs.h"
#include <cstdio>
#include <cstring>

#include "javascript_interface.h"

using std::string;
using std::unique_ptr;
using std::vector;

static struct mjs *mjs;

void JavascriptInterface::Init(const char* exeuction_path) {
mjs = mjs_create();
}

void JavascriptInterface::Execute(
const vector<unique_ptr<Instruction>>& instructions) {
string code;
for (const auto& instr : instructions) {
code += instr->Emit() + ";\n";
}
mjs_exec(mjs, code.c_str(), NULL);
}
6 changes: 6 additions & 0 deletions src/mjs.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cc_import(
name = "mjs_lib",
static_library = "libmjs.a",
hdrs = glob(["mjs/*.h"]),
visibility = ["//visibility:public"],
)