Scribble
Search…
Instrumented Code
While our intention is that you should not have to look at or understand instrumented code, scribble is still beta software and bugs will happen. In such cases it may be useful for you to understand how Scribble generates instrumented code. For this we will look at two examples.

Function Annotations

To understand function instrumentation lets look at the following (slightly contrived) example:
1
contract Foo {
2
int x;
3
/// #if_succeeds {:msg "test"} x == old(x) + y;
4
function add(int y) public {
5
require(y > 0);
6
x += y;
7
}
8
}
Copied!
The instrumented code generated would look like this:
1
contract Foo {
2
event AssertionFailed(string message);
3
4
struct vars0 {
5
int256 old_0;
6
}
7
8
int internal x;
9
10
function add(int y) public {
11
vars0 memory _v;
12
_v.old_0 = x;
13
_original_Foo_add(y);
14
if ((!((x == (_v.old_0 + y))))) {
15
emit AssertionFailed("0: test");
16
assert(false);
17
}
18
}
19
20
function _original_Foo_add(int y) private {
21
require((y > 0));
22
x += y;
23
}
24
}
Copied!
There are several important things to note:
  1. 1.
    Scribble interposes on functions by renaming them, making them private, and adding a new stub with the same signature as the original function. For example, note that the original code from the add function now lives in the new private function _original_Foo_add (lines 20-23).
  2. 2.
    In the generated wrapper add() we invoke the renamed original function, and check the if_succeeds annotation after the call (lines 14-17).
  3. 3.
    The values of old expressions are evaluated before the call (line 12) and require a temporary variable. However, instead of just adding multiple temporary variables, we group all of the temporary variables needed inside of a single struct (vars0) defined on lines 4-6. This is done to reduce the extra stack space used by instrumented code.
  4. 4.
    Which custom property failed is signaled by emitting the AssertionFailed event (line 15).

Property Maps

When the above property is violated, a message with the string 0: test (line 15) is emitted. The test part is the original user-readable label in the annotation. 0 is an internally assigned id. Scribble assigns such an id for each annotation we write. It is possible to hint Scribble to also output a metadata with map from ids to the original property. See more about the metadata format in section JSON output specification.
There are two ways to get the instrumentation metadata with Scribble:
  • Using --output-mode json (or -m json) command line option as part of JSON output in instrumentationMetadata key.
  • Using --instrumentation-metadata-file path/to/meta-file.json command line option to get it in a standalone JSON file at specified path.

Contract Annotations

For contract invariants there are several additional changes. Let's consider this slightly different example:
1
/// #invariant {:msg "foo"} x>=1;
2
contract Foo {
3
int x;
4
constructor() public {
5
x = 1;
6
}
7
8
function add(uint x) public {
9
x += 1;
10
}
11
}
Copied!
Now the emitted code is quite a bit more involved:
1
/// Utility contract holding a stack counter
2
contract __scribble_ReentrancyUtils {
3
bool __scribble_out_of_contract = true;
4
}
5
6
contract Foo is __scribble_ReentrancyUtils {
7
event AssertionFailed(string message);
8
9
struct vars1 {
10
bool __scribble_check_invs_at_end;
11
}
12
13
int internal x;
14
15
constructor() public {
16
__scribble_out_of_contract = false;
17
x = 1;
18
__scribble_check_state_invariants();
19
__scribble_out_of_contract = true;
20
}
21
22
function add(uint x) public {
23
vars1 memory _v;
24
_v.__scribble_check_invs_at_end = __scribble_out_of_contract;
25
__scribble_out_of_contract = false;
26
_original_Foo_add(x);
27
if (_v.__scribble_check_invs_at_end) __scribble_check_state_invariants();
28
__scribble_out_of_contract = _v.__scribble_check_invs_at_end;
29
}
30
31
function _original_Foo_add(uint x) private {
32
x += 1;
33
}
34
35
/// Check only the current contract's state invariants
36
function __scribble_Foo_check_state_invariants_internal() internal {
37
if ((!((x >= 1)))) {
38
emit AssertionFailed("0: foo");
39
assert(false);
40
}
41
}
42
43
/// Check the state invariant for the current contract and all its bases
44
function __scribble_check_state_invariants() virtual internal {
45
__scribble_Foo_check_state_invariants_internal();
46
}
47
}
Copied!
Again there are several changes:
  1. 1.
    A new helper contract - __scribble_ReentrancyUtils(lines 2-4) was added. This contract was also added as a base contract of Foo. (line 6). __scribble_ReentrancyUtils contains a single boolean property that is used for tracking whether we just entered the contract at the start of public functions (remember that public functions can be called both internally and externally).
  2. 2.
    A new function __scribble_Foo_check_state_invariants_internal was added (lines 36-41) which actually checks whether the invariant is true. It is only called indirectly, by __scribble_check_state_invariantswhich is itself called by the wrapped functions.
  3. 3.
    The publicadd function (lines 22-33) was wrapped similarly to functions in the previous section. The wrapper (lines 22-29) invokes the __scribble_check_state_invariants function at the end of execution (line 27) to check whether the invariant is preserved by the call to add.
  4. 4.
    The check on line 27 is done conditionally. This is a twist to make sure we are not checking contract invariants inside of public functions when the public functions are called internally. If add was declared external then the call on line 27 would be unconditional.
  5. 5.
    All external, public functions in the contract (and the constructor) receive additional code at their beginning and end that manipulates the __scribble_out_of_contract state variable defined in the contract. This is used to keep track of whether we are just entering the contract or not. The logic is particularly involved for public functions. Note that in the public function add , whether we check the invariants at the end depends on the value of __scribble_out_of_contract at the start of the function (which was stored in _v.__scrible_check_invs_at_end on line 24). This reflects the intuition that we want to check contract invariants only at the end of external calls, not internal calls (for more details on contract invariants see Contract Invariants).
  6. 6.
    Note that the contract invariants are also checked at the end of the constructor (line 18).
Last modified 6mo ago