1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 #include <vector>
22 
23 #include <unwindstack/Memory.h>
24 
25 namespace unwindstack {
26 
27 class MemoryBuffer : public Memory {
28  public:
29   // If a size is too large, assume it's likely corrupted data, and set to zero.
MemoryBuffer(size_t size)30   MemoryBuffer(size_t size) : raw_(size > kMaxBufferSize ? 0 : size), offset_(0) {}
MemoryBuffer(size_t size,uint64_t offset)31   MemoryBuffer(size_t size, uint64_t offset)
32       : raw_(size > kMaxBufferSize ? 0 : size), offset_(offset) {}
33   virtual ~MemoryBuffer() = default;
34 
35   size_t Read(uint64_t addr, void* dst, size_t size) override;
36 
37   uint8_t* GetPtr(size_t offset) override;
38 
Data()39   uint8_t* Data() { return raw_.data(); }
Size()40   uint64_t Size() { return raw_.size(); }
41 
42  private:
43   std::vector<uint8_t> raw_;
44   uint64_t offset_;
45 
46   // This class is only used for global data and a compressed .debug_frame in
47   // the library code. The limit of 10MB is way over what any valid existing
48   // globals data section is expected to be. A 50MB shared library only contains
49   // a .debug_frame that is < 100KB in size. Therefore, 10MB should be able to
50   // handle any valid large shared library with a valid large .debug_frame.
51   static constexpr size_t kMaxBufferSize = 10 * 1024 * 1024;
52 };
53 
54 }  // namespace unwindstack
55