-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRuntimeRuleUtils.cpp
More file actions
655 lines (532 loc) · 15.3 KB
/
Copy pathRuntimeRuleUtils.cpp
File metadata and controls
655 lines (532 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/**
*
* @file RuntimeRuleUtils.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/errors/runtime/RuntimeRuleUtils.hpp>
#include <vix/cli/errors/CodeFrame.hpp>
#include <vix/cli/errors/ErrorContext.hpp>
#include <cctype>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include <system_error>
#include <algorithm>
#include <cctype>
#include <filesystem>
#include <vix/cli/Style.hpp>
using namespace vix::cli::style;
namespace vix::cli::errors::runtime
{
namespace
{
bool is_probably_text_file(
const std::filesystem::path &path)
{
if (path.empty())
return false;
std::error_code error;
if (!std::filesystem::is_regular_file(path, error) ||
error)
{
return false;
}
std::ifstream input(path, std::ios::binary);
if (!input)
return false;
std::array<char, 8192> sample{};
input.read(
sample.data(),
static_cast<std::streamsize>(sample.size()));
const std::size_t count =
static_cast<std::size_t>(input.gcount());
if (count == 0)
return true;
std::size_t suspiciousBytes = 0;
for (std::size_t i = 0; i < count; ++i)
{
const unsigned char byte =
static_cast<unsigned char>(sample[i]);
/*
* A NUL byte is a strong indication that this is an
* executable, object file or another binary format.
*/
if (byte == 0)
return false;
/*
* Allow tabs, newlines and carriage returns. Count the
* remaining ASCII control characters as suspicious.
*/
if (byte < 0x09 ||
(byte > 0x0D && byte < 0x20))
{
++suspiciousBytes;
}
}
/*
* A normal source/configuration file should contain almost
* no unsupported control characters.
*/
return suspiciousBytes * 100 <= count * 2;
}
bool is_digit(char c) noexcept
{
return std::isdigit(static_cast<unsigned char>(c)) != 0;
}
std::optional<int> parse_positive_int(
const std::string &text,
std::size_t begin,
std::size_t *endOut = nullptr)
{
if (begin >= text.size() || !is_digit(text[begin]))
return std::nullopt;
int value = 0;
std::size_t i = begin;
while (i < text.size() && is_digit(text[i]))
{
value = (value * 10) + (text[i] - '0');
++i;
}
if (endOut)
*endOut = i;
return value;
}
RuntimeLocation parse_location_at(
const std::string &text,
std::size_t pathStart,
std::size_t pathEnd)
{
RuntimeLocation location{};
if (pathStart >= pathEnd || pathEnd >= text.size())
return location;
if (text[pathEnd] != ':')
return location;
std::size_t afterLine = 0;
const auto parsedLine = parse_positive_int(text, pathEnd + 1, &afterLine);
if (!parsedLine)
return location;
int column = 1;
if (afterLine < text.size() && text[afterLine] == ':')
{
std::size_t afterColumn = 0;
const auto parsedColumn =
parse_positive_int(text, afterLine + 1, &afterColumn);
if (parsedColumn)
column = *parsedColumn;
}
location.file = std::filesystem::path(
text.substr(pathStart, pathEnd - pathStart));
location.line = *parsedLine;
location.column = column;
return location;
}
RuntimeLocation find_location_from_exact_source(
const std::string &line,
const std::filesystem::path &sourceFile)
{
if (sourceFile.empty())
return {};
const std::string source = sourceFile.string();
const std::size_t pos = line.find(source);
if (pos == std::string::npos)
return {};
return parse_location_at(line, pos, pos + source.size());
}
RuntimeLocation find_location_from_filename(
const std::string &line,
const std::filesystem::path &sourceFile)
{
if (sourceFile.empty())
return {};
const std::string filename = sourceFile.filename().string();
if (filename.empty())
return {};
const std::size_t filenamePos = line.find(filename);
if (filenamePos == std::string::npos)
return {};
const std::size_t filenameEnd = filenamePos + filename.size();
if (filenameEnd >= line.size() || line[filenameEnd] != ':')
return {};
std::size_t pathStart = filenamePos;
while (pathStart > 0)
{
const char c = line[pathStart - 1];
if (std::isspace(static_cast<unsigned char>(c)) != 0 ||
c == '(' ||
c == '[')
{
break;
}
--pathStart;
}
return parse_location_at(line, pathStart, filenameEnd);
}
bool is_system_source_path(
const std::filesystem::path &path)
{
const std::string value =
path.lexically_normal().generic_string();
return value.rfind("/usr/include/", 0) == 0 ||
value.rfind("/usr/lib/", 0) == 0 ||
value.rfind("/usr/local/include/", 0) == 0 ||
value.rfind("/usr/local/lib/", 0) == 0 ||
value.rfind("/lib/", 0) == 0 ||
value.rfind("/lib64/", 0) == 0 ||
value.find("/libsanitizer/") != std::string::npos;
}
bool has_source_extension(
const std::filesystem::path &path)
{
std::string extension =
path.extension().string();
std::transform(
extension.begin(),
extension.end(),
extension.begin(),
[](unsigned char character)
{
return static_cast<char>(
std::tolower(character));
});
return extension == ".c" ||
extension == ".cc" ||
extension == ".cpp" ||
extension == ".cxx" ||
extension == ".h" ||
extension == ".hh" ||
extension == ".hpp" ||
extension == ".hxx" ||
extension == ".ipp" ||
extension == ".inl";
}
RuntimeLocation find_location_from_absolute_source(
const std::string &line)
{
std::size_t pathStart =
line.find('/');
while (pathStart != std::string::npos)
{
std::size_t pathEnd =
line.find(':', pathStart);
while (pathEnd != std::string::npos)
{
RuntimeLocation location =
parse_location_at(
line,
pathStart,
pathEnd);
if (location.valid() &&
location.file.is_absolute() &&
has_source_extension(location.file) &&
!is_system_source_path(location.file) &&
is_probably_text_file(location.file))
{
return location;
}
pathEnd =
line.find(':', pathEnd + 1);
}
pathStart =
line.find('/', pathStart + 1);
}
return {};
}
} // namespace
bool icontains(const std::string &text, const std::string &needle)
{
if (needle.empty())
return true;
auto lower = [](unsigned char c) -> char
{
if (c >= 'A' && c <= 'Z')
return static_cast<char>(c + ('a' - 'A'));
return static_cast<char>(c);
};
if (text.size() < needle.size())
return false;
for (std::size_t i = 0; i + needle.size() <= text.size(); ++i)
{
bool ok = true;
for (std::size_t j = 0; j < needle.size(); ++j)
{
if (lower(static_cast<unsigned char>(text[i + j])) !=
lower(static_cast<unsigned char>(needle[j])))
{
ok = false;
break;
}
}
if (ok)
return true;
}
return false;
}
bool runtime_technical_details_enabled()
{
const char *level = std::getenv("VIX_LOG_LEVEL");
if (level == nullptr || *level == '\0')
return false;
std::string value(level);
std::transform(
value.begin(),
value.end(),
value.begin(),
[](unsigned char character)
{
return static_cast<char>(std::tolower(character));
});
return value == "debug" || value == "trace";
}
std::string strip_line_comment(const std::string &line)
{
const std::size_t pos = line.find("//");
if (pos == std::string::npos)
return line;
return line.substr(0, pos);
}
std::optional<std::vector<std::string>> read_file_lines(
const std::filesystem::path &path)
{
if (!is_probably_text_file(path))
return std::nullopt;
std::ifstream ifs(path);
if (!ifs)
return std::nullopt;
std::vector<std::string> lines;
std::string line;
while (std::getline(ifs, line))
lines.push_back(line);
return lines;
}
RuntimeLocation find_best_runtime_location(
const std::string &log,
const std::filesystem::path &sourceFile)
{
/*
* A caller may provide a source file for standalone mode.
*
* Project mode normally provides no source file, so sanitizer
* frames must also be able to identify their own source path.
*/
std::filesystem::path fallbackSource =
sourceFile;
if (!fallbackSource.empty() &&
!is_probably_text_file(fallbackSource))
{
fallbackSource.clear();
}
std::istringstream input(log);
std::string line;
while (std::getline(input, line))
{
/*
* Sanitizer stack frames begin with entries such as:
*
* #1 ... /project/src/main.cpp:16
*/
if (line.find('#') == std::string::npos)
continue;
RuntimeLocation location =
find_location_from_exact_source(
line,
fallbackSource);
if (location.valid())
return location;
location =
find_location_from_filename(
line,
fallbackSource);
if (location.valid())
return location;
/*
* Project-mode runs do not have one predefined source file.
* Extract the first existing non-system source file directly
* from the sanitizer frame.
*/
location =
find_location_from_absolute_source(line);
if (location.valid())
return location;
}
return {};
}
RuntimeLocation find_best_runtime_location_or_source_hint(
const std::string &log,
const std::filesystem::path &sourceFile,
const std::vector<std::string> &sourcePatterns)
{
RuntimeLocation location =
find_best_runtime_location(log, sourceFile);
if (location.valid())
return location;
if (sourceFile.empty() ||
sourcePatterns.empty() ||
!is_probably_text_file(sourceFile))
{
return {};
}
const auto lines = read_file_lines(sourceFile);
if (!lines)
return {};
for (std::size_t i = 0; i < lines->size(); ++i)
{
const std::string cleanLine =
strip_line_comment((*lines)[i]);
for (const auto &pattern : sourcePatterns)
{
if (pattern.empty())
continue;
const std::size_t pos =
cleanLine.find(pattern);
if (pos == std::string::npos)
continue;
RuntimeLocation hinted{};
hinted.file = sourceFile;
hinted.line = static_cast<int>(i + 1);
hinted.column = static_cast<int>(pos + 1);
return hinted;
}
}
return {};
}
std::string make_at_text(
const RuntimeLocation &location,
[[maybe_unused]] const std::filesystem::path &sourceFile)
{
if (location.valid() &&
is_probably_text_file(location.file))
{
return location.file.string() +
":" +
std::to_string(location.line);
}
return {};
}
vix::cli::errors::CompilerError make_runtime_location(
const std::filesystem::path &sourceFile,
int line,
int column,
const std::string &message)
{
vix::cli::errors::CompilerError err;
err.file = sourceFile.string();
err.line = line;
err.column = column;
err.message = message;
return err;
}
void print_runtime_codeframe(
const vix::cli::errors::CompilerError &err)
{
const std::filesystem::path file = err.file;
/*
* Last safety boundary: even if a rule accidentally returns
* an executable path, CodeFrame must never print it.
*/
if (!is_probably_text_file(file))
return;
ErrorContext ctx;
CodeFrameOptions opt;
opt.contextLines = 2;
opt.maxLineWidth = 120;
opt.tabWidth = 4;
opt.leadingBlankLine = false;
printCodeFrame(err, ctx, opt);
}
void print_runtime_hints_and_at(
const std::vector<std::string> &hints,
const std::string &at)
{
for (const auto &hintText : hints)
{
std::cerr << YELLOW
<< "hint: "
<< RESET
<< hintText
<< "\n";
}
if (!at.empty())
{
std::cerr << GREEN
<< "at: "
<< RESET
<< at
<< "\n";
}
}
void print_runtime_log_excerpt(
const std::string &log,
std::size_t maxLines)
{
if (!runtime_technical_details_enabled() ||
log.empty() || maxLines == 0)
return;
std::istringstream input(log);
std::vector<std::string> lines;
std::string line;
while (std::getline(input, line))
lines.push_back(line);
if (lines.empty())
return;
std::size_t firstInteresting = lines.size();
for (std::size_t i = 0; i < lines.size(); ++i)
{
const std::string ¤t = lines[i];
const bool interesting =
icontains(current, "terminate called") ||
icontains(current, "terminate called without an active exception") ||
icontains(current, "std::terminate") ||
icontains(current, "std::thread") ||
icontains(current, "thread::~thread") ||
icontains(current, "~thread") ||
icontains(current, "what():") ||
icontains(current, "Aborted") ||
icontains(current, "SIGABRT") ||
icontains(current, "core dumped") ||
icontains(current, "websocket") ||
icontains(current, "LowLevelServer") ||
icontains(current, "Session") ||
icontains(current, "#");
if (interesting)
{
firstInteresting = i;
break;
}
}
const std::size_t start =
firstInteresting == lines.size()
? 0
: (firstInteresting >= 2 ? firstInteresting - 2 : 0);
const std::size_t end =
std::min(lines.size(), start + maxLines);
if (start >= end)
return;
std::cerr << "\n"
<< RED
<< "runtime log:"
<< RESET
<< "\n";
for (std::size_t i = start; i < end; ++i)
{
if (lines[i].empty())
continue;
std::cerr << " " << lines[i] << "\n";
}
std::cerr << "\n";
}
} // namespace vix::cli::errors::runtime