graalvm / sulong

Obsolete repository. Moved to oracle/graal.
Other
628 stars 63 forks source link

Fix memory issue concerning the "insertvalue" instruction #839

Closed anatol1234 closed 6 years ago

anatol1234 commented 6 years ago

This PR should fix a bug in the implementation of the insertvalue instruction which causes segmentation faults under certain conditions. More specifically, this bug concerns the allocation of the result aggregate. The LLVM reference (insertvalue instruction) does not indicate that insertvalue should behave like alloca with regards to its return value. Here is a minimal program which reproduces the observed issue (target: Ubuntu 64-bit):

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @main()  {
start:
  %agg = alloca { i64 }
  br label %bb1
bb1:
  %0 = load { i64 }, { i64 }* %agg
  %1 = insertvalue { i64 } %0, i64 0, 0
  br label %bb1
  ret i32 0
}

Executing this program on Sulong yields a segfault after a few seconds (after several loop iterations). I suspect what happens is basically a stack overflow.

Note: Replacing %0 with undef in insertvalue in the IR shown above will still cause a segfault with the fix enabled. However, this is a different issue concerning undef and struct literals. This other issue is not restricted to the insertvalue instruction, but also occurs, when undef or struct literals are used on extractvalue or store instructions.