# InvariantGuard : A framework to make DELEGATECALL safer

**URL:** <https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206>\
**Category:** Smart Contracts\
**Created:** [February 11, 2026, 7:26am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206 "2026-02-11T07:26:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helkomine](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/helkomine/32/23505_2.png) [@Helkomine](https://forum.openzeppelin.com/u/Helkomine)\
**Post date:** [February 11, 2026, 7:26am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/1 "2026-02-11T07:26:58Z")

</div>

Proposed new library for OpenZeppelin: `InvariantGuard`

## Background

`DELEGATECALL` was introduced very early in Ethereum (EIP-7) as a safer successor to `CALLCODE`.

It is a particularly powerful opcode: it allows a contract to load and execute code from a target address in the caller’s context. This implies that delegated code can freely modify the caller’s storage, something that plain `CALL` cannot fully replace.

In addition, `DELEGATECALL` preserves both `msg.sender` and `msg.value`, which makes it extremely useful for composability and immediate reasoning in delegated execution contexts.

However, despite its importance, the protocol has not introduced major improvements around this opcode since its inception. This does not mean that no issues exist. In practice, using `DELEGATECALL` imposes a significant additional burden on developers, especially regarding storage safety and layout management. Any inconsistency in layout assumptions can lead to catastrophic consequences.

### Existing Mitigations and Their Limitations

There have been attempts to mitigate these risks. One notable example is the introduction of explicit storage namespaces (ERC-7201), which aims to reduce layout collisions.

However, such solutions primarily address storage layout assumptions and rely on the proxy delegating to a well-behaved logic contract. This implicitly assumes that there exists at least one “valid” execution path. In reality, layouts can still be broken, for example by unintentionally activating malicious logic embedded in a backdoored contract.

This problem becomes particularly severe in modular smart contract architectures, where users are allowed to install custom modules. Most users lack the expertise to thoroughly analyze the safety of these modules. Once installed, a malicious module can remain dormant and later be triggered by seemingly harmless transactions, ultimately allowing an attacker to seize full control of a wallet and cause irreversible damage.

Some cautious teams have implemented pre- and post-execution value checks to reduce the impact of `DELEGATECALL`. While helpful, these patterns are not widely adopted, leaving most developers to repeatedly reinvent partial and fragile safety mechanisms. As a result, many deployed contracts remain fundamentally exposed: a single mistake during delegated execution can result in total loss of control.

## Motivation and Overview

Based on these observations, the author originally introduced a complete implementation named `Safe-Delegatecall`, later renamed to `Invariant-Guard` to reflect a more ambitious goal:

Not only controlling state changes caused by `DELEGATECALL`, but by any opcode or execution path that may alter critical invariants.

This repository presents the first public Solidity implementation of `Invariant-Guard`. Feedback from the community is highly appreciated.

The author is also preparing an EIP proposal to provide protocol-level invariant protection, enabling global guarantees that cannot be fully achieved at the contract level alone.

# Test Cases

The following example establishes an invariant on the `owner` variable of the diamond contract, any attempt to replace it within the `fallback` function results in a revert:

```auto
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.20;
import "https://github.com/Helkomine/invariant-guard/blob/main/invariant-guard/InvariantGuardInternal.sol";

contract InvariantDiamondSimple is InvariantGuardInternal {
    bytes32 constant OWNER_STORAGE_POSITION = keccak256("erc8109.owner");

    /**
     * @notice Storage for owner of the diamond.
     *
     * @custom:storage-location erc8042:erc8109.owner
     */
    struct OwnerStorage {
        address owner;
    }

    bytes32 constant DIAMOND_STORAGE_POSITION = keccak256("erc8109.diamond");

    /**
     * @notice Data stored for each function selector
     * @dev Facet address of function selector
     * Position of selector in the 'bytes4[] selectors' array
     */
    struct FacetAndPosition {
        address facet;
        uint32 position;
    }

    /**
     * @custom:storage-location erc8042:erc8109.diamond
     */
    struct DiamondStorage {
        mapping(bytes4 functionSelector => FacetAndPosition) facetAndPosition;
        /**
         * Array of all function selectors that can be called in the diamond
         */
        bytes4[] selectors;
    }

    function getDiamondStorage() internal pure returns (DiamondStorage storage s) {
        bytes32 position = DIAMOND_STORAGE_POSITION;
        assembly {
            s.slot := position
        }
    }

    error FunctionNotFound(bytes4 _selector);

    function getInvariantSlots() internal pure returns (bytes32[] memory slots) {
        slots = new bytes32[](1);
        slots[0] = OWNER_STORAGE_POSITION;
    }

    function _diamondFallback() internal invariantStorage(getInvariantSlots()) returns (bool success, bytes memory data) {
        DiamondStorage storage s = getDiamondStorage();
        // Get facet from function selector
        address facet = s.facetAndPosition[msg.sig].facet;
        if (facet == address(0)) {
            revert FunctionNotFound(msg.sig);
        }

        (success, data) = facet.delegatecall(msg.data);
    }

    fallback() external payable {
        (bool success, bytes memory data) = _diamondFallback();
        assembly {
            let ptr := add(data, 32)
            let len := mload(data)
            switch success
            case 0 { revert(ptr, len) }
            default { return(ptr, len) }
        }
    }
}

```

# Reference

Link to [InvariantGuard](https://github.com/Helkomine/invariant-guard/tree/main)

Link to [EIP](https://github.com/Helkomine/EIPs/blob/c6c2bf3fe83bb9a8ba1cc692af5637776cec1c74/EIPS/eip-9999.md)

---

<div class="post-metadata">

**Author:** ![primeember334](https://avatars.discourse-cdn.com/v4/letter/p/fbc32d/32.png) [@primeember334](https://forum.openzeppelin.com/u/primeember334)\
**Post date:** [November 5, 2026, 3:12pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/2 "2026-11-05T15:12:25Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![plaincanyon189](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@plaincanyon189](https://forum.openzeppelin.com/u/plaincanyon189)\
**Post date:** [November 13, 2026, 6:26pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/3 "2026-11-13T18:26:47Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![darkcircuit994](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@darkcircuit994](https://forum.openzeppelin.com/u/darkcircuit994)\
**Post date:** [November 13, 2026, 7:20pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/4 "2026-11-13T19:20:08Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![rustyfalcon206](https://avatars.discourse-cdn.com/v4/letter/r/eb8c5e/32.png) [@rustyfalcon206](https://forum.openzeppelin.com/u/rustyfalcon206)\
**Post date:** [November 13, 2026, 7:59pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/5 "2026-11-13T19:59:27Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![warmbeacon605](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@warmbeacon605](https://forum.openzeppelin.com/u/warmbeacon605)\
**Post date:** [November 13, 2026, 8:27pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/6 "2026-11-13T20:27:29Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![warmpillar281](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@warmpillar281](https://forum.openzeppelin.com/u/warmpillar281)\
**Post date:** [November 13, 2026, 8:54pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/7 "2026-11-13T20:54:32Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![silentanchor792](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@silentanchor792](https://forum.openzeppelin.com/u/silentanchor792)\
**Post date:** [November 13, 2026, 9:20pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/8 "2026-11-13T21:20:43Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![silentharbor451](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@silentharbor451](https://forum.openzeppelin.com/u/silentharbor451)\
**Post date:** [November 13, 2026, 9:43pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/9 "2026-11-13T21:43:35Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![solidmarlin583](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@solidmarlin583](https://forum.openzeppelin.com/u/solidmarlin583)\
**Post date:** [November 13, 2026, 10:17pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/10 "2026-11-13T22:17:15Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![briskember947](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@briskember947](https://forum.openzeppelin.com/u/briskember947)\
**Post date:** [November 13, 2026, 10:40pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/11 "2026-11-13T22:40:30Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![clearraven840](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@clearraven840](https://forum.openzeppelin.com/u/clearraven840)\
**Post date:** [November 13, 2026, 11:05pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/12 "2026-11-13T23:05:50Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![silentanchor509](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@silentanchor509](https://forum.openzeppelin.com/u/silentanchor509)\
**Post date:** [November 14, 2026, 2:54am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/13 "2026-11-14T02:54:53Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![primegasket633](https://avatars.discourse-cdn.com/v4/letter/p/67e7ee/32.png) [@primegasket633](https://forum.openzeppelin.com/u/primegasket633)\
**Post date:** [November 14, 2026, 3:13am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/14 "2026-11-14T03:13:53Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![smoothmeadow778](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@smoothmeadow778](https://forum.openzeppelin.com/u/smoothmeadow778)\
**Post date:** [November 14, 2026, 3:31am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/15 "2026-11-14T03:31:05Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![sharpraven914](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@sharpraven914](https://forum.openzeppelin.com/u/sharpraven914)\
**Post date:** [November 14, 2026, 8:23am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/16 "2026-11-14T08:23:34Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![straysummit808](https://avatars.discourse-cdn.com/v4/letter/s/bc8723/32.png) [@straysummit808](https://forum.openzeppelin.com/u/straysummit808)\
**Post date:** [November 14, 2026, 12:54pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/17 "2026-11-14T12:54:15Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![primewillow233](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@primewillow233](https://forum.openzeppelin.com/u/primewillow233)\
**Post date:** [November 16, 2026, 10:58am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/18 "2026-11-16T10:58:02Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![quietquartz896](https://avatars.discourse-cdn.com/v4/letter/q/d6d6ee/32.png) [@quietquartz896](https://forum.openzeppelin.com/u/quietquartz896)\
**Post date:** [November 16, 2026, 11:36am UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/19 "2026-11-16T11:36:55Z")

</div>

This post was flagged by the community and is temporarily hidden.

---

<div class="post-metadata">

**Author:** ![plainsummit189](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@plainsummit189](https://forum.openzeppelin.com/u/plainsummit189)\
**Post date:** [November 16, 2026, 1:02pm UTC](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206/20 "2026-11-16T13:02:07Z")

</div>

This post was flagged by the community and is temporarily hidden.

[Next page](https://forum.openzeppelin.com/t/invariantguard-a-framework-to-make-delegatecall-safer/45206.md?page=2)
