Skip to content

Commit 0903278

Browse files
committed
Enable some try catch analysis on analyzegc + fix found issues + fix static analyser on macos
1 parent 715c50d commit 0903278

File tree

16 files changed

+85
-27
lines changed

16 files changed

+85
-27
lines changed

Make.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ endif
14871487
CLANGSA_FLAGS :=
14881488
CLANGSA_CXXFLAGS :=
14891489
ifeq ($(OS), Darwin) # on new XCode, the files are hidden
1490-
CLANGSA_FLAGS += -isysroot $(shell xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
1490+
CLANGSA_FLAGS += -isysroot $(shell xcrun --show-sdk-path -sdk macosx)
14911491
endif
14921492
ifeq ($(USEGCC),1)
14931493
# try to help clang find the c++ files for CC by guessing the value for --prefix

src/ast.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ static jl_value_t *jl_invoke_julia_macro(jl_array_t *args, jl_module_t *inmodule
11201120
ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
11211121
if (ct->world_age > world)
11221122
ct->world_age = world;
1123-
jl_value_t *result;
1123+
jl_value_t *result = NULL;
11241124
JL_TRY {
11251125
margs[0] = jl_toplevel_eval(*ctx, margs[0]);
11261126
jl_method_instance_t *mfunc = jl_method_lookup(margs, nargs, ct->world_age);
@@ -1132,6 +1132,7 @@ static jl_value_t *jl_invoke_julia_macro(jl_array_t *args, jl_module_t *inmodule
11321132
jl_timing_show_macro(mfunc, margs[1], inmodule, JL_TIMING_DEFAULT_BLOCK);
11331133
*ctx = mfunc->def.method->module;
11341134
result = jl_invoke(margs[0], &margs[1], nargs - 1, mfunc);
1135+
margs[2] = result;
11351136
}
11361137
JL_CATCH {
11371138
if ((jl_loaderror_type == NULL) || !throw_load_error) {

src/builtins.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ JL_CALLABLE(jl_f__apply_pure)
845845
jl_task_t *ct = jl_current_task;
846846
int last_in = ct->ptls->in_pure_callback;
847847
jl_value_t *ret = NULL;
848+
JL_GC_PUSH1(&ret);
848849
JL_TRY {
849850
ct->ptls->in_pure_callback = 1;
850851
// because this function was declared pure,
@@ -862,6 +863,7 @@ JL_CALLABLE(jl_f__apply_pure)
862863
ct->ptls->in_pure_callback = last_in;
863864
jl_rethrow();
864865
}
866+
JL_GC_POP();
865867
return ret;
866868
}
867869

@@ -903,6 +905,7 @@ JL_CALLABLE(jl_f__call_in_world_total)
903905
jl_task_t *ct = jl_current_task;
904906
int last_in = ct->ptls->in_pure_callback;
905907
jl_value_t *ret = NULL;
908+
JL_GC_PUSH1(&ret);
906909
size_t last_age = ct->world_age;
907910
JL_TRY {
908911
ct->ptls->in_pure_callback = 1;
@@ -918,6 +921,7 @@ JL_CALLABLE(jl_f__call_in_world_total)
918921
ct->ptls->in_pure_callback = last_in;
919922
jl_rethrow();
920923
}
924+
JL_GC_POP();
921925
return ret;
922926
}
923927

@@ -2137,6 +2141,7 @@ JL_CALLABLE(jl_f__typebody)
21372141
{
21382142
JL_NARGS(_typebody!, 1, 2);
21392143
jl_datatype_t *dt = (jl_datatype_t*)jl_unwrap_unionall(args[0]);
2144+
JL_GC_PUSH1(&dt);
21402145
JL_TYPECHK(_typebody!, datatype, (jl_value_t*)dt);
21412146
if (nargs == 2) {
21422147
jl_value_t *ft = args[1];
@@ -2186,6 +2191,7 @@ JL_CALLABLE(jl_f__typebody)
21862191

21872192
if (jl_is_structtype(dt))
21882193
jl_compute_field_offsets(dt);
2194+
JL_GC_POP();
21892195
return jl_nothing;
21902196
}
21912197

src/codegen.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2843,14 +2843,16 @@ static jl_value_t *static_apply_type(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> arg
28432843
size_t last_age = jl_current_task->world_age;
28442844
// call apply_type, but ignore errors. we know that will work in world 1.
28452845
jl_current_task->world_age = 1;
2846-
jl_value_t *result;
2846+
jl_value_t *result = NULL;
2847+
JL_GC_PUSH1(&result);
28472848
JL_TRY {
28482849
result = jl_apply(v.data(), nargs);
28492850
}
28502851
JL_CATCH {
28512852
result = NULL;
28522853
}
28532854
jl_current_task->world_age = last_age;
2855+
JL_GC_POP();
28542856
return result;
28552857
}
28562858

@@ -2932,6 +2934,7 @@ static jl_value_t *static_eval(jl_codectx_t &ctx, jl_value_t *ex)
29322934
jl_value_t *result;
29332935
JL_TRY {
29342936
result = jl_apply(v, n+1);
2937+
v[0] = f;
29352938
}
29362939
JL_CATCH {
29372940
result = NULL;

src/gf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ jl_code_instance_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int fo
391391
ct->reentrant_timing += 0b10;
392392
JL_TRY {
393393
ci = (jl_code_instance_t*)jl_apply(fargs, 4);
394+
fargs[2] = (jl_value_t*)ci;
394395
}
395396
JL_CATCH {
396397
jl_value_t *e = jl_current_exception();

src/interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ static jl_value_t *eval_body(jl_array_t *stmts, interpreter_state *s, size_t ip,
540540
jl_unreachable();
541541
}
542542
}
543-
jl_eh_restore_state(&__eh);
543+
jl_eh_restore_state(&__eh, 1);
544544
if (s->continue_at) { // means we reached a :leave expression
545545
ip = s->continue_at;
546546
s->continue_at = 0;

src/jlapi.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static void _jl_exception_clear(jl_task_t *ct) JL_NOTSAFEPOINT
146146
*/
147147
JL_DLLEXPORT jl_value_t *jl_eval_string(const char *str)
148148
{
149-
jl_value_t *r;
149+
jl_value_t *r = NULL;
150+
JL_GC_PUSH1(&r);
150151
jl_task_t *ct = jl_current_task;
151152
JL_TRY {
152153
const char filename[] = "none";
@@ -161,6 +162,7 @@ JL_DLLEXPORT jl_value_t *jl_eval_string(const char *str)
161162
ct->ptls->previous_exception = jl_current_exception();
162163
r = NULL;
163164
}
165+
JL_GC_POP();
164166
return r;
165167
}
166168

@@ -283,7 +285,8 @@ JL_DLLEXPORT const char *jl_string_ptr(jl_value_t *s)
283285
*/
284286
JL_DLLEXPORT jl_value_t *jl_call(jl_function_t *f, jl_value_t **args, uint32_t nargs)
285287
{
286-
jl_value_t *v;
288+
jl_value_t *v = NULL;
289+
JL_GC_PUSH1(&v);
287290
jl_task_t *ct = jl_current_task;
288291
nargs++; // add f to args
289292
JL_TRY {
@@ -303,6 +306,7 @@ JL_DLLEXPORT jl_value_t *jl_call(jl_function_t *f, jl_value_t **args, uint32_t n
303306
ct->ptls->previous_exception = jl_current_exception();
304307
v = NULL;
305308
}
309+
JL_GC_POP();
306310
return v;
307311
}
308312

@@ -316,7 +320,8 @@ JL_DLLEXPORT jl_value_t *jl_call(jl_function_t *f, jl_value_t **args, uint32_t n
316320
*/
317321
JL_DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
318322
{
319-
jl_value_t *v;
323+
jl_value_t *v = NULL;
324+
JL_GC_PUSH1(&v);
320325
jl_task_t *ct = jl_current_task;
321326
JL_TRY {
322327
JL_GC_PUSH1(&f);
@@ -331,6 +336,7 @@ JL_DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
331336
ct->ptls->previous_exception = jl_current_exception();
332337
v = NULL;
333338
}
339+
JL_GC_POP();
334340
return v;
335341
}
336342

@@ -345,7 +351,8 @@ JL_DLLEXPORT jl_value_t *jl_call0(jl_function_t *f)
345351
*/
346352
JL_DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
347353
{
348-
jl_value_t *v;
354+
jl_value_t *v = NULL;
355+
JL_GC_PUSH1(&v);
349356
jl_task_t *ct = jl_current_task;
350357
JL_TRY {
351358
jl_value_t **argv;
@@ -363,6 +370,7 @@ JL_DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
363370
ct->ptls->previous_exception = jl_current_exception();
364371
v = NULL;
365372
}
373+
JL_GC_POP();
366374
return v;
367375
}
368376

@@ -378,7 +386,8 @@ JL_DLLEXPORT jl_value_t *jl_call1(jl_function_t *f, jl_value_t *a)
378386
*/
379387
JL_DLLEXPORT jl_value_t *jl_call2(jl_function_t *f, jl_value_t *a, jl_value_t *b)
380388
{
381-
jl_value_t *v;
389+
jl_value_t *v = NULL;
390+
JL_GC_PUSH1(&v);
382391
jl_task_t *ct = jl_current_task;
383392
JL_TRY {
384393
jl_value_t **argv;
@@ -397,6 +406,7 @@ JL_DLLEXPORT jl_value_t *jl_call2(jl_function_t *f, jl_value_t *a, jl_value_t *b
397406
ct->ptls->previous_exception = jl_current_exception();
398407
v = NULL;
399408
}
409+
JL_GC_POP();
400410
return v;
401411
}
402412

@@ -414,7 +424,8 @@ JL_DLLEXPORT jl_value_t *jl_call2(jl_function_t *f, jl_value_t *a, jl_value_t *b
414424
JL_DLLEXPORT jl_value_t *jl_call3(jl_function_t *f, jl_value_t *a,
415425
jl_value_t *b, jl_value_t *c)
416426
{
417-
jl_value_t *v;
427+
jl_value_t *v = NULL;
428+
JL_GC_PUSH1(&v);
418429
jl_task_t *ct = jl_current_task;
419430
JL_TRY {
420431
jl_value_t **argv;
@@ -434,6 +445,7 @@ JL_DLLEXPORT jl_value_t *jl_call3(jl_function_t *f, jl_value_t *a,
434445
ct->ptls->previous_exception = jl_current_exception();
435446
v = NULL;
436447
}
448+
JL_GC_POP();
437449
return v;
438450
}
439451

@@ -446,7 +458,8 @@ JL_DLLEXPORT jl_value_t *jl_call3(jl_function_t *f, jl_value_t *a,
446458
*/
447459
JL_DLLEXPORT jl_value_t *jl_get_field(jl_value_t *o, const char *fld)
448460
{
449-
jl_value_t *v;
461+
jl_value_t *v = NULL;
462+
JL_GC_PUSH1(&v);
450463
JL_TRY {
451464
jl_value_t *s = (jl_value_t*)jl_symbol(fld);
452465
int i = jl_field_index((jl_datatype_t*)jl_typeof(o), (jl_sym_t*)s, 1);
@@ -457,6 +470,7 @@ JL_DLLEXPORT jl_value_t *jl_get_field(jl_value_t *o, const char *fld)
457470
jl_current_task->ptls->previous_exception = jl_current_exception();
458471
v = NULL;
459472
}
473+
JL_GC_POP();
460474
return v;
461475
}
462476

src/jltypes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,7 @@ static jl_value_t *_jl_instantiate_type_in_env(jl_value_t *ty, jl_unionall_t *en
24732473
JL_DLLEXPORT jl_value_t *jl_instantiate_type_in_env(jl_value_t *ty, jl_unionall_t *env, jl_value_t **vals)
24742474
{
24752475
jl_value_t *typ = ty;
2476+
JL_GC_PUSH1(&typ);
24762477
if (jl_is_unionall(env)) {
24772478
JL_TRY {
24782479
typ = _jl_instantiate_type_in_env(ty, env, vals, NULL, NULL);
@@ -2481,6 +2482,7 @@ JL_DLLEXPORT jl_value_t *jl_instantiate_type_in_env(jl_value_t *ty, jl_unionall_
24812482
typ = jl_bottom_type;
24822483
}
24832484
}
2485+
JL_GC_POP();
24842486
return typ;
24852487
}
24862488

src/julia.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,8 +2282,8 @@ extern JL_DLLIMPORT int jl_task_ptls_offset;
22822282

22832283
#include "julia_locks.h" // requires jl_task_t definition
22842284

2285-
JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh);
2286-
JL_DLLEXPORT void jl_eh_restore_state(jl_handler_t *eh);
2285+
JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh) JL_NOTSAFEPOINT ;
2286+
JL_DLLEXPORT void jl_eh_restore_state(jl_handler_t *eh, int had_exception);
22872287
JL_DLLEXPORT void jl_pop_handler(int n);
22882288
JL_DLLEXPORT size_t jl_excstack_state(void) JL_NOTSAFEPOINT;
22892289
JL_DLLEXPORT void jl_restore_excstack(size_t state) JL_NOTSAFEPOINT;
@@ -2337,24 +2337,32 @@ extern void (*real_siglongjmp)(jmp_buf _Buf, int _Value);
23372337

23382338
#ifdef __clang_gcanalyzer__
23392339

2340+
extern int had_exception;
23402341
// This is hard. Ideally we'd teach the static analyzer about the extra control
23412342
// flow edges. But for now, just hide this as best we can
2342-
extern int had_exception;
2343-
#define JL_TRY if (1)
2344-
#define JL_CATCH if (had_exception)
2343+
#define JL_TRY \
2344+
int i__try, i__catch; jl_handler_t __eh; \
2345+
size_t __excstack_state = jl_excstack_state(); \
2346+
jl_enter_handler(&__eh); \
2347+
for (i__try=2; i__try; i__try--) \
2348+
if (i__try==2) \
2349+
2350+
#define JL_CATCH \
2351+
else \
2352+
for (i__catch=1, jl_eh_restore_state(&__eh, had_exception); i__catch && had_exception; i__catch=0, jl_restore_excstack(__excstack_state))
23452353

23462354
#else
23472355

23482356
#define JL_TRY \
2349-
int i__tr, i__ca; jl_handler_t __eh; \
2357+
int i__try, i__catch; jl_handler_t __eh; \
23502358
size_t __excstack_state = jl_excstack_state(); \
23512359
jl_enter_handler(&__eh); \
23522360
if (!jl_setjmp(__eh.eh_ctx,0)) \
2353-
for (i__tr=1; i__tr; i__tr=0, jl_eh_restore_state(&__eh))
2361+
for (i__try=1; i__try; i__try=0, jl_eh_restore_state(&__eh, 0))
23542362

23552363
#define JL_CATCH \
23562364
else \
2357-
for (i__ca=1, jl_eh_restore_state(&__eh); i__ca; i__ca=0, jl_restore_excstack(__excstack_state))
2365+
for (i__catch=1, jl_eh_restore_state(&__eh, 1); i__catch; i__catch=0, jl_restore_excstack(__excstack_state))
23582366

23592367
#endif
23602368

src/method.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_module_t *module, jl_sve
137137
}
138138
jl_value_t *rt = jl_exprarg(e, 2);
139139
jl_value_t *at = jl_exprarg(e, 3);
140+
JL_GC_PUSH2(&at, &rt);
140141
if (!jl_is_type(rt)) {
141142
JL_TRY {
142143
rt = jl_interpret_toplevel_expr_in(module, rt, NULL, sparam_vals);
@@ -164,12 +165,14 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_module_t *module, jl_sve
164165
check_c_types("cfunction method definition", rt, at);
165166
JL_TYPECHK(cfunction method definition, quotenode, jl_exprarg(e, 4));
166167
JL_TYPECHK(cfunction method definition, symbol, *(jl_value_t**)jl_exprarg(e, 4));
168+
JL_GC_POP();
167169
return expr;
168170
}
169171
if (e->head == jl_foreigncall_sym) {
170172
JL_NARGSV(ccall method definition, 5); // (fptr, rt, at, nreq, (cc, effects))
171173
jl_value_t *rt = jl_exprarg(e, 1);
172174
jl_value_t *at = jl_exprarg(e, 2);
175+
JL_GC_PUSH2(&at, &rt);
173176
if (!jl_is_type(rt)) {
174177
JL_TRY {
175178
rt = jl_interpret_toplevel_expr_in(module, rt, NULL, sparam_vals);
@@ -208,6 +211,7 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_module_t *module, jl_sve
208211
}
209212
jl_exprargset(e, 0, resolve_globals(jl_exprarg(e, 0), module, sparam_vals, binding_effects, 1));
210213
i++;
214+
JL_GC_POP();
211215
}
212216
if (e->head == jl_method_sym || e->head == jl_module_sym) {
213217
i++;
@@ -259,11 +263,13 @@ static jl_value_t *resolve_globals(jl_value_t *expr, jl_module_t *module, jl_sve
259263
}
260264
if (j == nargs) {
261265
jl_value_t *val = NULL;
266+
JL_GC_PUSH1(&val);
262267
JL_TRY {
263268
val = jl_interpret_toplevel_expr_in(module, (jl_value_t*)e, NULL, sparam_vals);
264269
}
265270
JL_CATCH {
266271
}
272+
JL_GC_POP();
267273
if (val)
268274
return val;
269275
}

0 commit comments

Comments
 (0)