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

Use QuickJS-NG #59

Merged
merged 13 commits into from
Oct 17, 2024
Merged
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
11 changes: 6 additions & 5 deletions .ci-scripts/ci-build-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ rm -rf "rizin-*"
cd "$CI_JSDEC"

# build jsdec and install in the rizin dir.
meson setup --buildtype=release -Dbuild_type=rizin build
sudo ninja -C build install
meson setup --buildtype=release -Dbuild_type=rizin --prefix=~/.local build
sudo ninja -C build install || sleep 0

# check if it was installed correctly and try to run it.
HAS_JSDEC=$(rizin -Qc "Lc" | grep jsdec)
HAS_JSDEC=$(rizin -qc "Lc" | grep jsdec)
if [ -z "$HAS_JSDEC" ]; then
echo "rizin failed to load jsdec."
rizin -e log.level=2 -Qc "Lc" | grep jsdec || sleep 0
rizin -e log.level=0 -qc "Lc"
rizin -hh
exit 1
fi

OUTPUT=$(rizin -Qc 'af ; pdd' /bin/ls)
OUTPUT=$(rizin -qc 'af ; pdd' /bin/ls)
CHECK=$(echo -e "$OUTPUT" | grep "jsdec pseudo code output")
echo -e "$OUTPUT"
if [ -z "$CHECK" ]; then
Expand Down
4 changes: 2 additions & 2 deletions .ci-scripts/ci-build-win.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ $env:LDFLAGS = "-LC:$env:HOMEPATH\AppData\Local\Programs\rizin\lib"

