aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/exec.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-05-12 15:10:43 +0900
committerOphestra <cat@gensokyo.uk>2026-05-12 15:11:17 +0900
commit6643cfbeee181ee1bd543e2b3783bb5a8c01ad13 (patch)
tree04b1b3b1d3fa044a1f8d5097355fa2779fe95af9 /internal/pkg/exec.go
parentdcde38f2e967ebf14a3c8a7f933b7dd1bf465566 (diff)
internal/pkg: optionally measure exec artifact
Useful for verifying deterministic output without enabling network access. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/exec.go')
-rw-r--r--internal/pkg/exec.go48
1 files changed, 29 insertions, 19 deletions
diff --git a/internal/pkg/exec.go b/internal/pkg/exec.go
index b91299dd..c0ea7485 100644
--- a/internal/pkg/exec.go
+++ b/internal/pkg/exec.go
@@ -167,6 +167,9 @@ var _ fmt.Stringer = new(execArtifact)
type execNetArtifact struct {
checksum Checksum
+ // Whether to keep host net namespace.
+ hostNet bool
+
execArtifact
}
@@ -175,15 +178,24 @@ var _ KnownChecksum = new(execNetArtifact)
// Checksum returns the caller-supplied checksum.
func (a *execNetArtifact) Checksum() Checksum { return a.checksum }
-// Kind returns the hardcoded [Kind] constant.
-func (*execNetArtifact) Kind() Kind { return KindExecNet }
+// Kind returns [KindExecNet], or [KindExec] if hostNet is false.
+func (a *execNetArtifact) Kind() Kind {
+ if a == nil || a.hostNet {
+ return KindExecNet
+ }
+ return KindExec
+}
// Cure cures the [Artifact] in the container described by the caller. The
// container retains host networking.
func (a *execNetArtifact) Cure(f *FContext) error {
- return a.cure(f, true)
+ return a.cure(f, a.hostNet)
}
+// ErrNetChecksum is panicked by [NewExec] if host net namespace is requested
+// with a nil checksum.
+var ErrNetChecksum = errors.New("attempting to keep net namespace without checksum")
+
// NewExec returns a new [Artifact] that executes the program path in a
// container with specified paths bind mounted read-only in order. A private
// instance of /proc and /dev is made available to the container.
@@ -197,7 +209,7 @@ func (a *execNetArtifact) Cure(f *FContext) error {
// regular or symlink.
//
// If checksum is non-nil, the resulting [Artifact] implements [KnownChecksum]
-// and its container runs in the host net namespace.
+// and its container optionally runs in the host net namespace.
//
// The container is allowed to run for the specified duration before the initial
// process and all processes originating from it is terminated. A zero or
@@ -211,7 +223,7 @@ func NewExec(
name, arch string,
checksum *Checksum,
timeout time.Duration,
- exclusive bool,
+ hostNet, exclusive bool,
dir *check.Absolute,
env []string,
@@ -234,9 +246,12 @@ func NewExec(
}
a := execArtifact{name, arch, paths, dir, env, pathname, args, timeout, exclusive}
if checksum == nil {
+ if hostNet {
+ panic(ErrNetChecksum)
+ }
return &a
}
- return &execNetArtifact{*checksum, a}
+ return &execNetArtifact{*checksum, hostNet, a}
}
// Kind returns the hardcoded [Kind] constant.
@@ -361,22 +376,17 @@ func readExecArtifact(r *IRReader, net bool) Artifact {
exclusive := r.ReadUint32() != 0
checksum, ok := r.Finalise()
-
var checksumP *Checksum
- if net {
- if !ok {
- panic(ErrExpectedChecksum)
- }
- checksumVal := checksum.Value()
- checksumP = &checksumVal
- } else {
- if ok {
- panic(ErrUnexpectedChecksum)
- }
+ if ok {
+ checksumP = new(checksum.Value())
+ }
+
+ if net && !ok {
+ panic(ErrExpectedChecksum)
}
return NewExec(
- name, arch, checksumP, timeout, exclusive, dir, env, pathname, args, paths...,
+ name, arch, checksumP, timeout, net, exclusive, dir, env, pathname, args, paths...,
)
}
@@ -590,7 +600,7 @@ func (c *Cache) EnterExec(
case *execNetArtifact:
e = &f.execArtifact
- hostNet = true
+ hostNet = f.hostNet
default:
return ErrNotExec