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

tudasc / TypeART / 13528988609

25 Feb 2025 07:06PM UTC coverage: 88.854% (-1.9%) from 90.735%
13528988609

Pull #163

github

web-flow
Merge e4a2d80f6 into d2e14acc5
Pull Request #163: LLVM 18 support

974 of 1122 new or added lines in 38 files covered. (86.81%)

30 existing lines in 6 files now uncovered.

4201 of 4728 relevant lines covered (88.85%)

190054.62 hits per line

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

94.64
/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 "TypeInterface.h"
20
#include "support/Logger.h"
21
#include "support/System.h"
22

23
#include "llvm/Support/raw_ostream.h"
24

25
#include <cassert>
26
#include <cstddef>
27
#include <cstdint>
28
#include <utility>
29
#include <vector>
30

31
namespace typeart {
32

33
namespace detail {
34

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

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

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

51
  if (num_members == 0) {
240✔
52
    return 0;
×
53
  }
54

55
  const auto* struct_offsets = structInfo.offsets;
240✔
56

57
  if (offset > struct_offsets[num_members - 1]) {
240✔
58
    return num_members - 1;
9✔
59
  }
60

61
  size_t member_index{0};
231✔
62
  while (member_index < (num_members - 1) && offset >= struct_offsets[member_index + 1]) {
501✔
63
    ++member_index;
270✔
64
  }
65
  return member_index;
231✔
66
}
240✔
67

68
}  // namespace detail
69

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

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

82
  if (offset >= containerInfo.extent) {
249✔
83
    return TYPEART_BAD_OFFSET;
9✔
84
  }
85

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

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

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

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

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

117
  return TYPEART_OK;
186✔
118
}
258✔
119

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

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

130
  const StructTypeInfo* structInfo = &containerInfo;
222✔
131

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

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

150
    if (status_subtype_info != TYPEART_OK) {
222✔
151
      return status_subtype_info;
45✔
152
    }
153

154
    baseAddr = subTypeBaseAddr;
177✔
155
    offset   = subTypeOffset;
177✔
156

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

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

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

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

188
  // Check for exact address match
189
  if (internal_byte_offset == 0) {
5,037✔
190
    *type  = containing_type;
4,635✔
191
    *count = containing_type_count;
4,635✔
192
    return TYPEART_OK;
4,635✔
193
  }
194

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

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

207
  return TYPEART_INVALID_ID;
×
208
}
5,467✔
209

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

219
  // Check for exact match -> no further checks and offsets calculations needed
220
  if (basePtr == addr) {
5,518✔
221
    *count  = ptrInfo.count;
4,356✔
222
    *offset = 0;
4,356✔
223
    return TYPEART_OK;
4,356✔
224
  }
225

226
  if (typeSize == 0) {
1,162✔
NEW
227
    return TYPEART_ERROR;
×
228
  }
229

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

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

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

244
  assert(addr >= basePtr && "Error in base address computation");
1,464✔
245

246
  const std::ptrdiff_t addrDif = detail::substract_pointer(addr, basePtr);
732✔
247

248
  // Offset of the pointer w.r.t. the start of the containing type
249
  const size_t internalOffset = addrDif % typeSize;
732✔
250

251
  // Array index
252
  const size_t typeOffset = addrDif / typeSize;
732✔
253
  const size_t typeCount  = basePtrInfo.count - typeOffset;
732✔
254

255
  // Retrieve and return type information
256
  *count  = typeCount;
732✔
257
  *offset = internalOffset;
732✔
258
  return TYPEART_OK;
732✔
259
}
5,518✔
260

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

267
  const auto* result = type_database.getStructInfo(type_id);
216✔
268

269
  if (result != nullptr) {
216✔
270
    *structInfo = result;
198✔
271
    return TYPEART_OK;
198✔
272
  }
273
  return TYPEART_INVALID_ID;
18✔
274
}
216✔
275

276
const TypeDB& TypeResolution::db() const {
658,851✔
277
  return type_database;
658,851✔
278
}
279

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

