From 49dba99a89a662da6dc509280ae1082f6e0c6070 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 25 Mar 2015 00:25:37 +0000 Subject: [PATCH] Produce an error instead of asserting on invalid .sleb128/.uleb128. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233155 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCExpr.h | 2 +- lib/MC/MCAssembler.cpp | 13 ++++++++++--- lib/MC/MCExpr.cpp | 11 ++++------- test/MC/X86/invalid-sleb.s | 5 +++++ 4 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 test/MC/X86/invalid-sleb.s diff --git a/include/llvm/MC/MCExpr.h b/include/llvm/MC/MCExpr.h index 48696750550..1f4edab3b64 100644 --- a/include/llvm/MC/MCExpr.h +++ b/include/llvm/MC/MCExpr.h @@ -94,7 +94,7 @@ public: bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const; bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; - int64_t evaluateKnownAbsolute(const MCAsmLayout &Layout) const; + bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable /// value, i.e. an expression of the fixed form (a - b + constant). diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index dada94b9dd3..5243facbe4e 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -1034,7 +1034,10 @@ bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { uint64_t OldSize = LF.getContents().size(); - int64_t Value = LF.getValue().evaluateKnownAbsolute(Layout); + int64_t Value; + bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout); + if (!Abs) + report_fatal_error("sleb128 and uleb128 expressions must be absolute"); SmallString<8> &Data = LF.getContents(); Data.clear(); raw_svector_ostream OSE(Data); @@ -1050,7 +1053,9 @@ bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF) { MCContext &Context = Layout.getAssembler().getContext(); uint64_t OldSize = DF.getContents().size(); - int64_t AddrDelta = DF.getAddrDelta().evaluateKnownAbsolute(Layout); + int64_t AddrDelta; + bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout); + assert(Abs && "We created a line delta with an invalid expression"); int64_t LineDelta; LineDelta = DF.getLineDelta(); SmallString<8> &Data = DF.getContents(); @@ -1065,7 +1070,9 @@ bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, MCDwarfCallFrameFragment &DF) { MCContext &Context = Layout.getAssembler().getContext(); uint64_t OldSize = DF.getContents().size(); - int64_t AddrDelta = DF.getAddrDelta().evaluateKnownAbsolute(Layout); + int64_t AddrDelta; + bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout); + assert(Abs && "We created call frame with an invalid expression"); SmallString<8> &Data = DF.getContents(); Data.clear(); raw_svector_ostream OSE(Data); diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index abd48c094dd..cac5110a4ff 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -406,13 +406,10 @@ bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const { return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr); } -int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const { - int64_t Res; - bool Abs = - evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true); - (void)Abs; - assert(Abs && "Not actually absolute"); - return Res; +bool MCExpr::evaluateKnownAbsolute(int64_t &Res, + const MCAsmLayout &Layout) const { + return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, + true); } bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, diff --git a/test/MC/X86/invalid-sleb.s b/test/MC/X86/invalid-sleb.s new file mode 100644 index 00000000000..ad27444d608 --- /dev/null +++ b/test/MC/X86/invalid-sleb.s @@ -0,0 +1,5 @@ +// RUN: not llvm-mc -filetype=obj -triple x86_64-pc-linux %s -o %t 2>&1 | FileCheck %s + +// CHECK: sleb128 and uleb128 expressions must be absolute + + .sleb128 undefined