提交 62217e09 编写于 作者: J jack.wxz 提交者: jia zhang

rune/libenclave: Use unified pal api interface

Use unified API symbols starting with 'pal_'.
Signed-off-by: Njack.wxz <wangxiaozhe@linux.alibaba.com>
Signed-off-by: NTianjia Zhang <tianjia.zhang@linux.alibaba.com>
上级 003ec8c8
...@@ -28,16 +28,14 @@ struct pal_stdio_fds { ...@@ -28,16 +28,14 @@ struct pal_stdio_fds {
int stdin, stdout, stderr; int stdin, stdout, stderr;
}; };
int *pal_version;
int (*fptr_pal_get_version)(void);
int (*fptr_pal_init)(const struct pal_attr_t *attr); int (*fptr_pal_init)(const struct pal_attr_t *attr);
int (*fptr_pal_exec)(const char *path, const char * const argv[], int (*fptr_pal_exec)(const char *path, const char * const argv[],
const struct pal_stdio_fds *stdio, int *exit_code); const struct pal_stdio_fds *stdio, int *exit_code);
int (*fptr_pal_kill)(int sig, int pid); int (*fptr_pal_kill)(int sig, int pid);
int (*fptr_pal_destroy)(void); int (*fptr_pal_destroy)(void);
#define PAL_SO_PREFIX "liberpal-"
#define PAL_SO_SUFFIX ".so"
int is_enclave(void) int is_enclave(void)
{ {
const char *env; const char *env;
...@@ -49,8 +47,7 @@ int is_enclave(void) ...@@ -49,8 +47,7 @@ int is_enclave(void)
int load_enclave_runtime(void) int load_enclave_runtime(void)
{ {
const char *file, *basename, *suffix, *name; const char *file;
int namelen;
const char *rootfs; const char *rootfs;
void *dl; void *dl;
...@@ -61,24 +58,6 @@ int load_enclave_runtime(void) ...@@ -61,24 +58,6 @@ int load_enclave_runtime(void)
} }
write_log(DEBUG, "_LIBCONTAINER_PAL_PATH = %s", file); write_log(DEBUG, "_LIBCONTAINER_PAL_PATH = %s", file);
/* fetch basename */
basename = strrchr(file, '/');
if (basename)
basename += 1; /* skip '/' */
else
basename = file;
/* check prefix and suffix */
if (strncmp(basename, PAL_SO_PREFIX, sizeof(PAL_SO_PREFIX) - 1) != 0)
return -ESRCH;
suffix = basename + strlen(basename) - sizeof(PAL_SO_SUFFIX) + 1;
if (strncmp(suffix, PAL_SO_SUFFIX, sizeof(PAL_SO_SUFFIX) - 1) != 0)
return -ESRCH;
/* pal name */
name = basename + sizeof(PAL_SO_PREFIX) - 1;
namelen = strlen(name) - sizeof(PAL_SO_SUFFIX) + 1;
/* dlopen */ /* dlopen */
rootfs = getenv("_LIBCONTAINER_PAL_ROOTFS"); rootfs = getenv("_LIBCONTAINER_PAL_ROOTFS");
if (rootfs && *rootfs != '\0') { if (rootfs && *rootfs != '\0') {
...@@ -86,7 +65,7 @@ int load_enclave_runtime(void) ...@@ -86,7 +65,7 @@ int load_enclave_runtime(void)
char ldpath[BUFSIZ]; char ldpath[BUFSIZ];
const char *env_ldpath; const char *env_ldpath;
if (basename == file) { if (*file != '/') {
write_log(DEBUG, "_LIBCONTAINER_PAL_PATH must be a absolute path"); write_log(DEBUG, "_LIBCONTAINER_PAL_PATH must be a absolute path");
return -ENOSPC; return -ENOSPC;
} }
...@@ -116,17 +95,13 @@ int load_enclave_runtime(void) ...@@ -116,17 +95,13 @@ int load_enclave_runtime(void)
return -ENOEXEC; return -ENOEXEC;
} }
pal_version = dlsym(dl, "pal_version");
write_log(DEBUG, "dlsym(%s) = %p", "pal_version", pal_version);
#define DLSYM(fn) \ #define DLSYM(fn) \
do { \ do { \
char fname[64]; \ fptr_pal_ ## fn = dlsym(dl, "pal_" #fn); \
snprintf(fname, sizeof(fname), "%.*s_pal_%s", namelen, name, #fn); \ write_log(DEBUG, "dlsym(%s) = %p", "pal_" #fn, fptr_pal_ ## fn); \
fptr_pal_ ## fn = dlsym(dl, fname); \
write_log(DEBUG, "dlsym(%s) = %p", fname, fptr_pal_ ## fn); \
} while (0) } while (0)
DLSYM(get_version);
DLSYM(init); DLSYM(init);
DLSYM(exec); DLSYM(exec);
DLSYM(kill); DLSYM(kill);
......
...@@ -17,7 +17,7 @@ struct pal_stdio_fds { ...@@ -17,7 +17,7 @@ struct pal_stdio_fds {
int stdin, stdout, stderr; int stdin, stdout, stderr;
}; };
extern int *pal_version; extern int (*fptr_pal_get_version)(void);
extern int (*fptr_pal_init)(const struct pal_attr_t *attr); extern int (*fptr_pal_init)(const struct pal_attr_t *attr);
extern int (*fptr_pal_exec)(const char *path, const char * const argv[], extern int (*fptr_pal_exec)(const char *path, const char * const argv[],
const struct pal_stdio_fds *stdio, int *exit_code); const struct pal_stdio_fds *stdio, int *exit_code);
...@@ -31,7 +31,7 @@ import ( ...@@ -31,7 +31,7 @@ import (
) )
func SymAddrPalVersion() unsafe.Pointer { func SymAddrPalVersion() unsafe.Pointer {
return unsafe.Pointer(C.pal_version) return unsafe.Pointer(C.fptr_pal_get_version)
} }
func SymAddrPalInit() unsafe.Pointer { func SymAddrPalInit() unsafe.Pointer {
......
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
) )
type EnclaveRuntime interface { type EnclaveRuntime interface {
Name() string
Load(path string) error Load(path string) error
Init(args string, logLevel string) error Init(args string, logLevel string) error
Attest() error Attest() error
...@@ -43,9 +42,8 @@ func StartInitialization(config *configs.InitEnclaveConfig, logLevel string) (*E ...@@ -43,9 +42,8 @@ func StartInitialization(config *configs.InitEnclaveConfig, logLevel string) (*E
if err != nil { if err != nil {
return nil, err return nil, err
} }
name := runtime.Name()
logrus.Infof("Initializing enclave runtime %s", name) logrus.Infof("Initializing enclave runtime")
err = runtime.Init(config.Args, logLevel) err = runtime.Init(config.Args, logLevel)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -58,13 +56,13 @@ func StartInitialization(config *configs.InitEnclaveConfig, logLevel string) (*E ...@@ -58,13 +56,13 @@ func StartInitialization(config *configs.InitEnclaveConfig, logLevel string) (*E
} }
func (rt *EnclaveRuntimeWrapper) LaunchAttestation() error { func (rt *EnclaveRuntimeWrapper) LaunchAttestation() error {
logrus.Debugf("attesting enclave runtime %s", rt.runtime.Name()) logrus.Debugf("attesting enclave runtime")
return rt.runtime.Attest() return rt.runtime.Attest()
} }
func (rt *EnclaveRuntimeWrapper) ExecutePayload(cmd []string, envp []string, stdio [3]*os.File) (int32, error) { func (rt *EnclaveRuntimeWrapper) ExecutePayload(cmd []string, envp []string, stdio [3]*os.File) (int32, error) {
logrus.Debugf("enclave runtime %s executing payload with commandline %s", rt.runtime.Name(), cmd) logrus.Debugf("enclave runtime %s executing payload with commandline", cmd)
// The executable may not exist in container at all according // The executable may not exist in container at all according
// to the design of enclave runtime, such as Occlum, which uses // to the design of enclave runtime, such as Occlum, which uses
...@@ -78,16 +76,16 @@ func (rt *EnclaveRuntimeWrapper) ExecutePayload(cmd []string, envp []string, std ...@@ -78,16 +76,16 @@ func (rt *EnclaveRuntimeWrapper) ExecutePayload(cmd []string, envp []string, std
func (rt *EnclaveRuntimeWrapper) KillPayload(sig int, pid int) error { func (rt *EnclaveRuntimeWrapper) KillPayload(sig int, pid int) error {
if pid != -1 { if pid != -1 {
logrus.Debugf("enclave runtime %s killing payload %d with signal %d", rt.runtime.Name(), pid, sig) logrus.Debugf("enclave runtime killing payload %d with signal %d", pid, sig)
} else { } else {
logrus.Debugf("enclave runtime %s killing all payloads with signal %d", rt.runtime.Name(), sig) logrus.Debugf("enclave runtime killing all payloads with signal %d", sig)
} }
return rt.runtime.Kill(sig, pid) return rt.runtime.Kill(sig, pid)
} }
func (rt *EnclaveRuntimeWrapper) DestroyInstance() error { func (rt *EnclaveRuntimeWrapper) DestroyInstance() error {
logrus.Debugf("Destroying enclave runtime %s", rt.runtime.Name()) logrus.Debugf("Destroying enclave runtime")
return rt.runtime.Destroy() return rt.runtime.Destroy()
} }
...@@ -3,6 +3,11 @@ package enclave_runtime_pal // import "github.com/opencontainers/runc/libenclave ...@@ -3,6 +3,11 @@ package enclave_runtime_pal // import "github.com/opencontainers/runc/libenclave
/* /*
#include <stdlib.h> #include <stdlib.h>
static int palGetVersion(void *sym)
{
return ((int (*)(void))sym)();
}
static int palInitV1(void *sym, const char *args, const char *log_level) static int palInitV1(void *sym, const char *args, const char *log_level)
{ {
typedef struct { typedef struct {
...@@ -65,7 +70,7 @@ func (pal *enclaveRuntimePalApiV1) get_version() uint32 { ...@@ -65,7 +70,7 @@ func (pal *enclaveRuntimePalApiV1) get_version() uint32 {
logrus.Debugf("pal get_version() called") logrus.Debugf("pal get_version() called")
sym := nsenter.SymAddrPalVersion() sym := nsenter.SymAddrPalVersion()
if sym != nil { if sym != nil {
return *(*uint32)(sym) return uint32(C.palGetVersion(sym))
} else { } else {
return palApiVersion return palApiVersion
} }
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
) )
type enclaveRuntimePal struct { type enclaveRuntimePal struct {
name string
version uint32 version uint32
} }
......
...@@ -3,27 +3,9 @@ package enclave_runtime_pal // import "github.com/opencontainers/runc/libenclave ...@@ -3,27 +3,9 @@ package enclave_runtime_pal // import "github.com/opencontainers/runc/libenclave
import ( import (
"fmt" "fmt"
"os" "os"
"path"
"strings"
)
const (
palPrefix = "liberpal-"
palSuffix = ".so"
) )
func (pal *enclaveRuntimePal) Load(palPath string) (err error) { func (pal *enclaveRuntimePal) Load(palPath string) (err error) {
bp := path.Base(palPath)
if !strings.HasPrefix(bp, palPrefix) {
return fmt.Errorf("not found pal prefix pattern in pal %s\n", palPath)
}
if !strings.HasSuffix(bp, palSuffix) {
return fmt.Errorf("not found pal suffix pattern in pal %s\n", palPath)
}
palName := strings.TrimSuffix(strings.TrimPrefix(bp, palPrefix), palSuffix)
pal.name = palName
if err = pal.getPalApiVersion(); err != nil { if err = pal.getPalApiVersion(); err != nil {
return err return err
} }
...@@ -40,10 +22,6 @@ func (pal *enclaveRuntimePal) getPalApiVersion() error { ...@@ -40,10 +22,6 @@ func (pal *enclaveRuntimePal) getPalApiVersion() error {
return nil return nil
} }
func (pal *enclaveRuntimePal) Name() string {
return fmt.Sprintf("%s (API version %d)", pal.name, pal.version)
}
func (pal *enclaveRuntimePal) Init(args string, logLevel string) error { func (pal *enclaveRuntimePal) Init(args string, logLevel string) error {
api := &enclaveRuntimePalApiV1{} api := &enclaveRuntimePalApiV1{}
return api.init(args, logLevel) return api.init(args, logLevel)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册