x86_64 assembly pack: add Goldmont performance results.
[oweals/openssl.git] / crypto / dso / dso_vms.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "dso_locl.h"
11
12 #ifdef OPENSSL_SYS_VMS
13
14 # pragma message disable DOLLARID
15 # include <errno.h>
16 # include <rms.h>
17 # include <lib$routines.h>
18 # include <libfisdef.h>
19 # include <stsdef.h>
20 # include <descrip.h>
21 # include <starlet.h>
22 # include "../vms_rms.h"
23
24 /* Some compiler options may mask the declaration of "_malloc32". */
25 # if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
26 #  if __INITIAL_POINTER_SIZE == 64
27 #   pragma pointer_size save
28 #   pragma pointer_size 32
29 void *_malloc32(__size_t);
30 #   pragma pointer_size restore
31 #  endif                        /* __INITIAL_POINTER_SIZE == 64 */
32 # endif                         /* __INITIAL_POINTER_SIZE && defined
33                                  * _ANSI_C_SOURCE */
34
35 # pragma message disable DOLLARID
36
37 static int vms_load(DSO *dso);
38 static int vms_unload(DSO *dso);
39 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
40 static char *vms_name_converter(DSO *dso, const char *filename);
41 static char *vms_merger(DSO *dso, const char *filespec1,
42                         const char *filespec2);
43
44 static DSO_METHOD dso_meth_vms = {
45     "OpenSSL 'VMS' shared library method",
46     vms_load,
47     NULL,                       /* unload */
48     vms_bind_func,
49     NULL,                       /* ctrl */
50     vms_name_converter,
51     vms_merger,
52     NULL,                       /* init */
53     NULL                        /* finish */
54 };
55
56 /*
57  * On VMS, the only "handle" is the file name.  LIB$FIND_IMAGE_SYMBOL depends
58  * on the reference to the file name being the same for all calls regarding
59  * one shared image, so we'll just store it in an instance of the following
60  * structure and put a pointer to that instance in the meth_data stack.
61  */
62 typedef struct dso_internal_st {
63     /*
64      * This should contain the name only, no directory, no extension, nothing
65      * but a name.
66      */
67     struct dsc$descriptor_s filename_dsc;
68     char filename[NAMX_MAXRSS + 1];
69     /*
70      * This contains whatever is not in filename, if needed. Normally not
71      * defined.
72      */
73     struct dsc$descriptor_s imagename_dsc;
74     char imagename[NAMX_MAXRSS + 1];
75 } DSO_VMS_INTERNAL;
76
77 DSO_METHOD *DSO_METHOD_openssl(void)
78 {
79     return &dso_meth_vms;
80 }
81
82 static int vms_load(DSO *dso)
83 {
84     void *ptr = NULL;
85     /* See applicable comments in dso_dl.c */
86     char *filename = DSO_convert_filename(dso, NULL);
87
88 /* Ensure 32-bit pointer for "p", and appropriate malloc() function. */
89 # if __INITIAL_POINTER_SIZE == 64
90 #  define DSO_MALLOC _malloc32
91 #  pragma pointer_size save
92 #  pragma pointer_size 32
93 # else                          /* __INITIAL_POINTER_SIZE == 64 */
94 #  define DSO_MALLOC OPENSSL_malloc
95 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
96
97     DSO_VMS_INTERNAL *p = NULL;
98
99 # if __INITIAL_POINTER_SIZE == 64
100 #  pragma pointer_size restore
101 # endif                         /* __INITIAL_POINTER_SIZE == 64 */
102
103     const char *sp1, *sp2;      /* Search result */
104     const char *ext = NULL;     /* possible extension to add */
105
106     if (filename == NULL) {
107         DSOerr(DSO_F_VMS_LOAD, DSO_R_NO_FILENAME);
108         goto err;
109     }
110
111     /*-
112      * A file specification may look like this:
113      *
114      *      node::dev:[dir-spec]name.type;ver
115      *
116      * or (for compatibility with TOPS-20):
117      *
118      *      node::dev:<dir-spec>name.type;ver
119      *
120      * and the dir-spec uses '.' as separator.  Also, a dir-spec
121      * may consist of several parts, with mixed use of [] and <>:
122      *
123      *      [dir1.]<dir2>
124      *
125      * We need to split the file specification into the name and
126      * the rest (both before and after the name itself).
127      */
128     /*
129      * Start with trying to find the end of a dir-spec, and save the position
130      * of the byte after in sp1
131      */
132     sp1 = strrchr(filename, ']');
133     sp2 = strrchr(filename, '>');
134     if (sp1 == NULL)
135         sp1 = sp2;
136     if (sp2 != NULL && sp2 > sp1)
137         sp1 = sp2;
138     if (sp1 == NULL)
139         sp1 = strrchr(filename, ':');
140     if (sp1 == NULL)
141         sp1 = filename;
142     else
143         sp1++;                  /* The byte after the found character */
144     /* Now, let's see if there's a type, and save the position in sp2 */
145     sp2 = strchr(sp1, '.');
146     /*
147      * If there is a period and the next character is a semi-colon,
148      * we need to add an extension
149      */
150     if (sp2 != NULL && sp2[1] == ';')
151         ext = ".EXE";
152     /*
153      * If we found it, that's where we'll cut.  Otherwise, look for a version
154      * number and save the position in sp2
155      */
156     if (sp2 == NULL) {
157         sp2 = strchr(sp1, ';');
158         ext = ".EXE";
159     }
160     /*
161      * If there was still nothing to find, set sp2 to point at the end of the
162      * string
163      */
164     if (sp2 == NULL)
165         sp2 = sp1 + strlen(sp1);
166
167     /* Check that we won't get buffer overflows */
168     if (sp2 - sp1 > FILENAME_MAX
169         || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
170         DSOerr(DSO_F_VMS_LOAD, DSO_R_FILENAME_TOO_BIG);
171         goto err;
172     }
173
174     p = DSO_MALLOC(sizeof(*p));
175     if (p == NULL) {
176         DSOerr(DSO_F_VMS_LOAD, ERR_R_MALLOC_FAILURE);
177         goto err;
178     }
179
180     strncpy(p->filename, sp1, sp2 - sp1);
181     p->filename[sp2 - sp1] = '\0';
182
183     strncpy(p->imagename, filename, sp1 - filename);
184     p->imagename[sp1 - filename] = '\0';
185     if (ext) {
186         strcat(p->imagename, ext);
187         if (*sp2 == '.')
188             sp2++;
189     }
190     strcat(p->imagename, sp2);
191
192     p->filename_dsc.dsc$w_length = strlen(p->filename);
193     p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
194     p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
195     p->filename_dsc.dsc$a_pointer = p->filename;
196     p->imagename_dsc.dsc$w_length = strlen(p->imagename);
197     p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
198     p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
199     p->imagename_dsc.dsc$a_pointer = p->imagename;
200
201     if (!sk_void_push(dso->meth_data, (char *)p)) {
202         DSOerr(DSO_F_VMS_LOAD, DSO_R_STACK_ERROR);
203         goto err;
204     }
205
206     /* Success (for now, we lie.  We actually do not know...) */
207     dso->loaded_filename = filename;
208     return (1);
209  err:
210     /* Cleanup! */
211     OPENSSL_free(p);
212     OPENSSL_free(filename);
213     return (0);
214 }
215
216 /*
217  * Note that this doesn't actually unload the shared image, as there is no
218  * such thing in VMS.  Next time it get loaded again, a new copy will
219  * actually be loaded.
220  */
221 static int vms_unload(DSO *dso)
222 {
223     DSO_VMS_INTERNAL *p;
224     if (dso == NULL) {
225         DSOerr(DSO_F_VMS_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
226         return (0);
227     }
228     if (sk_void_num(dso->meth_data) < 1)
229         return (1);
230     p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
231     if (p == NULL) {
232         DSOerr(DSO_F_VMS_UNLOAD, DSO_R_NULL_HANDLE);
233         return (0);
234     }
235     /* Cleanup */
236     OPENSSL_free(p);
237     return (1);
238 }
239
240 /*
241  * We must do this in a separate function because of the way the exception
242  * handler works (it makes this function return
243  */
244 static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
245                           struct dsc$descriptor_s *symname_dsc, void **sym,
246                           unsigned long flags)
247 {
248     /*
249      * Make sure that signals are caught and returned instead of aborting the
250      * program.  The exception handler gets unestablished automatically on
251      * return from this function.
252      */
253     lib$establish(lib$sig_to_ret);
254
255     if (ptr->imagename_dsc.dsc$w_length)
256         return lib$find_image_symbol(&ptr->filename_dsc,
257                                      symname_dsc, sym,
258                                      &ptr->imagename_dsc, flags);
259     else
260         return lib$find_image_symbol(&ptr->filename_dsc,
261                                      symname_dsc, sym, 0, flags);
262 }
263
264 # ifndef LIB$M_FIS_MIXEDCASE
265 #  define LIB$M_FIS_MIXEDCASE (1 << 4);
266 # endif
267 void vms_bind_sym(DSO *dso, const char *symname, void **sym)
268 {
269     DSO_VMS_INTERNAL *ptr;
270     int status = 0;
271     struct dsc$descriptor_s symname_dsc;
272
273 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
274 # if __INITIAL_POINTER_SIZE == 64
275 #  define SYMNAME symname_32p
276 #  pragma pointer_size save
277 #  pragma pointer_size 32
278     char *symname_32p;
279 #  pragma pointer_size restore
280     char symname_32[NAMX_MAXRSS + 1];
281 # else                          /* __INITIAL_POINTER_SIZE == 64 */
282 #  define SYMNAME ((char *) symname)
283 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
284
285     *sym = NULL;
286
287     if ((dso == NULL) || (symname == NULL)) {
288         DSOerr(DSO_F_VMS_BIND_SYM, ERR_R_PASSED_NULL_PARAMETER);
289         return;
290     }
291 # if __INITIAL_POINTER_SIZE == 64
292     /* Copy the symbol name to storage with a 32-bit pointer. */
293     symname_32p = symname_32;
294     strcpy(symname_32p, symname);
295 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
296
297     symname_dsc.dsc$w_length = strlen(SYMNAME);
298     symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
299     symname_dsc.dsc$b_class = DSC$K_CLASS_S;
300     symname_dsc.dsc$a_pointer = SYMNAME;
301
302     if (sk_void_num(dso->meth_data) < 1) {
303         DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_STACK_ERROR);
304         return;
305     }
306     ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
307                                             sk_void_num(dso->meth_data) - 1);
308     if (ptr == NULL) {
309         DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_NULL_HANDLE);
310         return;
311     }
312
313     status = do_find_symbol(ptr, &symname_dsc, sym, LIB$M_FIS_MIXEDCASE);
314
315     if (!$VMS_STATUS_SUCCESS(status))
316         status = do_find_symbol(ptr, &symname_dsc, sym, 0);
317
318     if (!$VMS_STATUS_SUCCESS(status)) {
319         unsigned short length;
320         char errstring[257];
321         struct dsc$descriptor_s errstring_dsc;
322
323         errstring_dsc.dsc$w_length = sizeof(errstring);
324         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
325         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
326         errstring_dsc.dsc$a_pointer = errstring;
327
328         *sym = NULL;
329
330         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
331
332         if (!$VMS_STATUS_SUCCESS(status))
333             lib$signal(status); /* This is really bad.  Abort! */
334         else {
335             errstring[length] = '\0';
336
337             DSOerr(DSO_F_VMS_BIND_SYM, DSO_R_SYM_FAILURE);
338             if (ptr->imagename_dsc.dsc$w_length)
339                 ERR_add_error_data(9,
340                                    "Symbol ", symname,
341                                    " in ", ptr->filename,
342                                    " (", ptr->imagename, ")",
343                                    ": ", errstring);
344             else
345                 ERR_add_error_data(6,
346                                    "Symbol ", symname,
347                                    " in ", ptr->filename, ": ", errstring);
348         }
349         return;
350     }
351     return;
352 }
353
354 static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
355 {
356     DSO_FUNC_TYPE sym = 0;
357     vms_bind_sym(dso, symname, (void **)&sym);
358     return sym;
359 }
360
361 static char *vms_merger(DSO *dso, const char *filespec1,
362                         const char *filespec2)
363 {
364     int status;
365     int filespec1len, filespec2len;
366     struct FAB fab;
367     struct NAMX_STRUCT nam;
368     char esa[NAMX_MAXRSS + 1];
369     char *merged;
370
371 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
372 # if __INITIAL_POINTER_SIZE == 64
373 #  define FILESPEC1 filespec1_32p;
374 #  define FILESPEC2 filespec2_32p;
375 #  pragma pointer_size save
376 #  pragma pointer_size 32
377     char *filespec1_32p;
378     char *filespec2_32p;
379 #  pragma pointer_size restore
380     char filespec1_32[NAMX_MAXRSS + 1];
381     char filespec2_32[NAMX_MAXRSS + 1];
382 # else                          /* __INITIAL_POINTER_SIZE == 64 */
383 #  define FILESPEC1 ((char *) filespec1)
384 #  define FILESPEC2 ((char *) filespec2)
385 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
386
387     if (!filespec1)
388         filespec1 = "";
389     if (!filespec2)
390         filespec2 = "";
391     filespec1len = strlen(filespec1);
392     filespec2len = strlen(filespec2);
393
394 # if __INITIAL_POINTER_SIZE == 64
395     /* Copy the file names to storage with a 32-bit pointer. */
396     filespec1_32p = filespec1_32;
397     filespec2_32p = filespec2_32;
398     strcpy(filespec1_32p, filespec1);
399     strcpy(filespec2_32p, filespec2);
400 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
401
402     fab = cc$rms_fab;
403     nam = CC_RMS_NAMX;
404
405     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
406     FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
407     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
408     FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
409     NAMX_DNA_FNA_SET(fab)
410
411         nam.NAMX_ESA = esa;
412     nam.NAMX_ESS = NAMX_MAXRSS;
413     nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
414     SET_NAMX_NO_SHORT_UPCASE(nam);
415
416     fab.FAB_NAMX = &nam;
417
418     status = sys$parse(&fab, 0, 0);
419
420     if (!$VMS_STATUS_SUCCESS(status)) {
421         unsigned short length;
422         char errstring[257];
423         struct dsc$descriptor_s errstring_dsc;
424
425         errstring_dsc.dsc$w_length = sizeof(errstring);
426         errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
427         errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
428         errstring_dsc.dsc$a_pointer = errstring;
429
430         status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);
431
432         if (!$VMS_STATUS_SUCCESS(status))
433             lib$signal(status); /* This is really bad.  Abort! */
434         else {
435             errstring[length] = '\0';
436
437             DSOerr(DSO_F_VMS_MERGER, DSO_R_FAILURE);
438             ERR_add_error_data(7,
439                                "filespec \"", filespec1, "\", ",
440                                "defaults \"", filespec2, "\": ", errstring);
441         }
442         return (NULL);
443     }
444
445     merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
446     if (merged == NULL)
447         goto malloc_err;
448     strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
449     merged[nam.NAMX_ESL] = '\0';
450     return (merged);
451  malloc_err:
452     DSOerr(DSO_F_VMS_MERGER, ERR_R_MALLOC_FAILURE);
453 }
454
455 static char *vms_name_converter(DSO *dso, const char *filename)
456 {
457     int len = strlen(filename);
458     char *not_translated = OPENSSL_malloc(len + 1);
459     if (not_translated != NULL)
460         strcpy(not_translated, filename);
461     return (not_translated);
462 }
463
464 #endif                          /* OPENSSL_SYS_VMS */