Skip to content

Commit 681bb79

Browse files
committed
Fixed value assignment in Lua 5.2 with NaN trick
1 parent 823c519 commit 681bb79

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

LuaDkmDebuggerComponent/Bytecode.cs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ internal static bool HasIntegerNumberExtendedType()
138138
if (value == null)
139139
return null;
140140

141+
tagAddress = address + Schema.LuaValueData.typeAddress.GetValueOrDefault(0);
142+
141143
if (double.IsNaN(value.Value))
142144
{
143-
tagAddress = address + Schema.LuaValueData.typeAddress.GetValueOrDefault(0);
144145
typeTag = DebugHelpers.ReadIntVariable(process, tagAddress, batch);
145146
}
146147
else
@@ -164,9 +165,10 @@ internal static bool HasIntegerNumberExtendedType()
164165
if (value == null)
165166
return null;
166167

168+
tagAddress = address + (ulong)DebugHelpers.GetPointerSize(process);
169+
167170
if (double.IsNaN(value.Value))
168171
{
169-
tagAddress = address + (ulong)DebugHelpers.GetPointerSize(process);
170172
typeTag = DebugHelpers.ReadIntVariable(process, tagAddress, batch);
171173
}
172174
else
@@ -632,6 +634,37 @@ internal static LuaValueDataBase ReadValueOfType(DkmProcess process, int typeTag
632634
return null;
633635
}
634636

637+
internal static bool WriteTypeTag(DkmProcess process, ulong tagAddress, int tagValue)
638+
{
639+
if (Schema.LuaValueData.available)
640+
{
641+
// Handle NAN trick
642+
if (Schema.LuaValueData.doubleAddress.HasValue)
643+
{
644+
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, tagValue | 0x7FF7A500))
645+
return false;
646+
}
647+
else
648+
{
649+
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, tagValue))
650+
return false;
651+
}
652+
}
653+
else if (luaVersion == 502 && !DebugHelpers.Is64Bit(process))
654+
{
655+
// union { struct { Value v__; int tt__; } i; double d__; } u
656+
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, tagValue | 0x7FF7A500)) // Handle NAN trick
657+
return false;
658+
}
659+
else
660+
{
661+
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, tagValue))
662+
return false;
663+
}
664+
665+
return true;
666+
}
667+
635668
internal static bool TryWriteValue(DkmProcess process, DkmStackWalkFrame stackFrame, DkmInspectionSession inspectionSession, ulong tagAddress, ulong valueAddress, LuaValueDataBase value, out string errorText)
636669
{
637670
if (tagAddress == 0 || valueAddress == 0)
@@ -648,7 +681,7 @@ bool Failed(string text, out string errorText_)
648681

649682
if (value is LuaValueDataNil)
650683
{
651-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)LuaExtendedType.Nil))
684+
if (!WriteTypeTag(process, tagAddress, (int)LuaExtendedType.Nil))
652685
return Failed("Failed to modify target process memory (tag)", out errorText);
653686

654687
if (!DebugHelpers.TryWriteIntVariable(process, valueAddress, 0))
@@ -661,15 +694,15 @@ bool Failed(string text, out string errorText_)
661694
{
662695
if (LuaHelpers.luaVersion == 504)
663696
{
664-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)(sourceBool.value ? LuaExtendedType.BooleanTrue : LuaExtendedType.Boolean)))
697+
if (!WriteTypeTag(process, tagAddress, (int)(sourceBool.value ? LuaExtendedType.BooleanTrue : LuaExtendedType.Boolean)))
665698
return Failed("Failed to modify target process memory (tag)", out errorText);
666699

667700
if (!DebugHelpers.TryWriteIntVariable(process, valueAddress, 0))
668701
return Failed("Failed to modify target process memory (value)", out errorText);
669702
}
670703
else
671704
{
672-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)LuaExtendedType.Boolean))
705+
if (!WriteTypeTag(process, tagAddress, (int)LuaExtendedType.Boolean))
673706
return Failed("Failed to modify target process memory (tag)", out errorText);
674707

675708
if (!DebugHelpers.TryWriteIntVariable(process, valueAddress, sourceBool.value ? 1 : 0))
@@ -684,7 +717,7 @@ bool Failed(string text, out string errorText_)
684717
if (sourceNumber.extendedType == GetFloatNumberExtendedType() || !LuaHelpers.HasIntegerNumberExtendedType())
685718
{
686719
// Write tag first here, unioned value will go over it when neccessary
687-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)GetFloatNumberExtendedType()))
720+
if (!WriteTypeTag(process, tagAddress, (int)GetFloatNumberExtendedType()))
688721
return Failed("Failed to modify target process memory (tag)", out errorText);
689722

690723
if (!DebugHelpers.TryWriteDoubleVariable(process, valueAddress, sourceNumber.value))
@@ -696,7 +729,7 @@ bool Failed(string text, out string errorText_)
696729

697730
if (sourceNumber.extendedType == GetIntegerNumberExtendedType() && LuaHelpers.HasIntegerNumberExtendedType())
698731
{
699-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)GetIntegerNumberExtendedType()))
732+
if (!WriteTypeTag(process, tagAddress, (int)GetIntegerNumberExtendedType()))
700733
return Failed("Failed to modify target process memory (tag)", out errorText);
701734

702735
if (!DebugHelpers.TryWriteIntVariable(process, valueAddress, (int)sourceNumber.value))
@@ -742,15 +775,15 @@ bool Failed(string text, out string errorText_)
742775
if (!registryAddress.HasValue)
743776
return Failed("Failed to create Lua string value", out errorText);
744777

745-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)value.extendedType))
778+
if (!WriteTypeTag(process, tagAddress, (int)value.extendedType))
746779
return Failed("Failed to modify target process memory (tag)", out errorText);
747780

748781
if (!DebugHelpers.TryWritePointerVariable(process, valueAddress, registryAddress.Value))
749782
return Failed("Failed to modify target process memory (value)", out errorText);
750783
}
751784
else
752785
{
753-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)value.extendedType))
786+
if (!WriteTypeTag(process, tagAddress, (int)value.extendedType))
754787
return Failed("Failed to modify target process memory (tag)", out errorText);
755788

756789
ulong luaStringOffset = LuaHelpers.GetStringDataOffset(process);
@@ -785,7 +818,7 @@ bool Failed(string text, out string errorText_)
785818

786819
bool collectable = LuaHelpers.luaVersion == 501 ? false : (value is LuaValueDataTable || value is LuaValueDataLuaFunction || value is LuaValueDataExternalClosure || value is LuaValueDataUserData || value is LuaValueDataThread);
787820

788-
if (!DebugHelpers.TryWriteIntVariable(process, tagAddress, (int)value.extendedType + (collectable ? 64 : 0)))
821+
if (!WriteTypeTag(process, tagAddress, (int)value.extendedType + (collectable ? 64 : 0)))
789822
return Failed("Failed to modify target process memory (tag)", out errorText);
790823

791824
if (!DebugHelpers.TryWritePointerVariable(process, valueAddress, targetAddress))

0 commit comments

Comments
 (0)