From e8a5027cab7ce008dbb0964fe0522c8fef585979 Mon Sep 17 00:00:00 2001 From: mlokr Date: Sun, 8 Sep 2024 17:25:33 +0200 Subject: [PATCH] fixing the obscure string allocation bug --- hblang/README.md | 4 +-- hblang/src/codegen.rs | 29 ++---------------- .../codegen_tests_writing_into_string.txt | Bin 768 -> 531 bytes 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/hblang/README.md b/hblang/README.md index b424282..5619078 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -831,13 +831,13 @@ multiple_breaks := fn(arg: int): int { #### writing_into_string ```hb outl := fn(): void { - msg := "\0\0\0\0\0\0\0\0" + msg := "whahaha\0" @as(u8, 0) return } inl := fn(): void { - msg := "\0\0\0\0" + msg := "luhahah\0" return } diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 35ff1d5..6600d5b 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -1046,24 +1046,18 @@ struct FTask { #[derive(Default, Clone, Copy, PartialEq, Eq, Debug)] pub struct Snapshot { code: usize, - string_data: usize, relocs: usize, - strings: usize, } impl Snapshot { fn _sub(&mut self, other: &Self) { self.code -= other.code; - self.string_data -= other.string_data; self.relocs -= other.relocs; - self.strings -= other.strings; } fn _add(&mut self, other: &Self) { self.code += other.code; - self.string_data += other.string_data; self.relocs += other.relocs; - self.strings += other.strings; } } @@ -1119,21 +1113,13 @@ impl Output { } fn pop(&mut self, stash: &mut Self, snap: &Snapshot) { - let init_code = stash.code.len(); - stash.code.extend(self.code.drain(snap.code..)); - stash.string_data.extend(self.string_data.drain(snap.string_data..)); stash.relocs.extend(self.relocs.drain(snap.relocs..)); - stash.strings.extend(self.strings.drain(snap.strings..).inspect(|str| { - debug_assert!(str.reloc.offset as usize + init_code < stash.code.len()) - })); } fn trunc(&mut self, snap: &Snapshot) { self.code.truncate(snap.code); - self.string_data.truncate(snap.string_data); self.relocs.truncate(snap.relocs); - self.strings.truncate(snap.strings); } fn write_trap(&mut self, kind: trap::Trap) { @@ -1143,12 +1129,7 @@ impl Output { } fn snap(&mut self) -> Snapshot { - Snapshot { - code: self.code.len(), - string_data: self.string_data.len(), - relocs: self.relocs.len(), - strings: self.strings.len(), - } + Snapshot { code: self.code.len(), relocs: self.relocs.len() } } } @@ -2922,8 +2903,8 @@ impl Codegen { } //self.compress_strings(); - for reloc in self.output.strings.iter() { - let from_offset = self.tys.offset_of_item(reloc.from, ct_hint).unwrap(); + for reloc in self.output.strings.iter().filter(|s| s.shifted) { + let Some(from_offset) = self.tys.offset_of_item(reloc.from, ct_hint) else { continue }; reloc.reloc.apply_jump(&mut self.output.code, reloc.range.start, from_offset); } } @@ -3406,17 +3387,13 @@ impl Codegen { fn local_snap(&self) -> Snapshot { Snapshot { code: self.output.code.len() - self.ci.snap.code, - string_data: self.output.string_data.len() - self.ci.snap.string_data, relocs: self.output.relocs.len() - self.ci.snap.relocs, - strings: self.output.strings.len() - self.ci.snap.strings, } } fn pop_local_snap(&mut self, snap: Snapshot) { self.output.code.truncate(snap.code + self.ci.snap.code); - self.output.string_data.truncate(snap.string_data + self.ci.snap.string_data); self.output.relocs.truncate(snap.relocs + self.ci.snap.relocs); - self.output.strings.truncate(snap.strings + self.ci.snap.strings); } fn pack_args(&mut self, pos: Pos, arg_base: usize) -> ty::Tuple { diff --git a/hblang/tests/codegen_tests_writing_into_string.txt b/hblang/tests/codegen_tests_writing_into_string.txt index cedfad66ccb839a58edf684778e872462abafa32..989afd0a05e2f774a6ac649b10e021d4d321116d 100644 GIT binary patch delta 174 zcmZo*o6Iu7ZeoJCpqHbMLXokdjzW=vj)GNYUe4qROyUz~8Zl+&O*|*fWN0=~UP9K; zEQ3n{2z-JZfr^cwigQXc5`hq;ff=Y`;spti@-3!#HI;+)B%*4Xe1u6!%E-u)t0=X^ XO2L4uxFoTpwAf0)KU+gXQG=)`FnZ-OUn_VCl^BYD5~g@|8U+hk5us?px>%=hgyZsa)Gz(5ma3 O{?u7meUC=tvAO^l-l6RP