From 0f3c0f79bc83c3678c325f7e8e17dd031acaf77f Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Fri, 26 Dec 2014 17:34:45 +0200 Subject: Move malloc support to tests/common directory --- tests/SConstruct | 8 +++++++- tests/alltypes_pointer/SConscript | 31 ++++++++++------------------ tests/common/SConscript | 27 ++++++++++++++++++++++++ tests/fuzztest/SConscript | 41 +++++++++---------------------------- tests/io_errors_pointers/SConscript | 26 +++++------------------ 5 files changed, 60 insertions(+), 73 deletions(-) (limited to 'tests') diff --git a/tests/SConstruct b/tests/SConstruct index 1890670b..9092d4f8 100644 --- a/tests/SConstruct +++ b/tests/SConstruct @@ -136,12 +136,18 @@ elif 'g++' in env['CXX'] or 'gcc' in env['CXX']: env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers') elif 'cl' in env['CXX']: env.Append(CXXFLAGS = '/Zi /W2 /WX') - + # Now include the SConscript files from all subdirectories import os.path env['VARIANT_DIR'] = 'build' env['BUILD'] = '#' + env['VARIANT_DIR'] env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common' + +# Include common/SConscript first to make sure its exports are available +# to other SConscripts. +SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common') + for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'): + if str(subdir).startswith("common"): continue SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir))) diff --git a/tests/alltypes_pointer/SConscript b/tests/alltypes_pointer/SConscript index 8fcf197e..52856f6e 100644 --- a/tests/alltypes_pointer/SConscript +++ b/tests/alltypes_pointer/SConscript @@ -1,31 +1,22 @@ # Encode the AllTypes message using pointers for all fields, and verify the # output against the normal AllTypes test case. -Import("env") - -# We need our own pb_decode.o for the malloc support -env = env.Clone() -env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1}); - -# Disable libmudflap, because it will confuse valgrind -# and other memory leak detection tools. -if '-fmudflap' in env["CCFLAGS"]: - env["CCFLAGS"].remove("-fmudflap") - env["LINKFLAGS"].remove("-fmudflap") - env["LIBS"].remove("mudflap") - -strict = env.Clone() -strict.Append(CFLAGS = strict['CORECFLAGS']) -strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c") -strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c") -strict.Object("pb_common_with_malloc.o", "$NANOPB/pb_common.c") +Import("env", "malloc_env") c = Copy("$TARGET", "$SOURCE") env.Command("alltypes.proto", "#alltypes/alltypes.proto", c) env.NanopbProto(["alltypes", "alltypes.options"]) -enc = env.Program(["encode_alltypes_pointer.c", "alltypes.pb.c", "pb_encode_with_malloc.o", "pb_common_with_malloc.o"]) -dec = env.Program(["decode_alltypes_pointer.c", "alltypes.pb.c", "pb_decode_with_malloc.o", "pb_common_with_malloc.o"]) +enc = malloc_env.Program(["encode_alltypes_pointer.c", + "alltypes.pb.c", + "$COMMON/pb_encode_with_malloc.o", + "$COMMON/pb_common_with_malloc.o", + "$COMMON/malloc_wrappers.o"]) +dec = malloc_env.Program(["decode_alltypes_pointer.c", + "alltypes.pb.c", + "$COMMON/pb_decode_with_malloc.o", + "$COMMON/pb_common_with_malloc.o", + "$COMMON/malloc_wrappers.o"]) # Encode and compare results to non-pointer alltypes test case env.RunTest(enc) diff --git a/tests/common/SConscript b/tests/common/SConscript index 4581bea1..a202ca47 100644 --- a/tests/common/SConscript +++ b/tests/common/SConscript @@ -8,6 +8,7 @@ env.NanopbProto("unittestproto") # Protocol definitions for basic_buffer/stream tests env.NanopbProto("person") +#-------------------------------------------- # Binaries of the pb_decode.c and pb_encode.c # These are built using more strict warning flags. strict = env.Clone() @@ -16,6 +17,32 @@ strict.Object("pb_decode.o", "$NANOPB/pb_decode.c") strict.Object("pb_encode.o", "$NANOPB/pb_encode.c") strict.Object("pb_common.o", "$NANOPB/pb_common.c") +#----------------------------------------------- +# Binaries of pb_decode etc. with malloc support +# Uses malloc_wrappers.c to count allocations. +malloc_env = env.Clone() +malloc_env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1, + 'PB_SYSTEM_HEADER': '\\"malloc_wrappers_syshdr.h\\"'}) +malloc_env.Append(CPPPATH = ["$COMMON"]) + +if 'SYSHDR' in malloc_env: + malloc_env.Append(CPPDEFINES = {'PB_OLD_SYSHDR': malloc_env['SYSHDR']}) + +# Disable libmudflap, because it will confuse valgrind +# and other memory leak detection tools. +if '-fmudflap' in env["CCFLAGS"]: + malloc_env["CCFLAGS"].remove("-fmudflap") + malloc_env["LINKFLAGS"].remove("-fmudflap") + malloc_env["LIBS"].remove("mudflap") + +malloc_strict = malloc_env.Clone() +malloc_strict.Append(CFLAGS = malloc_strict['CORECFLAGS']) +malloc_strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c") +malloc_strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c") +malloc_strict.Object("pb_common_with_malloc.o", "$NANOPB/pb_common.c") + mw = env.Object("malloc_wrappers.o", "malloc_wrappers.c") Depends(mw, ["malloc_wrappers_syshdr.h"]) +Export("malloc_env") + diff --git a/tests/fuzztest/SConscript b/tests/fuzztest/SConscript index 346ccab8..35b697f9 100644 --- a/tests/fuzztest/SConscript +++ b/tests/fuzztest/SConscript @@ -1,30 +1,9 @@ # Run a fuzz test to verify robustness against corrupted/malicious data. -Import("env") - -# We need our own pb_decode.o for the malloc support -env = env.Clone() -env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1, - 'PB_SYSTEM_HEADER': '\\"malloc_wrappers_syshdr.h\\"'}) -env.Append(CPPPATH = [".", "$COMMON"]) - -if 'SYSHDR' in env: - env.Append(CPPDEFINES = {'PB_OLD_SYSHDR': env['SYSHDR']}) - -# Disable libmudflap, because it will confuse valgrind -# and other memory leak detection tools. -if '-fmudflap' in env["CCFLAGS"]: - env["CCFLAGS"].remove("-fmudflap") - env["LINKFLAGS"].remove("-fmudflap") - env["LIBS"].remove("mudflap") - -strict = env.Clone() -strict.Append(CFLAGS = strict['CORECFLAGS']) -strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c") -strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c") -strict.Object("pb_common_with_malloc.o", "$NANOPB/pb_common.c") +Import("env", "malloc_env") # We want both pointer and static versions of the AllTypes message +# Prefix them with package name. env.Command("alltypes_static.proto", "#alltypes/alltypes.proto", lambda target, source, env: open(str(target[0]), 'w').write("package alltypes_static;\n" @@ -36,22 +15,22 @@ env.Command("alltypes_pointer.proto", "#alltypes/alltypes.proto", p1 = env.NanopbProto(["alltypes_pointer", "alltypes_pointer.options"]) p2 = env.NanopbProto(["alltypes_static", "alltypes_static.options"]) -fuzz = env.Program(["fuzztest.c", +fuzz = malloc_env.Program(["fuzztest.c", "alltypes_pointer.pb.c", "alltypes_static.pb.c", - "pb_encode_with_malloc.o", - "pb_decode_with_malloc.o", - "pb_common_with_malloc.o", + "$COMMON/pb_encode_with_malloc.o", + "$COMMON/pb_decode_with_malloc.o", + "$COMMON/pb_common_with_malloc.o", "$COMMON/malloc_wrappers.o"]) env.RunTest(fuzz) -fuzzstub = env.Program(["fuzzstub.c", +fuzzstub = malloc_env.Program(["fuzzstub.c", "alltypes_pointer.pb.c", "alltypes_static.pb.c", - "pb_encode_with_malloc.o", - "pb_decode_with_malloc.o", - "pb_common_with_malloc.o", + "$COMMON/pb_encode_with_malloc.o", + "$COMMON/pb_decode_with_malloc.o", + "$COMMON/pb_common_with_malloc.o", "$COMMON/malloc_wrappers.o"]) diff --git a/tests/io_errors_pointers/SConscript b/tests/io_errors_pointers/SConscript index 0b96177f..8d23f603 100644 --- a/tests/io_errors_pointers/SConscript +++ b/tests/io_errors_pointers/SConscript @@ -1,23 +1,6 @@ # Simulate io errors when encoding and decoding -Import("env") - -# We need our own pb_decode.o for the malloc support -env = env.Clone() -env.Append(CPPDEFINES = {'PB_ENABLE_MALLOC': 1}); - -# Disable libmudflap, because it will confuse valgrind -# and other memory leak detection tools. -if '-fmudflap' in env["CCFLAGS"]: - env["CCFLAGS"].remove("-fmudflap") - env["LINKFLAGS"].remove("-fmudflap") - env["LIBS"].remove("mudflap") - -strict = env.Clone() -strict.Append(CFLAGS = strict['CORECFLAGS']) -strict.Object("pb_decode_with_malloc.o", "$NANOPB/pb_decode.c") -strict.Object("pb_encode_with_malloc.o", "$NANOPB/pb_encode.c") -strict.Object("pb_common_with_malloc.o", "$NANOPB/pb_common.c") +Import("env", "malloc_env") c = Copy("$TARGET", "$SOURCE") env.Command("alltypes.proto", "#alltypes/alltypes.proto", c) @@ -26,9 +9,10 @@ env.Command("io_errors.c", "#io_errors/io_errors.c", c) env.NanopbProto(["alltypes", "alltypes.options"]) ioerr = env.Program(["io_errors.c", "alltypes.pb.c", - "pb_encode_with_malloc.o", - "pb_decode_with_malloc.o", - "pb_common_with_malloc.o"]) + "$COMMON/pb_encode_with_malloc.o", + "$COMMON/pb_decode_with_malloc.o", + "$COMMON/pb_common_with_malloc.o", + "$COMMON/malloc_wrappers.o"]) # Run tests under valgrind if available valgrind = env.WhereIs('valgrind') -- cgit 1.2.3-korg