Skip to content

Commit

Permalink
test(preload): build once, then test against both libc flavors
Browse files Browse the repository at this point in the history
The previous build & test setup was invalid: So far we have built
_different_ libdash0envhook.so binaries for the LD_PRELOAD hook per
libc flavor. With this, the binary works in the target system.

But actually this approach is not not possible in the real world. We
can set one binary as LD_PRELOAD when instrumenting a container and we
do not know which libc flavor it is ahead of time. Thus we ultimately
need to build a libdash0envhook.so binary that works independently of
which libc flavor the target system uses.

This commit only fixes the test setup. The binary does not yet support
both libc flavors. This will be handled in a follow up commit.

[skip ci]
  • Loading branch information
basti1302 committed Jun 24, 2024
1 parent b149f47 commit 53a7ec2
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 48 deletions.
28 changes: 17 additions & 11 deletions images/dash0-instrumentation/preload/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

ARCH := $(shell uname -m)
CFLAGS ?= -Wall -Werror -Wextra -O2
LIB_LINKER_FLAGS ?= -nostdlib -rdynamic -shared -ldl

SRC_DIR := src
BIN_DIR := bin
OBJ_DIR := obj
LIB_DIR := lib
TEST_DIR := test
TESTBIN_DIR := testbin/$(ARCH)
SRC_EXT := c

