github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/gosecu/api.go (about)

     1  package gosecu
     2  
     3  import (
     4  	"fmt"
     5  	"gosecommon"
     6  	"reflect"
     7  	"runtime"
     8  	"unsafe"
     9  )
    10  
    11  // Slice of gosecure targets.
    12  var (
    13  	secureMap map[string]func(size int32, argp *uint8)
    14  )
    15  
    16  func freeServer() {
    17  	for {
    18  		addr := <-runtime.Cooprt.Uach
    19  		runtime.UnsafeAllocator.FreeTracker(addr)
    20  	}
    21  }
    22  
    23  // We cannot use reflect to get the value of the arguments. Instead, we give
    24  // a pointer to a buffer allocated inside the ecall attribute and use it to pass
    25  // the arguments from the stack frame.
    26  func privateServer(c chan runtime.EcallReq) {
    27  	success := 0
    28  	for {
    29  		call := <-c
    30  		if fn := secureMap[call.Name]; fn != nil {
    31  			success++
    32  			go fn(call.Siz, call.Argp)
    33  		} else {
    34  			panic("gosecu: illegal gosecure call.")
    35  		}
    36  	}
    37  	fmt.Println("Closing the privateServer ", success)
    38  	panic("Closing the shit")
    39  }
    40  
    41  // EcallServer keeps polling the Cooprt.Ecall queue for incoming private ecall
    42  // server requests.
    43  func EcallServer() {
    44  	// Init the cross domain ref pointer for crossed routines.
    45  	//runtime.InitAllcg()
    46  	go freeServer()
    47  	for {
    48  		req := <-runtime.Cooprt.EcallSrv
    49  		if req == nil || req.PrivChan == nil {
    50  			panic("[EcallServer] nil value received, probably stack shrink")
    51  			continue
    52  		}
    53  
    54  		go privateServer(req.PrivChan)
    55  	}
    56  }
    57  
    58  // RegisterSecureFunction is called automatically at the begining of the enclave
    59  // execution, and registers all the functions that are a target of the gosecure
    60  // keyword.
    61  func RegisterSecureFunction(f interface{}) {
    62  	if secureMap == nil {
    63  		secureMap = make(map[string]func(size int32, argp *uint8))
    64  		runtime.SetCopiers(gosecommon.DeepCopier, gosecommon.DeepCopierSend, gosecommon.CanShallowCopy)
    65  	}
    66  
    67  	ptr := reflect.ValueOf(f).Pointer()
    68  	pc := runtime.FuncForPC(ptr)
    69  	if pc == nil {
    70  		//log.Fatalln("Unable to register secure function.")
    71  		panic("Unable to register secure function.")
    72  	}
    73  
    74  	//TODO @aghosn that will not be enough probably. Should have a pointer instead?
    75  	// or copy memory in a buffer inside the anonymous function?
    76  	secureMap[pc.Name()] = func(size int32, argp *uint8) {
    77  		// TODO deep copy the stack frame
    78  		if size == 0 {
    79  			runtime.Newproc(ptr, argp, size)
    80  			return
    81  		}
    82  		sl := gosecommon.DeepCopyStackFrame(size, argp, reflect.ValueOf(f).Type())
    83  		argpcpy := (*uint8)(unsafe.Pointer(&sl[0]))
    84  		runtime.Newproc(ptr, argpcpy, size)
    85  	}
    86  }