1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[PowerPC] Fix downcast from nullptr for target streamer

getTargetStreamer() might return null (e.g. when running inlined-strings.ll test),
downcasting to a reference will be wrong. This is detectable with -fsanitize=null.

Reviewed By: steven.zhang

Differential Revision: https://reviews.llvm.org/D78686
This commit is contained in:
Ng Zhi An 2020-04-28 09:20:10 +00:00 committed by QingShan Zhang
parent cc12df82ea
commit 347f41d492
5 changed files with 24 additions and 19 deletions

View File

@ -1731,10 +1731,10 @@ bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) {
if (parseToken(AsmToken::EndOfStatement))
return addErrorSuffix(" in '.machine' directive");
PPCTargetStreamer &TStreamer =
*static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
TStreamer.emitMachine(CPU);
PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
if (TStreamer != nullptr)
TStreamer->emitMachine(CPU);
return false;
}
@ -1773,10 +1773,10 @@ bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L) {
parseToken(AsmToken::EndOfStatement))
return addErrorSuffix(" in '.abiversion' directive");
PPCTargetStreamer &TStreamer =
*static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
TStreamer.emitAbiVersion(AbiVersion);
PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
if (TStreamer != nullptr)
TStreamer->emitAbiVersion(AbiVersion);
return false;
}
@ -1796,10 +1796,10 @@ bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L) {
parseToken(AsmToken::EndOfStatement))
return addErrorSuffix(" in '.localentry' directive");
PPCTargetStreamer &TStreamer =
*static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
TStreamer.emitLocalEntry(Sym, Expr);
PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>(
getParser().getStreamer().getTargetStreamer());
if (TStreamer != nullptr)
TStreamer->emitLocalEntry(Sym, Expr);
return false;
}

View File

@ -1396,8 +1396,8 @@ void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) {
bool isPPC64 = DL.getPointerSizeInBits() == 64;
PPCTargetStreamer &TS =
static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
PPCTargetStreamer *TS =
static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
if (!TOC.empty()) {
const char *Name = isPPC64 ? ".toc" : ".got2";
@ -1412,8 +1412,8 @@ void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) {
MCSymbol *const TOCEntryLabel = TOCMapPair.second;
OutStreamer->emitLabel(TOCEntryLabel);
if (isPPC64)
TS.emitTCEntry(*TOCEntryTarget);
if (isPPC64 && TS != nullptr)
TS->emitTCEntry(*TOCEntryTarget);
else
OutStreamer->emitSymbolValue(TOCEntryTarget, 4);
}
@ -1687,8 +1687,8 @@ void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
// Switch to section to emit TOC base.
OutStreamer->SwitchSection(getObjFileLowering().getTOCBaseSection());
PPCTargetStreamer &TS =
static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
PPCTargetStreamer *TS =
static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
const unsigned EntryByteSize = Subtarget->isPPC64() ? 8 : 4;
const unsigned TOCEntriesByteSize = TOC.size() * EntryByteSize;
@ -1707,7 +1707,8 @@ void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
OutStreamer->SwitchSection(TCEntry);
OutStreamer->emitLabel(I.second);
TS.emitTCEntry(*I.first);
if (TS != nullptr)
TS->emitTCEntry(*I.first);
}
}

View File

@ -1,4 +1,6 @@
; RUN: llc -verify-machineinstrs -code-model=small < %s | FileCheck %s
; Test with null streamer.
; RUN: llc -O0 -filetype=null < %s
target datalayout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v128:128:128-n32:64"
target triple = "powerpc64-unknown-linux-gnu"

View File

@ -1,6 +1,7 @@
# RUN: llvm-mc -triple powerpc-unknown-unknown %s
# RUN: llvm-mc -triple powerpc64-unknown-unknown %s
# RUN: llvm-mc -triple powerpc64le-unknown-unknown %s
# RUN: llvm-mc -triple powerpc64le-unknown-unknown -filetype=null %s
# For now, the only thing we check is that the .machine directive
# is accepted without syntax error.

View File

@ -3,6 +3,7 @@
# RUN: llvm-readobj -h | FileCheck %s
# RUN: llvm-mc -triple powerpc64le-unknown-unknown -filetype=obj %s | \
# RUN: llvm-readobj -h | FileCheck %s
# RUN: llvm-mc -triple powerpc64le-unknown-unknown -filetype=null %s
.abiversion 2
# CHECK: Flags [ (0x2)