llvm.org GIT mirror llvm / ddfb999
bpf: fix bug on silently truncating 64-bit immediate We came across an llvm bug when compiling some testcases that 64-bit immediates are silently truncated into 32-bit and then packed into BPF_JMP | BPF_K encoding. This caused comparison with wrong value. This bug looks to be introduced by r308080 (llvm 5.0). The Select_Ri pattern is supposed to be lowered into J*_Ri while the latter only support 32-bit immediate encoding, therefore Select_Ri should have similar immediate predicate check as what J*_Ri are doing. The bug is fixed by git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315889 91177308-0d34-0410-b5e6-96231b3b80d8 in llvm 6.0. This patch is largely the same as the fix in llvm 6.0 except one minor adjustment for the test case. Reported-by: John Fastabend <john.fastabend@gmail.com> Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Reviewed-by: Yonghong Song <yhs@fb.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@319633 91177308-0d34-0410-b5e6-96231b3b80d8 Yonghong Song 1 year, 8 months ago
3 changed file(s) with 42 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
577577 .addReg(LHS)
578578 .addReg(MI.getOperand(2).getReg())
579579 .addMBB(Copy1MBB);
580 else
580 else {
581 int64_t imm32 = MI.getOperand(2).getImm();
582 // sanity check before we build J*_ri instruction.
583 assert (isInt<32>(imm32));
581584 BuildMI(BB, DL, TII.get(NewCC))
582585 .addReg(LHS)
583 .addImm(MI.getOperand(2).getImm())
586 .addImm(imm32)
584587 .addMBB(Copy1MBB);
588 }
585589
586590 // Copy0MBB:
587591 // %FalseValue = ...
463463 (ins GPR:$lhs, i64imm:$rhs, i64imm:$imm, GPR:$src, GPR:$src2),
464464 "# Select PSEUDO $dst = $lhs $imm $rhs ? $src : $src2",
465465 [(set i64:$dst,
466 (BPFselectcc i64:$lhs, (i64 imm:$rhs), (i64 imm:$imm), i64:$src, i64:$src2))]>;
466 (BPFselectcc i64:$lhs, (i64 i64immSExt32:$rhs), (i64 imm:$imm), i64:$src, i64:$src2))]>;
467467 }
468468
469469 // load 64-bit global addr into register
2424 }
2525
2626 attributes #0 = { norecurse nounwind readonly }
27
28 ; test immediate out of 32-bit range
29 ; Source file:
30
31 ; unsigned long long
32 ; load_word(void *buf, unsigned long long off)
33 ; asm("llvm.bpf.load.word");
34 ;
35 ; int
36 ; foo(void *buf)
37 ; {
38 ; unsigned long long sum = 0;
39 ;
40 ; sum += load_word(buf, 100);
41 ; sum += load_word(buf, 104);
42 ;
43 ; if (sum != 0x1ffffffffULL)
44 ; return ~0U;
45 ;
46 ; return 0;
47 ;}
48
49 ; Function Attrs: nounwind readonly
50 define i32 @foo(i8*) local_unnamed_addr #0 {
51 %2 = tail call i64 @llvm.bpf.load.word(i8* %0, i64 100)
52 %3 = tail call i64 @llvm.bpf.load.word(i8* %0, i64 104)
53 %4 = add i64 %3, %2
54 %5 = icmp ne i64 %4, 8589934591
55 ; CHECK: r{{[0-9]+}} = 8589934591ll
56 %6 = sext i1 %5 to i32
57 ret i32 %6
58 }
59
60 ; Function Attrs: nounwind readonly
61 declare i64 @llvm.bpf.load.word(i8*, i64) #1