Skip to content

Commit 266e3a0

Browse files
authored
compress internal/lib/*.{so,dylib} files when embedded and uncompress when loading to disk (#103)
Those embedded files represent a real weight that is impacting the serverless agent (where every MiB impacts the latency of the serverless). This PR compresses the files (so that the embedded version is stored compressed in the binary), and uncompresses them in `DumpEmbeddedWAF`
1 parent 963a7f5 commit 266e3a0

15 files changed

+41
-10
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
*.swp
1919

2020
## Ensure WAF static libraries are not excluded from vendor folders
21-
!internal/lib/*.so
22-
!internal/lib/*.dylib
21+
!internal/lib/*.so.gz
22+
!internal/lib/*.dylib.gz
2323

_tools/libddwaf-updater/update.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func main() {
101101
written += wrote
102102
}
103103

104-
fmt.Println("All done! Don't forget to check in changes to internal/lib/ and internal/log/ddhaf.h, check the libddwaf upgrade guide to update bindings!")
104+
fmt.Println("All done! Don't forget to check in changes to internal/lib/ and internal/log/ddwaf.h, check the libddwaf upgrade guide to update bindings!")
105105
}
106106

107107
// createEmbedSource creates the embed source file for the given target.
@@ -197,10 +197,12 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, assets map[str
197197
}
198198

199199
var destPath string
200+
var compress bool
200201
switch name := header.FileInfo().Name(); name {
201202
case "libddwaf.so", "libddwaf.dylib", "ddwaf.dll":
202203
destPath = path.Join(libDir, tgt.binaryLibName())
203204
foundLib = true
205+
compress = true
204206
case "ddwaf.h":
205207
if !tgt.primary {
206208
continue
@@ -211,8 +213,13 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, assets map[str
211213
continue
212214
}
213215

216+
pipe := script.NewPipe().WithReader(arch)
217+
if compress {
218+
pipe = pipe.Filter(compressFilter)
219+
}
220+
214221
fmt.Printf("... downloaded %s\n", destPath)
215-
if _, err := script.NewPipe().WithReader(arch).WriteFile(destPath); err != nil {
222+
if _, err := pipe.WriteFile(destPath); err != nil {
216223
panic(err)
217224
}
218225
if path.Ext(destPath) != ".h" {
@@ -240,6 +247,13 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, assets map[str
240247
}
241248
}
242249

250+
func compressFilter(r io.Reader, w io.Writer) error {
251+
gz := gzip.NewWriter(w)
252+
defer gz.Close()
253+
_, err := io.Copy(gz, r)
254+
return err
255+
}
256+
243257
type target struct {
244258
os string
245259
arch string
@@ -290,7 +304,7 @@ var targets = []target{
290304
}
291305

292306
func (t target) binaryLibName() string {
293-
return fmt.Sprintf("%s-%s-%s.%s", t.base, t.os, t.arch, t.ext)
307+
return fmt.Sprintf("%s-%s-%s.%s.gz", t.base, t.os, t.arch, t.ext)
294308
}
295309

296310
func (t target) embedSourceFilename() string {

internal/lib/lib.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
package lib
99

1010
import (
11+
"bytes"
12+
"compress/gzip"
1113
"fmt"
14+
"io"
1215
"os"
1316

1417
_ "embed"
@@ -41,7 +44,21 @@ func DumpEmbeddedWAF() (path string, err error) {
4144
}
4245
}()
4346

44-
if err := os.WriteFile(file.Name(), libddwaf, 0400); err != nil {
47+
gr, err := gzip.NewReader(bytes.NewReader(libddwaf))
48+
if err != nil {
49+
return path, fmt.Errorf("error creating gzip reader: %w", err)
50+
}
51+
52+
uncompressedLibddwaf, err := io.ReadAll(gr)
53+
if err != nil {
54+
return path, fmt.Errorf("error reading gzip content: %w", err)
55+
}
56+
57+
if err := gr.Close(); err != nil {
58+
return path, fmt.Errorf("error closing gzip reader: %w", err)
59+
}
60+
61+
if err := os.WriteFile(file.Name(), uncompressedLibddwaf, 0400); err != nil {
4562
return path, fmt.Errorf("error writing file: %w", err)
4663
}
4764

internal/lib/lib_darwin_amd64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package lib
1111

1212
import _ "embed" // Needed for go:embed
1313

14-
//go:embed libddwaf-darwin-amd64.dylib
14+
//go:embed libddwaf-darwin-amd64.dylib.gz
1515
var libddwaf []byte
1616

1717
const embedNamePattern = "libddwaf-*.dylib"

internal/lib/lib_darwin_arm64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package lib
1111

1212
import _ "embed" // Needed for go:embed
1313

14-
//go:embed libddwaf-darwin-arm64.dylib
14+
//go:embed libddwaf-darwin-arm64.dylib.gz
1515
var libddwaf []byte
1616

1717
const embedNamePattern = "libddwaf-*.dylib"

internal/lib/lib_linux_amd64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package lib
1111

1212
import _ "embed" // Needed for go:embed
1313

14-
//go:embed libddwaf-linux-amd64.so
14+
//go:embed libddwaf-linux-amd64.so.gz
1515
var libddwaf []byte
1616

1717
const embedNamePattern = "libddwaf-*.so"

internal/lib/lib_linux_arm64.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package lib
1111

1212
import _ "embed" // Needed for go:embed
1313

14-
//go:embed libddwaf-linux-arm64.so
14+
//go:embed libddwaf-linux-arm64.so.gz
1515
var libddwaf []byte
1616

1717
const embedNamePattern = "libddwaf-*.so"
-1.67 MB
Binary file not shown.
616 KB
Binary file not shown.
-1.53 MB
Binary file not shown.

0 commit comments

Comments
 (0)