Invoke-NativeCommand meson setup --buildtype=release --prefix="$rizin_path" build
Invoke-NativeCommand ninja -C build install
rizin.exe -e log.level=2 -Qc "Lc"
rizin.exe -Qc "af ; pdd" "C:\Windows\System32\calc.exe"
rizin.exe -e log.level=2 -qc "Lc"
rizin.exe -qc "af ; pdd" "C:\Windows\System32\calc.exe"
2 changes: 1 addition & 1 deletion c/jsdec-plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ RzCorePlugin rz_core_plugin_jsdec = {
#ifdef _MSC_VER
#define _RZ_API __declspec(dllexport)
#else
#define _RZ_API
#define _RZ_API __attribute__((visibility("default")))
#endif

#ifndef CORELIB
Expand Down
18 changes: 7 additions & 11 deletions c/jsdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ static void print_jsval(JSValue val) {
errorf("JS_IsNumber\n");
} else if(JS_IsBigInt(NULL, val)) {
errorf("JS_IsBigInt\n");
} else if(JS_IsBigFloat(val)) {
errorf("JS_IsBigFloat\n");
} else if(JS_IsBigDecimal(val)) {
errorf("JS_IsBigDecimal\n");
} else if(JS_IsBool(val)) {
errorf("JS_IsBool\n");
} else if(JS_IsNull(val)) {
Expand Down Expand Up @@ -104,8 +100,10 @@ static int js_load_module(JSContext *ctx, const uint8_t *bytes, const uint32_t s
return 0;
}

//print_jsval(obj);
JSValue val = JS_EvalFunction(ctx, obj);
if (JS_IsException(val)) {
errorf("Error: jsdec thrown an exception when loading a module\n");
jsdec_handle_exception(ctx);
return 0;
}
Expand Down Expand Up @@ -194,17 +192,14 @@ jsdec_t *jsdec_new() {

// initialize all intrisic
JS_AddIntrinsicBaseObjects(ctx);
JS_AddIntrinsicDate(ctx);
JS_AddIntrinsicEval(ctx);
JS_AddIntrinsicRegExpCompiler(ctx);
JS_AddIntrinsicRegExp(ctx);
JS_AddIntrinsicJSON(ctx);
JS_AddIntrinsicMapSet(ctx);
JS_AddIntrinsicTypedArrays(ctx);
JS_AddIntrinsicPromise(ctx);
JS_AddIntrinsicBigInt(ctx);
JS_AddIntrinsicBigFloat(ctx);
JS_AddIntrinsicBigDecimal(ctx);
JS_AddIntrinsicOperators(ctx);
JS_AddIntrinsicEval(ctx);
JS_EnableBignumExt(ctx, 1);


// Setup global objects.
JSValue global = JS_GetGlobalObject(ctx);
Expand All @@ -223,6 +218,7 @@ jsdec_t *jsdec_new() {
JS_FreeValue(ctx, global);

if (!js_load_all_modules(ctx)) {
errorf("Error: failed to load modules\n");
JS_FreeContext(ctx);
JS_FreeRuntime(rt);
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions js/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ if get_option('build_type') == 'standalone'
build_always_stale: true,
output : 'bytecode.h',
input : jsdec_testsuite,
command : [qjsc, '-m', '-N', 'main_bytecode', '-c', '-o', '@OUTPUT@', '@INPUT@'],
command : [qjsc, '-m', '-N', 'main_bytecode', '-o', '@OUTPUT@', '@INPUT@'],
)
else
bytecode_h = custom_target(
'bytecode.h',
build_always_stale: true,
output : 'bytecode.h',
input : jsdec_plugin,
command : [qjsc, '-m', '-N', 'main_bytecode', '-c', '-o', '@OUTPUT@', '@INPUT@'],
command : [qjsc, '-m', '-N', 'main_bytecode', '-o', '@OUTPUT@', '@INPUT@'],
)
endif

Expand Down
15 changes: 14 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# SPDX-FileCopyrightText: 2023-2024 Giovanni Dante Grazioli <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

project('jsdec', 'c', meson_version: '>=0.56.0')
project('jsdec', 'c', meson_version: '>=0.56.0',
default_options: [
'buildtype=release',
'b_vscrt=from_buildtype',
'warning_level=1',
'c_std=c11'
])

cc = meson.get_compiler('c')

Expand Down Expand Up @@ -86,6 +92,13 @@ elif build_type == 'rizin'
'c' / 'jsdec-plugin.c'
]

if cc.has_argument('-fvisibility=hidden')
jsdec_c_args += '-fvisibility=hidden'
endif
if cc.has_argument('-flto')
jsdec_c_args += '-flto'
endif

shared_library('core_pdd', jsdec_src,
c_args : jsdec_c_args,
dependencies: jsdec_deps,
Expand Down
5 changes: 3 additions & 2 deletions subprojects/libquickjs.wrap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[wrap-git]
url = https://github.com/frida/quickjs.git
revision = c81f05c9859cea5f83a80057416a0c7affe9b876
url = https://github.com/quickjs-ng/quickjs.git
revision = v0.6.1
directory = libquickjs
patch_directory = libquickjs
depth = 1
116 changes: 55 additions & 61 deletions subprojects/packagefiles/libquickjs/meson.build
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
project('quickjs', 'c',
version: '2021-03-27-frida',
project('quickjs-ng', 'c',
version: 'v0.6.1',
default_options: [
'buildtype=release',
'b_vscrt=from_buildtype',
'warning_level=1',
'c_std=c11'
]
)

cc = meson.get_compiler('c')

have_msvc = cc.get_id() == 'msvc'

threads_dep = dependency('threads')
dl_dep = cc.find_library('dl', required: false)
m_dep = cc.find_library('m', required: false)
Expand All @@ -15,85 +19,75 @@ headers = [
]

sources = [
'quickjs.c',
'libregexp.c',
'libunicode.c',
'cutils.c',
'libbf.c',
'libregexp.c',
'libunicode.c',
'quickjs.c',
]

cdata = configuration_data()
cdata.set_quoted('CONFIG_VERSION', meson.project_version())

if host_machine.endian() == 'big'
cdata.set('WORDS_BIGENDIAN', 1)
endif

if cc.has_header('unistd.h')
cdata.set('HAVE_UNISTD_H', 1)
endif

if cc.has_function('malloc_usable_size')
cdata.set('HAVE_MALLOC_USABLE_SIZE', 1)
endif
compiler_flags = [
'-fvisibility=hidden',
'-Wno-implicit-fallthrough',
'-Wno-sign-compare',
'-Wno-missing-field-initializers',
'-Wno-unused-parameter',
'-Wno-unused-but-set-variable',
'-Wno-array-bounds',
'-Wno-format-truncation',
'-Wno-format-zero-length',
'-funsigned-char',
]

if get_option('libc')
headers += 'quickjs-libc.h'
sources += 'quickjs-libc.c'
if cc.get_id() == 'msvc'
compiler_flags += '-Wno-unsafe-buffer-usage'
compiler_flags += '-Wno-sign-conversion'
compiler_flags += '-Wno-nonportable-system-include-path'
compiler_flags += '-Wno-implicit-int-conversion'
compiler_flags += '-Wno-shorten-64-to-32'
compiler_flags += '-Wno-reserved-macro-identifier'
compiler_flags += '-Wno-reserved-identifier'
compiler_flags += '-Wdeprecated-declarations'
compiler_flags += '/experimental:c11atomics'
compiler_flags += '/wd4018' # -Wno-sign-conversion
compiler_flags += '/wd4061' # -Wno-implicit-fallthrough
compiler_flags += '/wd4100' # -Wno-unused-parameter
compiler_flags += '/wd4200' # -Wno-zero-length-array
compiler_flags += '/wd4242' # -Wno-shorten-64-to-32
compiler_flags += '/wd4244' # -Wno-shorten-64-to-32
compiler_flags += '/wd4245' # -Wno-sign-compare
compiler_flags += '/wd4267' # -Wno-shorten-64-to-32
compiler_flags += '/wd4388' # -Wno-sign-compare
compiler_flags += '/wd4389' # -Wno-sign-compare
compiler_flags += '/wd4710' # Function not inlined
compiler_flags += '/wd4711' # Function was inlined
compiler_flags += '/wd4820' # Padding added after construct
compiler_flags += '/wd4996' # -Wdeprecated-declarations
compiler_flags += '/wd5045' # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
endif

cdata.set('CONFIG_BIGNUM', get_option('bignum'))

atomics = false
opt = get_option('atomics')
if not opt.disabled()
if opt.auto()
atomics = not have_msvc
elif not have_msvc
atomics = true
else
error('Atomics support was requested but is not available')
endif
endif
cdata.set('CONFIG_ATOMICS', atomics)
# required on linux, but ignored by windows
add_project_arguments('-D_GNU_SOURCE=1', language: 'c')

stack_check = false
opt = get_option('stack_check')
if not opt.disabled()
if opt.auto()
stack_check = not have_msvc
elif not have_msvc
stack_check = true
else
error('Stack limitation was requested but is not available')
foreach flag: compiler_flags
if cc.has_argument(flag)
add_project_arguments(flag, language: 'c')
endif
endif
cdata.set('CONFIG_STACK_CHECK', stack_check)

configure_file(
output: 'config.h',
configuration: cdata,
)

add_project_arguments(
have_msvc ? '/FI' : '-include', meson.current_build_dir() / 'config.h',
'-D_GNU_SOURCE=1',
language: 'c',
)
endforeach

quickjs = static_library('quickjs', sources,
dependencies: [threads_dep, dl_dep, m_dep],
implicit_include_directories: false,
install: false,
)

quickjs_dep = declare_dependency(
link_with: quickjs,
include_directories: include_directories('.'),
)

qjsc_sources = [
'qjsc.c',
'quickjs-libc.c',
]

qjsc = executable('qjsc', qjsc_sources,
Expand Down
Loading