on master help sign in
Merge pull request #2138 from melted/gc

Fix crashes when garbage collecting bigints

commit222612ad09ef853588763ff74a6d337c52d50eb5 authorEdwin Brady <edwin.brady@gmail.com> dateTue 14 Apr 2015, 19:36 (12 years ago) parent09a0a8d610387c
8 files changed+47−2
idris.cabal+4 rts/idris_gc.c+7 rts/idris_rts.c+11−1 rts/idris_rts.h+2−1 test/README+1 test/bignum001/bignum001.idr+17 test/bignum001/expected+1 test/bignum001/run+4
idris.cabal+4
@@ -346,6 +346,10 @@ Extra-source-files:
346346 test/basic013/*.idr
347347 test/basic013/expected
348348
349+ test/bignum001/run
350+ test/bignum001/*.idr
351+ test/bignum001/expected
352+
349353 test/delab001/*.idr
350354 test/delab001/run
351355 test/delab001/expected
rts/idris_gc.c+7
@@ -53,6 +53,13 @@ VAL copy(VM* vm, VAL x) {
5353 break;
5454 case FWD:
5555 return x->info.ptr;
56+ case RAWDATA:
57+ {
58+ size_t size = x->info.size + sizeof(Closure);
59+ cl = allocate(size, 0);
60+ memcpy(cl, x, size);
61+ }
62+ break;
5663 default:
5764 break;
5865 }
rts/idris_rts.c+11−1
@@ -143,7 +143,10 @@ int space(VM* vm, size_t size) {
143143}
144144
145145void* idris_alloc(size_t size) {
146− return allocate(size, 0);
146+ Closure* cl = (Closure*) allocate(sizeof(Closure)+size, 0);
147+ SETTY(cl, RAWDATA);
148+ cl->info.size = size;
149+ return (void*)cl+sizeof(Closure);
147150}
148151
149152void* idris_realloc(void* old, size_t old_size, size_t size) {
@@ -712,6 +715,13 @@ VAL doCopyTo(VM* vm, VAL x) {
712715 case BITS64:
713716 cl = idris_b64CopyForGC(vm, x);
714717 break;
718+ case RAWDATA:
719+ {
720+ size_t size = x->info.size + sizeof(Closure);
721+ cl = allocate(size, 0);
722+ memcpy(cl, x, size);
723+ }
724+ break;
715725 default:
716726 assert(0); // We're in trouble if this happens...
717727 }
rts/idris_rts.h+2−1
@@ -26,7 +26,7 @@
2626typedef enum {
2727 CON, INT, BIGINT, FLOAT, STRING, STROFFSET,
2828 BITS8, BITS16, BITS32, BITS64, UNIT, PTR, FWD,
29− MANAGEDPTR
29+ MANAGEDPTR, RAWDATA
3030} ClosureType;
3131
3232typedef struct Closure *VAL;
@@ -76,6 +76,7 @@ typedef struct Closure {
7676 __m128i* bits128p;
7777 Buffer* buf;
7878 ManagedPtr* mptr;
79+ size_t size;
7980 } info;
8081} Closure;
8182
test/README+1
@@ -1,6 +1,7 @@
11Tests are categorised as follows:
22
33basic: Basic language features, some complete programs
4+bignum: Bignums and GMP
45dsl: Embedded DSLs and features to support DSL development
56effects: Effects package
67error: Error messages and error reflection
test/bignum001/bignum001.idrnew file+17
@@ -0,0 +1,17 @@
1+module Main
2+
3+fac : Integer -> Integer
4+fac 0 = 1
5+fac n = n*fac(n-1)
6+
7+binomial : Integer -> Integer -> Integer
8+binomial n k = divBigInt (fac n) ((fac k)*(fac (n-k)))
9+
10+sumBinomials : Integer -> Integer
11+sumBinomials n = foldr (+) 0 (map (\k=>binomial n k) [0..n])
12+
13+main : IO ()
14+main = do
15+ if sumBinomials 1000 - pow 2 1000 == 0
16+ then putStrLn "OK"
17+ else putStrLn "ERROR"
test/bignum001/expectednew file+1
@@ -0,0 +1 @@
1+OK
test/bignum001/runnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env bash
2+idris $@ bignum001.idr -o bignum001
3+./bignum001
4+rm -f bignum001 *.ibc