290
inline typeart_status query_struct_layout(int type_id, typeart_struct_layout* struct_layout) {
216✔
291
  const typeart::StructTypeInfo* struct_info;
216✔
292
  typeart_status status = typeart::RuntimeSystem::get().typeResolution.getStructInfo(type_id, &struct_info);
216✔
293
  if (status == TYPEART_OK) {
216✔
294
    struct_layout->type_id      = struct_info->type_id;
198✔
295
    struct_layout->name         = struct_info->name.c_str();
198✔
296
    struct_layout->num_members  = struct_info->num_members;
198✔
297
    struct_layout->extent       = struct_info->extent;
198✔
298
    struct_layout->offsets      = &struct_info->offsets[0];
198✔
299
    struct_layout->member_types = &struct_info->member_types[0];
198✔
300
    struct_layout->count        = &struct_info->array_sizes[0];
198✔
301
  } else {
198✔
302
    struct_layout->type_id      = std::numeric_limits<decltype(typeart_struct_layout::type_id)>::min();
18✔
303
    struct_layout->name         = "";
18✔
304
    struct_layout->num_members  = 0;
18✔
305
    struct_layout->extent       = 0;
18✔
306
    struct_layout->offsets      = nullptr;
18✔
307
    struct_layout->member_types = nullptr;
18✔
308
    struct_layout->count        = nullptr;
18✔
309
  }
310
  return status;
216✔
311
}
216✔
312

313
char* string2char(std::string_view src) {
27✔
314
  const void* ret_addr       = __builtin_return_address(0);
27✔
315
  const size_t source_length = src.size() + 1;  // +1 for '\0'
27✔
316
  char* string_copy          = (char*)malloc(sizeof(char) * source_length);
27✔
317

318
  if (string_copy == nullptr) {
27✔
319
    return nullptr;
×
320
  }
321

322
  typeart::RuntimeSystem::get().allocTracker.onAlloc(string_copy, TYPEART_CHAR_8, source_length, ret_addr);
27✔
323

324
  memcpy(string_copy, src.data(), source_length);
27✔
325

326
  return string_copy;
27✔
327
}
27✔
328

329
}  // namespace detail
330
}  // namespace typeart
331

332
/**
333
 * Runtime interface implementation
334
 *
335
 */
336

337
typeart_status typeart_get_type(const void* addr, int* type_id, size_t* count) {
5,657✔
338
  typeart::RTGuard guard;
5,657✔
339
  return typeart::detail::query_type(addr, type_id, count);
5,657✔
340
}
5,657✔
341

342
typeart_status typeart_get_type_length(const void* addr, size_t* count) {
9✔
343
  typeart::RTGuard guard;
9✔
344
  int type{0};
9✔
345
  return typeart::detail::query_type(addr, &type, count);
9✔
346
}
9✔
347

348
typeart_status typeart_get_type_id(const void* addr, int* type_id) {
54✔
349
  typeart::RTGuard guard;
54✔
350
  size_t count{0};
54✔
351
  return typeart::detail::query_type(addr, type_id, &count);
54✔
352
}
54✔
353

354
typeart_status typeart_get_containing_type(const void* addr, int* type_id, size_t* count, const void** base_address,
51✔
355
                                           size_t* byte_offset) {
356
  typeart::RTGuard guard;
51✔
357
  auto alloc = typeart::RuntimeSystem::get().allocTracker.findBaseAlloc(addr);
51✔
358
  if (alloc) {
51✔
359
    //    auto& allocVal = alloc.value();
360
    *type_id      = alloc->second.typeId;
51✔
361
    *base_address = alloc->first;
51✔
362
    return typeart::RuntimeSystem::get().typeResolution.getContainingTypeInfo(addr, alloc->first, alloc->second, count,
51✔
363
                                                                              byte_offset);
51✔
364
  }
365
  return TYPEART_UNKNOWN_ADDRESS;
×
366
}
51✔
367

368
typeart_status typeart_get_subtype(const void* base_addr, size_t offset, const typeart_struct_layout* container_layout,
36✔
369
                                   int* subtype_id, const void** subtype_base_addr, size_t* subtype_byte_offset,
370
                                   size_t* subtype_count) {
371
  typeart::RTGuard guard;
36✔
372
  auto status = typeart::RuntimeSystem::get().typeResolution.getSubTypeInfo(
36✔
373
      base_addr, offset, *container_layout, subtype_id, subtype_base_addr, subtype_byte_offset, subtype_count);
36✔
374
  return status;
36✔
375
}
36✔
376

