﻿{"id":21693,"date":"2023-06-01T08:28:53","date_gmt":"2023-06-01T00:28:53","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=21693"},"modified":"2023-06-01T08:28:53","modified_gmt":"2023-06-01T00:28:53","slug":"unsafe-rust","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/unsafe-rust.html","title":{"rendered":"\u4ec0\u4e48\u662f\u4e0d\u5b89\u5168\u7684 Rust\uff1f"},"content":{"rendered":"<p><em>\u4e0d\u5b89\u5168\u7684Rust\uff08Unsafe Rust\uff09\u63d0\u4f9b\u4e86\u5bf9\u5185\u5b58\u66f4\u591a\u7684\u63a7\u5236\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u5e76\u7406\u89e3\u4e0e\u5176\u4f7f\u7528\u76f8\u5173\u7684\u56fa\u6709\u98ce\u9669\u3002<\/em><\/p>\n<p>\u5185\u5b58\u5b89\u5168\u6027\u662fRust\u7684\u5356\u70b9\u4e4b\u4e00\uff1b\u7136\u800c\uff0cRust\u5e76\u975e\u514d\u75ab\u4e8e\u9519\u8bef\u548c\u6f0f\u6d1e\u3002\u7531\u4e8eRust\u7684\u501f\u7528\u68c0\u67e5\u5668\u5f3a\u5236\u5b9e\u65bd\u6240\u6709\u6743\u6a21\u578b\uff0c\u5b89\u5168\u68c0\u67e5\u8fc7\u7a0b\u4e2d\u7f16\u8bd1\u65f6\u95f4\u4f1a\u7a0d\u6709\u589e\u52a0\u3002<\/p>\n<p>Rust\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a&#8221;\u4e0d\u5b89\u5168\u7684Rust<em>\uff08Unsafe Rust\uff09<\/em>&#8220;\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u7ed5\u8fc7\u5b89\u5168\u68c0\u67e5\uff0c\u4ee5\u5b9e\u73b0\u6027\u80fd\u4f18\u5316\u3002&#8221;\u4e0d\u5b89\u5168\u7684Rust&#8221;\u662f\u4f7f\u7528Rust\u7f16\u5199\u9ad8\u6548\u3001\u4f4e\u7ea7\u7cfb\u7edf\u8f6f\u4ef6\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n<p><strong>\u7406\u89e3\u4e0d\u5b89\u5168\u7684Rust<\/strong><\/p>\n<p>\u4e0d\u5b89\u5168\u7684Rust\u662f\u4e00\u7ec4\u529f\u80fd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u6765\u7ed5\u8fc7Rust\u7684\u5b89\u5168\u6027\u4fdd\u8bc1\uff0c\u4ee5\u6362\u53d6\u5bf9\u5185\u5b58\u66f4\u5927\u7684\u63a7\u5236\u6743\u3002\u4e0d\u5b89\u5168\u7684Rust\u529f\u80fd\u5305\u62ec\u539f\u59cb\u6307\u9488\u3001\u4e0d\u5b89\u5168\u51fd\u6570\u548c\u4e0d\u5b89\u5168\u7279\u6027\u3002<\/p>\n<p>\u4e0d\u5b89\u5168\u7684Rust\u7684\u76ee\u7684\u662f\u4e3a\u5f00\u53d1\u4eba\u5458\u63d0\u4f9b\u7f16\u5199\u4f4e\u7ea7\u7cfb\u7edf\u4ee3\u7801\u7684\u80fd\u529b\uff0c\u800c\u4e0d\u4f1a\u727a\u7272\u6027\u80fd\u3002\u60a8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u548c\u64cd\u4f5c\u5185\u5b58\u8d44\u6e90\uff0c\u5e76\u501f\u52a9\u4e0d\u5b89\u5168\u7684Rust\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e0d\u5b89\u5168\u7684Rust\u5728\u64cd\u4f5c\u7cfb\u7edf\u5f00\u53d1\u3001\u7f51\u7edc\u7f16\u7a0b\u548c\u6e38\u620f\u5f00\u53d1\u7b49\u5bf9\u6027\u80fd\u8981\u6c42\u9ad8\u7684\u573a\u666f\u4e2d\u7279\u522b\u6709\u7528\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u9700\u8981\u5bf9\u7a0b\u5e8f\u7684\u5185\u5b58\u5e03\u5c40\u548c\u4ee3\u7801\u884c\u4e3a\u8fdb\u884c\u7ec6\u7c92\u5ea6\u63a7\u5236\u3002\u4e0d\u5b89\u5168\u7684Rust\u901a\u8fc7\u63d0\u4f9b\u4f4e\u7ea7\u62bd\u8c61\uff0c\u7528\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4f7f\u60a8\u80fd\u591f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u8fdb\u884c\u5de5\u4f5c<\/strong><\/p>\n<p>\u4e0d\u5b89\u5168\u5757\u63d0\u4f9b\u4e86\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u529f\u80fd\u7684\u529f\u80fd\u3002\u60a8\u5c06\u4f7f\u7528unsafe\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u5305\u542b\u6709\u6548Rust\u4ee3\u7801\u7684\u4e0d\u5b89\u5168\u5757\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u60a8\u5982\u4f55\u4f7f\u7528\u4e0d\u5b89\u5168\u5757\u6765\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u4ee5\u8fdb\u884c\u503c\u4fee\u6539\u7684\u793a\u4f8b\uff1a<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">fn main() {\r\n    let mut x = 10;\r\n\r\n    unsafe {\r\n        let raw = &amp;mut x as *mut i32;\r\n        *raw = 20;\r\n    }\r\n\r\n    println!(\"x is now {}\", x);\r\n}<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21695\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2023\/06\/urs1.png\" alt=\"\" width=\"1042\" height=\"721\" \/><\/p>\n<p>\u53d8\u91cfx\u662f\u4e00\u4e2a\u53ef\u53d8\u6574\u6570\u3002\u5728\u4e0d\u5b89\u5168\u5757\u4e2d\uff0c\u5c06x\u7684\u539f\u59cb\u6307\u9488\u5206\u914d\u4e00\u4e2a\u65b0\u503c\u7ed9x\u3002\u4e0d\u5b89\u5168\u5757\u4e2d\u7684\u4ee3\u7801\u662f\u6709\u6548\u7684\u4f46\u4e0d\u5b89\u5168\u7684\uff0c\u5982\u679c\u4e0d\u5728\u4e0d\u5b89\u5168\u5757\u4e2d\uff0c\u5219\u7a0b\u5e8f\u4f1a\u5d29\u6e83\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u58f0\u660e\u4e2d\u7684fn\u5173\u952e\u5b57\u4e4b\u524d\u6dfb\u52a0unsafe\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e0d\u5b89\u5168\u51fd\u6570\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">unsafe fn perform_unsafe_operation() {\r\n    \/\/ \u5728\u6b64\u5904\u7f16\u5199\u60a8\u7684\u4e0d\u5b89\u5168\u4ee3\u7801\r\n}<\/code><\/pre>\n<p>\u60a8\u9700\u8981\u4f7f\u7528\u4e0d\u5b89\u5168\u5757\u5728\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u8c03\u7528\u4e0d\u5b89\u5168\u51fd\u6570\u3002<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"null\">fn main() {\r\n    unsafe {\r\n        perform_unsafe_operation();\r\n    }\r\n}<\/code><\/pre>\n<p>\u5728\u51fd\u6570\u524d\u6dfb\u52a0unsafe\u5173\u952e\u5b57\u5e76\u4e0d\u610f\u5473\u7740\u8be5\u51fd\u6570\u672c\u8d28\u4e0a\u662f\u5371\u9669\u7684\u3002\u5b83\u8868\u793a\u8be5\u51fd\u6570\u5305\u542b\u7684\u4ee3\u7801\u5728\u4f7f\u7528\u65f6\u9700\u8981\u989d\u5916\u5c0f\u5fc3\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u7684\u76f8\u5173\u98ce\u9669<\/strong><\/p>\n<p>\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u9519\u8bef\u3001\u6570\u636e\u7ade\u4e89\u548c\u5176\u4ed6\u5b89\u5168\u6f0f\u6d1e\u3002\u56e0\u6b64\uff0c\u4e86\u89e3\u4e0e\u4e0d\u5b89\u5168\u7684Rust\u76f8\u5173\u7684\u98ce\u9669\uff0c\u5e76\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u4ee5\u7f16\u5199\u5b89\u5168\u548c\u9ad8\u6027\u80fd\u7684\u4ee3\u7801\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<p>\u4e0e\u4e0d\u5b89\u5168\u7684Rust\u76f8\u5173\u7684\u4e3b\u8981\u98ce\u9669\u662f\u53ef\u80fd\u5bfc\u81f4\u5d29\u6e83\u3001\u5b89\u5168\u6f0f\u6d1e\u548c\u672a\u5b9a\u4e49\u4ee3\u7801\u884c\u4e3a\u7684\u4e0e\u5185\u5b58\u76f8\u5173\u7684\u9519\u8bef\u3002<\/p>\n<p>\u5f53\u7a0b\u5e8f\u5f02\u5e38\u5730\u5c1d\u8bd5\u8bbf\u95ee\u5185\u5b58\u65f6\uff0c\u4f1a\u53d1\u751f\u5185\u5b58\u9519\u8bef\uff1b\u7a0b\u5e8f\u6700\u7ec8\u4f1a\u5d29\u6e83\u6216\u8868\u73b0\u51fa\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u6570\u636e\u7ade\u4e89\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u66f4\u591a\u7a0b\u5e8f\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u5757\u5185\u5b58\uff0c\u5e76\u4e14\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4fee\u6539\u5185\u5b58\u4e2d\u7684\u503c\uff0c\u4ece\u800c\u5bfc\u81f4\u7a0b\u5e8f\u8868\u73b0\u51fa\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u6b63\u786e\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\uff0c\u53ef\u80fd\u4f1a\u5f15\u53d1\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u7f13\u51b2\u533a\u6ea2\u51fa\u53d1\u751f\u5728\u7a0b\u5e8f\u5728\u7f13\u51b2\u533a\u672b\u5c3e\u4e4b\u5916\u5199\u5165\u6570\u636e\u65f6\u3002\u7f13\u51b2\u533a\u6ea2\u51fa\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff0c\u6216\u8005\u5141\u8bb8\u653b\u51fb\u8005\u6267\u884c\u4efb\u610f\u4ee3\u7801\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u6f0f\u6d1e\u662f\u4f7f\u7528\u540e\u91ca\u653e\uff08UAF\uff09\uff0c\u5b83\u53d1\u751f\u5728\u7a0b\u5e8f\u5728\u91ca\u653e\u503c\u4e4b\u540e\u8bbf\u95ee\u5185\u5b58\u65f6\u3002UAF\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8868\u73b0\u51fa\u4e0d\u53ef\u9884\u6d4b\u7684\u884c\u4e3a\uff0c\u5e76\u53ef\u80fd\u5f15\u5165\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<p>\u7531\u4e8e\u5b58\u5728\u8fd9\u4e9b\u6f0f\u6d1e\uff0c\u5728\u5904\u7406\u4e0d\u5b89\u5168\u7684Rust\u65f6\uff0c\u6df1\u5165\u7406\u89e3Rust\u7684\u6240\u6709\u6743\u548c\u501f\u7528\u6a21\u578b\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p><strong>Rust\u7684\u5185\u5b58\u7ba1\u7406\u5177\u6709\u7075\u6d3b\u6027 <\/strong><\/p>\n<p>Rust\u7684\u6240\u6709\u6743\u6a21\u578b\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u4ece\u800c\u51cf\u5c11\u4e0e\u5185\u5b58\u76f8\u5173\u7684\u9519\u8bef\u7684\u98ce\u9669\u3002\u501f\u7528\u673a\u5236\u4f7f\u5f97\u591a\u4e2a\u53d8\u91cf\u53ef\u4ee5\u540c\u65f6\u8bbf\u95ee\u76f8\u540c\u7684\u5185\u5b58\u8d44\u6e90\uff0c\u800c\u4e0d\u4f1a\u4ea7\u751f\u51b2\u7a81\u3002<\/p>\n<p>Rust\u7684\u5185\u5b58\u7ba1\u7406\u4e3a\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u63d0\u4f9b\u4e86\u6240\u9700\u7684\u7075\u6d3b\u6027\u3001\u5b89\u5168\u6027\u548c\u6027\u80fd\uff0c\u4f7f\u5f97Rust\u6210\u4e3a\u7f16\u5199\u9ad8\u6548\u53ef\u9760\u4ee3\u7801\u7684\u5f3a\u5927\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u5b89\u5168\u7684Rust\uff08Unsafe Rust\uff09\u63d0\u4f9b\u4e86\u5bf9\u5185\u5b58\u66f4\u591a\u7684\u63a7\u5236\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u4e0d\u5b89\u5168\u7684Rust\u5e76\u7406\u89e3 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[867],"class_list":["post-21693","post","type-post","status-publish","format-standard","hentry","category-linux","tag-rust"],"aioseo_notices":[],"views":211,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=21693"}],"version-history":[{"count":2,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21693\/revisions"}],"predecessor-version":[{"id":21696,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/21693\/revisions\/21696"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=21693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=21693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=21693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}