test_payload_full.py 14 KB

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