377
typeart_status typeart_resolve_type_addr(const void* addr, typeart_struct_layout* struct_layout) {
9✔
378
  typeart::RTGuard guard;
9✔
379
  int type_id{0};
9✔
380
  size_t size{0};
9✔
381
  auto status = typeart::detail::query_type(addr, &type_id, &size);
9✔
382
  if (status != TYPEART_OK) {
9✔
383
    return status;
×
384
  }
385
  return typeart::detail::query_struct_layout(type_id, struct_layout);
9✔
386
}
9✔
387

388
typeart_status typeart_resolve_type_id(int type_id, typeart_struct_layout* struct_layout) {
207✔
389
  typeart::RTGuard guard;
207✔
390
  return typeart::detail::query_struct_layout(type_id, struct_layout);
207✔
391
}
207✔
392

393
typeart_status typeart_get_return_address(const void* addr, const void** return_addr) {
45✔
394
  typeart::RTGuard guard;
45✔
395
  auto alloc = typeart::RuntimeSystem::get().allocTracker.findBaseAlloc(addr);
45✔
396

397
  if (alloc) {
45✔
398
    *return_addr = alloc.value().second.debug;
36✔
399
    return TYPEART_OK;
36✔
400
  }
401
  *return_addr = nullptr;
9✔
402
  return TYPEART_UNKNOWN_ADDRESS;
9✔
403
}
45✔
404

405
typeart_status_t typeart_get_source_location(const void* addr, char** file, char** function, char** line) {
9✔
406
  using namespace typeart::detail;
407
  typeart::RTGuard guard;
9✔
408

409
  auto source_loc = typeart::SourceLocation::create(addr);
9✔
410

411
  if (source_loc) {
9✔
412
    *file     = string2char(source_loc->file);
9✔
413
    *function = string2char(source_loc->function);
9✔
414
    *line     = string2char(source_loc->line);
9✔
415

416
    if (*file == nullptr || *function == nullptr || *line == nullptr) {
9✔
417
      return TYPEART_ERROR;
×
418
    }
419

420
    return TYPEART_OK;
9✔
421
  }
422

423
  return TYPEART_UNKNOWN_ADDRESS;
×
424
}
9✔
425

426
const char* typeart_get_type_name(int type_id) {
4,112✔
427
  typeart::RTGuard guard;
4,112✔
428
  return typeart::RuntimeSystem::get().typeResolution.db().getTypeName(type_id).c_str();
4,112✔
429
}
4,112✔
430

431
bool typeart_is_vector_type(int type_id) {
18✔
432
  typeart::RTGuard guard;
18✔
433
  return typeart::RuntimeSystem::get().typeResolution.db().isVectorType(type_id);
18✔
434
}
18✔
435

436
bool typeart_is_valid_type(int type_id) {
18✔
437
  typeart::RTGuard guard;
18✔
438
  return typeart::RuntimeSystem::get().typeResolution.db().isValid(type_id);
18✔
439
}
18✔
440

441
bool typeart_is_reserved_type(int type_id) {
18✔
442
  typeart::RTGuard guard;
18✔
443
  return typeart::RuntimeSystem::get().typeResolution.db().isReservedType(type_id);
18✔
444
}
18✔
445

446
bool typeart_is_builtin_type(int type_id) {
18✔
447
  typeart::RTGuard guard;
18✔
448
  return typeart::RuntimeSystem::get().typeResolution.db().isBuiltinType(type_id);
18✔
449
}
18✔
450

451
bool typeart_is_struct_type(int type_id) {
18✔
452
  typeart::RTGuard guard;
18✔
453
  return typeart::RuntimeSystem::get().typeResolution.db().isStructType(type_id);
18✔
454
}
18✔
455

456
bool typeart_is_userdefined_type(int type_id) {
18✔
457
  typeart::RTGuard guard;
18✔
458
  return typeart::RuntimeSystem::get().typeResolution.db().isUserDefinedType(type_id);
18✔
459
}
18✔
460

461
size_t typeart_get_type_size(int type_id) {
18✔
462
  typeart::RTGuard guard;
18✔
463
  return typeart::RuntimeSystem::get().typeResolution.db().getTypeSize(type_id);
18✔
464
}
18✔
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