OS := $(shell uname -s)
Expand All @@ -17,21 +19,25 @@ NAMES := $(notdir $(basename $(wildcard $(SRC_DIR)/*.$(SRC_EXT))))
OBJECTS :=$(patsubst %,$(OBJ_DIR)/%.o,$(NAMES))

TEST_NAMES := $(notdir $(basename $(wildcard $(TEST_DIR)/*.$(SRC_EXT))))
TEST_OBJECTS :=$(patsubst %,$(OBJ_DIR)/%.o,$(TEST_NAMES))
TEST_OBJECTS :=$(patsubst %,$(TESTBIN_DIR)/%.o,$(TEST_NAMES))

all: $(OBJECTS) $(TEST_OBJECTS) testbin/appundertest.so
$(CC) $(CFLAGS) -rdynamic -shared -ldl $(OBJECTS) -o $(LIB_DIR)/libdash0envhook.so
all: $(LIB_DIR)/libdash0envhook.so

$(LIB_DIR)/libdash0envhook.so: $(OBJECTS)
$(CC) $(CFLAGS) $(LIB_LINKER_FLAGS) $(OBJECTS) -o $(LIB_DIR)/libdash0envhook.so

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.$(SRC_EXT)
$(CC) -c -fPIC $^ -o $@ $(DEBUG) $(CFLAGS) $(LIBS)

$(OBJ_DIR)/%.o: $(TEST_DIR)/%.$(SRC_EXT)
$(CC) -c $^ -o $@ $(DEBUG) $(CFLAGS) $(LIBS)
clean: clean-test
@rm -f $(OBJECTS) $(LIB_DIR)/libdash0envhook.so

clean-test:
@rm -f $(TEST_OBJECTS)

testbin/appundertest.so: test/appundertest.c
$(CC) $(CFLAGS) -o $@ test/appundertest.c
build-test: $(TEST_OBJECTS)

clean:
@rm -f $(OBJECTS) $(OBJ_DIR)/*.o $(TEST_OBJECTS) $(LIB_DIR)/*.so testbin/appundertest.so
$(TESTBIN_DIR)/%.o: $(TEST_DIR)/%.$(SRC_EXT)
$(CC) $(CFLAGS) -o $@ $(DEBUG) $^

.PHONY: all clean install
.PHONY: all clean install clean-test build-test
12 changes: 12 additions & 0 deletions images/dash0-instrumentation/preload/docker/Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

FROM ubuntu:24.10

RUN apt update && \
apt-get install -y \
build-essential

WORKDIR /usr/src/dash0/preload/

CMD ["scripts/make-clean.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ RUN apt update && \

WORKDIR /usr/src/dash0/preload/

CMD ["test/smoke-test.sh"]
CMD ["test/run-tests.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN apk add --no-cache build-base

WORKDIR /usr/src/dash0/preload/

CMD ["test/smoke-test.sh"]
CMD ["test/run-tests.sh"]
54 changes: 54 additions & 0 deletions images/dash0-instrumentation/preload/scripts/build-in-container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env sh

# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

set -eu

cd "$(dirname "$0")"/..

# TODO build multi platform image

if [ -z "${ARCH:-}" ]; then
ARCH=arm64
fi
if [ "$ARCH" = arm64 ]; then
docker_platform=linux/arm64
elif [ "$ARCH" = x86_64 ]; then
docker_platform=linux/amd64
else
echo "The architecture $ARCH is not supported."
exit 1
fi

dockerfile_name=docker/Dockerfile-build
image_name=dash0-env-hook-builder
container_name=$image_name

docker_run_extra_arguments=""
if [ "${INTERACTIVE:-}" = "true" ]; then
docker_run_extra_arguments=/bin/bash
fi

echo
echo
echo ">>> Building the library on $ARCH <<<"

docker rm -f "$container_name"
docker build \
--platform "$docker_platform" \
. \
-f "$dockerfile_name" \
-t "$image_name"

# note: building one image for both platforms is not supported by Docker desktop's standard "docker build" command.
# docker build --platform linux/amd64,linux/arm64 . -f $dockerfile_name -t dash0-env-hook-builder-all-$LIBC

docker run \
--platform "$docker_platform" \
--name "$container_name" \
-it \
--volume "$(pwd):/usr/src/dash0/preload/" \
"$image_name" \
$docker_run_extra_arguments

12 changes: 12 additions & 0 deletions images/dash0-instrumentation/preload/scripts/make-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh

# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

set -eu

cd "$(dirname "$0")"/..

make clean
make

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set -eu

cd "$(dirname "$0")"
cd "$(dirname "$0")"/..

if [ -z "${ARCH:-}" ]; then
ARCH=arm64
Expand All @@ -25,17 +25,13 @@ if [ -z "${LIBC:-}" ]; then
LIBC=glibc
fi

echo
echo
echo ">>> Building and testing for $ARCH and $LIBC <<<"

dockerfile_name="Dockerfile-$LIBC"
dockerfile_name="docker/Dockerfile-test-$LIBC"
if [ ! -f "$dockerfile_name" ]; then
echo "The file \"$dockerfile_name\" does not exist, the libc flavor $LIBC is not supported."
exit 1
fi

image_name=dash0-env-hook-builder-$ARCH-$LIBC
image_name=dash0-env-hook-test-$ARCH-$LIBC
container_name=$image_name

docker_run_extra_arguments=""
Expand All @@ -50,11 +46,16 @@ if [ "${INTERACTIVE:-}" = "true" ]; then
fi
fi

docker rm -f "$container_name"
docker build --platform "$docker_platform" . -f "$dockerfile_name" -t "$image_name"
echo
echo
echo ">>> Testing the library on $ARCH and $LIBC <<<"

# note: building one image for both platforms is not suppored on Docker desktop
# docker build --platform linux/amd64,linux/arm64 . -f $dockerfile_name -t dash0-env-hook-builder-all-$LIBC
docker rm -f "$container_name"
docker build \
--platform "$docker_platform" \
. \
-f "$dockerfile_name" \
-t "$image_name"

docker run \
--platform "$docker_platform" \
Expand Down
16 changes: 16 additions & 0 deletions images/dash0-instrumentation/preload/scripts/test-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

# SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
# SPDX-License-Identifier: Apache-2.0

set -eu

cd "$(dirname "$0")"/..

scripts/build-in-container.sh

ARCH=arm64 LIBC=glibc scripts/run-tests-in-container.sh
ARCH=x86_64 LIBC=glibc scripts/run-tests-in-container.sh
ARCH=arm64 LIBC=musl scripts/run-tests-in-container.sh
ARCH=x86_64 LIBC=musl scripts/run-tests-in-container.sh

1 change: 0 additions & 1 deletion images/dash0-instrumentation/preload/src/libdash0envhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Entry map[1];

char* default_node_options_value = "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js";


__attribute__((constructor)) static void setup(void) {
Entry node_options_entry = { .key = "NODE_OPTIONS", .value = NULL };
map[0] = node_options_entry;
Expand Down
3 changes: 1 addition & 2 deletions images/dash0-instrumentation/preload/src/map.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc.
// SPDX-License-Identifier: Apache-2.0

#include <stdlib.h>
#include <string.h>

#include "map.h"

Entry* find(Entry map[], int len, const char* key) {
for (int i = 0; i < len; i++) {
for (int i = 0; i < len; i++) {
Entry* e = &map[i];
if (strcmp(e->key, key) == 0) {
return e;
Expand Down
1 change: 1 addition & 0 deletions images/dash0-instrumentation/preload/src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ typedef struct Entry {
char* get_map_entry(Entry map[], int len, const char* key);

void put_map_entry(Entry* map, int len, const char* key, char* value);

14 changes: 0 additions & 14 deletions images/dash0-instrumentation/preload/test-all.sh

This file was deleted.

3 changes: 1 addition & 2 deletions images/dash0-instrumentation/preload/test/appundertest.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ void echo_env_var_secure(const char* name) {

int main(int argc, char* argv[]) {
if (argc < 2) {
fputs("not enough arguments, the name of the test case needs to be specifed", stdout);
fputs("\n", stdout);
fputs("error: not enough arguments, the name of the test case needs to be specifed\n", stdout);
exit(1);
}
char* test_case = argv[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@ else
printf "${GREEN}verifying CPU architecture %s successful${NC}\n" "$EXPECTED_CPU_ARCHITECTURE"
fi

preload_lib=$directory/lib/libdash0envhook.so
if [ ! -f $preload_lib ]; then
printf "${RED}error: $preload_lib does not exist, not running any tests.${NC}\n"
exit 1
fi

appundertest=testbin/"${arch_output}"/appundertest.o
echo appundertest: $appundertest

run_test_case() {
test_case=$1
command=$2
expected=$3
existing_node_options_value=${4:-}
set +e
if [ "$existing_node_options_value" != "" ]; then
test_output=$(LD_PRELOAD="$directory/lib/libdash0envhook.so" NODE_OPTIONS="$existing_node_options_value" ./testbin/appundertest.so "$command")
test_output=$(LD_PRELOAD="$preload_lib" NODE_OPTIONS="$existing_node_options_value" "$appundertest" "$command")
else
test_output=$(LD_PRELOAD="$directory/lib/libdash0envhook.so" ./testbin/appundertest.so "$command")
test_output=$(LD_PRELOAD="$preload_lib" "$appundertest" "$command")
fi
test_exit_code=$?
set -e
Expand All @@ -62,8 +71,9 @@ run_test_case() {
fi
}

make clean
make
# We always need to clean out the old appundertest.o, it might have been built for a different libc flavor.
make clean-test
make build-test

exit_code=0

Expand Down
1 change: 0 additions & 1 deletion images/dash0-instrumentation/preload/testbin/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.o
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.o

0 comments on commit 53a7ec2

Please sign in to comment.