Skip to content

Commit 84d21f3

Browse files
authored
[CN-Exec/CN-Test-Gen] Generalize failure handling (#699)
1 parent f82b33c commit 84d21f3

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

runtime/libcn/include/cn-executable/utils.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,18 @@ void cn_free_sized(void*, size_t len);
138138
void cn_print_nr_u64(int i, unsigned long u) ;
139139
void cn_print_u64(const char *str, unsigned long u) ;
140140

141-
/* cn_exit callbacks */
142-
void set_cn_exit_cb(void (*callback)(void));
143-
void reset_cn_exit_cb(void);
141+
/* cn_failure callbacks */
142+
enum cn_failure_mode {
143+
CN_FAILURE_ASSERT = 1,
144+
CN_FAILURE_CHECK_OWNERSHIP,
145+
CN_FAILURE_OWNERSHIP_LEAK,
146+
CN_FAILURE_ALLOC
147+
};
148+
149+
typedef void (*cn_failure_callback)(enum cn_failure_mode);
150+
void set_cn_failure_cb(cn_failure_callback callback);
151+
void reset_cn_failure_cb(void);
152+
void cn_failure(enum cn_failure_mode mode);
144153

145154
/* Conversion functions */
146155

runtime/libcn/include/cn-testing/test.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void print_test_info(char* suite, char* name, int tests, int discards);
2222
if (setjmp(buf_##Name)) { \
2323
return CN_TEST_FAIL; \
2424
} \
25-
set_cn_exit_cb(&cn_test_##Name##_fail); \
25+
set_cn_failure_cb(&cn_test_##Name##_fail); \
2626
\
2727
CN_TEST_INIT(); \
2828
Name(); \
@@ -33,39 +33,42 @@ void print_test_info(char* suite, char* name, int tests, int discards);
3333
#define CN_RANDOM_TEST_CASE_WITH_CUSTOM_INIT(Suite, Name, Samples, Init, ...) \
3434
static jmp_buf buf_##Name; \
3535
\
36-
void cn_test_##Name##_fail () { \
37-
longjmp(buf_##Name, 1); \
36+
void cn_test_##Name##_fail (enum cn_failure_mode mode) { \
37+
longjmp(buf_##Name, mode); \
3838
} \
3939
\
4040
enum cn_test_result cn_test_##Name (int printing) { \
41-
if (setjmp(buf_##Name)) { \
42-
return CN_TEST_FAIL; \
43-
} \
44-
set_cn_exit_cb(&cn_test_##Name##_fail); \
45-
\
4641
cn_gen_rand_checkpoint checkpoint = cn_gen_rand_save(); \
4742
int i = 0, d = 0; \
43+
switch (setjmp(buf_##Name)) { \
44+
case CN_FAILURE_ASSERT: \
45+
case CN_FAILURE_CHECK_OWNERSHIP: \
46+
case CN_FAILURE_OWNERSHIP_LEAK: \
47+
return CN_TEST_FAIL; \
48+
case CN_FAILURE_ALLOC: \
49+
d++; \
50+
break; \
51+
} \
52+
set_cn_failure_cb(&cn_test_##Name##_fail); \
4853
for (; i < Samples; i++) { \
4954
if (printing) { \
5055
printf("\r"); \
5156
print_test_info(#Suite, #Name, i, d); \
5257
} \
58+
if (d == 10 * Samples) { \
59+
return CN_TEST_GEN_FAIL; \
60+
} \
61+
cn_gen_rand_replace(checkpoint); \
5362
CN_TEST_INIT(); \
5463
struct cn_gen_##Name##_record *res = cn_gen_##Name(); \
5564
if (cn_gen_backtrack_type() != CN_GEN_BACKTRACK_NONE) { \
5665
i--; \
5766
d++; \
58-
if (d == 10 * Samples) { \
59-
printf("\r"); \
60-
print_test_info(#Suite, #Name, i + 1, d); \
61-
return CN_TEST_GEN_FAIL; \
62-
} \
6367
continue; \
6468
} \
6569
assume_##Name(__VA_ARGS__); \
6670
Init(res); \
6771
Name(__VA_ARGS__); \
68-
cn_gen_rand_replace(checkpoint); \
6972
} \
7073
\
7174
if (printing) { \

runtime/libcn/src/cn-executable/alloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44

55
#include <cn-executable/alloc.h>
6+
#include <cn-executable/utils.h>
67

78
// #define foo(x)\
89
// [ x ] = #x
@@ -44,8 +45,8 @@ void *alloc_(long nbytes, const char *str, int line) {
4445
// printf("Alloc called: %s:%d\n", str, line);
4546
void *res = curr;
4647
if ((char *) curr + nbytes - buf > MEM_SIZE) {
47-
printf("Out of memory! %lu\n", count);
48-
exit(1);
48+
cn_failure(CN_FAILURE_ALLOC);
49+
return NULL;
4950
}
5051
count++;
5152
curr += nbytes;

runtime/libcn/src/cn-executable/utils.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,29 @@ enum cn_logging_level set_cn_logging_level(enum cn_logging_level new_level) {
2727
return old_level;
2828
}
2929

30-
void cn_exit_aux(void) {
31-
exit(SIGABRT);
30+
void cn_failure_default(enum cn_failure_mode mode) {
31+
switch (mode) {
32+
case CN_FAILURE_ALLOC:
33+
printf("Out of memory!");
34+
case CN_FAILURE_ASSERT:
35+
case CN_FAILURE_CHECK_OWNERSHIP:
36+
case CN_FAILURE_OWNERSHIP_LEAK:
37+
exit(SIGABRT);
38+
}
3239
}
3340

34-
void static (*cn_exit)(void) = &cn_exit_aux;
41+
static cn_failure_callback cn_failure_aux = &cn_failure_default;
3542

36-
void set_cn_exit_cb(void (*callback)(void)) {
37-
cn_exit = callback;
43+
void cn_failure(enum cn_failure_mode mode) {
44+
cn_failure_aux(mode);
3845
}
3946

40-
void reset_cn_exit_cb(void) {
41-
cn_exit = &cn_exit_aux;
47+
void set_cn_failure_cb(cn_failure_callback callback) {
48+
cn_failure_aux = callback;
49+
}
50+
51+
void reset_cn_failure_cb(void) {
52+
cn_failure_aux = &cn_failure_default;
4253
}
4354

4455
void print_error_msg_info(struct cn_error_message_info *info) {
@@ -54,7 +65,7 @@ void print_error_msg_info(struct cn_error_message_info *info) {
5465
}
5566
else {
5667
cn_printf(CN_LOGGING_ERROR, "Internal error: no error_msg_info available.");
57-
cn_exit_aux();
68+
exit(SIGABRT);
5869
}
5970
}
6071

@@ -74,7 +85,7 @@ void cn_assert(cn_bool *cn_b) {
7485
if (!(cn_b->val)) {
7586
print_error_msg_info(error_msg_info);
7687
cn_printf(CN_LOGGING_ERROR, "CN assertion failed.");
77-
cn_exit();
88+
cn_failure(CN_FAILURE_ASSERT);
7889
}
7990
}
8091

@@ -161,7 +172,7 @@ void ghost_stack_depth_decr(void) {
161172
if (*depth > cn_stack_depth) {
162173
print_error_msg_info(error_msg_info);
163174
cn_printf(CN_LOGGING_ERROR, "Leak check failed, ownership leaked for pointer "FMT_PTR"\n", *key);
164-
cn_exit();
175+
cn_failure(CN_FAILURE_OWNERSHIP_LEAK);
165176
// cn_printf(CN_LOGGING_INFO, FMT_PTR_2 " (%d),", *key, *depth);
166177
}
167178
}
@@ -277,7 +288,7 @@ void c_ownership_check(char *access_kind, uintptr_t generic_c_ptr, int offset, s
277288
cn_printf(CN_LOGGING_ERROR, " ==> "FMT_PTR"[%d] ("FMT_PTR") not owned at expected function call stack depth %ld\n", generic_c_ptr, i, (uintptr_t)((char*)generic_c_ptr + i), expected_stack_depth);
278289
cn_printf(CN_LOGGING_ERROR, " ==> (owned at stack depth: %d)\n", curr_depth);
279290
}
280-
cn_exit();
291+
cn_failure(CN_FAILURE_CHECK_OWNERSHIP);
281292
}
282293
}
283294
// cn_printf(CN_LOGGING_INFO, "\n");

runtime/libcn/src/cn-testing/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ int cn_test_main(int argc, char* argv[]) {
228228

229229
cn_gen_rand_restore(checkpoints[mapToCase[testcase - 1]]);
230230
set_cn_logging_level(CN_LOGGING_INFO);
231-
reset_cn_exit_cb();
231+
reset_cn_failure_cb();
232232
// raise(SIGTRAP); // Trigger breakpoint
233233
test_cases[mapToCase[testcase - 1]].func(0);
234234
}

0 commit comments

Comments
 (0)