Debian Codegraph @

lib/alg-yescrypt-platform.c

raw symbols SCIP
1 /*-
2 * Copyright 2013-2018,2022 Alexander Peslyak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
9 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
11 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
12 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
16 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
17 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18 * SUCH DAMAGE.
19 */
20
21 #ifdef __unix__cxx . . $ `<built-in>:395:9`!
22 #include <sys/mman.h>
23 #endif
24 #ifdef __linux__cxx . . $ `<built-in>:398:9`!
25 #include <linux/mman.h> /* for MAP_HUGE_2MB */
26 #endif
27
28 #define HUGEPAGE_THRESHOLDcxx . . $ `lib/alg-yescrypt-platform.c:28:9`! (32 * 1024 * 1024)
29
30 #ifdef __x86_64__cxx . . $ `<built-in>:372:9`!
31 #define HUGEPAGE_SIZEcxx . . $ `lib/alg-yescrypt-platform.c:31:9`! (2 * 1024 * 1024)
32 #else
33 #undef HUGEPAGE_SIZE
34 #endif
35
36 static void *alloc_regioncxx . . $ alloc_region(a6b0cddae93ea2fa).(yescrypt_region_tcxx . . $ yescrypt_region_t# *region, size_t size)
37 {
38 size_t base_size = size;
39 uint8_tcxx . . $ uint8_t# *base, *aligned;
40 #ifdef MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`!
41 unsigned int flags =
42 #ifdef MAP_NOCORE
43 MAP_NOCORE |
44 #endif
45 MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`! | MAP_PRIVATEcxx . . $ `<external>/12095511514740901327/mman.h:18:9`!;
46 #if defined(MAP_HUGETLBcxx . . $ `<external>/794195886694646091/mman-common.h:29:9`!) && defined(MAP_HUGE_2MBcxx . . $ `<external>/12095511514740901327/mman.h:36:9`!) && defined(HUGEPAGE_SIZEcxx . . $ `lib/alg-yescrypt-platform.c:31:9`!)
47 size_t new_size = size;
48 const size_t hugepage_mask = (size_t)HUGEPAGE_SIZEcxx . . $ `lib/alg-yescrypt-platform.c:31:9`! - 1;
49 if (size >= HUGEPAGE_THRESHOLDcxx . . $ `lib/alg-yescrypt-platform.c:28:9`! && size + hugepage_mask >= size) {
50 flags |= MAP_HUGETLBcxx . . $ `<external>/794195886694646091/mman-common.h:29:9`! | MAP_HUGE_2MBcxx . . $ `<external>/12095511514740901327/mman.h:36:9`!;
51 /*
52 * Linux's munmap() fails on MAP_HUGETLB mappings if size is not a multiple of
53 * huge page size, so let's round up to huge page size here.
54 */
55 new_size = size + hugepage_mask;
56 new_size &= ~hugepage_mask;
57 }
58 base = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, new_size, PROT_READcxx . . $ `<external>/794195886694646091/mman-common.h:10:9`! | PROT_WRITEcxx . . $ `<external>/794195886694646091/mman-common.h:11:9`!, (int)flags, -1, 0);
59 if (base != MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) {
60 base_size = new_size;
61 } else if (flags & MAP_HUGETLBcxx . . $ `<external>/794195886694646091/mman-common.h:29:9`!) {
62 flags &= ~(unsigned int)(MAP_HUGETLBcxx . . $ `<external>/794195886694646091/mman-common.h:29:9`! | MAP_HUGE_2MBcxx . . $ `<external>/12095511514740901327/mman.h:36:9`!);
63 base = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, size, PROT_READcxx . . $ `<external>/794195886694646091/mman-common.h:10:9`! | PROT_WRITEcxx . . $ `<external>/794195886694646091/mman-common.h:11:9`!, (int)flags, -1, 0);
64 }
65
66 #else
67 base = mmap(NULL, size, PROT_READ | PROT_WRITE, (int)flags, -1, 0);
68 #endif
69 if (base == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!)
70 base = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
71 aligned = base;
72 #else /* mmap not available */
73 base = aligned = NULL;
74 if (size + 63 < size) {
75 errno = ENOMEM;
76 } else if ((base = malloc(size + 63)) != NULL) {
77 aligned = base + 63;
78 aligned -= (uintptr_t)aligned & 63;
79 }
80 #endif
81 region->basecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base. = base;
82 region->alignedcxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#aligned. = aligned;
83 region->base_sizecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base_size. = base ? base_size : 0;
84 region->aligned_sizecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#aligned_size. = base ? size : 0;
85 return aligned;
86 }
87
88 static inline void init_regioncxx . . $ init_region(fd3272d669fc1463).(yescrypt_region_tcxx . . $ yescrypt_region_t# *region)
89 {
90 region->basecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base. = region->alignedcxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#aligned. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!;
91 region->base_sizecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base_size. = region->aligned_sizecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#aligned_size. = 0;
92 }
93
94 static int free_regioncxx . . $ free_region(32522aed775e25b7).(yescrypt_region_tcxx . . $ yescrypt_region_t# *region)
95 {
96 if (region->basecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base.) {
97 #ifdef MAP_ANONcxx . . $ `<external>/17226767460805440860/mman-linux.h:57:9`!
98 if (munmapcxx . . $ munmap(c0a5f96701d3f89).(region->basecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base., region->base_sizecxx . . $ $anonymous_type_b025ac6af4ec7e0f_0#base_size.))
99 return -1;
100 #else
101 free(region->base);
102 #endif
103 }
104 init_regioncxx . . $ init_region(fd3272d669fc1463).(region);
105 return 0;
106 }