gnu: Add kafs-client
[jackhill/guix/guix.git] / gnu / packages / patches / gcc-strmov-store-file-names.patch
CommitLineData
80337723
LC
1Make sure that statements such as:
2
3 strcpy (dst, "/gnu/store/…");
4
e2885727
LC
5or
6
7 static const char str[] = "/gnu/store/…";
8
9 strcpy (dst, str);
10
80337723 11do not result in chunked /gnu/store strings that are undetectable by
e2885727
LC
12Guix's GC and its grafting code. See <https://bugs.gnu.org/24703>
13and <https://bugs.gnu.org/30395>.
14
80337723
LC
15
16--- gcc-5.3.0/gcc/builtins.c 2016-10-18 10:50:46.080616285 +0200
17+++ gcc-5.3.0/gcc/builtins.c 2016-11-09 15:26:43.693042737 +0100
243ea867 18@@ -3192,6 +3192,58 @@ determine_block_size (tree len, rtx len_
80337723
LC
19 GET_MODE_MASK (GET_MODE (len_rtx)));
20 }
21
e2885727
LC
22+extern void debug_tree (tree);
23+
80337723
LC
24+/* Return true if STR contains the string "/gnu/store". */
25+
e2885727 26+bool
80337723
LC
27+store_reference_p (tree str)
28+{
e2885727
LC
29+ if (getenv ("GUIX_GCC_DEBUG") != NULL)
30+ debug_tree (str);
31+
80337723
LC
32+ if (TREE_CODE (str) == ADDR_EXPR)
33+ str = TREE_OPERAND (str, 0);
34+
e2885727
LC
35+ if (TREE_CODE (str) == VAR_DECL
36+ && TREE_STATIC (str)
37+ && TREE_READONLY (str))
243ea867
LC
38+ {
39+ /* STR may be a 'static const' variable whose initial value
40+ is a string constant. See <https://bugs.gnu.org/30395>. */
41+ str = DECL_INITIAL (str);
42+ if (str == NULL_TREE)
43+ return false;
44+ }
e2885727 45+
80337723
LC
46+ if (TREE_CODE (str) != STRING_CST)
47+ return false;
48+
49+ int len;
50+ const char *store;
51+
52+ store = getenv ("NIX_STORE") ? getenv ("NIX_STORE") : "/gnu/store";
53+ len = strlen (store);
54+
55+ /* Size of the hash part of store file names, including leading slash and
56+ trailing hyphen. */
57+ const int hash_len = 34;
58+
59+ if (TREE_STRING_LENGTH (str) < len + hash_len)
60+ return false;
61+
62+ /* We cannot use 'strstr' because 'TREE_STRING_POINTER' returns a string
63+ that is not necessarily NUL-terminated. */
64+
65+ for (int i = 0; i < TREE_STRING_LENGTH (str) - (len + hash_len); i++)
66+ {
67+ if (strncmp (TREE_STRING_POINTER (str) + i, store, len) == 0)
68+ return true;
69+ }
70+
71+ return false;
72+}
73+
74 /* Helper function to do the actual work for expand_builtin_memcpy. */
75
76 static rtx
77@@ -3207,6 +3243,13 @@ expand_builtin_memcpy_args (tree dest, t
78 unsigned HOST_WIDE_INT max_size;
79 unsigned HOST_WIDE_INT probable_max_size;
80
81+ /* Do not emit block moves, which translate to the 'movabs' instruction on
82+ x86_64, when SRC refers to store items. That way, store references
83+ remain visible to the Guix GC and grafting code. See
e2885727 84+ <https://bugs.gnu.org/24703>. */
80337723
LC
85+ if (store_reference_p (src))
86+ return NULL_RTX;
87+
88 /* If DEST is not a pointer type, call the normal function. */
89 if (dest_align == 0)
90 return NULL_RTX;
e2885727
LC
91
92--- gcc-5.5.0/gcc/gimple-fold.c 2018-03-20 11:36:16.709442004 +0100
93+++ gcc-5.5.0/gcc/gimple-fold.c 2018-03-20 11:46:43.838487065 +0100
94@@ -769,6 +769,8 @@ var_decl_component_p (tree var)
95 return SSA_VAR_P (inner);
96 }
97
98+extern bool store_reference_p (tree);
99+
100 /* Fold function call to builtin mem{{,p}cpy,move}. Return
101 false if no simplification can be made.
102 If ENDP is 0, return DEST (like memcpy).
103@@ -1099,6 +1101,9 @@ gimple_fold_builtin_memory_op (gimple_st
104 if (!srctype)
105 return false;
106
107+ if (store_reference_p (src))
108+ return false;
109+
110 src_align = get_pointer_alignment (src);
111 dest_align = get_pointer_alignment (dest);
112 if (dest_align < TYPE_ALIGN (desttype)