Skip to content

Commit

Permalink
Merge pull request #562 from bytedance/feat-golangbin
Browse files Browse the repository at this point in the history
feat golang binsize
  • Loading branch information
yoloyyh committed Dec 22, 2023
2 parents 92dd102 + 8b6fc0e commit 136657e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
13 changes: 7 additions & 6 deletions rasp/librasp/src/golang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,18 @@ pub fn golang_attach(pid: i32) -> Result<bool> {
};
}

pub fn golang_bin_inspect(bin_file: PathBuf) -> Result<bool> {
pub fn golang_bin_inspect(bin_file: PathBuf) -> Result<u64> {
let metadata = match fs::metadata(bin_file.clone()) {
Ok(md) => md,
Err(e) => {
return Err(anyhow!(e));
}
};
let size = metadata.len();
if size >= (500 * 1024 * 1024) {
return Err(anyhow!("bin file oversize"));
}
// 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)?;
Expand All @@ -142,9 +143,9 @@ pub fn golang_bin_inspect(bin_file: PathBuf) -> Result<bool> {
let offset = section.sh_name;
if let Some(name) = shstrtab.get(offset) {
if name.unwrap() == ".gopclntab" {
return Ok(true);
return Ok(size);
}
}
}
return Ok(false);
return Ok(0);
}
12 changes: 10 additions & 2 deletions rasp/librasp/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ impl RuntimeInspect for ProcessInfo {}
pub struct Runtime {
pub name: &'static str,
pub version: String,
pub size: u64,
}

impl Display for Runtime {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} {}", self.name, self.version)
write!(f, "{} {} {}", self.name, self.version, self.size)
}
}

Expand Down Expand Up @@ -87,6 +88,7 @@ pub trait RuntimeInspect {
return Ok(Some(Runtime {
name: "JVM",
version: version,
size: 0,
}));
}
let cpython_process_filter: RuntimeFilter =
Expand All @@ -106,6 +108,7 @@ pub trait RuntimeInspect {
return Ok(Some(Runtime {
name: "CPython",
version: String::new(),
size: 0,
}));
}
let nodejs_process_filter: RuntimeFilter =
Expand Down Expand Up @@ -144,6 +147,7 @@ pub trait RuntimeInspect {
return Ok(Some(Runtime {
name: "NodeJS",
version,
size: 0,
}));
}
let pid = process_info.pid.clone();
Expand All @@ -163,10 +167,11 @@ pub trait RuntimeInspect {
}
match golang_bin_inspect(path) {
Ok(res) => {
if res {
if res > 0 {
return Ok(Some(Runtime {
name: "Golang",
version: String::new(),
size: res,
}));
}
}
Expand All @@ -183,11 +188,13 @@ pub trait RuntimeInspect {
return Ok(Some(Runtime {
name: "PHP",
version: format!("{}.zts", version),
size: 0,
}));
} else {
return Ok(Some(Runtime {
name: "PHP",
version: version,
size: 0,
}));
}
}
Expand All @@ -206,6 +213,7 @@ pub trait RuntimeInspect {
return Ok(Some(Runtime {
name: "CPython",
version,
size: 0,
}))
}
None => {}
Expand Down
7 changes: 7 additions & 0 deletions rasp/plugin/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ pub fn make_report(
None => String::new(),
}
);
report.insert(
"runtime_size",
match &process.runtime {
Some(rt) => rt.size.to_string(),
None => String::new(),
}
);
report.insert(
"attach_start_time",
process
Expand Down
2 changes: 2 additions & 0 deletions rasp/plugin/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ pub fn generate_heartbeat(watched_process: &ProcessInfo) -> HashMap<&'static str
let runtime = watched_process.runtime.clone().unwrap_or(Runtime {
name: "unknown",
version: "unknown".to_string(),
size: 0,
});

message.insert("runtime", runtime.name.to_string());
message.insert("runtime_version", runtime.version);
message.insert("runtime_size", runtime.size.to_string());
message.insert("attach_start_time", watched_process.attach_start_time.clone().unwrap_or("".to_string()));
message.insert("attach_end_time", watched_process.attach_end_time.clone().unwrap_or("".to_string()));
message.insert("failed_time", watched_process.failed_time.clone().unwrap_or("".to_string()));
Expand Down

0 comments on commit 136657e

Please sign in to comment.