Skip to content

Commit

Permalink
Fixed a wied bug tirggered by specifc rustc versions
Browse files Browse the repository at this point in the history
  • Loading branch information
FractalFir committed Jul 20, 2024
1 parent 6c957b9 commit 703104d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 253 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lazy_static = "1.4.0"
libc = "0.2.153"
libloading = "0.8.1"

postcard = { version = "1.0.6", features = ["use-std"] }
postcard = { version = "1.0.8", features = ["use-std"] }
rayon = "1.10.0"
rustc-demangle = "0.1.23"
cilly = {path = "./cilly"}
Expand Down
14 changes: 10 additions & 4 deletions cilly/src/bin/linker/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ fn load_ar(r: &mut impl std::io::Read) -> std::io::Result<(Assembly, Vec<Linkabl
while let Some(entry_result) = archive.next_entry() {
let mut entry = entry_result?;
let name: String = String::from_utf8_lossy(entry.header().identifier()).into();

if name.contains(".bc") || name.contains(".cilly") {
let ext = if let Some(ext) = name.split('.').last() {
ext
} else {
continue;
};
if ext.contains(".bc") || ext.contains(".cilly") {
let mut asm_bytes = Vec::with_capacity(0x100);
entry
.read_to_end(&mut asm_bytes)
.expect("ERROR: Could not load the assembly file!");
let assembly = postcard::from_bytes(&asm_bytes)
.expect("ERROR:Could not decode the assembly file!");
.unwrap_or_else(|_| panic!("ERROR:Could not decode the assembly file {name}!"));
final_assembly = final_assembly.join(assembly);
} else if name.contains(".o") {
} else if ext.contains(".o") {
let mut file_bytes = Vec::with_capacity(0x100);
entry
.read_to_end(&mut file_bytes)
Expand All @@ -53,6 +57,7 @@ pub fn load_assemblies(
raw_files: &[&String],
archives: &[String],
) -> (Assembly, Vec<LinkableFile>) {
println!("Preparing to load assmeblies");
let mut final_assembly = Assembly::empty();
let mut linkables = Vec::new();
for asm_path in raw_files {
Expand All @@ -73,5 +78,6 @@ pub fn load_assemblies(
final_assembly = final_assembly.join(assembly.0);
linkables.extend(assembly.1);
}
println!("Loaded assmeblies");
(final_assembly, linkables)
}
6 changes: 1 addition & 5 deletions cilly/src/cil_iter_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::marker::PhantomData;

use ordered_float::OrderedFloat;

use crate::{
cil_node::CILNode,
cil_root::CILRoot,
intercow::{InterCow, InterCowRefMut},
};
use crate::{cil_node::CILNode, cil_root::CILRoot};

#[derive(Debug)]
enum CILIterElemUnsafe<'a> {
Expand Down
7 changes: 1 addition & 6 deletions cilly/src/cil_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use std::borrow::Borrow;

use serde::{Deserialize, Serialize};

use crate::{
cil_node::ValidationContext,
cil_root::CILRoot,
intercow::{InterCow, InterCowRefMut},
IString, Type,
};
use crate::{cil_node::ValidationContext, cil_root::CILRoot, IString, Type};

#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
/// A root of a CIL Tree with metadata about local variables it reads/writes into.
Expand Down
227 changes: 0 additions & 227 deletions cilly/src/intercow.rs
Original file line number Diff line number Diff line change
@@ -1,228 +1 @@
use std::{
borrow::{Borrow, BorrowMut},
fmt::{Debug, Display},
hash::Hash,
ops::{Deref, DerefMut},
};

use interner::global::GlobalPool;
use internment::ArcIntern;

use postcard::fixint::be::deserialize;
use serde::{Deserialize, Serialize};

use crate::utilis::MemoryUsage;
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InterCow<T: 'static + Send + Sync + Eq + Hash + Clone> {
inner: ArcIntern<T>,
}
impl<T: Eq + Sync + Send + Hash + Clone> InterCow<T> {
pub fn new(val: T) -> Self {
val.into()
}
pub fn borrow_mut(&mut self) -> InterCowRefMut<T> {
InterCowRefMut(self, self.inner.as_ref().clone())
}
}

impl<T: std::marker::Send + Hash + Eq + Sync + Debug + Clone> Debug for InterCow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<T: std::marker::Send + Hash + Eq + Sync + Clone> From<T> for InterCow<T> {
fn from(value: T) -> Self {
Self {
inner: value.into(),
}
}
}
impl<T: std::marker::Send + Hash + Eq + Sync + Clone> std::ops::Deref for InterCow<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.inner
}
}
pub struct InterCowRefMut<'a, T: 'static + Send + Sync + Eq + Hash + Clone>(&'a mut InterCow<T>, T);
impl<'a, T: 'static + Send + Sync + Eq + Hash + Clone> Deref for InterCowRefMut<'a, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.1
}
}
impl<'a, T: 'static + Send + Sync + Eq + Hash + Clone> DerefMut for InterCowRefMut<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.1
}
}
impl<'a, T: 'static + Send + Sync + Eq + Hash + Clone> Drop for InterCowRefMut<'a, T> {
fn drop(&mut self) {
*self.0 = self.1.clone().into();
}
}
impl From<&str> for InterCow<String> {
fn from(value: &str) -> Self {
Self::new(value.into())
}
}
impl Display for InterCow<String> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.inner, f)
}
}
impl AsRef<str> for InterCow<String> {
fn as_ref(&self) -> &str {
self.inner.as_ref()
}
}
impl Borrow<str> for InterCow<String> {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl From<&str> for InterCow<Box<str>> {
fn from(value: &str) -> Self {
Self::new(value.into())
}
}
impl From<String> for InterCow<Box<str>> {
fn from(value: String) -> Self {
Self::new(value.into())
}
}
impl Display for InterCow<Box<str>> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.inner, f)
}
}
impl AsRef<str> for InterCow<Box<str>> {
fn as_ref(&self) -> &str {
self.inner.as_ref()
}
}
impl Borrow<str> for InterCow<Box<str>> {
fn borrow(&self) -> &str {
self
}
}
#[test]
fn serde_intercow() {
use ordered_float::OrderedFloat;
let strcow = InterCow::new(crate::cil_node::CILNode::LdcF32(OrderedFloat(6.7)));
let bytes = postcard::to_stdvec(&strcow).unwrap();
let node: InterCow<crate::cil_node::CILNode> =
postcard::from_bytes(&bytes).expect("ERROR:Could not decode the assembly file!");
assert_eq!(strcow, node);
let a1: &crate::cil_node::CILNode = &strcow;
let a2: &crate::cil_node::CILNode = &node;
assert_eq!(a1 as *const _ as usize, a2 as *const _ as usize);
// They ought to have the same address.
}
#[test]
fn intercow_set() {
let mut strcow = InterCow::new(0);
*strcow.borrow_mut() = 8;
assert_eq!(*strcow, 8);
}
/*
// BEFORE MEMORY OPT:
Maximum resident set size (kbytes): 904452
Average resident set size (kbytes): 0
Maximum resident set size (kbytes): 895348
Average resident set size (kbytes): 0
Maximum resident set size (kbytes): 895280
Average resident set size (kbytes): 0
*/

