Skip to content

Commit

Permalink
Add stack trace macros to all functions.
Browse files Browse the repository at this point in the history
Low-level functions only include stack trace in test builds while higher-level functions ship with stack trace built-in. Stack traces include all parameters passed to the function but production builds only create the parameter list when the log level is set high enough, i.e. debug or trace depending on the function.
  • Loading branch information
dwsteele committed May 18, 2018
1 parent abb9651 commit 52bc073
Show file tree
Hide file tree
Showing 141 changed files with 6,482 additions and 1,172 deletions.
4 changes: 4 additions & 0 deletions doc/xml/release.xml
Expand Up @@ -25,6 +25,10 @@
</release-improvement-list>

<release-development-list>
<release-item>
<p>Add stack trace macros to all functions. Low-level functions only include stack trace in test builds while higher-level functions ship with stack trace built-in. Stack traces include all parameters passed to the function but production builds only create the parameter list when the log level is set high enough, i.e. <id>debug</id> or <id>trace</id> depending on the function.</p>
</release-item>

<release-item>
<p>Build <path>libc</path> using links rather than referencing the C files in <path>src</path> directly. The C library builds with different options which should not be reused for the C binary or vice versa.</p>
</release-item>
Expand Down
16 changes: 7 additions & 9 deletions libc/Makefile.PL
Expand Up @@ -15,7 +15,10 @@ use ExtUtils::MakeMaker;
use File::Basename qw(dirname);

use lib dirname($0) . '/lib';
use lib dirname($0) . '/build/lib';

use pgBackRest::LibCAuto;
use pgBackRestLibC::BuildParam;

####################################################################################################################################
# Storage object to use for all file operations
Expand Down Expand Up @@ -77,6 +80,7 @@ my @stryCFile =
'cipher/cipher.c',
'cipher/random.c',
'command/command.c',
'common/debug.c',
'common/encode.c',
'common/encode/base64.c',
'common/error.c',
Expand All @@ -86,7 +90,9 @@ my @stryCFile =
'common/log.c',
'common/memContext.c',
'common/regExp.c',
'common/stackTrace.c',
'common/time.c',
'common/type/convert.c',
'common/type/buffer.c',
'common/type/keyValue.c',
'common/type/list.c',
Expand Down Expand Up @@ -142,15 +148,7 @@ WriteMakefile
VERSION_FROM => 'lib/' . BACKREST_NAME . '/LibC.pm',
AUTHOR => 'David Steele <david@pgbackrest.org>',

CCFLAGS => join(' ', qw(
-Wfatal-errors -Wall -Wextra -Wwrite-strings -Wno-clobbered -Wno-missing-field-initializers
-o $@
-std=c99
-D_FILE_OFFSET_BITS=64
-funroll-loops
-ftree-vectorize
$(CFLAGS)
)),
CCFLAGS => join(' ', buildParamCCAll()),

