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+
810package internal
911
1012import (
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