• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "code_data_accessor.h"
17 
18 namespace panda::panda_file {
19 
CatchBlock(Span<const uint8_t> data)20 CodeDataAccessor::CatchBlock::CatchBlock(Span<const uint8_t> data)
21 {
22     auto sp = data;
23     type_idx_ = helpers::ReadULeb128(&sp) - 1;
24     handler_pc_ = helpers::ReadULeb128(&sp);
25     code_size_ = helpers::ReadULeb128(&sp);
26     size_ = sp.data() - data.data();
27 }
28 
TryBlock(Span<const uint8_t> data)29 CodeDataAccessor::TryBlock::TryBlock(Span<const uint8_t> data) : data_(data), size_(0)
30 {
31     start_pc_ = helpers::ReadULeb128(&data);
32     length_ = helpers::ReadULeb128(&data);
33     num_catches_ = helpers::ReadULeb128(&data);
34     catch_blocks_sp_ = data;
35 }
36 
CodeDataAccessor(const File & panda_file,File::EntityId code_id)37 CodeDataAccessor::CodeDataAccessor(const File &panda_file, File::EntityId code_id)
38     : panda_file_(panda_file), code_id_(code_id), size_(0)
39 {
40     auto sp = panda_file_.GetSpanFromId(code_id_);
41 
42     num_vregs_ = helpers::ReadULeb128(&sp);
43     num_args_ = helpers::ReadULeb128(&sp);
44     code_size_ = helpers::ReadULeb128(&sp);
45     tries_size_ = helpers::ReadULeb128(&sp);
46     instructions_ptr_ = sp.data();
47     THROW_IF(sp.Size() < code_size_, File::INVALID_FILE_OFFSET);
48     sp = sp.SubSpan(code_size_);
49     try_blocks_sp_ = sp;
50 }
51 
52 }  // namespace panda::panda_file
53