authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-27 16:11:07-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-07-27 16:11:07-07:00
logdfc7493dcb049788b92137ca09b8bd47cee23865
treedfd6c4da8bc1ff71e0ecc8a1d3a1ac29c433bc64
parent90f23e131eadae427c4253fb658002633263b82e
parent793db63746ca044927743ee94405dad2a3f02fb8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12256 from Vexu/stage2

stage2 typeInfo UAF fix + more

5 files changed, 49 insertions(+), 10 deletions(-)

src/AstGen.zig+5-1
...@@ -751,6 +751,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr...@@ -751,6 +751,7 @@ fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) InnerEr
751 },751 },
752752
753 .unreachable_literal => {753 .unreachable_literal => {
754 try emitDbgNode(gz, node);
754 _ = try gz.addAsIndex(.{755 _ = try gz.addAsIndex(.{
755 .tag = .@"unreachable",756 .tag = .@"unreachable",
756 .data = .{ .@"unreachable" = .{757 .data = .{ .@"unreachable" = .{
...@@ -7443,7 +7444,6 @@ fn builtinCall(...@@ -7443,7 +7444,6 @@ fn builtinCall(
7443 .bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),7444 .bool_to_int => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .bool_to_int),
7444 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),7445 .embed_file => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], .embed_file),
7445 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),7446 .error_name => return simpleUnOp(gz, scope, rl, node, .{ .ty = .anyerror_type }, params[0], .error_name),
7446 .panic => return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic),
7447 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),7447 .set_cold => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_cold),
7448 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),7448 .set_runtime_safety => return simpleUnOp(gz, scope, rl, node, bool_rl, params[0], .set_runtime_safety),
7449 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),7449 .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt),
...@@ -7476,6 +7476,10 @@ fn builtinCall(...@@ -7476,6 +7476,10 @@ fn builtinCall(
7476 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),7476 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
7477 // zig fmt: on7477 // zig fmt: on
74787478
7479 .panic => {
7480 try emitDbgNode(gz, node);
7481 return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic);
7482 },
7479 .error_to_int => {7483 .error_to_int => {
7480 const operand = try expr(gz, scope, .none, params[0]);7484 const operand = try expr(gz, scope, .none, params[0]);
7481 const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{7485 const result = try gz.addExtendedPayload(.error_to_int, Zir.Inst.UnNode{
src/Module.zig+10
...@@ -1220,6 +1220,7 @@ pub const Union = struct {...@@ -1220,6 +1220,7 @@ pub const Union = struct {
1220 };1220 };
1221 const node = owner_decl.relativeToNodeIndex(u.node_offset);1221 const node = owner_decl.relativeToNodeIndex(u.node_offset);
1222 const node_tags = tree.nodes.items(.tag);1222 const node_tags = tree.nodes.items(.tag);
1223 var buf: [2]Ast.Node.Index = undefined;
1223 switch (node_tags[node]) {1224 switch (node_tags[node]) {
1224 .container_decl,1225 .container_decl,
1225 .container_decl_trailing,1226 .container_decl_trailing,
...@@ -1231,6 +1232,15 @@ pub const Union = struct {...@@ -1231,6 +1232,15 @@ pub const Union = struct {
1231 .container_decl_arg,1232 .container_decl_arg,
1232 .container_decl_arg_trailing,1233 .container_decl_arg_trailing,
1233 => return queryFieldSrc(tree.*, query, file, tree.containerDeclArg(node)),1234 => return queryFieldSrc(tree.*, query, file, tree.containerDeclArg(node)),
1235 .tagged_union,
1236 .tagged_union_trailing,
1237 => return queryFieldSrc(tree.*, query, file, tree.taggedUnion(node)),
1238 .tagged_union_two,
1239 .tagged_union_two_trailing,
1240 => return queryFieldSrc(tree.*, query, file, tree.taggedUnionTwo(&buf, node)),
1241 .tagged_union_enum_tag,
1242 .tagged_union_enum_tag_trailing,
1243 => return queryFieldSrc(tree.*, query, file, tree.taggedUnionEnumTag(node)),
1234 else => unreachable,1244 else => unreachable,
1235 }1245 }
1236 }1246 }
src/Sema.zig+16-6
...@@ -5695,6 +5695,7 @@ fn analyzeCall(...@@ -5695,6 +5695,7 @@ fn analyzeCall(
5695 sema.inst_map.clearRetainingCapacity();5695 sema.inst_map.clearRetainingCapacity();
5696 const decl = sema.mod.declPtr(block.src_decl);5696 const decl = sema.mod.declPtr(block.src_decl);
5697 child_block.src_decl = block.src_decl;5697 child_block.src_decl = block.src_decl;
5698 arg_i = 0;
5698 try sema.analyzeInlineCallArg(5699 try sema.analyzeInlineCallArg(
5699 block,5700 block,
5700 &child_block,5701 &child_block,
...@@ -12864,7 +12865,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -12864,7 +12865,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
12864 else12865 else
12865 try Value.Tag.opt_payload.create(12866 try Value.Tag.opt_payload.create(
12866 params_anon_decl.arena(),12867 params_anon_decl.arena(),
12867 try Value.Tag.ty.create(params_anon_decl.arena(), param_ty),12868 try Value.Tag.ty.create(params_anon_decl.arena(), try param_ty.copy(params_anon_decl.arena())),
12868 );12869 );
1286912870
12870 const param_fields = try params_anon_decl.arena().create([3]Value);12871 const param_fields = try params_anon_decl.arena().create([3]Value);
...@@ -15441,8 +15442,17 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -15441,8 +15442,17 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
15441 const is_allowzero_val = struct_val[6];15442 const is_allowzero_val = struct_val[6];
15442 const sentinel_val = struct_val[7];15443 const sentinel_val = struct_val[7];
1544315444
15445 const abi_align = @intCast(u29, alignment_val.toUnsignedInt(target)); // TODO: Validate this value.
15446
15444 var buffer: Value.ToTypeBuffer = undefined;15447 var buffer: Value.ToTypeBuffer = undefined;
15445 const child_ty = child_val.toType(&buffer);15448 const unresolved_elem_ty = child_val.toType(&buffer);
15449 const elem_ty = if (abi_align == 0)
15450 unresolved_elem_ty
15451 else t: {
15452 const elem_ty = try sema.resolveTypeFields(block, src, unresolved_elem_ty);
15453 try sema.resolveTypeLayout(block, src, elem_ty);
15454 break :t elem_ty;
15455 };
1544615456
15447 const ptr_size = size_val.toEnum(std.builtin.Type.Pointer.Size);15457 const ptr_size = size_val.toEnum(std.builtin.Type.Pointer.Size);
1544815458
...@@ -15454,7 +15464,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -15454,7 +15464,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
15454 const sentinel_ptr_val = sentinel_val.castTag(.opt_payload).?.data;15464 const sentinel_ptr_val = sentinel_val.castTag(.opt_payload).?.data;
15455 const ptr_ty = try Type.ptr(sema.arena, mod, .{15465 const ptr_ty = try Type.ptr(sema.arena, mod, .{
15456 .@"addrspace" = .generic,15466 .@"addrspace" = .generic,
15457 .pointee_type = child_ty,15467 .pointee_type = try elem_ty.copy(sema.arena),
15458 });15468 });
15459 actual_sentinel = (try sema.pointerDeref(block, src, sentinel_ptr_val, ptr_ty)).?;15469 actual_sentinel = (try sema.pointerDeref(block, src, sentinel_ptr_val, ptr_ty)).?;
15460 }15470 }
...@@ -15463,9 +15473,9 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -15463,9 +15473,9 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
15463 .size = ptr_size,15473 .size = ptr_size,
15464 .mutable = !is_const_val.toBool(),15474 .mutable = !is_const_val.toBool(),
15465 .@"volatile" = is_volatile_val.toBool(),15475 .@"volatile" = is_volatile_val.toBool(),
15466 .@"align" = @intCast(u29, alignment_val.toUnsignedInt(target)), // TODO: Validate this value.15476 .@"align" = abi_align,
15467 .@"addrspace" = address_space_val.toEnum(std.builtin.AddressSpace),15477 .@"addrspace" = address_space_val.toEnum(std.builtin.AddressSpace),
15468 .pointee_type = try child_ty.copy(sema.arena),15478 .pointee_type = try elem_ty.copy(sema.arena),
15469 .@"allowzero" = is_allowzero_val.toBool(),15479 .@"allowzero" = is_allowzero_val.toBool(),
15470 .sentinel = actual_sentinel,15480 .sentinel = actual_sentinel,
15471 });15481 });
...@@ -26626,7 +26636,7 @@ fn getBuiltinType(...@@ -26626,7 +26636,7 @@ fn getBuiltinType(
26626) CompileError!Type {26636) CompileError!Type {
26627 const ty_inst = try sema.getBuiltin(block, src, name);26637 const ty_inst = try sema.getBuiltin(block, src, name);
26628 const result_ty = try sema.analyzeAsType(block, src, ty_inst);26638 const result_ty = try sema.analyzeAsType(block, src, ty_inst);
26629 try sema.queueFullTypeResolution(result_ty);26639 try sema.resolveTypeFully(block, src, result_ty); // Should not fail
26630 return result_ty;26640 return result_ty;
26631}26641}
2663226642
test/cases/fn_typeinfo_passed_to_comptime_fn.zig created+17
...@@ -0,0 +1,17 @@
1const std = @import("std");
2
3test {
4 try foo(@typeInfo(@TypeOf(someFn)));
5}
6
7fn someFn(arg: ?*c_int) f64 {
8 _ = arg;
9 return 8;
10}
11fn foo(comptime info: std.builtin.Type) !void {
12 try std.testing.expect(info.Fn.args[0].arg_type.? == ?*c_int);
13}
14
15// run
16// is_test=1
17//
test/standalone.zig+1-3
...@@ -60,9 +60,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {...@@ -60,9 +60,7 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
60 }60 }
61 // Try to build and run a PIE executable.61 // Try to build and run a PIE executable.
62 if (builtin.os.tag == .linux) {62 if (builtin.os.tag == .linux) {
63 if (builtin.zig_backend == .stage1) { // https://github.com/ziglang/zig/issues/1222363 cases.addBuildFile("test/standalone/pie/build.zig", .{});
64 cases.addBuildFile("test/standalone/pie/build.zig", .{});
65 }
66 }64 }
6765
68 // Ensure the development tools are buildable.66 // Ensure the development tools are buildable.