GNU Linux-libre 4.9.337-gnu1
[releases.git] / tools / testing / selftests / x86 / mpx-mini-test.c
1 /*
2  * mpx-mini-test.c: routines to test Intel MPX (Memory Protection eXtentions)
3  *
4  * Written by:
5  * "Ren, Qiaowei" <qiaowei.ren@intel.com>
6  * "Wei, Gang" <gang.wei@intel.com>
7  * "Hansen, Dave" <dave.hansen@intel.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License,
11  * version 2.
12  */
13
14 /*
15  * 2014-12-05: Dave Hansen: fixed all of the compiler warnings, and made sure
16  *             it works on 32-bit.
17  */
18
19 int inspect_every_this_many_mallocs = 100;
20 int zap_all_every_this_many_mallocs = 1000;
21
22 #define _GNU_SOURCE
23 #define _LARGEFILE64_SOURCE
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <signal.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <ucontext.h>
33 #include <sys/mman.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38
39 #include "mpx-hw.h"
40 #include "mpx-debug.h"
41 #include "mpx-mm.h"
42
43 #ifndef __always_inline
44 #define __always_inline inline __attribute__((always_inline)
45 #endif
46
47 #ifndef TEST_DURATION_SECS
48 #define TEST_DURATION_SECS 3
49 #endif
50
51 void write_int_to(char *prefix, char *file, int int_to_write)
52 {
53         char buf[100];
54         int fd = open(file, O_RDWR);
55         int len;
56         int ret;
57
58         assert(fd >= 0);
59         len = snprintf(buf, sizeof(buf), "%s%d", prefix, int_to_write);
60         assert(len >= 0);
61         assert(len < sizeof(buf));
62         ret = write(fd, buf, len);
63         assert(ret == len);
64         ret = close(fd);
65         assert(!ret);
66 }
67
68 void write_pid_to(char *prefix, char *file)
69 {
70         write_int_to(prefix, file, getpid());
71 }
72
73 void trace_me(void)
74 {
75 /* tracing events dir */
76 #define TED "/sys/kernel/debug/tracing/events/"
77 /*
78         write_pid_to("common_pid=", TED "signal/filter");
79         write_pid_to("common_pid=", TED "exceptions/filter");
80         write_int_to("", TED "signal/enable", 1);
81         write_int_to("", TED "exceptions/enable", 1);
82 */
83         write_pid_to("", "/sys/kernel/debug/tracing/set_ftrace_pid");
84         write_int_to("", "/sys/kernel/debug/tracing/trace", 0);
85 }
86
87 #define test_failed() __test_failed(__FILE__, __LINE__)
88 static void __test_failed(char *f, int l)
89 {
90         fprintf(stderr, "abort @ %s::%d\n", f, l);
91         abort();
92 }
93
94 /* Error Printf */
95 #define eprintf(args...)        fprintf(stderr, args)
96
97 #ifdef __i386__
98
99 /* i386 directory size is 4MB */
100 #define REG_IP_IDX      REG_EIP
101 #define REX_PREFIX
102
103 #define XSAVE_OFFSET_IN_FPMEM   sizeof(struct _libc_fpstate)
104
105 /*
106  * __cpuid() is from the Linux Kernel:
107  */
108 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
109                 unsigned int *ecx, unsigned int *edx)
110 {
111         /* ecx is often an input as well as an output. */
112         asm volatile(
113                 "push %%ebx;"
114                 "cpuid;"
115                 "mov %%ebx, %1;"
116                 "pop %%ebx"
117                 : "=a" (*eax),
118                   "=g" (*ebx),
119                   "=c" (*ecx),
120                   "=d" (*edx)
121                 : "0" (*eax), "2" (*ecx));
122 }
123
124 #else /* __i386__ */
125
126 #define REG_IP_IDX      REG_RIP
127 #define REX_PREFIX "0x48, "
128
129 #define XSAVE_OFFSET_IN_FPMEM   0
130
131 /*
132  * __cpuid() is from the Linux Kernel:
133  */
134 static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
135                 unsigned int *ecx, unsigned int *edx)
136 {
137         /* ecx is often an input as well as an output. */
138         asm volatile(
139                 "cpuid;"
140                 : "=a" (*eax),
141                   "=b" (*ebx),
142                   "=c" (*ecx),
143                   "=d" (*edx)
144                 : "0" (*eax), "2" (*ecx));
145 }
146
147 #endif /* !__i386__ */
148
149 struct xsave_hdr_struct {
150         uint64_t xstate_bv;
151         uint64_t reserved1[2];
152         uint64_t reserved2[5];
153 } __attribute__((packed));
154
155 struct bndregs_struct {
156         uint64_t bndregs[8];
157 } __attribute__((packed));
158
159 struct bndcsr_struct {
160         uint64_t cfg_reg_u;
161         uint64_t status_reg;
162 } __attribute__((packed));
163
164 struct xsave_struct {
165         uint8_t fpu_sse[512];
166         struct xsave_hdr_struct xsave_hdr;
167         uint8_t ymm[256];
168         uint8_t lwp[128];
169         struct bndregs_struct bndregs;
170         struct bndcsr_struct bndcsr;
171 } __attribute__((packed));
172
173 uint8_t __attribute__((__aligned__(64))) buffer[4096];
174 struct xsave_struct *xsave_buf = (struct xsave_struct *)buffer;
175
176 uint8_t __attribute__((__aligned__(64))) test_buffer[4096];
177 struct xsave_struct *xsave_test_buf = (struct xsave_struct *)test_buffer;
178
179 uint64_t num_bnd_chk;
180
181 static __always_inline void xrstor_state(struct xsave_struct *fx, uint64_t mask)
182 {
183         uint32_t lmask = mask;
184         uint32_t hmask = mask >> 32;
185
186         asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x2f\n\t"
187                      : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
188                      :   "memory");
189 }
190
191 static __always_inline void xsave_state_1(void *_fx, uint64_t mask)
192 {
193         uint32_t lmask = mask;
194         uint32_t hmask = mask >> 32;
195         unsigned char *fx = _fx;
196
197         asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t"
198                      : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
199                      :   "memory");
200 }
201
202 static inline uint64_t xgetbv(uint32_t index)
203 {
204         uint32_t eax, edx;
205
206         asm volatile(".byte 0x0f,0x01,0xd0" /* xgetbv */
207                      : "=a" (eax), "=d" (edx)
208                      : "c" (index));
209         return eax + ((uint64_t)edx << 32);
210 }
211
212 static uint64_t read_mpx_status_sig(ucontext_t *uctxt)
213 {
214         memset(buffer, 0, sizeof(buffer));
215         memcpy(buffer,
216                 (uint8_t *)uctxt->uc_mcontext.fpregs + XSAVE_OFFSET_IN_FPMEM,
217                 sizeof(struct xsave_struct));
218
219         return xsave_buf->bndcsr.status_reg;
220 }
221
222 #include <pthread.h>
223
224 static uint8_t *get_next_inst_ip(uint8_t *addr)
225 {
226         uint8_t *ip = addr;
227         uint8_t sib;
228         uint8_t rm;
229         uint8_t mod;
230         uint8_t base;
231         uint8_t modrm;
232
233         /* determine the prefix. */
234         switch(*ip) {
235         case 0xf2:
236         case 0xf3:
237         case 0x66:
238                 ip++;
239                 break;
240         }
241
242         /* look for rex prefix */
243         if ((*ip & 0x40) == 0x40)
244                 ip++;
245
246         /* Make sure we have a MPX instruction. */
247         if (*ip++ != 0x0f)
248                 return addr;
249
250         /* Skip the op code byte. */
251         ip++;
252
253         /* Get the modrm byte. */
254         modrm = *ip++;
255
256         /* Break it down into parts. */
257         rm = modrm & 7;
258         mod = (modrm >> 6);
259
260         /* Init the parts of the address mode. */
261         base = 8;
262
263         /* Is it a mem mode? */
264         if (mod != 3) {
265                 /* look for scaled indexed addressing */
266                 if (rm == 4) {
267                         /* SIB addressing */
268                         sib = *ip++;
269                         base = sib & 7;
270                         switch (mod) {
271                         case 0:
272                                 if (base == 5)
273                                         ip += 4;
274                                 break;
275
276                         case 1:
277                                 ip++;
278                                 break;
279
280                         case 2:
281                                 ip += 4;
282                                 break;
283                         }
284
285                 } else {
286                         /* MODRM addressing */
287                         switch (mod) {
288                         case 0:
289                                 /* DISP32 addressing, no base */
290                                 if (rm == 5)
291                                         ip += 4;
292                                 break;
293
294                         case 1:
295                                 ip++;
296                                 break;
297
298                         case 2:
299                                 ip += 4;
300                                 break;
301                         }
302                 }
303         }
304         return ip;
305 }
306
307 #ifdef si_lower
308 static inline void *__si_bounds_lower(siginfo_t *si)
309 {
310         return si->si_lower;
311 }
312
313 static inline void *__si_bounds_upper(siginfo_t *si)
314 {
315         return si->si_upper;
316 }
317 #else
318
319 /*
320  * This deals with old version of _sigfault in some distros:
321  *
322
323 old _sigfault:
324         struct {
325             void *si_addr;
326         } _sigfault;
327
328 new _sigfault:
329         struct {
330                 void __user *_addr;
331                 int _trapno;
332                 short _addr_lsb;
333                 union {
334                         struct {
335                                 void __user *_lower;
336                                 void __user *_upper;
337                         } _addr_bnd;
338                         __u32 _pkey;
339                 };
340         } _sigfault;
341  *
342  */
343
344 static inline void **__si_bounds_hack(siginfo_t *si)
345 {
346         void *sigfault = &si->_sifields._sigfault;
347         void *end_sigfault = sigfault + sizeof(si->_sifields._sigfault);
348         int *trapno = (int*)end_sigfault;
349         /* skip _trapno and _addr_lsb */
350         void **__si_lower = (void**)(trapno + 2);
351
352         return __si_lower;
353 }
354
355 static inline void *__si_bounds_lower(siginfo_t *si)
356 {
357         return *__si_bounds_hack(si);
358 }
359
360 static inline void *__si_bounds_upper(siginfo_t *si)
361 {
362         return *(__si_bounds_hack(si) + 1);
363 }
364 #endif
365
366 static int br_count;
367 static int expected_bnd_index = -1;
368 uint64_t shadow_plb[NR_MPX_BOUNDS_REGISTERS][2]; /* shadow MPX bound registers */
369 unsigned long shadow_map[NR_MPX_BOUNDS_REGISTERS];
370
371 /*
372  * The kernel is supposed to provide some information about the bounds
373  * exception in the siginfo.  It should match what we have in the bounds
374  * registers that we are checking against.  Just check against the shadow copy
375  * since it is easily available, and we also check that *it* matches the real
376  * registers.
377  */
378 void check_siginfo_vs_shadow(siginfo_t* si)
379 {
380         int siginfo_ok = 1;
381         void *shadow_lower = (void *)(unsigned long)shadow_plb[expected_bnd_index][0];
382         void *shadow_upper = (void *)(unsigned long)shadow_plb[expected_bnd_index][1];
383
384         if ((expected_bnd_index < 0) ||
385             (expected_bnd_index >= NR_MPX_BOUNDS_REGISTERS)) {
386                 fprintf(stderr, "ERROR: invalid expected_bnd_index: %d\n",
387                         expected_bnd_index);
388                 exit(6);
389         }
390         if (__si_bounds_lower(si) != shadow_lower)
391                 siginfo_ok = 0;
392         if (__si_bounds_upper(si) != shadow_upper)
393                 siginfo_ok = 0;
394
395         if (!siginfo_ok) {
396                 fprintf(stderr, "ERROR: siginfo bounds do not match "
397                         "shadow bounds for register %d\n", expected_bnd_index);
398                 exit(7);
399         }
400 }
401
402 void handler(int signum, siginfo_t *si, void *vucontext)
403 {
404         int i;
405         ucontext_t *uctxt = vucontext;
406         int trapno;
407         unsigned long ip;
408
409         dprintf1("entered signal handler\n");
410
411         trapno = uctxt->uc_mcontext.gregs[REG_TRAPNO];
412         ip = uctxt->uc_mcontext.gregs[REG_IP_IDX];
413
414         if (trapno == 5) {
415                 typeof(si->si_addr) *si_addr_ptr = &si->si_addr;
416                 uint64_t status = read_mpx_status_sig(uctxt);
417                 uint64_t br_reason =  status & 0x3;
418
419                 br_count++;
420                 dprintf1("#BR 0x%jx (total seen: %d)\n", status, br_count);
421
422 #define SEGV_BNDERR     3  /* failed address bound checks */
423
424                 dprintf2("Saw a #BR! status 0x%jx at %016lx br_reason: %jx\n",
425                                 status, ip, br_reason);
426                 dprintf2("si_signo: %d\n", si->si_signo);
427                 dprintf2("  signum: %d\n", signum);
428                 dprintf2("info->si_code == SEGV_BNDERR: %d\n",
429                                 (si->si_code == SEGV_BNDERR));
430                 dprintf2("info->si_code: %d\n", si->si_code);
431                 dprintf2("info->si_lower: %p\n", __si_bounds_lower(si));
432                 dprintf2("info->si_upper: %p\n", __si_bounds_upper(si));
433
434                 check_siginfo_vs_shadow(si);
435
436                 for (i = 0; i < 8; i++)
437                         dprintf3("[%d]: %p\n", i, si_addr_ptr[i]);
438                 switch (br_reason) {
439                 case 0: /* traditional BR */
440                         fprintf(stderr,
441                                 "Undefined status with bound exception:%jx\n",
442                                  status);
443                         exit(5);
444                 case 1: /* #BR MPX bounds exception */
445                         /* these are normal and we expect to see them */
446                         dprintf1("bounds exception (normal): status 0x%jx at %p si_addr: %p\n",
447                                 status, (void *)ip, si->si_addr);
448                         num_bnd_chk++;
449                         uctxt->uc_mcontext.gregs[REG_IP_IDX] =
450                                 (greg_t)get_next_inst_ip((uint8_t *)ip);
451                         break;
452                 case 2:
453                         fprintf(stderr, "#BR status == 2, missing bounds table,"
454                                         "kernel should have handled!!\n");
455                         exit(4);
456                         break;
457                 default:
458                         fprintf(stderr, "bound check error: status 0x%jx at %p\n",
459                                 status, (void *)ip);
460                         num_bnd_chk++;
461                         uctxt->uc_mcontext.gregs[REG_IP_IDX] =
462                                 (greg_t)get_next_inst_ip((uint8_t *)ip);
463                         fprintf(stderr, "bound check error: si_addr %p\n", si->si_addr);
464                         exit(3);
465                 }
466         } else if (trapno == 14) {
467                 eprintf("ERROR: In signal handler, page fault, trapno = %d, ip = %016lx\n",
468                         trapno, ip);
469                 eprintf("si_addr %p\n", si->si_addr);
470                 eprintf("REG_ERR: %lx\n", (unsigned long)uctxt->uc_mcontext.gregs[REG_ERR]);
471                 test_failed();
472         } else {
473                 eprintf("unexpected trap %d! at 0x%lx\n", trapno, ip);
474                 eprintf("si_addr %p\n", si->si_addr);
475                 eprintf("REG_ERR: %lx\n", (unsigned long)uctxt->uc_mcontext.gregs[REG_ERR]);
476                 test_failed();
477         }
478 }
479
480 static inline void cpuid_count(unsigned int op, int count,
481                                unsigned int *eax, unsigned int *ebx,
482                                unsigned int *ecx, unsigned int *edx)
483 {
484         *eax = op;
485         *ecx = count;
486         __cpuid(eax, ebx, ecx, edx);
487 }
488
489 #define XSTATE_CPUID        0x0000000d
490
491 /*
492  * List of XSAVE features Linux knows about:
493  */
494 enum xfeature_bit {
495         XSTATE_BIT_FP,
496         XSTATE_BIT_SSE,
497         XSTATE_BIT_YMM,
498         XSTATE_BIT_BNDREGS,
499         XSTATE_BIT_BNDCSR,
500         XSTATE_BIT_OPMASK,
501         XSTATE_BIT_ZMM_Hi256,
502         XSTATE_BIT_Hi16_ZMM,
503
504         XFEATURES_NR_MAX,
505 };
506
507 #define XSTATE_FP              (1 << XSTATE_BIT_FP)
508 #define XSTATE_SSE            (1 << XSTATE_BIT_SSE)
509 #define XSTATE_YMM            (1 << XSTATE_BIT_YMM)
510 #define XSTATE_BNDREGS    (1 << XSTATE_BIT_BNDREGS)
511 #define XSTATE_BNDCSR      (1 << XSTATE_BIT_BNDCSR)
512 #define XSTATE_OPMASK      (1 << XSTATE_BIT_OPMASK)
513 #define XSTATE_ZMM_Hi256        (1 << XSTATE_BIT_ZMM_Hi256)
514 #define XSTATE_Hi16_ZMM  (1 << XSTATE_BIT_Hi16_ZMM)
515
516 #define MPX_XSTATES             (XSTATE_BNDREGS | XSTATE_BNDCSR) /* 0x18 */
517
518 bool one_bit(unsigned int x, int bit)
519 {
520         return !!(x & (1<<bit));
521 }
522
523 void print_state_component(int state_bit_nr, char *name)
524 {
525         unsigned int eax, ebx, ecx, edx;
526         unsigned int state_component_size;
527         unsigned int state_component_supervisor;
528         unsigned int state_component_user;
529         unsigned int state_component_aligned;
530
531         /* See SDM Section 13.2 */
532         cpuid_count(XSTATE_CPUID, state_bit_nr, &eax, &ebx, &ecx, &edx);
533         assert(eax || ebx || ecx);
534         state_component_size = eax;
535         state_component_supervisor = ((!ebx) && one_bit(ecx, 0));
536         state_component_user = !one_bit(ecx, 0);
537         state_component_aligned = one_bit(ecx, 1);
538         printf("%8s: size: %d user: %d supervisor: %d aligned: %d\n",
539                 name,
540                 state_component_size,       state_component_user,
541                 state_component_supervisor, state_component_aligned);
542
543 }
544
545 /* Intel-defined CPU features, CPUID level 0x00000001 (ecx) */
546 #define XSAVE_FEATURE_BIT       (26)  /* XSAVE/XRSTOR/XSETBV/XGETBV */
547 #define OSXSAVE_FEATURE_BIT     (27) /* XSAVE enabled in the OS */
548
549 bool check_mpx_support(void)
550 {
551         unsigned int eax, ebx, ecx, edx;
552
553         cpuid_count(1, 0, &eax, &ebx, &ecx, &edx);
554
555         /* We can't do much without XSAVE, so just make these assert()'s */
556         if (!one_bit(ecx, XSAVE_FEATURE_BIT)) {
557                 fprintf(stderr, "processor lacks XSAVE, can not run MPX tests\n");
558                 exit(0);
559         }
560
561         if (!one_bit(ecx, OSXSAVE_FEATURE_BIT)) {
562                 fprintf(stderr, "processor lacks OSXSAVE, can not run MPX tests\n");
563                 exit(0);
564         }
565
566         /* CPUs not supporting the XSTATE CPUID leaf do not support MPX */
567         /* Is this redundant with the feature bit checks? */
568         cpuid_count(0, 0, &eax, &ebx, &ecx, &edx);
569         if (eax < XSTATE_CPUID) {
570                 fprintf(stderr, "processor lacks XSTATE CPUID leaf,"
571                                 " can not run MPX tests\n");
572                 exit(0);
573         }
574
575         printf("XSAVE is supported by HW & OS\n");
576
577         cpuid_count(XSTATE_CPUID, 0, &eax, &ebx, &ecx, &edx);
578
579         printf("XSAVE processor supported state mask: 0x%x\n", eax);
580         printf("XSAVE OS supported state mask: 0x%jx\n", xgetbv(0));
581
582         /* Make sure that the MPX states are enabled in in XCR0 */
583         if ((eax & MPX_XSTATES) != MPX_XSTATES) {
584                 fprintf(stderr, "processor lacks MPX XSTATE(s), can not run MPX tests\n");
585                 exit(0);
586         }
587
588         /* Make sure the MPX states are supported by XSAVE* */
589         if ((xgetbv(0) & MPX_XSTATES) != MPX_XSTATES) {
590                 fprintf(stderr, "MPX XSTATE(s) no enabled in XCR0, "
591                                 "can not run MPX tests\n");
592                 exit(0);
593         }
594
595         print_state_component(XSTATE_BIT_BNDREGS, "BNDREGS");
596         print_state_component(XSTATE_BIT_BNDCSR,  "BNDCSR");
597
598         return true;
599 }
600
601 void enable_mpx(void *l1base)
602 {
603         /* enable point lookup */
604         memset(buffer, 0, sizeof(buffer));
605         xrstor_state(xsave_buf, 0x18);
606
607         xsave_buf->xsave_hdr.xstate_bv = 0x10;
608         xsave_buf->bndcsr.cfg_reg_u = (unsigned long)l1base | 1;
609         xsave_buf->bndcsr.status_reg = 0;
610
611         dprintf2("bf xrstor\n");
612         dprintf2("xsave cndcsr: status %jx, configu %jx\n",
613                xsave_buf->bndcsr.status_reg, xsave_buf->bndcsr.cfg_reg_u);
614         xrstor_state(xsave_buf, 0x18);
615         dprintf2("after xrstor\n");
616
617         xsave_state_1(xsave_buf, 0x18);
618
619         dprintf1("xsave bndcsr: status %jx, configu %jx\n",
620                xsave_buf->bndcsr.status_reg, xsave_buf->bndcsr.cfg_reg_u);
621 }
622
623 #include <sys/prctl.h>
624
625 struct mpx_bounds_dir *bounds_dir_ptr;
626
627 unsigned long __bd_incore(const char *func, int line)
628 {
629         unsigned long ret = nr_incore(bounds_dir_ptr, MPX_BOUNDS_DIR_SIZE_BYTES);
630         return ret;
631 }
632 #define bd_incore() __bd_incore(__func__, __LINE__)
633
634 void check_clear(void *ptr, unsigned long sz)
635 {
636         unsigned long *i;
637
638         for (i = ptr; (void *)i < ptr + sz; i++) {
639                 if (*i) {
640                         dprintf1("%p is NOT clear at %p\n", ptr, i);
641                         assert(0);
642                 }
643         }
644         dprintf1("%p is clear for %lx\n", ptr, sz);
645 }
646
647 void check_clear_bd(void)
648 {
649         check_clear(bounds_dir_ptr, 2UL << 30);
650 }
651
652 #define USE_MALLOC_FOR_BOUNDS_DIR 1
653 bool process_specific_init(void)
654 {
655         unsigned long size;
656         unsigned long *dir;
657         /* Guarantee we have the space to align it, add padding: */
658         unsigned long pad = getpagesize();
659
660         size = 2UL << 30; /* 2GB */
661         if (sizeof(unsigned long) == 4)
662                 size = 4UL << 20; /* 4MB */
663         dprintf1("trying to allocate %ld MB bounds directory\n", (size >> 20));
664
665         if (USE_MALLOC_FOR_BOUNDS_DIR) {
666                 unsigned long _dir;
667
668                 dir = malloc(size + pad);
669                 assert(dir);
670                 _dir = (unsigned long)dir;
671                 _dir += 0xfffUL;
672                 _dir &= ~0xfffUL;
673                 dir = (void *)_dir;
674         } else {
675                 /*
676                  * This makes debugging easier because the address
677                  * calculations are simpler:
678                  */
679                 dir = mmap((void *)0x200000000000, size + pad,
680                                 PROT_READ|PROT_WRITE,
681                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
682                 if (dir == (void *)-1) {
683                         perror("unable to allocate bounds directory");
684                         abort();
685                 }
686                 check_clear(dir, size);
687         }
688         bounds_dir_ptr = (void *)dir;
689         madvise(bounds_dir_ptr, size, MADV_NOHUGEPAGE);
690         bd_incore();
691         dprintf1("bounds directory: 0x%p -> 0x%p\n", bounds_dir_ptr,
692                         (char *)bounds_dir_ptr + size);
693         check_clear(dir, size);
694         enable_mpx(dir);
695         check_clear(dir, size);
696         if (prctl(43, 0, 0, 0, 0)) {
697                 printf("no MPX support\n");
698                 abort();
699                 return false;
700         }
701         return true;
702 }
703
704 bool process_specific_finish(void)
705 {
706         if (prctl(44)) {
707                 printf("no MPX support\n");
708                 return false;
709         }
710         return true;
711 }
712
713 void setup_handler()
714 {
715         int r, rs;
716         struct sigaction newact;
717         struct sigaction oldact;
718
719         /* #BR is mapped to sigsegv */
720         int signum  = SIGSEGV;
721
722         newact.sa_handler = 0;   /* void(*)(int)*/
723         newact.sa_sigaction = handler; /* void (*)(int, siginfo_t*, void *) */
724
725         /*sigset_t - signals to block while in the handler */
726         /* get the old signal mask. */
727         rs = sigprocmask(SIG_SETMASK, 0, &newact.sa_mask);
728         assert(rs == 0);
729
730         /* call sa_sigaction, not sa_handler*/
731         newact.sa_flags = SA_SIGINFO;
732
733         newact.sa_restorer = 0;  /* void(*)(), obsolete */
734         r = sigaction(signum, &newact, &oldact);
735         assert(r == 0);
736 }
737
738 void mpx_prepare(void)
739 {
740         dprintf2("%s()\n", __func__);
741         setup_handler();
742         process_specific_init();
743 }
744
745 void mpx_cleanup(void)
746 {
747         printf("%s(): %jd BRs. bye...\n", __func__, num_bnd_chk);
748         process_specific_finish();
749 }
750
751 /*-------------- the following is test case ---------------*/
752 #include <stdint.h>
753 #include <stdbool.h>
754 #include <stdlib.h>
755 #include <stdio.h>
756 #include <time.h>
757
758 uint64_t num_lower_brs;
759 uint64_t num_upper_brs;
760
761 #define MPX_CONFIG_OFFSET 1024
762 #define MPX_BOUNDS_OFFSET 960
763 #define MPX_HEADER_OFFSET 512
764 #define MAX_ADDR_TESTED (1<<28)
765 #define TEST_ROUNDS 100
766
767 /*
768       0F 1A /r BNDLDX-Load
769       0F 1B /r BNDSTX-Store Extended Bounds Using Address Translation
770    66 0F 1A /r BNDMOV bnd1, bnd2/m128
771    66 0F 1B /r BNDMOV bnd1/m128, bnd2
772    F2 0F 1A /r BNDCU bnd, r/m64
773    F2 0F 1B /r BNDCN bnd, r/m64
774    F3 0F 1A /r BNDCL bnd, r/m64
775    F3 0F 1B /r BNDMK bnd, m64
776 */
777
778 static __always_inline void xsave_state(void *_fx, uint64_t mask)
779 {
780         uint32_t lmask = mask;
781         uint32_t hmask = mask >> 32;
782         unsigned char *fx = _fx;
783
784         asm volatile(".byte " REX_PREFIX "0x0f,0xae,0x27\n\t"
785                      : : "D" (fx), "m" (*fx), "a" (lmask), "d" (hmask)
786                      :   "memory");
787 }
788
789 static __always_inline void mpx_clear_bnd0(void)
790 {
791         long size = 0;
792         void *ptr = NULL;
793         /* F3 0F 1B /r BNDMK bnd, m64                   */
794         /* f3 0f 1b 04 11    bndmk  (%rcx,%rdx,1),%bnd0 */
795         asm volatile(".byte 0xf3,0x0f,0x1b,0x04,0x11\n\t"
796                      : : "c" (ptr), "d" (size-1)
797                      :   "memory");
798 }
799
800 static __always_inline void mpx_make_bound_helper(unsigned long ptr,
801                 unsigned long size)
802 {
803         /* F3 0F 1B /r          BNDMK bnd, m64                  */
804         /* f3 0f 1b 04 11       bndmk  (%rcx,%rdx,1),%bnd0      */
805         asm volatile(".byte 0xf3,0x0f,0x1b,0x04,0x11\n\t"
806                      : : "c" (ptr), "d" (size-1)
807                      :   "memory");
808 }
809
810 static __always_inline void mpx_check_lowerbound_helper(unsigned long ptr)
811 {
812         /* F3 0F 1A /r  NDCL bnd, r/m64                 */
813         /* f3 0f 1a 01  bndcl  (%rcx),%bnd0             */
814         asm volatile(".byte 0xf3,0x0f,0x1a,0x01\n\t"
815                      : : "c" (ptr)
816                      :   "memory");
817 }
818
819 static __always_inline void mpx_check_upperbound_helper(unsigned long ptr)
820 {
821         /* F2 0F 1A /r  BNDCU bnd, r/m64        */
822         /* f2 0f 1a 01  bndcu  (%rcx),%bnd0     */
823         asm volatile(".byte 0xf2,0x0f,0x1a,0x01\n\t"
824                      : : "c" (ptr)
825                      :   "memory");
826 }
827
828 static __always_inline void mpx_movbndreg_helper()
829 {
830         /* 66 0F 1B /r  BNDMOV bnd1/m128, bnd2  */
831         /* 66 0f 1b c2  bndmov %bnd0,%bnd2      */
832
833         asm volatile(".byte 0x66,0x0f,0x1b,0xc2\n\t");
834 }
835
836 static __always_inline void mpx_movbnd2mem_helper(uint8_t *mem)
837 {
838         /* 66 0F 1B /r  BNDMOV bnd1/m128, bnd2  */
839         /* 66 0f 1b 01  bndmov %bnd0,(%rcx)     */
840         asm volatile(".byte 0x66,0x0f,0x1b,0x01\n\t"
841                      : : "c" (mem)
842                      :   "memory");
843 }
844
845 static __always_inline void mpx_movbnd_from_mem_helper(uint8_t *mem)
846 {
847         /* 66 0F 1A /r  BNDMOV bnd1, bnd2/m128  */
848         /* 66 0f 1a 01  bndmov (%rcx),%bnd0     */
849         asm volatile(".byte 0x66,0x0f,0x1a,0x01\n\t"
850                      : : "c" (mem)
851                      :   "memory");
852 }
853
854 static __always_inline void mpx_store_dsc_helper(unsigned long ptr_addr,
855                 unsigned long ptr_val)
856 {
857         /* 0F 1B /r     BNDSTX-Store Extended Bounds Using Address Translation  */
858         /* 0f 1b 04 11  bndstx %bnd0,(%rcx,%rdx,1)                              */
859         asm volatile(".byte 0x0f,0x1b,0x04,0x11\n\t"
860                      : : "c" (ptr_addr), "d" (ptr_val)
861                      :   "memory");
862 }
863
864 static __always_inline void mpx_load_dsc_helper(unsigned long ptr_addr,
865                 unsigned long ptr_val)
866 {
867         /* 0F 1A /r     BNDLDX-Load                     */
868         /*/ 0f 1a 04 11 bndldx (%rcx,%rdx,1),%bnd0      */
869         asm volatile(".byte 0x0f,0x1a,0x04,0x11\n\t"
870                      : : "c" (ptr_addr), "d" (ptr_val)
871                      :   "memory");
872 }
873
874 void __print_context(void *__print_xsave_buffer, int line)
875 {
876         uint64_t *bounds = (uint64_t *)(__print_xsave_buffer + MPX_BOUNDS_OFFSET);
877         uint64_t *cfg    = (uint64_t *)(__print_xsave_buffer + MPX_CONFIG_OFFSET);
878
879         int i;
880         eprintf("%s()::%d\n", "print_context", line);
881         for (i = 0; i < 4; i++) {
882                 eprintf("bound[%d]: 0x%016lx 0x%016lx(0x%016lx)\n", i,
883                        (unsigned long)bounds[i*2],
884                        ~(unsigned long)bounds[i*2+1],
885                         (unsigned long)bounds[i*2+1]);
886         }
887
888         eprintf("cpcfg: %jx  cpstatus: %jx\n", cfg[0], cfg[1]);
889 }
890 #define print_context(x) __print_context(x, __LINE__)
891 #ifdef DEBUG
892 #define dprint_context(x) print_context(x)
893 #else
894 #define dprint_context(x) do{}while(0)
895 #endif
896
897 void init()
898 {
899         int i;
900
901         srand((unsigned int)time(NULL));
902
903         for (i = 0; i < 4; i++) {
904                 shadow_plb[i][0] = 0;
905                 shadow_plb[i][1] = ~(unsigned long)0;
906         }
907 }
908
909 long int __mpx_random(int line)
910 {
911 #ifdef NOT_SO_RANDOM
912         static long fake = 722122311;
913         fake += 563792075;
914         return fakse;
915 #else
916         return random();
917 #endif
918 }
919 #define mpx_random() __mpx_random(__LINE__)
920
921 uint8_t *get_random_addr()
922 {
923         uint8_t*addr = (uint8_t *)(unsigned long)(rand() % MAX_ADDR_TESTED);
924         return (addr - (unsigned long)addr % sizeof(uint8_t *));
925 }
926
927 static inline bool compare_context(void *__xsave_buffer)
928 {
929         uint64_t *bounds = (uint64_t *)(__xsave_buffer + MPX_BOUNDS_OFFSET);
930
931         int i;
932         for (i = 0; i < 4; i++) {
933                 dprintf3("shadow[%d]{%016lx/%016lx}\nbounds[%d]{%016lx/%016lx}\n",
934                        i, (unsigned long)shadow_plb[i][0], (unsigned long)shadow_plb[i][1],
935                        i, (unsigned long)bounds[i*2],     ~(unsigned long)bounds[i*2+1]);
936                 if ((shadow_plb[i][0] != bounds[i*2]) ||
937                     (shadow_plb[i][1] != ~(unsigned long)bounds[i*2+1])) {
938                         eprintf("ERROR comparing shadow to real bound register %d\n", i);
939                         eprintf("shadow{0x%016lx/0x%016lx}\nbounds{0x%016lx/0x%016lx}\n",
940                                (unsigned long)shadow_plb[i][0], (unsigned long)shadow_plb[i][1],
941                                (unsigned long)bounds[i*2], (unsigned long)bounds[i*2+1]);
942                         return false;
943                 }
944         }
945
946         return true;
947 }
948
949 void mkbnd_shadow(uint8_t *ptr, int index, long offset)
950 {
951         uint64_t *lower = (uint64_t *)&(shadow_plb[index][0]);
952         uint64_t *upper = (uint64_t *)&(shadow_plb[index][1]);
953         *lower = (unsigned long)ptr;
954         *upper = (unsigned long)ptr + offset - 1;
955 }
956
957 void check_lowerbound_shadow(uint8_t *ptr, int index)
958 {
959         uint64_t *lower = (uint64_t *)&(shadow_plb[index][0]);
960         if (*lower > (uint64_t)(unsigned long)ptr)
961                 num_lower_brs++;
962         else
963                 dprintf1("LowerBoundChk passed:%p\n", ptr);
964 }
965
966 void check_upperbound_shadow(uint8_t *ptr, int index)
967 {
968         uint64_t upper = *(uint64_t *)&(shadow_plb[index][1]);
969         if (upper < (uint64_t)(unsigned long)ptr)
970                 num_upper_brs++;
971         else
972                 dprintf1("UpperBoundChk passed:%p\n", ptr);
973 }
974
975 __always_inline void movbndreg_shadow(int src, int dest)
976 {
977         shadow_plb[dest][0] = shadow_plb[src][0];
978         shadow_plb[dest][1] = shadow_plb[src][1];
979 }
980
981 __always_inline void movbnd2mem_shadow(int src, unsigned long *dest)
982 {
983         unsigned long *lower = (unsigned long *)&(shadow_plb[src][0]);
984         unsigned long *upper = (unsigned long *)&(shadow_plb[src][1]);
985         *dest = *lower;
986         *(dest+1) = *upper;
987 }
988
989 __always_inline void movbnd_from_mem_shadow(unsigned long *src, int dest)
990 {
991         unsigned long *lower = (unsigned long *)&(shadow_plb[dest][0]);
992         unsigned long *upper = (unsigned long *)&(shadow_plb[dest][1]);
993         *lower = *src;
994         *upper = *(src+1);
995 }
996
997 __always_inline void stdsc_shadow(int index, uint8_t *ptr, uint8_t *ptr_val)
998 {
999         shadow_map[0] = (unsigned long)shadow_plb[index][0];
1000         shadow_map[1] = (unsigned long)shadow_plb[index][1];
1001         shadow_map[2] = (unsigned long)ptr_val;
1002         dprintf3("%s(%d, %p, %p) set shadow map[2]: %p\n", __func__,
1003                         index, ptr, ptr_val, ptr_val);
1004         /*ptr ignored */
1005 }
1006
1007 void lddsc_shadow(int index, uint8_t *ptr, uint8_t *ptr_val)
1008 {
1009         uint64_t lower = shadow_map[0];
1010         uint64_t upper = shadow_map[1];
1011         uint8_t *value = (uint8_t *)shadow_map[2];
1012
1013         if (value != ptr_val) {
1014                 dprintf2("%s(%d, %p, %p) init shadow bounds[%d] "
1015                          "because %p != %p\n", __func__, index, ptr,
1016                          ptr_val, index, value, ptr_val);
1017                 shadow_plb[index][0] = 0;
1018                 shadow_plb[index][1] = ~(unsigned long)0;
1019         } else {
1020                 shadow_plb[index][0] = lower;
1021                 shadow_plb[index][1] = upper;
1022         }
1023         /* ptr ignored */
1024 }
1025
1026 static __always_inline void mpx_test_helper0(uint8_t *buf, uint8_t *ptr)
1027 {
1028         mpx_make_bound_helper((unsigned long)ptr, 0x1800);
1029 }
1030
1031 static __always_inline void mpx_test_helper0_shadow(uint8_t *buf, uint8_t *ptr)
1032 {
1033         mkbnd_shadow(ptr, 0, 0x1800);
1034 }
1035
1036 static __always_inline void mpx_test_helper1(uint8_t *buf, uint8_t *ptr)
1037 {
1038         /* these are hard-coded to check bnd0 */
1039         expected_bnd_index = 0;
1040         mpx_check_lowerbound_helper((unsigned long)(ptr-1));
1041         mpx_check_upperbound_helper((unsigned long)(ptr+0x1800));
1042         /* reset this since we do not expect any more bounds exceptions */
1043         expected_bnd_index = -1;
1044 }
1045
1046 static __always_inline void mpx_test_helper1_shadow(uint8_t *buf, uint8_t *ptr)
1047 {
1048         check_lowerbound_shadow(ptr-1, 0);
1049         check_upperbound_shadow(ptr+0x1800, 0);
1050 }
1051
1052 static __always_inline void mpx_test_helper2(uint8_t *buf, uint8_t *ptr)
1053 {
1054         mpx_make_bound_helper((unsigned long)ptr, 0x1800);
1055         mpx_movbndreg_helper();
1056         mpx_movbnd2mem_helper(buf);
1057         mpx_make_bound_helper((unsigned long)(ptr+0x12), 0x1800);
1058 }
1059
1060 static __always_inline void mpx_test_helper2_shadow(uint8_t *buf, uint8_t *ptr)
1061 {
1062         mkbnd_shadow(ptr, 0, 0x1800);
1063         movbndreg_shadow(0, 2);
1064         movbnd2mem_shadow(0, (unsigned long *)buf);
1065         mkbnd_shadow(ptr+0x12, 0, 0x1800);
1066 }
1067
1068 static __always_inline void mpx_test_helper3(uint8_t *buf, uint8_t *ptr)
1069 {
1070         mpx_movbnd_from_mem_helper(buf);
1071 }
1072
1073 static __always_inline void mpx_test_helper3_shadow(uint8_t *buf, uint8_t *ptr)
1074 {
1075         movbnd_from_mem_shadow((unsigned long *)buf, 0);
1076 }
1077
1078 static __always_inline void mpx_test_helper4(uint8_t *buf, uint8_t *ptr)
1079 {
1080         mpx_store_dsc_helper((unsigned long)buf, (unsigned long)ptr);
1081         mpx_make_bound_helper((unsigned long)(ptr+0x12), 0x1800);
1082 }
1083
1084 static __always_inline void mpx_test_helper4_shadow(uint8_t *buf, uint8_t *ptr)
1085 {
1086         stdsc_shadow(0, buf, ptr);
1087         mkbnd_shadow(ptr+0x12, 0, 0x1800);
1088 }
1089
1090 static __always_inline void mpx_test_helper5(uint8_t *buf, uint8_t *ptr)
1091 {
1092         mpx_load_dsc_helper((unsigned long)buf, (unsigned long)ptr);
1093 }
1094
1095 static __always_inline void mpx_test_helper5_shadow(uint8_t *buf, uint8_t *ptr)
1096 {
1097         lddsc_shadow(0, buf, ptr);
1098 }
1099
1100 #define NR_MPX_TEST_FUNCTIONS 6
1101
1102 /*
1103  * For compatibility reasons, MPX will clear the bounds registers
1104  * when you make function calls (among other things).  We have to
1105  * preserve the registers in between calls to the "helpers" since
1106  * they build on each other.
1107  *
1108  * Be very careful not to make any function calls inside the
1109  * helpers, or anywhere else beween the xrstor and xsave.
1110  */
1111 #define run_helper(helper_nr, buf, buf_shadow, ptr)     do {    \
1112         xrstor_state(xsave_test_buf, flags);                    \
1113         mpx_test_helper##helper_nr(buf, ptr);                   \
1114         xsave_state(xsave_test_buf, flags);                     \
1115         mpx_test_helper##helper_nr##_shadow(buf_shadow, ptr);   \
1116 } while (0)
1117
1118 static void run_helpers(int nr, uint8_t *buf, uint8_t *buf_shadow, uint8_t *ptr)
1119 {
1120         uint64_t flags = 0x18;
1121
1122         dprint_context(xsave_test_buf);
1123         switch (nr) {
1124         case 0:
1125                 run_helper(0, buf, buf_shadow, ptr);
1126                 break;
1127         case 1:
1128                 run_helper(1, buf, buf_shadow, ptr);
1129                 break;
1130         case 2:
1131                 run_helper(2, buf, buf_shadow, ptr);
1132                 break;
1133         case 3:
1134                 run_helper(3, buf, buf_shadow, ptr);
1135                 break;
1136         case 4:
1137                 run_helper(4, buf, buf_shadow, ptr);
1138                 break;
1139         case 5:
1140                 run_helper(5, buf, buf_shadow, ptr);
1141                 break;
1142         default:
1143                 test_failed();
1144                 break;
1145         }
1146         dprint_context(xsave_test_buf);
1147 }
1148
1149 unsigned long buf_shadow[1024]; /* used to check load / store descriptors */
1150 extern long inspect_me(struct mpx_bounds_dir *bounds_dir);
1151
1152 long cover_buf_with_bt_entries(void *buf, long buf_len)
1153 {
1154         int i;
1155         long nr_to_fill;
1156         int ratio = 1000;
1157         unsigned long buf_len_in_ptrs;
1158
1159         /* Fill about 1/100 of the space with bt entries */
1160         nr_to_fill = buf_len / (sizeof(unsigned long) * ratio);
1161
1162         if (!nr_to_fill)
1163                 dprintf3("%s() nr_to_fill: %ld\n", __func__, nr_to_fill);
1164
1165         /* Align the buffer to pointer size */
1166         while (((unsigned long)buf) % sizeof(void *)) {
1167                 buf++;
1168                 buf_len--;
1169         }
1170         /* We are storing pointers, so make */
1171         buf_len_in_ptrs = buf_len / sizeof(void *);
1172
1173         for (i = 0; i < nr_to_fill; i++) {
1174                 long index = (mpx_random() % buf_len_in_ptrs);
1175                 void *ptr = buf + index * sizeof(unsigned long);
1176                 unsigned long ptr_addr = (unsigned long)ptr;
1177
1178                 /* ptr and size can be anything */
1179                 mpx_make_bound_helper((unsigned long)ptr, 8);
1180
1181                 /*
1182                  * take bnd0 and put it in to bounds tables "buf + index" is an
1183                  * address inside the buffer where we are pretending that we
1184                  * are going to put a pointer We do not, though because we will
1185                  * never load entries from the table, so it doesn't matter.
1186                  */
1187                 mpx_store_dsc_helper(ptr_addr, (unsigned long)ptr);
1188                 dprintf4("storing bound table entry for %lx (buf start @ %p)\n",
1189                                 ptr_addr, buf);
1190         }
1191         return nr_to_fill;
1192 }
1193
1194 unsigned long align_down(unsigned long alignme, unsigned long align_to)
1195 {
1196         return alignme & ~(align_to-1);
1197 }
1198
1199 unsigned long align_up(unsigned long alignme, unsigned long align_to)
1200 {
1201         return (alignme + align_to - 1) & ~(align_to-1);
1202 }
1203
1204 /*
1205  * Using 1MB alignment guarantees that each no allocation
1206  * will overlap with another's bounds tables.
1207  *
1208  * We have to cook our own allocator here.  malloc() can
1209  * mix other allocation with ours which means that even
1210  * if we free all of our allocations, there might still
1211  * be bounds tables for the *areas* since there is other
1212  * valid memory there.
1213  *
1214  * We also can't use malloc() because a free() of an area
1215  * might not free it back to the kernel.  We want it
1216  * completely unmapped an malloc() does not guarantee
1217  * that.
1218  */
1219 #ifdef __i386__
1220 long alignment = 4096;
1221 long sz_alignment = 4096;
1222 #else
1223 long alignment = 1 * MB;
1224 long sz_alignment = 1 * MB;
1225 #endif
1226 void *mpx_mini_alloc(unsigned long sz)
1227 {
1228         unsigned long long tries = 0;
1229         static void *last;
1230         void *ptr;
1231         void *try_at;
1232
1233         sz = align_up(sz, sz_alignment);
1234
1235         try_at = last + alignment;
1236         while (1) {
1237                 ptr = mmap(try_at, sz, PROT_READ|PROT_WRITE,
1238                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1239                 if (ptr == (void *)-1)
1240                         return NULL;
1241                 if (ptr == try_at)
1242                         break;
1243
1244                 munmap(ptr, sz);
1245                 try_at += alignment;
1246 #ifdef __i386__
1247                 /*
1248                  * This isn't quite correct for 32-bit binaries
1249                  * on 64-bit kernels since they can use the
1250                  * entire 32-bit address space, but it's close
1251                  * enough.
1252                  */
1253                 if (try_at > (void *)0xC0000000)
1254 #else
1255                 if (try_at > (void *)0x0000800000000000)
1256 #endif
1257                         try_at = (void *)0x0;
1258                 if (!(++tries % 10000))
1259                         dprintf1("stuck in %s(), tries: %lld\n", __func__, tries);
1260                 continue;
1261         }
1262         last = ptr;
1263         dprintf3("mpx_mini_alloc(0x%lx) returning: %p\n", sz, ptr);
1264         return ptr;
1265 }
1266 void mpx_mini_free(void *ptr, long sz)
1267 {
1268         dprintf2("%s() ptr: %p\n", __func__, ptr);
1269         if ((unsigned long)ptr > 0x100000000000) {
1270                 dprintf1("uh oh !!!!!!!!!!!!!!! pointer too high: %p\n", ptr);
1271                 test_failed();
1272         }
1273         sz = align_up(sz, sz_alignment);
1274         dprintf3("%s() ptr: %p before munmap\n", __func__, ptr);
1275         munmap(ptr, sz);
1276         dprintf3("%s() ptr: %p DONE\n", __func__, ptr);
1277 }
1278
1279 #define NR_MALLOCS 100
1280 struct one_malloc {
1281         char *ptr;
1282         int nr_filled_btes;
1283         unsigned long size;
1284 };
1285 struct one_malloc mallocs[NR_MALLOCS];
1286
1287 void free_one_malloc(int index)
1288 {
1289         unsigned long free_ptr;
1290         unsigned long mask;
1291
1292         if (!mallocs[index].ptr)
1293                 return;
1294
1295         mpx_mini_free(mallocs[index].ptr, mallocs[index].size);
1296         dprintf4("freed[%d]:  %p\n", index, mallocs[index].ptr);
1297
1298         free_ptr = (unsigned long)mallocs[index].ptr;
1299         mask = alignment-1;
1300         dprintf4("lowerbits: %lx / %lx mask: %lx\n", free_ptr,
1301                         (free_ptr & mask), mask);
1302         assert((free_ptr & mask) == 0);
1303
1304         mallocs[index].ptr = NULL;
1305 }
1306
1307 #ifdef __i386__
1308 #define MPX_BOUNDS_TABLE_COVERS 4096
1309 #else
1310 #define MPX_BOUNDS_TABLE_COVERS (1 * MB)
1311 #endif
1312 void zap_everything(void)
1313 {
1314         long after_zap;
1315         long before_zap;
1316         int i;
1317
1318         before_zap = inspect_me(bounds_dir_ptr);
1319         dprintf1("zapping everything start: %ld\n", before_zap);
1320         for (i = 0; i < NR_MALLOCS; i++)
1321                 free_one_malloc(i);
1322
1323         after_zap = inspect_me(bounds_dir_ptr);
1324         dprintf1("zapping everything done: %ld\n", after_zap);
1325         /*
1326          * We only guarantee to empty the thing out if our allocations are
1327          * exactly aligned on the boundaries of a boudns table.
1328          */
1329         if ((alignment >= MPX_BOUNDS_TABLE_COVERS) &&
1330             (sz_alignment >= MPX_BOUNDS_TABLE_COVERS)) {
1331                 if (after_zap != 0)
1332                         test_failed();
1333
1334                 assert(after_zap == 0);
1335         }
1336 }
1337
1338 void do_one_malloc(void)
1339 {
1340         static int malloc_counter;
1341         long sz;
1342         int rand_index = (mpx_random() % NR_MALLOCS);
1343         void *ptr = mallocs[rand_index].ptr;
1344
1345         dprintf3("%s() enter\n", __func__);
1346
1347         if (ptr) {
1348                 dprintf3("freeing one malloc at index: %d\n", rand_index);
1349                 free_one_malloc(rand_index);
1350                 if (mpx_random() % (NR_MALLOCS*3) == 3) {
1351                         int i;
1352                         dprintf3("zapping some more\n");
1353                         for (i = rand_index; i < NR_MALLOCS; i++)
1354                                 free_one_malloc(i);
1355                 }
1356                 if ((mpx_random() % zap_all_every_this_many_mallocs) == 4)
1357                         zap_everything();
1358         }
1359
1360         /* 1->~1M */
1361         sz = (1 + mpx_random() % 1000) * 1000;
1362         ptr = mpx_mini_alloc(sz);
1363         if (!ptr) {
1364                 /*
1365                  * If we are failing allocations, just assume we
1366                  * are out of memory and zap everything.
1367                  */
1368                 dprintf3("zapping everything because out of memory\n");
1369                 zap_everything();
1370                 goto out;
1371         }
1372
1373         dprintf3("malloc: %p size: 0x%lx\n", ptr, sz);
1374         mallocs[rand_index].nr_filled_btes = cover_buf_with_bt_entries(ptr, sz);
1375         mallocs[rand_index].ptr = ptr;
1376         mallocs[rand_index].size = sz;
1377 out:
1378         if ((++malloc_counter) % inspect_every_this_many_mallocs == 0)
1379                 inspect_me(bounds_dir_ptr);
1380 }
1381
1382 void run_timed_test(void (*test_func)(void))
1383 {
1384         int done = 0;
1385         long iteration = 0;
1386         static time_t last_print;
1387         time_t now;
1388         time_t start;
1389
1390         time(&start);
1391         while (!done) {
1392                 time(&now);
1393                 if ((now - start) > TEST_DURATION_SECS)
1394                         done = 1;
1395
1396                 test_func();
1397                 iteration++;
1398
1399                 if ((now - last_print > 1) || done) {
1400                         printf("iteration %ld complete, OK so far\n", iteration);
1401                         last_print = now;
1402                 }
1403         }
1404 }
1405
1406 void check_bounds_table_frees(void)
1407 {
1408         printf("executing unmaptest\n");
1409         inspect_me(bounds_dir_ptr);
1410         run_timed_test(&do_one_malloc);
1411         printf("done with malloc() fun\n");
1412 }
1413
1414 void insn_test_failed(int test_nr, int test_round, void *buf,
1415                 void *buf_shadow, void *ptr)
1416 {
1417         print_context(xsave_test_buf);
1418         eprintf("ERROR: test %d round %d failed\n", test_nr, test_round);
1419         while (test_nr == 5) {
1420                 struct mpx_bt_entry *bte;
1421                 struct mpx_bounds_dir *bd = (void *)bounds_dir_ptr;
1422                 struct mpx_bd_entry *bde = mpx_vaddr_to_bd_entry(buf, bd);
1423
1424                 printf("  bd: %p\n", bd);
1425                 printf("&bde: %p\n", bde);
1426                 printf("*bde: %lx\n", *(unsigned long *)bde);
1427                 if (!bd_entry_valid(bde))
1428                         break;
1429
1430                 bte = mpx_vaddr_to_bt_entry(buf, bd);
1431                 printf(" te: %p\n", bte);
1432                 printf("bte[0]: %lx\n", bte->contents[0]);
1433                 printf("bte[1]: %lx\n", bte->contents[1]);
1434                 printf("bte[2]: %lx\n", bte->contents[2]);
1435                 printf("bte[3]: %lx\n", bte->contents[3]);
1436                 break;
1437         }
1438         test_failed();
1439 }
1440
1441 void check_mpx_insns_and_tables(void)
1442 {
1443         int successes = 0;
1444         int failures  = 0;
1445         int buf_size = (1024*1024);
1446         unsigned long *buf = malloc(buf_size);
1447         const int total_nr_tests = NR_MPX_TEST_FUNCTIONS * TEST_ROUNDS;
1448         int i, j;
1449
1450         memset(buf, 0, buf_size);
1451         memset(buf_shadow, 0, sizeof(buf_shadow));
1452
1453         for (i = 0; i < TEST_ROUNDS; i++) {
1454                 uint8_t *ptr = get_random_addr() + 8;
1455
1456                 for (j = 0; j < NR_MPX_TEST_FUNCTIONS; j++) {
1457                         if (0 && j != 5) {
1458                                 successes++;
1459                                 continue;
1460                         }
1461                         dprintf2("starting test %d round %d\n", j, i);
1462                         dprint_context(xsave_test_buf);
1463                         /*
1464                          * test5 loads an address from the bounds tables.
1465                          * The load will only complete if 'ptr' matches
1466                          * the load and the store, so with random addrs,
1467                          * the odds of this are very small.  Make it
1468                          * higher by only moving 'ptr' 1/10 times.
1469                          */
1470                         if (random() % 10 <= 0)
1471                                 ptr = get_random_addr() + 8;
1472                         dprintf3("random ptr{%p}\n", ptr);
1473                         dprint_context(xsave_test_buf);
1474                         run_helpers(j, (void *)buf, (void *)buf_shadow, ptr);
1475                         dprint_context(xsave_test_buf);
1476                         if (!compare_context(xsave_test_buf)) {
1477                                 insn_test_failed(j, i, buf, buf_shadow, ptr);
1478                                 failures++;
1479                                 goto exit;
1480                         }
1481                         successes++;
1482                         dprint_context(xsave_test_buf);
1483                         dprintf2("finished test %d round %d\n", j, i);
1484                         dprintf3("\n");
1485                         dprint_context(xsave_test_buf);
1486                 }
1487         }
1488
1489 exit:
1490         dprintf2("\nabout to free:\n");
1491         free(buf);
1492         dprintf1("successes: %d\n", successes);
1493         dprintf1(" failures: %d\n", failures);
1494         dprintf1("    tests: %d\n", total_nr_tests);
1495         dprintf1(" expected: %jd #BRs\n", num_upper_brs + num_lower_brs);
1496         dprintf1("      saw: %d #BRs\n", br_count);
1497         if (failures) {
1498                 eprintf("ERROR: non-zero number of failures\n");
1499                 exit(20);
1500         }
1501         if (successes != total_nr_tests) {
1502                 eprintf("ERROR: succeded fewer than number of tries (%d != %d)\n",
1503                                 successes, total_nr_tests);
1504                 exit(21);
1505         }
1506         if (num_upper_brs + num_lower_brs != br_count) {
1507                 eprintf("ERROR: unexpected number of #BRs: %jd %jd %d\n",
1508                                 num_upper_brs, num_lower_brs, br_count);
1509                 eprintf("successes: %d\n", successes);
1510                 eprintf(" failures: %d\n", failures);
1511                 eprintf("    tests: %d\n", total_nr_tests);
1512                 eprintf(" expected: %jd #BRs\n", num_upper_brs + num_lower_brs);
1513                 eprintf("      saw: %d #BRs\n", br_count);
1514                 exit(22);
1515         }
1516 }
1517
1518 /*
1519  * This is supposed to SIGSEGV nicely once the kernel
1520  * can no longer allocate vaddr space.
1521  */
1522 void exhaust_vaddr_space(void)
1523 {
1524         unsigned long ptr;
1525         /* Try to make sure there is no room for a bounds table anywhere */
1526         unsigned long skip = MPX_BOUNDS_TABLE_SIZE_BYTES - PAGE_SIZE;
1527 #ifdef __i386__
1528         unsigned long max_vaddr = 0xf7788000UL;
1529 #else
1530         unsigned long max_vaddr = 0x800000000000UL;
1531 #endif
1532
1533         dprintf1("%s() start\n", __func__);
1534         /* do not start at 0, we aren't allowed to map there */
1535         for (ptr = PAGE_SIZE; ptr < max_vaddr; ptr += skip) {
1536                 void *ptr_ret;
1537                 int ret = madvise((void *)ptr, PAGE_SIZE, MADV_NORMAL);
1538
1539                 if (!ret) {
1540                         dprintf1("madvise() %lx ret: %d\n", ptr, ret);
1541                         continue;
1542                 }
1543                 ptr_ret = mmap((void *)ptr, PAGE_SIZE, PROT_READ|PROT_WRITE,
1544                                 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1545                 if (ptr_ret != (void *)ptr) {
1546                         perror("mmap");
1547                         dprintf1("mmap(%lx) ret: %p\n", ptr, ptr_ret);
1548                         break;
1549                 }
1550                 if (!(ptr & 0xffffff))
1551                         dprintf1("mmap(%lx) ret: %p\n", ptr, ptr_ret);
1552         }
1553         for (ptr = PAGE_SIZE; ptr < max_vaddr; ptr += skip) {
1554                 dprintf2("covering 0x%lx with bounds table entries\n", ptr);
1555                 cover_buf_with_bt_entries((void *)ptr, PAGE_SIZE);
1556         }
1557         dprintf1("%s() end\n", __func__);
1558         printf("done with vaddr space fun\n");
1559 }
1560
1561 void mpx_table_test(void)
1562 {
1563         printf("starting mpx bounds table test\n");
1564         run_timed_test(check_mpx_insns_and_tables);
1565         printf("done with mpx bounds table test\n");
1566 }
1567
1568 int main(int argc, char **argv)
1569 {
1570         int unmaptest = 0;
1571         int vaddrexhaust = 0;
1572         int tabletest = 0;
1573         int i;
1574
1575         check_mpx_support();
1576         mpx_prepare();
1577         srandom(11179);
1578
1579         bd_incore();
1580         init();
1581         bd_incore();
1582
1583         trace_me();
1584
1585         xsave_state((void *)xsave_test_buf, 0x1f);
1586         if (!compare_context(xsave_test_buf))
1587                 printf("Init failed\n");
1588
1589         for (i = 1; i < argc; i++) {
1590                 if (!strcmp(argv[i], "unmaptest"))
1591                         unmaptest = 1;
1592                 if (!strcmp(argv[i], "vaddrexhaust"))
1593                         vaddrexhaust = 1;
1594                 if (!strcmp(argv[i], "tabletest"))
1595                         tabletest = 1;
1596         }
1597         if (!(unmaptest || vaddrexhaust || tabletest)) {
1598                 unmaptest = 1;
1599                 /* vaddrexhaust = 1; */
1600                 tabletest = 1;
1601         }
1602         if (unmaptest)
1603                 check_bounds_table_frees();
1604         if (tabletest)
1605                 mpx_table_test();
1606         if (vaddrexhaust)
1607                 exhaust_vaddr_space();
1608         printf("%s completed successfully\n", argv[0]);
1609         exit(0);
1610 }
1611
1612 #include "mpx-dig.c"