File: validation.jl

package info (click to toggle)
julia 1.0.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,452 kB
  • sloc: lisp: 236,453; ansic: 55,579; cpp: 25,603; makefile: 1,685; pascal: 1,130; sh: 956; asm: 86; xml: 76
file content (132 lines) | stat: -rw-r--r-- 4,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Test, Core.IR

function f22938(a, b, x...)
    nothing
    nothing
    nothing
    nothing
    nothing
    nothing
    nothing
    d = 1
    a = d
    for i in 1:b
        d += i
    end
    return i * a
end

msig = Tuple{typeof(f22938),Int,Int,Int,Int}
world = typemax(UInt)
_, msp, m = Base._methods_by_ftype(msig, -1, world)[]
mi = Core.Compiler.code_for_method(m, msig, msp, world, false)
c0 = Core.Compiler.retrieve_code_info(mi)

@test isempty(Core.Compiler.validate_code(mi))
@test isempty(Core.Compiler.validate_code(c0))

@testset "INVALID_EXPR_HEAD" begin
    c = Core.Compiler.copy_code_info(c0)
    c.code[1] = Expr(:invalid, 1)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.INVALID_EXPR_HEAD
end

@testset "INVALID_LVALUE" begin
    c = Core.Compiler.copy_code_info(c0)
    c.code[1] = Expr(:(=), GotoNode(1), 1)
    c.code[2] = Expr(:(=), :x, 1)
    c.code[3] = Expr(:(=), 3, 1)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 3
    @test all(e.kind === Core.Compiler.INVALID_LVALUE for e in errors)
end

@testset "INVALID_RVALUE" begin
    c = Core.Compiler.copy_code_info(c0)
    c.code[1] = Expr(:(=), SlotNumber(2), GotoNode(1))
    c.code[2] = Expr(:(=), SlotNumber(2), LineNumberNode(2))
    i = 2
    for h in (:gotoifnot, :line, :const, :meta)
        c.code[i+=1] = Expr(:(=), SlotNumber(2), Expr(h))
    end
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 6
    @test count(e.kind === Core.Compiler.INVALID_RVALUE for e in errors) == 6
end

@testset "INVALID_CALL_ARG" begin
    c = Core.Compiler.copy_code_info(c0)
    c.code[1] = Expr(:(=), SlotNumber(2), Expr(:call, GlobalRef(Base,:+), SlotNumber(2), GotoNode(1)))
    c.code[2] = Expr(:call, GlobalRef(Base,:-), Expr(:call, GlobalRef(Base,:sin), GotoNode(2)), 3)
    c.code[3] = Expr(:call, LineNumberNode(2))
    i = 3
    for h in (:gotoifnot, :line, :const, :meta)
        c.code[i+=1] = Expr(:call, GlobalRef(@__MODULE__,:f), Expr(h))
    end
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 7
    @test count(e.kind === Core.Compiler.INVALID_CALL_ARG for e in errors) == 7
end

@testset "EMPTY_SLOTNAMES" begin
    c = Core.Compiler.copy_code_info(c0)
    empty!(c.slotnames)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 2
    @test any(e.kind === Core.Compiler.EMPTY_SLOTNAMES for e in errors)
    @test any(e.kind === Core.Compiler.SLOTFLAGS_MISMATCH for e in errors)
end

@testset "SLOTFLAGS_MISMATCH" begin
    c = Core.Compiler.copy_code_info(c0)
    push!(c.slotnames, :dummy)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.SLOTFLAGS_MISMATCH
end

@testset "SSAVALUETYPES_MISMATCH" begin
    c = code_typed(f22938, (Int,Int,Int,Int))[1][1]
    empty!(c.ssavaluetypes)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.SSAVALUETYPES_MISMATCH
end

@testset "SSAVALUETYPES_MISMATCH_UNINFERRED" begin
    c = Core.Compiler.copy_code_info(c0)
    c.ssavaluetypes -= 1
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.SSAVALUETYPES_MISMATCH_UNINFERRED
end

@testset "SIGNATURE_NARGS_MISMATCH" begin
    old_sig = mi.def.sig
    mi.def.sig = Tuple{1,2}
    errors = Core.Compiler.validate_code(mi)
    mi.def.sig = old_sig
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.SIGNATURE_NARGS_MISMATCH
end

@testset "NON_TOP_LEVEL_METHOD" begin
    c = Core.Compiler.copy_code_info(c0)
    c.code[1] = Expr(:method, :dummy)
    errors = Core.Compiler.validate_code(c)
    @test length(errors) == 1
    @test errors[1].kind === Core.Compiler.NON_TOP_LEVEL_METHOD
end

@testset "SLOTNAMES_NARGS_MISMATCH" begin
    mi.def.nargs += 20
    errors = Core.Compiler.validate_code(mi)
    mi.def.nargs -= 20
    @test length(errors) == 2
    @test count(e.kind === Core.Compiler.SLOTNAMES_NARGS_MISMATCH for e in errors) == 1
    @test count(e.kind === Core.Compiler.SIGNATURE_NARGS_MISMATCH for e in errors) == 1
end