mirror of
https://github.com/google/pebble.git
synced 2025-08-16 03:16:34 -04:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
48
third_party/nanopb/tests/common/SConscript
vendored
Normal file
48
third_party/nanopb/tests/common/SConscript
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
# Build the common files needed by multiple test cases
|
||||
|
||||
Import('env')
|
||||
|
||||
# Protocol definitions for the encode/decode_unittests
|
||||
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()
|
||||
strict.Append(CFLAGS = strict['CORECFLAGS'])
|
||||
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")
|
||||
|
||||
malloc_env.Object("malloc_wrappers.o", "malloc_wrappers.c")
|
||||
malloc_env.Depends("$NANOPB/pb.h", ["malloc_wrappers_syshdr.h", "malloc_wrappers.h"])
|
||||
|
||||
Export("malloc_env")
|
||||
|
189
third_party/nanopb/tests/common/malloc_wrappers.c
vendored
Normal file
189
third_party/nanopb/tests/common/malloc_wrappers.c
vendored
Normal file
|
@ -0,0 +1,189 @@
|
|||
/* The wrapper functions in this file work like regular malloc() and free(),
|
||||
* but store check values before and after the allocation. This helps to catch
|
||||
* any buffer overrun errors in the test cases.
|
||||
*/
|
||||
|
||||
#include "malloc_wrappers.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define GUARD_SIZE (sizeof(size_t)*3)
|
||||
#define PREFIX_SIZE (sizeof(size_t)*2)
|
||||
#define CHECK1 ((size_t)0xDEADBEEF)
|
||||
#define CHECK2 ((size_t)0x600DCAFE)
|
||||
|
||||
#ifndef MAX_ALLOC_BYTES
|
||||
#define MAX_ALLOC_BYTES 16*1024*1024
|
||||
#endif
|
||||
|
||||
#ifndef DEBUG_MALLOC
|
||||
#define DEBUG_MALLOC 0
|
||||
#endif
|
||||
|
||||
static size_t g_alloc_count = 0;
|
||||
static size_t g_alloc_bytes = 0;
|
||||
static size_t g_max_alloc_bytes = MAX_ALLOC_BYTES;
|
||||
|
||||
#ifdef LLVMFUZZER
|
||||
/* LLVM libsanitizer has a realloc() implementation that always copies
|
||||
* the whole memory block, even if there would be space to expand it in
|
||||
* place. This gets pretty slow when fuzzing, so this wrapper limits the
|
||||
* realloc() calls by rounding allocation size upwards. Real world
|
||||
* realloc() implementations are hopefully smarter. */
|
||||
static size_t round_blocksize(size_t size)
|
||||
{
|
||||
if (size < 256)
|
||||
{
|
||||
return size;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (size + 1023) / 1024 * 1024;
|
||||
}
|
||||
}
|
||||
#else
|
||||
static size_t round_blocksize(size_t size)
|
||||
{
|
||||
return size;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Allocate memory and place check values before and after. */
|
||||
void* malloc_with_check(size_t size)
|
||||
{
|
||||
char *buf = NULL;
|
||||
|
||||
if (size <= g_max_alloc_bytes - g_alloc_bytes)
|
||||
{
|
||||
buf = malloc(round_blocksize(size + GUARD_SIZE));
|
||||
}
|
||||
|
||||
if (buf)
|
||||
{
|
||||
((size_t*)buf)[0] = size;
|
||||
((size_t*)buf)[1] = CHECK1;
|
||||
((size_t*)(buf + size))[2] = CHECK2;
|
||||
g_alloc_count++;
|
||||
g_alloc_bytes += size;
|
||||
if (DEBUG_MALLOC) fprintf(stderr, "Alloc 0x%04x/%u\n", (unsigned)(uintptr_t)(buf + PREFIX_SIZE), (unsigned)size);
|
||||
return buf + PREFIX_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DEBUG_MALLOC) fprintf(stderr, "malloc(%u) failed\n", (unsigned)size);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Free memory allocated with malloc_with_check() and do the checks. */
|
||||
void free_with_check(void *mem)
|
||||
{
|
||||
if (mem)
|
||||
{
|
||||
char *buf = (char*)mem - PREFIX_SIZE;
|
||||
size_t size = ((size_t*)buf)[0];
|
||||
if (DEBUG_MALLOC) fprintf(stderr, "Release 0x%04x/%u\n", (unsigned)(uintptr_t)mem, (unsigned)size);
|
||||
assert(((size_t*)buf)[1] == CHECK1);
|
||||
assert(((size_t*)(buf + size))[2] == CHECK2);
|
||||
assert(g_alloc_count > 0);
|
||||
assert(g_alloc_bytes >= size);
|
||||
((size_t*)buf)[1] = 0;
|
||||
((size_t*)(buf + size))[2] = 0;
|
||||
g_alloc_count--;
|
||||
g_alloc_bytes -= size;
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* Reallocate block and check / write guard values */
|
||||
void* realloc_with_check(void *ptr, size_t size)
|
||||
{
|
||||
if (!ptr && size)
|
||||
{
|
||||
/* Allocate new block and write guard values */
|
||||
return malloc_with_check(size);
|
||||
}
|
||||
else if (ptr && size)
|
||||
{
|
||||
/* Change block size */
|
||||
char *buf = (char*)ptr - PREFIX_SIZE;
|
||||
size_t oldsize = ((size_t*)buf)[0];
|
||||
assert(((size_t*)buf)[1] == CHECK1);
|
||||
assert(((size_t*)(buf + oldsize))[2] == CHECK2);
|
||||
assert(g_alloc_count > 0);
|
||||
assert(g_alloc_bytes >= oldsize);
|
||||
|
||||
if (size <= g_max_alloc_bytes - (g_alloc_bytes - oldsize))
|
||||
{
|
||||
size_t new_rounded = round_blocksize(size + GUARD_SIZE);
|
||||
size_t old_rounded = round_blocksize(oldsize + GUARD_SIZE);
|
||||
|
||||
if (new_rounded != old_rounded)
|
||||
{
|
||||
buf = realloc(buf, new_rounded);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
buf = NULL;
|
||||
}
|
||||
|
||||
if (!buf)
|
||||
{
|
||||
if (DEBUG_MALLOC) fprintf(stderr, "Realloc 0x%04x/%u to %u failed\n", (unsigned)(uintptr_t)ptr, (unsigned)oldsize, (unsigned)size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
((size_t*)buf)[0] = size;
|
||||
((size_t*)buf)[1] = CHECK1;
|
||||
((size_t*)(buf + size))[2] = CHECK2;
|
||||
g_alloc_bytes -= oldsize;
|
||||
g_alloc_bytes += size;
|
||||
|
||||
if (DEBUG_MALLOC) fprintf(stderr, "Realloc 0x%04x/%u to 0x%04x/%u\n", (unsigned)(uintptr_t)ptr, (unsigned)oldsize, (unsigned)(uintptr_t)(buf + PREFIX_SIZE), (unsigned)size);
|
||||
return buf + PREFIX_SIZE;
|
||||
}
|
||||
else if (ptr && !size)
|
||||
{
|
||||
/* Deallocate */
|
||||
free_with_check(ptr);
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No action */
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return total number of allocations not yet released */
|
||||
size_t get_alloc_count()
|
||||
{
|
||||
return g_alloc_count;
|
||||
}
|
||||
|
||||
/* Return allocated size for a pointer returned from malloc(). */
|
||||
size_t get_allocation_size(const void *mem)
|
||||
{
|
||||
char *buf = (char*)mem - PREFIX_SIZE;
|
||||
return ((size_t*)buf)[0];
|
||||
}
|
||||
|
||||
/* Get total number of allocated bytes */
|
||||
size_t get_alloc_bytes()
|
||||
{
|
||||
return g_alloc_bytes;
|
||||
}
|
||||
|
||||
/* Set limit for allocation size */
|
||||
void set_max_alloc_bytes(size_t max_bytes)
|
||||
{
|
||||
g_max_alloc_bytes = max_bytes;
|
||||
}
|
||||
|
||||
size_t get_max_alloc_bytes()
|
||||
{
|
||||
return g_max_alloc_bytes;
|
||||
}
|
10
third_party/nanopb/tests/common/malloc_wrappers.h
vendored
Normal file
10
third_party/nanopb/tests/common/malloc_wrappers.h
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
void* malloc_with_check(size_t size);
|
||||
void free_with_check(void *mem);
|
||||
void* realloc_with_check(void *ptr, size_t size);
|
||||
size_t get_alloc_count();
|
||||
size_t get_allocation_size(const void *mem);
|
||||
size_t get_alloc_bytes();
|
||||
void set_max_alloc_bytes(size_t max_bytes);
|
||||
size_t get_max_alloc_bytes();
|
15
third_party/nanopb/tests/common/malloc_wrappers_syshdr.h
vendored
Normal file
15
third_party/nanopb/tests/common/malloc_wrappers_syshdr.h
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
/* This is just a wrapper in order to get our own malloc wrappers into nanopb core. */
|
||||
|
||||
#define pb_realloc(ptr,size) realloc_with_check(ptr,size)
|
||||
#define pb_free(ptr) free_with_check(ptr)
|
||||
|
||||
#ifdef PB_OLD_SYSHDR
|
||||
#include PB_OLD_SYSHDR
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#include <malloc_wrappers.h>
|
22
third_party/nanopb/tests/common/person.proto
vendored
Normal file
22
third_party/nanopb/tests/common/person.proto
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
syntax = "proto2";
|
||||
|
||||
import "nanopb.proto";
|
||||
|
||||
message Person {
|
||||
required string name = 1 [(nanopb).max_size = 40];
|
||||
required int32 id = 2;
|
||||
optional string email = 3 [(nanopb).max_size = 40];
|
||||
|
||||
enum PhoneType {
|
||||
MOBILE = 0;
|
||||
HOME = 1;
|
||||
WORK = 2;
|
||||
}
|
||||
|
||||
message PhoneNumber {
|
||||
required string number = 1 [(nanopb).max_size = 40];
|
||||
optional PhoneType type = 2 [default = HOME];
|
||||
}
|
||||
|
||||
repeated PhoneNumber phone = 4 [(nanopb).max_count = 5];
|
||||
}
|
17
third_party/nanopb/tests/common/test_helpers.h
vendored
Normal file
17
third_party/nanopb/tests/common/test_helpers.h
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
/* Compatibility helpers for the test programs. */
|
||||
|
||||
#ifndef _TEST_HELPERS_H_
|
||||
#define _TEST_HELPERS_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
|
||||
|
||||
#else
|
||||
#define SET_BINARY_MODE(file)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
43
third_party/nanopb/tests/common/unittestproto.proto
vendored
Normal file
43
third_party/nanopb/tests/common/unittestproto.proto
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
syntax = "proto2";
|
||||
|
||||
import 'nanopb.proto';
|
||||
|
||||
message IntegerArray {
|
||||
repeated int32 data = 1 [(nanopb).max_count = 10];
|
||||
}
|
||||
|
||||
message FloatArray {
|
||||
repeated float data = 1 [(nanopb).max_count = 10];
|
||||
}
|
||||
|
||||
message StringMessage {
|
||||
required string data = 1 [(nanopb).max_length = 10];
|
||||
}
|
||||
|
||||
message BytesMessage {
|
||||
required bytes data = 1 [(nanopb).max_size = 16];
|
||||
}
|
||||
|
||||
message CallbackArray {
|
||||
// We cheat a bit and use this message for testing other types, too.
|
||||
// Nanopb does not care about the actual defined data type for callback
|
||||
// fields.
|
||||
repeated int32 data = 1;
|
||||
}
|
||||
|
||||
message IntegerContainer {
|
||||
required IntegerArray submsg = 1;
|
||||
}
|
||||
|
||||
message CallbackContainer {
|
||||
required CallbackArray submsg = 1;
|
||||
}
|
||||
|
||||
message CallbackContainerContainer {
|
||||
required CallbackContainer submsg = 1;
|
||||
}
|
||||
|
||||
message StringPointerContainer {
|
||||
repeated string rep_str = 1 [(nanopb).type = FT_POINTER];
|
||||
}
|
||||
|
26
third_party/nanopb/tests/common/unittests.h
vendored
Normal file
26
third_party/nanopb/tests/common/unittests.h
vendored
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#ifdef UNITTESTS_SHORT_MSGS
|
||||
/* Short debug messages for platforms with limited memory */
|
||||
#define COMMENT(x) printf("\n----" x "----\n");
|
||||
#define TEST(x) \
|
||||
if (!(x)) { \
|
||||
fprintf(stderr, "FAIL: Line %d\n", __LINE__); \
|
||||
status = 1; \
|
||||
} else { \
|
||||
printf("OK: Line %d\n", __LINE__); \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
/* Elaborate debug messages for normal development */
|
||||
#define COMMENT(x) printf("\n----" x "----\n");
|
||||
#define TEST(x) \
|
||||
if (!(x)) { \
|
||||
fprintf(stderr, "\033[31;1mFAILED:\033[22;39m %s:%d %s\n", __FILE__, __LINE__, #x); \
|
||||
status = 1; \
|
||||
} else { \
|
||||
printf("\033[32;1mOK:\033[22;39m %s\n", #x); \
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue