diff --git a/rasp/librasp/src/golang.rs b/rasp/librasp/src/golang.rs index 95001efb8..1d07fd56a 100644 --- a/rasp/librasp/src/golang.rs +++ b/rasp/librasp/src/golang.rs @@ -4,7 +4,6 @@ use std::{fs, path::PathBuf, process::Command}; use regex::Regex; use anyhow::{anyhow, Result}; use goblin::elf::Elf; -use memmap::MmapOptions; use crate::async_command::run_async_process; use crate::process::ProcessInfo; @@ -125,7 +124,7 @@ pub fn golang_attach(pid: i32) -> Result { }; } -pub fn golang_bin_inspect(bin_file: &PathBuf) -> Result { +pub fn golang_bin_inspect(bin_file: &PathBuf, elf: &Elf) -> Result { let metadata = match fs::metadata(bin_file.clone()) { Ok(md) => md, Err(e) => { @@ -136,11 +135,7 @@ pub fn golang_bin_inspect(bin_file: &PathBuf) -> Result { // if size >= (500 * 1024 * 1024) { // return Err(anyhow!("bin file oversize")); // } - - let file = File::open(bin_file)?; - let bin = unsafe { MmapOptions::new().map(&file)? }; - let elf = Elf::parse(&bin)?; - let shstrtab = elf.shdr_strtab; + let shstrtab = &elf.shdr_strtab; for section in elf.section_headers.iter() { let offset = section.sh_name; if let Some(name) = shstrtab.get_at(offset) { @@ -165,21 +160,7 @@ pub fn parse_version(version: &String) -> Result { return Err(anyhow::anyhow!("Failed to extract version number, from: {}", version)); } -pub fn golang_version(bin_file: &PathBuf) -> Result { - let file = File::open(bin_file)?; - let buffer = unsafe { MmapOptions::new().map(&file)? }; - - // parse elf - let elf = match Elf::parse(&buffer) { - Ok(elf) => elf, - Err(err) => { - let msg = format!( - "Failed to parse ELF file: {}", err - ); - warn!("{}", msg); - return Err(anyhow!("{}", msg)); - } - }; +pub fn golang_version(file: &File, elf: &Elf) -> Result { if let Ok(version) = find_by_section(&elf, &file) { return parse_version(&version); diff --git a/rasp/librasp/src/runtime.rs b/rasp/librasp/src/runtime.rs index e78e00b9f..a0b813fcf 100644 --- a/rasp/librasp/src/runtime.rs +++ b/rasp/librasp/src/runtime.rs @@ -2,10 +2,13 @@ use std::collections::HashMap; use std::ffi::OsString; use std::fmt::{self, Display, Formatter}; use std::path::PathBuf; +use memmap::MmapOptions; +use goblin::elf::Elf; use anyhow::{anyhow, Result}; use log::*; use serde_json; use std::fs; +use std::fs::File; use std::path::Path; use crate::cpython; use crate::golang::{golang_bin_inspect, golang_version}; @@ -205,10 +208,14 @@ pub trait RuntimeInspect { path.push(p); } } - match golang_bin_inspect(&path) { + + let file = File::open(&path)?; + let bin = unsafe { MmapOptions::new().map(&file)? }; + let elf = Elf::parse(&bin)?; + match golang_bin_inspect(&path, &elf) { Ok(res) => { if res > 0 { - let version = match golang_version(&path) { + let version = match golang_version(&file, &elf) { Ok(v) => { v }