From 47ac17101f49364daecb411c656f265e5f21a02d Mon Sep 17 00:00:00 2001 From: Georgiy Komarov Date: Thu, 21 Dec 2023 14:00:42 -0300 Subject: [PATCH] feat(linter): Split `ink_linting` into two libraries Split `ink_linting` to `ink_linting_mandatory` and `ink_linting`. Mandatory lints will be integrated in the `cargo-build` build process in https://github.com/paritytech/cargo-contract/pull/1412. Extra lints are optional and could be run by the contract developer to highlight possible issues with secure coding style and to check the compliance with best practices. For more information about this design decision, see: https://github.com/paritytech/cargo-contract/pull/1412#discussion_r1407425483. Closes #2006 --- Cargo.toml | 1 + linting/Cargo.toml | 91 ++----------------- linting/README.md | 13 ++- linting/extra/Cargo.toml | 79 ++++++++++++++++ linting/extra/LICENSE | 1 + linting/extra/README.md | 2 + linting/{ => extra}/src/ink_utils.rs | 0 linting/{ => extra}/src/lib.rs | 3 - linting/{ => extra}/src/primitive_topic.rs | 0 .../{ => extra}/src/storage_never_freed.rs | 0 .../src/strict_balance_equality.rs | 0 .../{ => extra}/ui/fail/primitive_topic.rs | 0 .../ui/fail/primitive_topic.stderr | 0 .../ui/fail/storage_never_freed.rs | 0 .../ui/fail/storage_never_freed.stderr | 0 .../ui/fail/strict_balance_equality.rs | 0 .../ui/fail/strict_balance_equality.stderr | 0 .../{ => extra}/ui/pass/primitive_topic.rs | 0 .../ui/pass/storage_never_freed.rs | 0 .../ui/pass/strict_balance_equality.rs | 0 linting/mandatory/Cargo.toml | 64 +++++++++++++ linting/mandatory/LICENSE | 1 + linting/mandatory/README.md | 2 + linting/mandatory/src/lib.rs | 49 ++++++++++ linting/{ => mandatory}/src/no_main.rs | 0 linting/{ => mandatory}/ui/pass/no_main.rs | 0 26 files changed, 217 insertions(+), 89 deletions(-) create mode 100644 linting/extra/Cargo.toml create mode 120000 linting/extra/LICENSE create mode 100644 linting/extra/README.md rename linting/{ => extra}/src/ink_utils.rs (100%) rename linting/{ => extra}/src/lib.rs (94%) rename linting/{ => extra}/src/primitive_topic.rs (100%) rename linting/{ => extra}/src/storage_never_freed.rs (100%) rename linting/{ => extra}/src/strict_balance_equality.rs (100%) rename linting/{ => extra}/ui/fail/primitive_topic.rs (100%) rename linting/{ => extra}/ui/fail/primitive_topic.stderr (100%) rename linting/{ => extra}/ui/fail/storage_never_freed.rs (100%) rename linting/{ => extra}/ui/fail/storage_never_freed.stderr (100%) rename linting/{ => extra}/ui/fail/strict_balance_equality.rs (100%) rename linting/{ => extra}/ui/fail/strict_balance_equality.stderr (100%) rename linting/{ => extra}/ui/pass/primitive_topic.rs (100%) rename linting/{ => extra}/ui/pass/storage_never_freed.rs (100%) rename linting/{ => extra}/ui/pass/strict_balance_equality.rs (100%) create mode 100644 linting/mandatory/Cargo.toml create mode 120000 linting/mandatory/LICENSE create mode 100644 linting/mandatory/README.md create mode 100644 linting/mandatory/src/lib.rs rename linting/{ => mandatory}/src/no_main.rs (100%) rename linting/{ => mandatory}/ui/pass/no_main.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 2a264bf54ff..afac0c3a63c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ ] exclude = [ "integration-tests", + "linting", ] [workspace.package] diff --git a/linting/Cargo.toml b/linting/Cargo.toml index f622e319e9e..82264da62e4 100644 --- a/linting/Cargo.toml +++ b/linting/Cargo.toml @@ -1,85 +1,12 @@ -[package] -name = "ink_linting" -version = "5.0.0-rc" -authors = ["Parity Technologies "] -edition = "2021" -publish = false - -license = "Apache-2.0" -readme = "README.md" -repository = "https://github.com/paritytech/ink" -documentation = "https://docs.rs/ink_linting" -homepage = "https://github.com/paritytech/ink" -description = "Linting tool for ink! smart contracts." -keywords = ["parity", "blockchain", "ink", "smart contracts", "substrate"] -include = ["Cargo.toml", "*.rs", "LICENSE"] - -[lib] -crate-type = ["cdylib"] - -[dependencies] -clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "1d334696587ac22b3a9e651e7ac684ac9e0697b2" } -dylint_linting = "2.1.12" -if_chain = "1.0.2" -log = "0.4.14" -regex = "1.5.4" - -[dev-dependencies] -dylint_testing = "2.1.12" - -# The following are ink! dependencies, they are only required for the `ui` tests. -ink_env = { path = "../crates/env", default-features = false } -ink = { path = "../crates/ink", default-features = false, features = ["std"] } -ink_metadata = { path = "../crates/metadata", default-features = false } -ink_primitives = { path = "../crates/primitives", default-features = false } -ink_storage = { path = "../crates/storage", default-features = false } -scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"] } - -# For the moment we have to include the tests as examples and -# then use `dylint_testing::ui_test_examples`. -# -# The reason is that the `dylint_testing` API currently does not -# provide any other option to run the tests on those files -# *while giving us the option to specify the dependencies*. -# -# Those files require the ink! dependencies though, by having -# them as examples here, they inherit the `dev-dependencies`. -[[example]] -name = "primitive_topic_pass" -path = "ui/pass/primitive_topic.rs" -[[example]] -name = "primitive_topic_fail" -path = "ui/fail/primitive_topic.rs" -[[example]] -name = "storage_never_freed_pass" -path = "ui/pass/storage_never_freed.rs" -[[example]] -name = "storage_never_freed_fail" -path = "ui/fail/storage_never_freed.rs" -[[example]] -name = "strict_balance_equality_pass" -path = "ui/pass/strict_balance_equality.rs" -[[example]] -name = "strict_balance_equality_fail" -path = "ui/fail/strict_balance_equality.rs" -[[example]] -name = "no_main_pass" -path = "ui/pass/no_main.rs" - -[package.metadata.rust-analyzer] -rustc_private = true - [workspace] +resolver = "2" +members = [ + "mandatory", + "extra", +] -[features] -default = ["std"] -std = [ - "ink_metadata/std", - "ink_env/std", - "ink_storage/std", - "ink_primitives/std", - "scale/std", - "scale-info/std", +[workspace.metadata.dylint] +libraries = [ + { path = "mandatory" }, + { path = "extra" }, ] -ink-as-dependency = [] diff --git a/linting/README.md b/linting/README.md index 8cc265805b7..5257412880d 100644 --- a/linting/README.md +++ b/linting/README.md @@ -1,18 +1,23 @@ # ink! linting rules This crate uses [`dylint`](https://github.com/trailofbits/dylint) to define custom -linting rules for [ink!](https://github.com/paritytech/ink); +linting rules for [ink!](https://github.com/paritytech/ink). It is not part of the workspace because it needs a custom linker to be built. The lints are written against a fixed toolchain version because they are using unstable -APIs. This is why we have a toolchain file here. +APIs. This is why we have [a toolchain file](./rust-toolchain.toml) here. -You can use it by running `cargo dylint` after adding this to your `Cargo.toml`: +This crate contains two libraries: +* [`mandatory`](./mandatory) lints are integrated into the ink! smart contracts' build process, adding custom compilation errors to `cargo-build`. +* [`extra`](./extra) lints are designed to check for secure coding style in smart contracts and highlight potential issues. These are optional and intended for use by the contract developer to improve the security properties of their project. + +You can use them by running `cargo dylint` after adding this to your `Cargo.toml`: ```toml [workspace.metadata.dylint] libraries = [ - { git = "https://github.com/paritytech/ink.git", pattern = "linting/" }, + { git = "https://github.com/paritytech/ink.git", pattern = "linting/mandatory" }, + { git = "https://github.com/paritytech/ink.git", pattern = "linting/extra" }, ] ``` diff --git a/linting/extra/Cargo.toml b/linting/extra/Cargo.toml new file mode 100644 index 00000000000..dbbeb2719d7 --- /dev/null +++ b/linting/extra/Cargo.toml @@ -0,0 +1,79 @@ +[package] +name = "ink_linting" +version = "5.0.0-rc" +authors = ["Parity Technologies "] +edition = "2021" +publish = false + +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/paritytech/ink" +documentation = "https://docs.rs/ink_linting" +homepage = "https://github.com/paritytech/ink" +description = "Extra linting rules for ink! smart contracts" +keywords = ["parity", "blockchain", "ink", "smart contracts", "substrate"] +include = ["Cargo.toml", "*.rs", "LICENSE"] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "1d334696587ac22b3a9e651e7ac684ac9e0697b2" } +dylint_linting = "2.1.12" +if_chain = "1.0.2" +log = "0.4.14" +regex = "1.5.4" + +[dev-dependencies] +dylint_testing = "2.1.12" +# The following are ink! dependencies, they are only required for the `ui` tests. +ink_env = { path = "../../crates/env", default-features = false } +ink = { path = "../../crates/ink", default-features = false, features = ["std"] } +ink_metadata = { path = "../../crates/metadata", default-features = false } +ink_primitives = { path = "../../crates/primitives", default-features = false } +ink_storage = { path = "../../crates/storage", default-features = false } +scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] } +scale-info = { version = "2.6", default-features = false, features = ["derive"] } + +# For the moment we have to include the tests as examples and +# then use `dylint_testing::ui_test_examples`. +# +# The reason is that the `dylint_testing` API currently does not +# provide any other option to run the tests on those files +# *while giving us the option to specify the dependencies*. +# +# Those files require the ink! dependencies though, by having +# them as examples here, they inherit the `dev-dependencies`. +[[example]] +name = "primitive_topic_pass" +path = "ui/pass/primitive_topic.rs" +[[example]] +name = "primitive_topic_fail" +path = "ui/fail/primitive_topic.rs" +[[example]] +name = "storage_never_freed_pass" +path = "ui/pass/storage_never_freed.rs" +[[example]] +name = "storage_never_freed_fail" +path = "ui/fail/storage_never_freed.rs" +[[example]] +name = "strict_balance_equality_pass" +path = "ui/pass/strict_balance_equality.rs" +[[example]] +name = "strict_balance_equality_fail" +path = "ui/fail/strict_balance_equality.rs" + +[package.metadata.rust-analyzer] +rustc_private = true + +[features] +default = ["std"] +std = [ + "ink_metadata/std", + "ink_env/std", + "ink_storage/std", + "ink_primitives/std", + "scale/std", + "scale-info/std", +] +ink-as-dependency = [] diff --git a/linting/extra/LICENSE b/linting/extra/LICENSE new file mode 120000 index 00000000000..ea5b60640b0 --- /dev/null +++ b/linting/extra/LICENSE @@ -0,0 +1 @@ +../LICENSE \ No newline at end of file diff --git a/linting/extra/README.md b/linting/extra/README.md new file mode 100644 index 00000000000..b68a8de548f --- /dev/null +++ b/linting/extra/README.md @@ -0,0 +1,2 @@ +# Extra ink! linting rules +These lints are designed to check for secure coding style in smart contracts and highlight potential issues. These are optional and intended for use by the contract developer to improve the security properties of their project. diff --git a/linting/src/ink_utils.rs b/linting/extra/src/ink_utils.rs similarity index 100% rename from linting/src/ink_utils.rs rename to linting/extra/src/ink_utils.rs diff --git a/linting/src/lib.rs b/linting/extra/src/lib.rs similarity index 94% rename from linting/src/lib.rs rename to linting/extra/src/lib.rs index e9b7673e1bd..fdb19ab04b7 100644 --- a/linting/src/lib.rs +++ b/linting/extra/src/lib.rs @@ -32,7 +32,6 @@ extern crate rustc_session; extern crate rustc_span; mod ink_utils; -mod no_main; mod primitive_topic; mod storage_never_freed; mod strict_balance_equality; @@ -47,13 +46,11 @@ pub fn register_lints( primitive_topic::PRIMITIVE_TOPIC, storage_never_freed::STORAGE_NEVER_FREED, strict_balance_equality::STRICT_BALANCE_EQUALITY, - no_main::NO_MAIN, ]); lint_store.register_late_pass(|_| Box::new(primitive_topic::PrimitiveTopic)); lint_store.register_late_pass(|_| Box::new(storage_never_freed::StorageNeverFreed)); lint_store .register_late_pass(|_| Box::new(strict_balance_equality::StrictBalanceEquality)); - lint_store.register_early_pass(|| Box::new(no_main::NoMain)); } #[test] diff --git a/linting/src/primitive_topic.rs b/linting/extra/src/primitive_topic.rs similarity index 100% rename from linting/src/primitive_topic.rs rename to linting/extra/src/primitive_topic.rs diff --git a/linting/src/storage_never_freed.rs b/linting/extra/src/storage_never_freed.rs similarity index 100% rename from linting/src/storage_never_freed.rs rename to linting/extra/src/storage_never_freed.rs diff --git a/linting/src/strict_balance_equality.rs b/linting/extra/src/strict_balance_equality.rs similarity index 100% rename from linting/src/strict_balance_equality.rs rename to linting/extra/src/strict_balance_equality.rs diff --git a/linting/ui/fail/primitive_topic.rs b/linting/extra/ui/fail/primitive_topic.rs similarity index 100% rename from linting/ui/fail/primitive_topic.rs rename to linting/extra/ui/fail/primitive_topic.rs diff --git a/linting/ui/fail/primitive_topic.stderr b/linting/extra/ui/fail/primitive_topic.stderr similarity index 100% rename from linting/ui/fail/primitive_topic.stderr rename to linting/extra/ui/fail/primitive_topic.stderr diff --git a/linting/ui/fail/storage_never_freed.rs b/linting/extra/ui/fail/storage_never_freed.rs similarity index 100% rename from linting/ui/fail/storage_never_freed.rs rename to linting/extra/ui/fail/storage_never_freed.rs diff --git a/linting/ui/fail/storage_never_freed.stderr b/linting/extra/ui/fail/storage_never_freed.stderr similarity index 100% rename from linting/ui/fail/storage_never_freed.stderr rename to linting/extra/ui/fail/storage_never_freed.stderr diff --git a/linting/ui/fail/strict_balance_equality.rs b/linting/extra/ui/fail/strict_balance_equality.rs similarity index 100% rename from linting/ui/fail/strict_balance_equality.rs rename to linting/extra/ui/fail/strict_balance_equality.rs diff --git a/linting/ui/fail/strict_balance_equality.stderr b/linting/extra/ui/fail/strict_balance_equality.stderr similarity index 100% rename from linting/ui/fail/strict_balance_equality.stderr rename to linting/extra/ui/fail/strict_balance_equality.stderr diff --git a/linting/ui/pass/primitive_topic.rs b/linting/extra/ui/pass/primitive_topic.rs similarity index 100% rename from linting/ui/pass/primitive_topic.rs rename to linting/extra/ui/pass/primitive_topic.rs diff --git a/linting/ui/pass/storage_never_freed.rs b/linting/extra/ui/pass/storage_never_freed.rs similarity index 100% rename from linting/ui/pass/storage_never_freed.rs rename to linting/extra/ui/pass/storage_never_freed.rs diff --git a/linting/ui/pass/strict_balance_equality.rs b/linting/extra/ui/pass/strict_balance_equality.rs similarity index 100% rename from linting/ui/pass/strict_balance_equality.rs rename to linting/extra/ui/pass/strict_balance_equality.rs diff --git a/linting/mandatory/Cargo.toml b/linting/mandatory/Cargo.toml new file mode 100644 index 00000000000..db178816137 --- /dev/null +++ b/linting/mandatory/Cargo.toml @@ -0,0 +1,64 @@ +[package] +name = "ink_linting_mandatory" +version = "5.0.0-rc" +authors = ["Parity Technologies "] +edition = "2021" +publish = false + +license = "Apache-2.0" +readme = "README.md" +repository = "https://github.com/paritytech/ink" +documentation = "https://docs.rs/ink_linting" +homepage = "https://github.com/paritytech/ink" +description = "Mandatory ink! linting rules integrated in contracts' build process" +keywords = ["parity", "blockchain", "ink", "smart contracts", "substrate"] +include = ["Cargo.toml", "*.rs", "LICENSE"] + +[lib] +crate-type = ["cdylib"] + +[dependencies] +clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "1d334696587ac22b3a9e651e7ac684ac9e0697b2" } +dylint_linting = "2.1.12" +if_chain = "1.0.2" +log = "0.4.14" +regex = "1.5.4" + +[dev-dependencies] +dylint_testing = "2.1.12" +# The following are ink! dependencies, they are only required for the `ui` tests. +ink_env = { path = "../../crates/env", default-features = false } +ink = { path = "../../crates/ink", default-features = false, features = ["std"] } +ink_metadata = { path = "../../crates/metadata", default-features = false } +ink_primitives = { path = "../../crates/primitives", default-features = false } +ink_storage = { path = "../../crates/storage", default-features = false } +scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] } +scale-info = { version = "2.6", default-features = false, features = ["derive"] } + +# For the moment we have to include the tests as examples and +# then use `dylint_testing::ui_test_examples`. +# +# The reason is that the `dylint_testing` API currently does not +# provide any other option to run the tests on those files +# *while giving us the option to specify the dependencies*. +# +# Those files require the ink! dependencies though, by having +# them as examples here, they inherit the `dev-dependencies`. +[[example]] +name = "no_main_pass" +path = "ui/pass/no_main.rs" + +[package.metadata.rust-analyzer] +rustc_private = true + +[features] +default = ["std"] +std = [ + "ink_metadata/std", + "ink_env/std", + "ink_storage/std", + "ink_primitives/std", + "scale/std", + "scale-info/std", +] +ink-as-dependency = [] diff --git a/linting/mandatory/LICENSE b/linting/mandatory/LICENSE new file mode 120000 index 00000000000..ea5b60640b0 --- /dev/null +++ b/linting/mandatory/LICENSE @@ -0,0 +1 @@ +../LICENSE \ No newline at end of file diff --git a/linting/mandatory/README.md b/linting/mandatory/README.md new file mode 100644 index 00000000000..9702eb422e7 --- /dev/null +++ b/linting/mandatory/README.md @@ -0,0 +1,2 @@ +# Mandatory ink! linting rules +dylint-based lints integrated into the ink! contracts' build process, intended to generate custom compilation errors. diff --git a/linting/mandatory/src/lib.rs b/linting/mandatory/src/lib.rs new file mode 100644 index 00000000000..02b94a34b32 --- /dev/null +++ b/linting/mandatory/src/lib.rs @@ -0,0 +1,49 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// +// 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. + +#![doc( + html_logo_url = "https://use.ink/img/crate-docs/logo.png", + html_favicon_url = "https://use.ink/crate-docs/favicon.png" +)] +#![feature(rustc_private)] +#![feature(box_patterns)] + +dylint_linting::dylint_library!(); + +extern crate rustc_ast; +extern crate rustc_errors; +extern crate rustc_hir; +extern crate rustc_index; +extern crate rustc_lint; +extern crate rustc_middle; +extern crate rustc_mir_dataflow; +extern crate rustc_session; +extern crate rustc_span; + +mod no_main; + +#[doc(hidden)] +#[no_mangle] +pub fn register_lints( + _sess: &rustc_session::Session, + lint_store: &mut rustc_lint::LintStore, +) { + lint_store.register_lints(&[no_main::NO_MAIN]); + lint_store.register_early_pass(|| Box::new(no_main::NoMain)); +} + +#[test] +fn ui() { + dylint_testing::ui_test_examples(env!("CARGO_PKG_NAME")); +} diff --git a/linting/src/no_main.rs b/linting/mandatory/src/no_main.rs similarity index 100% rename from linting/src/no_main.rs rename to linting/mandatory/src/no_main.rs diff --git a/linting/ui/pass/no_main.rs b/linting/mandatory/ui/pass/no_main.rs similarity index 100% rename from linting/ui/pass/no_main.rs rename to linting/mandatory/ui/pass/no_main.rs