Skip to content

Commit c43d215

Browse files
committed
[fix] fixed concrete invoke
- added forbidden externs
1 parent b2e5804 commit c43d215

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

VSharp.IL/Loader.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ module Loader =
204204
let isInvocationForbidden fullMethodName =
205205
Set.contains fullMethodName invocationForbidden
206206

207+
let private externInvocationForbidden : Set<DllManager.dllImportInfo> =
208+
set [
209+
{ dllName = "libc"; entryPoint = "rand" }
210+
{ dllName = "msvcrt"; entryPoint = "rand" }
211+
]
212+
213+
let isExternInvocationForbidden dllImportInfo =
214+
Set.contains dllImportInfo externInvocationForbidden
215+
207216
let private concreteInvocations =
208217
set [
209218
// Types

VSharp.IL/MethodBody.fs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,19 @@ type MethodWithBody internal (m : MethodBase) =
6262
int (m.GetMethodImplementationFlags() &&& MethodImplAttributes.InternalCall) <> 0
6363
|| DllManager.isQCall m
6464
)
65+
let isExternalCall = lazy Reflection.isExternalMethod m
6566

66-
let invocationForbidden = lazy Loader.isInvocationForbidden fullGenericMethodName.Value
67+
let externInvocationForbidden = lazy(
68+
match DllManager.parseDllImport m with
69+
| Some info -> Loader.isExternInvocationForbidden info
70+
| None -> false
71+
)
72+
73+
let invocationForbidden =
74+
lazy (
75+
Loader.isInvocationForbidden fullGenericMethodName.Value
76+
|| isExternalCall.Value && externInvocationForbidden.Value
77+
)
6778

6879
let shouldAnalyseInvokable = lazy not invocationForbidden.Value
6980

@@ -217,7 +228,7 @@ type MethodWithBody internal (m : MethodBase) =
217228

218229
member x.IsConcretelyInvokable with get () = isConcretelyInvokable.Value
219230

220-
member x.IsExternalMethod with get() = Reflection.isExternalMethod m
231+
member x.IsExternalMethod with get() = isExternalCall.Value
221232
member x.IsQCall with get() = DllManager.isQCall m
222233

223234
member x.HasNonVoidResult = hasNonVoidResult.Value

VSharp.Utils/ExternMocking.fs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,27 @@ exception UnexpectedExternCallException of string
1212

1313
module DllManager =
1414

15+
type dllImportInfo =
16+
{
17+
dllName : string
18+
entryPoint : string
19+
}
20+
1521
let private dllImportAttribute = lazy AssemblyManager.NormalizeType(typeof<DllImportAttribute>)
1622

1723
let private getDllImportNameAndEntry (attr : Attribute) =
1824
let dllNameField = dllImportAttribute.Value.GetField("dllName")
1925
let entryPointField = dllImportAttribute.Value.GetField("EntryPoint")
20-
dllNameField.GetValue(attr) :?> string, entryPointField.GetValue(attr) :?> string
26+
let dllName = dllNameField.GetValue(attr) :?> string
27+
let entryPoint = entryPointField.GetValue(attr) :?> string
28+
{ dllName = dllName; entryPoint = entryPoint }
2129

2230
let parseDllImport (m : MethodBase) =
2331
// Case for assembly, loaded via default load context (F# internal calls)
2432
let findViaDefault (attr : Attribute) =
2533
match attr with
2634
| :? DllImportAttribute as attr ->
27-
Some (attr.Value, attr.EntryPoint)
35+
Some { dllName = attr.Value; entryPoint = attr.EntryPoint }
2836
| _ -> None
2937

3038
// Case for assembly, loaded via VSharp load context (C# internal calls)
@@ -36,7 +44,7 @@ module DllManager =
3644

3745
let isQCall (m : MethodBase) =
3846
match parseDllImport m with
39-
| Some(libName, _) -> libName.Equals("QCall")
47+
| Some { dllName = libName } -> libName.Equals("QCall")
4048
| None -> false
4149

4250
module ExtMocking =
@@ -168,11 +176,11 @@ module ExtMocking =
168176
let methodToPatch = mockType.MockedMethod
169177
let ptrFrom =
170178
if Reflection.isExternalMethod methodToPatch then
171-
let libName, methodName =
179+
let dllImportInfo =
172180
match DllManager.parseDllImport methodToPatch with
173181
| Some p -> p
174182
| None -> internalfail "External method without DllImport attribute"
175-
ExternMocker.GetExternPtr(libName, methodName)
183+
ExternMocker.GetExternPtr(dllImportInfo.dllName, dllImportInfo.entryPoint)
176184
else
177185
methodToPatch.MethodHandle.GetFunctionPointer()
178186

0 commit comments

Comments
 (0)