Questions tagged [assembly]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
17 views

Does stack persist through external calls?

Looking through the docs I found that a malicious caller can take the stack depth to ~1000 and then make a call which would fail due to stack depth. Based on that let's assume that stack indeed ...
0
votes
1answer
19 views

Recovering data from Bytes using Assembly

I have im my js file 3 variables: var stringData = "stingData"; var intNonce = 1234; var hexData = "0x65225648"; I want to concatenate then and send to my contract: var data = ...
0
votes
1answer
40 views

How to bit shift a bytes in solidity / assembly?

I can bit shift uint_n_ easily but I don't manage to bit shift a bytes memory myBytes. When doing myBytes << i I have a compile error and when using assembly I receive 0. See my MWE for assembly ...
0
votes
0answers
22 views

Understanding ERC721._checkOnERC721Received in openzeppelin library

When you use OpenZeppelin's ERC721 implementation, in most cases you call _safeMint function. _safeMint does some obvious basic operations except this one: require( _checkOnERC721Received(address(...
0
votes
1answer
28 views

Is the source code of any Ethereum smart necessarily contract open?

I thought that any smart contract had open code. However, from time to time I'll encounter smart contracts that have no publicly available Solidiy code, on etherscan. How can this be? Doesn't that ...
1
vote
1answer
48 views

Is it possible for the Solidity compiler to remove an external call?

I am experiencing a weird situation where it seems that the Solidity compiler (or optimizer) is removing an external call. I am performing an ERC20 token transfer on forked mainnet using Hardhat. From ...
1
vote
1answer
56 views

Cannot compile code with inline assembly using solc linux binaries

The code with inline assembly fails to compile with the solc binaries from soliditylang.org. The same code compiles without issue using Remix or solcjs. Because of this issue I cannot even compile the ...
0
votes
0answers
21 views

how to have base contracts have same variables

