changeset 592:f29db2bf3c0e

- added exception handling to test/call{,back}_suite_aggrs - test/callback_suite_aggrs: added missing memory cleanup
author Tassilo Philipp
date Mon, 19 Sep 2022 18:11:08 +0200
parents 1d6e51b1d4c7
children 806e415df417
files test/call_suite_aggrs/globals.c test/call_suite_aggrs/main.c test/callback_suite_aggrs/globals.c test/callback_suite_aggrs/main.c
diffstat 4 files changed, 53 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/test/call_suite_aggrs/globals.c	Mon Sep 19 16:59:52 2022 +0200
+++ b/test/call_suite_aggrs/globals.c	Mon Sep 19 18:11:08 2022 +0200
@@ -3,7 +3,7 @@
  Package: dyncall
  Library: test
  File: test/call_suite_aggrs/globals.c
- Description: 
+ Description:
  License:
 
    Copyright (c) 2022 Tassilo Philipp <tphilipp@potion-studios.com>
@@ -27,7 +27,7 @@
 #include <float.h>
 #include <string.h>
 
-#define X(CH,T) T *V_##CH; T *K_##CH; 
+#define X(CH,T) T *V_##CH; T *K_##CH;
 DEF_TYPES
 #undef X
 
@@ -117,8 +117,8 @@
 {
   int i;
   for(i=0;i<G_maxargs+1;++i) {
-    free((char*)V_a[i]-AGGR_MISALIGN);
-    free((char*)K_a[i]-AGGR_MISALIGN);
+    if(V_a[i]) free((char*)V_a[i]-AGGR_MISALIGN);
+    if(K_a[i]) free((char*)K_a[i]-AGGR_MISALIGN);
   }
 
 #define X(CH,T) free(V_##CH); free(K_##CH);
--- a/test/call_suite_aggrs/main.c	Mon Sep 19 16:59:52 2022 +0200
+++ b/test/call_suite_aggrs/main.c	Mon Sep 19 18:11:08 2022 +0200
@@ -25,6 +25,8 @@
 #include "dyncall.h"
 #include "globals.h"
 #include <string.h>
+#include <signal.h>
+#include <setjmp.h>
 #include "../common/platformInit.h"
 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */
 
@@ -225,9 +227,22 @@
   return !failure;
 }
 
+
+jmp_buf jbuf;
+void segv_handler(int sig)
+{
+  longjmp(jbuf, 1);
+}
+
+
 int main(int argc, char* argv[])
 {
-  int total, i;
+  int r = 0, i;
+
+  signal(SIGSEGV, segv_handler);
+#if !defined(DC_WINDOWS)
+  signal(SIGBUS,  segv_handler);
+#endif
 
   dcTest_initPlatform();
 
@@ -235,19 +250,20 @@
   G_callvm = (DCCallVM*) dcNewCallVM(32768);
 
   dcReset(G_callvm);
-  total = run_all();
+  if(setjmp(jbuf) == 0)
+    r = run_all();
 
-  /* free all DCaggrs created on the fly */
-  for(i=0; i<G_naggs; ++i)
+  /* free all DCaggrs created on the fly (backwards b/c they are interdependency-ordered */
+  for(i=G_naggs-1; i>=0; --i)
     dcFreeAggr(((DCaggr*(*)())G_agg_touchAfuncs[i])());
 
   dcFree(G_callvm);
   deinit_test_data(G_maxargs);
 
-  printf("result: call_suite_aggrs: %d\n", total);
+  printf("result: call_suite_aggrs: %d\n", r);
 
   dcTest_deInitPlatform();
 
-  return !total;
+  return !r;
 }
 
--- a/test/callback_suite_aggrs/globals.c	Mon Sep 19 16:59:52 2022 +0200
+++ b/test/callback_suite_aggrs/globals.c	Mon Sep 19 18:11:08 2022 +0200
@@ -3,7 +3,7 @@
  Package: dyncall
  Library: test
  File: test/callback_suite_aggrs/globals.c
- Description: 
+ Description:
  License:
 
    Copyright (c) 2022 Tassilo Philipp <tphilipp@potion-studios.com>
@@ -117,8 +117,8 @@
 {
   int i;
   for(i=0;i<G_maxargs+1;++i) {
-    free((char*)V_a[i]-AGGR_MISALIGN);
-    free((char*)K_a[i]-AGGR_MISALIGN);
+    if(V_a[i]) free((char*)V_a[i]-AGGR_MISALIGN);
+    if(K_a[i]) free((char*)K_a[i]-AGGR_MISALIGN);
   }
 
 #define X(CH,T) free(V_##CH); free(K_##CH);
--- a/test/callback_suite_aggrs/main.c	Mon Sep 19 16:59:52 2022 +0200
+++ b/test/callback_suite_aggrs/main.c	Mon Sep 19 18:11:08 2022 +0200
@@ -26,6 +26,8 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <string.h>
+#include <signal.h>
+#include <setjmp.h>
 #include "dyncall_callback.h"
 #include "globals.h"
 #include "../common/platformInit.h"
@@ -273,12 +275,24 @@
 }
 
 
+jmp_buf jbuf;
+void segv_handler(int sig)
+{
+  longjmp(jbuf, 1);
+}
+
+
 #define Error(X, Y, N) { fprintf(stderr, X, Y); print_usage(N); exit(1); }
 
 int main(int argc, char* argv[])
 {
   int from = 0, to = G_ncases-1;
-  int i, pos = 0, total;
+  int i, pos = 0, r = 0;
+
+  signal(SIGSEGV, segv_handler);
+#if !defined(DC_WINDOWS)
+  signal(SIGBUS,  segv_handler);
+#endif
 
   dcTest_initPlatform();
 
@@ -308,13 +322,19 @@
 
 
   init_test_data();
-  total = run_all(from, to);
+  if(setjmp(jbuf) == 0)
+    r = run_all(from, to);
+
+  /* free all DCaggrs created on the fly (backwards b/c they are interdependency-ordered */
+  for(i=G_naggs-1; i>=0; --i)
+    dcFreeAggr(((DCaggr*(*)())G_agg_touchAfuncs[i])());
+
   deinit_test_data();
 
-  printf("result: callback_suite_aggrs: %d\n", total);
+  printf("result: callback_suite_aggrs: %d\n", r);
 
   dcTest_deInitPlatform();
 
-  return !total;
+  return !r;
 }