/*
Command being timed: "cargo test --release"
User time (seconds): 80.10
System time (seconds): 2.75
Percent of CPU this job got: 146%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:56.43
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 895636
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 33
Minor (reclaiming a frame) page faults: 1187608
Voluntary context switches: 6770
Involuntary context switches: 1433
Swaps: 0
File system inputs: 17328
File system outputs: 1040544
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0 */
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InternedString {
inner: interner::global::GlobalString,
}
impl Serialize for InternedString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.inner.as_ref().serialize(serializer)
}
}
static STRINGS: GlobalPool<String> = GlobalPool::new();
impl<'de> Deserialize<'de> for InternedString {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let tmp: Box<str> = Box::<str>::deserialize(deserializer)?;
Ok(Self {
inner: STRINGS.get(tmp.as_ref()),
})
}
}
impl Display for InternedString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(std::convert::AsRef::<str>::as_ref(&*self.inner), f)
}
}
impl From<String> for InternedString {
fn from(value: String) -> Self {
Self {
inner: STRINGS.get(value),
}
}
}
impl From<&str> for InternedString {
fn from(value: &str) -> Self {
Self {
inner: STRINGS.get(value),
}
}
}
impl AsRef<str> for InternedString {
fn as_ref(&self) -> &str {
&self.inner
}
}
impl Borrow<str> for InternedString {
fn borrow(&self) -> &str {
&self.inner
}
}
impl Deref for InternedString {
fn deref(&self) -> &str {
&self.inner
}

type Target = str;
}
impl MemoryUsage for InternedString {
fn memory_usage(&self, _: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
0
}
}
11 changes: 2 additions & 9 deletions cilly/src/type_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::num::NonZeroU64;
use serde::{Deserialize, Serialize};

use crate::{
access_modifier::AccessModifer, intercow::InterCow, method::Method, utilis::MemoryUsage,
DotnetTypeRef, IString, Type,
access_modifier::AccessModifer, method::Method, utilis::MemoryUsage, DotnetTypeRef, IString,
Type,
};

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Debug)]
Expand Down Expand Up @@ -192,10 +192,3 @@ impl MemoryUsage for TypeDef {
total_size
}
}
impl<'de, T: Deserialize<'de> + std::marker::Send + std::hash::Hash + Eq + Sync + Clone> MemoryUsage
for InterCow<T>
{
fn memory_usage(&self, _: &mut impl crate::utilis::MemoryUsageCounter) -> usize {
0
}
}
3 changes: 2 additions & 1 deletion cilly/src/utilis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,12 +822,13 @@ impl<A: MemoryUsage, B: MemoryUsage, C: MemoryUsage, D: MemoryUsage> MemoryUsage
#[test]
fn environ() {
let mut asm = Assembly::empty();

get_environ(&mut asm);

let mut out = String::new();
asm.methods()
.find(|method| method.name().contains("environ"))
.unwrap()
.export(&mut out, crate::IlasmFlavour::Clasic, true, false, &asm)
.unwrap();
panic!("{out}");
}

0 comments on commit 703104d

Please sign in to comment.