Skip to content

Commit

Permalink
Fix all undefined behaviors found by UBsan
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 1, 2022
1 parent bf902e1 commit da7b5db
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
8 changes: 4 additions & 4 deletions elf/dwarf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ read_compunits(Context<E> &ctx, ObjectFile<E> &file) {
while (!data.empty()) {
if (data.size() < 4)
Fatal(ctx) << *file.debug_info << ": corrupted .debug_info";
if (*(u32 *)data.data() == 0xffffffff)
if (*(ul32 *)data.data() == 0xffffffff)
Fatal(ctx) << *file.debug_info << ": --gdb-index: DWARF64 not supported";
i64 len = *(u32 *)data.data() + 4;
i64 len = *(ul32 *)data.data() + 4;
vec.push_back(data.substr(0, len));
data = data.substr(len);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ inline u64 DebugInfoReader<E>::read(u64 form) {
case DW_FORM_strx2:
case DW_FORM_addrx2:
case DW_FORM_ref2: {
u64 val = *(u16 *)cu;
u64 val = *(ul16 *)cu;
cu += 2;
return val;
}
Expand Down Expand Up @@ -450,7 +450,7 @@ read_address_areas(Context<E> &ctx, ObjectFile<E> &file, i64 offset) {
Fatal(ctx) << file << ": --gdb-index: missing DW_AT_rnglists_base";

u8 *base = buf + *rnglists_base;
return read_rnglist_range(ctx, file, base + *(u32 *)base, addrx);
return read_rnglist_range(ctx, file, base + *(ul32 *)base, addrx);
}

// Handle a contiguous address range.
Expand Down
68 changes: 34 additions & 34 deletions elf/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1270,64 +1270,64 @@ struct Elf64Rela {
};

struct Elf32Rela {
u32 r_offset;
u32 r_type : 8;
u32 r_sym : 24;
i32 r_addend;
ul32 r_offset;
u8 r_type;
ul24 r_sym;
il32 r_addend;
};

struct Elf64Dyn {
u64 d_tag;
u64 d_val;
ul64 d_tag;
ul64 d_val;
};

struct Elf32Dyn {
u32 d_tag;
u32 d_val;
ul32 d_tag;
ul32 d_val;
};

struct ElfVerneed {
u16 vn_version;
u16 vn_cnt;
u32 vn_file;
u32 vn_aux;
u32 vn_next;
ul16 vn_version;
ul16 vn_cnt;
ul32 vn_file;
ul32 vn_aux;
ul32 vn_next;
};

struct ElfVernaux {
u32 vna_hash;
u16 vna_flags;
u16 vna_other;
u32 vna_name;
u32 vna_next;
ul32 vna_hash;
ul16 vna_flags;
ul16 vna_other;
ul32 vna_name;
ul32 vna_next;
};

struct ElfVerdef {
u16 vd_version;
u16 vd_flags;
u16 vd_ndx;
u16 vd_cnt;
u32 vd_hash;
u32 vd_aux;
u32 vd_next;
ul16 vd_version;
ul16 vd_flags;
ul16 vd_ndx;
ul16 vd_cnt;
ul32 vd_hash;
ul32 vd_aux;
ul32 vd_next;
};

struct ElfVerdaux {
u32 vda_name;
u32 vda_next;
ul32 vda_name;
ul32 vda_next;
};

struct Elf64Chdr {
u32 ch_type;
u32 ch_reserved;
u64 ch_size;
u64 ch_addralign;
ul32 ch_type;
ul32 ch_reserved;
ul64 ch_size;
ul64 ch_addralign;
};

struct Elf32Chdr {
u32 ch_type;
u32 ch_size;
u32 ch_addralign;
ul32 ch_type;
ul32 ch_size;
ul32 ch_addralign;
};

struct ElfNhdr {
Expand Down
3 changes: 3 additions & 0 deletions elf/mapfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ static Map<E> get_map(Context<E> &ctx) {
}
});

if (map.size() <= 1)
return map;

tbb::parallel_for(map.range(), [](const typename Map<E>::range_type &range) {
for (auto it = range.begin(); it != range.end(); it++) {
std::vector<Symbol<E> *> &vec = it->second;
Expand Down
2 changes: 2 additions & 0 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,8 @@ void DynsymSection<E>::add_symbol(Context<E> &ctx, Symbol<E> *sym) {
template <typename E>
void DynsymSection<E>::finalize(Context<E> &ctx) {
Timer t(ctx, "DynsymSection::finalize");
if (symbols.empty())
return;

// We need a stable sort for build reproducibility, but parallel_sort
// isn't stable, so we use this struct to make it stable.
Expand Down

0 comments on commit da7b5db

Please sign in to comment.