LLDB mainline
WasmAddress.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_WASMADDRESS_H
10#define LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_WASMADDRESS_H
11
12#include "lldb/lldb-defines.h"
13#include "lldb/lldb-types.h"
14#include <cstdint>
15
16namespace lldb_private {
17namespace wasm {
18
19/// Each WebAssembly module has separate address spaces for Code and Memory. A
20/// WebAssembly module also has a Data section which, when the module is loaded,
21/// gets mapped into a region in the module Memory.
22///
23/// Globals are not addressable: they live in an index space of their own. The
24/// synthetic Global space stands in for one, holding the index where an address
25/// would hold an offset, so that a global can be named and read.
26///
27/// The tag is two bits wide and these are the only spaces there are, so the
28/// remaining value means an address that belongs to nothing.
29enum WasmAddressType : uint8_t {
30 Memory = 0x00,
31 Object = 0x01,
32 Global = 0x02,
33 Invalid = 0x03,
34};
35
36/// Widths of the fields a 64-bit address is made of, from the low bits up. The
37/// bitfields below are declared with the same constants, so a field and the
38/// mask that extracts it cannot come to disagree.
39static constexpr uint32_t kWasmOffsetBits = 32;
40static constexpr uint32_t kWasmModuleIDBits = 30;
41static constexpr uint32_t kWasmAddressTypeBits = 2;
42
44 "a Wasm address has to account for all 64 bits");
45
46static constexpr uint32_t kWasmModuleIDShift = kWasmOffsetBits;
47static constexpr uint32_t kWasmAddressTypeShift =
49
50static constexpr uint64_t MakeFieldMask(uint32_t bits, uint32_t shift) {
51 return ((uint64_t(1) << bits) - 1) << shift;
52}
53
54static constexpr uint64_t kWasmOffsetMask = MakeFieldMask(kWasmOffsetBits, 0);
55static constexpr uint64_t kWasmModuleIDMask =
57static constexpr uint64_t kWasmAddressTypeMask =
59
60/// A value that names no module. Every value the id field can hold names a
61/// module, zero included, so the sentinel has to come from outside that range.
62static constexpr uint32_t kWasmInvalidModuleID = UINT32_MAX;
63
65 "the sentinel has to fall outside the range of a module id");
66
67/// For the purpose of debugging, we can represent all these separated 32-bit
68/// address spaces with a single virtual 64-bit address space. The
69/// wasm_addr_t provides this encoding using bitfields.
74
79
82
83 WasmAddressType GetType() const { return static_cast<WasmAddressType>(type); }
84 uint32_t GetModuleID() const { return module_id; }
85 uint32_t GetOffset() const { return offset; }
86
87 operator lldb::addr_t() { return *(uint64_t *)this; }
88};
89
90static_assert(sizeof(wasm_addr_t) == 8, "");
91
92/// The module an address belongs to, or kWasmInvalidModuleID for an invalid
93/// address.
94inline uint32_t GetWasmModuleID(lldb::addr_t addr) {
95 if (addr == LLDB_INVALID_ADDRESS)
97 return wasm_addr_t(addr).GetModuleID();
98}
99
100} // namespace wasm
101} // namespace lldb_private
102
103#endif
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
static constexpr uint32_t kWasmModuleIDShift
Definition WasmAddress.h:46
static constexpr uint64_t MakeFieldMask(uint32_t bits, uint32_t shift)
Definition WasmAddress.h:50
static constexpr uint32_t kWasmModuleIDBits
Definition WasmAddress.h:40
static constexpr uint64_t kWasmAddressTypeMask
Definition WasmAddress.h:57
static constexpr uint32_t kWasmInvalidModuleID
A value that names no module.
Definition WasmAddress.h:62
static constexpr uint32_t kWasmAddressTypeBits
Definition WasmAddress.h:41
uint32_t GetWasmModuleID(lldb::addr_t addr)
The module an address belongs to, or kWasmInvalidModuleID for an invalid address.
Definition WasmAddress.h:94
static constexpr uint32_t kWasmOffsetBits
Widths of the fields a 64-bit address is made of, from the low bits up.
Definition WasmAddress.h:39
WasmAddressType
Each WebAssembly module has separate address spaces for Code and Memory.
Definition WasmAddress.h:29
static constexpr uint64_t kWasmModuleIDMask
Definition WasmAddress.h:55
static constexpr uint32_t kWasmAddressTypeShift
Definition WasmAddress.h:47
static constexpr uint64_t kWasmOffsetMask
Definition WasmAddress.h:54
A class that represents a running process on the host machine.
static uint32_t bits(const uint32_t val, const uint32_t msbit, const uint32_t lsbit)
Definition ARMUtils.h:265
uint64_t addr_t
Definition lldb-types.h:80
For the purpose of debugging, we can represent all these separated 32-bit address spaces with a singl...
Definition WasmAddress.h:70
wasm_addr_t(WasmAddressType type, uint32_t module_id, uint32_t offset)
Definition WasmAddress.h:80
wasm_addr_t(lldb::addr_t addr)
Definition WasmAddress.h:75
WasmAddressType GetType() const
Definition WasmAddress.h:83