Skip to content

Commit 2282839

Browse files
committed
Address review comments
- Add link to reference implementation - Atomically write the signature to the start of the mapping - Ignore the return value of Prctl and do not log the error
1 parent 56e26fa commit 2282839

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

internal/otelcontextmapping_linux.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55

66
//go:build linux
77

8+
// This is a go port of https://github.com/DataDog/fullhost-code-hotspots-wip/blob/main/lang-exp/anonmapping-clib/otel_process_ctx.c
9+
810
package internal
911

1012
import (
13+
"encoding/binary"
1114
"fmt"
1215
"os"
16+
"sync/atomic"
1317
"unsafe"
1418

15-
"github.com/DataDog/dd-trace-go/v2/internal/log"
1619
"golang.org/x/sys/unix"
1720
)
1821

@@ -79,8 +82,9 @@ func CreateOtelProcessContextMapping(data []byte) error {
7982

8083
copy(mappingBytes[headerSize:], data)
8184
copy(mappingBytes[:headerSize], unsafe.Slice((*byte)(unsafe.Pointer(&header)), headerSize))
82-
// write the signature last to ensure that once a process validates the signature, it can safely read the whole data
83-
copy(mappingBytes, otelContextSignature)
85+
// write atomically the signature last to ensure that once a process validates the signature, it can safely read the whole data
86+
sig := binary.NativeEndian.Uint64(unsafe.Slice(unsafe.StringData(otelContextSignature), len(otelContextSignature)))
87+
atomic.StoreUint64((*uint64)(unsafe.Pointer(&mappingBytes[0])), sig)
8488

8589
err = unix.Mprotect(mappingBytes, unix.PROT_READ)
8690
if err != nil {
@@ -90,17 +94,14 @@ func CreateOtelProcessContextMapping(data []byte) error {
9094

9195
// prctl expects a null-terminated string
9296
contextNameNullTerminated, _ := unix.ByteSliceFromString(otelContextSignature)
93-
err = unix.Prctl(
97+
// Failure to set the vma anon name is not a critical error (only supported on Linux 5.17+), so we ignore the return value.
98+
_ = unix.Prctl(
9499
PR_SET_VMA,
95100
uintptr(PR_SET_VMA_ANON_NAME),
96101
addr,
97102
uintptr(otelContextMappingSize),
98103
uintptr(unsafe.Pointer(&contextNameNullTerminated[0])),
99104
)
100-
if err != nil {
101-
// Failure to set the vma anon name is not a critical error (only supported on Linux 5.17+), so we log it at the warning level.
102-
log.Warn("failed to set vma anon name: %s", err.Error())
103-
}
104105

105106
existingMappingBytes = mappingBytes
106107
return nil

0 commit comments

Comments
 (0)