Check ty is sized in build_malloc (fixes
@@ -12,7 +12,7 @@ use crate::basic_block::BasicBlock;
use crate::values::{AggregateValue, AggregateValueEnum, AsValueRef, BasicValue, BasicValueEnum, PhiValue, FunctionValue, IntValue, PointerValue, VectorValue, InstructionValue, GlobalValue, IntMathValue, FloatMathValue, PointerMathValue, InstructionOpcode, CallSiteValue};
#[llvm_versions(3.9..=latest)]
use crate::values::StructValue;
-use crate::types::{AsTypeRef, BasicType, IntMathType, FloatMathType, PointerType, PointerMathType};
+use crate::types::{AsTypeRef, BasicType, BasicTypeEnum, IntMathType, FloatMathType, PointerType, PointerMathType};
use std::ffi::CString;
use std::marker::PhantomData;
@@ -349,6 +349,19 @@ impl<'ctx> Builder<'ctx> {
// TODOC: Heap allocation
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> PointerValue<'ctx> {
+ // LLVMBulidMalloc segfaults if ty is unsized
+ let is_sized = match ty.as_basic_type_enum() {
+ BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
+ BasicTypeEnum::FloatType(ty) => ty.is_sized(),
+ BasicTypeEnum::IntType(ty) => ty.is_sized(),
+ BasicTypeEnum::PointerType(ty) => ty.is_sized(),
+ BasicTypeEnum::StructType(ty) => ty.is_sized(),
+ BasicTypeEnum::VectorType(ty) => ty.is_sized(),
+ };
+ if !is_sized {
+ panic!("Cannot build malloc call for an unsized type {:?}", ty);
+ }
+
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
let value = unsafe {
