gpt4 book ai didi

java - W/dalvikvm:VFY : invoke type does not match method type of

转载 作者:行者123 更新时间:2023-11-30 09:03:24 26 4
gpt4 key购买 nike

我经常遇到这个错误...尤其是在访问一个类的私有(private)方法时...真的不知道这是怎么回事...我也无法从谷歌中找到太多解释..

我只是在这里发布了一个导致异常的例子:

我有一个 SyncAdapter,这是导致崩溃的 onPerformSync 方法..

    @Override
@SuppressLint("NewApi")
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
// some code...

HmacSHA224 mac = new HmacSHA224(accessToken.getBytes());
JSONArray entries = new JSONArray();
JSONArray hashes = null;
String lastName = null;

// some other code
}

HmacSHA224 是我定义的自定义类,如下所示:

public class HmacSHA224 {

private SHA224 mMessageDigest;
private byte[] mKey;

public HmacSHA224(byte[] key) {
mMessageDigest = new SHA224();
mKey = key;
}

public byte[] hash(byte[] message) {
int blockSize = 64;
int digestSize = 28;
if (mKey.length > blockSize) {
mKey = mMessageDigest.digest(mKey);
}

if (mKey.length < blockSize) {
byte[] k = new byte[blockSize];
System.arraycopy(mKey, 0, k, 0, mKey.length);
mKey = k;
}

byte[] o = new byte[blockSize + digestSize];
byte[] i = new byte[blockSize + message.length];
for (int n = 0; n < blockSize; n++) {
o[n] = (byte)(mKey[n] ^ 0x5c);
i[n] = (byte)(mKey[n] ^ 0x36);
}

System.arraycopy(message, 0, i, blockSize, message.length);
System.arraycopy(mMessageDigest.digest(i), 0, o, blockSize, digestSize);

return mMessageDigest.digest(o);
}

和 SHA224 类:

public final class SHA224 extends MessageDigestSpi {

private static int[] H = { some values };

private static int[] K = { some values };

private ByteBuffer dataBuf = ByteBuffer.allocate(64);
private int length = 0;
private int[] h = H.clone();

private void processChunk() {
dataBuf.rewind();
int[] w = new int[64];
for (int i = 0; i < 16; i++)
w[i] = dataBuf.getInt();
dataBuf.clear();

for (int i = 16; i < 64; i++) {
int s0 = w[i - 15];
s0 = ((s0 >>> 7) | (s0 << 25)) ^ ((s0 >>> 18) | (s0 << 14)) ^ (s0 >>> 3);
int s1 = w[i - 2];
s1 = ((s1 >>> 17) | (s1 << 15)) ^ ((s1 >>> 19) | (s1 << 13)) ^ (s1 >>> 10);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}

int a = this.h[0], b = this.h[1], c = this.h[2], d = this.h[3],
e = this.h[4], f = this.h[5], g = this.h[6], h = this.h[7];

for (int i = 0; i < 64; i++) {
int s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
int maj = (a & b) ^ (a & c) ^ (b & c);
int t2 = s0 + maj;
int s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
int ch = (e & f) ^ ((~e) & g);
int t1 = h + s1 + ch + K[i] + w[i];

h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}

this.h[0] += a;
this.h[1] += b;
this.h[2] += c;
this.h[3] += d;
this.h[4] += e;
this.h[5] += f;
this.h[6] += g;
this.h[7] += h;
}

@Override
protected byte[] engineDigest() {
long bitLength = length * 8L;
engineUpdate((byte) 0x80);
for (; (length + 8) % 64 != 0;)
engineUpdate((byte) 0);
dataBuf.putLong(bitLength);
processChunk();

ByteBuffer result = ByteBuffer.allocate(28);
for (int i = 0; i < 7; i++)
result.putInt(h[i]);

engineReset();
return result.array();
}

@Override
protected void engineReset() {
dataBuf.clear();
length = 0;
h = H.clone();
}

@Override
protected void engineUpdate(byte src) {
dataBuf.put(src);
length += 1;
if (length % 64 == 0)
processChunk();
}

@Override
protected void engineUpdate(byte[] src, int offset, int count) {
for (int i = offset; i < offset + count; i++)
engineUpdate(src[i]);
}

public byte[] digest(byte[] input) {
engineUpdate(input, 0, input.length);
return engineDigest();
}

}

我发现当我启动 mac 值时发生了崩溃.. 错误跟踪到

VFY: rejected Lim/com/example/utils/SHA224;.engineDigest ()

更新:

我有以下日志:

09-03 14:38:59.841  25084-25134/com.example.android:sync W/dalvikvm﹕ VFY: invoke type does not match method type of Lcom/example/android/utils/SHA224;.processChunk
09-03 14:38:59.841 25084-25134/com.example.android:sync W/dalvikvm﹕ VFY: rejecting opcode 0x6f at 0x002b
09-03 14:38:59.841 25084-25134/com.example.android:sync W/dalvikvm﹕ VFY: rejected Lcom/example/android/utils/SHA224;.engineDigest ()[B
09-03 14:38:59.841 25084-25134/com.example.android:sync W/dalvikvm﹕ Verifier rejected class Lcom/example/android/utils/SHA224;
09-03 14:38:59.841 25084-25134/com.example.android:sync W/dalvikvm﹕ threadid=23: thread exiting with uncaught exception (group=0x415daba8)
09-03 14:38:59.841 25084-25134/com.example.android:sync W/System.err﹕ java.lang.VerifyError: com/example/android/utils/SHA224
09-03 14:38:59.841 25084-25134/com.example.android:sync W/System.err﹕ at com.example.android.utils.HmacSHA224.<init>(HmacSHA224.java:5)
09-03 14:38:59.841 25084-25134/com.example.android:sync W/System.err﹕ at com.example.android.adapters.SyncAdapter.onPerformSync(SyncAdapter.java:71)
09-03 14:38:59.841 25084-25134/com.example.android:sync W/System.err﹕ at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)
09-03 14:38:59.851 25084-25134/com.example.android:sync E/AndroidRuntime﹕ FATAL EXCEPTION: SyncAdapterThread-1
Process: com.example.android:sync, PID: 25084
java.lang.VerifyError: com/example/android/utils/SHA224
at com.example.android.utils.HmacSHA224.<init>(HmacSHA224.java:5)
at com.example.android.adapters.SyncAdapter.onPerformSync(SyncAdapter.java:71)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)