I have 2 base contracts. contract Base1 { function nice1() public { } } contract Base2 { function nice2() public { } } contract Derived is Base1, Base2 { function ...
1
vote
1answer
38 views

Q: `extcodesize` is non-zero after `selfdestruct`

I ran into an EVM edge case. Is it correct that extcodesize(addr) is not zero, after calling selfdestruct on addr in the same transaction? This behavior seems strange and I could not find any ...
1
vote
1answer
63 views

Is there a size limitation on create2 bytecode field?

I have the following method in my Solidity contract. function deploy(bytes memory bytecode, uint _salt) public payable returns (address){ address addr; assembly { addr := create2( ...
4
votes
1answer
164 views

How does Uniswap v3's logarithm library (TickMath.sol) work?

I want to calculate logarithm of a non-standard base at low gas cost. First thought is to use change of base rule with log2() fixed point methods mentioned in this thread. However Uniswap v3 core has ...
1
vote
1answer
52 views

What is the layout of returndata from returndatacopy opcode?

I am attempting to perform operations on returned data from delegatecall opcode using a similar familiar proxy pattern: assembly { let ptr := mload(0x40) // load empty memory address pointer ...
0
votes
1answer
179 views

Solidity shr opcode

What does this code mean? sender := shr(96, calldataload(sub(calldatasize(), 20))) Btw sender is an address variable.
1
vote
1answer
51 views

mstore8 assembly instruction makes call goes out of gas

My solidity code example: // SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.2; library TestLibrary { function sz_varint(uint256 i) internal pure returns (uint256) { uint256 ...
0
votes
0answers
61 views

Solidity inline-assembly: How to persist a value to storage and access from any scope within a function call

I'm writing a contract in inline-assembly. I have a parent function in my contract which interacts with an external contract. This in turn triggers a callback function in my contract. So multiple ...
0
votes
0answers
11 views

Sending arbitrary bytes to a function for later unpacking

I have a function I want to write that takes the smallest possible input data as an exercise: function test(bytes memory test_data ) public { // the bytes memory is 53 bytes, 16 + 16 + 20 +...
0
votes
1answer
80 views

How to load the first 4 bytes from a bytes calldata var?

It is common for calldata to be encoded as bytes calldata when developing contracts in Solidity. I need to extract the function selector from such a bytes var. What's the best way to load the first 4 ...
0
votes
0answers
29 views

What does "caller()" return in Yul when DELEGATECALL is used?

While browsing the Solidity docs, I noticed this instruction available in assembly: caller(): call sender (excluding delegatecall) What do the docs mean by "excluding delegatecall"? Is it ...
0
votes
1answer
89 views

How to store mapping with inline assembly?

I have a special needs, store the mapping with assembly code. thank you very for everyone who can response this question. for example: contract assemblyMappingTest{ mapping(address => uint) ...
0
votes
0answers
11 views

Writing ByteCode with as few opcodes as possible

So I'm very new to understanding assembly. So with this blog: Magic Number Ethernaut Writeup , I have learnt how to return a value from a contract irrespective of what function is called, with as few ...
0
votes
0answers
16 views

Is it possible to compile Yul with truffle?

I have a contract written in Yul that I want to compile. It is not clear to me if this is possible with truffle. Alternatively I tried compiling it in remix and then migrating the artifact file, but ...
2
votes
1answer
80 views

Disassemble "delete array" in solidity

am trying to learn about EVM bytes code, but faced this problem. First, this is the solidity code pragma solidity ^0.8.0; contract Assembly { function f() public { uint8[3] memory a = [1, ...
0
votes
1answer
87 views

How to read the current program counter (PC) in Solidity or Yul?

I was reading about the EVM dialect in the docs, and saw this: Instruction Explanation pc() Current position in code I went to Remix to try it out: function read_pc() external view returns (uint256 ...
1
vote
2answers
125 views

How can one access values returned via "return(0, returndatasize())"?

I was looking at the _delegate function in OpenZeppelin's Proxy.sol: /** * @dev Delegates the current call to `implementation`. * * This function does not return to its internall call site, it will ...
2
votes
1answer
149 views

How to read high-level storage variable from inline assembly?

This code does not compile: pragma solidity >=0.8.0; contract MyContract { uint256 public foo = 314; function myFunction() external view returns (uint256 result) { assembly { ...
1
vote
1answer
52 views

Understanding why this blob of assembly code adds 0x20 and 0x1f [duplicate]

The execute function defined in MakerDAO's DSProxy contains this blob of assembly code: assembly { let succeeded := delegatecall(sub(gas, 5000), _target, add(_data, 0x20), mload(_data), 0, 0) ...
1
vote
1answer
153 views

Storing immutable state in contract data

This post highlights how to use CREATE2 to implement smart contract state using contract data. However, the author also says: "if storage doesn’t need to change, you can do away with metamorphic ...
1
vote
0answers
24 views

transfer from one contract to another

// ContractA function nice(address to) public payable { require(_to.send(_value), ERROR_SEND_REVERTED); } I also have a contractB which was deployed with EIP-1167. Now, The problem is that when ...
0
votes
1answer
33 views

Assigning variables via assembler

I have a task to split a time string in UTC format (e.g. 2021-06-16T00:29:01Z) into a year, month and day. Since the UTC format assumes a constant string length, I wanted to assign values to variables ...
1
vote
1answer
100 views

How to make inline assembly keccak256 work like Solidity keccak256

Is there a way to use inline assembly keccak256(p, n) work like the Solidity keccak256(p)? I imagine there would be a neutral input I could give for either p or n to make that happen?
1
vote
1answer
260 views

Storage and memory layout of strings

How exactly are strings stored in storage? And, how does that differ from the way they're stored in memory when loaded from storage into a memory variable?
0
votes
0answers
18 views

Overriding memory with mstore()

Is it safe to override memory using mstore() ? In my case, I have an external function receiving bytes calldata data as argument, and I'm doing multiple calls in that same function. To do this, I ...
2
votes
1answer
119 views

How to enable the inline assembly IntelliSense for Solidity on VSCode

I have installed the solidity v0.0.125 extension from VSCode marketplace, but it seems doesn't support the inline assembly snippets by default. How to enable the feature or I need install more ...
1
vote
0answers
115 views

Multiple low level function calls using inline assembly in a smart contract not working

I'm trying to low-level call two separate contracts (B and C, with the same function signature) using inline assembly from my own contract (A) within the same function. The call to B modifies some ...
2
votes
1answer
229 views

Solidity Assembly basic callData return 4 bytes

I write a small test script to see how callData works in remix. The script is listed below: contract Assem { uint256 public count; function callData() public pure returns(bytes4) { ...
0
votes
0answers
20 views

Converting a for loop with arrays into assembly

here is a simple function for generating a sequence of numbers. I am wondering if anyone could help revise it to a version using Assembly for efficiency and perhaps speed. The relevant materials on-...
1
vote
1answer
30 views

Why 1225312 is the last addressable location in memory?

I was dabbling with inline assembly, and I wanted to discover the extent of addressable memory. After many trials, I found out that the only usable memory locations that could be accessed with OPCODEs ...
0
votes
0answers
67 views

Solidity: Converting a hex-string into bytes efficiently

I need to convert a string variable (passed to a function), containing a hex-string, into bytes in Solidity. In a way, this is the same as the following, just that the string variable will be passed ...
2
votes
0answers
121 views

Smart contract function signature only 1 byte

I have noticed some contract calls only contain the function signature in the 1st byte of the input data. For example: ...
0
votes
2answers
541 views

abi.encode and abi.decode arguments, including variable-sized arrays

I'm passing some arguments to a function and abi.encode()-ing them. Then I pass this data to another function where I abi.decode() it, but Im not entirely sure how to do it: Arguments I'm encoding: ...
1
vote
1answer
376 views

Understanding parameters of the STATICCALL opcode in Solidity inline assembly

I am new to inline assembly. I have tried understanding different opcodes, and understood quite a bit, but still not be able to correlate them in context to STATICCALL opcode. Kindly help me ...
1
vote
1answer
571 views

What is the benefit of using create2() to create a smart contract?

Why would someone use create2() inline assembly code to create a child contract in factory pattern as opposed to using newContract() to create an instance of "newContract"? What are the ...
2
votes
0answers
60 views

Struct getter and Setter using inline assembly in solidity

I have a storage slot defined bytes32 internal constant USER_STRUCT_SLOT = 0xd722976edd762e336e189ef10d2d356b8698dc8a2e03a97b539ab9d3974a1630; struct UserData { uint256 level; ...
1
vote
2answers
114 views

Compile Solidity to assembly keeping a correspondence with the source code

I would like to compile a contract written in Solidity (or even just the part relating to a single method) to study it and understand if I can optimize it. I already managed to get the bytecode as ...
1
vote
1answer
198 views

What does add(source, 32) do in solidity?

I understand what add() does. It used used in the assembly to perform additions. My question is why are we adding 32 here? What is the purpose of doing so? The full code is here
1
vote
0answers
27 views

Inline assembly function parameter has same name with local variable

contract Test { function f() public returns(uint r) { uint x = 100; assembly { function f(x) -> y { y := add(x, 1) } r := f(100) } } } Use ...
2
votes
0answers
29 views

Why use assembly in this operation?

I'm trying to understand why assembly for this sum is required? // 1 _structHash = keccak256( abi.encode( _blockhash, totalAddresses, ...
2
votes
0answers
13 views

Is it possible to call a higher-level function from within assembly?

Take the following code: contract Foo { function increment(uint256 x) external returns (uint256 result) { result = x + 1; } function increment_twice(uint256 x) external ...
1
vote
0answers
52 views

What is the neatest way to get hold of max uint256 in assembly?

Since Solidity v0.6.8, the neatest way to get hold of max uint256 in Solidity is this: uint256 foo = type(uint256).max; Is there something like that for Yul? I couldn't find anything in the docs. I ...
0
votes
1answer
77 views

Do the Solidity v0.8 overflow checks apply to assembly operations too?

Since Solidity v0.8, the compiler has checks for {over,under}flow by default for all arithmetic operations. Do these checks apply to assembly (yul) arithmetic operations too?

1
2 3 4 5