From 8de276fb0f14059353e4aeb3af8ca14a889887ec Mon Sep 17 00:00:00 2001 From: Jani Hakala Date: Wed, 20 Nov 2019 21:37:03 +0200 Subject: [PATCH] Fix incorrect bignum allocation sizes Memory allocation problems were detected by AddressSanitizer provided by gcc 9.2.1. When lolevel-tests.scm was run, AddressSanitizer pointed out bignum1 and bignum2 related problems in library.scm and srfi-4.scm. In library.scm, C_bignum2 needs 4 words (header, sign and the two digit words), plus the 2 words for the bignum wrapper. In srfi-4.scm, the [su]32vector-ref functions might ultimately call C_bignum1, which needs 5 words. Signed-off-by: Evan Hanson --- c-platform.scm | 8 ++++++-- library.scm | 4 ++-- srfi-4.scm | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/c-platform.scm b/c-platform.scm index 61a4ac87..27fdb65d 100644 --- a/c-platform.scm +++ b/c-platform.scm @@ -78,6 +78,7 @@ (define default-units '(library eval)) (define words-per-flonum 4) +(define min-words-per-bignum 5) (eq-inline-operator "C_eqp") (membership-test-operators @@ -968,8 +969,11 @@ (rewrite 'chicken.memory#pointer-f32-set! 2 2 "C_u_i_pointer_f32_set" #f) (rewrite 'chicken.memory#pointer-f64-set! 2 2 "C_u_i_pointer_f64_set" #f) -(rewrite 'chicken.memory#pointer-u32-ref 16 1 "C_a_u_i_pointer_u32_ref" #f words-per-flonum) -(rewrite 'chicken.memory#pointer-s32-ref 16 1 "C_a_u_i_pointer_s32_ref" #f words-per-flonum) +;; on 32-bit platforms, 32-bit integers do not always fit in a word, +;; bignum1 and bignum wrapper (5 words) may be used instead +(rewrite 'chicken.memory#pointer-u32-ref 16 1 "C_a_u_i_pointer_u32_ref" #f min-words-per-bignum) +(rewrite 'chicken.memory#pointer-s32-ref 16 1 "C_a_u_i_pointer_s32_ref" #f min-words-per-bignum) + (rewrite 'chicken.memory#pointer-f32-ref 16 1 "C_a_u_i_pointer_f32_ref" #f words-per-flonum) (rewrite 'chicken.memory#pointer-f64-ref 16 1 "C_a_u_i_pointer_f64_ref" #f words-per-flonum) diff --git a/library.scm b/library.scm index e52d1452..7b8c577a 100644 --- a/library.scm +++ b/library.scm @@ -2468,7 +2468,7 @@ EOF (end (or hashes digits))) (and-let* ((end) (num (##core#inline_allocate - ("C_s_a_i_digits_to_integer" 5) + ("C_s_a_i_digits_to_integer" 6) str start (car end) radix neg?))) (when hashes ; Eeewww. Feeling dirty yet? (set! seen-hashes? #t) @@ -2482,7 +2482,7 @@ EOF (and-let* ((start (if sign (fx+ start 1) start)) (end (scan-digits start))) (cons (##core#inline_allocate - ("C_s_a_i_digits_to_integer" 5) + ("C_s_a_i_digits_to_integer" 6) str start (car end) radix (eq? sign 'neg)) (cdr end))))))) (scan-decimal-tail ; The part after the decimal dot diff --git a/srfi-4.scm b/srfi-4.scm index 6faaa475..537d8288 100644 --- a/srfi-4.scm +++ b/srfi-4.scm @@ -209,13 +209,13 @@ EOF (define u32vector-ref (getter-with-setter - (lambda (x i) (##core#inline_allocate ("C_a_i_u32vector_ref" 4) x i)) + (lambda (x i) (##core#inline_allocate ("C_a_i_u32vector_ref" 5) x i)) u32vector-set! "(chicken.srfi-4#u32vector-ref v i)")) (define s32vector-ref (getter-with-setter - (lambda (x i) (##core#inline_allocate ("C_a_i_s32vector_ref" 4) x i)) + (lambda (x i) (##core#inline_allocate ("C_a_i_s32vector_ref" 5) x i)) s32vector-set! "(chicken.srfi-4#s32vector-ref v i)")) -- 2.23.0