• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tudasc / TypeART / 19336558931

13 Nov 2025 03:24PM UTC coverage: 90.337% (+1.4%) from 88.924%
19336558931

push

github

web-flow
Global variables as type descriptors (#173)

597 of 629 new or added lines in 25 files covered. (94.91%)

2 existing lines in 2 files now uncovered.

4768 of 5278 relevant lines covered (90.34%)

208224.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.73
/lib/runtime/TypeResolution.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "TypeResolution.h"
14

15
#include "AllocationTracking.h"
16
#include "Runtime.h"
17
#include "RuntimeData.h"
18
#include "RuntimeInterface.h"
19
#include "TypeDB.h"
20
#include "TypeInterface.h"
21
#include "support/Logger.h"
22
#include "support/System.h"
23

24
#include <cassert>
25
#include <charconv>
26
#include <cstddef>
27
#include <utility>
28
#include <vector>
29

30
namespace typeart {
31

32
namespace detail {
33

34
template <typename P, typename T>
35
inline const void* add_byte_offset(const P* addr, T offset) {
2,410✔
36
  assert(addr != nullptr);
2,410✔
37
  return static_cast<const P*>(static_cast<const char*>(addr) + offset);
2,410✔
38
}
39

40
template <typename T>
41
inline std::ptrdiff_t substract_pointer(const T* addr_a, const T* addr_b) {
1,678✔
42
  assert(addr_a != nullptr);
1,678✔
43
  assert(addr_b != nullptr);
1,678✔
44
  return static_cast<const char*>(addr_a) - static_cast<const char*>(addr_b);
1,678✔
45
}
46

47
inline size_t get_member_index_at(const typeart_struct_layout& structInfo, size_t offset) {
420✔
48
  const size_t num_members = structInfo.num_members;
420✔
49

50
  if (num_members == 0) {
420✔
51
    return 0;
×
52
  }
53

54
  const auto* struct_offsets = structInfo.offsets;
420✔
55

56
  if (offset > struct_offsets[num_members - 1]) {
420✔
57
    return num_members - 1;
15✔
58
  }
59

60
  size_t member_index{0};
405✔
61
  while (member_index < (num_members - 1) && offset >= struct_offsets[member_index + 1]) {
888✔
62
    ++member_index;
483✔
63
  }
64
  return member_index;
405✔
65
}
420✔
66

67
}  // namespace detail
68

69
TypeResolution::TypeResolution(const TypeDB& type_db, Recorder& recorder)
818✔
70
    : type_database{type_db}, runtime_recorder{recorder} {
818✔
71
}
818✔
72

73
TypeResolution::TypeArtStatus TypeResolution::getSubTypeInfo(const void* baseAddr, size_t offset,
444✔
74
                                                             const typeart_struct_layout& containerInfo, int* subType,
420✔
75
                                                             const void** subTypeBaseAddr, size_t* subTypeOffset,
76
                                                             size_t* subTypeCount) const {
77
  if (containerInfo.type_id < 0) {
444✔
78
    return TYPEART_ERROR;
12✔
79
  }
80

81
  if (offset >= containerInfo.extent) {
432✔
82
    return TYPEART_BAD_OFFSET;
12✔
83
  }
84

85
  // Get index of the struct member at the address
86
  const size_t memberIndex = detail::get_member_index_at(containerInfo, offset);
420✔
87
  const int memberType     = containerInfo.member_types[memberIndex];
420✔
88
  const size_t baseOffset  = containerInfo.offsets[memberIndex];
420✔
89

90
  const size_t internalOffset = offset - baseOffset;
420✔
91
  const size_t typeSize       = type_database.getTypeSize(memberType);
420✔
92
  if (typeSize == 0) {
420✔
93
    return TYPEART_INVALID_ID;
×
94
  }
95
  const size_t offsetInTypeSize = internalOffset / typeSize;
420✔
96
  const size_t newOffset        = internalOffset % typeSize;
420✔
97

98
  // If newOffset != 0, the subtype cannot be atomic, i.e. must be a struct
99
  if (newOffset != 0) {
420✔
100
    if (type_database.isReservedType(memberType)) {
24✔
101
      return TYPEART_BAD_ALIGNMENT;
24✔
102
    }
103
  }
×
104

105
  // Ensure that the array index is in bounds
106
  if (offsetInTypeSize >= containerInfo.count[memberIndex]) {
396✔
107
    // Address points to padding
108
    return TYPEART_BAD_ALIGNMENT;
60✔
109
  }
110

111
  *subType         = memberType;
336✔
112
  *subTypeBaseAddr = detail::add_byte_offset(baseAddr, baseOffset);
336✔
113
  *subTypeOffset   = newOffset;
336✔
114
  *subTypeCount    = containerInfo.count[memberIndex] - offsetInTypeSize;
336✔
115

116
  return TYPEART_OK;
336✔
117
}
444✔
118

119
TypeResolution::TypeArtStatus TypeResolution::getTypeInfoInternal(const void* baseAddr, size_t offset,
396✔
120
                                                                  const StructTypeInfo& containerInfo, int* type,
121
                                                                  size_t* count) const {
122
  assert(offset < containerInfo.extent && "Something went wrong with the base address computation");
792✔
123

124
  int subType{-1};
396✔
125
  const void* subTypeBaseAddr;
396✔
126
  size_t subTypeOffset{0};
396✔
127
  size_t subTypeCount{0};
396✔
128

129
  const StructTypeInfo* structInfo = &containerInfo;
396✔
130

131
  const auto to_struct_layout_info = [](const auto& struct_type_info) {
792✔
132
    typeart_struct_layout struct_layout;
396✔
133
    struct_layout.type_id      = struct_type_info.type_id;
396✔
134
    struct_layout.name         = struct_type_info.name.c_str();
396✔
135
    struct_layout.num_members  = struct_type_info.num_members;
396✔
136
    struct_layout.extent       = struct_type_info.extent;
396✔
137
    struct_layout.offsets      = &struct_type_info.offsets[0];
396✔
138
    struct_layout.member_types = &struct_type_info.member_types[0];
396✔
139
    struct_layout.count        = &struct_type_info.array_sizes[0];
396✔
140
    return struct_layout;
396✔
141
  };
396✔
142

143
  // Resolve type recursively, until the address matches exactly
144
  bool resolve{true};
396✔
145
  while (resolve) {
720✔
146
    const auto status_subtype_info = getSubTypeInfo(baseAddr, offset, to_struct_layout_info(*structInfo), &subType,
396✔
147
                                                    &subTypeBaseAddr, &subTypeOffset, &subTypeCount);
148

149
    if (status_subtype_info != TYPEART_OK) {
396✔
150
      return status_subtype_info;
72✔
151
    }
152

153
    baseAddr = subTypeBaseAddr;
324✔
154
    offset   = subTypeOffset;
324✔
155

156
    // Continue as long as there is a byte offset
157
    resolve = offset != 0;
324✔
158

159
    // Get layout of the nested struct
160
    if (resolve) {
324✔
161
      const auto status_struct_info = getStructInfo(subType, &structInfo);
×
162
      if (status_struct_info != TYPEART_OK) {
×
163
        return status_struct_info;
×
164
      }
165
    }
×
166
  }
396✔
167
  *type  = subType;
324✔
168
  *count = subTypeCount;
324✔
169
  return TYPEART_OK;
324✔
170
}
396✔
171

172
TypeResolution::TypeArtStatus TypeResolution::getTypeInfo(const void* addr, const void* basePtr,
7,637✔
173
                                                          const PointerInfo& ptrInfo, int* type, size_t* count) const {
174
  const int containing_type = ptrInfo.typeId;
7,637✔
175
  size_t containing_type_count{0};
7,637✔
176
  size_t internal_byte_offset{0};
7,637✔
177

178
  // First, retrieve the containing type
179
  TypeArtStatus status = getContainingTypeInfo(addr, basePtr, ptrInfo, &containing_type_count, &internal_byte_offset);
7,637✔
180
  if (status != TYPEART_OK) {
7,637✔
181
    if (status == TYPEART_UNKNOWN_ADDRESS) {
604✔
182
      runtime_recorder.incAddrMissing(addr);
604✔
183
    }
604✔
184
    return status;
604✔
185
  }
186

187
  // Check for exact address match
188
  if (internal_byte_offset == 0) {
7,033✔
189
    *type  = containing_type;
6,397✔
190
    *count = containing_type_count;
6,397✔
191
    return TYPEART_OK;
6,397✔
192
  }
193

194
  if (type_database.isBuiltinType(containing_type)) {
636✔
195
    // Address points to the middle of a builtin type
196
    return TYPEART_BAD_ALIGNMENT;
240✔
197
  }
198

199
  // Resolve struct recursively
200
  const auto* structInfo = type_database.getStructInfo(containing_type);
396✔
201
  if (structInfo != nullptr) {
396✔
202
    const void* containingTypeAddr = detail::add_byte_offset(addr, -std::ptrdiff_t(internal_byte_offset));
396✔
203
    return getTypeInfoInternal(containingTypeAddr, internal_byte_offset, *structInfo, type, count);
396✔
204
  }
396✔
205

206
  return TYPEART_INVALID_ID;
×
207
}
7,637✔
208

209
TypeResolution::TypeArtStatus TypeResolution::getContainingTypeInfo(const void* addr, const void* basePtr,
7,691✔
210
                                                                    const PointerInfo& ptrInfo, size_t* count,
211
                                                                    size_t* offset) const {
212
  const auto& basePtrInfo = ptrInfo;
7,691✔
213
  size_t typeSize         = type_database.getTypeSize(basePtrInfo.typeId);
7,691✔
214
  if (basePtrInfo.typeId == TYPEART_VOID || basePtrInfo.typeId == TYPEART_POINTER) {
7,691✔
215
    // typeSize = 1;
216
  }
108✔
217

218
  // Check for exact match -> no further checks and offsets calculations needed
219
  if (basePtr == addr) {
7,691✔
220
    *count  = ptrInfo.count;
6,013✔
221
    *offset = 0;
6,013✔
222
    return TYPEART_OK;
6,013✔
223
  }
224

225
  if (typeSize == 0) {
1,678✔
226
    return TYPEART_ERROR;
×
227
  }
228

229
  // The address points inside a known array
230
  const void* blockEnd = detail::add_byte_offset(basePtr, basePtrInfo.count * typeSize);
1,678✔
231

232
  // Ensure that the given address is in bounds and points to the start of an element
233
  if (addr >= blockEnd) {
1,678✔
234
    const std::ptrdiff_t byte_offset_to_base = detail::substract_pointer(addr, basePtr);
604✔
235

236
    const auto elements  = byte_offset_to_base / typeSize;
604✔
237
    const auto oob_index = elements - basePtrInfo.count + 1;
604✔
238
    LOG_WARNING("Out of bounds for the lookup: (" << debug::toString(addr, basePtrInfo)
604✔
239
                                                  << ") #Elements too far: " << oob_index);
240
    return TYPEART_UNKNOWN_ADDRESS;
604✔
241
  }
604✔
242

243
  assert(addr >= basePtr && "Error in base address computation");
2,148✔
244

245
  const std::ptrdiff_t addrDif = detail::substract_pointer(addr, basePtr);
1,074✔
246

247
  // Offset of the pointer w.r.t. the start of the containing type
248
  const size_t internalOffset = addrDif % typeSize;
1,074✔
249

250
  // Array index
251
  const size_t typeOffset = addrDif / typeSize;
1,074✔
252
  const size_t typeCount  = basePtrInfo.count - typeOffset;
1,074✔
253

254
  // Retrieve and return type information
255
  *count  = typeCount;
1,074✔
256
  *offset = internalOffset;
1,074✔
257
  return TYPEART_OK;
1,074✔
258
}
7,691✔
259

260
TypeResolution::TypeArtStatus TypeResolution::getStructInfo(int type_id, const StructTypeInfo** structInfo) const {
360✔
261
  // Requested ID must correspond to a struct
262
  if (!type_database.isStructType(type_id)) {
360✔
263
    return TYPEART_WRONG_KIND;
×
264
  }
265

266
  const auto* result = type_database.getStructInfo(type_id);
360✔
267

268
  if (result != nullptr) {
360✔
269
    *structInfo = result;
336✔
270
    return TYPEART_OK;
336✔
271
  }
272
  return TYPEART_INVALID_ID;
24✔
273
}
360✔
274

275
// const TypeDB& TypeResolution::db() const {
276
//   return type_database;
277
// }
278

279
namespace detail {
280
// inline typeart_status query_type(const void* addr, int* type, size_t* count) {
281
//   auto alloc = typeart::RuntimeSystem::get().allocTracker.findBaseAlloc(addr);
282
//   typeart::RuntimeSystem::get().recorder.incUsedInRequest(addr);
283
//   if (alloc) {
284
//     return typeart::RuntimeSystem::get().getTypeResolution().getTypeInfo(addr, alloc->first, alloc->second, type,
285
//     count);
286
//   }
287
//   return TYPEART_UNKNOWN_ADDRESS;
288
// }
289

290
inline typeart_status query_type(const void* addr, typeart_type_info& info) {
7,788✔
291
  auto alloc = typeart::RuntimeSystem::get().allocation_tracker().findBaseAlloc(addr);
7,788✔
292
  typeart::RuntimeSystem::get().recorder.incUsedInRequest(addr);
7,788✔
293
  if (alloc) {
7,788✔
294
    typeart_base_type_info base;
7,637✔
295
    base.address        = alloc->first;
7,637✔
296
    base.type_id        = alloc->second.typeId;
7,637✔
297
    base.count          = alloc->second.count;
7,637✔
298
    info.base_type_info = base;
7,637✔
299
    info.address        = addr;
7,637✔
300

301
    const auto result = typeart::RuntimeSystem::get().get_type_resolution().getTypeInfo(
15,274✔
302
        addr, alloc->first, alloc->second, &info.type_id, &info.count);
7,637✔
303

304
    typeart::RuntimeSystem::get().recorder.incTypeQuery(base.type_id);
7,637✔
305
    return result;
7,637✔
306
  }
7,637✔
307
  return TYPEART_UNKNOWN_ADDRESS;
151✔
308
}
7,788✔
309

310
inline typeart_status query_struct_layout(int type_id, typeart_struct_layout* struct_layout) {
360✔
311
  const typeart::StructTypeInfo* struct_info;
360✔
312
  typeart_status status = typeart::RuntimeSystem::get().get_type_resolution().getStructInfo(type_id, &struct_info);
360✔
313
  if (status == TYPEART_OK) {
360✔
314
    struct_layout->type_id      = struct_info->type_id;
336✔
315
    struct_layout->name         = struct_info->name.c_str();
336✔
316
    struct_layout->num_members  = struct_info->num_members;
336✔
317
    struct_layout->extent       = struct_info->extent;
336✔
318
    struct_layout->offsets      = (struct_info->offsets).data();
336✔
319
    struct_layout->member_types = (struct_info->member_types).data();
336✔
320
    struct_layout->count        = (struct_info->array_sizes).data();
336✔
321
  } else {
336✔
322
    struct_layout->type_id      = std::numeric_limits<decltype(typeart_struct_layout::type_id)>::min();
24✔
323
    struct_layout->name         = "";
24✔
324
    struct_layout->num_members  = 0;
24✔
325
    struct_layout->extent       = 0;
24✔
326
    struct_layout->offsets      = nullptr;
24✔
327
    struct_layout->member_types = nullptr;
24✔
328
    struct_layout->count        = nullptr;
24✔
329
  }
330
  return status;
360✔
331
}
360✔
332

333
char* string2char(std::string_view src) {
24✔
334
  // const void* ret_addr       = __builtin_return_address(0);
335
  const size_t source_length = src.size() + 1;  // +1 for '\0'
24✔
336
  char* string_copy          = (char*)malloc(sizeof(char) * source_length);
24✔
337

338
  if (string_copy == nullptr) {
24✔
339
    return nullptr;
×
340
  }
341

342
  // typeart::RuntimeSystem::get().allocTracker.onAlloc(string_copy, TYPEART_CHAR_8, source_length, ret_addr);
343

344
  memcpy(string_copy, src.data(), source_length);
24✔
345

346
  return string_copy;
24✔
347
}
24✔
348

349
}  // namespace detail
350
}  // namespace typeart
351

352
/**
353
 * Runtime interface implementation
354
 *
355
 */
356

357
typeart_status typeart_get_type(const void* addr, typeart_type_info* base_type) {
7,788✔
358
  typeart::RTGuard guard;
7,788✔
359
  return typeart::detail::query_type(addr, *base_type);
7,788✔
360
}
7,788✔
361

362
typeart_status typeart_get_containing_type(typeart_type_info type, typeart_base_type_info* containing_type,
54✔
363
                                           size_t* byte_offset) {
364
  typeart::RTGuard guard;
54✔
365
  containing_type->type_id = type.base_type_info.type_id;
54✔
366
  containing_type->count   = type.base_type_info.count;
54✔
367
  containing_type->address = type.base_type_info.address;
54✔
368
  const typeart::PointerInfo info{type.base_type_info.type_id, type.base_type_info.count};
54✔
369
  const auto result = typeart::RuntimeSystem::get().type_resolution().getContainingTypeInfo(
60✔
370
      type.address, containing_type->address, info, &containing_type->count, byte_offset);
30✔
371

372
  return result;
30✔
373
}
30✔
374

375
typeart_status typeart_get_subtype(const typeart_struct_layout* container_layout, const void* base_addr, size_t offset,
48✔
376
                                   typeart_base_type_info* subtype_info, size_t* subtype_byte_offset) {
377
  typeart::RTGuard guard;
48✔
378
  auto status = typeart::RuntimeSystem::get().get_type_resolution().getSubTypeInfo(
96✔
379
      base_addr, offset, *container_layout, &subtype_info->type_id, &subtype_info->address, subtype_byte_offset,
48✔
380
      &subtype_info->count);
48✔
381
  return status;
48✔
382
}
48✔
383

384
typeart_status typeart_resolve_type_id(int type_id, typeart_struct_layout* struct_layout) {
360✔
385
  typeart::RTGuard guard;
360✔
386
  return typeart::detail::query_struct_layout(type_id, struct_layout);
360✔
387
}
360✔
388

389
typeart_status typeart_get_return_address(const void* addr, const void** return_addr) {
24✔
390
  typeart::RTGuard guard;
24✔
391
  auto alloc = typeart::RuntimeSystem::get().allocation_tracker().findBaseAlloc(addr);
24✔
392

393
  if (alloc) {
24✔
394
    *return_addr = alloc.value().second.debug;
12✔
395
    return TYPEART_OK;
12✔
396
  }
397
  *return_addr = nullptr;
12✔
398
  return TYPEART_UNKNOWN_ADDRESS;
12✔
399
}
24✔
400

401
// typeart_status_t typeart_get_source_location(const void* addr, char** file, char** function, char** line) {
402
//   using namespace typeart::detail;
403
//   typeart::RTGuard guard;
404

405
//   auto source_loc = typeart::SourceLocation::create(addr);
406

407
//   if (source_loc) {
408
//     *file     = string2char(source_loc->file);
409
//     *function = string2char(source_loc->function);
410
//     *line     = string2char(source_loc->line);
411

412
//     if (*file == nullptr || *function == nullptr || *line == nullptr) {
413
//       return TYPEART_ERROR;
414
//     }
415

416
//     return TYPEART_OK;
417
//   }
418

419
//   return TYPEART_UNKNOWN_ADDRESS;
420
// }
421

422
typeart_status_t typeart_get_source_location(const void* addr, typeart_source_location* source_location) {
12✔
423
  using namespace typeart::detail;
424
  typeart::RTGuard guard;
12✔
425

426
  auto source_loc = typeart::SourceLocation::create(addr);
12✔
427

428
  if (source_loc) {
12✔
429
    source_location->file     = string2char(source_loc->file);
12✔
430
    source_location->function = string2char(source_loc->function);
12✔
431
    source_location->line     = 0;
12✔
432
    const auto& line          = source_loc->line;
12✔
433
    auto [ptr, ec]            = std::from_chars(line.data(), line.data() + line.size(), source_location->line);
24✔
434

435
    if (source_location->file == nullptr || source_location->function == nullptr || ec != std::errc{}) {
12✔
436
      return TYPEART_ERROR;
×
437
    }
438

439
    return TYPEART_OK;
12✔
440
  }
12✔
441

442
  return TYPEART_UNKNOWN_ADDRESS;
×
443
}
12✔
444

445
typeart_status_t typeart_free_source_location(typeart_source_location* source_location) {
12✔
446
  using namespace typeart::detail;
447
  typeart::RTGuard guard;
12✔
448

449
  if (source_location == nullptr) {
12✔
450
    return TYPEART_ERROR;
×
451
  }
452

453
  free(source_location->file);
12✔
454
  free(source_location->function);
12✔
455

456
  source_location->file     = nullptr;
12✔
457
  source_location->function = nullptr;
12✔
458
  source_location->line     = 0;
12✔
459

460
  return TYPEART_OK;
12✔
461
}
12✔
462

463
const char* typeart_get_type_name(int type_id) {
5,875✔
464
  typeart::RTGuard guard;
5,875✔
465
  return typeart::RuntimeSystem::get().database().getTypeName(type_id).c_str();
5,875✔
466
}
5,875✔
467

468
bool typeart_is_vector_type(int type_id) {
24✔
469
  typeart::RTGuard guard;
24✔
470
  return typeart::RuntimeSystem::get().database().isVectorType(type_id);
24✔
471
}
24✔
472

473
bool typeart_is_valid_type(int type_id) {
24✔
474
  typeart::RTGuard guard;
24✔
475
  return typeart::RuntimeSystem::get().database().isValid(type_id);
24✔
476
}
24✔
477

478
bool typeart_is_reserved_type(int type_id) {
24✔
479
  typeart::RTGuard guard;
24✔
480
  return typeart::RuntimeSystem::get().database().isReservedType(type_id);
24✔
481
}
24✔
482

483
bool typeart_is_builtin_type(int type_id) {
24✔
484
  typeart::RTGuard guard;
24✔
485
  return typeart::RuntimeSystem::get().database().isBuiltinType(type_id);
24✔
486
}
24✔
487

488
bool typeart_is_struct_type(int type_id) {
24✔
489
  typeart::RTGuard guard;
24✔
490
  return typeart::RuntimeSystem::get().database().isStructType(type_id);
24✔
491
}
24✔
492

493
bool typeart_is_userdefined_type(int type_id) {
24✔
494
  typeart::RTGuard guard;
24✔
495
  return typeart::RuntimeSystem::get().database().isUserDefinedType(type_id);
24✔
496
}
24✔
497

498
bool typeart_is_union(int type_id) {
×
499
  typeart::RTGuard guard;
×
NEW
500
  return typeart::RuntimeSystem::get().database().isUnion(type_id);
×
501
}
×
502

503
size_t typeart_get_type_size(int type_id) {
24✔
504
  typeart::RTGuard guard;
24✔
505
  return typeart::RuntimeSystem::get().database().getTypeSize(type_id);
24✔
506
}
24✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc