Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mem,btrace: fix struct alignment #372

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ endif()

if(CMAKE_C_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wshorten-64-to-32)
# Ensure struct mem is aligned (used as fat pointer)
set_source_files_properties(src/mem/mem.c PROPERTIES COMPILE_FLAGS -Wpadded)
endif()


Expand Down
4 changes: 2 additions & 2 deletions include/re_btrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

struct btrace {
void *stack[BTRACE_SZ];
int len;
size_t len;
};

int btrace_print(struct re_printf *pf, struct btrace *btrace);
Expand All @@ -20,7 +20,7 @@ static inline int btrace(struct btrace *btrace)
if (!btrace)
return EINVAL;

btrace->len = (int)backtrace(btrace->stack, BTRACE_SZ);
btrace->len = backtrace(btrace->stack, BTRACE_SZ);

return 0;
}
Expand Down
10 changes: 7 additions & 3 deletions src/btrace/btrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,30 @@ static int print_debug(struct re_printf *pf, struct btrace *btrace,
if (!btrace->len)
return 0;

#if defined(FREEBSD) || defined(OPENBSD)
symbols = backtrace_symbols(btrace->stack, btrace->len);
#else
symbols = backtrace_symbols(btrace->stack, (int)btrace->len);
#endif

if (!symbols)
return 0;

switch (type) {
case BTRACE_CSV:
for (int j = 0; j < btrace->len; j++) {
for (size_t j = 0; j < btrace->len; j++) {
re_hprintf(pf, "%s%s", symbols[j],
((j + 1) < btrace->len) ? ", " : "");
}
break;
case BTRACE_NEWLINE:
for (int j = 0; j < btrace->len; j++) {
for (size_t j = 0; j < btrace->len; j++) {
re_hprintf(pf, "%s \n", symbols[j]);
}
break;
case BTRACE_JSON:
re_hprintf(pf, "[");
for (int j = 0; j < btrace->len; j++) {
for (size_t j = 0; j < btrace->len; j++) {
re_hprintf(pf, "\"%s\"%s", symbols[j],
((j + 1) < btrace->len) ? ", " : "");
}
Expand Down
12 changes: 6 additions & 6 deletions src/mem/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@

/** Defines a reference-counting memory object */
struct mem {
uint32_t nrefs; /**< Number of references */
mem_destroy_h *dh; /**< Destroy handler */
size_t nrefs; /**< Number of references */
mem_destroy_h *dh; /**< Destroy handler */
#if MEM_DEBUG
struct le le; /**< Linked list element */
uint32_t magic; /**< Magic number */
size_t magic; /**< Magic number */
size_t size; /**< Size of memory object */
struct le le; /**< Linked list element */
struct btrace btraces; /**< Backtrace array */
#endif
};

#if MEM_DEBUG
/* Memory debugging */
static struct list meml = LIST_INIT;
static const uint32_t mem_magic = 0xe7fb9ac4;
static const size_t mem_magic = 0xe7fb9ac4;
static ssize_t threshold = -1; /**< Memory threshold, disabled by default */

static struct memstat memstat = {
Expand Down Expand Up @@ -349,7 +349,7 @@ uint32_t mem_nrefs(const void *data)

MAGIC_CHECK(m);

return m->nrefs;
return (uint32_t)m->nrefs;
}


Expand Down