[LLVMdev] compiler-rt: Infinite loop/stack overflow in __modsi3()

Chris Lattner clattner at apple.com
Thu Mar 10 14:16:00 PST 2011


On Mar 10, 2011, at 11:31 AM, Matt Johnson wrote:

> Hi All,
>     The default implementation of __modsi3() (signed integer modulus) 
> in compiler-rt/lib/modsi3.c is defined recursively.  Thankfully, LLVM is 
> smart enough to do tail call elimination on the recursion, so I got an 
> infinite loop rather than a stack overflow :)

Looks good, applied in r127429, thanks!

-Chris

> 
>     Here's the patch, patterned after the correct implementation in 
> umodsi3.c:
> 
> diff --git a/lib/compiler-rt/lib/modsi3.c b/lib/compiler-rt/lib/modsi3.c
> index 388418a..3759ce0 100644
> --- a/lib/compiler-rt/lib/modsi3.c
> +++ b/lib/compiler-rt/lib/modsi3.c
> @@ -16,8 +16,10 @@
> 
>  /* Returns: a % b */
> 
> +su_int __divsi3(si_int a, si_int b);
> +
>  si_int
>  __modsi3(si_int a, si_int b)
>  {
> -    return a - (a / b) * b;
> +    return a - __divsi3(a, b) * b;
>  }
> 
> 
> Best,
> Matt
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list