From 621f589e022c10e223120cb37ebda542efa7c3ba Mon Sep 17 00:00:00 2001 From: mjoekhan Date: Tue, 21 Jul 2026 13:18:16 +0500 Subject: [PATCH] VVP: initialize thread flags/words and codespace chunks vthread_new only cleared flags[4..7] while codegen uses higher indices; zero all flags and word registers. Value-initialize codespace chunks so unused opcode slots are not indeterminate. --- vvp/codes.cc | 5 +++-- vvp/vthread.cc | 8 +++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/vvp/codes.cc b/vvp/codes.cc index bb8bc8b27..52ed21e80 100644 --- a/vvp/codes.cc +++ b/vvp/codes.cc @@ -46,7 +46,8 @@ static unsigned current_within_chunk = 0; void codespace_init(void) { assert(current_chunk == 0); - first_chunk = new struct vvp_code_s [code_chunk_size]; + /* Value-initialize so unused slots are not random (opcode NULL). */ + first_chunk = new struct vvp_code_s [code_chunk_size](); current_chunk = first_chunk; current_chunk[0].opcode = &of_ZOMBIE; @@ -64,7 +65,7 @@ vvp_code_t codespace_next(void) { if (current_within_chunk == (code_chunk_size-1)) { current_chunk[code_chunk_size-1].cptr - = new struct vvp_code_s [code_chunk_size]; + = new struct vvp_code_s [code_chunk_size](); current_chunk = current_chunk[code_chunk_size-1].cptr; /* Put a link opcode on the end of the chunk. */ diff --git a/vvp/vthread.cc b/vvp/vthread.cc index 8170c2d17..aefde9e02 100644 --- a/vvp/vthread.cc +++ b/vvp/vthread.cc @@ -770,9 +770,15 @@ vthread_t vthread_new(vvp_code_t pc, __vpiScope*scope) thr->flags[1] = BIT4_1; thr->flags[2] = BIT4_X; thr->flags[3] = BIT4_Z; - for (int idx = 4 ; idx < 8 ; idx += 1) + /* Initialize the remaining flags. Older code only cleared 4..7; the + codegen freely uses higher flag indices, so leave nothing + uninitialized for tools like valgrind. */ + for (int idx = 4 ; idx < vthread_s::FLAGS_COUNT ; idx += 1) thr->flags[idx] = BIT4_X; + for (int idx = 0 ; idx < vthread_s::WORDS_COUNT ; idx += 1) + thr->words[idx].w_uint = 0; + scope->threads .insert(thr); return thr; }