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

feat parse elf memory usage #696

Merged
merged 1 commit into from
Oct 16, 2024
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
8 changes: 3 additions & 5 deletions rasp/librasp/src/golang.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use log::*;
use std::fs::File;
use std::{fs, path::PathBuf, process::Command};
use std::io::Read;
use regex::Regex;
use anyhow::{anyhow, Result};
use goblin::elf::Elf;
Expand Down Expand Up @@ -167,9 +166,8 @@ pub fn parse_version(version: &String) -> Result<String> {
}

pub fn golang_version(bin_file: &PathBuf) -> Result<String> {
let mut file = File::open(bin_file)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let file = File::open(bin_file)?;
let buffer = unsafe { MmapOptions::new().map(&file)? };

// parse elf
let elf = match Elf::parse(&buffer) {
Expand All @@ -183,7 +181,7 @@ pub fn golang_version(bin_file: &PathBuf) -> Result<String> {
}
};

if let Ok(version) = find_by_section(&elf, &buffer, &file) {
if let Ok(version) = find_by_section(&elf, &file) {
return parse_version(&version);
} else {
if let Ok(version) = find_by_symbol(&elf, &file) {
Expand Down
24 changes: 16 additions & 8 deletions rasp/librasp/src/parse_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use byteorder::{ByteOrder, LittleEndian, BigEndian};
use anyhow::{anyhow, Result};
use std::io::{Read, Seek, SeekFrom};
use log::*;

use memmap::Mmap;
const MAGIC: &[u8] = b"\xff Go buildinf:";
const RUNTIME_VERSION_MAGIC: &str = "runtime.buildVersion";
const EXPECTED_MAGIC_LEN: usize = 14;
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn find_by_symbol(elf: &Elf, file: &File) -> Result<String> {
}
}

pub fn find_by_section(elf: &Elf, buffer:&Vec<u8>, file: &File) -> Result<String> {
pub fn find_by_section(elf: &Elf, file: &File) -> Result<String> {
let mut version: String = String::new();

// find .go.buildinfo
Expand All @@ -154,13 +154,21 @@ pub fn find_by_section(elf: &Elf, buffer:&Vec<u8>, file: &File) -> Result<String
false
}
}) {
// read ".go.buildinfo" section data
if buffer.len() < (go_buildinfo_section.sh_offset as usize + go_buildinfo_section.sh_size as usize) || go_buildinfo_section.sh_size < BUILDINFO_HEADER_SIZE as u64 {
return Err(anyhow!("buidinfo size too large: size: {}, offset: {}",go_buildinfo_section.sh_offset, go_buildinfo_section.sh_size));
// read ".go.buildinfo" section data
let start = go_buildinfo_section.sh_offset as usize;
let end = (go_buildinfo_section.sh_offset + go_buildinfo_section.sh_size) as usize;

// Memory map the specific section
let mmap = match unsafe { Mmap::map(file) } {
Ok(m) => m,
Err(e) => {return Err(anyhow!(e));}// Return empty string if mmap fails
};
if mmap.len() < end {
return Err(anyhow!("mmap length invaild")); // Return empty string if the section is out of bounds
}

let buildinfo_data = &buffer[go_buildinfo_section.sh_offset as usize
..(go_buildinfo_section.sh_offset + go_buildinfo_section.sh_size) as usize];
// Extract the data of the section
let buildinfo_data = &mmap[start..end];

// check Magic
let magic_header = &buildinfo_data[0..EXPECTED_MAGIC_LEN];
Expand Down
Loading