> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/web3-security-research/foundry/ffi.md).

# FFI

{% embed url="<https://youtu.be/DTyn5ShI2vQ>" %}
FFI
{% endembed %}

## Setup

Test file:

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "forge-std/Test.sol";
import "forge-std/console.sol";

// forge test --match-path test/FFI.t.sol --ffi -vvvv

contract FFITest is Test {
    function testFFI() public {
        string[] memory cmds = new string[](2);
        cmds[0] = "cat";
        cmds[1] = "ffi_test.txt";
        bytes memory res = vm.ffi(cmds);
        console.log(string(res));
    }
}
```

## vm.ffi()

`vm.ffi()` lets you execute arbitrary command. For example, if we want to cat a file:

```solidity
string[] memory cmds = new string[](2);
cmds[0] = "cat";
cmds[1] = "ffi_test.txt";
bytes memory res = vm.ffi(cmds);
console.log(string(res));
```

To run FFI, we must specify it in the terminal when doing `forge test`:

```bash
forge test --match-path test/FFI.t.sol --ffi -vvv
```
