[llvm-branch-commits] [llvm] 3ca502a - Use DataExtractor to decode SLEB128 in android_relas.

2021-01-28 Thread Rahman Lavaee via llvm-branch-commits

Author: Rahman Lavaee
Date: 2021-01-28T01:35:18-08:00
New Revision: 3ca502a7d60787abe14db1fa541950ff79c7585b

URL: 
https://github.com/llvm/llvm-project/commit/3ca502a7d60787abe14db1fa541950ff79c7585b
DIFF: 
https://github.com/llvm/llvm-project/commit/3ca502a7d60787abe14db1fa541950ff79c7585b.diff

LOG: Use DataExtractor to decode SLEB128 in android_relas.

A simple refactoring patch which let us use `DataExtractor::getSLEB128` rather 
than using a lambda function.

Differential Revision: https://reviews.llvm.org/D95158

Added: 


Modified: 
llvm/lib/Object/ELF.cpp
llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s

Removed: 




diff  --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 264f115ddbb5..27a35bbd759d 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -8,7 +8,7 @@
 
 #include "llvm/Object/ELF.h"
 #include "llvm/BinaryFormat/ELF.h"
-#include "llvm/Support/LEB128.h"
+#include "llvm/Support/DataExtractor.h"
 
 using namespace llvm;
 using namespace object;
@@ -373,39 +373,31 @@ ELFFile::android_relas(const Elf_Shdr &Sec) const {
   Expected> ContentsOrErr = getSectionContents(Sec);
   if (!ContentsOrErr)
 return ContentsOrErr.takeError();
-  const uint8_t *Cur = ContentsOrErr->begin();
-  const uint8_t *End = ContentsOrErr->end();
-  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
-  Cur[2] != 'S' || Cur[3] != '2')
+  ArrayRef Content = *ContentsOrErr;
+  if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
+  Content[2] != 'S' || Content[3] != '2')
 return createError("invalid packed relocation header");
-  Cur += 4;
-
-  const char *ErrStr = nullptr;
-  auto ReadSLEB = [&]() -> int64_t {
-if (ErrStr)
-  return 0;
-unsigned Len;
-int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
-Cur += Len;
-return Result;
-  };
+  DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
+  DataExtractor::Cursor Cur(/*Offset=*/4);
 
-  uint64_t NumRelocs = ReadSLEB();
-  uint64_t Offset = ReadSLEB();
+  uint64_t NumRelocs = Data.getSLEB128(Cur);
+  uint64_t Offset = Data.getSLEB128(Cur);
   uint64_t Addend = 0;
 
-  if (ErrStr)
-return createError(ErrStr);
+  if (!Cur)
+return std::move(Cur.takeError());
 
   std::vector Relocs;
   Relocs.reserve(NumRelocs);
   while (NumRelocs) {
-uint64_t NumRelocsInGroup = ReadSLEB();
+uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
+if (!Cur)
+  return std::move(Cur.takeError());
 if (NumRelocsInGroup > NumRelocs)
   return createError("relocation group unexpectedly large");
 NumRelocs -= NumRelocsInGroup;
 
-uint64_t GroupFlags = ReadSLEB();
+uint64_t GroupFlags = Data.getSLEB128(Cur);
 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
 bool GroupedByOffsetDelta = GroupFlags & 
ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
@@ -413,34 +405,30 @@ ELFFile::android_relas(const Elf_Shdr &Sec) const {
 
 uint64_t GroupOffsetDelta;
 if (GroupedByOffsetDelta)
-  GroupOffsetDelta = ReadSLEB();
+  GroupOffsetDelta = Data.getSLEB128(Cur);
 
 uint64_t GroupRInfo;
 if (GroupedByInfo)
-  GroupRInfo = ReadSLEB();
+  GroupRInfo = Data.getSLEB128(Cur);
 
 if (GroupedByAddend && GroupHasAddend)
-  Addend += ReadSLEB();
+  Addend += Data.getSLEB128(Cur);
 
 if (!GroupHasAddend)
   Addend = 0;
 
-for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
+for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
   Elf_Rela R;
-  Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
+  Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
   R.r_offset = Offset;
-  R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
+  R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
   if (GroupHasAddend && !GroupedByAddend)
-Addend += ReadSLEB();
+Addend += Data.getSLEB128(Cur);
   R.r_addend = Addend;
   Relocs.push_back(R);
-
-  if (ErrStr)
-return createError(ErrStr);
 }
-
-if (ErrStr)
-  return createError(ErrStr);
+if (!Cur)
+  return std::move(Cur.takeError());
   }
 
   return Relocs;

diff  --git a/llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s 
b/llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s
index 4f2e65ed220f..32e96d716ca4 100644
--- a/llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s
+++ b/llvm/test/tools/llvm-readobj/ELF/packed-relocs-errors.s
@@ -23,7 +23,7 @@
 .section .rela.dyn, "a", @0x6001
 .ascii "APS2"
 
-# ERR-PAST-END: warning: '[[FILE]]': unable to read relocations from 
SHT_ANDROID_REL section with index 3: malformed sleb128, extends past end
+# ERR-PAST-END: warning: '[[FILE]]': unable to read relocations from 
SH

