-
Notifications
You must be signed in to change notification settings - Fork 7
llvm: expose deterministic archive writing #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: xgo
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //===- archive.go - Bindings for archive writing --------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file defines bindings for llvm::writeArchive. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package llvm | ||
|
|
||
| /* | ||
| #include "llvm-c/Core.h" | ||
| #include "backports.h" | ||
| #include <stdlib.h> | ||
| */ | ||
| import "C" | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // ArchiveMember is an object or bitcode file to add to an archive. | ||
| // | ||
| // A memory-buffer member borrows its buffer. The caller must keep the buffer | ||
| // alive until WriteArchive returns. | ||
| type ArchiveMember struct { | ||
| name string | ||
| path string | ||
| buffer MemoryBuffer | ||
| } | ||
|
|
||
| // NewArchiveMemberFromFile creates a path-backed archive member. The archive | ||
| // member name is the base name of path. | ||
| func NewArchiveMemberFromFile(path string) ArchiveMember { | ||
| return ArchiveMember{path: path} | ||
| } | ||
|
|
||
| // NewArchiveMemberFromMemoryBuffer creates a memory-backed archive member. | ||
| // name is the name stored in the archive. | ||
| func NewArchiveMemberFromMemoryBuffer(name string, buffer MemoryBuffer) ArchiveMember { | ||
| return ArchiveMember{name: name, buffer: buffer} | ||
| } | ||
|
|
||
| // WriteArchive creates a deterministic, non-thin archive with a symbol table. | ||
| // targetTriple determines the platform archive format. | ||
| func WriteArchive(path, targetTriple string, members []ArchiveMember) error { | ||
| if path == "" { | ||
| return errors.New("archive path is empty") | ||
| } | ||
| if len(members) == 0 { | ||
| return errors.New("archive has no members") | ||
| } | ||
|
|
||
| cTriple := C.CString(targetTriple) | ||
| defer C.free(unsafe.Pointer(cTriple)) | ||
| writer := C.LLVMGoCreateArchiveWriter(cTriple) | ||
| if writer == nil { | ||
| return errors.New("failed to create archive writer") | ||
| } | ||
| defer C.LLVMGoDisposeArchiveWriter(writer) | ||
|
|
||
| for i, member := range members { | ||
| var cErr *C.char | ||
| switch { | ||
| case member.path != "": | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| cPath := C.CString(member.path) | ||
| var cName *C.char | ||
| if member.name != "" { | ||
| cName = C.CString(member.name) | ||
| } | ||
| cErr = C.LLVMGoArchiveWriterAddFile(writer, cPath, cName) | ||
| C.free(unsafe.Pointer(cPath)) | ||
| C.free(unsafe.Pointer(cName)) | ||
| case !member.buffer.IsNil(): | ||
| if member.name == "" { | ||
| return fmt.Errorf("archive member %d has an empty name", i) | ||
| } | ||
| cName := C.CString(member.name) | ||
| cErr = C.LLVMGoArchiveWriterAddMemoryBuffer(writer, member.buffer.C, cName) | ||
| C.free(unsafe.Pointer(cName)) | ||
| default: | ||
| return fmt.Errorf("archive member %d has no file or memory buffer", i) | ||
| } | ||
| if cErr != nil { | ||
| err := errors.New(C.GoString(cErr)) | ||
| C.LLVMDisposeMessage(cErr) | ||
| label := member.name | ||
| if member.path != "" { | ||
| label = member.path | ||
| } | ||
| return fmt.Errorf("add archive member %d (%q): %w", i, label, err) | ||
| } | ||
| } | ||
|
|
||
| cPath := C.CString(path) | ||
| defer C.free(unsafe.Pointer(cPath)) | ||
| if cErr := C.LLVMGoArchiveWriterWrite(writer, cPath); cErr != nil { | ||
| err := errors.New(C.GoString(cErr)) | ||
| C.LLVMDisposeMessage(cErr) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| //===- archive_test.go - Tests for archive writing ------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package llvm | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteArchiveMemoryAndFileMembers(t *testing.T) { | ||
| ctx := NewContext() | ||
| defer ctx.Dispose() | ||
|
|
||
| mod := ctx.NewModule("archive") | ||
| defer mod.Dispose() | ||
| mod.SetTarget("x86_64-unknown-linux-gnu") | ||
| AddFunction(mod, "answer", FunctionType(ctx.Int32Type(), nil, false)) | ||
|
|
||
| memoryBuf := WriteBitcodeToMemoryBuffer(mod) | ||
| defer memoryBuf.Dispose() | ||
|
|
||
| fileBuf := WriteBitcodeToMemoryBuffer(mod) | ||
| defer fileBuf.Dispose() | ||
| filePath := filepath.Join(t.TempDir(), "from-file.bc") | ||
| if err := os.WriteFile(filePath, fileBuf.Bytes(), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| archivePath := filepath.Join(t.TempDir(), "libarchive.a") | ||
| err := WriteArchive(archivePath, mod.Target(), []ArchiveMember{ | ||
| NewArchiveMemberFromMemoryBuffer("from-memory.bc", memoryBuf), | ||
| NewArchiveMemberFromFile(filePath), | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| data, err := os.ReadFile(archivePath) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !bytes.HasPrefix(data, []byte("!<arch>\n")) { | ||
| end := len(data) | ||
| if end > 8 { | ||
| end = 8 | ||
| } | ||
| t.Fatalf("archive magic = %q", data[:end]) | ||
| } | ||
| for _, name := range []string{"from-memory.bc", "from-file.bc"} { | ||
| if !bytes.Contains(data, []byte(name)) { | ||
| t.Errorf("archive does not contain member name %q", name) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestWriteArchiveRejectsInvalidMembers(t *testing.T) { | ||
| path := filepath.Join(t.TempDir(), "invalid.a") | ||
| missing := filepath.Join(t.TempDir(), "missing.o") | ||
| tests := []struct { | ||
| name string | ||
| path string | ||
| members []ArchiveMember | ||
| want string | ||
| }{ | ||
| {name: "empty path", members: []ArchiveMember{{}}, want: "archive path is empty"}, | ||
| {name: "no members", path: path, want: "archive has no members"}, | ||
| {name: "empty member", path: path, members: []ArchiveMember{{}}, want: "has no file or memory buffer"}, | ||
| {name: "missing file", path: path, members: []ArchiveMember{NewArchiveMemberFromFile(missing)}, want: missing}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := WriteArchive(tt.path, "x86_64-unknown-linux-gnu", tt.members) | ||
| if err == nil || !strings.Contains(err.Error(), tt.want) { | ||
| t.Fatalf("WriteArchive error = %v, want %q", err, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
targetTripleis empty, the C side silently falls back toArchive::getDefaultKind()(host default) rather than failing (backports.cpp:47-51).WriteArchivedoesn't reject an empty triple, so this fallback is reachable. Worth documenting the empty-triple behavior here so callers know an unset triple produces a host-default archive format rather than an error.