authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-06 19:31:07+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-06 19:31:07+02:00
log0268f54fcfbb8a1c9f258b0d6f9c2a87ab597531
treed70d196180b7738f192f15054b30ea750ebcac55
parent2ed1ed9b32ae588f6a8997248d69817b5d89a133
parentb45d2968e51cd22650b019f668056333337513e9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7313 from LemonBoy/booo

Fix a few unsound optimizations on single-element union/enum

3 files changed, 114 insertions(+), 20 deletions(-)

src/stage1/analyze.cpp+6-1
......@@ -6113,7 +6113,7 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
61136113 TypeUnionField *only_field = &union_type->data.unionation.fields[0];
61146114 ZigType *field_type = resolve_union_field_type(g, only_field);
61156115 assert(field_type);
6116 bigint_init_unsigned(&result->data.x_union.tag, 0);
6116 bigint_init_bigint(&result->data.x_union.tag, &only_field->enum_field->value);
61176117 result->data.x_union.payload = g->pass1_arena->create<ZigValue>();
61186118 copy_const_val(g, result->data.x_union.payload,
61196119 get_the_one_possible_value(g, field_type));
......@@ -6122,6 +6122,11 @@ ZigValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry) {
61226122 result->data.x_ptr.mut = ConstPtrMutComptimeConst;
61236123 result->data.x_ptr.special = ConstPtrSpecialRef;
61246124 result->data.x_ptr.data.ref.pointee = get_the_one_possible_value(g, result->type->data.pointer.child_type);
6125 } else if (result->type->id == ZigTypeIdEnum) {
6126 ZigType *enum_type = result->type;
6127 assert(enum_type->data.enumeration.src_field_count == 1);
6128 TypeEnumField *only_field = &result->type->data.enumeration.fields[0];
6129 bigint_init_bigint(&result->data.x_enum_tag, &only_field->value);
61256130 }
61266131 g->one_possible_values.put(type_entry, result);
61276132 return result;
src/stage1/ir.cpp+29-19
......@@ -14186,6 +14186,18 @@ static ZigType *ir_resolve_union_tag_type(IrAnalyze *ira, AstNode *source_node,
1418614186 }
1418714187}
1418814188
14189static bool can_fold_enum_type(ZigType *ty) {
14190 assert(ty->id == ZigTypeIdEnum);
14191 // We can fold the enum type (and avoid any check, be it at runtime or at
14192 // compile time) iff it has only a single element and its tag type is
14193 // zero-sized.
14194 ZigType *tag_int_type = ty->data.enumeration.tag_int_type;
14195 return ty->data.enumeration.layout == ContainerLayoutAuto &&
14196 ty->data.enumeration.src_field_count == 1 &&
14197 !ty->data.enumeration.non_exhaustive &&
14198 (tag_int_type->id == ZigTypeIdInt && tag_int_type->data.integral.bit_count == 0);
14199}
14200
1418914201static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, IrInstGen *target) {
1419014202 Error err;
1419114203
......@@ -14214,10 +14226,7 @@ static IrInstGen *ir_analyze_enum_to_int(IrAnalyze *ira, IrInst *source_instr, I
1421414226 assert(tag_type->id == ZigTypeIdInt || tag_type->id == ZigTypeIdComptimeInt);
1421514227
1421614228 // If there is only one possible tag, then we know at comptime what it is.
14217 if (enum_type->data.enumeration.layout == ContainerLayoutAuto &&
14218 enum_type->data.enumeration.src_field_count == 1 &&
14219 !enum_type->data.enumeration.non_exhaustive)
14220 {
14229 if (can_fold_enum_type(enum_type)) {
1422114230 IrInstGen *result = ir_const(ira, source_instr, tag_type);
1422214231 init_const_bigint(result->value, tag_type,
1422314232 &enum_type->data.enumeration.fields[0].value);
......@@ -14255,10 +14264,7 @@ static IrInstGen *ir_analyze_union_to_tag(IrAnalyze *ira, IrInst* source_instr,
1425514264 }
1425614265
1425714266 // If there is only 1 possible tag, then we know at comptime what it is.
14258 if (wanted_type->data.enumeration.layout == ContainerLayoutAuto &&
14259 wanted_type->data.enumeration.src_field_count == 1 &&
14260 !wanted_type->data.enumeration.non_exhaustive)
14261 {
14267 if (can_fold_enum_type(wanted_type)) {
1426214268 IrInstGen *result = ir_const(ira, source_instr, wanted_type);
1426314269 result->value->special = ConstValSpecialStatic;
1426414270 result->value->type = wanted_type;
......@@ -24039,7 +24045,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
2403924045 bigint_init_bigint(&result->value->data.x_enum_tag, &pointee_val->data.x_union.tag);
2404024046 return result;
2404124047 }
24042 if (tag_type->data.enumeration.src_field_count == 1 && !tag_type->data.enumeration.non_exhaustive) {
24048
24049 if (can_fold_enum_type(tag_type)) {
2404324050 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, tag_type);
2404424051 TypeEnumField *only_field = &tag_type->data.enumeration.fields[0];
2404524052 bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
......@@ -24054,7 +24061,8 @@ static IrInstGen *ir_analyze_instruction_switch_target(IrAnalyze *ira,
2405424061 case ZigTypeIdEnum: {
2405524062 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
2405624063 return ira->codegen->invalid_inst_gen;
24057 if (target_type->data.enumeration.src_field_count == 1 && !target_type->data.enumeration.non_exhaustive) {
24064
24065 if (can_fold_enum_type(target_type)) {
2405824066 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
2405924067 IrInstGen *result = ir_const(ira, &switch_target_instruction->base.base, target_type);
2406024068 bigint_init_bigint(&result->value->data.x_enum_tag, &only_field->value);
......@@ -24789,7 +24797,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2478924797 if (type_is_invalid(target->value->type))
2479024798 return ira->codegen->invalid_inst_gen;
2479124799
24792 if (target->value->type->id == ZigTypeIdEnumLiteral) {
24800 ZigType *target_type = target->value->type;
24801
24802 if (target_type->id == ZigTypeIdEnumLiteral) {
2479324803 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
2479424804 Buf *field_name = target->value->data.x_enum_literal;
2479524805 ZigValue *array_val = create_const_str_lit(ira->codegen, field_name)->data.x_ptr.data.ref.pointee;
......@@ -24797,21 +24807,21 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2479724807 return result;
2479824808 }
2479924809
24800 if (target->value->type->id == ZigTypeIdUnion) {
24810 if (target_type->id == ZigTypeIdUnion) {
2480124811 target = ir_analyze_union_tag(ira, &instruction->base.base, target, instruction->base.is_gen);
2480224812 if (type_is_invalid(target->value->type))
2480324813 return ira->codegen->invalid_inst_gen;
24814 target_type = target->value->type;
2480424815 }
2480524816
24806 if (target->value->type->id != ZigTypeIdEnum) {
24817 if (target_type->id != ZigTypeIdEnum) {
2480724818 ir_add_error(ira, &target->base,
24808 buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target->value->type->name)));
24819 buf_sprintf("expected enum tag, found '%s'", buf_ptr(&target_type->name)));
2480924820 return ira->codegen->invalid_inst_gen;
2481024821 }
2481124822
24812 if (target->value->type->data.enumeration.src_field_count == 1 &&
24813 !target->value->type->data.enumeration.non_exhaustive) {
24814 TypeEnumField *only_field = &target->value->type->data.enumeration.fields[0];
24823 if (can_fold_enum_type(target_type)) {
24824 TypeEnumField *only_field = &target_type->data.enumeration.fields[0];
2481524825 ZigValue *array_val = create_const_str_lit(ira->codegen, only_field->name)->data.x_ptr.data.ref.pointee;
2481624826 IrInstGen *result = ir_const(ira, &instruction->base.base, nullptr);
2481724827 init_const_slice(ira->codegen, result->value, array_val, 0, buf_len(only_field->name), true);
......@@ -24819,9 +24829,9 @@ static IrInstGen *ir_analyze_instruction_enum_tag_name(IrAnalyze *ira, IrInstSrc
2481924829 }
2482024830
2482124831 if (instr_is_comptime(target)) {
24822 if ((err = type_resolve(ira->codegen, target->value->type, ResolveStatusZeroBitsKnown)))
24832 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusZeroBitsKnown)))
2482324833 return ira->codegen->invalid_inst_gen;
24824 TypeEnumField *field = find_enum_field_by_tag(target->value->type, &target->value->data.x_bigint);
24834 TypeEnumField *field = find_enum_field_by_tag(target_type, &target->value->data.x_bigint);
2482524835 if (field == nullptr) {
2482624836 Buf *int_buf = buf_alloc();
2482724837 bigint_append_buf(int_buf, &target->value->data.x_bigint, 10);
test/runtime_safety.zig+79
......@@ -1,6 +1,85 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompareOutputContext) void {
4 {
5 const check_panic_msg =
6 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
7 \\ if (std.mem.eql(u8, message, "reached unreachable code")) {
8 \\ std.process.exit(126); // good
9 \\ }
10 \\ std.process.exit(0); // test failed
11 \\}
12 ;
13
14 cases.addRuntimeSafety("switch on corrupted enum value",
15 \\const std = @import("std");
16 ++ check_panic_msg ++
17 \\const E = enum(u32) {
18 \\ X = 1,
19 \\};
20 \\pub fn main() void {
21 \\ var e: E = undefined;
22 \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
23 \\ switch (e) {
24 \\ .X => @breakpoint(),
25 \\ }
26 \\}
27 );
28
29 cases.addRuntimeSafety("switch on corrupted union value",
30 \\const std = @import("std");
31 ++ check_panic_msg ++
32 \\const U = union(enum(u32)) {
33 \\ X: u8,
34 \\};
35 \\pub fn main() void {
36 \\ var u: U = undefined;
37 \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
38 \\ switch (u) {
39 \\ .X => @breakpoint(),
40 \\ }
41 \\}
42 );
43 }
44
45 {
46 const check_panic_msg =
47 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
48 \\ if (std.mem.eql(u8, message, "invalid enum value")) {
49 \\ std.process.exit(126); // good
50 \\ }
51 \\ std.process.exit(0); // test failed
52 \\}
53 ;
54
55 cases.addRuntimeSafety("@tagName on corrupted enum value",
56 \\const std = @import("std");
57 ++ check_panic_msg ++
58 \\const E = enum(u32) {
59 \\ X = 1,
60 \\};
61 \\pub fn main() void {
62 \\ var e: E = undefined;
63 \\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
64 \\ var n = @tagName(e);
65 \\}
66 );
67
68 cases.addRuntimeSafety("@tagName on corrupted union value",
69 \\const std = @import("std");
70 ++ check_panic_msg ++
71 \\const U = union(enum(u32)) {
72 \\ X: u8,
73 \\};
74 \\pub fn main() void {
75 \\ var u: U = undefined;
76 \\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
77 \\ var t: @TagType(U) = u;
78 \\ var n = @tagName(t);
79 \\}
80 );
81 }
82
483 {
584 const check_panic_msg =
685 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {