Should UBSan detect 0 input to __builtin_clz/ctz regardless of target?

CodeGenFunction::EmitCheckedArgForBuiltin checks getTarget().isCLZForZeroUndef() so it will only detect errors on targets where __builtin_clz/ctz aren’t defined for zero.

gcc’s documentation for these builtins says 0 is undefined.

The original commit that added isCLZForZeroUndef, Make clz/ctz builtins defined for zero on ARM targets. rdar://10732455 · llvm/llvm-project@a7a61e2 · GitHub seemed to be trying to prevent optimizations where someone assumed __builtin_clz would return 32 on Arm. This pre-dates the UBSan code.

So my question is, should UBSan detect 0 on Arm since the behavior isn’t portable?

@vedantk @efriedma-quic @nikic

Yes, we should detect it. The commit doesn’t actually “define” the behavior, just changes the actual effect of undefined behavior. The definition comes from gcc; we can’t change it even if we wanted to.