Skip to content

Commit 509d9e8

Browse files
[llvm-dis] Fix non-deterministic disassembly across multiple inputs
Prior to this patch, the LLVMContext was shared across inputs to llvm-dis. Consequently, NamedStructTypes was shared across inputs, which impacts StructType::setName - if a name was reused across inputs, it would get renamed during construction of the struct type, leading to tricky to diagnose confusion.
1 parent 44df106 commit 509d9e8

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: llvm-as -o %t0 %s
2+
; RUN: cp %t0 %t1
3+
; RUN: llvm-dis %t0 %t1
4+
; RUN: FileCheck %s < %t0.ll
5+
; RUN: FileCheck %s < %t1.ll
6+
7+
; Test that if we disassemble the same bitcode twice, the type names are
8+
; unchanged between the two. This protects against a bug whereby state was
9+
; preserved across inputs and the types ended up with different names.
10+
11+
; CHECK: %Foo = type { ptr }
12+
%Foo = type { ptr }
13+
14+
; CHECK: @foo = global %Foo zeroinitializer
15+
@foo = global %Foo zeroinitializer

llvm/tools/llvm-dis/llvm-dis.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,6 @@ int main(int argc, char **argv) {
191191
if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)
192192
LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_TRUE;
193193

194-
LLVMContext Context;
195-
Context.setDiagnosticHandler(
196-
std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
197-
198194
if (InputFilenames.size() < 1) {
199195
InputFilenames.push_back("-");
200196
} else if (InputFilenames.size() > 1 && !OutputFilename.empty()) {
@@ -204,6 +200,12 @@ int main(int argc, char **argv) {
204200
}
205201

206202
for (const auto &InputFilename : InputFilenames) {
203+
// Use a fresh context is used for each input to avoid issues of
204+
// state cross-contamination (e.g. type name maps).
205+
LLVMContext Context;
206+
Context.setDiagnosticHandler(
207+
std::make_unique<LLVMDisDiagnosticHandler>(argv[0]));
208+
207209
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
208210
MemoryBuffer::getFileOrSTDIN(InputFilename);
209211
if (std::error_code EC = BufferOrErr.getError()) {

0 commit comments

Comments
 (0)