mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-15 16:57:35 +02:00
Use alloca for any allocations with let/local-vars
This prevents situations where local variables are allocated within local scope blocks and then are assigned to pointers. This is necessary as those locals are not guaranteed to remain on the stack once the block ends, so the pointer can easily point to random memory, leading to GC corruption and/or undefined behavior.
This commit is contained in:
parent
4863f0d4d1
commit
4b0466f37b
1 changed files with 11 additions and 3 deletions
|
@ -652,11 +652,17 @@
|
|||
(and (> len 0)
|
||||
(equal? end (substring str (- len 1) len)))))
|
||||
|
||||
(define *use-alloca* #f)
|
||||
|
||||
(define (set-use-alloca! v)
|
||||
(set! *use-alloca* v))
|
||||
|
||||
;; Use alloca() for stack allocations?
|
||||
(define (alloca? ast-id)
|
||||
(let ((adbf:fnc (adb:get/default ast-id #f)))
|
||||
(and adbf:fnc
|
||||
(adbf:calls-self? adbf:fnc))))
|
||||
(or *use-alloca*
|
||||
(let ((adbf:fnc (adb:get/default ast-id #f)))
|
||||
(and adbf:fnc
|
||||
(adbf:calls-self? adbf:fnc)))))
|
||||
|
||||
;; c-compile-prim : prim-exp -> string -> string
|
||||
(define (c-compile-prim p cont ast-id)
|
||||
|
@ -1194,9 +1200,11 @@
|
|||
(body (caddr exp))
|
||||
(vexps (foldr
|
||||
(lambda (var/val acc)
|
||||
(set-use-alloca! #t) ;; Force alloca to ensure safe c stack allocs
|
||||
;; Join expressions; based on c:append
|
||||
(let ((cp1 (c-compile-exp (cadr var/val) append-preamble cont ast-id trace cps?))
|
||||
(cp2 acc))
|
||||
(set-use-alloca! #f) ;; Revert flag
|
||||
(c-code/vars
|
||||
(let ((cp1-body (c:body cp1)))
|
||||
(string-append cp1-body ";" (c:body cp2)))
|
||||
|
|
Loading…
Add table
Reference in a new issue