Skip to content

Commit a9a4918

Browse files
Baltolirv-jenkins
andauthored
Print hook argument values on crash (#603)
* Implement minimal format function * Call through to format from error macro * Start writing better messages * INT hooks * Extract int/float toString * link properly * Float hook arguments * Remaining * Exn type * FMT submodule * FMT * Nixify fmt * Reduce duplication Co-authored-by: rv-jenkins <[email protected]>
1 parent 4eee49a commit a9a4918

27 files changed

+280
-141
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
path = deps/pybind11
99
url = ../../pybind/pybind11
1010
branch = stable
11+
[submodule "deps/fmt"]
12+
path = deps/fmt
13+
url = https://github.com/fmtlib/fmt.git

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ install(
171171
DIRECTORY include/runtime
172172
DESTINATION include/kllvm
173173
)
174+
install(
175+
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/fmt
176+
DESTINATION include/fmt
177+
)
174178
install(
175179
DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/immer
176180
DESTINATION include/kllvm
@@ -186,6 +190,7 @@ install(
186190
)
187191

188192
file(COPY ${PROJECT_SOURCE_DIR}/deps/immer/immer DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)
193+
file(COPY ${PROJECT_SOURCE_DIR}/deps/fmt/include/fmt DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)
189194
file(COPY ${PROJECT_SOURCE_DIR}/deps/rapidjson/include/rapidjson DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/include)
190195

191196
set(GC_THRESHOLD 2097152 CACHE STRING "Initial Young Generation Size")
@@ -255,6 +260,8 @@ add_subdirectory(lib)
255260
add_subdirectory(tools)
256261
add_subdirectory(runtime)
257262

263+
find_package(fmt)
264+
258265
if(BUILD_TESTS)
259266
add_subdirectory(unittests)
260267
endif()

bin/llvm-kompile-clang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ run @CMAKE_CXX_COMPILER@ -Wno-override-module -Wno-return-type-c-linkage "$modop
155155
"$MAINFILES" \
156156
"$LIBDIR"/libutil.a \
157157
"$LIBDIR"/libstrings.a \
158+
"$LIBDIR"/libnumeric_strings.a \
158159
"$LIBDIR"/libio.a \
159160
"$LIBDIR"/libcollections.a \
160161
"$LIBDIR"/libParser.a \

deps/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(base64)
2+
add_subdirectory(fmt)
23
add_subdirectory(pybind11)

deps/fmt

Submodule fmt added at a337011

flake.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
inputs = {
55
utils.url = "github:numtide/flake-utils";
66
nixpkgs.url = "github:nixos/nixpkgs";
7+
fmt-src.url =
8+
"github:fmtlib/fmt/9.1.0";
9+
fmt-src.flake = false;
710
immer-src.url =
811
"github:runtimeverification/immer/198c2ae260d49ef1800a2fe4433e07d7dec20059";
912
immer-src.flake = false;
@@ -16,15 +19,15 @@
1619
mavenix.url = "github:nix-community/mavenix";
1720
};
1821

19-
outputs = { self, nixpkgs, utils, immer-src, rapidjson-src, pybind11-src, mavenix }:
22+
outputs = { self, nixpkgs, utils, fmt-src, immer-src, rapidjson-src, pybind11-src, mavenix }:
2023
let
2124
inherit (nixpkgs) lib;
2225

2326
# put devShell and any other required packages into local overlay
2427
# if you have additional overlays, you may add them here
2528
localOverlay = import ./nix/overlay.nix; # this should expose devShell
2629
depsOverlay = (final: prev: {
27-
inherit immer-src rapidjson-src pybind11-src;
30+
inherit fmt-src immer-src rapidjson-src pybind11-src;
2831

2932
llvm-backend-src = prev.stdenv.mkDerivation {
3033
name = "llvm-backend-src";
@@ -41,9 +44,11 @@
4144
mkdir $out
4245
cp -rv $src/* $out
4346
chmod -R u+w $out
47+
mkdir -p $out/deps/fmt
4448
mkdir -p $out/deps/immer
4549
mkdir -p $out/deps/rapidjson
4650
mkdir -p $out/deps/pybind11
51+
cp -rv ${final.fmt-src}/* $out/deps/fmt
4752
cp -rv ${final.immer-src}/* $out/deps/immer
4853
cp -rv ${final.rapidjson-src}/* $out/deps/rapidjson
4954
cp -rv ${final.pybind11-src}/* $out/deps/pybind11

include/runtime/header.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdint>
66
#include <limits>
77

8+
#include <fmt/format.h>
89
#include <gmp.h>
910
#include <mpfr.h>
1011

@@ -275,10 +276,13 @@ block *strip_injection(block *);
275276
std::string floatToString(const floating *);
276277
void init_float2(floating *, std::string);
277278

278-
#define KLLVM_HOOK_INVALID_ARGUMENT(msg) \
279+
std::string intToStringInBase(mpz_t, uint64_t);
280+
std::string intToString(mpz_t);
281+
282+
#define KLLVM_HOOK_INVALID_ARGUMENT(...) \
279283
do { \
280-
auto err_msg = std::string("[") + std::string(__func__) \
281-
+ std::string("]: ") + std::string(msg); \
284+
auto err_msg \
285+
= ::fmt::format("[{}]: {}", __func__, ::fmt::format(__VA_ARGS__)); \
282286
throw std::invalid_argument(err_msg); \
283287
} while (false)
284288

runtime/arithmetic/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ add_library(arithmetic STATIC
33
float.cpp
44
)
55

6+
target_link_libraries(arithmetic
7+
numeric_strings fmt::fmt-header-only
8+
)
9+
610
install(
711
TARGETS arithmetic
812
ARCHIVE DESTINATION lib/kllvm

runtime/arithmetic/float.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ SortFloat hook_FLOAT_trunc(SortFloat a) {
8484

8585
SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {
8686
if (!mpz_fits_ulong_p(prec)) {
87-
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
87+
KLLVM_HOOK_INVALID_ARGUMENT(
88+
"Precision out of range: {}", intToString(prec));
8889
}
8990
unsigned long uprec = mpz_get_ui(prec);
9091
if (!mpz_fits_ulong_p(exp)) {
91-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
92+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range: {}", intToString(exp));
9293
}
9394
unsigned long uexp = mpz_get_ui(exp);
9495
floating result[1];
@@ -100,7 +101,7 @@ SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {
100101

101102
mpz_ptr hook_FLOAT_float2int(SortFloat a) {
102103
if (!mpfr_number_p(a->f)) {
103-
KLLVM_HOOK_INVALID_ARGUMENT("Not a finite number");
104+
KLLVM_HOOK_INVALID_ARGUMENT("Not a finite number: {}", floatToString(a));
104105
}
105106
mpz_t result;
106107
mpz_init(result);
@@ -110,11 +111,12 @@ mpz_ptr hook_FLOAT_float2int(SortFloat a) {
110111

111112
SortFloat hook_FLOAT_int2float(SortInt a, SortInt prec, SortInt exp) {
112113
if (!mpz_fits_ulong_p(prec)) {
113-
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
114+
KLLVM_HOOK_INVALID_ARGUMENT(
115+
"Precision out of range: {}", intToString(prec));
114116
}
115117
unsigned long uprec = mpz_get_ui(prec);
116118
if (!mpz_fits_ulong_p(exp)) {
117-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
119+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range: {}", intToString(exp));
118120
}
119121
unsigned long uexp = mpz_get_ui(exp);
120122
floating result[1];
@@ -263,11 +265,12 @@ bool hook_FLOAT_isNaN(SortFloat a) {
263265

264266
SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {
265267
if (!mpz_fits_ulong_p(prec)) {
266-
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
268+
KLLVM_HOOK_INVALID_ARGUMENT(
269+
"Precision out of range: {}", intToString(prec));
267270
}
268271
unsigned long uprec = mpz_get_ui(prec);
269272
if (!mpz_fits_ulong_p(exp)) {
270-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
273+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range: {}", intToString(exp));
271274
}
272275
unsigned long uexp = mpz_get_ui(exp);
273276
floating result[1];
@@ -280,11 +283,12 @@ SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {
280283

281284
SortFloat hook_FLOAT_minValue(SortInt prec, SortInt exp) {
282285
if (!mpz_fits_ulong_p(prec)) {
283-
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
286+
KLLVM_HOOK_INVALID_ARGUMENT(
287+
"Precision out of range: {}", intToString(prec));
284288
}
285289
unsigned long uprec = mpz_get_ui(prec);
286290
if (!mpz_fits_ulong_p(exp)) {
287-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
291+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range: {}", intToString(exp));
288292
}
289293
unsigned long uexp = mpz_get_ui(exp);
290294
floating result[1];
@@ -423,7 +427,7 @@ SortFloat hook_FLOAT_pow(SortFloat a, SortFloat b) {
423427

424428
SortFloat hook_FLOAT_root(SortFloat a, SortInt b) {
425429
if (!mpz_fits_ulong_p(b)) {
426-
KLLVM_HOOK_INVALID_ARGUMENT("Root out of range");
430+
KLLVM_HOOK_INVALID_ARGUMENT("Root out of range: {}", intToString(b));
427431
}
428432
unsigned long root = mpz_get_ui(b);
429433
floating result[1];
@@ -456,11 +460,12 @@ bool hook_FLOAT_sign(SortFloat a) {
456460
SortFloat hook_FLOAT_rat2float(
457461
SortInt numerator, SortInt denominator, SortInt prec, SortInt exp) {
458462
if (!mpz_fits_ulong_p(prec)) {
459-
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
463+
KLLVM_HOOK_INVALID_ARGUMENT(
464+
"Precision out of range: {}", intToString(prec));
460465
}
461466
unsigned long uprec = mpz_get_ui(prec);
462467
if (!mpz_fits_ulong_p(exp)) {
463-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
468+
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range: {}", intToString(exp));
464469
}
465470
unsigned long uexp = mpz_get_ui(exp);
466471

runtime/arithmetic/int.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ void add_hash64(void *, uint64_t);
1313
SortInt hook_INT_tmod(SortInt a, SortInt b) {
1414
mpz_t result;
1515
if (mpz_sgn(b) == 0) {
16-
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
16+
KLLVM_HOOK_INVALID_ARGUMENT(
17+
"Modulus by zero: {} % {}", intToString(a), intToString(b));
1718
}
1819
mpz_init(result);
1920
mpz_tdiv_r(result, a, b);
@@ -23,7 +24,8 @@ SortInt hook_INT_tmod(SortInt a, SortInt b) {
2324
SortInt hook_INT_emod(SortInt a, SortInt b) {
2425
mpz_t result;
2526
if (mpz_sgn(b) == 0) {
26-
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
27+
KLLVM_HOOK_INVALID_ARGUMENT(
28+
"Modulus by zero: {} % {}", intToString(a), intToString(b));
2729
}
2830
mpz_init(result);
2931
mpz_tdiv_r(result, a, b);
@@ -81,7 +83,8 @@ SortInt hook_INT_sub(SortInt a, SortInt b) {
8183
SortInt hook_INT_tdiv(SortInt a, SortInt b) {
8284
mpz_t result;
8385
if (mpz_sgn(b) == 0) {
84-
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
86+
KLLVM_HOOK_INVALID_ARGUMENT(
87+
"Division by zero: {} / {}", intToString(a), intToString(b));
8588
}
8689
mpz_init(result);
8790
mpz_tdiv_q(result, a, b);
@@ -91,7 +94,8 @@ SortInt hook_INT_tdiv(SortInt a, SortInt b) {
9194
SortInt hook_INT_ediv(SortInt a, SortInt b) {
9295
mpz_t result;
9396
if (mpz_sgn(b) == 0) {
94-
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
97+
KLLVM_HOOK_INVALID_ARGUMENT(
98+
"Division by zero: {} / {}", intToString(a), intToString(b));
9599
}
96100
mpz_init(result);
97101
if (mpz_sgn(b) >= 0) {
@@ -105,7 +109,9 @@ SortInt hook_INT_ediv(SortInt a, SortInt b) {
105109
SortInt hook_INT_shl(SortInt a, SortInt b) {
106110
mpz_t result;
107111
if (!mpz_fits_ulong_p(b)) {
108-
KLLVM_HOOK_INVALID_ARGUMENT("Shift amount out of range");
112+
KLLVM_HOOK_INVALID_ARGUMENT(
113+
"Left shift amount out of range: {} << {}", intToString(a),
114+
intToString(b));
109115
}
110116
mpz_init(result);
111117
unsigned long blong = mpz_get_ui(b);
@@ -126,7 +132,9 @@ SortInt hook_INT_shr(SortInt a, SortInt b) {
126132
mpz_init(result);
127133
if (!mpz_fits_ulong_p(b)) {
128134
if (mpz_sgn(b) < 0) {
129-
KLLVM_HOOK_INVALID_ARGUMENT("Negative shift amount");
135+
KLLVM_HOOK_INVALID_ARGUMENT(
136+
"Negative right shift amount: {} >> {}", intToString(a),
137+
intToString(b));
130138
}
131139
if (mpz_sgn(a) < 0) {
132140
mpz_set_si(result, -1);
@@ -145,7 +153,8 @@ bool hook_INT_gt(SortInt a, SortInt b) {
145153
SortInt hook_INT_pow(SortInt a, SortInt b) {
146154
mpz_t result;
147155
if (!mpz_fits_ulong_p(b)) {
148-
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
156+
KLLVM_HOOK_INVALID_ARGUMENT(
157+
"Exponent out of range: {} ^ {}", intToString(a), intToString(b));
149158
}
150159
mpz_init(result);
151160
unsigned long blong = mpz_get_ui(b);
@@ -160,7 +169,9 @@ SortInt hook_INT_powmod(SortInt a, SortInt b, SortInt mod) {
160169
mpz_gcd(result, a, mod);
161170
if (mpz_cmp_ui(result, 1) != 0) {
162171
mpz_clear(result);
163-
KLLVM_HOOK_INVALID_ARGUMENT("Modular inverse not defined");
172+
KLLVM_HOOK_INVALID_ARGUMENT(
173+
"Modular inverse not defined: {} ^ {} % {}", intToString(a),
174+
intToString(b), intToString(mod));
164175
}
165176
}
166177
mpz_powm(result, a, b, mod);
@@ -220,7 +231,8 @@ SortInt hook_INT_min(SortInt a, SortInt b) {
220231
SortInt hook_INT_log2(SortInt a) {
221232
mpz_t result;
222233
if (mpz_sgn(a) <= 0) {
223-
KLLVM_HOOK_INVALID_ARGUMENT("Logarithm of nonpositive integer");
234+
KLLVM_HOOK_INVALID_ARGUMENT(
235+
"Logarithm of nonpositive integer: log2({})", intToString(a));
224236
}
225237
mpz_init(result);
226238
size_t log = mpz_sizeinbase(a, 2) - 1;
@@ -278,12 +290,12 @@ SortInt hook_INT_bitRange(SortInt i, SortInt off, SortInt len) {
278290
return move_int(result);
279291
}
280292
if (!mpz_fits_ulong_p(len)) {
281-
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
293+
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range: {}", intToString(len));
282294
}
283295
unsigned long lenlong = mpz_get_ui(len);
284296
if (!mpz_fits_ulong_p(off)) {
285297
if (mpz_sgn(off) < 0) {
286-
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
298+
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset: {}", intToString(off));
287299
}
288300
mpz_init(result);
289301
if (mpz_sgn(i) < 0) {
@@ -324,7 +336,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
324336
mpz_t result;
325337
if (!mpz_fits_ulong_p(off)) {
326338
if (mpz_sgn(off) < 0) {
327-
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
339+
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset: {}", intToString(off));
328340
}
329341
mpz_init(result);
330342
if (mpz_sgn(i) < 0) {
@@ -333,7 +345,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
333345
return move_int(result);
334346
}
335347
if (!mpz_fits_ulong_p(len)) {
336-
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
348+
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range: {}", intToString(len));
337349
}
338350
unsigned long offlong = mpz_get_ui(off);
339351
unsigned long lenlong = mpz_get_ui(len);

runtime/collections/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ install(
99
TARGETS collections
1010
ARCHIVE DESTINATION lib/kllvm
1111
)
12+
13+
target_link_libraries(collections
14+
numeric_strings fmt::fmt-header-only)

0 commit comments

Comments
 (0)