Re: [llvm-branch-commits] [clang] c3a21e5 - [ASTMatchers] Ensure that we can match inside lambdas

2021-01-28 Thread Alexander Kornienko via llvm-branch-commits
Thanks for the prompt response!

On Thu, Jan 28, 2021 at 12:52 AM Stephen Kelly  wrote:

>
> Thanks for reporting. Please try https://reviews.llvm.org/D95573
>
> Thanks,
>
> Stephen.
> On 27/01/2021 22:58, Alexander Kornienko wrote:
>
> This patch causes practically infinite traversal times on code that
> contains deeply nested lambdas. Please fix or revert the commit.
>
> There's a very simple test case (add more nesting, if it's still fast ;):
>
> void f() {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   [] {
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
>   }();
> }
>
> Three nested lambdas are enough to demonstrate the issue by looking at the
> AST dump. The body of the innermost lambda (0x45593fda99a0) is printed 8
> times, and it will be traversed 8 times as well by AST matchers:
> `-FunctionDecl 0x45593fda9198 
> line:1:6 f 'void ()'
>   `-CompoundStmt 0x45593fdce970 
> `-ExprWithCleanups 0x45593fdce958  'void':'void'
>   `-CXXOperatorCallExpr 0x45593fdce928 
> 'void':'void' '()'
> |-ImplicitCastExpr 0x45593fdce8b0  'auto (*)() const
> -> void' 
> | `-DeclRefExpr 0x45593fdce890  'auto () const ->
> void' lvalue CXXMethod 0x45593fda9490 'operator()' 'auto () const -> void'
> `-ImplicitCastExpr 0x45593fdce910  'const
> (lambda at /tmp/nested-lambdas.cc:2:3)' lvalue 
>   `-MaterializeTemporaryExpr 0x45593fdce8f8 
> '(lambda at /tmp/nested-lambdas.cc:2:3)' lvalue
> `-LambdaExpr 0x45593fdce788  '(lambda at
> /tmp/nested-lambdas.cc:2:3)'
>   |-CXXRecordDecl 0x45593fda9350  col:3 implicit
> class definition
>   | |-DefinitionData lambda pass_in_registers empty
> standard_layout trivially_copyable literal can_const_default_init
>   | | |-DefaultConstructor defaulted_is_constexpr
>   | | |-CopyConstructor simple trivial has_const_param
> needs_implicit implicit_has_const_param
>   | | |-MoveConstructor exists simple trivial needs_implicit
>   | | |-CopyAssignment trivial has_const_param needs_implicit
> implicit_has_const_param
>   | | |-MoveAssignment
>   | | `-Destructor simple irrelevant trivial
>   | |-CXXMethodDecl 0x45593fda9490  line:2:3
> used constexpr operator() 'auto () const -> void' inline
>   | | `-CompoundStmt 0x45593fdce4e0 
>   | |   `-ExprWithCleanups 0x45593fdce4c8 
> 'void':'void'
>   | | `-CXXOperatorCallExpr 0x45593fdce498  line:6:5> 'void':'void' '()'
>   | |   |-ImplicitCastExpr 0x45593fdce420 
> 'auto (*)() const -> void' 
>   | |   | `-DeclRefExpr 0x45593fdce400 
> 'auto () const -> void' lvalue CXXMethod 0x45593fda96c0 'operator()' 'auto
> () const -> void'
>   | |   `-ImplicitCastExpr 0x45593fdce480  line:6:3> 'const (lambda at /tmp/nested-lambdas.cc:3:3)' lvalue 
>   | | `-MaterializeTemporaryExpr 0x45593fdce468
>  '(lambda at /tmp/nested-lambdas.cc:3:3)' lvalue
>   | |   `-LambdaExpr 0x45593fdce2f0  line:6:3> '(lambda at /tmp/nested-lambdas.cc:3:3)'
>   | | |-CXXRecordDecl 0x45593fda9588 
> col:3 implicit class definition
>   | | | |-DefinitionData lambda pass_in_registers
> empty standard_layout trivially_copyable literal can_const_default_init
>   | | | | |-DefaultConstructor
> defaulted_is_constexpr
>   | | | | |-CopyConstructor simple trivial
> has_const_param needs_implicit implicit_has_const_param
>   | | | | |-MoveConstructor exists simple trivial
> needs_implicit
>   | | | | |-CopyAssignment trivial has_const_param
> needs_implicit implicit_has_const_param
>   | | | | |-MoveAssignment
>   | | | | `-Destructor simple irrelevant trivial
>   | | | |-CXXMethodDecl 0x45593fda96c0  line:6:3> line:3:3 used constexpr operator() 'auto () const -> void' inline
>   | | | | `-CompoundStmt 0x45593fdce048  line:6:3>
>   | | | |   `-ExprWithCleanups 0x45593fdce030
>  'void':'void'
>   | | | | `-CXXOperatorCallExpr 0x45593fdce000
>  'void':'void' '()'
>   | | | |   |-ImplicitCastExpr 0x45593fda9f88
>  'auto (*)() const -> void' 
>   | | | |   | `-DeclRefExpr 0x45593fda9f08
>  'auto () const -> void' lvalue CXXMethod 0x45593fda98f0
> 'operator()' 'auto () c