VLSI DV Interview Puzzles · All levels
Shallow Copy in Nested Payload
You clone a transaction before pushing into an expected queue. Predict the print and name the exact ownership bug.
Puzzle
Difficulty: Easy · Puzzle 1 of 6 · Topic: Handle and Copy Puzzles
You clone a transaction before pushing into an expected queue. Predict the print and name the exact ownership bug.
Code
class payload;
rand bit [7:0] data[];
function new();
data = new[2];
endfunction
endclass
class txn extends uvm_sequence_item;
`uvm_object_utils(txn)
rand bit [31:0] addr;
payload pld;
function new(string name = "txn");
super.new(name);
pld = new();
endfunction
function void do_copy(uvm_object rhs);
txn r;
if (!$cast(r, rhs)) return;
super.do_copy(rhs);
addr = r.addr;
pld = r.pld;
endfunction
endclass
initial begin
txn a = txn::type_id::create("a");
txn b = txn::type_id::create("b");
a.pld.data[0] = 8'h11;
b.copy(a);
a.pld.data[0] = 8'hEE;
$display("a=%0h b=%0h", a.pld.data[0], b.pld.data[0]);
endHint
Focus on what `pld = r.pld` copies: object contents or just the handle value.
Step-by-step solution
1) `b.copy(a)` calls `do_copy`, which assigns `b.pld` to the same handle as `a.pld`.
2) After copy, both `a.pld` and `b.pld` refer to one payload object.
3) Updating `a.pld.data[0]` updates shared storage, so `b.pld.data[0]` changes too.
4) Fix by allocating/cloning payload inside `do_copy` (`pld = payload::type_id::create(...)` or `pld = r.pld.clone()`).Answer
Answer: It prints `a=ee b=ee`; the bug is shallow copy of nested handle `pld` inside `do_copy`.
Why candidates get it wrong
Calling `copy()` does not guarantee deep copy; UVM only performs what your override explicitly implements.
Interviewer follow-up
Show a `do_compare` snippet that can detect aliasing by checking handle identity in addition to field equality.