authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-17 13:24:29+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-23 12:36:44-07:00
logf16d1603ab199b6e605c5e83f2edaebcf89d5512
treed398a622a3b63befba2b4dc7817c97fdb07d3f8b
parent42226fc1b77ba042e72fcdfb1f3c1973650a7cc3

spirv: fix type_map use-after-realloc issues


1 files changed, 19 insertions(+), 36 deletions(-)

src/codegen/spirv.zig+19-36
...@@ -947,9 +947,10 @@ pub const DeclGen = struct {...@@ -947,9 +947,10 @@ pub const DeclGen = struct {
947 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);947 return try self.resolveType(union_obj.enum_tag_ty.toType(), .indirect);
948 }948 }
949949
950 // TODO: We need to add the active field to the key.950 // TODO: We need to add the active field to the key, somehow.
951 // const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());951 if (maybe_active_field == null) {
952 // if (entry.found_existing) return entry.value_ptr.ty_ref;952 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
953 }
953954
954 var member_types: [4]CacheRef = undefined;955 var member_types: [4]CacheRef = undefined;
955 var member_names: [4]CacheString = undefined;956 var member_names: [4]CacheString = undefined;
...@@ -986,10 +987,9 @@ pub const DeclGen = struct {...@@ -986,10 +987,9 @@ pub const DeclGen = struct {
986 .member_names = member_names[0..layout.total_fields],987 .member_names = member_names[0..layout.total_fields],
987 } });988 } });
988989
989 // entry.value_ptr.* = .{990 if (maybe_active_field == null) {
990 // .ty_ref = ty_ref,991 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
991 // };992 }
992
993 return ty_ref;993 return ty_ref;
994 }994 }
995995
...@@ -1033,8 +1033,7 @@ pub const DeclGen = struct {...@@ -1033,8 +1033,7 @@ pub const DeclGen = struct {
1033 return try self.spv.resolve(.{ .float_type = .{ .bits = bits } });1033 return try self.spv.resolve(.{ .float_type = .{ .bits = bits } });
1034 },1034 },
1035 .Array => {1035 .Array => {
1036 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());1036 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1037 if (entry.found_existing) return entry.value_ptr.ty_ref;
10381037
1039 const elem_ty = ty.childType(mod);1038 const elem_ty = ty.childType(mod);
1040 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);1039 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
...@@ -1042,15 +1041,12 @@ pub const DeclGen = struct {...@@ -1042,15 +1041,12 @@ pub const DeclGen = struct {
1042 return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)});1041 return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)});
1043 };1042 };
1044 const ty_ref = try self.spv.arrayType(total_len, elem_ty_ref);1043 const ty_ref = try self.spv.arrayType(total_len, elem_ty_ref);
1045 entry.value_ptr.* = .{1044 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1046 .ty_ref = ty_ref,
1047 };
1048 return ty_ref;1045 return ty_ref;
1049 },1046 },
1050 .Fn => switch (repr) {1047 .Fn => switch (repr) {
1051 .direct => {1048 .direct => {
1052 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());1049 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1053 if (entry.found_existing) return entry.value_ptr.ty_ref;
10541050
1055 const fn_info = mod.typeToFunc(ty).?;1051 const fn_info = mod.typeToFunc(ty).?;
1056 // TODO: Put this somewhere in Sema.zig1052 // TODO: Put this somewhere in Sema.zig
...@@ -1069,10 +1065,7 @@ pub const DeclGen = struct {...@@ -1069,10 +1065,7 @@ pub const DeclGen = struct {
1069 .parameters = param_ty_refs,1065 .parameters = param_ty_refs,
1070 } });1066 } });
10711067
1072 entry.value_ptr.* = .{1068 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1073 .ty_ref = ty_ref,
1074 };
1075
1076 return ty_ref;1069 return ty_ref;
1077 },1070 },
1078 .indirect => {1071 .indirect => {
...@@ -1119,8 +1112,7 @@ pub const DeclGen = struct {...@@ -1119,8 +1112,7 @@ pub const DeclGen = struct {
1119 } });1112 } });
1120 },1113 },
1121 .Struct => {1114 .Struct => {
1122 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());1115 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1123 if (entry.found_existing) return entry.value_ptr.ty_ref;
11241116
1125 const struct_type = switch (ip.indexToKey(ty.toIntern())) {1117 const struct_type = switch (ip.indexToKey(ty.toIntern())) {
1126 .anon_struct_type => |tuple| {1118 .anon_struct_type => |tuple| {
...@@ -1140,9 +1132,7 @@ pub const DeclGen = struct {...@@ -1140,9 +1132,7 @@ pub const DeclGen = struct {
1140 .member_types = member_types[0..member_index],1132 .member_types = member_types[0..member_index],
1141 } });1133 } });
11421134
1143 entry.value_ptr.* = .{1135 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1144 .ty_ref = ty_ref,
1145 };
1146 return ty_ref;1136 return ty_ref;
1147 },1137 },
1148 .struct_type => |struct_type| struct_type,1138 .struct_type => |struct_type| struct_type,
...@@ -1151,6 +1141,7 @@ pub const DeclGen = struct {...@@ -1151,6 +1141,7 @@ pub const DeclGen = struct {
11511141
1152 if (struct_type.layout == .Packed) {1142 if (struct_type.layout == .Packed) {
1153 return try self.resolveType(struct_type.backingIntType(ip).toType(), .direct);1143 return try self.resolveType(struct_type.backingIntType(ip).toType(), .direct);
1144 }
11541145
1155 var member_types = std.ArrayList(CacheRef).init(self.gpa);1146 var member_types = std.ArrayList(CacheRef).init(self.gpa);
1156 defer member_types.deinit();1147 defer member_types.deinit();
...@@ -1172,9 +1163,7 @@ pub const DeclGen = struct {...@@ -1172,9 +1163,7 @@ pub const DeclGen = struct {
1172 .member_names = member_names.items,1163 .member_names = member_names.items,
1173 } });1164 } });
11741165
1175 entry.value_ptr.* = .{1166 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1176 .ty_ref = ty_ref,
1177 };
1178 return ty_ref;1167 return ty_ref;
1179 },1168 },
1180 .Optional => {1169 .Optional => {
...@@ -1192,8 +1181,7 @@ pub const DeclGen = struct {...@@ -1192,8 +1181,7 @@ pub const DeclGen = struct {
1192 return payload_ty_ref;1181 return payload_ty_ref;
1193 }1182 }
11941183
1195 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());1184 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1196 if (entry.found_existing) return entry.value_ptr.ty_ref;
11971185
1198 const bool_ty_ref = try self.resolveType(Type.bool, .indirect);1186 const bool_ty_ref = try self.resolveType(Type.bool, .indirect);
11991187
...@@ -1205,9 +1193,7 @@ pub const DeclGen = struct {...@@ -1205,9 +1193,7 @@ pub const DeclGen = struct {
1205 },1193 },
1206 } });1194 } });
12071195
1208 entry.value_ptr.* = .{1196 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1209 .ty_ref = ty_ref,
1210 };
1211 return ty_ref;1197 return ty_ref;
1212 },1198 },
1213 .Union => return try self.resolveUnionType(ty, null),1199 .Union => return try self.resolveUnionType(ty, null),
...@@ -1221,8 +1207,7 @@ pub const DeclGen = struct {...@@ -1221,8 +1207,7 @@ pub const DeclGen = struct {
1221 return error_ty_ref;1207 return error_ty_ref;
1222 }1208 }
12231209
1224 const entry = try self.type_map.getOrPut(self.gpa, ty.toIntern());1210 if (self.type_map.get(ty.toIntern())) |info| return info.ty_ref;
1225 if (entry.found_existing) return entry.value_ptr.ty_ref;
12261211
1227 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);1212 const payload_ty_ref = try self.resolveType(payload_ty, .indirect);
12281213
...@@ -1252,9 +1237,7 @@ pub const DeclGen = struct {...@@ -1252,9 +1237,7 @@ pub const DeclGen = struct {
1252 .member_names = &member_names,1237 .member_names = &member_names,
1253 } });1238 } });
12541239
1255 entry.value_ptr.* = .{1240 try self.type_map.put(self.gpa, ty.toIntern(), .{ .ty_ref = ty_ref });
1256 .ty_ref = ty_ref,
1257 };
1258 return ty_ref;1241 return ty_ref;
1259 },1242 },
12601243