|
1 | 1 | // |
2 | | - // IRCallerCapabilityChecks.swift |
3 | | - // IRGen |
4 | | - // |
5 | | - // Created by Hails, Daniel R on 11/07/2018. |
6 | | - // |
7 | | - |
8 | | - import AST |
9 | | - |
10 | | - /// Checks whether the caller of a function has appropriate caller capabilities. |
11 | | - struct IRCallerCapabilityChecks { |
12 | | - static let postfix: String = "CallerCheck" |
13 | | - static let varName: String = "_flint" + postfix |
14 | | - |
15 | | - var variableName: String |
16 | | - var callerCapabilities: [CallerCapability] |
17 | | - let revert: Bool |
18 | | - |
19 | | - init(callerCapabilities: [CallerCapability], revert: Bool = true, variableName: String = varName) { |
20 | | - self.variableName = variableName |
21 | | - self.callerCapabilities = callerCapabilities |
22 | | - self.revert = revert |
23 | | - } |
24 | | - |
25 | | - func rendered(enclosingType: RawTypeIdentifier, environment: Environment) -> String { |
26 | | - let checks = callerCapabilities.compactMap { callerCapability -> String? in |
27 | | - guard !callerCapability.isAny else { return nil } |
28 | | - |
29 | | - let type = environment.type(of: callerCapability.identifier.name, enclosingType: enclosingType) |
30 | | - let offset = environment.propertyOffset(for: callerCapability.name, enclosingType: enclosingType)! |
31 | | - |
32 | | - switch type { |
33 | | - case .fixedSizeArrayType(_, let size): |
34 | | - return (0..<size).map { index in |
35 | | - let check = IRRuntimeFunction.isValidCallerCapability(address: "sload(add(\(offset), \(index)))") |
36 | | - return "\(variableName) := add(\(variableName), \(check)" |
37 | | - }.joined(separator: "\n") |
38 | | - case .arrayType(_): |
39 | | - let check = IRRuntimeFunction.isCallerCapabilityInArray(arrayOffset: offset) |
40 | | - return "\(variableName) := add(\(variableName), \(check))" |
41 | | - default: |
42 | | - let check = IRRuntimeFunction.isValidCallerCapability(address: "sload(\(offset)))") |
43 | | - return "\(variableName) := add(\(variableName), \(check)" |
44 | | - } |
45 | | - } |
46 | | - let revertString = revert ? "if eq(\(variableName), 0) { revert(0, 0) }" : "" |
47 | | - if !checks.isEmpty { |
48 | | - return """ |
49 | | - let \(variableName) := 0 |
50 | | - \(checks.joined(separator: "\n")) |
51 | | - \(revertString) |
52 | | - """ |
53 | | - } |
54 | | - |
55 | | - return "" |
56 | | - } |
57 | | - } |
| 2 | +// IRCallerCapabilityChecks.swift |
| 3 | +// IRGen |
| 4 | +// |
| 5 | +// Created by Hails, Daniel R on 11/07/2018. |
| 6 | +// |
| 7 | + |
| 8 | +import AST |
| 9 | + |
| 10 | +/// Checks whether the caller of a function has appropriate caller capabilities. |
| 11 | +struct IRCallerCapabilityChecks { |
| 12 | + static let postfix: String = "CallerCheck" |
| 13 | + static let varName: String = "_flint" + postfix |
| 14 | + |
| 15 | + var variableName: String |
| 16 | + var callerCapabilities: [CallerCapability] |
| 17 | + let revert: Bool |
| 18 | + |
| 19 | + init(callerCapabilities: [CallerCapability], revert: Bool = true, variableName: String = varName) { |
| 20 | + self.variableName = variableName |
| 21 | + self.callerCapabilities = callerCapabilities |
| 22 | + self.revert = revert |
| 23 | + } |
| 24 | + |
| 25 | + func rendered(enclosingType: RawTypeIdentifier, environment: Environment) -> String { |
| 26 | + let checks = callerCapabilities.compactMap { callerCapability -> String? in |
| 27 | + guard !callerCapability.isAny else { return nil } |
| 28 | + |
| 29 | + let type = environment.type(of: callerCapability.identifier.name, enclosingType: enclosingType) |
| 30 | + let offset = environment.propertyOffset(for: callerCapability.name, enclosingType: enclosingType) |
| 31 | + let functionContext = FunctionContext(environment: environment, scopeContext: ScopeContext(), enclosingTypeName: enclosingType, isInStructFunction: false) |
| 32 | + |
| 33 | + switch type { |
| 34 | + case .functionType(parameters: [], result: .basicType(.address)): |
| 35 | + var identifier = callerCapability.identifier |
| 36 | + let name = Mangler.mangleFunctionName(identifier.name, parameterTypes: [], enclosingType: enclosingType) |
| 37 | + identifier.identifierToken.kind = .identifier(name) |
| 38 | + let functionCall = IRFunctionCall(functionCall: FunctionCall(identifier: identifier, arguments: [], closeBracketToken: .init(kind: .punctuation(.closeBracket), sourceLocation: .DUMMY), isAttempted: false)) |
| 39 | + let check = "eq(caller(), \(functionCall.rendered(functionContext: functionContext)))" |
| 40 | + return "\(variableName) := add(\(variableName), \(check))" |
| 41 | + case .functionType(parameters: [.basicType(.address)], result: .basicType(.bool)): |
| 42 | + var identifier = callerCapability.identifier |
| 43 | + let name = Mangler.mangleFunctionName(identifier.name, parameterTypes: [.basicType(.address)], enclosingType: enclosingType) |
| 44 | + identifier.identifierToken.kind = .identifier(name) |
| 45 | + let functionCall = IRFunctionCall(functionCall: FunctionCall(identifier: identifier, arguments: [FunctionArgument(.rawAssembly("caller()", resultType: .basicType(.address)))], closeBracketToken: .init(kind: .punctuation(.closeBracket), sourceLocation: .DUMMY), isAttempted: false)) |
| 46 | + let check = "\(functionCall.rendered(functionContext: functionContext))" |
| 47 | + return "\(variableName) := add(\(variableName), \(check))" |
| 48 | + case .fixedSizeArrayType(_, let size): |
| 49 | + return (0..<size).map { index in |
| 50 | + let check = IRRuntimeFunction.isValidCallerCapability(address: "sload(add(\(offset!), \(index)))") |
| 51 | + return "\(variableName) := add(\(variableName), \(check)" |
| 52 | + }.joined(separator: "\n") |
| 53 | + case .arrayType(_): |
| 54 | + let check = IRRuntimeFunction.isCallerCapabilityInArray(arrayOffset: offset!) |
| 55 | + return "\(variableName) := add(\(variableName), \(check))" |
| 56 | + case .basicType(.address): |
| 57 | + let check = IRRuntimeFunction.isValidCallerCapability(address: "sload(\(offset!)))") |
| 58 | + return "\(variableName) := add(\(variableName), \(check)" |
| 59 | + case .basicType(_), .stdlibType(_), .rangeType(_), .dictionaryType(_), .userDefinedType(_), |
| 60 | + .inoutType(_), .functionType(_), .any, .errorType: |
| 61 | + return "" |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + } |
| 68 | + let revertString = revert ? "if eq(\(variableName), 0) { revert(0, 0) }" : "" |
| 69 | + if !checks.isEmpty { |
| 70 | + return """ |
| 71 | + let \(variableName) := 0 |
| 72 | + \(checks.joined(separator: "\n")) |
| 73 | + \(revertString) |
| 74 | + """ |
| 75 | + } |
| 76 | + |
| 77 | + return "" |
| 78 | + } |
| 79 | +} |
0 commit comments