1 /* Verify that changing AVX registers in audit library won't affect
2    function parameter passing/return.  */
3 
4 #include <dlfcn.h>
5 #include <link.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <bits/wordsize.h>
13 #include <gnu/lib-names.h>
14 
15 unsigned int
la_version(unsigned int v)16 la_version (unsigned int v)
17 {
18   setlinebuf (stdout);
19 
20   printf ("version: %u\n", v);
21 
22   char buf[20];
23   sprintf (buf, "%u", v);
24 
25   return v;
26 }
27 
28 void
la_activity(uintptr_t * cookie,unsigned int flag)29 la_activity (uintptr_t *cookie, unsigned int flag)
30 {
31   if (flag == LA_ACT_CONSISTENT)
32     printf ("activity: consistent\n");
33   else if (flag == LA_ACT_ADD)
34     printf ("activity: add\n");
35   else if (flag == LA_ACT_DELETE)
36     printf ("activity: delete\n");
37   else
38     printf ("activity: unknown activity %u\n", flag);
39 }
40 
41 char *
la_objsearch(const char * name,uintptr_t * cookie,unsigned int flag)42 la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
43 {
44   char buf[100];
45   const char *flagstr;
46   if (flag == LA_SER_ORIG)
47     flagstr = "LA_SET_ORIG";
48   else if (flag == LA_SER_LIBPATH)
49     flagstr = "LA_SER_LIBPATH";
50   else if (flag == LA_SER_RUNPATH)
51     flagstr = "LA_SER_RUNPATH";
52   else if (flag == LA_SER_CONFIG)
53     flagstr = "LA_SER_CONFIG";
54   else if (flag == LA_SER_DEFAULT)
55     flagstr = "LA_SER_DEFAULT";
56   else if (flag == LA_SER_SECURE)
57     flagstr = "LA_SER_SECURE";
58   else
59     {
60        sprintf (buf, "unknown flag %d", flag);
61        flagstr = buf;
62     }
63   printf ("objsearch: %s, %s\n", name, flagstr);
64 
65   return (char *) name;
66 }
67 
68 unsigned int
la_objopen(struct link_map * l,Lmid_t lmid,uintptr_t * cookie)69 la_objopen (struct link_map *l, Lmid_t lmid, uintptr_t *cookie)
70 {
71   printf ("objopen: %ld, %s\n", lmid, l->l_name);
72 
73   return 3;
74 }
75 
76 void
la_preinit(uintptr_t * cookie)77 la_preinit (uintptr_t *cookie)
78 {
79   printf ("preinit\n");
80 }
81 
82 unsigned int
la_objclose(uintptr_t * cookie)83 la_objclose  (uintptr_t *cookie)
84 {
85   printf ("objclose\n");
86   return 0;
87 }
88 
89 uintptr_t
la_symbind32(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)90 la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
91 	      uintptr_t *defcook, unsigned int *flags, const char *symname)
92 {
93   printf ("symbind32: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
94 	  symname, (long int) sym->st_value, ndx, *flags);
95 
96   return sym->st_value;
97 }
98 
99 uintptr_t
la_symbind64(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)100 la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
101 	      uintptr_t *defcook, unsigned int *flags, const char *symname)
102 {
103   printf ("symbind64: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
104 	  symname, (long int) sym->st_value, ndx, *flags);
105 
106   return sym->st_value;
107 }
108 
109 #include <tst-audit.h>
110 
111 #ifdef __AVX__
112 #include <immintrin.h>
113 #include <cpuid.h>
114 
115 static int avx = -1;
116 
117 static inline int
118 __attribute ((always_inline))
check_avx(void)119 check_avx (void)
120 {
121   if (avx == -1)
122     {
123       unsigned int eax, ebx, ecx, edx;
124 
125       if (__get_cpuid (1, &eax, &ebx, &ecx, &edx)
126 	  && (ecx & bit_AVX))
127 	avx = 1;
128       else
129 	avx = 0;
130     }
131   return avx;
132 }
133 #else
134 #include <emmintrin.h>
135 #endif
136 
137 ElfW(Addr)
pltenter(ElfW (Sym)* sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,La_regs * regs,unsigned int * flags,const char * symname,long int * framesizep)138 pltenter (ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
139 	  uintptr_t *defcook, La_regs *regs, unsigned int *flags,
140 	  const char *symname, long int *framesizep)
141 {
142   printf ("pltenter: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
143 	  symname, (long int) sym->st_value, ndx, *flags);
144 
145 #ifdef __AVX__
146   if (check_avx () && strcmp (symname, "audit_test") == 0)
147     {
148       __m256i zero = _mm256_setzero_si256 ();
149       if (memcmp (&regs->lr_vector[0], &zero, sizeof (zero))
150 	  || memcmp (&regs->lr_vector[1], &zero, sizeof (zero))
151 	  || memcmp (&regs->lr_vector[2], &zero, sizeof (zero))
152 	  || memcmp (&regs->lr_vector[3], &zero, sizeof (zero))
153 	  || memcmp (&regs->lr_vector[4], &zero, sizeof (zero))
154 	  || memcmp (&regs->lr_vector[5], &zero, sizeof (zero))
155 	  || memcmp (&regs->lr_vector[6], &zero, sizeof (zero))
156 	  || memcmp (&regs->lr_vector[7], &zero, sizeof (zero)))
157 	abort ();
158 
159       for (int i = 0; i < 8; i++)
160 	regs->lr_vector[i].ymm[0]
161 	  = (La_x86_64_ymm) _mm256_set1_epi32 (i + 1);
162 
163       __m256i ymm = _mm256_set1_epi32 (-1);
164       asm volatile ("vmovdqa %0, %%ymm0" : : "x" (ymm) : "xmm0" );
165       asm volatile ("vmovdqa %0, %%ymm1" : : "x" (ymm) : "xmm1" );
166       asm volatile ("vmovdqa %0, %%ymm2" : : "x" (ymm) : "xmm2" );
167       asm volatile ("vmovdqa %0, %%ymm3" : : "x" (ymm) : "xmm3" );
168       asm volatile ("vmovdqa %0, %%ymm4" : : "x" (ymm) : "xmm4" );
169       asm volatile ("vmovdqa %0, %%ymm5" : : "x" (ymm) : "xmm5" );
170       asm volatile ("vmovdqa %0, %%ymm6" : : "x" (ymm) : "xmm6" );
171       asm volatile ("vmovdqa %0, %%ymm7" : : "x" (ymm) : "xmm7" );
172 
173       *framesizep = 1024;
174     }
175 #endif
176 
177   return sym->st_value;
178 }
179 
180 unsigned int
pltexit(ElfW (Sym)* sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,const La_regs * inregs,La_retval * outregs,const char * symname)181 pltexit (ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
182 	 uintptr_t *defcook, const La_regs *inregs, La_retval *outregs,
183 	 const char *symname)
184 {
185   printf ("pltexit: symname=%s, st_value=%#lx, ndx=%u, retval=%tu\n",
186 	  symname, (long int) sym->st_value, ndx,
187 	  (ptrdiff_t) outregs->int_retval);
188 
189 #ifdef __AVX__
190   if (check_avx () && strcmp (symname, "audit_test") == 0)
191     {
192       __m256i zero = _mm256_setzero_si256 ();
193       if (memcmp (&outregs->lrv_vector0, &zero, sizeof (zero)))
194 	abort ();
195 
196       for (int i = 0; i < 8; i++)
197 	{
198 	  __m256i ymm = _mm256_set1_epi32 (i + 1);
199 	  if (memcmp (&inregs->lr_vector[i], &ymm, sizeof (ymm)) != 0)
200 	    abort ();
201 	}
202 
203       outregs->lrv_vector0.ymm[0]
204 	= (La_x86_64_ymm) _mm256_set1_epi32 (0x12349876);
205 
206       __m256i ymm = _mm256_set1_epi32 (-1);
207       asm volatile ("vmovdqa %0, %%ymm0" : : "x" (ymm) : "xmm0" );
208       asm volatile ("vmovdqa %0, %%ymm1" : : "x" (ymm) : "xmm1" );
209     }
210 #endif
211 
212   return 0;
213 }
214