avmpack.c

Include dependency graph for avmpack.c:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "5" [label="atom.h" tooltip="atom.h"] "7" [label="atom_table.h" tooltip="atom_table.h"] "1" [label="/__w/AtomVM/AtomVM/src/libAtomVM/avmpack.c" tooltip="/__w/AtomVM/AtomVM/src/libAtomVM/avmpack.c" fillcolor="#BFBFBF"] "2" [label="avmpack.h" tooltip="avmpack.h"] "13" [label="erl_nif.h" tooltip="erl_nif.h"] "17" [label="ets.h" tooltip="ets.h"] "3" [label="globalcontext.h" tooltip="globalcontext.h"] "24" [label="intn.h" tooltip="intn.h"] "19" [label="list.h" tooltip="list.h"] "28" [label="mailbox.h" tooltip="mailbox.h"] "25" [label="memory.h" tooltip="memory.h"] "26" [label="refc_binary.h" tooltip="refc_binary.h"] "27" [label="resources.h" tooltip="resources.h"] "20" [label="smp.h" tooltip="smp.h"] "18" [label="synclist.h" tooltip="synclist.h"] "21" [label="term.h" tooltip="term.h"] "14" [label="term_typedef.h" tooltip="term_typedef.h"] "29" [label="timer_list.h" tooltip="timer_list.h"] "9" [label="utils.h" tooltip="utils.h"] "30" [label="valueshashtable.h" tooltip="valueshashtable.h"] "15" [label="assert.h" tooltip="assert.h"] "10" [label="inttypes.h" tooltip="inttypes.h"] "16" [label="limits.h" tooltip="limits.h"] "8" [label="stdbool.h" tooltip="stdbool.h"] "11" [label="stddef.h" tooltip="stddef.h"] "4" [label="stdint.h" tooltip="stdint.h"] "12" [label="stdio.h" tooltip="stdio.h"] "6" [label="stdlib.h" tooltip="stdlib.h"] "23" [label="string.h" tooltip="string.h"] "22" [label="sys/types.h" tooltip="sys/types.h"] "5" -> "4" [dir=forward tooltip="include"] "5" -> "6" [dir=forward tooltip="include"] "7" -> "8" [dir=forward tooltip="include"] "7" -> "5" [dir=forward tooltip="include"] "7" -> "9" [dir=forward tooltip="include"] "1" -> "2" [dir=forward tooltip="include"] "1" -> "9" [dir=forward tooltip="include"] "1" -> "6" [dir=forward tooltip="include"] "1" -> "23" [dir=forward tooltip="include"] "1" -> "12" [dir=forward tooltip="include"] "2" -> "3" [dir=forward tooltip="include"] "2" -> "19" [dir=forward tooltip="include"] "2" -> "8" [dir=forward tooltip="include"] "2" -> "11" [dir=forward tooltip="include"] "2" -> "4" [dir=forward tooltip="include"] "13" -> "14" [dir=forward tooltip="include"] "17" -> "8" [dir=forward tooltip="include"] "17" -> "18" [dir=forward tooltip="include"] "17" -> "21" [dir=forward tooltip="include"] "3" -> "4" [dir=forward tooltip="include"] "3" -> "5" [dir=forward tooltip="include"] "3" -> "7" [dir=forward tooltip="include"] "3" -> "13" [dir=forward tooltip="include"] "3" -> "17" [dir=forward tooltip="include"] "3" -> "19" [dir=forward tooltip="include"] "3" -> "28" [dir=forward tooltip="include"] "3" -> "20" [dir=forward tooltip="include"] "3" -> "18" [dir=forward tooltip="include"] "3" -> "21" [dir=forward tooltip="include"] "3" -> "29" [dir=forward tooltip="include"] "3" -> "30" [dir=forward tooltip="include"] "24" -> "8" [dir=forward tooltip="include"] "24" -> "23" [dir=forward tooltip="include"] "24" -> "9" [dir=forward tooltip="include"] "19" -> "8" [dir=forward tooltip="include"] "28" -> "8" [dir=forward tooltip="include"] "28" -> "19" [dir=forward tooltip="include"] "28" -> "14" [dir=forward tooltip="include"] "28" -> "9" [dir=forward tooltip="include"] "25" -> "4" [dir=forward tooltip="include"] "25" -> "6" [dir=forward tooltip="include"] "25" -> "13" [dir=forward tooltip="include"] "25" -> "14" [dir=forward tooltip="include"] "25" -> "9" [dir=forward tooltip="include"] "26" -> "8" [dir=forward tooltip="include"] "26" -> "6" [dir=forward tooltip="include"] "26" -> "19" [dir=forward tooltip="include"] "26" -> "27" [dir=forward tooltip="include"] "27" -> "6" [dir=forward tooltip="include"] "27" -> "13" [dir=forward tooltip="include"] "27" -> "19" [dir=forward tooltip="include"] "27" -> "28" [dir=forward tooltip="include"] "27" -> "25" [dir=forward tooltip="include"] "27" -> "18" [dir=forward tooltip="include"] "20" -> "8" [dir=forward tooltip="include"] "18" -> "12" [dir=forward tooltip="include"] "18" -> "19" [dir=forward tooltip="include"] "18" -> "20" [dir=forward tooltip="include"] "21" -> "22" [dir=forward tooltip="include"] "21" -> "8" [dir=forward tooltip="include"] "21" -> "4" [dir=forward tooltip="include"] "21" -> "12" [dir=forward tooltip="include"] "21" -> "6" [dir=forward tooltip="include"] "21" -> "23" [dir=forward tooltip="include"] "21" -> "5" [dir=forward tooltip="include"] "21" -> "24" [dir=forward tooltip="include"] "21" -> "25" [dir=forward tooltip="include"] "21" -> "26" [dir=forward tooltip="include"] "21" -> "27" [dir=forward tooltip="include"] "21" -> "9" [dir=forward tooltip="include"] "21" -> "14" [dir=forward tooltip="include"] "14" -> "15" [dir=forward tooltip="include"] "14" -> "16" [dir=forward tooltip="include"] "14" -> "10" [dir=forward tooltip="include"] "14" -> "4" [dir=forward tooltip="include"] "29" -> "8" [dir=forward tooltip="include"] "29" -> "4" [dir=forward tooltip="include"] "29" -> "19" [dir=forward tooltip="include"] "9" -> "10" [dir=forward tooltip="include"] "9" -> "8" [dir=forward tooltip="include"] "9" -> "11" [dir=forward tooltip="include"] "9" -> "12" [dir=forward tooltip="include"] "9" -> "6" [dir=forward tooltip="include"] "30" -> "11" [dir=forward tooltip="include"] "30" -> "4" [dir=forward tooltip="include"] }

