Skip to content

Commit 2a070c7

Browse files
vtjnashjohanmon
authored andcommitted
codegen: fixing union isbits comparison UB (JuliaLang#41046)
Fixes JuliaLang#40612 Fixes JuliaLang#40834
1 parent 8af3b6b commit 2a070c7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/codegen.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2442,11 +2442,18 @@ static Value *emit_bitsunion_compare(jl_codectx_t &ctx, const jl_cgval_t &arg1,
24422442
{
24432443
assert(jl_egal(arg1.typ, arg2.typ) && arg1.TIndex && arg2.TIndex && jl_is_uniontype(arg1.typ) && "unimplemented");
24442444
Value *tindex = arg1.TIndex;
2445+
tindex = ctx.builder.CreateAnd(tindex, ConstantInt::get(T_int8, 0x7f));
2446+
Value *tindex2 = arg2.TIndex;
2447+
tindex2 = ctx.builder.CreateAnd(tindex2, ConstantInt::get(T_int8, 0x7f));
2448+
Value *typeeq = ctx.builder.CreateICmpEQ(tindex, tindex2);
2449+
tindex = ctx.builder.CreateSelect(typeeq, tindex, ConstantInt::get(T_int8, 0x00));
24452450
BasicBlock *defaultBB = BasicBlock::Create(jl_LLVMContext, "unionbits_is_boxed", ctx.f);
24462451
SwitchInst *switchInst = ctx.builder.CreateSwitch(tindex, defaultBB);
24472452
BasicBlock *postBB = BasicBlock::Create(jl_LLVMContext, "post_unionbits_is", ctx.f);
24482453
ctx.builder.SetInsertPoint(postBB);
24492454
PHINode *phi = ctx.builder.CreatePHI(T_int1, 2);
2455+
switchInst->addCase(ConstantInt::get(T_int8, 0), postBB);
2456+
phi->addIncoming(ConstantInt::get(T_int1, 0), switchInst->getParent());
24502457
unsigned counter = 0;
24512458
bool allunboxed = for_each_uniontype_small(
24522459
[&](unsigned idx, jl_datatype_t *jt) {
@@ -2470,7 +2477,7 @@ static Value *emit_bitsunion_compare(jl_codectx_t &ctx, const jl_cgval_t &arg1,
24702477
ctx.builder.CreateCall(trap_func);
24712478
ctx.builder.CreateUnreachable();
24722479
ctx.builder.SetInsertPoint(postBB);
2473-
return ctx.builder.CreateAnd(phi, ctx.builder.CreateICmpEQ(arg1.TIndex, arg2.TIndex));
2480+
return phi;
24742481
}
24752482

24762483
static Value *emit_bits_compare(jl_codectx_t &ctx, jl_cgval_t arg1, jl_cgval_t arg2)

test/compiler/codegen.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,13 @@ struct A40855
570570
end
571571
g() = string(A40855(X40855, 1))
572572
@test g() == "$(@__MODULE__).A40855($(@__MODULE__).X40855, 1)"
573+
574+
# issue #40612
575+
f40612(a, b) = a|b === a|b
576+
g40612(a, b) = a[]|a[] === b[]|b[]
577+
@test f40612(true, missing)
578+
@test !g40612(Union{Bool,Missing}[missing], Union{Bool,Missing}[true])
579+
@test !g40612(Union{Bool,Missing}[false], Union{Bool,Missing}[true])
580+
@test g40612(Union{Bool,Missing}[missing], Union{Bool,Missing}[missing])
581+
@test g40612(Union{Bool,Missing}[true], Union{Bool,Missing}[true])
582+
@test g40612(Union{Bool,Missing}[false], Union{Bool,Missing}[false])

0 commit comments

Comments
 (0)