Skip to content

Commit

Permalink
Merge pull request #697 from bytedance/feat-parse-twice
Browse files Browse the repository at this point in the history
feat parse twice
  • Loading branch information
yoloyyh authored Oct 17, 2024
2 parents 1c4b4f0 + f2e1f1d commit ba96a4b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
25 changes: 3 additions & 22 deletions rasp/librasp/src/golang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,7 +124,7 @@ pub fn golang_attach(pid: i32) -> Result<bool> {
};
}

pub fn golang_bin_inspect(bin_file: &PathBuf) -> Result<u64> {
pub fn golang_bin_inspect(bin_file: &PathBuf, elf: &Elf) -> Result<u64> {
let metadata = match fs::metadata(bin_file.clone()) {
Ok(md) => md,
Err(e) => {
Expand All @@ -136,11 +135,7 @@ pub fn golang_bin_inspect(bin_file: &PathBuf) -> Result<u64> {
// 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) {
Expand All @@ -165,21 +160,7 @@ pub fn parse_version(version: &String) -> Result<String> {
return Err(anyhow::anyhow!("Failed to extract version number, from: {}", version));
}

pub fn golang_version(bin_file: &PathBuf) -> Result<String> {
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<String> {

if let Ok(version) = find_by_section(&elf, &file) {
return parse_version(&version);
Expand Down
11 changes: 9 additions & 2 deletions rasp/librasp/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ba96a4b

Please sign in to comment.