emsec / hal

HAL – The Hardware Analyzer
MIT License
530 stars 72 forks source link

Crash on assertion failure after starting the process of redrawing the design #561

Closed CuriousCodeCrafter closed 2 months ago

CuriousCodeCrafter commented 4 months ago

The program crashes when I try to create a module on a medium-sized netlist. In my original script, I run a script that gathers a list of all the gates that should be included in a new module that I want to create. The program runs correctly. Then, I run the following code:

...

m = netlist.createmodule(f"module{i}", netlist.get_top_module())

for gate_id in Gate_IDs: m.assign_gate(netlist.get_gate_by_id(gate_id))

without receiving any errors. However, the program automatically shuts down during the recalculation process to display the new design (with the new module) in the "Graph-Views" tab. It appears that although the code runs successfully, there's an issue with redrawing the design in the "Graph-Views" tab after the new module has been created. Both the HAL GUI and the terminal from which I launch the program crash and close automatically, leaving no diagnostic messages.

When I attempt to reopen the project, in the "Modules" section at the upper-left corner I can see that my module has been created and is listed within the top-module hierarchy. However, when the program is about 8% through drawing the design in the "Graph-Views" tab, the program crashes again.

Do you know what can be happening?

Thanks in advance.

RenWal commented 4 months ago

There is definitely something broken with the module renderer right now, but I couldn't yet pinpoint exactly what it is.

What I found so far:

It appears that the layouter is bailing out with an assertion failure:

Fatal: ASSERT: "iy1 == iy0 + 1" in file hal/plugins/gui/src/graph_widget/layouters/graph_layouter.cpp, line 781 (hal/plugins/gui/src/graph_widget/layouters/graph_layouter.cpp:781, )

I have built a test case for this which reliably crashes HAL. I'll clean that up a bit and upload it later. Some more familiar with the intricacies of the graph layouter may want to triage based on that.

Also, I found a possibly related issue:

RenWal commented 4 months ago

Executing the script in the following HAL project reliably crashes HAL on an assertion failure in GraphLayouter::calculateGateOffsets(). hal_project_toy_cipher.zip

Just before the crash, mCoordY contains the keys 0,2. Hence, the iy1 == iy0 + 1 assertion in the iteration over this map fails. I didn't fully figure out how the mCoordY map is filled in the first place, perhaps someone else can chime in.

joern274 commented 4 months ago

Thanks @RenWal for submitting this great example to reproduce the problem.

The solution would be to remove the ASSERT ;-)

A little background : for fast net routing a virtual grid is used where the x-position of each node corresponds to the x-position where the node has been placed. The y-position however gets calculated as node-y-position*2 thus adding intermediate positions for horizontal wires.

The exact location (mCoordY) gets calculated when doing the net routing. The ASSERT should assure that this step has been done before calculating gate positions and their offsets.

However - in this special case there are only unconnected modules so that the net routing did not produce any results. After removing the ASSERT the special case gets rendered without error.

CuriousCodeCrafter commented 4 months ago

Thank you very much for the help. I have seen that you have updated the corresponding file in the repository. After reinstalling HAL and importing the project where the error previously occurred, I can confirm that the error has been corrected and the modules are now created correctly.

joern274 commented 4 months ago

@CuriousCodeCrafter Thanks for feed. I'll change the issue status to "fixed".