Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary support for segfault-based safepoints
- Loading branch information
Showing
6 changed files
with
141 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
#include <gc.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
// At the moment we rely on the conservative | ||
// mode of Boehm GC as our garbage collector. | ||
|
||
void scalanative_init() { | ||
GC_init(); | ||
} | ||
void scalanative_init() { GC_init(); } | ||
|
||
void* scalanative_alloc(void* info, size_t size) { | ||
void** alloc = (void**) GC_malloc(size); | ||
void *scalanative_alloc(void *info, size_t size) { | ||
void **alloc = (void **)GC_malloc(size); | ||
*alloc = info; | ||
return (void*) alloc; | ||
return (void *)alloc; | ||
} | ||
|
||
void* scalanative_alloc_raw(size_t size) { | ||
return GC_malloc(size); | ||
} | ||
void *scalanative_alloc_raw(size_t size) { return GC_malloc(size); } | ||
|
||
void* scalanative_alloc_raw_atomic(size_t size) { | ||
void *scalanative_alloc_raw_atomic(size_t size) { | ||
return GC_malloc_atomic(size); | ||
} | ||
|
||
void scalanative_collect() { | ||
GC_gcollect(); | ||
} | ||
void scalanative_collect() { GC_gcollect(); } | ||
|
||
void scalanative_safepoint() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <signal.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/mman.h> | ||
|
||
extern unsigned char scalanative_safepoint_trigger[4096] __attribute__((aligned(4096))); | ||
|
||
void scalanative_safepoint(); | ||
|
||
void scalanative_safepoint_on() { | ||
mprotect((void*) &scalanative_safepoint_trigger, 4096, PROT_NONE); | ||
} | ||
|
||
void scalanative_safepoint_off() { | ||
mprotect((void*) &scalanative_safepoint_trigger, 4096, PROT_READ); | ||
} | ||
|
||
void scalanative_safepoint_handler(int sig, siginfo_t *info, void *ucontext) { | ||
if (info->si_addr == (void*) &scalanative_safepoint_trigger) { | ||
scalanative_safepoint(); | ||
} else { | ||
printf("Segfault %d at %p", sig, info->si_addr); | ||
exit(1); | ||
} | ||
} | ||
|
||
void scalanative_safepoint_init() { | ||
struct sigaction action; | ||
action.sa_sigaction = scalanative_safepoint_handler; | ||
action.sa_flags = SA_RESTART | SA_SIGINFO; | ||
sigemptyset(&action.sa_mask); | ||
sigaction(SIGSEGV, &action, NULL); | ||
sigaction(SIGBUS, &action, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
tools/src/main/scala/scala/scalanative/optimizer/inject/SafepointTrigger.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package scala.scalanative.optimizer.inject | ||
|
||
import scala.collection.mutable | ||
import scala.scalanative.nir._ | ||
import scala.scalanative.optimizer.analysis.ClassHierarchy.Top | ||
import scala.scalanative.optimizer.{Inject, InjectCompanion} | ||
import scala.scalanative.tools.Config | ||
|
||
class SafepointTrigger(top: Top) extends Inject { | ||
import SafepointTrigger._ | ||
|
||
override def apply(buf: mutable.Buffer[Defn]) = | ||
buf += safepointTriggerDefn | ||
} | ||
|
||
object SafepointTrigger extends InjectCompanion { | ||
val safepointTriggerName = Global.Top("scalanative_safepoint_trigger") | ||
val safepointTriggerTy = Type.Array(Type.Byte, 4096) | ||
val safepointTriggerInit = Val.Zero(safepointTriggerTy) | ||
val safepointTriggerDefn = | ||
Defn.Var(Attrs(align = Some(4096)), | ||
safepointTriggerName, | ||
safepointTriggerTy, | ||
safepointTriggerInit) | ||
|
||
override def apply(config: Config, top: Top) = new SafepointTrigger(top) | ||
} |
50 changes: 50 additions & 0 deletions
50
tools/src/main/scala/scala/scalanative/optimizer/pass/SafepointInsertion.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package scala.scalanative | ||
package optimizer | ||
package pass | ||
|
||
import scala.collection.mutable | ||
import analysis.ClassHierarchy.Top | ||
import nir._ | ||
|
||
/** Inserts safepoint probes before every return. */ | ||
class SafepointInsertion(implicit fresh: Fresh) extends Pass { | ||
import SafepointInsertion._ | ||
|
||
override def onDefns(defns: Seq[Defn]): Seq[Defn] = { | ||
val buf = mutable.UnrolledBuffer.empty[Defn] | ||
|
||
defns.foreach { | ||
case defn: Defn.Define if defn.attrs.inline ne Attr.AlwaysInline => | ||
buf += defn.copy(insts = onInsts(defn.insts)) | ||
|
||
case defn => | ||
buf += defn | ||
} | ||
|
||
buf | ||
} | ||
|
||
override def onInsts(insts: Seq[Inst]): Seq[Inst] = { | ||
val buf = new nir.Buffer | ||
import buf._ | ||
|
||
insts.foreach { | ||
case inst: Inst.Ret => | ||
let(Op.Load(Type.Byte, safepointTriggerVal, isVolatile = true)) | ||
buf += inst | ||
|
||
case inst => | ||
buf += inst | ||
} | ||
|
||
buf.toSeq | ||
} | ||
} | ||
|
||
object SafepointInsertion extends PassCompanion { | ||
val safepointTriggerName = Global.Top("scalanative_safepoint_trigger") | ||
val safepointTriggerVal = Val.Global(safepointTriggerName, Type.Ptr) | ||
|
||
override def apply(config: tools.Config, top: Top) = | ||
new SafepointInsertion()(top.fresh) | ||
} |