Skip to content

3.20: Shielded storage opcodes are emitted for pre-Mercury EVM versions#201

Merged
cdrappi merged 2 commits intozellic-auditfrom
cops-emitted-pre-mercury
Feb 16, 2026
Merged

3.20: Shielded storage opcodes are emitted for pre-Mercury EVM versions#201
cdrappi merged 2 commits intozellic-auditfrom
cops-emitted-pre-mercury

Conversation

@cdrappi
Copy link
Contributor

@cdrappi cdrappi commented Feb 14, 2026

No description provided.

@cdrappi cdrappi force-pushed the cops-emitted-pre-mercury branch 2 times, most recently from 520a5b6 to 892b9c6 Compare February 14, 2026 23:39
Add comprehensive test coverage for shielded type validation
across different EVM versions. Tests verify that shielded types
are rejected on pre-Mercury EVM versions with error 9978.

State storage variables:
- shielded_storage_evm_version_paris.sol: Single suint256 on Paris
- shielded_storage_evm_version_cancun.sol: Multiple shielded types on Cancun
- shielded_storage_struct_evm_version.sol: Struct containing shielded type
- shielded_storage_sbytes_evm_version.sol: Shielded byte array

Other variable contexts:
- shielded_local_variable_evm_version.sol: Local function variable
- shielded_immutable_evm_version.sol: Immutable variable (error 7491)
- shielded_constant_evm_version.sol: Constant variable (error 7491)

Positive test:
- shielded_storage_evm_version_mercury.sol: Confirms Mercury support

All tests run with both legacy and via-IR pipelines (compileViaYul: true).

Note: Immutable and constant tests expect error 7491 (existing validation)
rather than 9978, as shielded types cannot be immutable or constant.
Prevent emission of CLOAD/CSTORE opcodes on pre-Mercury EVM versions
when using shielded types (suint, sbool, saddress, sbytes) in ANY context.

## Problem
Compiling with --evm-version paris (or any pre-Mercury version)
incorrectly emitted cload/cstore opcodes for shielded types,
causing "invalid opcode" runtime errors on pre-Mercury VMs.

## Solution: Two-Layer Defense

### Layer 1: Type Checker (libsolidity/analysis/TypeChecker.cpp)
Add comprehensive early validation in visit(VariableDeclaration) to reject
ALL uses of shielded types on pre-Mercury versions:

**Checked contexts:**
- State storage variables (regular, non-constant, non-immutable)
- Local storage references/pointers
- Local value-type variables (stack/memory)
- Immutable variables (value types only)
- Constant variables

**Safety considerations:**
- For immutables: only check if value type (non-value types already error at line 532)
- For constants: always safe (must be value types)
- For state variables: always safe (storage location guaranteed)
- For local storage: safe (storage location)
- For local value types: safe (no nested mapping issues)

Error 9978 provides actionable message guiding users to --evm-version mercury.

### Layer 2: Codegen Assertions

**Legacy Pipeline (libsolidity/codegen/LValue.cpp)**
Add 6 defensive assertions to catch any cases that slip through:
- GenericStorageItem::retrieveValue() - line 237
- GenericStorageItem::storeValue() - line 310
- GenericStorageItem::setToZero() - line 521
- StorageByteArrayElement::retrieveValue() - line 569
- StorageByteArrayElement::storeValue() - line 584
- StorageByteArrayElement::setToZero() - line 614

**Via-IR Pipeline (libsolidity/codegen/YulUtilFunctions.cpp)**
Add assertions in key functions:
- readFromStorageValueType() - line 2864
- resizeDynamicByteArrayFunction() - line 1448
- storageArrayPopFunction() - line 1648
- storageArrayPushZeroFunction() - line 1799

## Coverage
- All variable contexts (state, local, immutable, constant)
- Arrays/structs containing shielded types (containsShieldedType check)
- Shielded byte arrays (StorageByteArrayElement handlers)
- Both legacy and via-IR compilation pipelines
- All shielded type categories (suint, sbool, saddress, sbytes)
@cdrappi cdrappi force-pushed the cops-emitted-pre-mercury branch from 215dcc3 to 293eab1 Compare February 15, 2026 00:32
@cdrappi cdrappi merged commit b35d358 into zellic-audit Feb 16, 2026
1 check passed
@cdrappi cdrappi deleted the cops-emitted-pre-mercury branch February 16, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant