undefined_variable_detection.rs 17 KB

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