Commit e44ff2de authored by Paul Eggert's avatar Paul Eggert
Browse files

Remove assumption of uint64_t etc. in portable code

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 <config.h>" 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.
parent 44a39e3e
...@@ -89,7 +89,7 @@ main (int argc, char **argv) ...@@ -89,7 +89,7 @@ main (int argc, char **argv)
fclose (f); fclose (f);
} }
uint8_t digest[32]; unsigned char digest[32];
sha256_finish_ctx (&ctx, digest); sha256_finish_ctx (&ctx, digest);
if (raw) if (raw)
...@@ -99,12 +99,12 @@ main (int argc, char **argv) ...@@ -99,12 +99,12 @@ main (int argc, char **argv)
} }
else else
{ {
printf ("#include \"fingerprint.h\"\n"); puts ("#include <config.h>\n"
printf ("\n"); "#include \"fingerprint.h\"\n"
printf ("const uint8_t fingerprint[32] = { "); "unsigned char const fingerprint[] = {");
for (int i = 0; i < 32; ++i) for (int i = 0; i < 32; ++i)
printf ("%s0x%02X", i ? ", " : "", digest[i]); printf ("\t0x%02X,\n", digest[i]);
printf (" };\n"); puts ("};");
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
...@@ -629,7 +629,9 @@ $(libsrc)/make-fingerprint$(EXEEXT): $(libsrc)/make-fingerprint.c $(lib)/libgnu. ...@@ -629,7 +629,9 @@ $(libsrc)/make-fingerprint$(EXEEXT): $(libsrc)/make-fingerprint.c $(lib)/libgnu.
$(MAKE) -C $(libsrc) make-fingerprint$(EXEEXT) $(MAKE) -C $(libsrc) make-fingerprint$(EXEEXT)
fingerprint.c: temacs.in$(EXEEXT) $(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 ## We have to create $(etc) here because init_cmdargs tests its
## existence when setting Vinstallation_directory (FIXME?). ## existence when setting Vinstallation_directory (FIXME?).
......
...@@ -17,7 +17,9 @@ GNU General Public License for more details. ...@@ -17,7 +17,9 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "fingerprint.h" #include "fingerprint.h"
/* Dummy fingerprint to use as hash input. */ /* Dummy fingerprint to use as hash input. */
const uint8_t fingerprint[32] = { 0 }; unsigned char const fingerprint[32] = { 0 };
...@@ -20,12 +20,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ ...@@ -20,12 +20,10 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#ifndef EMACS_FINGERPRINT_H #ifndef EMACS_FINGERPRINT_H
#define EMACS_FINGERPRINT_H #define EMACS_FINGERPRINT_H
#include <stdint.h>
/* We generate fingerprint.c and fingerprint.o from all the sources in /* 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 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 data files (like a portable dump image) with a specific build of
Emacs. */ Emacs. */
extern const uint8_t fingerprint[32]; extern unsigned char const fingerprint[32];
#endif #endif
...@@ -123,8 +123,6 @@ verify (sizeof (intptr_t) == sizeof (ptrdiff_t)); ...@@ -123,8 +123,6 @@ verify (sizeof (intptr_t) == sizeof (ptrdiff_t));
verify (sizeof (void (*)(void)) == sizeof (void *)); verify (sizeof (void (*)(void)) == sizeof (void *));
verify (sizeof (ptrdiff_t) <= sizeof (Lisp_Object)); verify (sizeof (ptrdiff_t) <= sizeof (Lisp_Object));
verify (sizeof (ptrdiff_t) <= sizeof (EMACS_INT)); verify (sizeof (ptrdiff_t) <= sizeof (EMACS_INT));
verify (sizeof (off_t) == sizeof (int32_t)
|| sizeof (off_t) == sizeof (int64_t));
verify (CHAR_BIT == 8); verify (CHAR_BIT == 8);
#define DIVIDE_ROUND_UP(x, y) (((x) + (y) - 1) / (y)) #define DIVIDE_ROUND_UP(x, y) (((x) + (y) - 1) / (y))
...@@ -145,9 +143,9 @@ static struct ...@@ -145,9 +143,9 @@ static struct
} remembered_data[32]; } remembered_data[32];
static int nr_remembered_data = 0; static int nr_remembered_data = 0;
typedef int32_t dump_off; typedef int_least32_t dump_off;
#define DUMP_OFF_MIN INT32_MIN #define DUMP_OFF_MIN INT_LEAST32_MIN
#define DUMP_OFF_MAX INT32_MAX #define DUMP_OFF_MAX INT_LEAST32_MAX
__attribute__((format (printf,1,2))) __attribute__((format (printf,1,2)))
static void static void
...@@ -290,10 +288,10 @@ verify (DUMP_ALIGNMENT >= GCALIGNMENT); ...@@ -290,10 +288,10 @@ verify (DUMP_ALIGNMENT >= GCALIGNMENT);
struct dump_reloc 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; 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. /* Set the type of a dump relocation.
...@@ -323,7 +321,7 @@ dump_reloc_set_offset (struct dump_reloc *reloc, dump_off offset) ...@@ -323,7 +321,7 @@ dump_reloc_set_offset (struct dump_reloc *reloc, dump_off offset)
} }
static void static void
dump_fingerprint (const char *label, const uint8_t *xfingerprint) dump_fingerprint (const char *label, unsigned char const *xfingerprint)
{ {
fprintf (stderr, "%s: ", label); fprintf (stderr, "%s: ", label);
for (int i = 0; i < 32; ++i) for (int i = 0; i < 32; ++i)
...@@ -354,7 +352,7 @@ struct dump_header ...@@ -354,7 +352,7 @@ struct dump_header
char magic[sizeof (dump_magic)]; char magic[sizeof (dump_magic)];
/* Associated Emacs binary. */ /* Associated Emacs binary. */
uint8_t fingerprint[32]; unsigned char fingerprint[32];
/* Relocation table for the dump file; each entry is a /* Relocation table for the dump file; each entry is a
struct dump_reloc. */ struct dump_reloc. */
...@@ -4230,17 +4228,12 @@ enum dump_memory_protection ...@@ -4230,17 +4228,12 @@ enum dump_memory_protection
DUMP_MEMORY_ACCESS_READWRITE = 3, DUMP_MEMORY_ACCESS_READWRITE = 3,
}; };
#if VM_SUPPORTED == VM_MS_WINDOWS
static void * static void *
dump_anonymous_allocate_w32 (void *base, dump_anonymous_allocate_w32 (void *base,
size_t size, size_t size,
enum dump_memory_protection protection) enum dump_memory_protection protection)
{ {
#if VM_SUPPORTED != VM_MS_WINDOWS
(void) base;
(void) size;
(void) protection;
emacs_abort ();
#else
void *ret; void *ret;
DWORD mem_type; DWORD mem_type;
DWORD mem_prot; DWORD mem_prot;
...@@ -4269,26 +4262,22 @@ dump_anonymous_allocate_w32 (void *base, ...@@ -4269,26 +4262,22 @@ dump_anonymous_allocate_w32 (void *base,
? EBUSY ? EBUSY
: EPERM; : EPERM;
return ret; return ret;
#endif
} }
#endif
#if VM_SUPPORTED == VM_POSIX
/* Old versions of macOS only define MAP_ANON, not MAP_ANONYMOUS. /* Old versions of macOS only define MAP_ANON, not MAP_ANONYMOUS.
FIXME: This probably belongs elsewhere (gnulib/autoconf?) */ FIXME: This probably belongs elsewhere (gnulib/autoconf?) */
#ifndef MAP_ANONYMOUS # ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON # define MAP_ANONYMOUS MAP_ANON
#endif # endif
static void * static void *
dump_anonymous_allocate_posix (void *base, dump_anonymous_allocate_posix (void *base,
size_t size, size_t size,
enum dump_memory_protection protection) enum dump_memory_protection protection)
{ {
#if VM_SUPPORTED != VM_POSIX
(void) base;
(void) size;
(void) protection;
emacs_abort ();
#else
void *ret; void *ret;
int mem_prot; int mem_prot;
...@@ -4333,8 +4322,8 @@ dump_anonymous_allocate_posix (void *base, ...@@ -4333,8 +4322,8 @@ dump_anonymous_allocate_posix (void *base,
if (ret == MAP_FAILED) if (ret == MAP_FAILED)
ret = NULL; ret = NULL;
return ret; return ret;
#endif
} }
#endif
/* Perform anonymous memory allocation. */ /* Perform anonymous memory allocation. */
static void * static void *
...@@ -4342,14 +4331,14 @@ dump_anonymous_allocate (void *base, ...@@ -4342,14 +4331,14 @@ dump_anonymous_allocate (void *base,
const size_t size, const size_t size,
enum dump_memory_protection protection) enum dump_memory_protection protection)
{ {
void *ret = NULL; #if VM_SUPPORTED == VM_POSIX
if (VM_SUPPORTED == VM_MS_WINDOWS) return dump_anonymous_allocate_posix (base, size, protection);
ret = dump_anonymous_allocate_w32 (base, size, protection); #elif VM_SUPPORTED == VM_MS_WINDOWS
else if (VM_SUPPORTED == VM_POSIX) return dump_anonymous_allocate_w32 (base, size, protection);
ret = dump_anonymous_allocate_posix (base, size, protection); #else
else
errno = ENOSYS; errno = ENOSYS;
return ret; return NULL;
#endif
} }
/* Undo the effect of dump_reserve_address_space(). */ /* Undo the effect of dump_reserve_address_space(). */
...@@ -4371,18 +4360,11 @@ dump_anonymous_release (void *addr, size_t size) ...@@ -4371,18 +4360,11 @@ dump_anonymous_release (void *addr, size_t size)
#endif #endif
} }
#if VM_SUPPORTED == VM_MS_WINDOWS
static void * static void *
dump_map_file_w32 (void *base, int fd, off_t offset, size_t size, dump_map_file_w32 (void *base, int fd, off_t offset, size_t size,
enum dump_memory_protection protection) 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; void *ret = NULL;
HANDLE section = NULL; HANDLE section = NULL;
HANDLE file; HANDLE file;
...@@ -4437,21 +4419,14 @@ dump_map_file_w32 (void *base, int fd, off_t offset, size_t size, ...@@ -4437,21 +4419,14 @@ dump_map_file_w32 (void *base, int fd, off_t offset, size_t size,
if (section && !CloseHandle (section)) if (section && !CloseHandle (section))
emacs_abort (); emacs_abort ();
return ret; return ret;
#endif
} }
#endif
#if VM_SUPPORTED == VM_POSIX
static void * static void *
dump_map_file_posix (void *base, int fd, off_t offset, size_t size, dump_map_file_posix (void *base, int fd, off_t offset, size_t size,
enum dump_memory_protection protection) 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; void *ret;
int mem_prot; int mem_prot;
int mem_flags; int mem_flags;
...@@ -4481,22 +4456,22 @@ dump_map_file_posix (void *base, int fd, off_t offset, size_t size, ...@@ -4481,22 +4456,22 @@ dump_map_file_posix (void *base, int fd, off_t offset, size_t size,
if (ret == MAP_FAILED) if (ret == MAP_FAILED)
ret = NULL; ret = NULL;
return ret; return ret;
#endif
} }
#endif
/* Map a file into memory. */ /* Map a file into memory. */
static void * static void *
dump_map_file (void *base, int fd, off_t offset, size_t size, dump_map_file (void *base, int fd, off_t offset, size_t size,
enum dump_memory_protection protection) enum dump_memory_protection protection)
{ {
void *ret = NULL; #if VM_SUPPORTED == VM_POSIX
if (VM_SUPPORTED == VM_MS_WINDOWS) return dump_map_file_posix (base, fd, offset, size, protection);
ret = dump_map_file_w32 (base, fd, offset, size, protection); #elif VM_SUPPORTED == VM_MS_WINDOWS
else if (VM_SUPPORTED == VM_POSIX) return dump_map_file_w32 (base, fd, offset, size, protection);
ret = dump_map_file_posix (base, fd, offset, size, protection); #else
else
errno = ENOSYS; errno = ENOSYS;
return ret; return ret;
#endif
} }
/* Remove a virtual memory mapping. /* Remove a virtual memory mapping.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment