# Upgradable contract assembler

**URL:** <https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723>\
**Category:** Upgrades\
**Created:** [June 2, 2021, 7:20pm UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723 "2021-06-02T19:20:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftermath.digital](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@aftermath.digital](https://forum.openzeppelin.com/u/aftermath.digital)\
**Post date:** [June 2, 2021, 7:20pm UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723/1 "2021-06-02T19:20:51Z")

</div>

Hey,

I’ve recently been going over my understanding of transparent proxies, and noticed that at some-point there’s been a change:

Old code from OZ blog post

```auto
assembly {
  let ptr := mload(0x40)

  // (1) copy incoming call data
  calldatacopy(ptr, 0, calldatasize)

  // (2) forward call to logic contract
  let result := delegatecall(gas, _impl, ptr, calldatasize, 0, 0)
  let size := returndatasize

  // (3) retrieve return data
  returndatacopy(ptr, 0, size)

  // (4) forward return data back to caller
  switch result
  case 0 { revert(ptr, size) }
  default { return(ptr, size) }
}

```

New code from OZ contract:

```auto
assembly {
  // (1) copy incoming call data
  calldatacopy(0, 0, calldatasize())

  // (2) forward call to logic contract
  let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)

  // (3) retrieve return data
  returndatacopy(0, 0, returndatasize())

  // (4) forward return data back to caller
  switch result
  case 0 { revert(0, returndatasize()) }
  default { return(0, returndatasize()) }
}

```

I know that ` let ptr := mload(0x40)` used to be a special-slot containing the address of the next allocatable slot, so it made sense to me that it was used as the reference slot for some of these OPs, but now they use 0, is this some kind of shortcut which has been introduced? Or is there some assumption being made that 0 is the slot where the delegatcall will return its value?

P.S. This is a dupe from a post I made in security a few days ago, I now realise it should have been posted here - but I don’t appear to be able to delete my previous post.

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [June 2, 2021, 8:06pm UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723/2 "2021-06-02T20:06:11Z")

</div>

Welcome to the forum @aftermath.digital. I’ve deleted your previous post.

This was changed because it is cheaper this way, and it’s not necessary to write in the free memory pointer (`mload(0x40)`). It’s not necessary because we will return immediately after writing this memory, so we don’t have to preserve any Solidity invariants and we can just write to memory starting at position 0.

There is no assumption made, delegatecall returns its value to a separate “returndata” memory space, and we use `returndatacopy` to copy it over to the general memory space, starting at position 0 because of what I mentioned in the previous paragraph.

---

<div class="post-metadata">

**Author:** ![PaulRBerg](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/paulrberg/32/14286_2.png) [@PaulRBerg](https://forum.openzeppelin.com/u/PaulRBerg)\
**Post date:** [August 30, 2021, 4:24pm UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723/3 "2021-08-30T16:24:10Z")

</div>

> [@frangio](#):
>
> we don’t have to preserve any Solidity invariants

Just double-checking that it is the [first four 32-byte slots](https://docs.soliditylang.org/en/v0.8.7/internals/layout_in_memory.html) reserved by Solidity, which you referred to? Or only the first two?

---

<div class="post-metadata">

**Author:** ![frangio](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/frangio/32/15251_2.png) [@frangio](https://forum.openzeppelin.com/u/frangio)\
**Post date:** [August 31, 2021, 2:29am UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723/4 "2021-08-31T02:29:04Z")

</div>

It would always be okay to write in the first two words. In the two words after that, it would generally be wrong, but in this case we're not returning to Solidity code, so we can write over the four.

---

<div class="post-metadata">

**Author:** ![aftermath.digital](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@aftermath.digital](https://forum.openzeppelin.com/u/aftermath.digital)\
**Post date:** [September 2, 2021, 8:43am UTC](https://forum.openzeppelin.com/t/upgradable-contract-assembler/9723/5 "2021-09-02T08:43:15Z")

</div>

Yo,

I should really have responded to this myself.

The way I understand it is that at present solidity only uses those first two slots to facilitate hashing, so you can always use them yourself as solidity itself will clobber and use them immediately should the need arise.

So if you store something in there and then perform some hashing functionality at the solidity level, you'll be clobbered, but you shouldn't ever impair the solidity run-time by using them.
