From ac837365be321df8443cafd4c89f256eefde3e1d Mon Sep 17 00:00:00 2001 From: Alex Jbanca Date: Mon, 8 May 2023 14:34:36 +0300 Subject: [PATCH] performance(sqlcipher): Fix burn_stack performance issue Implements a cross-platform version of https://github.com/OP-TEE/optee_os/pull/3102 1. Remove recursion 2. Use memset instead of while loop --- go.mod | 2 ++ go.sum | 4 ++-- .../github.com/mutecomm/go-sqlcipher/burn_stack.c | 4 +--- vendor/github.com/mutecomm/go-sqlcipher/zeromem.c | 15 +++++++++++---- vendor/modules.txt | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6edbde551..ced50b7f1 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ replace github.com/nfnt/resize => github.com/status-im/resize v0.0.0-20201215164 replace github.com/forPelevin/gomoji => github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4 +replace github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f => github.com/status-im/go-sqlcipher v0.1.0-status.1 + require ( github.com/anacrolix/torrent v1.41.0 github.com/beevik/ntp v0.3.0 diff --git a/go.sum b/go.sum index bf48d78ba..bddccbbe8 100644 --- a/go.sum +++ b/go.sum @@ -1551,8 +1551,6 @@ github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/n github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f h1:hd3r+uv9DNLScbOrnlj82rBldHQf3XWmCeXAWbw8euQ= -github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f/go.mod h1:MyUWrZlB1aI5bs7j9/pJ8ckLLZ4QcCYcNiSbsAW32D4= github.com/mutecomm/go-sqlcipher/v4 v4.4.0/go.mod h1:PyN04SaWalavxRGH9E8ZftG6Ju7rsPrGmQRjrEaVpiY= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= @@ -1993,6 +1991,8 @@ github.com/status-im/go-ethereum v1.10.25-status.6 h1:5YC8k1inTBqA6LpON0uX6y86ni github.com/status-im/go-ethereum v1.10.25-status.6/go.mod h1:Dt4K5JYMhJRdtXJwBEyGZLZn9iz/chSOZyjVmt5ZhwQ= github.com/status-im/go-multiaddr-ethv4 v1.2.4 h1:7fw0Y48TJXEqx4fOHlDOUiM/uBq9zG5w4x975Mjh4E0= github.com/status-im/go-multiaddr-ethv4 v1.2.4/go.mod h1:PDh4D7h5CvecPIy0ji0rLNwTnzzEcyz9uTPHD42VyH4= +github.com/status-im/go-sqlcipher v0.1.0-status.1 h1:5fkuM4FG3G5o754zFO9xErATxeig9pww+Nr6sVTUN4o= +github.com/status-im/go-sqlcipher v0.1.0-status.1/go.mod h1:MyUWrZlB1aI5bs7j9/pJ8ckLLZ4QcCYcNiSbsAW32D4= github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4 h1:CtobZoiNdHpx+xurFxnuJ1xsGm3oKMfcZkB3vmomJmA= github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4/go.mod h1:hmpnZzkzSZJbFYWAUkrPV8I36x7mdYiPhPqnALP4fKA= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= diff --git a/vendor/github.com/mutecomm/go-sqlcipher/burn_stack.c b/vendor/github.com/mutecomm/go-sqlcipher/burn_stack.c index 2610c060d..9cc982639 100644 --- a/vendor/github.com/mutecomm/go-sqlcipher/burn_stack.c +++ b/vendor/github.com/mutecomm/go-sqlcipher/burn_stack.c @@ -21,10 +21,8 @@ */ void burn_stack(unsigned long len) { - unsigned char buf[32]; + unsigned char buf[len]; zeromem(buf, sizeof(buf)); - if (len > (unsigned long)sizeof(buf)) - burn_stack(len - sizeof(buf)); } diff --git a/vendor/github.com/mutecomm/go-sqlcipher/zeromem.c b/vendor/github.com/mutecomm/go-sqlcipher/zeromem.c index 9dff602ab..118237232 100644 --- a/vendor/github.com/mutecomm/go-sqlcipher/zeromem.c +++ b/vendor/github.com/mutecomm/go-sqlcipher/zeromem.c @@ -9,12 +9,22 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" +#include /** @file zeromem.c Zero a block of memory, Tom St Denis */ +/* + * Pointer to memset is volatile so that compiler must de-reference + * the pointer and can't assume that it points to any function in + * particular (such as memset, which it then might further "optimize") + */ +typedef void *(*memset_t)(void *, int, size_t); + +static volatile memset_t memset_func = memset; + /** Zero a block of memory @param out The destination of the area to zero @@ -22,11 +32,8 @@ */ void zeromem(void *out, size_t outlen) { - unsigned char *mem = out; LTC_ARGCHKVD(out != NULL); - while (outlen-- > 0) { - *mem++ = 0; - } + memset_func((void *)out, 0, outlen); } /* $Source$ */ diff --git a/vendor/modules.txt b/vendor/modules.txt index 8c17bde19..e63b590b5 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -652,7 +652,7 @@ github.com/multiformats/go-multistream # github.com/multiformats/go-varint v0.0.7 ## explicit; go 1.18 github.com/multiformats/go-varint -# github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f +# github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f => github.com/status-im/go-sqlcipher v0.1.0-status.1 ## explicit; go 1.12 github.com/mutecomm/go-sqlcipher # github.com/nfnt/resize v0.0.0-00010101000000-000000000000 => github.com/status-im/resize v0.0.0-20201215164250-7c6d9f0d3088