Skip to content

Commit

Permalink
cli: Require explicit overflow-checks flag (#2716)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Nov 29, 2023
1 parent 9dd1b54 commit a423f78
Show file tree
Hide file tree
Showing 55 changed files with 1,116 additions and 720 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The minor version will be incremented upon a breaking change and the patch versi
### Breaking

- cli: Make `cargo build-sbf` the default build command ([#2694](https://github.com/coral-xyz/anchor/pull/2694)).
- cli: Require explicit `overflow-checks` flag ([#2716](https://github.com/coral-xyz/anchor/pull/2716)).

## [0.29.0] - 2023-10-16

Expand Down
12 changes: 6 additions & 6 deletions bench/BINARY_SIZE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ The programs and their tests are located in [/tests/bench](https://github.com/co

Solana version: 1.17.0

| Program | Binary Size | - |
| ------- | ----------- | --- |
| bench | 735,800 | - |
| Program | Binary Size | - |
| ------- | ----------- | ------------------- |
| bench | 743,208 | 🔴 **+152 (0.02%)** |

### Notable changes

Expand All @@ -30,7 +30,7 @@ Solana version: 1.17.0

| Program | Binary Size | +/- |
| ------- | ----------- | ------------------------ |
| bench | 735,800 | 🟢 **-417,936 (36.22%)** |
| bench | 743,056 | 🟢 **-417,904 (36.00%)** |

### Notable changes

Expand All @@ -46,7 +46,7 @@ Solana version: 1.16.0

| Program | Binary Size | +/- |
| ------- | ----------- | ---------------------- |
| bench | 1,153,736 | 🔴 **+35,000 (3.13%)** |
| bench | 1,160,960 | 🔴 **+23,272 (2.05%)** |

### Notable changes

Expand All @@ -60,6 +60,6 @@ Solana version: 1.14.16

| Program | Binary Size | +/- |
| ------- | ----------- | --- |
| bench | 1,118,736 | N/A |
| bench | 1,137,688 | N/A |

---
696 changes: 348 additions & 348 deletions bench/COMPUTE_UNITS.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions cli/src/checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::Path;

use anyhow::{anyhow, Result};

use crate::config::Manifest;

/// Check whether `overflow-checks` codegen option is enabled.
///
/// https://doc.rust-lang.org/rustc/codegen-options/index.html#overflow-checks
pub fn check_overflow(cargo_toml_path: impl AsRef<Path>) -> Result<bool> {
Manifest::from_path(cargo_toml_path)?
.profile
.release
.as_ref()
.and_then(|profile| profile.overflow_checks)
.ok_or(anyhow!(
"`overflow-checks` is not enabled. To enable, add:\n\n\
[profile.release]\n\
overflow-checks = true\n\n\
in workspace root Cargo.toml.",
))
}
9 changes: 4 additions & 5 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,11 @@ impl Manifest {
format!("Error reading the directory with path: {}", cwd.display())
})?
.path();
if let Some(filename) = p.file_name() {
if filename.to_str() == Some("Cargo.toml") {
let m = WithPath::new(Manifest::from_path(&p)?, p);
return Ok(Some(m));
if let Some(filename) = p.file_name().and_then(|name| name.to_str()) {
if filename == "Cargo.toml" {
return Ok(Some(WithPath::new(Manifest::from_path(&p)?, p)));
}
if filename.to_str() == Some("Anchor.toml") {
if filename == "Anchor.toml" {
anchor_toml = true;
}
}
Expand Down
23 changes: 15 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anchor_syn::idl::types::{
IdlTypeDefinitionTy,
};
use anyhow::{anyhow, Context, Result};
use checks::check_overflow;
use clap::Parser;
use dirs::home_dir;
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -49,6 +50,7 @@ use std::str::FromStr;
use std::string::ToString;
use tar::Archive;

mod checks;
pub mod config;
pub mod rust_template;
pub mod solidity_template;
Expand Down Expand Up @@ -1183,15 +1185,13 @@ pub fn build(
}

let cfg = Config::discover(cfg_override)?.expect("Not in workspace.");
let build_config = BuildConfig {
verifiable,
solana_version: solana_version.or_else(|| cfg.toolchain.solana_version.clone()),
docker_image: docker_image.unwrap_or_else(|| cfg.docker()),
bootstrap,
};
let cfg_parent = cfg.path().parent().expect("Invalid Anchor.toml");

let cargo = Manifest::discover()?;
// Require overflow checks
let workspace_cargo_toml_path = cfg_parent.join("Cargo.toml");
if workspace_cargo_toml_path.exists() {
check_overflow(workspace_cargo_toml_path)?;
}

let idl_out = match idl {
Some(idl) => Some(PathBuf::from(idl)),
Expand All @@ -1205,10 +1205,17 @@ pub fn build(
};
fs::create_dir_all(idl_ts_out.as_ref().unwrap())?;

if !&cfg.workspace.types.is_empty() {
if !cfg.workspace.types.is_empty() {
fs::create_dir_all(cfg_parent.join(&cfg.workspace.types))?;
};

let cargo = Manifest::discover()?;
let build_config = BuildConfig {
verifiable,
solana_version: solana_version.or_else(|| cfg.toolchain.solana_version.clone()),
docker_image: docker_image.unwrap_or_else(|| cfg.docker()),
bootstrap,
};
match cargo {
// No Cargo.toml so build the entire workspace.
None => build_all(
Expand Down
3 changes: 3 additions & 0 deletions examples/tutorial/basic-0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions examples/tutorial/basic-1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions examples/tutorial/basic-2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions examples/tutorial/basic-3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions examples/tutorial/basic-4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
members = [
"programs/*"
]

[profile.release]
overflow-checks = true
5 changes: 4 additions & 1 deletion examples/tutorial/basic-5/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[workspace]
members = [
"programs/*"
]
]

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions tests/auction-house/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
3 changes: 3 additions & 0 deletions tests/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ members = [
"programs/*"
]
resolver = "2"

[profile.release]
overflow-checks = true
Loading

1 comment on commit a423f78

@vercel
Copy link

@vercel vercel bot commented on a423f78 Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com
anchor-docs-200ms.vercel.app

Please sign in to comment.