Skip to content
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

Fix gRPC instrumentation on recent kernels #150

Merged
merged 7 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http

## [Unreleased]

### Fixed

- Fix gRPC instrumentation memory access issue on newer kernels. ([#150](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/150))

## [v0.2.0-alpha] - 2023-05-03

### Added
Expand Down
16 changes: 16 additions & 0 deletions include/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "bpf_helpers.h"

#define MAX_ENTRIES 50
#define MAX_BUFFER_SIZE 1024
#define MIN_BUFFER_SIZE 1

// Injected in init
volatile const u32 total_cpus;
Expand Down Expand Up @@ -65,6 +67,19 @@ static __always_inline u64 get_area_end(u64 start)
}
}

static __always_inline s32 bound_number(s32 num, s32 min, s32 max)
{
if (num < min)
{
return min;
}
else if (num > max)
{
return max;
}
return num;
}

static __always_inline void *write_target_data(void *data, s32 size)
{
if (!data || data == NULL)
Expand All @@ -83,6 +98,7 @@ static __always_inline void *write_target_data(void *data, s32 size)
}

void *target = (void *)start;
size = bound_number(size, MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
long success = bpf_probe_write_user(target, data, size);
if (success == 0)
{
Expand Down
18 changes: 16 additions & 2 deletions include/go_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "bpf_helpers.h"

#define MAX_REALLOCATION 400
#define MAX_DATA_SIZE 400

struct go_string
{
Expand Down Expand Up @@ -72,6 +73,11 @@ static __always_inline void append_item_to_slice(struct go_slice *slice, void *n
else
{
// No room on current array - copy to new one of size item_size * (len + 1)
if (slice->len > MAX_DATA_SIZE || slice->len < 1)
{
return;
}

s32 alloc_size = item_size * slice->len;
s32 bounded_alloc_size = alloc_size > MAX_REALLOCATION ? MAX_REALLOCATION : (alloc_size < 1 ? 1 : alloc_size);

Expand All @@ -86,7 +92,15 @@ static __always_inline void append_item_to_slice(struct go_slice *slice, void *n
// Append to buffer
bpf_probe_read_user(map_buff, bounded_alloc_size, slice->array);
bpf_probe_read(map_buff + bounded_alloc_size, item_size, new_item);
void *new_array = write_target_data(map_buff, bounded_alloc_size + item_size);

// Copy buffer to userspace
u32 new_array_size = bounded_alloc_size + item_size;
if (new_array_size > MAX_DATA_SIZE || new_array_size < 1)
{
return;
}

void *new_array = write_target_data(map_buff, new_array_size);

// Update array
slice->array = new_array;
Expand All @@ -100,4 +114,4 @@ static __always_inline void append_item_to_slice(struct go_slice *slice, void *n
// Update len
slice->len++;
long success = bpf_probe_write_user(slice_user_ptr->len, &slice->len, sizeof(slice->len));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (g *Instrumentor) Load(ctx *context.InstrumentorContext) error {
}

up, err := ctx.Executable.Uprobe("", g.bpfObjects.UprobeServerHandleStream, &link.UprobeOptions{
Offset: offset,
Address: offset,
})
if err != nil {
return err
Expand Down