再次更新所以问题出在 processChunk() 方法上,当更改为 protected (而不是私有(private))时,崩溃消失了。但!!!我想知道为什么它会这样......为什么我不能将某些方法保持私有(private)并在同一个类中调用......

最佳答案

对于为什么会发生这种情况还没有明确的答案,但这是我对正在发生的事情和调查方向的分析:

日志显示类 SHA224 的字节码验证在尝试将其加载到 VM 内存时失败:

Verifier rejected class Lcom/example/android/utils/SHA224;

细节告诉我们,验证失败是因为调用了processChunk()的父类实现,MessageDigestSpi父类中不存在:

VFY: invoke type does not match method type of Lcom/example/android/utils/SHA224;.processChunk
VFY: rejecting opcode 0x6f at 0x002b
VFY: rejected Lcom/example/android/utils/SHA224;.engineDigest ()[B

(操作码0x6F 用于调用 super 方法on Dalvik)

所以看起来您的代码中对 processChunk() 的调用被编译为 super.processChunk(),这自然无法在加载时解析。为什么,我不能说,这可能是构建工具链中的一个错误,特别是因为你说只要将 processChunk() 方法范围更改为 protected 就可以正确编译它.

进一步尝试调查的事情:

  • 尝试调用 this.processChunk() 而不是 processChunk()(这应该是相同的但会提示编译器问题)
  • 尝试使用最新的构建工具针对稳定的 API 版本(例如 19)进行编译,并在稳定的 Android 运行时(例如 4.4)上运行它

我在我的环境中尝试了您的代码(SDK 工具 23.0.2,平台工具 20,目标最低 API 8,在 4.4/Dalvik 上运行)我没有遇到这个问题,即使输入了摘要代码在单独的进程中运行的同步适配器。所以我肯定会检查您的建筑环境。


这不会解决问题的根源,但对于 Android 上的哈希,您宁愿使用 native API :

MessageDigest sha224 = MessageDigest.getInstance("SHA-224");
// etc

它会在不提供 SHA-224 哈希算法的设备上失败...

关于java - W/dalvikvm:VFY : invoke type does not match method type of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25639449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com