[PATCH] D43934: [ELF] - Report location for div/mod by zero.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 2 12:10:37 PST 2018


LGTM

George Rimar via Phabricator via llvm-commits
<llvm-commits at lists.llvm.org> writes:

> grimar updated this revision to Diff 136687.
> grimar added a comment.
>
> - Addressed review comments.
>
>
> https://reviews.llvm.org/D43934
>
> Files:
>   ELF/ScriptParser.cpp
>   test/ELF/linkerscript/operators.test
>
>
> Index: test/ELF/linkerscript/operators.test
> ===================================================================
> --- test/ELF/linkerscript/operators.test
> +++ test/ELF/linkerscript/operators.test
> @@ -91,7 +91,13 @@
>  # RUN: echo "SECTIONS { . = 1 / 0; }" > %t.script
>  # RUN: not ld.lld %t --script %t.script -o %t2 2>&1 | \
>  # RUN:  FileCheck --check-prefix=DIVZERO %s
> -# DIVZERO: division by zero
> +# DIVZERO: {{.*}}.script:1: division by zero
> +
> +## Mod by zero error.
> +# RUN: echo "SECTIONS { . = 1 % 0; }" > %t.script
> +# RUN: not ld.lld %t --script %t.script -o %t2 2>&1 | \
> +# RUN:  FileCheck --check-prefix=MODZERO %s
> +# MODZERO: {{.*}}.script:1: modulo by zero
>  
>  ## Broken ternary operator expression.
>  # RUN: echo "SECTIONS { . = 1 ? 2; }" > %t.script
> Index: ELF/ScriptParser.cpp
> ===================================================================
> --- ELF/ScriptParser.cpp
> +++ ELF/ScriptParser.cpp
> @@ -98,6 +98,7 @@
>    uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
>    std::pair<uint32_t, uint32_t> readMemoryAttributes();
>  
> +  Expr combine(StringRef Op, Expr L, Expr R);
>    Expr readExpr();
>    Expr readExpr1(Expr Lhs, int MinPrec);
>    StringRef readParenLiteral();
> @@ -157,20 +158,6 @@
>    return {A.Sec, false, A.getSectionOffset() - B.getValue(), A.Loc};
>  }
>  
> -static ExprValue div(ExprValue A, ExprValue B) {
> -  if (uint64_t BV = B.getValue())
> -    return A.getValue() / BV;
> -  error("division by zero");
> -  return 0;
> -}
> -
> -static ExprValue mod(ExprValue A, ExprValue B) {
> -  if (uint64_t BV = B.getValue())
> -    return A.getValue() % BV;
> -  error("modulo by zero");
> -  return 0;
> -}
> -
>  static ExprValue bitAnd(ExprValue A, ExprValue B) {
>    moveAbsRight(A, B);
>    return {A.Sec, A.ForceAbsolute,
> @@ -809,17 +796,31 @@
>    return E;
>  }
>  
> -static Expr combine(StringRef Op, Expr L, Expr R) {
> +Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
>    if (Op == "+")
>      return [=] { return add(L(), R()); };
>    if (Op == "-")
>      return [=] { return sub(L(), R()); };
>    if (Op == "*")
>      return [=] { return L().getValue() * R().getValue(); };
> -  if (Op == "/")
> -    return [=] { return div(L(), R()); };
> -  if (Op == "%")
> -    return [=] { return mod(L(), R()); };
> +  if (Op == "/") {
> +    std::string Loc = getCurrentLocation();
> +    return [=] {
> +      if (uint64_t RV = R().getValue())
> +        return L().getValue() / RV;
> +      error(Loc + ": division by zero");
> +      return (uint64_t)0;
> +    };
> +  }
> +  if (Op == "%") {
> +    std::string Loc = getCurrentLocation();
> +    return [=] {
> +      if (uint64_t RV = R().getValue())
> +        return L().getValue() % RV;
> +      error(Loc + ": modulo by zero");
> +      return (uint64_t)0;
> +    };
> +  }
>    if (Op == "<<")
>      return [=] { return L().getValue() << R().getValue(); };
>    if (Op == ">>")
>
>
> Index: test/ELF/linkerscript/operators.test
> ===================================================================
> --- test/ELF/linkerscript/operators.test
> +++ test/ELF/linkerscript/operators.test
> @@ -91,7 +91,13 @@
>  # RUN: echo "SECTIONS { . = 1 / 0; }" > %t.script
>  # RUN: not ld.lld %t --script %t.script -o %t2 2>&1 | \
>  # RUN:  FileCheck --check-prefix=DIVZERO %s
> -# DIVZERO: division by zero
> +# DIVZERO: {{.*}}.script:1: division by zero
> +
> +## Mod by zero error.
> +# RUN: echo "SECTIONS { . = 1 % 0; }" > %t.script
> +# RUN: not ld.lld %t --script %t.script -o %t2 2>&1 | \
> +# RUN:  FileCheck --check-prefix=MODZERO %s
> +# MODZERO: {{.*}}.script:1: modulo by zero
>  
>  ## Broken ternary operator expression.
>  # RUN: echo "SECTIONS { . = 1 ? 2; }" > %t.script
> Index: ELF/ScriptParser.cpp
> ===================================================================
> --- ELF/ScriptParser.cpp
> +++ ELF/ScriptParser.cpp
> @@ -98,6 +98,7 @@
>    uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
>    std::pair<uint32_t, uint32_t> readMemoryAttributes();
>  
> +  Expr combine(StringRef Op, Expr L, Expr R);
>    Expr readExpr();
>    Expr readExpr1(Expr Lhs, int MinPrec);
>    StringRef readParenLiteral();
> @@ -157,20 +158,6 @@
>    return {A.Sec, false, A.getSectionOffset() - B.getValue(), A.Loc};
>  }
>  
> -static ExprValue div(ExprValue A, ExprValue B) {
> -  if (uint64_t BV = B.getValue())
> -    return A.getValue() / BV;
> -  error("division by zero");
> -  return 0;
> -}
> -
> -static ExprValue mod(ExprValue A, ExprValue B) {
> -  if (uint64_t BV = B.getValue())
> -    return A.getValue() % BV;
> -  error("modulo by zero");
> -  return 0;
> -}
> -
>  static ExprValue bitAnd(ExprValue A, ExprValue B) {
>    moveAbsRight(A, B);
>    return {A.Sec, A.ForceAbsolute,
> @@ -809,17 +796,31 @@
>    return E;
>  }
>  
> -static Expr combine(StringRef Op, Expr L, Expr R) {
> +Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
>    if (Op == "+")
>      return [=] { return add(L(), R()); };
>    if (Op == "-")
>      return [=] { return sub(L(), R()); };
>    if (Op == "*")
>      return [=] { return L().getValue() * R().getValue(); };
> -  if (Op == "/")
> -    return [=] { return div(L(), R()); };
> -  if (Op == "%")
> -    return [=] { return mod(L(), R()); };
> +  if (Op == "/") {
> +    std::string Loc = getCurrentLocation();
> +    return [=] {
> +      if (uint64_t RV = R().getValue())
> +        return L().getValue() / RV;
> +      error(Loc + ": division by zero");
> +      return (uint64_t)0;
> +    };
> +  }
> +  if (Op == "%") {
> +    std::string Loc = getCurrentLocation();
> +    return [=] {
> +      if (uint64_t RV = R().getValue())
> +        return L().getValue() % RV;
> +      error(Loc + ": modulo by zero");
> +      return (uint64_t)0;
> +    };
> +  }
>    if (Op == "<<")
>      return [=] { return L().getValue() << R().getValue(); };
>    if (Op == ">>")
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list