Skip to content

Commit 95600bc

Browse files
authored
Merge pull request #2378 from CosmWasm/co/patch-on-main
Merge patch to main
2 parents e7c5e77 + 4c5e28b commit 95600bc

File tree

6 files changed

+235
-93
lines changed

6 files changed

+235
-93
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ and this project adheres to
2828
`len` argument to allow truncating address data as part of the generation
2929
process. ([#2155])
3030
- cosmwasm-vm: Updated wasmer to 5.0.4 ([#2374])
31+
- cosmwasm-vm: Charge gas for `write_region` ([#2378])
3132

3233
## Fixed
3334

3435
- cosmwasm-schema: The schema export now doesn't overwrite existing
3536
`additionalProperties` values anymore ([#2310])
37+
- cosmwasm-vm: Fix CWA-2025-002.
3638

3739
[#2155]: https://github.com/CosmWasm/cosmwasm/issues/2155
3840
[#2268]: https://github.com/CosmWasm/cosmwasm/issues/2268
@@ -41,7 +43,8 @@ and this project adheres to
4143
[#2337]: https://github.com/CosmWasm/cosmwasm/issues/2337
4244
[#2340]: https://github.com/CosmWasm/cosmwasm/pull/2340
4345
[#2344]: https://github.com/CosmWasm/cosmwasm/pull/2344
44-
[#2374]: https://github.com/CosmWasm/cosmwasm/issues/2155
46+
[#2374]: https://github.com/CosmWasm/cosmwasm/issues/2374
47+
[#2378]: https://github.com/CosmWasm/cosmwasm/issues/2378
4548

4649
## [2.2.0] - 2024-12-17
4750

contracts/hackatom/tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn execute_allocate_large_memory() {
435435
// Gas consumption is relatively small
436436
// Note: the exact gas usage depends on the Rust version used to compile Wasm,
437437
// which we only fix when using rust-optimizer, not integration tests.
438-
let expected = 9553320; // +/- 20%
438+
let expected = 12162675; // +/- 20%
439439
assert!(gas_used > expected * 80 / 100, "Gas used: {gas_used}");
440440
assert!(gas_used < expected * 120 / 100, "Gas used: {gas_used}");
441441
let used = deps.memory_pages();

packages/vm/src/environment.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ pub struct GasConfig {
5555
pub bls12_381_hash_to_g2_cost: u64,
5656
/// bls12-381 pairing equality check cost
5757
pub bls12_381_pairing_equality_cost: LinearGasCost,
58+
/// cost for writing memory regions
59+
pub write_region_cost: LinearGasCost,
60+
/// cost for reading memory regions <= 8MB
61+
pub read_region_small_cost: LinearGasCost,
62+
/// cost for reading memory regions > 8MB
63+
pub read_region_large_cost: LinearGasCost,
64+
/// cost for validating bytes into a String
65+
pub string_from_bytes_cost: LinearGasCost,
66+
/// cost for calling a host function
67+
pub host_call_cost: u64,
5868
}
5969

6070
impl Default for GasConfig {
@@ -97,6 +107,34 @@ impl Default for GasConfig {
97107
base: 2112 * GAS_PER_US,
98108
per_item: 163 * GAS_PER_US,
99109
},
110+
write_region_cost: LinearGasCost {
111+
base: 230000,
112+
per_item: 570,
113+
},
114+
read_region_small_cost: LinearGasCost {
115+
base: 200000,
116+
per_item: 115,
117+
},
118+
read_region_large_cost: LinearGasCost {
119+
base: 0,
120+
per_item: 520,
121+
},
122+
string_from_bytes_cost: LinearGasCost {
123+
base: 28700,
124+
per_item: 1400,
125+
},
126+
host_call_cost: 18000,
127+
}
128+
}
129+
}
130+
131+
impl GasConfig {
132+
pub fn read_region_cost(&self, bytes: usize) -> VmResult<u64> {
133+
const THRESHOLD: usize = 8 * 1000 * 1000;
134+
if bytes <= THRESHOLD {
135+
self.read_region_small_cost.total_cost(bytes as u64)
136+
} else {
137+
self.read_region_large_cost.total_cost(bytes as u64)
100138
}
101139
}
102140
}
@@ -115,8 +153,13 @@ pub struct LinearGasCost {
115153
}
116154

117155
impl LinearGasCost {
118-
pub fn total_cost(&self, items: u64) -> u64 {
119-
self.base + self.per_item * items
156+
pub fn total_cost(&self, items: u64) -> VmResult<u64> {
157+
self.total_cost_opt(items)
158+
.ok_or_else(VmError::gas_depletion)
159+
}
160+
161+
fn total_cost_opt(&self, items: u64) -> Option<u64> {
162+
self.base.checked_add(self.per_item.checked_mul(items)?)
120163
}
121164
}
122165

0 commit comments

Comments
 (0)