Defines

AVMPACK_SIZE 24
AVMPACK_SECTION_HEADER_SIZE 12
AVMPACK_MIN_SECTION_SIZE 16
AVMPACK_END_MARKER_SIZE 16

Enums

enum AVMPackSectionKind

Values:

enumerator AVMPackSectionInvalid
enumerator AVMPackSectionRegular
enumerator AVMPackSectionEnd

Functions

static inline size_t pad(size_t size)
bool avmpack_is_valid(const void *avmpack_binary, uint32_t size)

Returns true if the pointed binary starts with a valid AVM Pack header.

Checks the 24-byte magic header only; see avmpack_is_complete and avmpack_compute_size for truncation.

Parameters:
  • avmpack_binary – a pointer to an AVM Pack binary.

  • size – the size of AVM Pack binary.

Returns:

true if it is a valid AVM Pack binary, false otherwise.

static bool is_end_marker_name(const char *name)
static enum AVMPackSectionKind read_section(const void *avmpack_binary, uint32_t avmpack_size, uint32_t offset, struct AVMPackSection *section)
int avmpack_find_section_by_flag(const void *avmpack_binary, uint32_t avmpack_size, uint32_t flags_mask, uint32_t flags_value, const void **ptr, uint32_t *size, const char **name)

Finds an AVM Pack section that has certain flags set.

