1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #include "pxr/pxr.h"
26 #include "pxr/base/tf/errorMark.h"
27 #include "pxr/base/arch/stackTrace.h"
28 
29 #include <unistd.h>
30 
31 #include <thread>
32 #include <atomic>
33 #include <iostream>
34 
35 PXR_NAMESPACE_USING_DIRECTIVE
36 
37 // This could probably be a regular int since we're not really worried
38 // about full thread safety, just whether it is 1 or 0.
39 static std::atomic_int synchronizer;
40 
41 /**
42  * This executable performs multiple "simultaneous" invalid memory
43  * references (SIGSEGV) for testing of the Tf crash handler from
44  * multiple threads.
45  */
46 
47 static void
_ThreadTask()48 _ThreadTask()
49 {
50     TfErrorMark m;
51     TF_RUNTIME_ERROR("Pending secondary thread error for crash report!");
52 
53     // Wait for synchronizer to become 0
54     while (synchronizer) {
55         // spin!
56     }
57 
58     // Dereference a null pointer!
59     int* bunk(0);
60     std::cout << *bunk << '\n';
61 }
62 
63 int
main(int argc,char ** argv)64 main(int argc, char **argv)
65 {
66     ArchSetFatalStackLogging( true );
67 
68     TfErrorMark m;
69 
70     TF_RUNTIME_ERROR("Pending error to report in crash output!");
71 
72     // Make sure the threads don't run off and generate segmentation faults
73     // before we're ready.
74     //
75     synchronizer = 1;
76 
77     // Spawn 2 threads, each of which will wait for synchronizer to
78     // become 0 and then generate a SIGSEGV. The desire is to produce
79     // two SIGSEGV signals in two different threads at very nearly the
80     // same time.
81     //
82     std::thread t1(_ThreadTask);
83     std::thread t2(_ThreadTask);
84 
85     // Wait to ensure the threads are spinning on synchronizer
86     sleep(1);
87 
88     // Release them.
89     synchronizer = 0;
90 
91     // Wait for them to die
92     t1.join();
93     t2.join();
94 
95     return 0;
96 }
97 
98 
99