From 56bc1d7945b578de24116867bc2761a1b547d1cf Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 27 May 2017 10:18:34 +1000 Subject: [PATCH] Add PUFFS_MAGIC. --- cmd/internal/cgen/cgen.go | 9 +++++++++ gen/c/gif.c | 9 +++++++++ gen/h/gif.h | 1 + test/c/gif/basic.c | 33 +++++++++++++++++++-------------- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cmd/internal/cgen/cgen.go b/cmd/internal/cgen/cgen.go index 66a5472b..8560be42 100644 --- a/cmd/internal/cgen/cgen.go +++ b/cmd/internal/cgen/cgen.go @@ -68,6 +68,12 @@ func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byt } b.WriteString("// ---------------- Constructor and Destructor Implementations\n\n") + b.WriteString("// PUFFS_MAGIC is a magic number to check that constructors are called. It's\n") + b.WriteString("// not foolproof, given C doesn't automatically zero memory before use, but it\n") + b.WriteString("// should catch 99.99% of cases.\n") + b.WriteString("//\n") + b.WriteString("// Its (non-zero) value is arbitrary, based on md5sum(\"puffs\").\n") + b.WriteString("#define PUFFS_MAGIC (0xCB3699CCU)\n\n") for _, f := range files { for _, n := range f.TopLevelDecls() { if n.Kind() == a.KStruct { @@ -86,6 +92,7 @@ func writeStruct(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error fmt.Fprintf(b, "typedef struct {\n") if n.Suspendible() { fmt.Fprintf(b, "puffs_%s_status status;\n", pkgName) + fmt.Fprintf(b, "uint32_t magic;\n") } for _, f := range n.Fields() { if err := writeField(b, m, f.Field()); err != nil { @@ -131,6 +138,8 @@ func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) er fmt.Fprintf(b, "}\n") b.WriteString("memset(self, 0, sizeof(*self));\n") + b.WriteString("self->magic = PUFFS_MAGIC;\n") + // TODO: set any non-zero default values. } diff --git a/gen/c/gif.c b/gen/c/gif.c index 7211b8d0..ac019af6 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -46,6 +46,7 @@ typedef enum { typedef struct { puffs_gif_status status; + uint32_t magic; uint32_t f_literal_width; uint8_t f_stack[4096]; uint8_t f_suffixes[4096]; @@ -65,6 +66,13 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); // ---------------- Constructor and Destructor Implementations +// PUFFS_MAGIC is a magic number to check that constructors are called. It's +// not foolproof, given C doesn't automatically zero memory before use, but it +// should catch 99.99% of cases. +// +// Its (non-zero) value is arbitrary, based on md5sum("puffs"). +#define PUFFS_MAGIC (0xCB3699CCU) + void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, uint32_t puffs_version) { if (puffs_version != PUFFS_VERSION) { @@ -72,6 +80,7 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, return; } memset(self, 0, sizeof(*self)); + self->magic = PUFFS_MAGIC; } void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {} diff --git a/gen/h/gif.h b/gen/h/gif.h index 1be6ee4a..b53cec70 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -46,6 +46,7 @@ typedef enum { typedef struct { puffs_gif_status status; + uint32_t magic; uint32_t f_literal_width; uint8_t f_stack[4096]; uint8_t f_suffixes[4096]; diff --git a/test/c/gif/basic.c b/test/c/gif/basic.c index 3f35517d..66794c93 100644 --- a/test/c/gif/basic.c +++ b/test/c/gif/basic.c @@ -12,29 +12,34 @@ done Each edition should print "PASS", amongst other information, and exit(0). */ -#include "../testlib/testlib.c" - #include "../../../gen/c/gif.c" +#include "../testlib/testlib.c" const char* test_filename = "gif/basic.c"; -void test_default_literal_width() { +void test_puffs_version_bad() { puffs_gif_lzw_decoder dec; - puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION); - if (dec.f_literal_width != 0) { - FAIL("test_default_literal_width: got %d, want %d", dec.f_literal_width, 0); + puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION. + if (dec.status != puffs_gif_error_bad_version) { + FAIL("test_puffs_version_bad: status: got %d, want %d", dec.status, + puffs_gif_error_bad_version); goto cleanup0; } cleanup0: puffs_gif_lzw_decoder_destructor(&dec); } -void test_puffs_version_bad() { +void test_puffs_version_good() { puffs_gif_lzw_decoder dec; - puffs_gif_lzw_decoder_constructor(&dec, 0); // 0 is not PUFFS_VERSION. - if (dec.status != puffs_gif_error_bad_version) { - FAIL("test_puffs_version_bad: got %d, want %d", dec.status, - puffs_gif_error_bad_version); + puffs_gif_lzw_decoder_constructor(&dec, PUFFS_VERSION); + if (dec.magic != PUFFS_MAGIC) { + FAIL("test_puffs_version_good: magic: got %u, want %u", dec.magic, + PUFFS_MAGIC); + goto cleanup0; + } + if (dec.f_literal_width != 0) { + FAIL("test_puffs_version_good: f_literal_width: got %u, want %u", + dec.f_literal_width, 0); goto cleanup0; } cleanup0: @@ -43,7 +48,7 @@ cleanup0: // The empty comments forces clang-format to place one element per line. test tests[] = { - test_default_literal_width, // - test_puffs_version_bad, // - NULL, // + test_puffs_version_bad, // + test_puffs_version_good, // + NULL, // };