Finds an AVM Pack section that has certain flags set and returns a pointer to it, its size and its name. The scan is bounded by avmpack_size.

Parameters:
  • avmpack_binary – a pointer to valid AVM Pack file data.

  • avmpack_size – the size in bytes of the AVM Pack.

  • flags_mask – that will be matched against file sections.

  • flags_value – that will be matched against file sections.

  • ptr – [out] will point to the found file section.

  • size – [out] the payload size at ptr, without the section header and the padded name; not updated if the section has not been found.

  • name – [out] the section name, as defined in the module header.

Returns:

1 if the file section has been found, 0 otherwise.

int avmpack_find_section_by_name(const void *avmpack_binary, uint32_t avmpack_size, const char *name, const void **ptr, uint32_t *size)

Finds an AVM Pack section that has certain name.

Finds an AVM Pack section with a certain name and returns a pointer to it and its size. The scan is bounded by avmpack_size.

Parameters:
  • avmpack_binary – a pointer to valid AVM Pack file data.

  • avmpack_size – the size in bytes of the AVM Pack.

  • name – the file section name that will be searched.

  • ptr – [out] will point to the found file section; not updated if the section has not been found.

  • size – [out] the payload size at ptr, without the section header and the padded name; not updated if the section has not been found.

Returns:

1 if the file section has been found, 0 otherwise.

void *avmpack_fold(void *accum, const void *avmpack_binary, uint32_t avmpack_size, avmpack_fold_fun fold_fun)

Fold over all the sections in an AVM Pack.

This function will call the callback on each section of the AVM Pack, passing in the current section of each module in the AVM binary to the supplied fold function. The scan is bounded by avmpack_size and stops on the terminator or on the first invalid section header.

Parameters:
  • accum – The accumulator supplied by the application.

  • avmpack_binary – a pointer to an AVM Pack binary.

  • avmpack_size – the size in bytes of the AVM Pack.

  • fold_fun – function that will be called for each AVM section.

bool avmpack_is_complete(const void *avmpack_binary, size_t exact_size)

Returns true if an AVM Pack of exactly known size ends with its end terminator.

Reads the header and the last 16 bytes only, so a memory-mapped pack stays lazily paged. Trailing bytes after the terminator and sizes above UINT32_MAX are rejected.

Parameters:
  • avmpack_binary – a pointer to an AVM Pack binary.

  • exact_size – the exact size in bytes of the AVM Pack.

Returns:

true if the pack is complete, false otherwise.

bool avmpack_compute_size(const void *avmpack_binary, uint32_t max_size, uint32_t *real_size)

Finds the end of an AVM Pack within a region and returns its real size.

Walks the section chain, validating every header, until the end terminator or max_size is reached.

Parameters:
  • avmpack_binary – a pointer to an AVM Pack binary.

  • max_size – the size in bytes of the region containing the AVM Pack.

  • real_size – [out] set to the size in bytes of the AVM Pack on success.

Returns:

true if the terminator was found within max_size, false otherwise.

static void in_memory_avm_pack_destructor(struct AVMPackData *obj, GlobalContext *global)
static void const_avm_pack_destructor(struct AVMPackData *obj, GlobalContext *global)

Variables

const struct AVMPackInfo in_memory_avm_pack_info = {.destructor = in_memory_avm_pack_destructor}
const struct AVMPackInfo const_avm_pack_info = {.destructor = const_avm_pack_destructor}
struct AVMPackSection

Public Members

const char *name
const void *data
uint32_t size
uint32_t data_size
uint32_t flags