From e44ff2de819ead77b00c7fb4ede75ada685ff099 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 9 Apr 2019 15:42:10 -0700 Subject: [PATCH] Remove assumption of uint64_t etc. in portable code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit C11 doesn’t guarantee the existence of types like uint64_t, so avoid these types in portable code, as it’s easy to do so. There’s no need to avoid the types in w32-specific code, since w32 is guaranteed to have them. * lib-src/make-fingerprint.c (main): * src/fingerprint-dummy.c: * src/fingerprint.h: * src/pdumper.c (dump_fingerprint, struct dump_header): Prefer unsigned char to uint8_t in portable code, as either will do. Put an "#include " in fingerprint.c files, so that the corresponding .o file is rebuilt after ./configure is run. * lib-src/make-fingerprint.c (main): Simplify loop. * src/Makefile.in (fingerprint.c): Update atomically. * src/pdumper.c: Omit unnecessary check that off_t is the same size as int32_t or int64_t, as the code does not rely on this assumption. (dump_off): Use int_least32_t, not int32_t. (struct dump_reloc): Use unsigned int, not uint32_t. (dump_anonymous_allocate_w32, dump_anonymous_allocate_posix) (dump_anonymous_allocate, dump_map_file_w32, dump_map_file_posix) (dump_map_file: Do the sanity checks at compile time, not at run-time, to avoid usage of uint64_t etc. on non-w32 platforms. --- lib-src/make-fingerprint.c | 12 ++--- src/Makefile.in | 4 +- src/fingerprint-dummy.c | 4 +- src/fingerprint.h | 4 +- src/pdumper.c | 93 ++++++++++++++------------------------ 5 files changed, 47 insertions(+), 70 deletions(-) diff --git a/lib-src/make-fingerprint.c b/lib-src/make-fingerprint.c index d310366442d..4bfeaa0742c 100644 --- a/lib-src/make-fingerprint.c +++ b/lib-src/make-fingerprint.c @@ -89,7 +89,7 @@ main (int argc, char **argv) fclose (f); } - uint8_t digest[32]; + unsigned char digest[32]; sha256_finish_ctx (&ctx, digest); if (raw) @@ -99,12 +99,12 @@ main (int argc, char **argv) } else { - printf ("#include \"fingerprint.h\"\n"); - printf ("\n"); - printf ("const uint8_t fingerprint[32] = { "); + puts ("#include \n" + "#include \"fingerprint.h\"\n" + "unsigned char const fingerprint[] = {"); for (int i = 0; i < 32; ++i) - printf ("%s0x%02X", i ? ", " : "", digest[i]); - printf (" };\n"); + printf ("\t0x%02X,\n", digest[i]); + puts ("};"); } return EXIT_SUCCESS; diff --git a/src/Makefile.in b/src/Makefile.in index 10b2da319b2..0613a0dbed4 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -629,7 +629,9 @@ $(libsrc)/make-fingerprint$(EXEEXT): $(libsrc)/make-fingerprint.c $(lib)/libgnu. $(MAKE) -C $(libsrc) make-fingerprint$(EXEEXT) fingerprint.c: temacs.in$(EXEEXT) $(libsrc)/make-fingerprint$(EXEEXT) - $(libsrc)/make-fingerprint$(EXEEXT) temacs.in$(EXEEXT) > fingerprint.c + $(AM_V_GEN)$(libsrc)/make-fingerprint$(EXEEXT) temacs.in$(EXEEXT) \ + >$@.tmp + $(AM_V_at)mv $@.tmp $@ ## We have to create $(etc) here because init_cmdargs tests its ## existence when setting Vinstallation_directory (FIXME?). diff --git a/src/fingerprint-dummy.c b/src/fingerprint-dummy.c index 1603519783e..04938bd1d08 100644 --- a/src/fingerprint-dummy.c +++ b/src/fingerprint-dummy.c @@ -17,7 +17,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Emacs. If not, see . */ +#include + #include "fingerprint.h" /* Dummy fingerprint to use as hash input. */ -const uint8_t fingerprint[32] = { 0 }; +unsigned char const fingerprint[32] = { 0 }; diff --git a/src/fingerprint.h b/src/fingerprint.h index 913b668b4e0..0b195fd0ca7 100644 --- a/src/fingerprint.h +++ b/src/fingerprint.h @@ -20,12 +20,10 @@ along with GNU Emacs. If not, see . */ #ifndef EMACS_FINGERPRINT_H #define EMACS_FINGERPRINT_H -#include - /* We generate fingerprint.c and fingerprint.o from all the sources in Emacs. This way, we have a unique value that we can use to pair data files (like a portable dump image) with a specific build of Emacs. */ -extern const uint8_t fingerprint[32]; +extern unsigned char const fingerprint[32]; #endif diff --git a/src/pdumper.c b/src/pdumper.c index 68c412d47cd..3aa941221db 100644 --- a/src/pdumper.c +++ b/src/pdumper.c @@ -123,8 +123,6 @@ verify (sizeof (intptr_t) == sizeof (ptrdiff_t)); verify (sizeof (void (*)(void)) == sizeof (void *)); verify (sizeof (ptrdiff_t) <= sizeof (Lisp_Object)); verify (sizeof (ptrdiff_t) <= sizeof (EMACS_INT)); -verify (sizeof (off_t) == sizeof (int32_t) - || sizeof (off_t) == sizeof (int64_t)); verify (CHAR_BIT == 8); #define DIVIDE_ROUND_UP(x, y) (((x) + (y) - 1) / (y)) @@ -145,9 +143,9 @@ static struct } remembered_data[32]; static int nr_remembered_data = 0; -typedef int32_t dump_off; -#define DUMP_OFF_MIN INT32_MIN -#define DUMP_OFF_MAX INT32_MAX +typedef int_least32_t dump_off; +#define DUMP_OFF_MIN INT_LEAST32_MIN +#define DUMP_OFF_MAX INT_LEAST32_MAX __attribute__((format (printf,1,2))) static void @@ -290,10 +288,10 @@ verify (DUMP_ALIGNMENT >= GCALIGNMENT); struct dump_reloc { - uint32_t raw_offset : DUMP_RELOC_OFFSET_BITS; + unsigned int raw_offset : DUMP_RELOC_OFFSET_BITS; ENUM_BF (dump_reloc_type) type : DUMP_RELOC_TYPE_BITS; }; -verify (sizeof (struct dump_reloc) == sizeof (int32_t)); +verify (sizeof (struct dump_reloc) == sizeof (dump_off)); /* Set the type of a dump relocation. @@ -323,7 +321,7 @@ dump_reloc_set_offset (struct dump_reloc *reloc, dump_off offset) } static void -dump_fingerprint (const char *label, const uint8_t *xfingerprint) +dump_fingerprint (const char *label, unsigned char const *xfingerprint) { fprintf (stderr, "%s: ", label); for (int i = 0; i < 32; ++i) @@ -354,7 +352,7 @@ struct dump_header char magic[sizeof (dump_magic)]; /* Associated Emacs binary. */ - uint8_t fingerprint[32]; + unsigned char fingerprint[32]; /* Relocation table for the dump file; each entry is a struct dump_reloc. */ @@ -4230,17 +4228,12 @@ enum dump_memory_protection DUMP_MEMORY_ACCESS_READWRITE = 3, }; +#if VM_SUPPORTED == VM_MS_WINDOWS static void * dump_anonymous_allocate_w32 (void *base, size_t size, enum dump_memory_protection protection) { -#if VM_SUPPORTED != VM_MS_WINDOWS - (void) base; - (void) size; - (void) protection; - emacs_abort (); -#else void *ret; DWORD mem_type; DWORD mem_prot; @@ -4269,26 +4262,22 @@ dump_anonymous_allocate_w32 (void *base, ? EBUSY : EPERM; return ret; -#endif } +#endif + +#if VM_SUPPORTED == VM_POSIX /* Old versions of macOS only define MAP_ANON, not MAP_ANONYMOUS. FIXME: This probably belongs elsewhere (gnulib/autoconf?) */ -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif +# ifndef MAP_ANONYMOUS +# define MAP_ANONYMOUS MAP_ANON +# endif static void * dump_anonymous_allocate_posix (void *base, size_t size, enum dump_memory_protection protection) { -#if VM_SUPPORTED != VM_POSIX - (void) base; - (void) size; - (void) protection; - emacs_abort (); -#else void *ret; int mem_prot; @@ -4333,8 +4322,8 @@ dump_anonymous_allocate_posix (void *base, if (ret == MAP_FAILED) ret = NULL; return ret; -#endif } +#endif /* Perform anonymous memory allocation. */ static void * @@ -4342,14 +4331,14 @@ dump_anonymous_allocate (void *base, const size_t size, enum dump_memory_protection protection) { - void *ret = NULL; - if (VM_SUPPORTED == VM_MS_WINDOWS) - ret = dump_anonymous_allocate_w32 (base, size, protection); - else if (VM_SUPPORTED == VM_POSIX) - ret = dump_anonymous_allocate_posix (base, size, protection); - else - errno = ENOSYS; - return ret; +#if VM_SUPPORTED == VM_POSIX + return dump_anonymous_allocate_posix (base, size, protection); +#elif VM_SUPPORTED == VM_MS_WINDOWS + return dump_anonymous_allocate_w32 (base, size, protection); +#else + errno = ENOSYS; + return NULL; +#endif } /* Undo the effect of dump_reserve_address_space(). */ @@ -4371,18 +4360,11 @@ dump_anonymous_release (void *addr, size_t size) #endif } +#if VM_SUPPORTED == VM_MS_WINDOWS static void * dump_map_file_w32 (void *base, int fd, off_t offset, size_t size, enum dump_memory_protection protection) { -#if VM_SUPPORTED != VM_MS_WINDOWS - (void) base; - (void) fd; - (void) offset; - (void) size; - (void) protection; - emacs_abort (); -#else void *ret = NULL; HANDLE section = NULL; HANDLE file; @@ -4437,21 +4419,14 @@ dump_map_file_w32 (void *base, int fd, off_t offset, size_t size, if (section && !CloseHandle (section)) emacs_abort (); return ret; -#endif } +#endif +#if VM_SUPPORTED == VM_POSIX static void * dump_map_file_posix (void *base, int fd, off_t offset, size_t size, enum dump_memory_protection protection) { -#if VM_SUPPORTED != VM_POSIX - (void) base; - (void) fd; - (void) offset; - (void) size; - (void) protection; - emacs_abort (); -#else void *ret; int mem_prot; int mem_flags; @@ -4481,22 +4456,22 @@ dump_map_file_posix (void *base, int fd, off_t offset, size_t size, if (ret == MAP_FAILED) ret = NULL; return ret; -#endif } +#endif /* Map a file into memory. */ static void * dump_map_file (void *base, int fd, off_t offset, size_t size, enum dump_memory_protection protection) { - void *ret = NULL; - if (VM_SUPPORTED == VM_MS_WINDOWS) - ret = dump_map_file_w32 (base, fd, offset, size, protection); - else if (VM_SUPPORTED == VM_POSIX) - ret = dump_map_file_posix (base, fd, offset, size, protection); - else - errno = ENOSYS; +#if VM_SUPPORTED == VM_POSIX + return dump_map_file_posix (base, fd, offset, size, protection); +#elif VM_SUPPORTED == VM_MS_WINDOWS + return dump_map_file_w32 (base, fd, offset, size, protection); +#else + errno = ENOSYS; return ret; +#endif } /* Remove a virtual memory mapping. -- 2.39.2