Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wasm-watermarker
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nagayama15
wasm-watermarker
Commits
dc4af29c
Commit
dc4af29c
authored
Jul 14, 2019
by
nagayama15
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🚧
Change the return value of the visitor
parent
d428484f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
149 additions
and
92 deletions
+149
-92
OperandSwappingWatermarker.cpp
src/kyut/watermarker/OperandSwappingWatermarker.cpp
+149
-92
No files found.
src/kyut/watermarker/OperandSwappingWatermarker.cpp
View file @
dc4af29c
...
...
@@ -45,175 +45,232 @@ namespace kyut::watermarker {
EXPR_TYPE(MemoryCopy) \
EXPR_TYPE(MemoryFill)
enum
class
SideEffect
:
std
::
uint32_t
{
none
=
0
,
readOnly
=
1
,
write
=
2
,
};
// Watermark embedder
std
::
size_
t
embedExpression
(
wasm
::
Expression
*
expr
,
CircularBitStreamReader
&
stream
);
SideEffec
t
embedExpression
(
wasm
::
Expression
*
expr
,
CircularBitStreamReader
&
stream
);
std
::
size_
t
embedExpressionList
(
const
wasm
::
ExpressionList
&
exprs
,
CircularBitStreamReader
&
stream
)
{
std
::
size_t
numBits
=
0
;
SideEffec
t
embedExpressionList
(
const
wasm
::
ExpressionList
&
exprs
,
CircularBitStreamReader
&
stream
)
{
auto
effect
=
SideEffect
::
none
;
for
(
const
auto
expr
:
exprs
)
{
numBits
+=
embedExpression
(
expr
,
stream
);
effect
=
(
std
::
max
)(
embedExpression
(
expr
,
stream
),
effect
);
}
return
numBits
;
return
effect
;
}
std
::
size_
t
embedBlock
(
wasm
::
Block
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedBlock
(
wasm
::
Block
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpressionList
(
expr
.
list
,
stream
);
}
std
::
size_t
embedIf
(
wasm
::
If
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
condition
,
stream
)
+
embedExpression
(
expr
.
ifTrue
,
stream
)
+
embedExpression
(
expr
.
ifFalse
,
stream
);
SideEffect
embedIf
(
wasm
::
If
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)({
embedExpression
(
expr
.
condition
,
stream
),
embedExpression
(
expr
.
ifTrue
,
stream
),
embedExpression
(
expr
.
ifFalse
,
stream
),
});
}
std
::
size_
t
embedLoop
(
wasm
::
Loop
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedLoop
(
wasm
::
Loop
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
body
,
stream
);
}
std
::
size_t
embedBreak
(
wasm
::
Break
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
)
+
embedExpression
(
expr
.
condition
,
stream
);
SideEffect
embedBreak
(
wasm
::
Break
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
value
,
stream
);
embedExpression
(
expr
.
condition
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedSwitch
(
wasm
::
Switch
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
condition
,
stream
)
+
embedExpression
(
expr
.
value
,
stream
);
SideEffec
t
embedSwitch
(
wasm
::
Switch
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)(
embedExpression
(
expr
.
condition
,
stream
),
embedExpression
(
expr
.
value
,
stream
)
);
}
std
::
size_t
embedCall
(
wasm
::
Call
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpressionList
(
expr
.
operands
,
stream
);
SideEffect
embedCall
(
wasm
::
Call
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpressionList
(
expr
.
operands
,
stream
);
// It is difficult to estimate the side effects of the function calls
return
SideEffect
::
write
;
}
std
::
size_t
embedCallIndirect
(
wasm
::
CallIndirect
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
target
,
stream
)
+
embedExpressionList
(
expr
.
operands
,
stream
);
SideEffect
embedCallIndirect
(
wasm
::
CallIndirect
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
target
,
stream
);
embedExpressionList
(
expr
.
operands
,
stream
);
// It is difficult to estimate the side effects of the function calls
return
SideEffect
::
write
;
}
std
::
size_
t
embedGetLocal
([[
maybe_unused
]]
wasm
::
GetLocal
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedGetLocal
([[
maybe_unused
]]
wasm
::
GetLocal
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
readOnly
;
}
std
::
size_t
embedSetLocal
(
wasm
::
SetLocal
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
);
SideEffect
embedSetLocal
(
wasm
::
SetLocal
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
value
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedGetGlobal
([[
maybe_unused
]]
wasm
::
GetGlobal
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedGetGlobal
([[
maybe_unused
]]
wasm
::
GetGlobal
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
readOnly
;
}
std
::
size_t
embedSetGlobal
(
wasm
::
SetGlobal
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
);
SideEffect
embedSetGlobal
(
wasm
::
SetGlobal
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
value
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedLoad
(
wasm
::
Load
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
);
SideEffec
t
embedLoad
(
wasm
::
Load
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)(
embedExpression
(
expr
.
ptr
,
stream
),
SideEffect
::
readOnly
);
}
std
::
size_t
embedStore
(
wasm
::
Store
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
)
+
embedExpression
(
expr
.
value
,
stream
);
SideEffect
embedStore
(
wasm
::
Store
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
ptr
,
stream
);
embedExpression
(
expr
.
value
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedConst
([[
maybe_unused
]]
wasm
::
Const
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedConst
([[
maybe_unused
]]
wasm
::
Const
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
none
;
}
std
::
size_
t
embedUnary
(
wasm
::
Unary
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedUnary
(
wasm
::
Unary
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
);
}
std
::
size_
t
embedBinary
(
wasm
::
Binary
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedBinary
(
wasm
::
Binary
&
expr
,
CircularBitStreamReader
&
stream
)
{
// TODO: implement watermarking
return
embedExpression
(
expr
.
left
,
stream
)
+
embedExpression
(
expr
.
right
,
stream
);
return
(
std
::
max
)(
embedExpression
(
expr
.
left
,
stream
),
embedExpression
(
expr
.
right
,
stream
)
);
}
std
::
size_t
embedSelect
(
wasm
::
Select
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
condition
,
stream
)
+
embedExpression
(
expr
.
ifTrue
,
stream
)
+
embedExpression
(
expr
.
ifFalse
,
stream
);
SideEffect
embedSelect
(
wasm
::
Select
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)({
embedExpression
(
expr
.
condition
,
stream
),
embedExpression
(
expr
.
ifTrue
,
stream
),
embedExpression
(
expr
.
ifFalse
,
stream
),
});
}
std
::
size_
t
embedDrop
(
wasm
::
Drop
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedDrop
(
wasm
::
Drop
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
);
}
std
::
size_t
embedReturn
(
wasm
::
Return
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
value
,
stream
);
SideEffect
embedReturn
(
wasm
::
Return
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
value
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_t
embedHost
(
wasm
::
Host
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpressionList
(
expr
.
operands
,
stream
);
SideEffect
embedHost
(
wasm
::
Host
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpressionList
(
expr
.
operands
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedNop
([[
maybe_unused
]]
wasm
::
Nop
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedNop
([[
maybe_unused
]]
wasm
::
Nop
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
none
;
}
std
::
size_
t
embedUnreachable
([[
maybe_unused
]]
wasm
::
Unreachable
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedUnreachable
([[
maybe_unused
]]
wasm
::
Unreachable
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
write
;
}
std
::
size_t
embedAtomicRMW
(
wasm
::
AtomicRMW
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
)
+
embedExpression
(
expr
.
value
,
stream
);
SideEffect
embedAtomicRMW
(
wasm
::
AtomicRMW
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
ptr
,
stream
);
embedExpression
(
expr
.
value
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_t
embedAtomicCmpxchg
(
wasm
::
AtomicCmpxchg
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
)
+
embedExpression
(
expr
.
expected
,
stream
)
+
embedExpression
(
expr
.
replacement
,
stream
);
SideEffect
embedAtomicCmpxchg
(
wasm
::
AtomicCmpxchg
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
ptr
,
stream
);
embedExpression
(
expr
.
expected
,
stream
);
embedExpression
(
expr
.
replacement
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_t
embedAtomicWait
(
wasm
::
AtomicWait
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
)
+
embedExpression
(
expr
.
expected
,
stream
)
+
embedExpression
(
expr
.
timeout
,
stream
);
SideEffect
embedAtomicWait
(
wasm
::
AtomicWait
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
ptr
,
stream
);
embedExpression
(
expr
.
expected
,
stream
);
embedExpression
(
expr
.
timeout
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_t
embedAtomicNotify
(
wasm
::
AtomicNotify
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
ptr
,
stream
)
+
embedExpression
(
expr
.
notifyCount
,
stream
);
SideEffect
embedAtomicNotify
(
wasm
::
AtomicNotify
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
ptr
,
stream
);
embedExpression
(
expr
.
notifyCount
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedSIMDExtract
(
wasm
::
SIMDExtract
&
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedSIMDExtract
(
wasm
::
SIMDExtract
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
vec
,
stream
);
}
std
::
size_
t
embedSIMDReplace
(
wasm
::
SIMDReplace
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
vec
,
stream
)
+
embedExpression
(
expr
.
value
,
stream
);
SideEffec
t
embedSIMDReplace
(
wasm
::
SIMDReplace
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)(
embedExpression
(
expr
.
vec
,
stream
),
embedExpression
(
expr
.
value
,
stream
)
);
}
std
::
size_
t
embedSIMDShuffle
(
wasm
::
SIMDShuffle
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
left
,
stream
)
+
embedExpression
(
expr
.
right
,
stream
);
SideEffec
t
embedSIMDShuffle
(
wasm
::
SIMDShuffle
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)(
embedExpression
(
expr
.
left
,
stream
),
embedExpression
(
expr
.
right
,
stream
)
);
}
std
::
size_t
embedSIMDBitselect
(
wasm
::
SIMDBitselect
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
cond
,
stream
)
+
embedExpression
(
expr
.
left
,
stream
)
+
embedExpression
(
expr
.
right
,
stream
);
SideEffect
embedSIMDBitselect
(
wasm
::
SIMDBitselect
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)({
embedExpression
(
expr
.
cond
,
stream
),
embedExpression
(
expr
.
left
,
stream
),
embedExpression
(
expr
.
right
,
stream
),
});
}
std
::
size_
t
embedSIMDShift
(
wasm
::
SIMDShift
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
vec
,
stream
)
+
embedExpression
(
expr
.
shift
,
stream
);
SideEffec
t
embedSIMDShift
(
wasm
::
SIMDShift
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
(
std
::
max
)(
embedExpression
(
expr
.
vec
,
stream
),
embedExpression
(
expr
.
shift
,
stream
)
);
}
std
::
size_t
embedMemoryInit
(
wasm
::
MemoryInit
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
dest
,
stream
)
+
embedExpression
(
expr
.
offset
,
stream
)
+
embedExpression
(
expr
.
size
,
stream
);
SideEffect
embedMemoryInit
(
wasm
::
MemoryInit
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
dest
,
stream
);
embedExpression
(
expr
.
offset
,
stream
);
embedExpression
(
expr
.
size
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedDataDrop
([[
maybe_unused
]]
wasm
::
DataDrop
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
0
;
SideEffec
t
embedDataDrop
([[
maybe_unused
]]
wasm
::
DataDrop
&
expr
,
[[
maybe_unused
]]
CircularBitStreamReader
&
stream
)
{
return
SideEffect
::
write
;
}
std
::
size_t
embedMemoryCopy
(
wasm
::
MemoryCopy
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
dest
,
stream
)
+
embedExpression
(
expr
.
source
,
stream
)
+
embedExpression
(
expr
.
size
,
stream
);
SideEffect
embedMemoryCopy
(
wasm
::
MemoryCopy
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
dest
,
stream
);
embedExpression
(
expr
.
source
,
stream
);
embedExpression
(
expr
.
size
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_t
embedMemoryFill
(
wasm
::
MemoryFill
&
expr
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
expr
.
dest
,
stream
)
+
embedExpression
(
expr
.
value
,
stream
)
+
embedExpression
(
expr
.
size
,
stream
);
SideEffect
embedMemoryFill
(
wasm
::
MemoryFill
&
expr
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
expr
.
dest
,
stream
);
embedExpression
(
expr
.
value
,
stream
);
embedExpression
(
expr
.
size
,
stream
);
return
SideEffect
::
write
;
}
std
::
size_
t
embedExpression
(
wasm
::
Expression
*
expr
,
CircularBitStreamReader
&
stream
)
{
SideEffec
t
embedExpression
(
wasm
::
Expression
*
expr
,
CircularBitStreamReader
&
stream
)
{
if
(
expr
==
nullptr
)
{
return
0
;
return
SideEffect
::
none
;
}
switch
(
expr
->
_id
)
{
...
...
@@ -228,8 +285,8 @@ namespace kyut::watermarker {
}
}
std
::
size_t
embedFunction
(
wasm
::
Function
&
function
,
CircularBitStreamReader
&
stream
)
{
return
embedExpression
(
function
.
body
,
stream
);
void
embedFunction
(
wasm
::
Function
&
function
,
CircularBitStreamReader
&
stream
)
{
embedExpression
(
function
.
body
,
stream
);
}
}
// namespace
...
...
@@ -246,20 +303,20 @@ namespace kyut::watermarker {
std
::
sort
(
std
::
begin
(
functions
),
std
::
end
(
functions
),
[](
const
auto
a
,
const
auto
b
)
{
return
a
->
name
<
b
->
name
;
});
// Number of bits embedded in the module
std
::
size_t
numBits
=
0
;
// Embed watermarks
std
::
size_t
posStart
=
stream
.
tell
();
for
(
const
auto
f
:
functions
)
{
numBits
+=
embedFunction
(
*
f
,
stream
);
embedFunction
(
*
f
,
stream
);
}
return
numBits
;
return
stream
.
tell
()
-
posStart
;
}
std
::
size_t
extractOperandSwapping
(
wasm
::
Module
&
module
,
BitStreamWriter
&
stream
)
{
(
void
)
module
;
(
void
)
stream
;
return
0
;
WASM_UNREACHABLE
();
}
}
// namespace kyut::watermarker
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment