From fdc0233125867be36594e083f2112ba7e6f68cdf Mon Sep 17 00:00:00 2001 From: xjd Date: Wed, 8 Jan 2025 16:01:02 +0800 Subject: [PATCH] Fix uncaught exception returning success (#29) * fix uncaught exception return success, see bellard/quickjs#232 * fix bug in eval_buf: bellard/quickjs#379 * cleanup test cases --- src/qjs.c | 19 ++++- tests/ckb_js_tests/Makefile | 54 ++++++------- tests/ckb_js_tests/src/bin/module.rs | 9 --- tests/ckb_js_tests/templates/module.json | 75 ------------------- .../test_data/fs_module/fib_module.js | 10 --- .../ckb_js_tests/test_data/fs_module/index.js | 6 -- .../test_data/fs_module_mount/index.js | 3 +- .../test_data/fs_module_mount/init.js | 2 + tests/ckb_js_tests/test_data/syscall.js | 1 + tools/fs-packer/dist/index.js | 8 +- tools/fs-packer/src/index.ts | 6 +- 11 files changed, 60 insertions(+), 133 deletions(-) delete mode 100644 tests/ckb_js_tests/src/bin/module.rs delete mode 100644 tests/ckb_js_tests/templates/module.json delete mode 100644 tests/ckb_js_tests/test_data/fs_module/fib_module.js delete mode 100644 tests/ckb_js_tests/test_data/fs_module/index.js diff --git a/src/qjs.c b/src/qjs.c index 9880951..2b5e7ed 100644 --- a/src/qjs.c +++ b/src/qjs.c @@ -163,7 +163,24 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *fi js_std_dump_error(ctx); ret = -1; } else { - ret = 0; + if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) { + int promise_state = JS_PromiseState(ctx, val); + if (promise_state == JS_PROMISE_REJECTED) { + printf("JS_Eval return a rejected promise"); + JSValue result = JS_PromiseResult(ctx, val); + const char *str = JS_ToCString(ctx, result); + if (str) { + printf("This promise returns: %s", str); + JS_FreeCString(ctx, str); + } + JS_FreeValue(ctx, result); + ret = -2; + } else { + ret = 0; + } + } else { + ret = 0; + } } JS_FreeValue(ctx, val); return ret; diff --git a/tests/ckb_js_tests/Makefile b/tests/ckb_js_tests/Makefile index 704643e..b6f502d 100644 --- a/tests/ckb_js_tests/Makefile +++ b/tests/ckb_js_tests/Makefile @@ -2,53 +2,53 @@ CKB_DEBUGGER ?= ckb-debugger MAX_CYCLES ?= 2000000000 ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) BIN_PATH = $(ROOT_DIR)/../../build/ckb-js-vm +FS_PACKER = node $(ROOT_DIR)/../../tools/fs-packer/dist/index.js -all: \ +BYTECODE_DIR = $(ROOT_DIR)/../../build/bytecode +JS_SOURCE_DIR = $(ROOT_DIR)/test_data/fs_module_mount +BYTECODE_FILES = $(BYTECODE_DIR)/index.bc $(BYTECODE_DIR)/fib_module.bc + +define compile_js_to_bc + $(CKB_DEBUGGER) --read-file $(1) --bin $(BIN_PATH) -- -c | \ + awk -f $(ROOT_DIR)/../../tools/compile.awk | \ + xxd -r -p > $(2) +endef + +all: out \ cargo_test \ - file_system \ syscall \ - fs_bytecode \ simple_udt \ fs_mount \ - module + fs_bytecode \ + cell_target + +out: + @mkdir -p $(ROOT_DIR)/../../build/bytecode cargo_test: cargo test -spawn_caller: - clang-18 --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs \ - -I ../../deps/ckb-c-stdlib/libc -I ../../deps/ckb-c-stdlib \ - -nostdinc -nostdlib -o ../../build/bytecode/spawn_caller test_data/spawn_caller.c - -module: spawn_caller - cargo run --bin module | ${CKB_DEBUGGER} --tx-file=- -s lock - -build/bytecode/fs_modules.fs: test_data/fs_module/index.js test_data/fs_module/fib_module.js - cd test_data/fs_module && node ../../../../tools/fs-packer/dist/index.js pack ../../../../$@ index.js fib_module.js - fs_bytecode: - $(CKB_DEBUGGER) --read-file test_data/fs_module/index.js --bin $(BIN_PATH) -- -c | awk -f $(ROOT_DIR)/../../tools/compile.awk | xxd -r -p > ../../build/bytecode/index.bc - $(CKB_DEBUGGER) --read-file test_data/fs_module/fib_module.js --bin $(BIN_PATH) -- -c | awk -f $(ROOT_DIR)/../../tools/compile.awk | xxd -r -p > ../../build/bytecode/fib_module.bc - cd ../../build/bytecode && node ../../tools/fs-packer/dist/index.js pack ../../build/bytecode/fs_modules_bc.fs index.bc fib_module.bc - $(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) --read-file ../../build/bytecode/fs_modules_bc.fs --bin $(BIN_PATH) -- -f -r 2>&1 | fgrep 'Run result: 0' - -file_system: build/bytecode/fs_modules.fs - cargo run --bin default_by_cell | $(CKB_DEBUGGER) -s lock --tx-file=- --read-file ../../$^ -- -f -r 2>&1 | fgrep 'Run result: 0' + $(call compile_js_to_bc,$(JS_SOURCE_DIR)/index.js,$(BYTECODE_DIR)/index.bc) + $(call compile_js_to_bc,$(JS_SOURCE_DIR)/fib_module.js,$(BYTECODE_DIR)/fib_module.bc) + cd $(BYTECODE_DIR) && $(FS_PACKER) pack fs_modules_bc.fs $(notdir $(BYTECODE_FILES)) + $(CKB_DEBUGGER) --max-cycles $(MAX_CYCLES) \ + --read-file $(BYTECODE_DIR)/fs_modules_bc.fs \ + --bin $(BIN_PATH) -- -f -r 2>&1 | \ + fgrep 'Run result: 0' syscall: cargo run --bin syscall | $(CKB_DEBUGGER) --tx-file=- -s lock fs_mount: - cd test_data/fs_module_mount && node ../../../../tools/fs-packer/dist/index.js pack ../../../../build/bytecode/fib_module.fs fib_module.js - cd test_data/fs_module_mount && node ../../../../tools/fs-packer/dist/index.js pack ../../../../build/bytecode/fib_module_mount.fs index.js init.js - + cd test_data/fs_module_mount && $(FS_PACKER) pack ../../../../build/bytecode/fib_module.fs fib_module.js + cd test_data/fs_module_mount && $(FS_PACKER) pack ../../../../build/bytecode/fib_module_mount.fs index.js init.js cargo run --bin module_mount | ${CKB_DEBUGGER} --tx-file=- -s lock simple_udt: cargo run --bin simple_udt | $(CKB_DEBUGGER) --tx-file=- --script-group-type type --cell-type output --cell-index 0 cell_target: - echo "blake2b(../../basic/test_loop.js) == 91bc1d1fc8c19289e72d15004dd506b76246bfead13391a5b35179cf7c8f37ef" cargo run --bin default_by_cell | $(CKB_DEBUGGER) -s lock --tx-file=- -- -t 91bc1d1fc8c19289e72d15004dd506b76246bfead13391a5b35179cf7c8f37ef01 install-lua: @@ -56,4 +56,4 @@ install-lua: clean: cargo clean - rm -rf ../../build/testdata_fs_modules.bin + rm -rf ../../build/bytecode diff --git a/tests/ckb_js_tests/src/bin/module.rs b/tests/ckb_js_tests/src/bin/module.rs deleted file mode 100644 index 398a692..0000000 --- a/tests/ckb_js_tests/src/bin/module.rs +++ /dev/null @@ -1,9 +0,0 @@ -use ckb_js_tests::read_tx_template; - -pub fn main() -> Result<(), Box> { - let tx = read_tx_template("templates/module.json")?; - - let json = serde_json::to_string_pretty(&tx).unwrap(); - println!("{}", json); - Ok(()) -} diff --git a/tests/ckb_js_tests/templates/module.json b/tests/ckb_js_tests/templates/module.json deleted file mode 100644 index 8fe1100..0000000 --- a/tests/ckb_js_tests/templates/module.json +++ /dev/null @@ -1,75 +0,0 @@ -{ - "mock_info": { - "inputs": [ - { - "output": { - "capacity": "0x10000000", - "lock": { - "args": "0x0000{{ ref_type js-code-file }}01", - "code_hash": "0x{{ ref_type spawn_caller }}", - "hash_type": "type" - }, - "type": null - }, - "data": "0x" - } - ], - "cell_deps": [ - { - "output": { - "capacity": "0x10000000", - "lock": { - "args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", - "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "hash_type": "data1" - }, - "type": "{{ def_type spawn_caller }}" - }, - "data": "0x{{ data ../../../build/bytecode/spawn_caller }}" - }, - { - "output": { - "capacity": "0x10000000", - "lock": { - "args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", - "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "hash_type": "data1" - }, - "type": "{{ def_type ckb-js-vm }}" - }, - "data": "0x{{ data ../../../build/ckb-js-vm }}" - }, - { - "output": { - "capacity": "0x10000000", - "lock": { - "args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", - "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "hash_type": "data1" - }, - "type": "{{ def_type js-code-file }}" - }, - "data": "0x{{ data ../../../build/bytecode/fs_modules_bc.fs }}" - } - ], - "header_deps": [] - }, - "tx": { - "outputs": [ - { - "capacity": "0x0", - "lock": { - "args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", - "code_hash": "0x{{ ref_type spawn_caller }}", - "hash_type": "type" - } - } - ], - "witnesses": [ - "0x55000000100000005500000055000000410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - ], - "outputs_data": [ - "0x" - ] - } -} diff --git a/tests/ckb_js_tests/test_data/fs_module/fib_module.js b/tests/ckb_js_tests/test_data/fs_module/fib_module.js deleted file mode 100644 index 6a81071..0000000 --- a/tests/ckb_js_tests/test_data/fs_module/fib_module.js +++ /dev/null @@ -1,10 +0,0 @@ -/* fib module */ -export function fib(n) -{ - if (n <= 0) - return 0; - else if (n == 1) - return 1; - else - return fib(n - 1) + fib(n - 2); -} diff --git a/tests/ckb_js_tests/test_data/fs_module/index.js b/tests/ckb_js_tests/test_data/fs_module/index.js deleted file mode 100644 index b0bf624..0000000 --- a/tests/ckb_js_tests/test_data/fs_module/index.js +++ /dev/null @@ -1,6 +0,0 @@ -/* example of JS module */ - -import { fib } from "./fib_module.js"; - -console.log("Hello World"); -console.log(`fib(10)=${fib(10)}`); diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/index.js b/tests/ckb_js_tests/test_data/fs_module_mount/index.js index 2347fb1..e63bfa3 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/index.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/index.js @@ -1,3 +1,4 @@ /* example of JS module */ import * as module from './fib_module.js'; -console.log(`fib(10)=${module.fib(10)}`); + +console.log(`fib(10)=${module.fib(10)}!`); diff --git a/tests/ckb_js_tests/test_data/fs_module_mount/init.js b/tests/ckb_js_tests/test_data/fs_module_mount/init.js index 4028f5c..eb56cd6 100644 --- a/tests/ckb_js_tests/test_data/fs_module_mount/init.js +++ b/tests/ckb_js_tests/test_data/fs_module_mount/init.js @@ -1,2 +1,4 @@ import * as ckb from "ckb"; ckb.mount(2, ckb.SOURCE_CELL_DEP) + +console.log("init.js"); \ No newline at end of file diff --git a/tests/ckb_js_tests/test_data/syscall.js b/tests/ckb_js_tests/test_data/syscall.js index b078425..b491d91 100644 --- a/tests/ckb_js_tests/test_data/syscall.js +++ b/tests/ckb_js_tests/test_data/syscall.js @@ -112,6 +112,7 @@ function test_misc() { function test_spawn() { console.log('test_spawn ...'); const js_code = ` + import * as ckb from 'ckb'; let fds = ckb.inherited_fds(); ckb.write(fds[0], new Uint8Array([0, 1, 2, 3])); ckb.close(fds[0]); diff --git a/tools/fs-packer/dist/index.js b/tools/fs-packer/dist/index.js index 7fc0fc2..9dab6b2 100644 --- a/tools/fs-packer/dist/index.js +++ b/tools/fs-packer/dist/index.js @@ -87,6 +87,9 @@ function appendFileToStream(filePath, stream) { content = _a.sent(); return [4 /*yield*/, writeToFile(content, stream)]; case 2: + _a.sent(); + return [4 /*yield*/, writeToFile(Buffer.from([0]), stream)]; + case 3: _a.sent(); return [2 /*return*/]; } @@ -145,11 +148,12 @@ function pack(files, outputStream) { return [4 /*yield*/, appendIntegerToStream(offset, outputStream)]; case 3: _f.sent(); - length = Buffer.byteLength(name_1) + 1; + length = Buffer.byteLength(name_1); return [4 /*yield*/, appendIntegerToStream(length, outputStream)]; case 4: _f.sent(); offset += length; + offset += 1; // add trailing zero return [4 /*yield*/, appendIntegerToStream(offset, outputStream)]; case 5: _f.sent(); @@ -160,6 +164,7 @@ function pack(files, outputStream) { case 7: _f.sent(); offset += length; + offset += 1; // add trailing zero _f.label = 8; case 8: _i++; @@ -208,7 +213,6 @@ function unpack(directory, fileContent) { var value = fileContent .toString("utf8", position, position + length) .replace(/\0$/, ""); - position += length; return value; } function copyToFile(directory, filename, offset, length) { diff --git a/tools/fs-packer/src/index.ts b/tools/fs-packer/src/index.ts index 2250b90..8b1d9ae 100644 --- a/tools/fs-packer/src/index.ts +++ b/tools/fs-packer/src/index.ts @@ -39,6 +39,7 @@ async function appendFileToStream( ): Promise { const content = await fs.readFile(filePath); await writeToFile(content, stream); + await writeToFile(Buffer.from([0]), stream); } async function appendStringNullToStream( @@ -72,13 +73,15 @@ async function pack( for (const [name, filePath] of Object.entries(files)) { console.log(`packing file ${filePath} to ${name}`); await appendIntegerToStream(offset, outputStream); - length = Buffer.byteLength(name) + 1; + length = Buffer.byteLength(name); await appendIntegerToStream(length, outputStream); offset += length; + offset += 1; // add trailing zero await appendIntegerToStream(offset, outputStream); length = await getFileSize(filePath); await appendIntegerToStream(length, outputStream); offset += length; + offset += 1; // add trailing zero } // Write actual file data @@ -105,7 +108,6 @@ async function unpack(directory: string, fileContent: Buffer): Promise { const value = fileContent .toString("utf8", position, position + length) .replace(/\0$/, ""); - position += length; return value; }