[PATCH] Verify Size of Objcode Headers
Add static checks to make sure that the statically-generated object code headers in continuations.c, control.c, foreign.c, gsubr.c and smob.c are the same length as the struct scm_objcode data type in objcodes.h. --- libguile/continuations.c |4 libguile/control.c |3 +++ libguile/foreign.c |3 +++ libguile/gsubr.c |3 +++ libguile/objcodes.h |5 + libguile/smob.c |3 +++ 6 files changed, 21 insertions(+), 0 deletions(-) diff --git a/libguile/continuations.c b/libguile/continuations.c index dc504f0..e99043f 100644 --- a/libguile/continuations.c +++ b/libguile/continuations.c @@ -37,6 +37,7 @@ #include "libguile/eval.h" #include "libguile/vm.h" #include "libguile/instructions.h" +#include "libguile/objcodes.h" #include "libguile/validate.h" #include "libguile/continuations.h" @@ -537,6 +538,9 @@ SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0, void scm_init_continuations () { + scm_t_uint8 dummy[] = { OBJCODE_HEADER(5,5) }; + VERIFY_OBJCODE_HEADER_SIZE(dummy); + tc16_continuation = scm_make_smob_type ("continuation", 0); scm_set_smob_print (tc16_continuation, continuation_print); #include "libguile/continuations.x" diff --git a/libguile/control.c b/libguile/control.c index 6c20675..91fedf1 100644 --- a/libguile/control.c +++ b/libguile/control.c @@ -271,6 +271,9 @@ scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) void scm_init_control (void) { + scm_t_uint8 dummy[] = { OBJCODE_HEADER(5,5) }; + VERIFY_OBJCODE_HEADER_SIZE(dummy); + #include "libguile/control.x" } diff --git a/libguile/foreign.c b/libguile/foreign.c index aae4c67..60a01a1 100644 --- a/libguile/foreign.c +++ b/libguile/foreign.c @@ -1028,6 +1028,9 @@ scm_i_foreign_call (SCM foreign, const SCM *argv) static void scm_init_foreign (void) { + scm_t_uint8 dummy[] = { OBJCODE_HEADER }; + VERIFY_OBJCODE_HEADER_SIZE(dummy); + #ifndef SCM_MAGIC_SNARFER #include "libguile/foreign.x" #endif diff --git a/libguile/gsubr.c b/libguile/gsubr.c index de4bff6..c01471c 100644 --- a/libguile/gsubr.c +++ b/libguile/gsubr.c @@ -878,6 +878,9 @@ gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst) void scm_init_gsubr() { + scm_t_uint8 dummy[] = { OBJCODE_HEADER }; + VERIFY_OBJCODE_HEADER_SIZE(dummy); + #ifdef GSUBR_TEST scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */ #endif diff --git a/libguile/objcodes.h b/libguile/objcodes.h index 2bff9aa..cb86e63 100644 --- a/libguile/objcodes.h +++ b/libguile/objcodes.h @@ -21,6 +21,8 @@ #include +#include /* from Gnulib, in guile/lib */ + /* Objcode data should be directly mappable to this C structure. */ struct scm_objcode { @@ -54,6 +56,9 @@ struct scm_objcode #define SCM_OBJCODE_IS_BYTEVECTOR(x) (SCM_OBJCODE_FLAGS (x) & SCM_F_OBJCODE_IS_BYTEVECTOR) #define SCM_OBJCODE_IS_SLICE(x) (SCM_OBJCODE_FLAGS (x) & SCM_F_OBJCODE_IS_SLICE) +#define VERIFY_OBJCODE_HEADER_SIZE(header) verify(sizeof(header)\ + == sizeof(struct scm_objcode)) + SCM scm_c_make_objcode_slice (SCM parent, const scm_t_uint8 *ptr); SCM_API SCM scm_load_objcode (SCM file); SCM_API SCM scm_objcode_p (SCM obj); diff --git a/libguile/smob.c b/libguile/smob.c index 171db8d..3aba9de 100644 --- a/libguile/smob.c +++ b/libguile/smob.c @@ -598,6 +598,9 @@ void scm_smob_prehistory () { long i; + scm_t_uint8 dummy[] = { OBJCODE_HEADER }; + + VERIFY_OBJCODE_HEADER_SIZE(dummy); smob_gc_kind = GC_new_kind (GC_new_free_list (), GC_MAKE_PROC (GC_new_proc (smob_mark), 0), -- 1.7.1
Re: [PATCH] Verify Size of Objcode Headers
On Sun 20 Jun 2010 21:44, Noah Lavine writes: > Add static checks to make sure that the statically-generated object code > headers in continuations.c, control.c, foreign.c, gsubr.c and smob.c are the > same length as the struct scm_objcode data type in objcodes.h. I'd be happy to apply these. Would you be willing to assign copyright for your changes to Guile to the FSF? Reply privately and we can get the paperwork in motion. Cheers, Andy -- http://wingolog.org/
Re: [PATCH] Verify Size of Objcode Headers
Hi Noah, Thanks for the patch! Noah Lavine writes: > + scm_t_uint8 dummy[] = { OBJCODE_HEADER(5,5) }; > + VERIFY_OBJCODE_HEADER_SIZE(dummy); I think that: verify (sizeof (dummy) == sizeof (struct scm_objcode)); would be enough (see below). Perhaps the ‘SCM_UNUSED’ attribute is needed here. > --- a/libguile/objcodes.h > +++ b/libguile/objcodes.h > @@ -21,6 +21,8 @@ > > #include > > +#include /* from Gnulib, in guile/lib */ This is a public header, so it can’t use private Gnulib headers. > +#define VERIFY_OBJCODE_HEADER_SIZE(header) verify(sizeof(header)\ > + == sizeof(struct > scm_objcode)) This is a private macro, so it would have to start with ‘SCM_I_’. But again, we can just avoid it altogether. Besides, make sure to follow the GNU coding style (info "(standards) Writing C"), such as leaving a white space before an opening bracket and after a closing bracket. Would you like to post an updated patch? Thanks, Ludo’.
Re: [PATCH] Verify Size of Objcode Headers
Thanks for the corrections. I think this patch addresses them. Noah Add static checks to make sure that the statically-generated object code headers in continuations.c, control.c, foreign.c, gsubr.c and smob.c are the same length as the struct scm_objcode data type in objcodes.h. --- libguile/continuations.c |5 + libguile/control.c |4 libguile/foreign.c |4 libguile/gsubr.c |5 + libguile/smob.c |5 - 5 files changed, 22 insertions(+), 1 deletions(-) diff --git a/libguile/continuations.c b/libguile/continuations.c index dc504f0..0ded7e7 100644 --- a/libguile/continuations.c +++ b/libguile/continuations.c @@ -37,10 +37,12 @@ #include "libguile/eval.h" #include "libguile/vm.h" #include "libguile/instructions.h" +#include "libguile/objcodes.h" #include "libguile/validate.h" #include "libguile/continuations.h" +#include "verify.h" /* from Gnulib, in guile/lib */ static scm_t_bits tc16_continuation; @@ -537,6 +539,9 @@ SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0, void scm_init_continuations () { + scm_t_uint8 dummy[] = { OBJCODE_HEADER(5,5) } SCM_UNUSED; + verify (sizeof (dummy) == sizeof (struct scm_objcode)); + tc16_continuation = scm_make_smob_type ("continuation", 0); scm_set_smob_print (tc16_continuation, continuation_print); #include "libguile/continuations.x" diff --git a/libguile/control.c b/libguile/control.c index 6c20675..605b2f9 100644 --- a/libguile/control.c +++ b/libguile/control.c @@ -26,6 +26,7 @@ #include "libguile/instructions.h" #include "libguile/vm.h" +#include "verify.h" /* from Gnulib, in guile/lib */ @@ -271,6 +272,9 @@ scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) void scm_init_control (void) { + scm_t_uint8 dummy[] = { OBJCODE_HEADER(5,5) } SCM_UNUSED; + verify (sizeof (dummy) == sizeof (struct scm_objcode)); + #include "libguile/control.x" } diff --git a/libguile/foreign.c b/libguile/foreign.c index aae4c67..30dcf60 100644 --- a/libguile/foreign.c +++ b/libguile/foreign.c @@ -31,6 +31,7 @@ #include "libguile/instructions.h" #include "libguile/foreign.h" +#include "verify.h" /* from Gnulib, in guile/lib */ SCM_SYMBOL (sym_void, "void"); @@ -1028,6 +1029,9 @@ scm_i_foreign_call (SCM foreign, const SCM *argv) static void scm_init_foreign (void) { + scm_t_uint8 dummy[] = { OBJCODE_HEADER } SCM_UNUSED; + verify (sizeof (dummy) == sizeof (struct scm_objcode)); + #ifndef SCM_MAGIC_SNARFER #include "libguile/foreign.x" #endif diff --git a/libguile/gsubr.c b/libguile/gsubr.c index de4bff6..21580b7 100644 --- a/libguile/gsubr.c +++ b/libguile/gsubr.c @@ -33,6 +33,8 @@ #include "libguile/programs.h" #include "libguile/private-options.h" + +#include "verify.h" /* from Gnulib, in guile/lib */ /* * gsubr.c @@ -878,6 +880,9 @@ gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst) void scm_init_gsubr() { + scm_t_uint8 dummy[] = { OBJCODE_HEADER } SCM_UNUSED; + verify (sizeof (dummy) == sizeof (struct scm_objcode)); + #ifdef GSUBR_TEST scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */ #endif diff --git a/libguile/smob.c b/libguile/smob.c index 171db8d..676c83b 100644 --- a/libguile/smob.c +++ b/libguile/smob.c @@ -42,7 +42,7 @@ #include "libguile/bdw-gc.h" #include - +#include "verify.h" /* from Gnulib, in guile/lib */ /* scm_smobs scm_numsmob @@ -598,6 +598,9 @@ void scm_smob_prehistory () { long i; + scm_t_uint8 dummy[] = { OBJCODE_HEADER } SCM_UNUSED; + + verify (sizeof (dummy) == sizeof (struct scm_objcode)); smob_gc_kind = GC_new_kind (GC_new_free_list (), GC_MAKE_PROC (GC_new_proc (smob_mark), 0), -- 1.7.1