From 8a29232801708e2679ee1f444136d76e6e340837 Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Wed, 25 Mar 2020 18:46:49 +0100 Subject: [PATCH] Ban duplicates of parity-uil-mem from being linked into the same program (#363) * Prevent multiple versions of parity-uil-mem from being linked into the same program * fmt * parity-util-mem: add a warning about defining global allocs * parity-util-mem: document empty build script * parity-util-mem: extend the warning based on David comments --- parity-util-mem/CHANGELOG.md | 2 ++ parity-util-mem/Cargo.toml | 7 +++++++ parity-util-mem/README.md | 14 ++++++++++++++ parity-util-mem/build.rs | 1 + 4 files changed, 24 insertions(+) create mode 100644 parity-util-mem/build.rs diff --git a/parity-util-mem/CHANGELOG.md b/parity-util-mem/CHANGELOG.md index fdb3f5f7b..ec8ef91f9 100644 --- a/parity-util-mem/CHANGELOG.md +++ b/parity-util-mem/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog]. [Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Breaking +- Prevent multiple versions from being linked into the same program. [#363](https://github.com/paritytech/parity-common/pull/363) ## [0.6.0] - 2020-03-13 - Updated dependencies. [#361](https://github.com/paritytech/parity-common/pull/361) diff --git a/parity-util-mem/Cargo.toml b/parity-util-mem/Cargo.toml index af3916902..700061c32 100644 --- a/parity-util-mem/Cargo.toml +++ b/parity-util-mem/Cargo.toml @@ -7,6 +7,13 @@ description = "Collection of memory related utilities" license = "MIT OR Apache-2.0" edition = "2018" +# Prevent multiple versions from being linked into the same program. +links = "parity-util-mem-ban-duplicates" +# `links` requires a build script to be present: +# https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key +# so we use an empty build script +build = "build.rs" + [dependencies] cfg-if = "0.1.10" dlmalloc = { version = "0.1.3", features = ["global"], optional = true } diff --git a/parity-util-mem/README.md b/parity-util-mem/README.md index 14d6e6c19..a6f15b90b 100644 --- a/parity-util-mem/README.md +++ b/parity-util-mem/README.md @@ -2,6 +2,20 @@ Collection of memory related utilities. +## WARNING + +When `parity-util-mem` is used as a dependency with any of the global allocator features enabled, +it must be the sole place where a global allocator is defined. +The only exception to this rule is when used in a `no_std` context or when the `estimate-heapsize` feature is used. + +Because of that, it must be present in the dependency tree with a single version. +Starting from version 0.7, having duplicate versions of `parity-util-mem` will lead +to a compile-time error. It still will be possible to have 0.6 and 0.7 versions in the same binary though. + +Unless heeded you risk UB; see discussion in [issue 364]. + +[issue 364]: https://github.com/paritytech/parity-common/issues/364 + ## Features - estimate-heapsize : Do not use allocator, but `size_of` or `size_of_val`. diff --git a/parity-util-mem/build.rs b/parity-util-mem/build.rs new file mode 100644 index 000000000..f328e4d9d --- /dev/null +++ b/parity-util-mem/build.rs @@ -0,0 +1 @@ +fn main() {}