diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-09-12 22:50:24 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-09-12 22:50:24 +0200 |
| commit | 1b18c250d57e29fd318058bd2356b626e4cc1c47 (patch) | |
| tree | 53ac7a2593372bc0c4b1bbfa9ad6d149ca86c586 /lib | |
| parent | ffaa5353031149ab2416bcf6d8b46e0a44a3bb39 (diff) | |
| download | tools.nes-1b18c250d57e29fd318058bd2356b626e4cc1c47.tar.gz tools.nes-1b18c250d57e29fd318058bd2356b626e4cc1c47.zip | |
xixanta: Don't allow negative variables on asan
Negative variables make no sense when it comes to do stuff like:
variable = -1
lda variable
That being said, as an assembler you never know the hacks and nonsense
programmers are willing to endure. But we do know that if the address
sanitizer is enabled, since if it's an "asan-friendly", then an explicit
negative variable can be barred.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/xixanta/src/assembler.rs | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs index 89ad3ac..6ae4d3c 100644 --- a/lib/xixanta/src/assembler.rs +++ b/lib/xixanta/src/assembler.rs @@ -499,10 +499,12 @@ impl<'a> Assembler<'a> { match self.evaluate_node(node.left.as_ref().unwrap()) { Ok(value) => { // Check that the given name makes sense if the - // address sanitizer is enable and the assigned + // address sanitizer is enabled and the assigned // value is known. if self.asan_enabled && value.resolved { - self.asan_check_variable_name(node, &value); + if let Err(err) = self.asan_check_variable_name(node, &value) { + errors.push(err); + } } if let Err(err) = self.context.set_variable( @@ -2652,11 +2654,21 @@ impl<'a> Assembler<'a> { // Check that the variable name follows the coding convention as expected // from the address sanitizer. - fn asan_check_variable_name(&mut self, node: &PNode, bundle: &Bundle) { + fn asan_check_variable_name(&mut self, node: &PNode, bundle: &Bundle) -> Result<(), Error> { // If this is not an asan-friendly name, skip it, as it might be a // constant or something else that might be catched later. if !is_asan_friendly_name(&node.value.value) { - return; + return Ok(()); + } + + // If the bundle was explicitely marked as negative, then cry out. + if bundle.negative { + return Err(Error { + line: node.value.line, + message: "you cannot use a negative value for a memory variable".to_string(), + source: self.source_for(node), + global: false, + }); } // Let's create this object now to makes things easier for possible @@ -2708,6 +2720,8 @@ impl<'a> Assembler<'a> { }); } } + + Ok(()) } fn to_relative_address(&self, node: &PNode, bundle: &mut Bundle) -> Result<(), Error> { @@ -3195,6 +3209,29 @@ mod tests { } #[test] + fn asan_invalid_negative_variable() { + let line = "m_var = -1"; + let real_line = minimal_header().to_string() + line; + + let res = assemble_with_mapping( + real_line.as_bytes(), + empty(), + &[], + &SourceInfo::default(), + true, + ); + + assert!(res.bundles.is_empty()); + assert!(!res.errors.is_empty()); + + // NOTE: 3 lines for the "minimal header" being prepended. + assert_eq!( + res.errors[0].to_string().as_str(), + "you cannot use a negative value for a memory variable (line 4)" + ); + } + + #[test] fn divide_by_zero() { assert_error( r#"Value = 0 |
