diff --git a/StaticMemory/README.md b/StaticMemory/README.md new file mode 100644 index 0000000..2c9ae64 --- /dev/null +++ b/StaticMemory/README.md @@ -0,0 +1,15 @@ +This all is not needed with global heap hints introduced in PR + (https://github.com/wolfSSL/wolfssl/pull/7478). For older versions of wolfSSL + and wolfSSH though WMALLOC_USER can be used to call a custom heap allocator + function that uses a set heap hint. + +This example will use a custom global heap hint in the SCP client by remapping + WMALLOC/WFREE calls. + +build.sh is a script to help build the example `./build.sh`. + +Once built run the exmaple wolfSSH echoserver from another terminal: + +`./examples/echoserver/echoserver -j ./keys/hansel-key-rsa.der` + +Then run the example scp client produced from `./build.sh` diff --git a/StaticMemory/build.sh b/StaticMemory/build.sh new file mode 100755 index 0000000..dbe8721 --- /dev/null +++ b/StaticMemory/build.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Clone and build wolfSSL +if [ ! -d wolfssl ]; then + git clone --depth=1 git@github.com:wolfssl/wolfssl +fi +cd wolfssl && ./autogen.sh && ./configure --enable-ssh --enable-staticmemory CPPFLAGS="-DWOLFSSL_NO_MALLOC -DWOLFSSL_DEBUG_MEMORY -DWOLFMEM_BUCKETS=64,128,256,1024,2048,3456,8192,16000,34000" --disable-examples --disable-crypttests --prefix=$PWD/../wolfssl-install/ && make install +cd .. + +# Clone and build wolfSSH +if [ ! -d wolfssh ]; then + git clone --depth=1 git@github.com:wolfssl/wolfssh +fi +cd wolfssh +patch -p1 < ../scp-client-static-memory.patch +./autogen.sh && ./configure CPPFLAGS="-DWMALLOC_USER -DDEFAULT_WINDOW_SZ=4096" --with-wolfssl=$PWD/../wolfssl-install/ --prefix=$PWD/../wolfssl-install/ --disable-examples --enable-scp --enable-debug && make src/libwolfssh.la +cd .. + +# Build modified example +gcc -DDEBUG_WOLFSSH -DWOLFSSH_SCP -DWMALLOC_USER -I$PWD/wolfssl-install/include -I$PWD/wolfssh/ wolfssh/examples/scpclient/scpclient.c wolfssh/examples/client/common.c -L$PWD/wolfssl-install/lib -L$PWD/wolfssh/src/.libs -lwolfssl -lwolfssh -o static-memory-scp-client + + +echo "" +echo "Done" +echo "Run the example application with :" +echo "head -c 1M /tmp/test.txt" +echo "" +echo "LD_LIBRARY_PATH=$PWD/wolfssl-install/lib:$PWD/wolfssh/src/.libs ./static-memory-scp-client valgrind ./static-memory-scp-client -H 127.0.0.1 -p 22222 -u jill -L /tmp/test.txt:/tmp/test-copy.txt -P upthehill" + +exit 0 + diff --git a/StaticMemory/scp-client-static-memory.patch b/StaticMemory/scp-client-static-memory.patch new file mode 100644 index 0000000..e661d30 --- /dev/null +++ b/StaticMemory/scp-client-static-memory.patch @@ -0,0 +1,109 @@ +diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c +index 980b74a..fa71204 100644 +--- a/examples/scpclient/scpclient.c ++++ b/examples/scpclient/scpclient.c +@@ -86,6 +86,35 @@ static void ShowUsage(void) + } + + ++WOLFSSL_HEAP_HINT *wolfSSH_heap = NULL; ++byte memory[250000]; ++ ++void* myMalloc(int size, void* heap, int type, const char* func, int line) ++{ ++ void* ret = NULL; ++ ret = XMALLOC(size, wolfSSH_heap, type); ++ fprintf(stderr, "wolfSSH_ALLOC %p -> %d bytes from %s line %d\n", ++ ret, size, func, line); ++ if (ret == NULL) { ++ fprintf(stderr, "Failed to malloc for size %d\n", size); ++ } ++ return ret; ++} ++ ++void myFree(void* ptr, void* heap, int type, const char* func, int line) ++{ ++ fprintf(stderr, "wolfSSH_FREE %p from %s line %d\n", ++ ptr, func, line); ++ XFREE(ptr, wolfSSH_heap, type); ++} ++ ++void* myRealloc(void* ptr, int size, void* heap, int type) ++{ ++ fprintf(stderr, "wolfSSH REALLOC[%p]: %d bytes\n", ++ ptr, size); ++ return XREALLOC(ptr, size, wolfSSH_heap, type); ++} ++ + enum copyDir {copyNone, copyToSrv, copyFromSrv}; + + +@@ -237,7 +266,7 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) + err_sys("Error setting public key"); + } + +- ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL); ++ ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, wolfSSH_heap); + if (ctx == NULL) + err_sys("Couldn't create wolfSSH client context."); + +@@ -309,8 +338,10 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) + } + } while (ret == WS_WANT_READ || ret == WS_WANT_WRITE || + ret == WS_CHAN_RXD || ret == WS_REKEYING); +- if (ret != WS_SUCCESS) ++ if (ret != WS_SUCCESS) { ++ fprintf(stderr, "Error %d\n", ret); + err_sys("Couldn't copy the file."); ++ } + + ret = wolfSSH_shutdown(ssh); + /* do not continue on with shutdown process if peer already disconnected */ +@@ -353,6 +384,13 @@ int main(int argc, char* argv[]) + args.return_code = 0; + args.user_auth = NULL; + ++ /* First load up static memory */ ++ if (wc_LoadStaticMemory(&wolfSSH_heap, memory, sizeof(memory), ++ WOLFMEM_GENERAL, 1) != 0) { ++ fprintf(stderr, "unable to load static memory"); ++ return 1; ++ } ++ + #ifdef DEBUG_WOLFSSH + wolfSSH_Debugging_ON(); + #endif +diff --git a/src/internal.c b/src/internal.c +index 6444596..52fe4ee 100644 +--- a/src/internal.c ++++ b/src/internal.c +@@ -993,7 +993,8 @@ WOLFSSH* SshInit(WOLFSSH* ssh, WOLFSSH_CTX* ctx) + handshake = HandshakeInfoNew(heap); + rng = (WC_RNG*)WMALLOC(sizeof(WC_RNG), heap, DYNTYPE_RNG); + +- if (handshake == NULL || rng == NULL || wc_InitRng(rng) != 0) { ++ if (handshake == NULL || rng == NULL || ++ wc_InitRng_ex(rng, heap, INVALID_DEVID) != 0) { + + WLOG(WS_LOG_DEBUG, "SshInit: Cannot allocate memory.\n"); + WFREE(handshake, heap, DYNTYPE_HS); +diff --git a/wolfssh/settings.h b/wolfssh/settings.h +index 12e7527..5eb5998 100644 +--- a/wolfssh/settings.h ++++ b/wolfssh/settings.h +@@ -47,6 +47,15 @@ extern "C" { + #endif + + ++/* these could be added to user_settings.h if building with ++ * WOLFSSL_USER_SETTINGS */ ++#define WMALLOC(s, h, t) myMalloc((s), (h), (t), __func__, __LINE__) ++#define WFREE(p, h, t) myFree((p), (h), (t), __func__, __LINE__) ++#define WREALLOC(p, n, h, t) myRealloc((p), (n), (h), (t)) ++void* myMalloc(int size, void* heap, int type, const char* func, int line); ++void myFree(void* ptr, void* heap, int type, const char* func, int line); ++void* myRealloc(void* ptr, int size, void* heap, int type); ++ + /* user settings here */ + #ifndef WMALLOC_USER + #define USE_WOLFSSH_MEMORY /* default memory handlers */