test_payload_full.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import os.path
  2. import zipfile
  3. from io import BytesIO
  4. from unittest.mock import patch
  5. from uuid import uuid4
  6. import pytest
  7. from django.core.files.uploadedfile import SimpleUploadedFile
  8. from django.urls import reverse
  9. from sentry import eventstore
  10. from sentry.models import (
  11. ArtifactBundle,
  12. DebugIdArtifactBundle,
  13. File,
  14. ProjectArtifactBundle,
  15. ProjectDebugFile,
  16. SourceFileType,
  17. )
  18. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  19. from sentry.testutils.factories import get_fixture_path
  20. from sentry.testutils.helpers.datetime import before_now, iso_format
  21. from sentry.testutils.helpers.options import override_options
  22. from sentry.utils import json
  23. from tests.symbolicator import insta_snapshot_native_stacktrace_data, redact_location
  24. # IMPORTANT:
  25. #
  26. # This test suite requires Symbolicator in order to run correctly.
  27. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  28. #
  29. # If you are using a local instance of Symbolicator, you need to
  30. # either change `system.url-prefix` option override inside `initialize` fixture to `system.internal-url-prefix`,
  31. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  32. REAL_RESOLVING_EVENT_DATA = {
  33. "platform": "cocoa",
  34. "debug_meta": {
  35. "images": [
  36. {
  37. "type": "apple",
  38. "arch": "x86_64",
  39. "uuid": "502fc0a5-1ec1-3e47-9998-684fa139dca7",
  40. "image_vmaddr": "0x0000000100000000",
  41. "image_size": 4096,
  42. "image_addr": "0x0000000100000000",
  43. "name": "Foo.app/Contents/Foo",
  44. }
  45. ],
  46. "sdk_info": {
  47. "dsym_type": "macho",
  48. "sdk_name": "macOS",
  49. "version_major": 10,
  50. "version_minor": 12,
  51. "version_patchlevel": 4,
  52. },
  53. },
  54. "exception": {
  55. "values": [
  56. {
  57. "stacktrace": {
  58. "frames": [
  59. {"platform": "foobar", "function": "hi"},
  60. {"function": "unknown", "instruction_addr": "0x0000000100000fa0"},
  61. ]
  62. },
  63. "type": "Fail",
  64. "value": "fail",
  65. }
  66. ]
  67. },
  68. "timestamp": iso_format(before_now(seconds=1)),
  69. }
  70. def get_local_fixture_path(name):
  71. return os.path.join(os.path.dirname(__file__), "fixtures", name)
  72. def load_fixture(name):
  73. with open(get_local_fixture_path(name), "rb") as fp:
  74. return fp.read()
  75. class SymbolicatorResolvingIntegrationTest(RelayStoreHelper, TransactionTestCase):
  76. @pytest.fixture(autouse=True)
  77. def initialize(self, live_server):
  78. self.project.update_option("sentry:builtin_symbol_sources", [])
  79. self.min_ago = iso_format(before_now(minutes=1))
  80. with patch("sentry.auth.system.is_internal_ip", return_value=True), self.options(
  81. {"system.url-prefix": live_server.url}
  82. ):
  83. # Run test case
  84. yield
  85. def get_event(self, event_id):
  86. return eventstore.get_event_by_id(self.project.id, event_id)
  87. def test_real_resolving(self):
  88. url = reverse(
  89. "sentry-api-0-dsym-files",
  90. kwargs={
  91. "organization_slug": self.project.organization.slug,
  92. "project_slug": self.project.slug,
  93. },
  94. )
  95. self.login_as(user=self.user)
  96. out = BytesIO()
  97. f = zipfile.ZipFile(out, "w")
  98. f.write(get_fixture_path("native", "hello.dsym"), "dSYM/hello")
  99. f.close()
  100. response = self.client.post(
  101. url,
  102. {
  103. "file": SimpleUploadedFile(
  104. "symbols.zip", out.getvalue(), content_type="application/zip"
  105. )
  106. },
  107. format="multipart",
  108. )
  109. assert response.status_code == 201, response.content
  110. assert len(response.data) == 1
  111. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  112. assert event.data["culprit"] == "main"
  113. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  114. redact_location(candidates)
  115. event.data["debug_meta"]["images"][0]["candidates"] = candidates
  116. insta_snapshot_native_stacktrace_data(self, event.data)
  117. def test_debug_id_resolving(self):
  118. file = File.objects.create(
  119. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  120. )
  121. path = get_fixture_path("native", "windows.sym")
  122. with open(path, "rb") as f:
  123. file.putfile(f)
  124. ProjectDebugFile.objects.create(
  125. file=file,
  126. object_name="crash.pdb",
  127. cpu_name="x86",
  128. project_id=self.project.id,
  129. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  130. code_id="5AB380779000",
  131. )
  132. self.login_as(user=self.user)
  133. event_data = {
  134. "contexts": {
  135. "device": {"arch": "x86"},
  136. "os": {"build": "", "name": "Windows", "type": "os", "version": "10.0.14393"},
  137. },
  138. "debug_meta": {
  139. "images": [
  140. {
  141. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  142. "image_addr": "0x2a0000",
  143. "image_size": 36864,
  144. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  145. "type": "symbolic",
  146. }
  147. ]
  148. },
  149. "exception": {
  150. "stacktrace": {
  151. "frames": [
  152. {
  153. "function": "<unknown>",
  154. "instruction_addr": "0x2a2a3d",
  155. "package": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  156. }
  157. ]
  158. },
  159. "thread_id": 1636,
  160. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  161. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  162. },
  163. "platform": "native",
  164. "timestamp": iso_format(before_now(seconds=1)),
  165. }
  166. event = self.post_and_retrieve_event(event_data)
  167. assert event.data["culprit"] == "main"
  168. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  169. redact_location(candidates)
  170. event.data["debug_meta"]["images"][0]["candidates"] = candidates
  171. insta_snapshot_native_stacktrace_data(self, event.data)
  172. def test_missing_dsym(self):
  173. self.login_as(user=self.user)
  174. event = self.post_and_retrieve_event(REAL_RESOLVING_EVENT_DATA)
  175. assert event.data["culprit"] == "unknown"
  176. insta_snapshot_native_stacktrace_data(self, event.data)
  177. def test_missing_debug_images(self):
  178. self.login_as(user=self.user)
  179. payload = dict(project=self.project.id, **REAL_RESOLVING_EVENT_DATA)
  180. del payload["debug_meta"]
  181. event = self.post_and_retrieve_event(payload)
  182. assert event.data["culprit"] == "unknown"
  183. insta_snapshot_native_stacktrace_data(self, event.data)
  184. def test_resolving_with_candidates_sentry_source(self):
  185. # Checks the candidates with a sentry source URI for location
  186. file = File.objects.create(
  187. name="crash.pdb", type="default", headers={"Content-Type": "text/x-breakpad"}
  188. )
  189. path = get_fixture_path("native", "windows.sym")
  190. with open(path, "rb") as f:
  191. file.putfile(f)
  192. ProjectDebugFile.objects.create(
  193. file=file,
  194. object_name="crash.pdb",
  195. cpu_name="x86",
  196. project_id=self.project.id,
  197. debug_id="3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  198. code_id="5AB380779000",
  199. )
  200. self.login_as(user=self.user)
  201. event_data = {
  202. "contexts": {
  203. "device": {"arch": "x86"},
  204. },
  205. "debug_meta": {
  206. "images": [
  207. {
  208. "id": "3249d99d-0c40-4931-8610-f4e4fb0b6936-1",
  209. "image_addr": "0x2a0000",
  210. "image_size": 36864,
  211. "name": "C:\\projects\\breakpad-tools\\windows\\Release\\crash.exe",
  212. "type": "symbolic",
  213. }
  214. ]
  215. },
  216. "exception": {
  217. "stacktrace": {
  218. "frames": [
  219. {
  220. "instruction_addr": "0x2a2a3d",
  221. }
  222. ]
  223. },
  224. "type": "EXCEPTION_ACCESS_VIOLATION_WRITE",
  225. "value": "Fatal Error: EXCEPTION_ACCESS_VIOLATION_WRITE",
  226. },
  227. "platform": "native",
  228. "timestamp": iso_format(before_now(seconds=1)),
  229. }
  230. event = self.post_and_retrieve_event(event_data)
  231. assert event.data["culprit"] == "main"
  232. candidates = event.data["debug_meta"]["images"][0]["candidates"]
  233. redact_location(candidates)
  234. self.insta_snapshot(candidates)
  235. def test_resolve_mixed_stack_trace(self):
  236. # JS debug files:
  237. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  238. compressed = BytesIO(b"SYSB")
  239. with zipfile.ZipFile(compressed, "a") as zip_file:
  240. zip_file.writestr("files/_/_/test.min.js", load_fixture("test.min.js"))
  241. zip_file.writestr("files/_/_/test.map", load_fixture("test.map"))
  242. zip_file.writestr(
  243. "manifest.json",
  244. json.dumps(
  245. {
  246. "files": {
  247. "files/_/_/test.min.js": {
  248. "url": "~/test.min.js",
  249. "type": "minified_source",
  250. "headers": {
  251. "debug-id": debug_id,
  252. "sourcemap": "test.map",
  253. },
  254. },
  255. "files/_/_/test.map": {
  256. "url": "~/file.wc.sourcemap.js",
  257. "type": "source_map",
  258. "headers": {
  259. "debug-id": debug_id,
  260. },
  261. },
  262. },
  263. }
  264. ),
  265. )
  266. compressed.seek(0)
  267. bundle_file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  268. bundle_file.putfile(compressed)
  269. artifact_bundle = ArtifactBundle.objects.create(
  270. organization_id=self.organization.id,
  271. bundle_id=uuid4(),
  272. file=bundle_file,
  273. artifact_count=2,
  274. )
  275. ProjectArtifactBundle.objects.create(
  276. organization_id=self.organization.id,
  277. project_id=self.project.id,
  278. artifact_bundle=artifact_bundle,
  279. )
  280. DebugIdArtifactBundle.objects.create(
  281. organization_id=self.organization.id,
  282. debug_id=debug_id,
  283. artifact_bundle=artifact_bundle,
  284. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  285. )
  286. DebugIdArtifactBundle.objects.create(
  287. organization_id=self.organization.id,
  288. debug_id=debug_id,
  289. artifact_bundle=artifact_bundle,
  290. source_file_type=SourceFileType.SOURCE_MAP.value,
  291. )
  292. # native debug files:
  293. wasm_file = File.objects.create(
  294. name="test.wasm", type="default", headers={"Content-Type": "application/wasm"}
  295. )
  296. with open(get_local_fixture_path("a18fd85d4a4eb893022d6bfad846b1.debug"), "rb") as f:
  297. wasm_file.putfile(f)
  298. ProjectDebugFile.objects.create(
  299. file=wasm_file,
  300. object_name="test.wasm",
  301. cpu_name="wasm32",
  302. project_id=self.project.id,
  303. debug_id="bda18fd8-5d4a-4eb8-9302-2d6bfad846b1",
  304. code_id="bda18fd85d4a4eb893022d6bfad846b1",
  305. )
  306. data = {
  307. "timestamp": self.min_ago,
  308. "message": "hello",
  309. "platform": "javascript",
  310. "release": "abc",
  311. "exception": {
  312. "values": [
  313. {
  314. "type": "Error",
  315. "stacktrace": {
  316. "frames": [
  317. {
  318. "abs_path": "http://example.com/test.min.js",
  319. "lineno": 1,
  320. "colno": 183,
  321. },
  322. {
  323. "platform": "native",
  324. "instruction_addr": "0x8c",
  325. "addr_mode": "rel:0",
  326. },
  327. ]
  328. },
  329. }
  330. ]
  331. },
  332. "debug_meta": {
  333. "images": [
  334. {
  335. "type": "sourcemap",
  336. "debug_id": debug_id,
  337. "code_file": "http://example.com/test.min.js",
  338. },
  339. {
  340. "type": "wasm",
  341. "debug_id": "bda18fd8-5d4a-4eb8-9302-2d6bfad846b1",
  342. "code_id": "bda18fd85d4a4eb893022d6bfad846b1",
  343. "debug_file": "file://foo.invalid/demo.wasm",
  344. },
  345. ]
  346. },
  347. }
  348. with override_options(
  349. {
  350. "symbolicator.sourcemaps-processing-sample-rate": 1.0,
  351. "symbolicator.sourcemap-lookup-id-rate": 1.0,
  352. }
  353. ):
  354. event = self.post_and_retrieve_event(data)
  355. exception = event.interfaces["exception"]
  356. frames = exception.values[0].stacktrace.frames
  357. assert frames[0].abs_path == "http://example.com/test.js"
  358. assert frames[0].lineno == 20
  359. assert frames[0].colno == 5
  360. assert frames[0].context_line == " invoke(data);"
  361. assert frames[1].abs_path == "/Users/mitsuhiko/Development/wasm-example/simple/src/lib.rs"
  362. assert frames[1].lineno == 19
  363. assert frames[1].function == "internal_func"
  364. images = event.data["debug_meta"]["images"]
  365. assert images[1]["debug_status"] == "found"