undefined_variable_detection.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // SPDX-License-Identifier: Apache-2.0
  2. use solang::codegen::{codegen, OptimizationLevel, Options};
  3. use solang::file_resolver::FileResolver;
  4. use solang::sema::ast::Diagnostic;
  5. use solang::sema::ast::Namespace;
  6. use solang::{parse_and_resolve, Target};
  7. use std::ffi::OsStr;
  8. fn parse_and_codegen(src: &'static str) -> Namespace {
  9. let mut cache = FileResolver::default();
  10. cache.set_file_contents("test.sol", src.to_string());
  11. let mut ns = parse_and_resolve(
  12. OsStr::new("test.sol"),
  13. &mut cache,
  14. Target::default_polkadot(),
  15. );
  16. let opt = Options {
  17. dead_storage: false,
  18. constant_folding: false,
  19. strength_reduce: false,
  20. vector_to_slice: false,
  21. common_subexpression_elimination: false,
  22. opt_level: OptimizationLevel::Default,
  23. generate_debug_information: false,
  24. log_runtime_errors: false,
  25. log_prints: true,
  26. strict_soroban_types: false,
  27. #[cfg(feature = "wasm_opt")]
  28. wasm_opt: None,
  29. soroban_version: None,
  30. };
  31. codegen(&mut ns, &opt);
  32. ns
  33. }
  34. fn contains_error_message_and_notes(
  35. errors: &[&Diagnostic],
  36. message: &str,
  37. notes_no: usize,
  38. ) -> bool {
  39. for error in errors {
  40. if error.message == message {
  41. return error.notes.len() == notes_no;
  42. }
  43. }
  44. false
  45. }
  46. #[test]
  47. fn used_before_being_defined() {
  48. let file = r#"
  49. contract Test {
  50. bytes byteArr;
  51. bytes32 baRR;
  52. function get() public {
  53. string memory s = "Test";
  54. byteArr = bytes(s);
  55. uint16 a = 1;
  56. uint8 b;
  57. b = uint8(a);
  58. uint256 c;
  59. c = b;
  60. bytes32 b32;
  61. bytes memory char = bytes(bytes32(uint(a) * 2 ** (8 * b)));
  62. baRR = bytes32(c);
  63. bytes32 cdr = bytes32(char);
  64. assert(b32 == baRR);
  65. if(b32 != cdr) {
  66. }
  67. }
  68. }
  69. "#;
  70. let ns = parse_and_codegen(file);
  71. let errors = ns.diagnostics.errors();
  72. assert_eq!(errors.len(), 1);
  73. assert_eq!(errors[0].message, "Variable 'b32' is undefined");
  74. assert_eq!(errors[0].notes.len(), 2);
  75. assert_eq!(
  76. errors[0].notes[0].message,
  77. "Variable read before being defined"
  78. );
  79. assert_eq!(
  80. errors[0].notes[1].message,
  81. "Variable read before being defined"
  82. );
  83. }
  84. #[test]
  85. fn struct_as_ref() {
  86. let file = r#"
  87. contract test_struct_parsing {
  88. struct foo {
  89. bool x;
  90. uint32 y;
  91. }
  92. function func(foo f) private {
  93. // assigning to f members dereferences f
  94. f.x = true;
  95. f.y = 64;
  96. // assigning to f changes the reference
  97. f = foo({ x: false, y: 256 });
  98. // f no longer point to f in caller function
  99. f.x = false;
  100. f.y = 98123;
  101. }
  102. function test() public {
  103. foo f;
  104. func(f);
  105. assert(f.x == true);
  106. assert(f.y == 64);
  107. }
  108. }
  109. "#;
  110. let ns = parse_and_codegen(file);
  111. let errors = ns.diagnostics.errors();
  112. assert_eq!(errors.len(), 1);
  113. assert_eq!(errors[0].message, "Variable 'f' is undefined");
  114. assert_eq!(errors[0].notes.len(), 3);
  115. assert_eq!(
  116. errors[0].notes[0].message,
  117. "Variable read before being defined"
  118. );
  119. assert_eq!(
  120. errors[0].notes[1].message,
  121. "Variable read before being defined"
  122. );
  123. assert_eq!(
  124. errors[0].notes[2].message,
  125. "Variable read before being defined"
  126. );
  127. let file = r#"
  128. contract test_struct_parsing {
  129. struct foo {
  130. bool x;
  131. uint32 y;
  132. }
  133. function func(foo f) private {
  134. // assigning to f members dereferences f
  135. f.x = true;
  136. f.y = 64;
  137. // assigning to f changes the reference
  138. f = foo({ x: false, y: 256 });
  139. // f no longer point to f in caller function
  140. f.x = false;
  141. f.y = 98123;
  142. }
  143. function test() public {
  144. foo f = foo(false, 2);
  145. func(f);
  146. assert(f.x == true);
  147. assert(f.y == 64);
  148. }
  149. }
  150. "#;
  151. let ns = parse_and_codegen(file);
  152. let errors = ns.diagnostics.errors();
  153. assert_eq!(errors.len(), 0);
  154. }
  155. #[test]
  156. fn while_loop() {
  157. let file = r#"
  158. contract testing {
  159. function test(int x) public pure returns (string) {
  160. string s;
  161. while(x > 0){
  162. s = "testing_string";
  163. x--;
  164. }
  165. return s;
  166. }
  167. }
  168. "#;
  169. let ns = parse_and_codegen(file);
  170. let errors = ns.diagnostics.errors();
  171. assert_eq!(errors.len(), 1);
  172. assert_eq!(errors[0].message, "Variable 's' is undefined");
  173. assert_eq!(errors[0].notes.len(), 1);
  174. assert_eq!(
  175. errors[0].notes[0].message,
  176. "Variable read before being defined"
  177. );
  178. let file = r#"
  179. contract testing {
  180. function test(int x) public pure returns (string) {
  181. string s;
  182. while(x > 0){
  183. s = "testing_string";
  184. x--;
  185. }
  186. if(x < 0) {
  187. s = "another_test";
  188. }
  189. return s;
  190. }
  191. }
  192. "#;
  193. let ns = parse_and_codegen(file);
  194. let errors = ns.diagnostics.errors();
  195. assert_eq!(errors.len(), 1);
  196. assert_eq!(errors[0].message, "Variable 's' is undefined");
  197. assert_eq!(errors[0].notes.len(), 1);
  198. assert_eq!(
  199. errors[0].notes[0].message,
  200. "Variable read before being defined"
  201. );
  202. let file = r#"
  203. contract testing {
  204. function test(int x) public pure returns (string) {
  205. string s;
  206. while(x > 0){
  207. s = "testing_string";
  208. x--;
  209. }
  210. if(x < 0) {
  211. s = "another_test";
  212. } else {
  213. s = "should_work";
  214. }
  215. return s;
  216. }
  217. }
  218. "#;
  219. let ns = parse_and_codegen(file);
  220. let errors = ns.diagnostics.errors();
  221. assert_eq!(errors.len(), 0);
  222. }
  223. #[test]
  224. fn for_loop() {
  225. let file = r#"
  226. contract testing {
  227. function test(int x) public pure returns (int) {
  228. int s;
  229. for(int i=0; i<x; i++) {
  230. s = 5;
  231. }
  232. int p;
  233. if(x < 0) {
  234. p = s + 2;
  235. }
  236. return p;
  237. }
  238. }
  239. "#;
  240. let ns = parse_and_codegen(file);
  241. let errors = ns.diagnostics.errors();
  242. assert_eq!(errors.len(), 2);
  243. assert!(contains_error_message_and_notes(
  244. &errors,
  245. "Variable 'p' is undefined",
  246. 1
  247. ));
  248. assert!(contains_error_message_and_notes(
  249. &errors,
  250. "Variable 's' is undefined",
  251. 1
  252. ));
  253. let file = r#"
  254. contract testing {
  255. function test(int x) public pure returns (int) {
  256. int s;
  257. for(int i=0; i<x; i++) {
  258. s = 5;
  259. }
  260. s=5;
  261. int p;
  262. if(x < 0) {
  263. p = s + 2;
  264. } else {
  265. p = 2;
  266. s = 2;
  267. }
  268. return p;
  269. }
  270. }
  271. "#;
  272. let ns = parse_and_codegen(file);
  273. let errors = ns.diagnostics.errors();
  274. assert_eq!(errors.len(), 0);
  275. }
  276. #[test]
  277. fn do_while_loop() {
  278. let file = r#"
  279. contract testing {
  280. struct other {
  281. int a;
  282. }
  283. function test(int x) public pure returns (int) {
  284. other o;
  285. do {
  286. x--;
  287. o = other(1);
  288. }while(x > 0);
  289. return o.a;
  290. }
  291. }
  292. "#;
  293. let ns = parse_and_codegen(file);
  294. let errors = ns.diagnostics.errors();
  295. assert_eq!(errors.len(), 0);
  296. }
  297. #[test]
  298. fn if_else_condition() {
  299. let file = r#"
  300. contract testing {
  301. struct other {
  302. int a;
  303. }
  304. function test(int x) public pure returns (int) {
  305. other o;
  306. if(x > 0) {
  307. o = other(2);
  308. }
  309. return o.a;
  310. }
  311. }
  312. "#;
  313. let ns = parse_and_codegen(file);
  314. let errors = ns.diagnostics.errors();
  315. assert_eq!(errors.len(), 1);
  316. assert_eq!(errors[0].message, "Variable 'o' is undefined");
  317. assert_eq!(errors[0].notes.len(), 1);
  318. assert_eq!(
  319. errors[0].notes[0].message,
  320. "Variable read before being defined"
  321. );
  322. let file = r#"
  323. contract testing {
  324. struct other {
  325. int a;
  326. }
  327. function test(int x) public pure returns (int) {
  328. other o;
  329. if(x > 0) {
  330. x += 2;
  331. } else if(x < 0) {
  332. o = other(2);
  333. } else {
  334. x++;
  335. }
  336. return o.a;
  337. }
  338. }
  339. "#;
  340. let ns = parse_and_codegen(file);
  341. let errors = ns.diagnostics.errors();
  342. assert_eq!(errors.len(), 1);
  343. assert_eq!(errors[0].message, "Variable 'o' is undefined");
  344. assert_eq!(errors[0].notes.len(), 1);
  345. assert_eq!(
  346. errors[0].notes[0].message,
  347. "Variable read before being defined"
  348. );
  349. let file = r#"
  350. contract testing {
  351. struct other {
  352. int a;
  353. }
  354. function test(int x) public pure returns (int) {
  355. other o;
  356. if(x > 0) {
  357. o = other(2);
  358. } else {
  359. o = other(2);
  360. }
  361. return o.a;
  362. }
  363. }
  364. "#;
  365. let ns = parse_and_codegen(file);
  366. let errors = ns.diagnostics.errors();
  367. assert_eq!(errors.len(), 0);
  368. }
  369. #[test]
  370. fn array() {
  371. let file = r#"
  372. contract test {
  373. function testing(int x) public pure returns (int) {
  374. int[] vec;
  375. return vec[0];
  376. }
  377. }
  378. "#;
  379. let ns = parse_and_codegen(file);
  380. let errors = ns.diagnostics.errors();
  381. assert_eq!(errors.len(), 0);
  382. let file = r#"
  383. contract test {
  384. function testing(int x) public pure returns (int) {
  385. int[] vec;
  386. if(x > 0) {
  387. vec.push(2);
  388. }
  389. return vec[0];
  390. }
  391. }
  392. "#;
  393. let ns = parse_and_codegen(file);
  394. let errors = ns.diagnostics.errors();
  395. assert_eq!(errors.len(), 0);
  396. }
  397. #[test]
  398. fn contract_and_enum() {
  399. let file = r#"
  400. contract other {
  401. int public a;
  402. function testing() public returns (int) {
  403. return 2;
  404. }
  405. }
  406. contract test {
  407. enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
  408. function testing(int x) public returns (int) {
  409. other o;
  410. FreshJuiceSize choice;
  411. if(x > 0 && o.testing() < 5) {
  412. o = new other();
  413. }
  414. assert(choice == FreshJuiceSize.LARGE);
  415. return o.a();
  416. }
  417. }
  418. "#;
  419. let ns = parse_and_codegen(file);
  420. let errors = ns.diagnostics.errors();
  421. assert!(contains_error_message_and_notes(
  422. &errors,
  423. "Variable 'o' is undefined",
  424. 2
  425. ));
  426. assert!(contains_error_message_and_notes(
  427. &errors,
  428. "Variable 'choice' is undefined",
  429. 1
  430. ));
  431. }
  432. #[test]
  433. fn basic_types() {
  434. let file = r#"
  435. contract test {
  436. function testing(int x) public returns (address, int, uint, bool, bytes, int) {
  437. address a;
  438. int i;
  439. uint u;
  440. bool b;
  441. bytes bt;
  442. int[5] vec;
  443. while(x > 0) {
  444. x--;
  445. a = address(this);
  446. i = -2;
  447. u = 2;
  448. b = true;
  449. bt = hex"1234";
  450. vec[0] = 2;
  451. }
  452. return (a, i, u, b, bt, vec[1]);
  453. }
  454. }
  455. "#;
  456. let ns = parse_and_codegen(file);
  457. let errors = ns.diagnostics.errors();
  458. assert!(contains_error_message_and_notes(
  459. &errors,
  460. "Variable 'bt' is undefined",
  461. 1
  462. ));
  463. assert!(contains_error_message_and_notes(
  464. &errors,
  465. "Variable 'b' is undefined",
  466. 1
  467. ));
  468. assert!(contains_error_message_and_notes(
  469. &errors,
  470. "Variable 'a' is undefined",
  471. 1
  472. ));
  473. assert!(contains_error_message_and_notes(
  474. &errors,
  475. "Variable 'i' is undefined",
  476. 1
  477. ));
  478. assert!(contains_error_message_and_notes(
  479. &errors,
  480. "Variable 'u' is undefined",
  481. 1
  482. ));
  483. }
  484. #[test]
  485. fn nested_branches() {
  486. let file = r#"
  487. contract test {
  488. function testing(int x) public returns (int) {
  489. int i;
  490. while(x > 0) {
  491. int b;
  492. if(x > 5) {
  493. b = 2;
  494. }
  495. i = b;
  496. }
  497. int a;
  498. if(x < 5) {
  499. if(x < 2) {
  500. a = 2;
  501. } else {
  502. a = 1;
  503. }
  504. } else if(x < 4) {
  505. a = 5;
  506. }
  507. return i + a;
  508. }
  509. }
  510. "#;
  511. let ns = parse_and_codegen(file);
  512. let errors = ns.diagnostics.errors();
  513. assert!(contains_error_message_and_notes(
  514. &errors,
  515. "Variable 'b' is undefined",
  516. 1
  517. ));
  518. assert!(contains_error_message_and_notes(
  519. &errors,
  520. "Variable 'a' is undefined",
  521. 1
  522. ));
  523. assert!(contains_error_message_and_notes(
  524. &errors,
  525. "Variable 'i' is undefined",
  526. 2
  527. ));
  528. }
  529. #[test]
  530. fn try_catch() {
  531. let file = r#"
  532. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  533. contract Example {
  534. AddNumbers addContract;
  535. event StringFailure(string stringFailure);
  536. event BytesFailure(bytes bytesFailure);
  537. function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
  538. bytes r;
  539. try addContract.add(_a, _b) returns (uint256 _value) {
  540. r = hex"ABCD";
  541. return r;
  542. } catch Error(string memory _err) {
  543. r = hex"ABCD";
  544. emit StringFailure(_err);
  545. } catch (bytes memory _err) {
  546. emit BytesFailure(_err);
  547. }
  548. return r;
  549. }
  550. }
  551. "#;
  552. let ns = parse_and_codegen(file);
  553. let errors = ns.diagnostics.errors();
  554. assert_eq!(errors.len(), 1);
  555. assert_eq!(errors[0].message, "Variable 'r' is undefined");
  556. assert_eq!(errors[0].notes.len(), 1);
  557. assert_eq!(
  558. errors[0].notes[0].message,
  559. "Variable read before being defined"
  560. );
  561. let file = r#"
  562. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  563. contract Example {
  564. AddNumbers addContract;
  565. event StringFailure(string stringFailure);
  566. event BytesFailure(bytes bytesFailure);
  567. function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
  568. bytes r;
  569. try addContract.add(_a, _b) returns (uint256 _value) {
  570. r = hex"ABCD";
  571. return r;
  572. } catch Error(string memory _err) {
  573. r = hex"ABCD";
  574. emit StringFailure(_err);
  575. } catch (bytes memory _err) {
  576. r = hex"ABCD";
  577. emit BytesFailure(_err);
  578. }
  579. return r;
  580. }
  581. }
  582. "#;
  583. let ns = parse_and_codegen(file);
  584. let errors = ns.diagnostics.errors();
  585. assert_eq!(errors.len(), 0);
  586. let file = r#"
  587. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  588. contract Example {
  589. AddNumbers addContract;
  590. event StringFailure(string stringFailure);
  591. event BytesFailure(bytes bytesFailure);
  592. function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
  593. bytes r;
  594. try addContract.add(_a, _b) returns (uint256 _value) {
  595. return r;
  596. } catch Error(string memory _err) {
  597. r = hex"ABCD";
  598. emit StringFailure(_err);
  599. } catch (bytes memory _err) {
  600. emit BytesFailure(_err);
  601. }
  602. return r;
  603. }
  604. }
  605. "#;
  606. let ns = parse_and_codegen(file);
  607. let errors = ns.diagnostics.errors();
  608. assert_eq!(errors.len(), 1);
  609. assert_eq!(errors[0].message, "Variable 'r' is undefined");
  610. assert_eq!(errors[0].notes.len(), 2);
  611. assert!(errors[0]
  612. .notes
  613. .iter()
  614. .all(|note| { note.message == "Variable read before being defined" }));
  615. let file = r#"
  616. contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
  617. contract Example {
  618. AddNumbers addContract;
  619. event StringFailure(string stringFailure);
  620. event BytesFailure(bytes bytesFailure);
  621. function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
  622. bytes r;
  623. try addContract.add(_a, _b) returns (uint256 _value) {
  624. r = hex"ABCD";
  625. return r;
  626. } catch Error(string memory _err) {
  627. emit StringFailure(_err);
  628. } catch (bytes memory _err) {
  629. emit BytesFailure(_err);
  630. }
  631. return r;
  632. }
  633. }
  634. "#;
  635. let ns = parse_and_codegen(file);
  636. let errors = ns.diagnostics.errors();
  637. assert_eq!(errors.len(), 1);
  638. assert_eq!(errors[0].message, "Variable 'r' is undefined");
  639. assert_eq!(errors[0].notes.len(), 1);
  640. assert_eq!(
  641. errors[0].notes[0].message,
  642. "Variable read before being defined"
  643. );
  644. }