INC => join(' ', qw(
-I.
Expand Down
46 changes: 46 additions & 0 deletions libc/build/lib/pgBackRestLibC/BuildParam.pm
@@ -0,0 +1,46 @@
####################################################################################################################################
# Parameters used by LibC builds
####################################################################################################################################
package pgBackRestLibC::BuildParam;

use strict;
use warnings FATAL => qw(all);
use Carp qw(confess);
use English '-no_match_vars';

use Exporter qw(import);
our @EXPORT = qw();

####################################################################################################################################
# All CC params used for a debug build
####################################################################################################################################
sub buildParamCCDebug
{
return qw(
-Wfatal-errors -Wall -Wextra -Wwrite-strings -Wno-clobbered -Wno-missing-field-initializers
-o $@
-std=c99
-D_FILE_OFFSET_BITS=64
$(CFLAGS));
}

push @EXPORT, qw(buildParamCCDebug);

####################################################################################################################################
# All CC params used for a production build
####################################################################################################################################
sub buildParamCCAll
{
my @stryParams = buildParamCCDebug;

push(@stryParams, qw(
-DNDEBUG
-funroll-loops
-ftree-vectorize));

return @stryParams;
}

push @EXPORT, qw(buildParamCCAll);

1;
5 changes: 4 additions & 1 deletion src/Makefile
Expand Up @@ -59,17 +59,20 @@ SRCS = \
command/archive/push/push.c \
command/help/help.c \
command/command.c \
common/fork.c \
common/debug.c \
common/error.c \
common/exit.c \
common/fork.c \
common/io/handle.c \
common/ini.c \
common/lock.c \
common/log.c \
common/memContext.c \
common/regExp.c \
common/stackTrace.c \
common/time.c \
common/type/buffer.c \
common/type/convert.c \
common/type/keyValue.c \
common/type/list.c \
common/type/string.c \
Expand Down
55 changes: 51 additions & 4 deletions src/cipher/block.c
Expand Up @@ -6,6 +6,7 @@ Block Cipher
#include <openssl/evp.h>
#include <openssl/err.h>

#include "common/debug.h"
#include "common/memContext.h"
#include "cipher/block.h"
#include "cipher/random.h"
Expand Down Expand Up @@ -45,6 +46,18 @@ New block encrypt/decrypt object
CipherBlock *
cipherBlockNew(CipherMode mode, const char *cipherName, const unsigned char *pass, size_t passSize, const char *digestName)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(ENUM, mode);
FUNCTION_DEBUG_PARAM(STRINGZ, cipherName);
FUNCTION_DEBUG_PARAM(UCHARP, pass);
FUNCTION_DEBUG_PARAM(SIZE, passSize);
FUNCTION_DEBUG_PARAM(STRINGZ, digestName);

FUNCTION_DEBUG_ASSERT(cipherName != NULL);
FUNCTION_DEBUG_ASSERT(pass != NULL);
FUNCTION_DEBUG_ASSERT(passSize > 0);
FUNCTION_DEBUG_END();

// Only need to init once.
if (!cipherIsInit())
cipherInit();
Expand Down Expand Up @@ -90,7 +103,7 @@ cipherBlockNew(CipherMode mode, const char *cipherName, const unsigned char *pas
}
MEM_CONTEXT_NEW_END();

return this;
FUNCTION_DEBUG_RESULT(CIPHER_BLOCK, this);
}

/***********************************************************************************************************************************
Expand All @@ -99,14 +112,21 @@ Determine how large the destination buffer should be
size_t
cipherBlockProcessSize(CipherBlock *this, size_t sourceSize)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(CIPHER_BLOCK, this);
FUNCTION_DEBUG_PARAM(SIZE, sourceSize);

FUNCTION_DEBUG_ASSERT(this != NULL);
FUNCTION_DEBUG_END();

// Destination size is source size plus one extra block
size_t destinationSize = sourceSize + EVP_MAX_BLOCK_LENGTH;

// On encrypt the header size must be included before the first block
if (this->mode == cipherModeEncrypt && !this->saltDone)
destinationSize += CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN;

return destinationSize;
FUNCTION_DEBUG_RESULT(SIZE, destinationSize);
}

/***********************************************************************************************************************************
Expand All @@ -115,6 +135,17 @@ Encrypt/decrypt data
size_t
cipherBlockProcess(CipherBlock *this, const unsigned char *source, size_t sourceSize, unsigned char *destination)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(CIPHER_BLOCK, this);
FUNCTION_DEBUG_PARAM(UCHARP, source);
FUNCTION_DEBUG_PARAM(SIZE, sourceSize);
FUNCTION_DEBUG_PARAM(UCHARP, destination);

FUNCTION_DEBUG_ASSERT(this != NULL);
FUNCTION_DEBUG_ASSERT(source != NULL);
FUNCTION_DEBUG_ASSERT(destination != NULL);
FUNCTION_DEBUG_END();

// Actual destination size
size_t destinationSize = 0;

Expand Down Expand Up @@ -212,7 +243,7 @@ cipherBlockProcess(CipherBlock *this, const unsigned char *source, size_t source
}

// Return actual destination size
return destinationSize;
FUNCTION_DEBUG_RESULT(SIZE, destinationSize);
}

/***********************************************************************************************************************************
Expand All @@ -221,6 +252,14 @@ Flush the remaining data
size_t
cipherBlockFlush(CipherBlock *this, unsigned char *destination)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(CIPHER_BLOCK, this);
FUNCTION_DEBUG_PARAM(UCHARP, destination);

FUNCTION_DEBUG_ASSERT(this != NULL);
FUNCTION_DEBUG_ASSERT(destination != NULL);
FUNCTION_DEBUG_END();

// Actual destination size
size_t destinationSize = 0;

Expand All @@ -233,7 +272,7 @@ cipherBlockFlush(CipherBlock *this, unsigned char *destination)
THROW(CipherError, "unable to flush");

// Return actual destination size
return destinationSize;
FUNCTION_DEBUG_RESULT(SIZE, destinationSize);
}

/***********************************************************************************************************************************
Expand All @@ -242,10 +281,18 @@ Free memory
void
cipherBlockFree(CipherBlock *this)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(CIPHER_BLOCK, this);

FUNCTION_DEBUG_ASSERT(this != NULL);
FUNCTION_DEBUG_END();

// Free cipher context
if (this->cipherContext)
EVP_CIPHER_CTX_cleanup(this->cipherContext);

// Free mem context
memContextFree(this->memContext);

FUNCTION_DEBUG_RESULT_VOID();
}
8 changes: 8 additions & 0 deletions src/cipher/block.h
Expand Up @@ -21,4 +21,12 @@ size_t cipherBlockProcess(CipherBlock *this, const unsigned char *source, size_t
size_t cipherBlockFlush(CipherBlock *this, unsigned char *destination);
void cipherBlockFree(CipherBlock *this);

/***********************************************************************************************************************************
Macros for function logging
***********************************************************************************************************************************/
#define FUNCTION_DEBUG_CIPHER_BLOCK_TYPE \
CipherBlock *
#define FUNCTION_DEBUG_CIPHER_BLOCK_FORMAT(value, buffer, bufferSize) \
objToLog(value, "CipherBlock", buffer, bufferSize)

#endif
26 changes: 7 additions & 19 deletions src/cipher/cipher.c
Expand Up @@ -6,6 +6,7 @@ Cipher General Init and Free
#include <openssl/err.h>

#include "cipher/cipher.h"
#include "common/debug.h"

/***********************************************************************************************************************************
Flag to indicate if OpenSSL has already been initialized
Expand All @@ -18,13 +19,17 @@ Initialize ciphers
void
cipherInit()
{
FUNCTION_DEBUG_VOID(logLevelTrace);

if (!cipherInitDone)
{
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();

cipherInitDone = true;
}

FUNCTION_DEBUG_RESULT_VOID();
}

/***********************************************************************************************************************************
Expand All @@ -33,23 +38,6 @@ Have the ciphers been initialized?
bool
cipherIsInit()
{
return cipherInitDone;
}

/***********************************************************************************************************************************
Free ciphers
***********************************************************************************************************************************/
void
cipherFree()
{
if (cipherInitDone)
{
FIPS_mode_set(0);
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
ERR_remove_thread_state(NULL);
ERR_free_strings();

cipherInitDone = false;
}
FUNCTION_TEST_VOID();
FUNCTION_TEST_RESULT(BOOL, cipherInitDone);
}
1 change: 0 additions & 1 deletion src/cipher/cipher.h
Expand Up @@ -20,6 +20,5 @@ Functions
***********************************************************************************************************************************/
void cipherInit();
bool cipherIsInit();
void cipherFree();

#endif
12 changes: 12 additions & 0 deletions src/cipher/random.c
Expand Up @@ -4,12 +4,24 @@ Cipher
#include <openssl/rand.h>

#include "cipher/random.h"
#include "common/debug.h"
#include "common/error.h"

/***********************************************************************************************************************************
Generate random bytes
***********************************************************************************************************************************/
void
randomBytes(unsigned char *buffer, size_t size)
{
FUNCTION_DEBUG_BEGIN(logLevelTrace);
FUNCTION_DEBUG_PARAM(UCHARP, buffer);
FUNCTION_DEBUG_PARAM(SIZE, size);

FUNCTION_DEBUG_ASSERT(buffer != NULL);
FUNCTION_DEBUG_ASSERT(size > 0);
FUNCTION_DEBUG_END();

RAND_bytes(buffer, (int)size);

FUNCTION_DEBUG_RESULT_VOID();
}

0 comments on commit 52bc073

Please sign in to comment.