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

Matt Johnson johnso87 at crhc.illinois.edu
Thu Mar 10 11:31:31 PST 2011


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 :)

     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



More information about the llvm-dev mailing list