added segfault testing
authorDavid Schleef <ds@schleef.org>
Tue, 23 Jan 2001 18:21:47 +0000 (18:21 +0000)
committerDavid Schleef <ds@schleef.org>
Tue, 23 Jan 2001 18:21:47 +0000 (18:21 +0000)
testing/mmap.c

index 0aaab152f0e22f1a0b02438c81e98f90ab6228f9..8a20e06691cb84117254a5e47d1cfeca5b53b90e 100644 (file)
 #include <sys/time.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <stdlib.h>
+
 #include "comedi_test.h"
 
+/* XXX this should come from elsewhere */
+#define PAGE_SIZE 4096
+
 static int comedi_get_cmd_fast_1chan(comedi_t *it,unsigned int s,comedi_cmd *cmd);
 
 #define N_SAMPLES 10000
@@ -21,17 +28,48 @@ static int comedi_get_cmd_fast_1chan(comedi_t *it,unsigned int s,comedi_cmd *cmd
 
 #define MAPLEN 20480
 
+jmp_buf jump_env;
+
+void segv_handler(int num,siginfo_t *si,void *x)
+{
+       longjmp(jump_env,1);
+}
+
+int test_segfault(void *memptr)
+{
+       volatile char tmp;
+       int ret;
+
+       ret=setjmp(jump_env);
+       if(!ret) tmp = *((char *)(memptr));
+       return ret;
+}
+
+void setup_segfaulter(void)
+{
+       struct sigaction act;
+
+       memset(&act,0,sizeof(act));
+       act.sa_sigaction=&segv_handler;
+       act.sa_flags = SA_SIGINFO;
+       sigaction(SIGSEGV,&act,NULL);
+}
+
 int test_mmap(void)
 {
        comedi_cmd cmd;
-       char buf[BUFSZ];
+       char *buf;
        unsigned int chanlist[1];
        int go;
        int total=0;
        int ret;
-       void *b;
+       void *b, *adr;
        sampl_t *map;
 
+       setup_segfaulter();
+
+       buf=malloc(BUFSZ);
+
        if(comedi_get_cmd_fast_1chan(device,subdevice,&cmd)<0){
                printf("  not supported\n");
                return 0;
@@ -43,6 +81,16 @@ int test_mmap(void)
                return 0;
        }
 
+       /* test readability */
+       for(adr=map;adr<(void *)map+MAPLEN;adr+=PAGE_SIZE){
+               ret=test_segfault(adr);
+               if(ret){
+                       printf("E: %p failed\n",adr);
+               }else{
+                       printf("%p ok\n",adr);
+               }
+       }
+
        cmd.chanlist = chanlist;
        cmd.scan_end_arg = 1;
        cmd.stop_arg = N_SAMPLES;
@@ -77,6 +125,8 @@ int test_mmap(void)
        }
        munmap(map,MAPLEN);
 
+       free(buf);
+
        return 0;
 }