-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Add Memory
utility library
#5189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
9eb5f1c
Add memory utils
ernestognw 2d397f4
Fix tests upgradeable
ernestognw 2a0fb7e
Add docs
ernestognw a7e61c3
Make use of the library
ernestognw 1aae8bb
Update docs/modules/ROOT/pages/utilities.adoc
ernestognw 1b2679a
Merge branch 'master' into utils/memory
Amxx d514606
fix tests
Amxx 14fa04e
Update contracts/utils/Memory.sol
ernestognw d0d55fc
Update contracts/utils/Memory.sol
arr00 608e3cd
Merge branch 'master' into utils/memory
ernestognw ac92bb4
up
ernestognw 6094bb7
Merge branch 'master' into utils/memory
ernestognw 6bb96d5
WIP: Add more Memory functions
ernestognw 860e5a8
up
ernestognw ecdb768
revert
ernestognw 95907aa
Update docs
ernestognw 124ccee
Nit
ernestognw c3237df
Finish fuzz tests and FV
ernestognw 27f0a9b
up
ernestognw e67e8b4
up
ernestognw 3847050
Remove extra functions
ernestognw 4fd1947
Update docs
ernestognw c4e0375
up
ernestognw 340e94c
Update Memory.sol
ernestognw f03d149
Update docs/modules/ROOT/pages/utilities.adoc
ernestognw e5e9103
Merge branch 'master' into utils/memory
Amxx 05a2890
minimize
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Memory`: Add library with utilities to manipulate memory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
/** | ||
* @dev Utilities to manipulate memory. | ||
* | ||
* Memory is a contiguous and dynamic byte array in which Solidity stores non-primitive types. | ||
* This library provides functions to manipulate pointers to this dynamic array. | ||
* | ||
* WARNING: When manipulating memory, make sure to follow the Solidity documentation | ||
* guidelines for https://docs.soliditylang.org/en/v0.8.20/assembly.html#memory-safety[Memory Safety]. | ||
*/ | ||
library Memory { | ||
type Pointer is bytes32; | ||
|
||
/// @dev Returns a `Pointer` to the current free `Pointer`. | ||
function getFreeMemoryPointer() internal pure returns (Pointer ptr) { | ||
assembly ("memory-safe") { | ||
ptr := mload(0x40) | ||
} | ||
} | ||
|
||
/** | ||
* @dev Sets the free `Pointer` to a specific value. | ||
* | ||
* WARNING: Everything after the pointer may be overwritten. | ||
**/ | ||
function setFreeMemoryPointer(Pointer ptr) internal pure { | ||
assembly ("memory-safe") { | ||
mstore(0x40, ptr) | ||
} | ||
} | ||
|
||
/// @dev `Pointer` to `bytes32`. Expects a pointer to a properly ABI-encoded `bytes` object. | ||
arr00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function asBytes32(Pointer ptr) internal pure returns (bytes32) { | ||
return Pointer.unwrap(ptr); | ||
} | ||
|
||
/// @dev `bytes32` to `Pointer`. Expects a pointer to a properly ABI-encoded `bytes` object. | ||
function asPointer(bytes32 value) internal pure returns (Pointer) { | ||
return Pointer.wrap(value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {Memory} from "@openzeppelin/contracts/utils/Memory.sol"; | ||
|
||
contract MemoryTest is Test { | ||
using Memory for *; | ||
|
||
// - first 0x80 bytes are reserved (scratch + FMP + zero) | ||
uint256 constant START_PTR = 0x80; | ||
// - moving the free memory pointer to far causes OOG errors | ||
uint256 constant END_PTR = type(uint24).max; | ||
|
||
function testGetsetFreeMemoryPointer(uint256 seed) public pure { | ||
bytes32 ptr = bytes32(bound(seed, START_PTR, END_PTR)); | ||
ptr.asPointer().setFreeMemoryPointer(); | ||
assertEq(Memory.getFreeMemoryPointer().asBytes32(), ptr); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
||
async function fixture() { | ||
const mock = await ethers.deployContract('$Memory'); | ||
return { mock }; | ||
} | ||
|
||
describe('Memory', function () { | ||
beforeEach(async function () { | ||
Object.assign(this, await loadFixture(fixture)); | ||
}); | ||
|
||
describe('free pointer', function () { | ||
it('sets free memory pointer', async function () { | ||
const ptr = ethers.toBeHex(0xa0, 32); | ||
await expect(this.mock.$setFreeMemoryPointer(ptr)).to.not.be.reverted; | ||
}); | ||
|
||
it('gets free memory pointer', async function () { | ||
await expect(this.mock.$getFreeMemoryPointer()).to.eventually.equal( | ||
ethers.toBeHex(0x80, 32), // Default pointer | ||
); | ||
}); | ||
}); | ||
|
||
describe('pointer conversions', function () { | ||
it('asBytes32', async function () { | ||
const ptr = ethers.toBeHex('0x1234', 32); | ||
await expect(this.mock.$asBytes32(ptr)).to.eventually.equal(ptr); | ||
}); | ||
|
||
it('asPointer', async function () { | ||
const ptr = ethers.toBeHex('0x1234', 32); | ||
await expect(this.mock.$asPointer(ptr)).to.eventually.equal(ptr); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.