csail-csg / pyverilator

Python wrapper for verilator model
MIT License
78 stars 34 forks source link

Crash on start_vcd_trace #7

Closed kammoh closed 4 years ago

kammoh commented 4 years ago

On both linux and mac (python 3.7 and 3.8) all examples seg fault. This happens upon calling start_vcd_trace. I was able to trace down the crash to where the init callbacks in top->trace(tfp,99) is called in the C code. The crash happens in Top::traceInit: the userthis pointer seems to be corrupt! Unfortunately I was not able to get any further with pinpointing the cause.

Verilator version: master PyVerilator: master

acw1251 commented 4 years ago

Thanks for the report. Unfortunately I'm not able to reproduce the bug you've seen (I used python3.7 in ubuntu). Can you try running the tests in pyverilator/tests/test_pyverilator.py? You can do this by running either python3 -m pytest or python3 -m unittest in the pyverilator/tests folder. pytest should give easier to read output.

I tried running the examples using python3.7 (in the past I only used 3.5 and 3.6), and ran into issues because of the tcl communication being done with gtkwave (I think this was due to running this over SSH). I also ran into an issue with make failing. I think that was due to having a dirty build directory with some files that were created by an older version of pyverilator. If I removed the lines connecting to gtkwave and removed the build directory, the examples seemed to work (definitely no segfault).

Maybe you can also try taking simple_example.py and remove the three lines related to gtkwave (the call to sim.start_gtkwave and the two calls to sim.send_to_gtkwave)?

kammoh commented 4 years ago

Thank you for the quick reply! As I mentioned, this problem doesn't seem to have anything to do with either gtkwave or tcl wrappers. In addition to all the examples, the crash happens if I try to just generate a vcd trace (and this is what I actually need right now). I'm not sure if I'm doing the right thing here, but this is what I do:

sim = pyverilator.PyVerilator.build("top.v")
sim.start_vcd_trace('top.vcd') # <- seg-faults

Commenting out the lines with sim.*_gtkwave stops the crash, and I assume it's because there's no call to sim.start_gtkwave or eventually top->trace. I also tried verilator stable as well as pyverilator versions as far back as 0.1.0-32-g3dc31d6.

Running python -m pytest in pyverilator/tests:

================================ test session starts =================================
platform linux -- Python 3.8.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /home/kamyar/src/pyverilator
collected 7 items                                                                    

test_pyverilator.py Fatal Python error: Segmentation fault

Current thread 0x00007f97592a5740 (most recent call first):
  File "/home/kamyar/src/pyverilator/pyverilator/pyverilator.py", line 705 in start_vcd_trace
  File "/home/kamyar/src/pyverilator/pyverilator/tests/test_pyverilator.py", line 43 in test_pyverilator
  File "/home/kamyar/.local/lib/python3.8/site-packages/_pytest/python.py", line 184 in pytest_pyfunc_call
...
  File "/usr/lib/python3.8/runpy.py", line 86 in _run_code
  File "/usr/lib/python3.8/runpy.py", line 193 in _run_module_as_main
Segmentation fault (core dumped)

Python versions I tested inlucde : 3.8.2 and 3.7.7 both on Arch linux and macOS.

kammoh commented 4 years ago

Ok I was finally able to test on an Ubuntu 18.04 machine and it works without an issue. Both python 3.6.9 and 3.8.2. gcc version is 7.5.0. Could this be a compiler option/compatibility issue? using gcc 9.2.1 to compile both verilator and verilated binaries on the ubuntu machine also works fine. But I none of my development machines or servers are ubuntu :(

acw1251 commented 4 years ago

I took a look in the code for things that could result in a segfault (specifically the python/c interface), and a few things stood out. I fixed all the issues I saw in 4d58d91. Can you try this out to see if you still get the segfault?

In the process of trying to reproduce the segfault, I updated verilator to master, and now it looks like some of the tests fail because of it. I'm going to look into what's going on with it.

kammoh commented 4 years ago

Thank you! This indeed solves the crash, and the vcd file is now correctly generated. As you mentioned 5 of the tests (out of 14) fail on my machine, and the gtkwave example does not actually work. One recent change I noticed in verilator is the way internal signal namespace is managed in the verilated cpp. I'm not sure if any of the failing tests depend on the previous behaviour.

acw1251 commented 4 years ago

That's good news that the segfault doesn't happen any more. I've made issue #8 for the new failing tests. If you revert back to an older version of verilator, that should be fixed as well.

acw1251 commented 4 years ago

Just a heads up, @kammoh , If you revert to verilator version v4.020 or before, you can get around issue #8.