Skip to content

Better error messages from hooks #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/runtime/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,11 @@ block *strip_injection(block *);
std::string floatToString(const floating *);
void init_float2(floating *, std::string);

#define KLLVM_HOOK_INVALID_ARGUMENT(msg) \
do { \
auto err_msg = std::string("[") + std::string(__func__) \
+ std::string("]: ") + std::string(msg); \
throw std::invalid_argument(err_msg); \
} while (false)

Comment on lines +276 to +282
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this PR are largely mechanical; replacing throw std::invalid_argument with a call to this macro.

#endif // RUNTIME_HEADER_H
28 changes: 14 additions & 14 deletions runtime/arithmetic/float.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ floating *move_float(floating *);
mpz_ptr move_int(mpz_t);
void add_hash64(void *, uint64_t);
void *move_mint(mpz_t, uint64_t) {
throw std::invalid_argument("not yet implemented");
KLLVM_HOOK_INVALID_ARGUMENT("not yet implemented");
}

SortFloat hook_FLOAT_ceil(SortFloat a) {
Expand Down Expand Up @@ -84,11 +84,11 @@ SortFloat hook_FLOAT_trunc(SortFloat a) {

SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {
if (!mpz_fits_ulong_p(prec)) {
throw std::invalid_argument("Precision out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
}
unsigned long uprec = mpz_get_ui(prec);
if (!mpz_fits_ulong_p(exp)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
unsigned long uexp = mpz_get_ui(exp);
floating result[1];
Expand All @@ -100,7 +100,7 @@ SortFloat hook_FLOAT_round(SortFloat a, SortInt prec, SortInt exp) {

mpz_ptr hook_FLOAT_float2int(SortFloat a) {
if (!mpfr_number_p(a->f)) {
throw std::invalid_argument("Not a finite number");
KLLVM_HOOK_INVALID_ARGUMENT("Not a finite number");
}
mpz_t result;
mpz_init(result);
Expand All @@ -110,11 +110,11 @@ mpz_ptr hook_FLOAT_float2int(SortFloat a) {

SortFloat hook_FLOAT_int2float(SortInt a, SortInt prec, SortInt exp) {
if (!mpz_fits_ulong_p(prec)) {
throw std::invalid_argument("Precision out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
}
unsigned long uprec = mpz_get_ui(prec);
if (!mpz_fits_ulong_p(exp)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
unsigned long uexp = mpz_get_ui(exp);
floating result[1];
Expand Down Expand Up @@ -237,7 +237,7 @@ mpz_ptr hook_FLOAT_exponent(SortFloat a) {

void *hook_FLOAT_significand(SortFloat a) {
if (mpfr_nan_p(a->f)) {
throw std::invalid_argument("NaN payload is undefined");
KLLVM_HOOK_INVALID_ARGUMENT("NaN payload is undefined");
}
mpfr_prec_t prec = mpfr_get_prec(a->f);
uint64_t len = (prec + 7) / 8;
Expand All @@ -263,11 +263,11 @@ bool hook_FLOAT_isNaN(SortFloat a) {

SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {
if (!mpz_fits_ulong_p(prec)) {
throw std::invalid_argument("Precision out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
}
unsigned long uprec = mpz_get_ui(prec);
if (!mpz_fits_ulong_p(exp)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
unsigned long uexp = mpz_get_ui(exp);
floating result[1];
Expand All @@ -280,11 +280,11 @@ SortFloat hook_FLOAT_maxValue(SortInt prec, SortInt exp) {

SortFloat hook_FLOAT_minValue(SortInt prec, SortInt exp) {
if (!mpz_fits_ulong_p(prec)) {
throw std::invalid_argument("Precision out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
}
unsigned long uprec = mpz_get_ui(prec);
if (!mpz_fits_ulong_p(exp)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
unsigned long uexp = mpz_get_ui(exp);
floating result[1];
Expand Down Expand Up @@ -423,7 +423,7 @@ SortFloat hook_FLOAT_pow(SortFloat a, SortFloat b) {

SortFloat hook_FLOAT_root(SortFloat a, SortInt b) {
if (!mpz_fits_ulong_p(b)) {
throw std::invalid_argument("Root out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Root out of range");
}
unsigned long root = mpz_get_ui(b);
floating result[1];
Expand Down Expand Up @@ -456,11 +456,11 @@ bool hook_FLOAT_sign(SortFloat a) {
SortFloat hook_FLOAT_rat2float(
SortInt numerator, SortInt denominator, SortInt prec, SortInt exp) {
if (!mpz_fits_ulong_p(prec)) {
throw std::invalid_argument("Precision out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Precision out of range");
}
unsigned long uprec = mpz_get_ui(prec);
if (!mpz_fits_ulong_p(exp)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
unsigned long uexp = mpz_get_ui(exp);

Expand Down
26 changes: 13 additions & 13 deletions runtime/arithmetic/int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void add_hash64(void *, uint64_t);
SortInt hook_INT_tmod(SortInt a, SortInt b) {
mpz_t result;
if (mpz_sgn(b) == 0) {
throw std::invalid_argument("Modulus by zero");
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
}
mpz_init(result);
mpz_tdiv_r(result, a, b);
Expand All @@ -23,7 +23,7 @@ SortInt hook_INT_tmod(SortInt a, SortInt b) {
SortInt hook_INT_emod(SortInt a, SortInt b) {
mpz_t result;
if (mpz_sgn(b) == 0) {
throw std::invalid_argument("Modulus by zero");
KLLVM_HOOK_INVALID_ARGUMENT("Modulus by zero");
}
mpz_init(result);
mpz_tdiv_r(result, a, b);
Expand Down Expand Up @@ -81,7 +81,7 @@ SortInt hook_INT_sub(SortInt a, SortInt b) {
SortInt hook_INT_tdiv(SortInt a, SortInt b) {
mpz_t result;
if (mpz_sgn(b) == 0) {
throw std::invalid_argument("Division by zero");
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
}
mpz_init(result);
mpz_tdiv_q(result, a, b);
Expand All @@ -91,7 +91,7 @@ SortInt hook_INT_tdiv(SortInt a, SortInt b) {
SortInt hook_INT_ediv(SortInt a, SortInt b) {
mpz_t result;
if (mpz_sgn(b) == 0) {
throw std::invalid_argument("Division by zero");
KLLVM_HOOK_INVALID_ARGUMENT("Division by zero");
}
mpz_init(result);
if (mpz_sgn(b) >= 0) {
Expand All @@ -105,7 +105,7 @@ SortInt hook_INT_ediv(SortInt a, SortInt b) {
SortInt hook_INT_shl(SortInt a, SortInt b) {
mpz_t result;
if (!mpz_fits_ulong_p(b)) {
throw std::invalid_argument("Shift amount out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Shift amount out of range");
}
mpz_init(result);
unsigned long blong = mpz_get_ui(b);
Expand All @@ -126,7 +126,7 @@ SortInt hook_INT_shr(SortInt a, SortInt b) {
mpz_init(result);
if (!mpz_fits_ulong_p(b)) {
if (mpz_sgn(b) < 0) {
throw std::invalid_argument("Negative shift amount");
KLLVM_HOOK_INVALID_ARGUMENT("Negative shift amount");
}
if (mpz_sgn(a) < 0) {
mpz_set_si(result, -1);
Expand All @@ -145,7 +145,7 @@ bool hook_INT_gt(SortInt a, SortInt b) {
SortInt hook_INT_pow(SortInt a, SortInt b) {
mpz_t result;
if (!mpz_fits_ulong_p(b)) {
throw std::invalid_argument("Exponent out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Exponent out of range");
}
mpz_init(result);
unsigned long blong = mpz_get_ui(b);
Expand All @@ -160,7 +160,7 @@ SortInt hook_INT_powmod(SortInt a, SortInt b, SortInt mod) {
mpz_gcd(result, a, mod);
if (mpz_cmp_ui(result, 1) != 0) {
mpz_clear(result);
throw std::invalid_argument("Modular inverse not defined");
KLLVM_HOOK_INVALID_ARGUMENT("Modular inverse not defined");
}
}
mpz_powm(result, a, b, mod);
Expand Down Expand Up @@ -220,7 +220,7 @@ SortInt hook_INT_min(SortInt a, SortInt b) {
SortInt hook_INT_log2(SortInt a) {
mpz_t result;
if (mpz_sgn(a) <= 0) {
throw std::invalid_argument("Logarithm of nonpositive integer");
KLLVM_HOOK_INVALID_ARGUMENT("Logarithm of nonpositive integer");
}
mpz_init(result);
size_t log = mpz_sizeinbase(a, 2) - 1;
Expand Down Expand Up @@ -278,12 +278,12 @@ SortInt hook_INT_bitRange(SortInt i, SortInt off, SortInt len) {
return move_int(result);
}
if (!mpz_fits_ulong_p(len)) {
throw std::invalid_argument("Length out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
}
unsigned long lenlong = mpz_get_ui(len);
if (!mpz_fits_ulong_p(off)) {
if (mpz_sgn(off) < 0) {
throw std::invalid_argument("Negative offset");
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
}
mpz_init(result);
if (mpz_sgn(i) < 0) {
Expand Down Expand Up @@ -324,7 +324,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
mpz_t result;
if (!mpz_fits_ulong_p(off)) {
if (mpz_sgn(off) < 0) {
throw std::invalid_argument("Negative offset");
KLLVM_HOOK_INVALID_ARGUMENT("Negative offset");
}
mpz_init(result);
if (mpz_sgn(i) < 0) {
Expand All @@ -333,7 +333,7 @@ SortInt hook_INT_signExtendBitRange(SortInt i, SortInt off, SortInt len) {
return move_int(result);
}
if (!mpz_fits_ulong_p(len)) {
throw std::invalid_argument("Length out of range");
KLLVM_HOOK_INVALID_ARGUMENT("Length out of range");
}
unsigned long offlong = mpz_get_ui(off);
unsigned long lenlong = mpz_get_ui(len);
Expand Down
20 changes: 10 additions & 10 deletions runtime/collections/lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool hook_LIST_in(SortKItem value, SortList list) {

bool hook_LIST_in_keys(SortInt index, SortList list) {
if (!mpz_fits_ulong_p(index)) {
throw std::invalid_argument("Index is too large for in_keys");
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for in_keys");
}
size_t idx = mpz_get_ui(index);
return idx < list->size();
Expand All @@ -50,7 +50,7 @@ SortKItem hook_LIST_get_long(SortList list, ssize_t idx) {

SortKItem hook_LIST_get(SortList list, SortInt index) {
if (!mpz_fits_slong_p(index)) {
throw std::invalid_argument("Index is too large for get");
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for get");
}
ssize_t idx = mpz_get_si(index);
return hook_LIST_get_long(list, idx);
Expand All @@ -75,7 +75,7 @@ list hook_LIST_range_long(SortList list, size_t front, size_t back) {

list hook_LIST_range(SortList list, SortInt from_front, SortInt from_back) {
if (!mpz_fits_ulong_p(from_front) || !mpz_fits_ulong_p(from_back)) {
throw std::invalid_argument("Range index too large for range");
KLLVM_HOOK_INVALID_ARGUMENT("Range index too large for range");
}

size_t front = mpz_get_ui(from_front);
Expand All @@ -96,7 +96,7 @@ SortInt hook_LIST_size(SortList list) {

list hook_LIST_make(SortInt len, SortKItem value) {
if (!mpz_fits_ulong_p(len)) {
throw std::invalid_argument("Length is too large for make");
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for make");
}

size_t length = mpz_get_ui(len);
Expand All @@ -105,28 +105,28 @@ list hook_LIST_make(SortInt len, SortKItem value) {

list hook_LIST_update(SortList list, SortInt index, SortKItem value) {
if (!mpz_fits_ulong_p(index)) {
throw std::invalid_argument("Length is too large for update");
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for update");
}

size_t idx = mpz_get_ui(index);
if (idx >= list->size()) {
throw std::invalid_argument("Index out of range for update");
KLLVM_HOOK_INVALID_ARGUMENT("Index out of range for update");
}

return list->set(idx, value);
}

list hook_LIST_updateAll(SortList l1, SortInt index, SortList l2) {
if (!mpz_fits_ulong_p(index)) {
throw std::invalid_argument("Length is too large for updateAll");
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for updateAll");
}

size_t idx = mpz_get_ui(index);
size_t size = l1->size();
size_t size2 = l2->size();
if (idx != 0 && size2 != 0) {
if (idx + size2 - 1 >= size) {
throw std::invalid_argument("Index out of range for updateAll");
KLLVM_HOOK_INVALID_ARGUMENT("Index out of range for updateAll");
}
}

Expand All @@ -151,11 +151,11 @@ list hook_LIST_updateAll(SortList l1, SortInt index, SortList l2) {

list hook_LIST_fill(SortList l, SortInt index, SortInt len, SortKItem val) {
if (!mpz_fits_ulong_p(index)) {
throw std::invalid_argument("Index is too large for fill");
KLLVM_HOOK_INVALID_ARGUMENT("Index is too large for fill");
}

if (!mpz_fits_ulong_p(len)) {
throw std::invalid_argument("Length is too large for fill");
KLLVM_HOOK_INVALID_ARGUMENT("Length is too large for fill");
}

size_t idx = mpz_get_ui(index);
Expand Down
6 changes: 3 additions & 3 deletions runtime/collections/maps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ map hook_MAP_concat(SortMap m1, SortMap m2) {
for (auto iter = from->begin(); iter != from->end(); ++iter) {
auto entry = *iter;
if (to.find(entry.first)) {
throw std::invalid_argument("Duplicate keys in map concatenation");
KLLVM_HOOK_INVALID_ARGUMENT("Duplicate keys in map concatenation");
}
to = to.insert(*iter);
}
Expand All @@ -45,7 +45,7 @@ SortKItem hook_MAP_lookup_null(SortMap m, SortKItem key) {
SortKItem hook_MAP_lookup(SortMap m, SortKItem key) {
auto res = hook_MAP_lookup_null(m, key);
if (!res) {
throw std::invalid_argument("Key not found for map lookup");
KLLVM_HOOK_INVALID_ARGUMENT("Key not found for map lookup");
}
return res;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ list hook_MAP_values(SortMap m) {

SortKItem hook_MAP_choice(SortMap m) {
if (m->empty()) {
throw std::invalid_argument("Cannot choose from an empty map");
KLLVM_HOOK_INVALID_ARGUMENT("Cannot choose from an empty map");
}
return m->begin()->first;
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/collections/sets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ set hook_SET_intersection(SortSet s1, SortSet s2) {

SortKItem hook_SET_choice(SortSet s) {
if (s->empty()) {
throw std::invalid_argument("Set is empty");
KLLVM_HOOK_INVALID_ARGUMENT("Set is empty");
}
return *s->begin();
}
Expand Down
Loading