question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when instrumenting an "if" in an assembly block

See original GitHub issue

We’re getting an error when trying to instrument one of our contracts that has some assembly:

There was a problem instrumenting ./coverageEnv/contracts/MyContract.sol: SyntaxError: Expected "=:", "for", "function", "hex", "let", "return", "switch", "{", "}", comment, end of line, number, string, or whitespace but "i" found. Line: 29, Column: 7

It fails in the if statement at the bottom.

Any idea why?

  /// @notice Checks if a given address is whitelisted
  /// @return true if address is whitelisted, false if not
  function isWhitelisted
  (
    address _address
  )
    public
    view
    returns (bool _isWhitelisted)
  {
    bytes4 _signature = bytes4(keccak256("whitelisted(address)"));
    address _whitelistContract = getContractAddress("Whitelist");

    assembly {
      let _pointer := mload(0x40)  // Set _pointer to free memory pointer
      mstore(_pointer, _signature) // Store _signature at _pointer
      mstore(add(_pointer, 0x04), _address) // Store _address at _pointer. Offset by 4 bytes for previously stored _signature

      // staticcall(g, a, in, insize, out, outsize) => returns 0 on error, 1 on success
      let result := staticcall(
        gas,                // g = gas: whatever was passed already
        _whitelistContract, // a = address: _whitelist address assigned from getContractAddress()
        _pointer,           // in = mem in  mem[in..(in+insize): set to _pointer pointer
        0x24,               // insize = mem insize  mem[in..(in+insize): size of signature (bytes4) + bytes32 = 0x24
        _pointer,           // out = mem out  mem[out..(out+outsize): output assigned to this storage address
        0x20                // outsize = mem outsize  mem[out..(out+outsize): output should be 32byte slot (bool size = 0x01 < slot size 0x20)
      )

      // Revert if not successful
      if iszero(result) {
        revert(0, 0)
      }

      _isWhitelisted := mload(_pointer) // Assign result to returned value
      mstore(0x40, add(_pointer, 0x24)) // Advance free memory pointer by largest _pointer size
    }
  }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
0agecommented, Oct 10, 2018

If you have the flexibility to change the inline assembly a bit, a workaround is to use a switch statement instead. So:

      if iszero(result) {
        revert(0, 0)
      }

becomes:

      switch iszero(result)
      case 1 {
        revert(0, 0)
      }
6reactions
cgeweckecommented, Sep 7, 2018

@chapati23 It looks like the parser needs to be updated to handle some of the newer assembly syntax. Will fix this weekend, thanks for reporting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use ASM lib in java to instrument an if-else block?
I'm instrumenting this if-else block at the end of each called method. That is, I override visitInsn method. I check if the Opcode...
Read more >
Instrumentor fails on assembly code syntax: ERROR - IBM
This technote explains how to resolve a Preprocessing error. This error might occur, when you instrument code in IBM Rational Test RealTime.
Read more >
Add distributed tracing instrumentation - .NET - Microsoft Learn
If an assembly is adding instrumentation for code in a second, independent assembly, the name should be based on the assembly that defines ......
Read more >
Select.NET constructor assembly error - NI Community
Hello, I am trying to select a .dll to use in a >NET constructor. I keep getting an error and i have no...
Read more >
Using Valgrind to detect undefined value errors with bit ...
Memcheck is implemented using the dynamic binary instrumentation framework ... Third, it checks that memory blocks supplied as arguments to functions like ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found