VLSI DV Interview Puzzles · All levels

Null Handle During Partial Deep Copy

This `do_copy` tries to deep-copy nested policy object, but simulation crashes with null-handle access. Identify root cause and fix.

Puzzle

Difficulty: Hard · Puzzle 4 of 6 · Topic: Handle and Copy Puzzles

This `do_copy` tries to deep-copy nested policy object, but simulation crashes with null-handle access. Identify root cause and fix.

Code

systemverilog
class qos extends uvm_object;
  `uvm_object_utils(qos)
  int prio;
  function new(string name="qos"); super.new(name); endfunction
endclass

class pkt extends uvm_sequence_item;
  `uvm_object_utils(pkt)
  qos policy;
  function new(string name="pkt");
    super.new(name);
  endfunction
  function void do_copy(uvm_object rhs);
    pkt r;
    if (!$cast(r, rhs)) return;
    policy.copy(r.policy);
  endfunction
endclass

Hint

Before calling `policy.copy(...)`, what must be true about destination handle allocation?

Step-by-step solution

diagram
1) `policy` is never constructed in `new`, so destination handle remains null.
2) `policy.copy(r.policy)` dereferences null and triggers runtime fatal/null-handle error.
3) Allocate destination before copy (`if (policy == null) policy = qos::type_id::create(...)`).
4) Also guard source null case and set destination null accordingly.

Answer

Answer: Crash occurs because `policy` is null in destination; deep-copy code dereferences an unallocated nested handle.

Why candidates get it wrong

Engineers remember to copy nested fields but forget destination object ownership/allocation lifecycle.

Interviewer follow-up

How would you implement symmetric null-handling so copy preserves null vs non-null state exactly?

Related topics