Skip to content

Commit

Permalink
Don't panic (#360)
Browse files Browse the repository at this point in the history
* Dont panic in PreMain

* Disable stack trace on error

Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse authored Jan 27, 2023
1 parent 2332380 commit 1a1c46e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
14 changes: 8 additions & 6 deletions cmd/coordinator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -35,16 +36,17 @@ func run(validator quote.Validator, issuer quote.Issuer, sealDir string, sealer
devMode := util.Getenv(constants.DevMode, constants.DevModeDefault) == "1"

// Setup logging with Zap Logger
// Development Logger shows a stacktrace for warnings & errors, Production Logger only for errors
var log *zap.Logger
var err error
var cfg zap.Config
if devMode {
log, err = zap.NewDevelopment()
cfg = zap.NewDevelopmentConfig()
} else {
log, err = zap.NewProduction()
cfg = zap.NewProductionConfig()
cfg.DisableStacktrace = true // Disable stacktraces in production
}
log, err := cfg.Build()
if err != nil {
panic(err)
fmt.Fprintf(os.Stderr, "failed to create logger: %s\n", err)
os.Exit(1)
}
defer log.Sync() // flushes buffer, if any

Expand Down
19 changes: 14 additions & 5 deletions cmd/premain-libos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand All @@ -26,13 +27,21 @@ const (
occlum
)

func exit(format string, args ...interface{}) {
// Print error message in red and append newline
// then exit with error code 1
msg := fmt.Sprintf("Error: %s\n", format)
_, _ = color.New(color.FgRed).Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}

func main() {
log.SetPrefix("[PreMain] ")

// Automatically detect libOS based on uname
libOS, err := detectLibOS()
if err != nil {
panic(err)
exit("failed to detect libOS: %s", err)
}

// Use filesystem from libOS
Expand All @@ -47,7 +56,7 @@ func main() {
// Gramine: Get service to launch before MarbleRun's premain
service, err = prepareGramine(hostfs)
if err != nil {
panic(err)
exit("activating Gramine Marble failed: %s", err)
}

case occlum:
Expand All @@ -56,13 +65,13 @@ func main() {
// Occlum: Get entrypoint from MarbleRun manifest, adjust it to Occlum's quirks
service, err = prepareOcclum(hostfs)
if err != nil {
panic(err)
exit("activating Occlum Marble failed: %s", err)
}
}

// Launch service
if err := unix.Exec(service, os.Args, os.Environ()); err != nil {
panic(err)
exit("%s", err)
}
}

Expand Down Expand Up @@ -109,7 +118,7 @@ func prepareGramine(hostfs afero.Fs) (string, error) {
func prepareOcclum(hostfs afero.Fs) (string, error) {
// Run MarbleRun premain
if err := marblePremain.PreMainEx(marblePremain.OcclumQuoteIssuer{}, marblePremain.ActivateRPC, hostfs, hostfs); err != nil {
panic(err)
return "", err
}

// Check if the entrypoint defined in os.Args[0] actually exists
Expand Down

0 comments on commit 1a1c46e

Please sign in to comment.