From 13cedcaf4538dbe1e5c643ba1c5ee0eafa6cb795 Mon Sep 17 00:00:00 2001 From: Sergey Dmitriev Date: Mon, 25 Jan 2021 18:07:23 -0800 Subject: [PATCH] [llvm-link] Fix crash when materializing appending global This patch fixes llvm-link crash when materializing global variable with appending linkage and initializer that depends on another global with appending linkage. Reviewed By: tra Differential Revision: https://reviews.llvm.org/D95329 --- llvm/lib/Transforms/Utils/ValueMapper.cpp | 10 +++++++--- llvm/test/Linker/appending-global-crash.ll | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 llvm/test/Linker/appending-global-crash.ll diff --git a/llvm/lib/Transforms/Utils/ValueMapper.cpp b/llvm/lib/Transforms/Utils/ValueMapper.cpp index 8ab272e..930e0b7 100644 --- a/llvm/lib/Transforms/Utils/ValueMapper.cpp +++ b/llvm/lib/Transforms/Utils/ValueMapper.cpp @@ -819,11 +819,15 @@ void Mapper::flush() { break; case WorklistEntry::MapAppendingVar: { unsigned PrefixSize = AppendingInits.size() - E.AppendingGVNumNewMembers; + // mapAppendingVariable call can change AppendingInits if initalizer for + // the variable depends on another appending global, because of that inits + // need to be extracted and updated before the call. + SmallVector NewInits( + drop_begin(AppendingInits, PrefixSize)); + AppendingInits.resize(PrefixSize); mapAppendingVariable(*E.Data.AppendingGV.GV, E.Data.AppendingGV.InitPrefix, - E.AppendingGVIsOldCtorDtor, - makeArrayRef(AppendingInits).slice(PrefixSize)); - AppendingInits.resize(PrefixSize); + E.AppendingGVIsOldCtorDtor, makeArrayRef(NewInits)); break; } case WorklistEntry::MapGlobalIndirectSymbol: diff --git a/llvm/test/Linker/appending-global-crash.ll b/llvm/test/Linker/appending-global-crash.ll new file mode 100644 index 0000000..28987e2 --- /dev/null +++ b/llvm/test/Linker/appending-global-crash.ll @@ -0,0 +1,10 @@ +; RUN: llvm-link %s -S -o - | FileCheck %s + +; Check that llvm-link does not crash when materializing appending global with +; initializer depending on another appending global. + +; CHECK-DAG: @use = appending global [1 x i8*] [i8* bitcast ([1 x i8*]* @var to i8*)] +; CHECK-DAG: @var = appending global [1 x i8*] undef + +@use = appending global [1 x i8*] [i8* bitcast ([1 x i8*]* @var to i8*)] +@var = appending global [1 x i8*] undef -- 2.7.4