miri_stress.rs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Stress Miri as much as possible.
  3. Prove that we don't leak memory and that our methods are safe.
  4. Specifically:
  5. - [ ] VirtualDom drops memory safely
  6. - [ ] Borrowed components don't expose invalid pointers
  7. - [ ] Async isn't busted
  8. */
  9. use dioxus::{prelude::*, SchedulerMsg, ScopeId};
  10. use dioxus_core as dioxus;
  11. use dioxus_core_macro::*;
  12. use dioxus_html as dioxus_elements;
  13. mod test_logging;
  14. const IS_LOGGING_ENABLED: bool = false;
  15. fn new_dom<P: 'static + Send>(app: Component<P>, props: P) -> VirtualDom {
  16. test_logging::set_up_logging(IS_LOGGING_ENABLED);
  17. VirtualDom::new_with_props(app, props)
  18. }
  19. /// This test ensures that if a component aborts early, it is replaced with a placeholder.
  20. /// In debug, this should also toss a warning.
  21. #[test]
  22. fn test_memory_leak() {
  23. fn app(cx: Scope) -> Element {
  24. let val = cx.use_hook(|_| 0);
  25. *val += 1;
  26. if *val == 2 || *val == 4 {
  27. return None;
  28. }
  29. let name = cx.use_hook(|_| String::from("asd"));
  30. cx.render(rsx!(
  31. div { "Hello, world!" }
  32. child()
  33. child()
  34. child()
  35. child()
  36. child()
  37. child()
  38. borrowed_child(na: name)
  39. borrowed_child(na: name)
  40. borrowed_child(na: name)
  41. borrowed_child(na: name)
  42. borrowed_child(na: name)
  43. ))
  44. }
  45. #[derive(Props)]
  46. struct BorrowedProps<'a> {
  47. na: &'a str,
  48. }
  49. fn borrowed_child<'a>(cx: Scope<'a, BorrowedProps<'a>>) -> Element {
  50. rsx!(cx, div {
  51. "goodbye {cx.props.na}"
  52. child()
  53. child()
  54. })
  55. }
  56. fn child(cx: Scope) -> Element {
  57. rsx!(cx, div { "goodbye world" })
  58. }
  59. let mut dom = new_dom(app, ());
  60. dom.rebuild();
  61. dom.hard_diff(ScopeId(0));
  62. dom.hard_diff(ScopeId(0));
  63. dom.hard_diff(ScopeId(0));
  64. dom.hard_diff(ScopeId(0));
  65. dom.hard_diff(ScopeId(0));
  66. dom.hard_diff(ScopeId(0));
  67. }
  68. #[test]
  69. fn memo_works_properly() {
  70. fn app(cx: Scope) -> Element {
  71. let val = cx.use_hook(|_| 0);
  72. *val += 1;
  73. if *val == 2 || *val == 4 {
  74. return None;
  75. }
  76. let name = cx.use_hook(|_| String::from("asd"));
  77. cx.render(rsx!(
  78. div { "Hello, world! {name}" }
  79. child(na: "asdfg".to_string() )
  80. ))
  81. }
  82. #[derive(PartialEq, Props)]
  83. struct ChildProps {
  84. na: String,
  85. }
  86. fn child(cx: Scope<ChildProps>) -> Element {
  87. rsx!(cx, div { "goodbye world" })
  88. }
  89. let mut dom = new_dom(app, ());
  90. dom.rebuild();
  91. dom.hard_diff(ScopeId(0));
  92. dom.hard_diff(ScopeId(0));
  93. dom.hard_diff(ScopeId(0));
  94. dom.hard_diff(ScopeId(0));
  95. dom.hard_diff(ScopeId(0));
  96. dom.hard_diff(ScopeId(0));
  97. dom.hard_diff(ScopeId(0));
  98. }
  99. #[test]
  100. fn free_works_on_root_props() {
  101. fn app(cx: Scope<Custom>) -> Element {
  102. cx.render(rsx! {
  103. child(a: "alpha")
  104. child(a: "beta")
  105. child(a: "gamma")
  106. child(a: "delta")
  107. })
  108. }
  109. #[derive(Props, PartialEq)]
  110. struct ChildProps {
  111. a: &'static str,
  112. }
  113. fn child(cx: Scope<ChildProps>) -> Element {
  114. rsx!(cx, "child {cx.props.a}")
  115. }
  116. struct Custom {
  117. val: String,
  118. }
  119. impl Drop for Custom {
  120. fn drop(&mut self) {
  121. dbg!("dropped! {}", &self.val);
  122. }
  123. }
  124. let mut dom = new_dom(
  125. app,
  126. Custom {
  127. val: String::from("asd"),
  128. },
  129. );
  130. dom.rebuild();
  131. }
  132. #[test]
  133. fn free_works_on_borrowed() {
  134. fn app(cx: Scope) -> Element {
  135. cx.render(rsx! {
  136. child(a: "alpha", b: "asd".to_string())
  137. })
  138. }
  139. #[derive(Props)]
  140. struct ChildProps<'a> {
  141. a: &'a str,
  142. b: String,
  143. }
  144. fn child<'a>(cx: Scope<'a, ChildProps<'a>>) -> Element {
  145. dbg!("rendering child");
  146. rsx!(cx, "child {cx.props.a}, {cx.props.b}")
  147. }
  148. impl Drop for ChildProps<'_> {
  149. fn drop(&mut self) {
  150. dbg!("dropped child!");
  151. }
  152. }
  153. let mut dom = new_dom(app, ());
  154. let _ = dom.rebuild();
  155. }
  156. #[test]
  157. fn free_works_on_root_hooks() {
  158. /*
  159. On Drop, scopearena drops all the hook contents.
  160. */
  161. struct Droppable<T>(T);
  162. impl<T> Drop for Droppable<T> {
  163. fn drop(&mut self) {
  164. dbg!("dropping droppable");
  165. }
  166. }
  167. fn app(cx: Scope) -> Element {
  168. let name = cx.use_hook(|_| Droppable(String::from("asd")));
  169. rsx!(cx, div { "{name.0}" })
  170. }
  171. let mut dom = new_dom(app, ());
  172. let _ = dom.rebuild();
  173. }
  174. #[test]
  175. fn old_props_arent_stale() {
  176. fn app(cx: Scope) -> Element {
  177. dbg!("rendering parent");
  178. let cnt = cx.use_hook(|_| 0);
  179. *cnt += 1;
  180. if *cnt == 1 {
  181. rsx!(cx, div { child(a: "abcdef".to_string()) })
  182. } else {
  183. rsx!(cx, div { child(a: "abcdef".to_string()) })
  184. }
  185. }
  186. #[derive(Props, PartialEq)]
  187. struct ChildProps {
  188. a: String,
  189. }
  190. fn child(cx: Scope<ChildProps>) -> Element {
  191. dbg!("rendering child", &cx.props.a);
  192. rsx!(cx, div { "child {cx.props.a}" })
  193. }
  194. let mut dom = new_dom(app, ());
  195. let _ = dom.rebuild();
  196. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  197. dom.work_with_deadline(|| false);
  198. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  199. dom.work_with_deadline(|| false);
  200. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  201. dom.work_with_deadline(|| false);
  202. dbg!("forcing update to child");
  203. dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  204. dom.work_with_deadline(|| false);
  205. dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  206. dom.work_with_deadline(|| false);
  207. dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
  208. dom.work_with_deadline(|| false);
  209. }
  210. #[test]
  211. fn basic() {
  212. fn app(cx: Scope) -> Element {
  213. rsx!(cx, div {
  214. child(a: "abcdef".to_string())
  215. })
  216. }
  217. #[derive(Props, PartialEq)]
  218. struct ChildProps {
  219. a: String,
  220. }
  221. fn child(cx: Scope<ChildProps>) -> Element {
  222. dbg!("rendering child", &cx.props.a);
  223. rsx!(cx, div { "child {cx.props.a}" })
  224. }
  225. let mut dom = new_dom(app, ());
  226. let _ = dom.rebuild();
  227. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  228. dom.work_with_deadline(|| false);
  229. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  230. dom.work_with_deadline(|| false);
  231. }
  232. #[test]
  233. fn leak_thru_children() {
  234. fn app(cx: Scope) -> Element {
  235. cx.render(rsx! {
  236. Child {
  237. name: "asd".to_string(),
  238. }
  239. });
  240. cx.render(rsx! {
  241. div {}
  242. })
  243. }
  244. #[inline_props]
  245. fn Child(cx: Scope, name: String) -> Element {
  246. rsx!(cx, div { "child {name}" })
  247. }
  248. let mut dom = new_dom(app, ());
  249. let _ = dom.rebuild();
  250. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  251. dom.work_with_deadline(|| false);
  252. dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
  253. dom.work_with_deadline(|| false);
  254. }