[PATCH] D12776: [MC] Don't crash on division by zero

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 10 16:59:17 PDT 2015


On Thu, Sep 10, 2015 at 3:00 PM, Rafael EspĂ­ndola
<llvm-commits at lists.llvm.org> wrote:
>> +// Check that llvm-mc doesn't crash on division by zero.
>> +// RUN: llvm-mc -triple x86_64-pc-linux-gnu %s -o - 2>&1
>> +
>> +.int 1/0
>
> Does it work with -filetype=obj? What is output?
>

I forgot to mention in the original mail. It emits out an error, but
probably not the most useful one :(

% /exps/llvm2/build/bin/llvm-mc -filetype=obj test/MC/ELF/div-by-zero.s
test/MC/ELF/div-by-zero.s:4:6: error: expected relocatable expression
.int 1/0
     ^

We don't have a context in MCExpr so this should be probably handled
by the parser to have something meaningful. MCExpr may return back an
error code to the MCAssembler so that it can discriminate between
'relocatable expression' and 'division by zero' error.
Maybe there's a better way of handling anyway.

>> Index: lib/MC/MCExpr.cpp
>> ===================================================================
>> --- lib/MC/MCExpr.cpp
>> +++ lib/MC/MCExpr.cpp
>> @@ -734,6 +734,11 @@
>>      // width, and gas defines the result of comparisons differently from
>>      // Apple as.
>>      int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
>> +
>> +    // Handle division by zero.
>> +    if (ABE->getOpcode() == MCBinaryExpr::Div && RHS == 0)
>> +      return false;
>> +
>
> Move the check to the "case MCBinaryExpr::Div:" bellow.
>
>>      int64_t Result = 0;
>>      switch (ABE->getOpcode()) {
>>      case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
>
>
> Cheers,
> Rafael
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits

-- 
Davide

"There are no solved problems; there are only problems that are more
or less solved" -- Henri Poincare


More information about the llvm-commits mailing list