From 5539d144ed005123a2295e13f18f98bcc5b05d88 Mon Sep 17 00:00:00 2001 From: David Mak Date: Mon, 12 Aug 2024 17:46:47 +0800 Subject: [PATCH] [core] Add `CodeGenContext::build_in_bounds_gep_and_load` For safer accesses to `gep`-able values and faster fails. --- nac3core/src/codegen/expr.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 1b79c0b6..b63da080 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -82,6 +82,20 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { self.builder.build_load(gep, name.unwrap_or_default()).unwrap() } + /// Builds a sequence of `getelementptr inbounds` and `load` instructions which stores the value + /// of a struct field into an LLVM value. + /// + /// Any out-of-bounds accesses to `ptr` will return in a `poison` value. + pub fn build_in_bounds_gep_and_load( + &mut self, + ptr: PointerValue<'ctx>, + index: &[IntValue<'ctx>], + name: Option<&str>, + ) -> BasicValueEnum<'ctx> { + let gep = unsafe { self.builder.build_in_bounds_gep(ptr, index, "") }.unwrap(); + self.builder.build_load(gep, name.unwrap_or_default()).unwrap() + } + fn get_subst_key( &mut self, obj: Option,