-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1726 from atsign-foundation/c-memcheck-tools
feat: C memcheck tools
- Loading branch information
Showing
27 changed files
with
347 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ build* | |
|
||
compile_commands.json | ||
|
||
just.env | ||
|
||
valgrind-sshnpd-*.log | ||
core | ||
valgrind-out.txt* | ||
Testing/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.19) | ||
set(CMAKE_C_STANDARD 99) | ||
cmake_policy(SET CMP0135 NEW) | ||
project(csshnpd VERSION 0.4.1 LANGUAGES C) | ||
project(csshnpd VERSION 0.4.3 LANGUAGES C) | ||
add_subdirectory(sshnpd) | ||
add_subdirectory(graceful-shutdown-tool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"version": 8, | ||
"configurePresets": [ | ||
{ | ||
"name": "dev", | ||
"binaryDir": "${sourceDir}/build/dev", | ||
"cacheVariables": { | ||
"BUILD_SHARED_LIBS": "OFF", | ||
"CMAKE_BUILD_TYPE": "Debug", | ||
"CMAKE_C_FLAGS": "-std=c99 -Wno-error", | ||
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON", | ||
"ATSDK_BUILD_TESTS": "OFF", | ||
"ATSDK_MEMCHECK": "OFF", | ||
"ENABLE_TESTING": "OFF", | ||
"ENABLE_PROGRAMS": "OFF", | ||
"ENABLE_CJSON_TEST": "OFF", | ||
"CJSON_OVERRIDE_BUILD_SHARED_LIBS": "ON", | ||
"CJSON_BUILD_SHARED_LIBS": "OFF", | ||
"BUILD_SHARED_AND_STATIC_LIBS": "OFF", | ||
"ENABLE_TARGET_EXPORT": "OFF", | ||
"ENABLE_CJSON_VERSION_SO": "OFF" | ||
} | ||
}, | ||
{ | ||
"name": "dev-ninja", | ||
"inherits": ["dev"], | ||
"generator": "Ninja" | ||
}, | ||
{ | ||
"name": "strict", | ||
"binaryDir": "${sourceDir}/build/strict", | ||
"cacheVariables": { | ||
"BUILD_SHARED_LIBS": "OFF", | ||
"CMAKE_BUILD_TYPE": "Debug", | ||
"CMAKE_C_FLAGS": "-std=c99 -Wall -Wextra -Werror-implicit-function-declaration", | ||
"CMAKE_EXPORT_COMPILE_COMMANDS": "OFF", | ||
"ATSDK_BUILD_TESTS": "OFF", | ||
"ATSDK_MEMCHECK": "OFF", | ||
"ENABLE_TESTING": "OFF", | ||
"ENABLE_PROGRAMS": "OFF", | ||
"ENABLE_CJSON_TEST": "OFF", | ||
"CJSON_OVERRIDE_BUILD_SHARED_LIBS": "ON", | ||
"CJSON_BUILD_SHARED_LIBS": "OFF", | ||
"BUILD_SHARED_AND_STATIC_LIBS": "OFF", | ||
"ENABLE_TARGET_EXPORT": "OFF", | ||
"ENABLE_CJSON_VERSION_SO": "OFF" | ||
} | ||
}, | ||
{ | ||
"name": "strict-ninja", | ||
"inherits": ["strict"], | ||
"generator": "Ninja" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.19) | ||
set(CMAKE_C_STANDARD 99) | ||
cmake_policy(SET CMP0135 NEW) | ||
project(graceful-shutdown-tool VERSION 0.4.1 LANGUAGES C) | ||
|
||
include(FetchContent) | ||
include(GNUInstallDirs) | ||
cmake_policy(SET CMP0135 NEW) | ||
|
||
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/atsdk.cmake) | ||
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/main.c) | ||
|
||
target_link_libraries( | ||
${PROJECT_NAME} | ||
PRIVATE atclient atchops atlogger cjson argparse::argparse-static | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# graceful-shutdown-tool | ||
|
||
When sshnpd is built with the `SSHNPD_ENABLE_TESTING_SHUTDOWN_NOTIFICATION` | ||
compile definition, then it enables an extra notification handler for the key | ||
`graceful_shutdown`. When sshnpd receives this key, it does a graceful shutdown, | ||
exiting the main handler loop and proceeding with shutdown. | ||
|
||
Normally this type of exit only occurs when sshnpd has some unrecoverable error. | ||
Passing SIGINT to sshnpd does not gracefully exit, so in order for sshnpd to | ||
exit in such a way that we can properly run memcheck, this special notification, | ||
which is only available behind the compile definition is used to trigger an exit. | ||
|
||
This graceful-shutdown-tool can be used to send this notification. | ||
|
||
## Usage | ||
|
||
`./graceful-shutdown-tool $FROM $TO` | ||
|
||
> FROM is the -a flag in sshnpd | ||
> TO is the -m flag in sshnpd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "atclient/notify.h" | ||
#include "atclient/notify_params.h" | ||
#include "atlogger/atlogger.h" | ||
#include <atclient/atclient.h> | ||
#include <string.h> | ||
|
||
// proper error handling and memory management is not done in this program | ||
// since it is only used as a development tool, it is not perfect code | ||
|
||
int main(int argc, char **argv) { | ||
if (argc != 4) { | ||
printf("Usage: %s <from_atsign> <to_atsign> <device_name>\n", argv[0]); | ||
exit(1); | ||
} | ||
atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); | ||
|
||
char *from = argv[1]; | ||
char *to = argv[2]; | ||
char *device_name = argv[3]; | ||
|
||
atclient_atkeys_file atkeys_file; | ||
atclient_atkeys_file_init(&atkeys_file); | ||
|
||
char *file_path = atkeys_file_get_default_path(from); | ||
if (file_path == NULL) { | ||
printf("A\n"); | ||
return 1; | ||
} | ||
|
||
atclient_atkeys atkeys; | ||
atclient_atkeys_init(&atkeys); | ||
|
||
int ret = atclient_atkeys_populate_from_path(&atkeys, file_path); | ||
if (ret != 0) { | ||
printf("B\n"); | ||
return ret; | ||
} | ||
|
||
atclient_atkey atkey; | ||
atclient_atkey_init(&atkey); | ||
|
||
size_t namespace_len = strlen(device_name) + 1 + 5 + 1; | ||
char namespace[namespace_len]; | ||
snprintf(namespace, namespace_len, "%s.sshnp", device_name); | ||
atclient_atkey_set_shared_with(&atkey, to); | ||
atclient_atkey_set_key(&atkey, "graceful_shutdown"); | ||
atclient_atkey_set_namespace_str(&atkey, namespace); | ||
atclient_atkey_set_shared_by(&atkey, from); | ||
|
||
atclient_notify_params params; | ||
atclient_notify_params_init(¶ms); | ||
|
||
atclient_notify_params_set_atkey(¶ms, &atkey); | ||
atclient_notify_params_set_operation(¶ms, ATCLIENT_NOTIFY_OPERATION_UPDATE); | ||
atclient_notify_params_set_value(¶ms, "foo"); // ignored, value doesn't matter | ||
atclient_notify_params_set_should_encrypt(¶ms, true); | ||
|
||
atclient atclient; | ||
atclient_init(&atclient); | ||
|
||
ret = atclient_pkam_authenticate(&atclient, from, &atkeys, NULL, NULL); | ||
if (ret != 0) { | ||
printf("C\n"); | ||
return ret; | ||
} | ||
|
||
ret = atclient_notify(&atclient, ¶ms, NULL); | ||
if (ret != 0) { | ||
printf("D\n"); | ||
return ret; | ||
} | ||
|
||
printf("sent\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
C_COMPILER=gcc | ||
GENERATOR=Ninja | ||
# Used by memcheck & graceful shutdown tool in justfile | ||
FROM_ATSIGN= | ||
TO_ATSIGN= | ||
DEVICE_NAME= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
alias dev := build-dev | ||
alias strict := build-strict | ||
alias vald := valgrind-docker | ||
alias memd := memcheck-docker | ||
alias shutdown := graceful-shutdown | ||
|
||
set dotenv-filename := "just.env" | ||
set dotenv-load | ||
|
||
postfix := quote(os() + "-" + arch()) | ||
dev_dir := quote(justfile_directory() / "build/dev-") + postfix | ||
strict_dir := quote(justfile_directory() / "build/strict-") + postfix | ||
|
||
# SETUP COMMANDS | ||
|
||
setup: configure-dev | ||
[ -f "$PWD/compile_commands.json" ] && rm $PWD/compile_commands.json || true | ||
ln -s {{dev_dir}}/compile_commands.json $PWD/ | ||
|
||
clean: | ||
rm -rf $PWD/build | ||
rm $PWD/compile_commands.json | ||
|
||
# INSTALL COMMANDS | ||
|
||
install: build-dev | ||
cmake --build {{dev_dir}} --target install | ||
|
||
# BUILD COMMANDS | ||
|
||
build-dev: configure-dev | ||
cmake --build {{ dev_dir }} | ||
|
||
build-strict: configure-strict | ||
cmake --build {{ strict_dir }} | ||
|
||
dev-docker +ARGS='': | ||
docker run --rm --platform linux/amd64 \ | ||
--mount type=bind,src=$PWD,dst=/mnt/workdir \ | ||
--mount type=bind,src=$HOME/.atsign/keys,dst=/root/.atsign/keys \ | ||
atsigncompany/valgrind:latest \ | ||
just build-dev | ||
|
||
strict-docker +ARGS='': | ||
docker run --rm --platform linux/amd64 \ | ||
--mount type=bind,src=$PWD,dst=/mnt/workdir \ | ||
--mount type=bind,src=$HOME/.atsign/keys,dst=/root/.atsign/keys \ | ||
atsigncompany/valgrind:latest \ | ||
just build-strict | ||
|
||
memcheck +ARGS='': build-dev | ||
#!/usr/bin/env bash | ||
file="valgrind-sshnpd-$(date +%s).log" | ||
valgrind --log-file="$file" \ | ||
--tool=memcheck \ | ||
--leak-check=full \ | ||
--show-leak-kinds=all \ | ||
{{dev_dir}}/sshnpd/sshnpd -a $TO_ATSIGN -m $FROM_ATSIGN -d $DEVICE_NAME \ | ||
--permit-open "*:*" -v | ||
echo "memcheck log saved to: $file" | ||
# CONTAINER COMMANDS | ||
|
||
graceful-shutdown: | ||
{{dev_dir}}/graceful-shutdown-tool/graceful-shutdown-tool \ | ||
$FROM_ATSIGN $TO_ATSIGN $DEVICE_NAME | ||
|
||
memcheck-docker +ARGS='': | ||
docker run --rm --platform linux/amd64 -ti \ | ||
--network=host \ | ||
--mount type=bind,src=$PWD,dst=/mnt/workdir \ | ||
--mount type=bind,src=$HOME/.atsign/keys,dst=/root/.atsign/keys \ | ||
atsigncompany/valgrind:latest \ | ||
/bin/bash -c "mkdir /root/.ssh/; touch /root/.ssh/authorized_keys; export USER=root; export HOME=/root; just memcheck {{ARGS}}" | ||
|
||
valgrind-docker: | ||
docker run --rm --platform linux/amd64 -ti \ | ||
--network=host \ | ||
--mount type=bind,src=$PWD,dst=/mnt/workdir \ | ||
--mount type=bind,src=$HOME/.atsign/keys,dst=/root/.atsign/keys \ | ||
atsigncompany/valgrind:latest \ | ||
/bin/bash -c "export USER=root; export HOME=/root; /bin/bash --login" | ||
|
||
# CONFIGURE COMMANDS | ||
|
||
configure-dev: | ||
cmake -B {{ dev_dir }} \ | ||
-S $PWD \ | ||
-G "$GENERATOR" \ | ||
--preset dev \ | ||
-DCMAKE_INSTALL_PREFIX="$HOME/.local/" \ | ||
-DCMAKE_BUILD_TYPE=Debug \ | ||
-DCMAKE_C_COMPILER=$C_COMPILER \ | ||
-DSSHNPD_ENABLE_TESTING_SHUTDOWN_NOTIFICATION=ON | ||
|
||
configure-strict: | ||
cmake -B {{ dev_dir }} \ | ||
-S $PWD \ | ||
-G "$GENERATOR" \ | ||
--preset dev \ | ||
-DCMAKE_INSTALL_PREFIX="$HOME/.local/" \ | ||
-DCMAKE_BUILD_TYPE=Debug \ | ||
-DCMAKE_C_COMPILER=$C_COMPILER | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.