1 /* Verify that changing xmm 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 #include <emmintrin.h>
15 
16 unsigned int
la_version(unsigned int v)17 la_version (unsigned int v)
18 {
19   setlinebuf (stdout);
20 
21   printf ("version: %u\n", v);
22 
23   char buf[20];
24   sprintf (buf, "%u", v);
25 
26   return v;
27 }
28 
29 void
la_activity(uintptr_t * cookie,unsigned int flag)30 la_activity (uintptr_t *cookie, unsigned int flag)
31 {
32   if (flag == LA_ACT_CONSISTENT)
33     printf ("activity: consistent\n");
34   else if (flag == LA_ACT_ADD)
35     printf ("activity: add\n");
36   else if (flag == LA_ACT_DELETE)
37     printf ("activity: delete\n");
38   else
39     printf ("activity: unknown activity %u\n", flag);
40 }
41 
42 char *
la_objsearch(const char * name,uintptr_t * cookie,unsigned int flag)43 la_objsearch (const char *name, uintptr_t *cookie, unsigned int flag)
44 {
45   char buf[100];
46   const char *flagstr;
47   if (flag == LA_SER_ORIG)
48     flagstr = "LA_SET_ORIG";
49   else if (flag == LA_SER_LIBPATH)
50     flagstr = "LA_SER_LIBPATH";
51   else if (flag == LA_SER_RUNPATH)
52     flagstr = "LA_SER_RUNPATH";
53   else if (flag == LA_SER_CONFIG)
54     flagstr = "LA_SER_CONFIG";
55   else if (flag == LA_SER_DEFAULT)
56     flagstr = "LA_SER_DEFAULT";
57   else if (flag == LA_SER_SECURE)
58     flagstr = "LA_SER_SECURE";
59   else
60     {
61        sprintf (buf, "unknown flag %d", flag);
62        flagstr = buf;
63     }
64   printf ("objsearch: %s, %s\n", name, flagstr);
65 
66   return (char *) name;
67 }
68 
69 unsigned int
la_objopen(struct link_map * l,Lmid_t lmid,uintptr_t * cookie)70 la_objopen (struct link_map *l, Lmid_t lmid, uintptr_t *cookie)
71 {
72   printf ("objopen: %ld, %s\n", lmid, l->l_name);
73 
74   return 3;
75 }
76 
77 void
la_preinit(uintptr_t * cookie)78 la_preinit (uintptr_t *cookie)
79 {
80   printf ("preinit\n");
81 }
82 
83 unsigned int
la_objclose(uintptr_t * cookie)84 la_objclose  (uintptr_t *cookie)
85 {
86   printf ("objclose\n");
87   return 0;
88 }
89 
90 uintptr_t
la_symbind32(Elf32_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)91 la_symbind32 (Elf32_Sym *sym, unsigned int ndx, uintptr_t *refcook,
92 	      uintptr_t *defcook, unsigned int *flags, const char *symname)
93 {
94   printf ("symbind32: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
95 	  symname, (long int) sym->st_value, ndx, *flags);
96 
97   return sym->st_value;
98 }
99 
100 uintptr_t
la_symbind64(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)101 la_symbind64 (Elf64_Sym *sym, unsigned int ndx, uintptr_t *refcook,
102 	      uintptr_t *defcook, unsigned int *flags, const char *symname)
103 {
104   printf ("symbind64: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
105 	  symname, (long int) sym->st_value, ndx, *flags);
106 
107   return sym->st_value;
108 }
109 
110 #include <tst-audit.h>
111 
112 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)113 pltenter (ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
114 	  uintptr_t *defcook, La_regs *regs, unsigned int *flags,
115 	  const char *symname, long int *framesizep)
116 {
117   printf ("pltenter: symname=%s, st_value=%#lx, ndx=%u, flags=%u\n",
118 	  symname, (long int) sym->st_value, ndx, *flags);
119 
120   __m128i minusone = _mm_set1_epi32 (-1);
121 
122   if (strcmp (symname, "audit_test") == 0)
123     {
124       __m128i zero = _mm_setzero_si128 ();
125       if (memcmp (&regs->lr_xmm[0], &zero, sizeof (zero))
126 	  || memcmp (&regs->lr_xmm[1], &zero, sizeof (zero))
127 	  || memcmp (&regs->lr_xmm[2], &zero, sizeof (zero))
128 	  || memcmp (&regs->lr_xmm[3], &zero, sizeof (zero))
129 	  || memcmp (&regs->lr_xmm[4], &zero, sizeof (zero))
130 	  || memcmp (&regs->lr_xmm[5], &zero, sizeof (zero))
131 	  || memcmp (&regs->lr_xmm[6], &zero, sizeof (zero))
132 	  || memcmp (&regs->lr_xmm[7], &zero, sizeof (zero)))
133 	abort ();
134 
135       for (int i = 0; i < 8; i++)
136 	regs->lr_xmm[i] = (La_x86_64_xmm) _mm_set1_epi32 (i + 1);
137 
138       *framesizep = 1024;
139     }
140 
141   asm volatile ("movdqa %0, %%xmm0" : : "x" (minusone) : "xmm0" );
142   asm volatile ("movdqa %0, %%xmm1" : : "x" (minusone) : "xmm1" );
143   asm volatile ("movdqa %0, %%xmm2" : : "x" (minusone) : "xmm2" );
144   asm volatile ("movdqa %0, %%xmm3" : : "x" (minusone) : "xmm3" );
145   asm volatile ("movdqa %0, %%xmm4" : : "x" (minusone) : "xmm4" );
146   asm volatile ("movdqa %0, %%xmm5" : : "x" (minusone) : "xmm5" );
147   asm volatile ("movdqa %0, %%xmm6" : : "x" (minusone) : "xmm6" );
148   asm volatile ("movdqa %0, %%xmm7" : : "x" (minusone) : "xmm7" );
149 
150   return sym->st_value;
151 }
152 
153 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)154 pltexit (ElfW(Sym) *sym, unsigned int ndx, uintptr_t *refcook,
155 	 uintptr_t *defcook, const La_regs *inregs, La_retval *outregs,
156 	 const char *symname)
157 {
158   printf ("pltexit: symname=%s, st_value=%#lx, ndx=%u, retval=%tu\n",
159 	  symname, (long int) sym->st_value, ndx,
160 	  (ptrdiff_t) outregs->int_retval);
161 
162   __m128i xmm;
163 
164   if (strcmp (symname, "audit_test") == 0)
165     {
166       __m128i zero = _mm_setzero_si128 ();
167       if (memcmp (&outregs->lrv_xmm0, &zero, sizeof (zero)))
168 	abort ();
169 
170       for (int i = 0; i < 8; i++)
171 	{
172 	  xmm = _mm_set1_epi32 (i + 1);
173 	  if (memcmp (&inregs->lr_xmm[i], &xmm, sizeof (xmm)) != 0)
174 	    abort ();
175 	}
176 
177       outregs->lrv_xmm0 = (La_x86_64_xmm) _mm_set1_epi32 (0x12349876);
178     }
179 
180   xmm = _mm_set1_epi32 (-1);
181   asm volatile ("movdqa %0, %%xmm0" : : "x" (xmm) : "xmm0" );
182   asm volatile ("movdqa %0, %%xmm1" : : "x" (xmm) : "xmm1" );
183 
184   return 0;
185 }
186