[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] [PATCH] Fix an off-by-one size calculation error in co

From: Peter Bex
Subject: [Chicken-hackers] [PATCH] Fix an off-by-one size calculation error in compiler
Date: Sat, 23 May 2015 16:15:14 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

Hi all,

While working on the numbers-integration branch, I stumbled across a bug
that's been in CHICKEN for a while: when compiling something that involves
a call to (list ...), it will generate a call to the inline allocating C
function "C_a_i_list" with the calculated size of the number of items
times three.  Unfortunately, this is one word too short, as can be seen
from the following definition from chicken.h:

#define C_SIZEOF_LIST(n)          ((n) * 3 + 1)

Because it's "only" an off-by-one, it rarely causes issues (though it may
explain a few mysterious bugs).

A good example is the locative-stress-test:

If you look at the generated C code, it will have generated code to
allocate 90 words for storing a list of 10 items which are the result
of conversion from the C "long" type.  In the chicken-4 and master
branches, this will look like this:

/* k408 in loop in k475 in k383 in k303 in k300 in k297 in k294 */
static void C_ccall f_410(C_word c,C_word t0,C_word t1){
C_word tmp;
C_word t2;
C_word t3;
C_word t4;
C_word ab[70],*a=ab;
*)C_data_pointer(((C_word*)t0)[2]))),C_long_to_num(&a,*((long *)C_d
((long *)C_data_pointer(((C_word*)t0)[5]))),C_long_to_num(&a,*((long 
C_trace("locative-stress-test.scm:50: loop");
C_trace("locative-stress-test.scm:49: error");

If you check the &a's, you'll see it only passes it to C_stack_probe
(which does not allocate), to C_a_i_list (which will use 31 words:
1 for the NIL at the end and 3 slots for each pair containing one
number) and to the 10 calls to C_long_to_num (which use up 4 words each,
as determined in the "long" part in estimate-foreign-result-size in

This gives us 31+40*1 = 71 words.  As you can see, it misses one word.
Usually this doesn't cause trouble, but it may trigger a bug when the
stack is lined up *just* so.  Attached are patches for the
numbers-scratchspace, chicken-5 and master branches.


Attachment: 0001-Fix-size-calculation-for-generated-code-for-list-CHICKEN-5.patch
Description: Text Data

Attachment: 0001-Fix-size-calculation-for-generated-code-for-list-MASTER.patch
Description: Text Data

Attachment: 0001-Fix-size-calculation-for-generated-code-for-list-NUMBERS-INTEGRATION-SCRATCHSPACE.patch
Description: Text Data

Attachment: signature.asc
Description: Digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]