From fd24e206f24d28e86dc2d4a1ec8d7f748f7da574 Mon Sep 17 00:00:00 2001 From: Alexander Rodin Date: Fri, 9 Aug 2019 20:39:46 +0000 Subject: [PATCH 1/3] Add "patched" feature with "stack-overflow-signed.patch" This commit adds a new "patched" feature to both the bindings and the main crate. This feature applies patches to the embedded version that fix up/extend quickjs. All patches are in libquickjs-sys/embed/patches. --- Cargo.toml | 3 ++ README.md | 6 ++++ libquickjs-sys/Cargo.toml | 1 + libquickjs-sys/build.rs | 29 +++++++++++++++++++ .../embed/patches/stack-overflow-signed.patch | 15 ++++++++++ 5 files changed, 54 insertions(+) create mode 100644 libquickjs-sys/embed/patches/stack-overflow-signed.patch diff --git a/Cargo.toml b/Cargo.toml index e52a2a7..c39ecfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,6 @@ libquickjs-sys = { version = "0.3.0", path = "./libquickjs-sys" } members = [ "libquickjs-sys", ] + +[features] +patched = ["libquickjs-sys/patched"] diff --git a/README.md b/README.md index 427a460..a88fe55 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,12 @@ If you would like to use a system version instead, see below. QuickJS will always be statically linked to your binary. +### Features + +The crate supports the following features: + +* `patched` applies QuickJS patches that can be found in `libquickjs-sys/embed/patches` directory. + ### System installation To use the system installation, without the bundled feature, first install the required diff --git a/libquickjs-sys/Cargo.toml b/libquickjs-sys/Cargo.toml index 953e75c..5167bfb 100644 --- a/libquickjs-sys/Cargo.toml +++ b/libquickjs-sys/Cargo.toml @@ -20,6 +20,7 @@ copy_dir = { version = "0.1.2", optional = true } [features] bundled = ["copy_dir"] +patched = [] default = ["bundled"] system = ["bindgen"] diff --git a/libquickjs-sys/build.rs b/libquickjs-sys/build.rs index f2785a4..34501f5 100644 --- a/libquickjs-sys/build.rs +++ b/libquickjs-sys/build.rs @@ -19,6 +19,9 @@ fn main() { #[cfg(not(feature = "bindgen"))] panic!("Invalid configuration for libquickjs-sys: Must either enable the bundled or the bindgen feature"); + #[cfg(feature = "patched")] + panic!("Invalid configuration for libquickjs-sys: the patched feature is incompatible with the system feature"); + let lib = if cfg!(unix) { if exists("/usr/lib/quickjs/libquickjs.a") { "/usr/lib/quickjs" @@ -58,6 +61,9 @@ fn main() { } copy_dir::copy_dir("./embed/quickjs", &code_dir).expect("Could not copy quickjs directory"); + #[cfg(feature = "patched")] + apply_patches(&code_dir); + eprintln!("Compiling quickjs..."); std::process::Command::new("make") .arg("libquickjs.a") @@ -77,3 +83,26 @@ fn main() { ); println!("cargo:rustc-link-lib=static=quickjs"); } + +#[cfg(feature = "patched")] +fn apply_patches(code_dir: &PathBuf) { + use std::fs; + + eprintln!("Applying patches..."); + for patch in fs::read_dir("./embed/patches").expect("Could not open patches directory") { + let patch = patch.expect("Could not open patch"); + eprintln!("Applying {:?}...", patch.file_name()); + let status = std::process::Command::new("patch") + .current_dir(&code_dir) + .arg("-i") + .arg(fs::canonicalize(patch.path()).expect("Cannot canonicalize patch path")) + .spawn() + .expect("Could not apply patches") + .wait() + .expect("Could not apply patches"); + assert!( + status.success(), + "Patch command returned non-zero exit code" + ); + } +} diff --git a/libquickjs-sys/embed/patches/stack-overflow-signed.patch b/libquickjs-sys/embed/patches/stack-overflow-signed.patch new file mode 100644 index 0000000..c4ce46b --- /dev/null +++ b/libquickjs-sys/embed/patches/stack-overflow-signed.patch @@ -0,0 +1,15 @@ +diff -urN quickjs-2019-07-28/quickjs.c quickjs-2019-07-28-stack-overflow-signed/quickjs.c +--- quickjs-2019-07-28/quickjs.c 2019-07-28 15:03:03.000000000 +0000 ++++ quickjs-2019-07-28-stack-overflow-signed/quickjs.c 2019-08-09 20:00:03.666846091 +0000 +@@ -1732,9 +1732,9 @@ + + static inline BOOL js_check_stack_overflow(JSContext *ctx, size_t alloca_size) + { +- size_t size; ++ ptrdiff_t size; + size = ctx->stack_top - js_get_stack_pointer(); +- return unlikely((size + alloca_size) > ctx->stack_size); ++ return unlikely((size + (ptrdiff_t)alloca_size) > (ptrdiff_t)ctx->stack_size); + } + #endif + From 9afa90d30ab8a6dccf8d1d3cba057b6023291246 Mon Sep 17 00:00:00 2001 From: Alexander Rodin Date: Sun, 11 Aug 2019 16:37:22 +0000 Subject: [PATCH 2/3] ci: Test patched and default features on CI --- .circleci/config.yml | 50 +++++++++++++++++++++++++++++--------------- azure-pipelines.yml | 8 ++++++- justfile | 3 ++- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 010be2c..dac1f69 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,20 +1,36 @@ version: 2.0 -jobs: - build: - docker: - - image: debian - steps: - - checkout - - run: - name: Setup, Build & Test - command: | - export PATH="$HOME/.cargo/bin:$HOME:$PATH" - apt-get update && apt-get install -y curl +shared: &shared + docker: + - image: debian + steps: + - checkout + - run: + name: Setup, Build & Test + command: | + export PATH="$HOME/.cargo/bin:$HOME:$PATH" + + apt-get update && apt-get install -y curl + + echo "Installing Rust..." + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable + echo "Installing just..." + curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git casey/just --target x86_64-unknown-linux-musl --to $HOME + hash -r + just FEATURES="$FEATURES" ci-debian - echo "Installing Rust..." - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable - echo "Installing just..." - curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git casey/just --target x86_64-unknown-linux-musl --to $HOME - hash -r - just ci-debian +jobs: + default: + environment: + FEATURES: '' + <<: *shared + patched: + environment: + FEATURES: 'patched' + <<: *shared + +workflows: + version: 2 + build: + - default + - patched diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 15c7778..be6fdaa 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -23,6 +23,12 @@ jobs: - job: macos_stable displayName: Mac OS Stable + strategy: + matrix: + default: + FEATURES: '' + patched: + FEATURES: 'patched' pool: vmImage: 'macOS-10.14' @@ -37,5 +43,5 @@ jobs: echo "Installing just..." curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git casey/just --to $HOME hash -r - just ci-macos + just FEATURES="$FEATURES" ci-macos displayName: setup and test diff --git a/justfile b/justfile index 7eb3c23..8766072 100644 --- a/justfile +++ b/justfile @@ -1,6 +1,7 @@ embed_dir := "./libquickjs-sys/embed/quickjs" DOWNLOAD_URL := "https://bellard.org/quickjs/quickjs-2019-08-10.tar.xz" +FEATURES := "" download-new: test -d {{embed_dir}} && rm -r {{embed_dir}} || echo "" @@ -25,7 +26,7 @@ ci-debian-setup: ci-test: # Limit test threads to 1 to show test name before execution. - RUST_TEST_THREADS=1 cargo test --verbose + RUST_TEST_THREADS=1 cargo test --verbose --features="{{FEATURES}}" ci-lint: rustup component add rustfmt clippy From a309c90d69094625b5c1070397ff0866d5a4e206 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Mon, 12 Aug 2019 23:52:54 +0200 Subject: [PATCH 3/3] ci: Fix up circleci config for new patched features testing --- .circleci/config.yml | 69 +++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index dac1f69..e7f3923 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,36 +1,47 @@ -version: 2.0 +version: 2.1 -shared: &shared - docker: - - image: debian - steps: - - checkout - - run: - name: Setup, Build & Test - command: | - export PATH="$HOME/.cargo/bin:$HOME:$PATH" +commands: + tests: + description: "Run tests" + parameters: + features: + type: string + steps: + - checkout + - run: + name: Setup + command: | + echo "Installing curl..." + apt-get update && apt-get install -y curl + echo "Installing Rust..." + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable + echo "Installing just..." + curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git casey/just --target x86_64-unknown-linux-musl --to $HOME + - run: + name: Test + command: | + export PATH="$HOME/.cargo/bin:$HOME:$PATH" + just FEATURES="<>" ci-debian - apt-get update && apt-get install -y curl - - echo "Installing Rust..." - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable - echo "Installing just..." - curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git casey/just --target x86_64-unknown-linux-musl --to $HOME - hash -r - just FEATURES="$FEATURES" ci-debian jobs: - default: - environment: - FEATURES: '' - <<: *shared - patched: - environment: - FEATURES: 'patched' - <<: *shared + test-features-default: + docker: + - image: debian + steps: + - tests: + features: "" + test-features-patched: + docker: + - image: debian + steps: + - tests: + features: "patched" workflows: version: 2 - build: - - default - - patched + tests: + jobs: + - test-features-default + - test-features-patched +