Coverage Report

Created: 2026-06-30 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/WasmEdge/include/runtime/instance/data.h
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: Copyright The WasmEdge Authors
3
4
//===-- wasmedge/runtime/instance/data.h - Data Instance definition -------===//
5
//
6
// Part of the WasmEdge Project.
7
//
8
//===----------------------------------------------------------------------===//
9
///
10
/// \file
11
/// This file contains the data instance definition in store manager.
12
///
13
//===----------------------------------------------------------------------===//
14
#pragma once
15
16
#include "common/span.h"
17
#include "common/types.h"
18
19
#include <vector>
20
21
namespace WasmEdge {
22
namespace Runtime {
23
namespace Instance {
24
25
class DataInstance {
26
public:
27
  DataInstance() = delete;
28
  DataInstance(const uint64_t Offset, Span<const Byte> Init) noexcept
29
0
      : Off(Offset), Data(Init.begin(), Init.end()) {}
30
31
  /// Get offset in data instance.
32
0
  uint64_t getOffset() const noexcept { return Off; }
33
34
  /// Get data in data instance.
35
0
  Span<const Byte> getData() const noexcept { return Data; }
36
37
  /// Load bytes to value.
38
0
  ValVariant loadValue(const uint64_t Offset, const uint32_t N) const noexcept {
39
0
    assuming(N <= 16);
40
    // Due to applying the Memory64 proposal, we should avoid the overflow issue
41
    // of the following code.
42
    // Check the data boundary.
43
0
    if (unlikely(std::numeric_limits<uint64_t>::max() - Offset < N ||
44
0
                 Offset + N > Data.size())) {
45
0
      return 0;
46
0
    }
47
    // Load the data to the value.
48
0
    EndianValue<uint128_t> Value;
49
0
    std::memcpy(&Value.raw(), &Data[Offset], N);
50
    if constexpr (Endian::native == Endian::big) {
51
      Value.raw() >>= (128 - N * 8);
52
    }
53
0
    return Value.le();
54
0
  }
55
56
  /// Clear data in data instance.
57
0
  void clear() noexcept { Data.clear(); }
58
59
private:
60
  /// \name Data of data instance.
61
  /// @{
62
  const uint64_t Off;
63
  std::vector<Byte> Data;
64
  /// @}
65
};
66
67
} // namespace Instance
68
} // namespace Runtime
69
} // namespace WasmEdge