From 70f5d7a1c99680f93eb81c6c67fb7d1a0b5cef5e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 25 Nov 2020 19:50:48 +0000 Subject: [PATCH] Enforce zeroing through memset to ensure padding is zeroed with llvm api --- src/llvm_backend.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 2d5e14c68..2f84ce263 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2961,11 +2961,29 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p LLVMTypeRef llvm_type = lb_type(p->module, type); LLVMValueRef ptr = LLVMBuildAlloca(p->builder, llvm_type, name); - LLVMSetAlignment(ptr, 16); // TODO(bill): Make this configurable + + // unsigned alignment = 16; // TODO(bill): Make this configurable + unsigned alignment = cast(unsigned)lb_alignof(llvm_type); + LLVMSetAlignment(ptr, alignment); LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block); if (zero_init) { - LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr); + LLVMTypeKind kind = LLVMGetTypeKind(llvm_type); + + switch (kind) { + case LLVMStructTypeKind: + case LLVMArrayTypeKind: + { + // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too + LLVMTypeRef type_i8 = LLVMInt8TypeInContext(p->module->ctx); + LLVMTypeRef type_i32 = LLVMInt32TypeInContext(p->module->ctx); + i32 sz = cast(i32)type_size_of(type); + LLVMBuildMemSet(p->builder, ptr, LLVMConstNull(type_i8), LLVMConstInt(type_i32, sz, false), alignment); + } + break; + default: + LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr); + } } lbValue val = {};