研究evmc项目,记录如下:
首先当然是编译运行
1、修改CMakeLists.txt文件,修改如下:
option(EVMC_TESTING "Build EVMC tests and test tools" ON)
这里直接打开测试用例,确保后续研究代码方便
2、编译
老套的方式
mkdir build
cd build
cmake ..
make
如果下载失败需要在命令行窗口执行:
export http_proxy=http://yourproxy/:8080
export https_proxy=http://yourproxy/:8080
如果在mingw下编译,需要使用:
cmake -G "MinGW Makefiles" ..
如果在vs2015下编译,需要使用:
cmake -G "Visual Studio 14 2015 Win64" ..
3、运行demo
cd evmc/build/examples
./evmc-example
Execution result:
EVM execution failure: 1
Segmentation fault (core dumped)
执行错误
4、运行测试用例
cd evmc/build/test
./evmc-test
./evmc-vmtester
其中evmc-test执行成功,而evmc-vmtester执行异常
5、检查demo执行失败的原因
5.1、增加调试信息
修改evmc/examples/CMakeLists.txt文件,在尾部增加
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
删除build目录后重新执行第二步编译
5.2、gdb调试
cd evmc/build/examples
gdb
file evmc-example
b example.c:16
r
后面就单步走,直到
evmc_release_result(&result); 函数出错
使用q退出gdb,然后再gdb一次,进入这个函数
gdb
file evmc-example
b example.c:52
r
s
p result->release
$1 = (evmc_release_result_fn) 0x0
打印发现result->release函数指针为空,因此这里crash
从前面跟踪看到还有个原因是evmc_execute执行失败导致
先修改文件evmc/include/evmc/helpers.h:116
static inline void evmc_release_result(struct evmc_result* result)
{
if(result->release != NULL) {
result->release(result);
}
}
直接在目录evmc/build/examples执行make
然后执行
./evmc-example
Execution result:
EVM execution failure: 1
5.3、继续解决上面这个错误
gdb过程不详述,这里只记录修改的代码
调试发现example.c里面是有sizeof求字符串长度得到长度9,而example_vm.c里面是用strlen求长度得到长度8,因此匹配错误执行失败
我们修改example.c
#include <string.h>
...
const size_t code_size = sizeof(code) - 1;
编译后执行:
./evmc-example
Execution result:
Gas used: 200000
Gas left: 0
Output size: 20
Output: 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
这次demo就执行成功了
6、evmc-vmtester执行异常
研究代码发现这个程序执行需要指定vm虚拟机的库,我们参考hera git编译出libhera.so,并把so复制到当前目录下,执行
./evmc-vmtester ./libhera.so
Testing ./libhera.so
[==========] Running 9 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 9 tests from evmc_vm_test
[ RUN ] evmc_vm_test.abi_version_match
[ OK ] evmc_vm_test.abi_version_match (0 ms)
[ RUN ] evmc_vm_test.execute
Executing message in Hera
InternalError: Only Byzantium supported.
[ OK ] evmc_vm_test.execute (0 ms)
[ RUN ] evmc_vm_test.set_option_unknown_name
[ OK ] evmc_vm_test.set_option_unknown_name (0 ms)
[ RUN ] evmc_vm_test.set_option_empty_value
[ OK ] evmc_vm_test.set_option_empty_value (0 ms)
[ RUN ] evmc_vm_test.set_option_unknown_value
[ OK ] evmc_vm_test.set_option_unknown_value (0 ms)
[ RUN ] evmc_vm_test.name
[ OK ] evmc_vm_test.name (0 ms)
[ RUN ] evmc_vm_test.version
[ OK ] evmc_vm_test.version (0 ms)
[ RUN ] evmc_vm_test.set_tracer
[ OK ] evmc_vm_test.set_tracer (0 ms)
[ RUN ] evmc_vm_test.capabilities
[ OK ] evmc_vm_test.capabilities (0 ms)
[----------] 9 tests from evmc_vm_test (0 ms total)
[----------] Global test environment tear-down
[==========] 9 tests from 1 test case ran. (0 ms total)
[ PASSED ] 9 tests.
执行成功
网友评论